MIRROR: javascript for ๐Ÿœ's, a tiny runtime with big ambitions
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

toStringTag

+158 -5
+1 -1
meson.build
··· 74 74 build_date = run_command('date', '+%Y-%m-%d', check: true).stdout().strip() 75 75 76 76 version_conf = configuration_data() 77 - version_conf.set('ANT_VERSION', '0.0.8.4') 77 + version_conf.set('ANT_VERSION', '0.0.8.5') 78 78 version_conf.set('ANT_GIT_HASH', git_hash) 79 79 version_conf.set('ANT_BUILD_DATE', build_date) 80 80
+60
src/ant.c
··· 2533 2533 if (vtype(proto) == T_NULL || vtype(proto) == T_UNDEF) break; 2534 2534 cur = proto; 2535 2535 depth++; 2536 + } else if (t == T_CFUNC) { 2537 + jsval_t func_proto = get_ctor_proto(js, "Function", 8); 2538 + if (vtype(func_proto) == T_OBJ || vtype(func_proto) == T_ARR || vtype(func_proto) == T_FUNC) { 2539 + jsval_t as_obj = (vtype(func_proto) == T_OBJ) ? func_proto : mkval(T_OBJ, vdata(func_proto)); 2540 + jsoff_t off = lkp(js, as_obj, key, len); 2541 + if (off != 0) return off; 2542 + } 2543 + break; 2536 2544 } 2537 2545 else break; 2538 2546 } ··· 2819 2827 jsval_t key = js_mkstr(js, ptr, plen); 2820 2828 jsval_t prop = setprop(js, func_obj, key, js_mkundef()); 2821 2829 return prop; 2830 + } 2831 + 2832 + if (t == T_CFUNC) { 2833 + jsoff_t off = lkp_proto(js, l, ptr, plen); 2834 + if (off != 0) return resolveprop(js, mkval(T_PROP, off)); 2835 + if (streq(ptr, plen, "name", 4)) return js_mkstr(js, "", 0); 2836 + return js_mkundef(); 2822 2837 } 2823 2838 2824 2839 if (t != T_OBJ && t != T_ARR) { ··· 7781 7796 return obj; 7782 7797 } 7783 7798 7799 + static jsval_t builtin_object_toString(struct js *js, jsval_t *args, int nargs) { 7800 + (void)args; (void)nargs; 7801 + jsval_t obj = js->this_val; 7802 + 7803 + obj = resolveprop(js, obj); 7804 + uint8_t t = vtype(obj); 7805 + 7806 + if (t == T_OBJ || t == T_ARR || t == T_FUNC) { 7807 + jsval_t check_obj = (t == T_FUNC) ? mkval(T_OBJ, vdata(obj)) : obj; 7808 + jsoff_t tag_off = lkp(js, check_obj, "@@toStringTag", 13); 7809 + if (tag_off != 0) { 7810 + jsval_t tag_val = resolveprop(js, mkval(T_PROP, tag_off)); 7811 + if (vtype(tag_val) == T_STR) { 7812 + jsoff_t tag_len, tag_str_off = vstr(js, tag_val, &tag_len); 7813 + const char *tag_str = (const char *)&js->mem[tag_str_off]; 7814 + 7815 + char buf[256]; 7816 + int n = snprintf(buf, sizeof(buf), "[object %.*s]", (int)tag_len, tag_str); 7817 + return js_mkstr(js, buf, n); 7818 + } 7819 + } 7820 + } 7821 + 7822 + const char *type_name = NULL; 7823 + 7824 + switch (t) { 7825 + case T_UNDEF: type_name = "Undefined"; break; 7826 + case T_NULL: type_name = "Null"; break; 7827 + case T_BOOL: type_name = "Boolean"; break; 7828 + case T_NUM: type_name = "Number"; break; 7829 + case T_STR: type_name = "String"; break; 7830 + case T_ARR: type_name = "Array"; break; 7831 + case T_FUNC: type_name = "Function"; break; 7832 + case T_ERR: type_name = "Error"; break; 7833 + case T_BIGINT: type_name = "BigInt"; break; 7834 + case T_OBJ: type_name = "Object"; break; 7835 + default: type_name = "Unknown"; break; 7836 + } 7837 + 7838 + char buf[256]; 7839 + int n = snprintf(buf, sizeof(buf), "[object %s]", type_name); 7840 + return js_mkstr(js, buf, n); 7841 + } 7842 + 7784 7843 static jsval_t builtin_array_push(struct js *js, jsval_t *args, int nargs) { 7785 7844 jsval_t arr = js->this_val; 7786 7845 arr = resolveprop(js, arr); ··· 10576 10635 10577 10636 jsval_t glob = js->scope; 10578 10637 jsval_t object_proto = js_mkobj(js); 10638 + setprop(js, object_proto, js_mkstr(js, "toString", 8), js_mkfun(builtin_object_toString)); 10579 10639 10580 10640 jsval_t function_proto = js_mkobj(js); 10581 10641 set_proto(js, function_proto, object_proto);
+1
src/modules/buffer.c
··· 750 750 jsval_t buffer_obj = js_mkobj(js); 751 751 js_set(js, buffer_obj, "from", js_mkfun(js_buffer_from)); 752 752 js_set(js, buffer_obj, "alloc", js_mkfun(js_buffer_alloc)); 753 + js_set(js, buffer_obj, "@@toStringTag", js_mkstr(js, "Buffer", 6)); 753 754 js_set(js, glob, "Buffer", buffer_obj); 754 755 }
+3 -1
src/modules/crypto.c
··· 134 134 jsval_t ant_obj = rt->ant_obj; 135 135 136 136 jsval_t crypto_obj = js_mkobj(js); 137 - js_set(js, ant_obj, "Crypto", crypto_obj); 138 137 139 138 js_set(js, crypto_obj, "random", js_mkfun(js_crypto_random)); 140 139 js_set(js, crypto_obj, "randomBytes", js_mkfun(js_crypto_random_bytes)); 141 140 js_set(js, crypto_obj, "randomUUID", js_mkfun(js_crypto_random_uuid)); 142 141 js_set(js, crypto_obj, "randomUUIDv7", js_mkfun(js_crypto_random_uuidv7)); 142 + 143 + js_set(js, crypto_obj, "@@toStringTag", js_mkstr(js, "Crypto", 6)); 144 + js_set(js, ant_obj, "Crypto", crypto_obj); 143 145 } 144 146
+1
src/modules/ffi.c
··· 125 125 js_set(js, ffi_types, "string", js_mkstr(js, "string", 6)); 126 126 js_set(js, ffi_types, "spread", js_mkstr(js, "...", 3)); 127 127 js_set(js, ffi_obj, "FFIType", ffi_types); 128 + js_set(js, ffi_obj, "@@toStringTag", js_mkstr(js, "FFI", 3)); 128 129 129 130 return ffi_obj; 130 131 }
+1
src/modules/fs.c
··· 725 725 js_set(js, lib, "rmdirSync", js_mkfun(builtin_fs_rmdirSync)); 726 726 js_set(js, lib, "stat", js_mkfun(builtin_fs_stat)); 727 727 js_set(js, lib, "statSync", js_mkfun(builtin_fs_statSync)); 728 + js_set(js, lib, "@@toStringTag", js_mkstr(js, "fs", 2)); 728 729 729 730 return lib; 730 731 }
+3 -1
src/modules/io.c
··· 192 192 struct js *js = rt->js; 193 193 jsval_t console_obj = js_mkobj(js); 194 194 195 - js_set(js, js_glob(js), "console", console_obj); 196 195 js_set(js, console_obj, "log", js_mkfun(js_console_log)); 197 196 js_set(js, console_obj, "error", js_mkfun(js_console_error)); 198 197 js_set(js, console_obj, "warn", js_mkfun(js_console_warn)); 199 198 js_set(js, console_obj, "assert", js_mkfun(js_console_assert)); 200 199 js_set(js, console_obj, "trace", js_mkfun(js_console_trace)); 200 + 201 + js_set(js, console_obj, "@@toStringTag", js_mkstr(js, "console", 7)); 202 + js_set(js, js_glob(js), "console", console_obj); 201 203 }
+3 -1
src/modules/json.c
··· 215 215 struct js *js = rt->js; 216 216 jsval_t json_obj = js_mkobj(js); 217 217 218 - js_set(js, js_glob(js), "JSON", json_obj); 219 218 js_set(js, json_obj, "parse", js_mkfun(js_json_parse)); 220 219 js_set(js, json_obj, "stringify", js_mkfun(js_json_stringify)); 220 + 221 + js_set(js, json_obj, "@@toStringTag", js_mkstr(js, "JSON", 4)); 222 + js_set(js, js_glob(js), "JSON", json_obj); 221 223 }
+1
src/modules/path.c
··· 411 411 js_set(js, lib, "sep", js_mkstr(js, PATH_SEP_STR, 1)); 412 412 char delimiter_str[2] = {PATH_DELIMITER, '\0'}; 413 413 js_set(js, lib, "delimiter", js_mkstr(js, delimiter_str, 1)); 414 + js_set(js, lib, "@@toStringTag", js_mkstr(js, "path", 4)); 414 415 415 416 return lib; 416 417 }
+3 -1
src/modules/process.c
··· 93 93 jsval_t process_obj = js_mkobj(js); 94 94 jsval_t env_obj = js_mkobj(js); 95 95 96 - js_set(js, js_glob(js), "process", process_obj); 97 96 js_set(js, process_obj, "env", env_obj); 98 97 js_set(js, process_obj, "exit", js_mkfun(process_exit)); 99 98 ··· 128 127 129 128 load_dotenv_file(js, env_obj); 130 129 js_set_getter(js, env_obj, env_getter); 130 + 131 + js_set(js, process_obj, "@@toStringTag", js_mkstr(js, "process", 7)); 132 + js_set(js, js_glob(js), "process", process_obj); 131 133 }
+2
src/modules/shell.c
··· 215 215 216 216 jsval_t shell_library(struct js *js) { 217 217 jsval_t lib = js_mkobj(js); 218 + 218 219 js_set(js, lib, "$", js_mkfun(builtin_shell_dollar)); 220 + js_set(js, lib, "@@toStringTag", js_mkstr(js, "shell", 5)); 219 221 220 222 return lib; 221 223 }
+34
tests/test_all_modules.cjs
··· 1 + // Test toStringTag for all modules 2 + 3 + console.log("Testing @@toStringTag for all modules:\n"); 4 + 5 + // Built-in modules 6 + console.log("Atomics:", Object.prototype.toString.call(Atomics)); 7 + console.log("console:", Object.prototype.toString.call(console)); 8 + console.log("JSON:", Object.prototype.toString.call(JSON)); 9 + console.log("process:", Object.prototype.toString.call(process)); 10 + console.log("Buffer:", Object.prototype.toString.call(Buffer)); 11 + 12 + // Test Ant.Crypto if available 13 + if (typeof Ant !== 'undefined' && Ant.Crypto) { 14 + console.log("Ant.Crypto:", Object.prototype.toString.call(Ant.Crypto)); 15 + } 16 + 17 + // Test imported modules 18 + console.log("\nTesting imported modules:"); 19 + 20 + // Import path module 21 + import * as path from 'ant:path'; 22 + console.log("path:", Object.prototype.toString.call(path)); 23 + 24 + // Import fs module 25 + import * as fs from 'ant:fs'; 26 + console.log("fs:", Object.prototype.toString.call(fs)); 27 + 28 + // Import shell module 29 + import * as shell from 'ant:shell'; 30 + console.log("shell:", Object.prototype.toString.call(shell)); 31 + 32 + // Import ffi module 33 + import * as ffi from 'ant:ffi'; 34 + console.log("ffi:", Object.prototype.toString.call(ffi));
+22
tests/test_toString.cjs
··· 1 + // Test Object.prototype.toString 2 + 3 + console.log("Testing Object.prototype.toString:"); 4 + 5 + // Test with Atomics (has @@toStringTag) 6 + console.log("Object.prototype.toString.call(Atomics):", Object.prototype.toString.call(Atomics)); 7 + 8 + // Test with other built-ins 9 + console.log("\nOther types:"); 10 + console.log("Object.prototype.toString.call({}):", Object.prototype.toString.call({})); 11 + console.log("Object.prototype.toString.call([]):", Object.prototype.toString.call([])); 12 + console.log("Object.prototype.toString.call(function(){}):", Object.prototype.toString.call(function(){})); 13 + console.log("Object.prototype.toString.call(42):", Object.prototype.toString.call(42)); 14 + console.log("Object.prototype.toString.call('hello'):", Object.prototype.toString.call("hello")); 15 + console.log("Object.prototype.toString.call(true):", Object.prototype.toString.call(true)); 16 + console.log("Object.prototype.toString.call(null):", Object.prototype.toString.call(null)); 17 + console.log("Object.prototype.toString.call(undefined):", Object.prototype.toString.call(undefined)); 18 + 19 + // Test with custom @@toStringTag 20 + console.log("\nCustom @@toStringTag:"); 21 + const custom = { "@@toStringTag": "MyCustomType" }; 22 + console.log("Object.prototype.toString.call(custom):", Object.prototype.toString.call(custom));
+23
tests/test_toString_simple.cjs
··· 1 + // Test Object.prototype.toString directly 2 + 3 + console.log("Testing toString:"); 4 + 5 + // Test on objects directly 6 + const obj = {}; 7 + console.log("obj.toString():", obj.toString()); 8 + 9 + const arr = []; 10 + console.log("arr.toString():", arr.toString()); 11 + 12 + const atomics = Atomics; 13 + console.log("Atomics object:", atomics); 14 + console.log("Atomics.toString:", atomics.toString); 15 + 16 + // If toString exists, call it 17 + if (atomics.toString) { 18 + console.log("Calling Atomics.toString():", atomics.toString()); 19 + } 20 + 21 + // Test custom object with @@toStringTag 22 + const custom = { "@@toStringTag": "MyCustomType" }; 23 + console.log("custom.toString():", custom.toString());