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.

proper globalThis and window support

+96 -19
+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.1.0.1') 77 + version_conf.set('ANT_VERSION', '0.1.0.2') 78 78 version_conf.set('ANT_GIT_HASH', git_hash) 79 79 version_conf.set('ANT_BUILD_DATE', build_date) 80 80
+6 -2
src/ant.c
··· 245 245 TOK_DEFAULT, TOK_DELETE, TOK_DO, TOK_ELSE, TOK_EXPORT, TOK_FINALLY, TOK_FOR, TOK_FROM, TOK_FUNC, 246 246 TOK_IF, TOK_IMPORT, TOK_IN, TOK_INSTANCEOF, TOK_LET, TOK_NEW, TOK_OF, TOK_RETURN, TOK_SWITCH, 247 247 TOK_THIS, TOK_THROW, TOK_TRY, TOK_VAR, TOK_VOID, TOK_WHILE, TOK_WITH, 248 - TOK_YIELD, TOK_UNDEF, TOK_NULL, TOK_TRUE, TOK_FALSE, TOK_AS, TOK_STATIC, 248 + TOK_YIELD, TOK_UNDEF, TOK_NULL, TOK_TRUE, TOK_FALSE, TOK_AS, TOK_STATIC, TOK_WINDOW, TOK_GLOBAL_THIS, 249 249 TOK_DOT = 100, TOK_CALL, TOK_BRACKET, TOK_POSTINC, TOK_POSTDEC, TOK_NOT, TOK_TILDA, 250 250 TOK_TYPEOF, TOK_UPLUS, TOK_UMINUS, TOK_EXP, TOK_MUL, TOK_DIV, TOK_REM, 251 251 TOK_OPTIONAL_CHAIN, TOK_REST, ··· 2406 2406 case 'd': if (streq("do", 2, buf, len)) return TOK_DO; if (streq("default", 7, buf, len)) return TOK_DEFAULT; if (streq("delete", 6, buf, len)) return TOK_DELETE; break; 2407 2407 case 'e': if (streq("else", 4, buf, len)) return TOK_ELSE; if (streq("export", 6, buf, len)) return TOK_EXPORT; break; 2408 2408 case 'f': if (streq("for", 3, buf, len)) return TOK_FOR; if (streq("from", 4, buf, len)) return TOK_FROM; if (streq("function", 8, buf, len)) return TOK_FUNC; if (streq("finally", 7, buf, len)) return TOK_FINALLY; if (streq("false", 5, buf, len)) return TOK_FALSE; break; 2409 + case 'g': if (streq("globalThis", 10, buf, len)) return TOK_GLOBAL_THIS; break; 2409 2410 case 'i': if (streq("if", 2, buf, len)) return TOK_IF; if (streq("import", 6, buf, len)) return TOK_IMPORT; if (streq("in", 2, buf, len)) return TOK_IN; if (streq("instanceof", 10, buf, len)) return TOK_INSTANCEOF; break; 2410 2411 case 'l': if (streq("let", 3, buf, len)) return TOK_LET; break; 2411 2412 case 'n': if (streq("new", 3, buf, len)) return TOK_NEW; if (streq("null", 4, buf, len)) return TOK_NULL; break; ··· 2415 2416 case 't': if (streq("try", 3, buf, len)) return TOK_TRY; if (streq("this", 4, buf, len)) return TOK_THIS; if (streq("throw", 5, buf, len)) return TOK_THROW; if (streq("true", 4, buf, len)) return TOK_TRUE; if (streq("typeof", 6, buf, len)) return TOK_TYPEOF; break; 2416 2417 case 'u': if (streq("undefined", 9, buf, len)) return TOK_UNDEF; break; 2417 2418 case 'v': if (streq("var", 3, buf, len)) return TOK_VAR; if (streq("void", 4, buf, len)) return TOK_VOID; break; 2418 - case 'w': if (streq("while", 5, buf, len)) return TOK_WHILE; if (streq("with", 4, buf, len)) return TOK_WITH; break; 2419 + case 'w': if (streq("while", 5, buf, len)) return TOK_WHILE; if (streq("with", 4, buf, len)) return TOK_WITH; if (streq("window", 6, buf, len)) return TOK_WINDOW; break; 2419 2420 case 'y': if (streq("yield", 5, buf, len)) return TOK_YIELD; break; 2420 2421 } 2421 2422 return TOK_IDENTIFIER; ··· 4715 4716 case TOK_TRUE: return js_mktrue(); 4716 4717 case TOK_FALSE: return js_mkfalse(); 4717 4718 case TOK_THIS: return js->this_val; 4719 + 4720 + case TOK_WINDOW: 4721 + case TOK_GLOBAL_THIS: return js_glob(js); 4718 4722 4719 4723 case TOK_TYPEOF: return mkcoderef((jsoff_t) js->toff, (jsoff_t) js->tlen); 4720 4724 case TOK_FROM: return mkcoderef((jsoff_t) js->toff, (jsoff_t) js->tlen);
+1 -1
src/core/events.js
··· 1 - this.createEventTarget = function () { 1 + globalThis.createEventTarget = function () { 2 2 const obj = {}; 3 3 obj.addEventListener = EventTargetPrototype.addEventListener; 4 4 obj.removeEventListener = EventTargetPrototype.removeEventListener;
+11 -4
src/main.c
··· 10 10 #include "runtime.h" 11 11 #include "repl.h" 12 12 13 + #ifndef ANT_SNAPSHOT_GENERATOR 14 + #include "snapshot.h" 15 + #endif 16 + 13 17 #include "modules/builtin.h" 14 18 #include "modules/buffer.h" 15 19 #include "modules/atomics.h" ··· 167 171 } 168 172 169 173 if (gct->count > 0) js_setgct(js, gct->ival[0]); 170 - 171 174 ant_runtime_init(js); 172 - 173 - js_set(js, js_glob(js), "global", js_glob(js)); 174 - js_set(js, js_glob(js), "globalThis", js_glob(js)); 175 175 176 176 init_builtin_module(); 177 177 init_buffer_module(); ··· 192 192 ant_register_library(path_library, "ant:path", "node:path", NULL); 193 193 ant_register_library(fs_library, "ant:fs", "node:fs", NULL); 194 194 ant_register_library(crypto_library, "ant:crypto", "node:crypto", NULL); 195 + 196 + #ifndef ANT_SNAPSHOT_GENERATOR 197 + jsval_t snapshot_result = ant_load_snapshot(js); 198 + if (js_type(snapshot_result) == JS_ERR) { 199 + fprintf(stderr, "Warning: Failed to load snapshot: %s\n", js_str(js, snapshot_result)); 200 + } 201 + #endif 195 202 196 203 if (eval->count > 0) eval_code(js, eval, print); 197 204 else if (repl_mode) ant_repl_run(); else {
+1 -11
src/runtime.c
··· 1 - #ifndef ANT_SNAPSHOT_GENERATOR 2 - #include "snapshot.h" 3 - #endif 4 - 5 1 #include <stdio.h> 6 2 #include <stdlib.h> 7 3 #include <runtime.h> ··· 17 13 runtime.external_event_loop_active = 0; 18 14 19 15 js_set(js, js_glob(js), "Ant", runtime.ant_obj); 16 + js_set(js, js_glob(js), "global", js_glob(js)); 20 17 21 - #ifndef ANT_SNAPSHOT_GENERATOR 22 - jsval_t snapshot_result = ant_load_snapshot(js); 23 - if (js_type(snapshot_result) == JS_ERR) { 24 - fprintf(stderr, "Warning: Failed to load snapshot: %s\n", js_str(js, snapshot_result)); 25 - } 26 - #endif 27 - 28 18 return &runtime; 29 19 }
+76
tests/test_function_scope.js
··· 1 + // Test that window/globalThis remain global inside functions 2 + 3 + console.log("=== Global Scope ==="); 4 + globalThis.globalVar = "I am global"; 5 + console.log("globalThis.globalVar:", globalThis.globalVar); 6 + 7 + console.log("\n=== Inside Regular Function ==="); 8 + function testRegularFunc() { 9 + console.log("Inside function, globalThis.globalVar:", globalThis.globalVar); 10 + console.log("Inside function, window.globalVar:", window.globalVar); 11 + 12 + // Set a new property from inside the function 13 + window.fromFunction = "set from inside function"; 14 + globalThis.alsoFromFunction = "also from inside"; 15 + 16 + // Test that window/globalThis are the same 17 + console.log("Inside function, window === globalThis:", window === globalThis); 18 + } 19 + 20 + testRegularFunc(); 21 + console.log("After function, window.fromFunction:", window.fromFunction); 22 + console.log("After function, globalThis.alsoFromFunction:", globalThis.alsoFromFunction); 23 + 24 + console.log("\n=== Inside Arrow Function ==="); 25 + const testArrowFunc = () => { 26 + console.log("Arrow function, globalThis.globalVar:", globalThis.globalVar); 27 + console.log("Arrow function, window.globalVar:", window.globalVar); 28 + window.fromArrow = "from arrow function"; 29 + }; 30 + 31 + testArrowFunc(); 32 + console.log("After arrow, window.fromArrow:", window.fromArrow); 33 + 34 + console.log("\n=== Inside Nested Function ==="); 35 + function outer() { 36 + console.log("Outer function, globalThis.globalVar:", globalThis.globalVar); 37 + 38 + function inner() { 39 + console.log("Inner function, globalThis.globalVar:", globalThis.globalVar); 40 + console.log("Inner function, window.globalVar:", window.globalVar); 41 + window.fromNested = "from nested function"; 42 + } 43 + 44 + inner(); 45 + } 46 + 47 + outer(); 48 + console.log("After nested, window.fromNested:", window.fromNested); 49 + 50 + console.log("\n=== Inside Constructor ==="); 51 + function MyClass() { 52 + console.log("Constructor, globalThis.globalVar:", globalThis.globalVar); 53 + console.log("Constructor, window.globalVar:", window.globalVar); 54 + console.log("Constructor, this === window:", this === window); 55 + console.log("Constructor, this === globalThis:", this === globalThis); 56 + window.fromConstructor = "from constructor"; 57 + } 58 + 59 + new MyClass(); 60 + console.log("After constructor, window.fromConstructor:", window.fromConstructor); 61 + 62 + console.log("\n=== Inside Method ==="); 63 + const obj = { 64 + method: function() { 65 + console.log("Method, globalThis.globalVar:", globalThis.globalVar); 66 + console.log("Method, window.globalVar:", window.globalVar); 67 + console.log("Method, this === window:", this === window); 68 + console.log("Method, this === globalThis:", this === globalThis); 69 + window.fromMethod = "from method"; 70 + } 71 + }; 72 + 73 + obj.method(); 74 + console.log("After method, window.fromMethod:", window.fromMethod); 75 + 76 + console.log("\n=== All tests passed! ===");