diff --git a/examples/spec/modules.js b/examples/spec/modules.js index f0b2f16..771ff43 100644 --- a/examples/spec/modules.js +++ b/examples/spec/modules.js @@ -1,5 +1,10 @@ import { test, summary } from './helpers.js'; +import * as path from 'ant:path'; +import * as fs from 'ant:fs'; +import * as shell from 'ant:shell'; +import * as ffi from 'ant:ffi'; + console.log('Module Tests\n'); test('import.meta exists', typeof import.meta, 'object'); @@ -9,4 +14,16 @@ test('import.meta.url is file', import.meta.url.startsWith('file://'), true); test('module imported test', typeof test, 'function'); test('module imported summary', typeof summary, 'function'); +test('Atomics toStringTag', Object.prototype.toString.call(Atomics), '[object Atomics]'); +test('console toStringTag', Object.prototype.toString.call(console), '[object console]'); +test('JSON toStringTag', Object.prototype.toString.call(JSON), '[object JSON]'); +test('process toStringTag', Object.prototype.toString.call(process), '[object process]'); +test('Buffer toStringTag', Object.prototype.toString.call(Buffer), '[object Buffer]'); +test('crypto toStringTag', Object.prototype.toString.call(crypto), '[object Crypto]'); + +test('path toStringTag', Object.prototype.toString.call(path), '[object path]'); +test('fs toStringTag', Object.prototype.toString.call(fs), '[object fs]'); +test('shell toStringTag', Object.prototype.toString.call(shell), '[object shell]'); +test('ffi toStringTag', Object.prototype.toString.call(ffi), '[object FFI]'); + summary(); diff --git a/examples/spec/symbols.js b/examples/spec/symbols.js index 455ca9e..677d042 100644 --- a/examples/spec/symbols.js +++ b/examples/spec/symbols.js @@ -29,4 +29,7 @@ test('Symbol.iterator exists', typeof Symbol.iterator, 'symbol'); test('Symbol.toStringTag exists', typeof Symbol.toStringTag, 'symbol'); test('Symbol.hasInstance exists', typeof Symbol.hasInstance, 'symbol'); +const custom = { [Symbol.toStringTag]: 'MyCustomType' }; +test('Symbol.toStringTag custom', Object.prototype.toString.call(custom), '[object MyCustomType]'); + summary(); diff --git a/include/modules/symbol.h b/include/modules/symbol.h index 6f2a059..8fdd593 100644 --- a/include/modules/symbol.h +++ b/include/modules/symbol.h @@ -5,6 +5,8 @@ void init_symbol_module(void); jsval_t get_iterator_symbol(void); + const char *get_iterator_sym_key(void); +const char *get_toStringTag_sym_key(void); #endif diff --git a/meson.build b/meson.build index 64f64ea..da0903a 100644 --- a/meson.build +++ b/meson.build @@ -74,7 +74,7 @@ endif build_date = run_command('date', '+%Y-%m-%d', check: true).stdout().strip() version_conf = configuration_data() -version_conf.set('ANT_VERSION', '0.1.2.0') +version_conf.set('ANT_VERSION', '0.1.2.1') version_conf.set('ANT_GIT_HASH', git_hash) version_conf.set('ANT_BUILD_DATE', build_date) diff --git a/src/ant.c b/src/ant.c index be39943..a15ddc7 100644 --- a/src/ant.c +++ b/src/ant.c @@ -1199,7 +1199,9 @@ static size_t print_prototype(struct js *js, jsval_t proto_val, char *buf, size_ jsoff_t pklen = offtolen(loadoff(js, pkoff)); const char *pkstr = (const char *) &js->mem[pkoff + sizeof(jsoff_t)]; - if (!streq(pkstr, pklen, "__proto__", 9) && !streq(pkstr, pklen, "constructor", 11) && !streq(pkstr, pklen, "@@toStringTag", 13) && !streq(pkstr, pklen, "__getter", 8)) { + const char *tag_key = get_toStringTag_sym_key(); + size_t tag_key_len = strlen(tag_key); + if (!streq(pkstr, pklen, "__proto__", 9) && !streq(pkstr, pklen, "constructor", 11) && !streq(pkstr, pklen, tag_key, tag_key_len) && !streq(pkstr, pklen, "__getter", 8)) { has_proto_props = true; break; } @@ -1223,7 +1225,9 @@ static size_t print_prototype(struct js *js, jsval_t proto_val, char *buf, size_ jsoff_t pklen = offtolen(loadoff(js, pkoff)); const char *pkstr = (const char *) &js->mem[pkoff + sizeof(jsoff_t)]; - if (!streq(pkstr, pklen, "__proto__", 9) && !streq(pkstr, pklen, "constructor", 11) && !streq(pkstr, pklen, "@@toStringTag", 13) && !streq(pkstr, pklen, "__getter", 8)) { + const char *tag_key2 = get_toStringTag_sym_key(); + size_t tag_key_len2 = strlen(tag_key2); + if (!streq(pkstr, pklen, "__proto__", 9) && !streq(pkstr, pklen, "constructor", 11) && !streq(pkstr, pklen, tag_key2, tag_key_len2) && !streq(pkstr, pklen, "__getter", 8)) { if (!proto_first) n += cpy(buf + n, len - n, ",\n", 2); proto_first = false; n += add_indent(buf + n, len - n, stringify_indent); @@ -1261,7 +1265,8 @@ static size_t strobj(struct js *js, jsval_t obj, char *buf, size_t len) { n += (size_t) snprintf(buf + n, len - n, " ", self_ref); } - jsoff_t tag_off = lkp_proto(js, obj, "@@toStringTag", 13); + const char *tostring_tag_key = get_toStringTag_sym_key(); + jsoff_t tag_off = lkp_proto(js, obj, tostring_tag_key, strlen(tostring_tag_key)); bool is_map = false, is_set = false; jsoff_t tlen = 0, toff = 0; const char *tag_str = NULL; @@ -1381,7 +1386,8 @@ continue_object_print: const char *key = (char *) &js->mem[koff + sizeof(koff)]; bool is_desc = (klen > 7 && key[0] == '_' && key[1] == '_' && key[2] == 'd' && key[3] == 'e' && key[4] == 's' && key[5] == 'c' && key[6] == '_'); - bool should_hide = streq(key, klen, "__proto__", 9) || streq(key, klen, "@@toStringTag", 13) || streq(key, klen, "__getter", 8) || is_desc; + const char *tag_sym_key = get_toStringTag_sym_key(); + bool should_hide = streq(key, klen, "__proto__", 9) || streq(key, klen, tag_sym_key, strlen(tag_sym_key)) || streq(key, klen, "__getter", 8) || is_desc; if (!should_hide) { char desc_key[128]; @@ -1494,7 +1500,8 @@ static size_t strfunc_ctor(struct js *js, jsval_t func_obj, char *buf, size_t le jsoff_t klen = offtolen(loadoff(js, koff)); const char *kstr = (const char *) &js->mem[koff + sizeof(jsoff_t)]; - if (!is_internal_prop(kstr, klen) && !streq(kstr, klen, "name", 4) && !streq(kstr, klen, "@@toStringTag", 13) && !streq(kstr, klen, "__getter", 8)) { + const char *func_tag_key = get_toStringTag_sym_key(); + if (!is_internal_prop(kstr, klen) && !streq(kstr, klen, "name", 4) && !streq(kstr, klen, func_tag_key, strlen(func_tag_key)) && !streq(kstr, klen, "__getter", 8)) { if (!first) n += cpy(buf + n, len - n, ",\n", 2); first = false; n += add_indent(buf + n, len - n, stringify_indent); @@ -10527,7 +10534,8 @@ static jsval_t builtin_object_toString(struct js *js, jsval_t *args, int nargs) if (t == T_OBJ || t == T_ARR || t == T_FUNC) { jsval_t check_obj = (t == T_FUNC) ? mkval(T_OBJ, vdata(obj)) : obj; - jsoff_t tag_off = lkp(js, check_obj, "@@toStringTag", 13); + const char *tostr_tag_key = get_toStringTag_sym_key(); + jsoff_t tag_off = lkp(js, check_obj, tostr_tag_key, strlen(tostr_tag_key)); if (tag_off != 0) { jsval_t tag_val = resolveprop(js, mkval(T_PROP, tag_off)); if (vtype(tag_val) == T_STR) { @@ -16004,7 +16012,8 @@ struct js *js_create(void *buf, size_t len) { setprop(js, map_proto, js_mkstr(js, "clear", 5), js_mkfun(map_clear)); setprop(js, map_proto, js_mkstr(js, "size", 4), js_mkfun(map_size)); setprop(js, map_proto, js_mkstr(js, "entries", 7), js_mkfun(map_entries)); - setprop(js, map_proto, js_mkstr(js, "@@toStringTag", 13), js_mkstr(js, "Map", 3)); + const char *map_tag_key = get_toStringTag_sym_key(); + js_set(js, map_proto, map_tag_key, js_mkstr(js, "Map", 3)); jsval_t set_proto_obj = js_mkobj(js); set_proto(js, set_proto_obj, object_proto); @@ -16014,7 +16023,8 @@ struct js *js_create(void *buf, size_t len) { setprop(js, set_proto_obj, js_mkstr(js, "clear", 5), js_mkfun(set_clear)); setprop(js, set_proto_obj, js_mkstr(js, "size", 4), js_mkfun(set_size)); setprop(js, set_proto_obj, js_mkstr(js, "values", 6), js_mkfun(set_values)); - setprop(js, set_proto_obj, js_mkstr(js, "@@toStringTag", 13), js_mkstr(js, "Set", 3)); + const char *set_tag_key = get_toStringTag_sym_key(); + js_set(js, set_proto_obj, set_tag_key, js_mkstr(js, "Set", 3)); jsval_t weakmap_proto = js_mkobj(js); set_proto(js, weakmap_proto, object_proto); diff --git a/src/main.c b/src/main.c index 7e41b35..c1880c0 100644 --- a/src/main.c +++ b/src/main.c @@ -187,6 +187,7 @@ int main(int argc, char *argv[]) { if (gct->count > 0) js_setgct(js, gct->ival[0]); ant_runtime_init(js, argc, argv); + init_symbol_module(); init_builtin_module(); init_buffer_module(); init_atomics_module(); @@ -202,7 +203,6 @@ int main(int argc, char *argv[]) { init_uri_module(); init_url_module(); init_reflect_module(); - init_symbol_module(); ant_register_library(shell_library, "ant:shell", NULL); ant_register_library(ffi_library, "ant:ffi", NULL); diff --git a/src/modules/atomics.c b/src/modules/atomics.c index ca6e979..bacb9c7 100644 --- a/src/modules/atomics.c +++ b/src/modules/atomics.c @@ -8,8 +8,10 @@ #include "ant.h" #include "runtime.h" + #include "modules/buffer.h" #include "modules/atomics.h" +#include "modules/symbol.h" static WaitQueue global_wait_queue; static pthread_once_t wait_queue_init_once = PTHREAD_ONCE_INIT; @@ -836,6 +838,6 @@ void init_atomics_module(void) { js_set(js, atomics, "waitAsync", js_mkfun(js_atomics_waitAsync)); js_set(js, atomics, "xor", js_mkfun(js_atomics_xor)); - js_set(js, atomics, "@@toStringTag", js_mkstr(js, "Atomics", 7)); + js_set(js, atomics, get_toStringTag_sym_key(), js_mkstr(js, "Atomics", 7)); js_set(js, glob, "Atomics", atomics); } diff --git a/src/modules/buffer.c b/src/modules/buffer.c index b0846a2..9f21465 100644 --- a/src/modules/buffer.c +++ b/src/modules/buffer.c @@ -5,6 +5,7 @@ #include "runtime.h" #include "modules/buffer.h" +#include "modules/symbol.h" static jsval_t js_arraybuffer_slice(struct js *js, jsval_t *args, int nargs); static jsval_t js_typedarray_slice(struct js *js, jsval_t *args, int nargs); @@ -815,6 +816,6 @@ void init_buffer_module() { js_set(js, buffer_obj, "from", js_mkfun(js_buffer_from)); js_set(js, buffer_obj, "alloc", js_mkfun(js_buffer_alloc)); js_set(js, buffer_obj, "allocUnsafe", js_mkfun(js_buffer_allocUnsafe)); - js_set(js, buffer_obj, "@@toStringTag", js_mkstr(js, "Buffer", 6)); + js_set(js, buffer_obj, get_toStringTag_sym_key(), js_mkstr(js, "Buffer", 6)); js_set(js, glob, "Buffer", buffer_obj); } diff --git a/src/modules/crypto.c b/src/modules/crypto.c index bd49669..80b9d1c 100644 --- a/src/modules/crypto.c +++ b/src/modules/crypto.c @@ -8,6 +8,7 @@ #include "runtime.h" #include "modules/crypto.h" #include "modules/buffer.h" +#include "modules/symbol.h" static int ensure_crypto_init(struct js *js) { static int crypto_initialized = 0; @@ -171,7 +172,7 @@ static jsval_t create_crypto_obj(struct js *js) { js_set(js, crypto_obj, "randomUUIDv7", js_mkfun(js_crypto_random_uuidv7)); js_set(js, crypto_obj, "getRandomValues", js_mkfun(js_crypto_get_random_values)); - js_set(js, crypto_obj, "@@toStringTag", js_mkstr(js, "Crypto", 6)); + js_set(js, crypto_obj, get_toStringTag_sym_key(), js_mkstr(js, "Crypto", 6)); return crypto_obj; } @@ -189,7 +190,7 @@ jsval_t crypto_library(struct js *js) { js_set(js, lib, "randomBytes", js_mkfun(js_crypto_random_bytes)); js_set(js, lib, "randomUUID", js_mkfun(js_crypto_random_uuid)); js_set(js, lib, "getRandomValues", js_mkfun(js_crypto_get_random_values)); - js_set(js, lib, "@@toStringTag", js_mkstr(js, "crypto", 6)); + js_set(js, lib, get_toStringTag_sym_key(), js_mkstr(js, "crypto", 6)); js_set(js, lib, "default", lib); return lib; diff --git a/src/modules/events.c b/src/modules/events.c index cf71acd..d53eeaa 100644 --- a/src/modules/events.c +++ b/src/modules/events.c @@ -6,6 +6,7 @@ #include "ant.h" #include "runtime.h" #include "modules/events.h" +#include "modules/symbol.h" #include "uthash.h" #define MAX_LISTENERS_PER_EVENT 32 @@ -287,7 +288,7 @@ static jsval_t js_dispatch_event_method(struct js *js, jsval_t *args, int nargs) jsval_t event_obj = js_mkobj(js); js_set(js, event_obj, "type", args[0]); js_set(js, event_obj, "target", this_obj); - js_set(js, event_obj, "@@toStringTag", js_mkstr(js, "Event", 5)); + js_set(js, event_obj, get_toStringTag_sym_key(), js_mkstr(js, "Event", 5)); if (nargs >= 2 && js_type(args[1]) != JS_UNDEF) { js_set(js, event_obj, "detail", args[1]); @@ -336,7 +337,7 @@ static jsval_t js_dispatch_event(struct js *js, jsval_t *args, int nargs) { jsval_t event_obj = js_mkobj(js); js_set(js, event_obj, "type", args[0]); - js_set(js, event_obj, "@@toStringTag", js_mkstr(js, "Event", 5)); + js_set(js, event_obj, get_toStringTag_sym_key(), js_mkstr(js, "Event", 5)); if (nargs >= 2 && js_type(args[1]) != JS_UNDEF) { js_set(js, event_obj, "detail", args[1]); @@ -620,7 +621,7 @@ static jsval_t js_eventemitter_constructor(struct js *js, jsval_t *args, int nar js_set(js, obj, "removeAllListeners", js_mkfun(js_eventemitter_removeAllListeners)); js_set(js, obj, "listenerCount", js_mkfun(js_eventemitter_listenerCount)); js_set(js, obj, "eventNames", js_mkfun(js_eventemitter_eventNames)); - js_set(js, obj, "@@toStringTag", js_mkstr(js, "EventEmitter", 12)); + js_set(js, obj, get_toStringTag_sym_key(), js_mkstr(js, "EventEmitter", 12)); return obj; } @@ -629,7 +630,7 @@ jsval_t events_library(struct js *js) { jsval_t lib = js_mkobj(js); js_set(js, lib, "EventEmitter", js_mkfun(js_eventemitter_constructor)); - js_set(js, lib, "@@toStringTag", js_mkstr(js, "events", 6)); + js_set(js, lib, get_toStringTag_sym_key(), js_mkstr(js, "events", 6)); return lib; } @@ -647,7 +648,7 @@ void init_events_module() { js_set(js, event_target_proto, "addEventListener", js_mkfun(js_add_event_listener_method)); js_set(js, event_target_proto, "removeEventListener", js_mkfun(js_remove_event_listener_method)); js_set(js, event_target_proto, "dispatchEvent", js_mkfun(js_dispatch_event_method)); - js_set(js, event_target_proto, "@@toStringTag", js_mkstr(js, "EventTarget", 11)); + js_set(js, event_target_proto, get_toStringTag_sym_key(), js_mkstr(js, "EventTarget", 11)); js_set(js, global, "EventTargetPrototype", event_target_proto); } diff --git a/src/modules/ffi.c b/src/modules/ffi.c index e338702..b854eb2 100644 --- a/src/modules/ffi.c +++ b/src/modules/ffi.c @@ -7,6 +7,7 @@ #include #include "modules/ffi.h" +#include "modules/symbol.h" typedef struct ffi_lib { char name[256]; @@ -125,7 +126,7 @@ jsval_t ffi_library(struct js *js) { js_set(js, ffi_types, "string", js_mkstr(js, "string", 6)); js_set(js, ffi_types, "spread", js_mkstr(js, "...", 3)); js_set(js, ffi_obj, "FFIType", ffi_types); - js_set(js, ffi_obj, "@@toStringTag", js_mkstr(js, "FFI", 3)); + js_set(js, ffi_obj, get_toStringTag_sym_key(), js_mkstr(js, "FFI", 3)); js_set(js, ffi_obj, "default", ffi_obj); return ffi_obj; diff --git a/src/modules/fs.c b/src/modules/fs.c index 38bc8b0..0d83b25 100644 --- a/src/modules/fs.c +++ b/src/modules/fs.c @@ -9,6 +9,7 @@ #include #include #include "modules/fs.h" +#include "modules/symbol.h" #include "ant.h" #include "runtime.h" @@ -1140,7 +1141,7 @@ jsval_t fs_library(struct js *js) { js_set(js, lib, "existsSync", js_mkfun(builtin_fs_existsSync)); js_set(js, lib, "readdir", js_mkfun(builtin_fs_readdir)); js_set(js, lib, "readdirSync", js_mkfun(builtin_fs_readdirSync)); - js_set(js, lib, "@@toStringTag", js_mkstr(js, "fs", 2)); + js_set(js, lib, get_toStringTag_sym_key(), js_mkstr(js, "fs", 2)); js_set(js, lib, "default", lib); return lib; diff --git a/src/modules/io.c b/src/modules/io.c index 49b6128..0123fad 100644 --- a/src/modules/io.c +++ b/src/modules/io.c @@ -8,6 +8,7 @@ #include "runtime.h" #include "modules/io.h" +#include "modules/symbol.h" #define ANSI_RED "\x1b[31m" #define ANSI_YELLOW "\x1b[33m" @@ -345,6 +346,6 @@ void init_console_module() { js_set(js, console_obj, "timeEnd", js_mkfun(js_console_timeEnd)); js_set(js, console_obj, "clear", js_mkfun(js_console_clear)); - js_set(js, console_obj, "@@toStringTag", js_mkstr(js, "console", 7)); + js_set(js, console_obj, get_toStringTag_sym_key(), js_mkstr(js, "console", 7)); js_set(js, js_glob(js), "console", console_obj); } \ No newline at end of file diff --git a/src/modules/json.c b/src/modules/json.c index 2b14695..d8f7bfe 100644 --- a/src/modules/json.c +++ b/src/modules/json.c @@ -5,6 +5,7 @@ #include "ant.h" #include "runtime.h" #include "modules/json.h" +#include "modules/symbol.h" static jsval_t yyjson_to_jsval(struct js *js, yyjson_val *val) { if (!val) return js_mkundef(); @@ -218,6 +219,6 @@ void init_json_module() { js_set(js, json_obj, "parse", js_mkfun(js_json_parse)); js_set(js, json_obj, "stringify", js_mkfun(js_json_stringify)); - js_set(js, json_obj, "@@toStringTag", js_mkstr(js, "JSON", 4)); + js_set(js, json_obj, get_toStringTag_sym_key(), js_mkstr(js, "JSON", 4)); js_set(js, js_glob(js), "JSON", json_obj); } \ No newline at end of file diff --git a/src/modules/path.c b/src/modules/path.c index 15792f3..edcf189 100644 --- a/src/modules/path.c +++ b/src/modules/path.c @@ -5,6 +5,7 @@ #include #include "ant.h" +#include "modules/symbol.h" #ifndef PATH_MAX #define PATH_MAX 4096 @@ -474,7 +475,7 @@ jsval_t path_library(struct js *js) { js_set(js, lib, "sep", js_mkstr(js, PATH_SEP_STR, 1)); char delimiter_str[2] = {PATH_DELIMITER, '\0'}; js_set(js, lib, "delimiter", js_mkstr(js, delimiter_str, 1)); - js_set(js, lib, "@@toStringTag", js_mkstr(js, "path", 4)); + js_set(js, lib, get_toStringTag_sym_key(), js_mkstr(js, "path", 4)); js_set(js, lib, "default", lib); return lib; diff --git a/src/modules/process.c b/src/modules/process.c index 1ca28f4..91b444e 100644 --- a/src/modules/process.c +++ b/src/modules/process.c @@ -6,6 +6,7 @@ #include "ant.h" #include "runtime.h" #include "modules/process.h" +#include "modules/symbol.h" extern char **environ; @@ -174,6 +175,6 @@ void init_process_module() { js_set(js, process_obj, "argv", argv_arr); js_set(js, process_obj, "cwd", js_mkfun(process_cwd)); - js_set(js, process_obj, "@@toStringTag", js_mkstr(js, "process", 7)); + js_set(js, process_obj, get_toStringTag_sym_key(), js_mkstr(js, "process", 7)); js_set(js, js_glob(js), "process", process_obj); } diff --git a/src/modules/reflect.c b/src/modules/reflect.c index a09926f..38dd5e1 100644 --- a/src/modules/reflect.c +++ b/src/modules/reflect.c @@ -4,6 +4,7 @@ #include "ant.h" #include "runtime.h" #include "modules/reflect.h" +#include "modules/symbol.h" static jsval_t reflect_get(struct js *js, jsval_t *args, int nargs) { if (nargs < 2) return js_mkundef(); @@ -319,6 +320,6 @@ void init_reflect_module(void) { js_set(js, reflect_obj, "isExtensible", js_mkfun(reflect_is_extensible)); js_set(js, reflect_obj, "preventExtensions", js_mkfun(reflect_prevent_extensions)); - js_set(js, reflect_obj, "@@toStringTag", js_mkstr(js, "Reflect", 7)); + js_set(js, reflect_obj, get_toStringTag_sym_key(), js_mkstr(js, "Reflect", 7)); js_set(js, js_glob(js), "Reflect", reflect_obj); } diff --git a/src/modules/shell.c b/src/modules/shell.c index f79053f..648d4c7 100644 --- a/src/modules/shell.c +++ b/src/modules/shell.c @@ -5,6 +5,7 @@ #include #include "ant.h" +#include "modules/symbol.h" static jsval_t builtin_shell_text(struct js *js, jsval_t *args, int nargs); static jsval_t builtin_shell_lines(struct js *js, jsval_t *args, int nargs); @@ -217,7 +218,7 @@ jsval_t shell_library(struct js *js) { jsval_t lib = js_mkobj(js); js_set(js, lib, "$", js_mkfun(builtin_shell_dollar)); - js_set(js, lib, "@@toStringTag", js_mkstr(js, "shell", 5)); + js_set(js, lib, get_toStringTag_sym_key(), js_mkstr(js, "shell", 5)); return lib; } diff --git a/src/modules/symbol.c b/src/modules/symbol.c index b45f9c0..e70904a 100644 --- a/src/modules/symbol.c +++ b/src/modules/symbol.c @@ -16,15 +16,13 @@ static int g_registry_count = 0; static jsval_t g_iterator_sym = 0; static jsval_t g_toStringTag_sym = 0; static jsval_t g_hasInstance_sym = 0; + static char g_iter_sym_key[32] = {0}; +static char g_toStringTag_sym_key[32] = {0}; -jsval_t get_iterator_symbol(void) { - return g_iterator_sym; -} - -const char *get_iterator_sym_key(void) { - return g_iter_sym_key; -} +jsval_t get_iterator_symbol(void) { return g_iterator_sym; } +const char *get_iterator_sym_key(void) { return g_iter_sym_key; } +const char *get_toStringTag_sym_key(void) { return g_toStringTag_sym_key; } static jsval_t builtin_Symbol(struct js *js, jsval_t *args, int nargs) { const char *desc = NULL; @@ -186,8 +184,11 @@ void init_symbol_module(void) { g_toStringTag_sym = js_mksym(js, "Symbol.toStringTag"); g_hasInstance_sym = js_mksym(js, "Symbol.hasInstance"); - jsval_t sym_id = js_get(js, g_iterator_sym, "__sym_id"); - snprintf(g_iter_sym_key, sizeof(g_iter_sym_key), "__sym_%.0f__", js_getnum(sym_id)); + jsval_t iter_sym_id = js_get(js, g_iterator_sym, "__sym_id"); + snprintf(g_iter_sym_key, sizeof(g_iter_sym_key), "__sym_%.0f__", js_getnum(iter_sym_id)); + + jsval_t tag_sym_id = js_get(js, g_toStringTag_sym, "__sym_id"); + snprintf(g_toStringTag_sym_key, sizeof(g_toStringTag_sym_key), "__sym_%.0f__", js_getnum(tag_sym_id)); jsval_t symbol_ctor = js_mkobj(js); js_set(js, symbol_ctor, "__native_func", js_mkfun(builtin_Symbol)); diff --git a/tests/test_all_modules.cjs b/tests/test_all_modules.cjs index ffeed91..71176f8 100644 --- a/tests/test_all_modules.cjs +++ b/tests/test_all_modules.cjs @@ -1,6 +1,6 @@ // Test toStringTag for all modules -console.log('Testing @@toStringTag for all modules:\n'); +console.log('Testing Symbol.toStringTag for all modules:\n'); // Built-in modules console.log('Atomics:', Object.prototype.toString.call(Atomics)); diff --git a/tests/test_toString.cjs b/tests/test_toString.cjs index 298db40..0017211 100644 --- a/tests/test_toString.cjs +++ b/tests/test_toString.cjs @@ -1,22 +1,25 @@ // Test Object.prototype.toString -console.log("Testing Object.prototype.toString:"); +console.log('Testing Object.prototype.toString:'); -// Test with Atomics (has @@toStringTag) -console.log("Object.prototype.toString.call(Atomics):", Object.prototype.toString.call(Atomics)); +// Test with Atomics (has Symbol.toStringTag) +console.log('Object.prototype.toString.call(Atomics):', Object.prototype.toString.call(Atomics)); // Test with other built-ins -console.log("\nOther types:"); -console.log("Object.prototype.toString.call({}):", Object.prototype.toString.call({})); -console.log("Object.prototype.toString.call([]):", Object.prototype.toString.call([])); -console.log("Object.prototype.toString.call(function(){}):", Object.prototype.toString.call(function(){})); -console.log("Object.prototype.toString.call(42):", Object.prototype.toString.call(42)); -console.log("Object.prototype.toString.call('hello'):", Object.prototype.toString.call("hello")); -console.log("Object.prototype.toString.call(true):", Object.prototype.toString.call(true)); -console.log("Object.prototype.toString.call(null):", Object.prototype.toString.call(null)); -console.log("Object.prototype.toString.call(undefined):", Object.prototype.toString.call(undefined)); +console.log('\nOther types:'); +console.log('Object.prototype.toString.call({}):', Object.prototype.toString.call({})); +console.log('Object.prototype.toString.call([]):', Object.prototype.toString.call([])); +console.log( + 'Object.prototype.toString.call(function(){}):', + Object.prototype.toString.call(function () {}) +); +console.log('Object.prototype.toString.call(42):', Object.prototype.toString.call(42)); +console.log("Object.prototype.toString.call('hello'):", Object.prototype.toString.call('hello')); +console.log('Object.prototype.toString.call(true):', Object.prototype.toString.call(true)); +console.log('Object.prototype.toString.call(null):', Object.prototype.toString.call(null)); +console.log('Object.prototype.toString.call(undefined):', Object.prototype.toString.call(undefined)); -// Test with custom @@toStringTag -console.log("\nCustom @@toStringTag:"); -const custom = { "@@toStringTag": "MyCustomType" }; -console.log("Object.prototype.toString.call(custom):", Object.prototype.toString.call(custom)); +// Test with custom Symbol.toStringTag +console.log('\nCustom Symbol.toStringTag:'); +const custom = { [Symbol.toStringTag]: 'MyCustomType' }; +console.log('Object.prototype.toString.call(custom):', Object.prototype.toString.call(custom)); diff --git a/tests/test_toString_simple.cjs b/tests/test_toString_simple.cjs deleted file mode 100644 index b90ab20..0000000 --- a/tests/test_toString_simple.cjs +++ /dev/null @@ -1,23 +0,0 @@ -// Test Object.prototype.toString directly - -console.log("Testing toString:"); - -// Test on objects directly -const obj = {}; -console.log("obj.toString():", obj.toString()); - -const arr = []; -console.log("arr.toString():", arr.toString()); - -const atomics = Atomics; -console.log("Atomics object:", atomics); -console.log("Atomics.toString:", atomics.toString); - -// If toString exists, call it -if (atomics.toString) { - console.log("Calling Atomics.toString():", atomics.toString()); -} - -// Test custom object with @@toStringTag -const custom = { "@@toStringTag": "MyCustomType" }; -console.log("custom.toString():", custom.toString());