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.

fix closure scoping issues

+87 -8
+1 -4
include/ant.h
··· 72 72 73 73 jsval_t js_mkpromise(struct js *js); 74 74 void js_resolve_promise(struct js *js, jsval_t promise, jsval_t value); 75 - void js_reject_promise(struct js *js, jsval_t promise, jsval_t value); 76 - 77 - int js_has_pending_coroutines(void); 78 - void js_process_coroutines(struct js *js); 75 + void js_reject_promise(struct js *js, jsval_t promise, jsval_t value);
+1 -1
meson.build
··· 41 41 build_date = run_command('date', '+%Y-%m-%d', check: true).stdout().strip() 42 42 43 43 version_conf = configuration_data() 44 - version_conf.set('ANT_VERSION', '0.0.6.3') 44 + version_conf.set('ANT_VERSION', '0.0.6.6') 45 45 version_conf.set('ANT_GIT_HASH', git_hash) 46 46 version_conf.set('ANT_BUILD_DATE', build_date) 47 47
+10 -3
src/ant.c
··· 7342 7342 const char *fn = (const char *) (&js->mem[fnoff]); 7343 7343 jsoff_t fnpos = 1; 7344 7344 7345 + jsval_t saved_scope = js->scope; 7346 + jsoff_t scope_off = lkp(js, func_obj, "__scope", 7); 7347 + if (scope_off != 0) { 7348 + jsval_t closure_scope = resolveprop(js, mkval(T_PROP, scope_off)); 7349 + if (vtype(closure_scope) == T_OBJ) { 7350 + js->scope = closure_scope; 7351 + } 7352 + } 7353 + 7345 7354 uint8_t saved_flags = js->flags; 7346 7355 js->flags = 0; 7347 7356 mkscope(js); ··· 7415 7424 7416 7425 js->this_val = saved_this; 7417 7426 delscope(js); 7427 + js->scope = saved_scope; 7418 7428 7419 7429 return res; 7420 7430 } ··· 7466 7476 } 7467 7477 7468 7478 jsval_t js_mkpromise(struct js *js) { return mkpromise(js); } 7469 - int js_has_pending_coroutines(void) { return has_pending_coroutines() ? 1 : 0; } 7470 - 7471 - void js_process_coroutines(struct js *js) { (void)js; } 7472 7479 void js_resolve_promise(struct js *js, jsval_t promise, jsval_t value) { resolve_promise(js, promise, value); } 7473 7480 void js_reject_promise(struct js *js, jsval_t promise, jsval_t value) { reject_promise(js, promise, value); } 7474 7481
+31
tests/test_arrow_closure.cjs
··· 1 + // Test arrow function closures 2 + console.log("Test 1: Basic arrow closure"); 3 + function outer(x) { 4 + return () => x; 5 + } 6 + const fn = outer(42); 7 + console.log("Result:", fn()); 8 + 9 + console.log("\nTest 2: Arrow in setTimeout"); 10 + function test2(value) { 11 + setTimeout(() => { 12 + console.log("Value in setTimeout:", value); 13 + }, 10); 14 + } 15 + test2(123); 16 + 17 + console.log("\nTest 3: Promise with arrow closure"); 18 + function test3(num) { 19 + return new Promise((resolve) => { 20 + setTimeout(() => { 21 + console.log("Num in promise:", num); 22 + resolve(num * 2); 23 + }, 10); 24 + }); 25 + } 26 + 27 + test3(5).then(result => { 28 + console.log("Promise resolved with:", result); 29 + }); 30 + 31 + console.log("Synchronous code done");
+32
tests/test_async_coroutines.cjs
··· 1 + // Test async/await with coroutines 2 + console.log("Starting async test"); 3 + 4 + async function delay(ms) { 5 + return new Promise((resolve) => { 6 + setTimeout(() => { 7 + console.log(`Delayed ${ms}ms`); 8 + resolve(`Result after ${ms}ms`); 9 + }, ms); 10 + }); 11 + } 12 + 13 + async function main() { 14 + console.log("Before await 1"); 15 + const result1 = await delay(100); 16 + console.log("After await 1:", result1); 17 + 18 + console.log("Before await 2"); 19 + const result2 = await delay(50); 20 + console.log("After await 2:", result2); 21 + 22 + return "All done!"; 23 + } 24 + 25 + // Call the async function 26 + main().then(result => { 27 + console.log("Final result:", result); 28 + }).catch(err => { 29 + console.error("Error:", err); 30 + }); 31 + 32 + console.log("After main() call (should execute before awaits)");
+12
tests/test_promise_only.cjs
··· 1 + console.log("Start"); 2 + 3 + new Promise((resolve) => { 4 + setTimeout(() => { 5 + console.log("Timer fired"); 6 + resolve("Done"); 7 + }, 100); 8 + }).then(result => { 9 + console.log("Promise resolved:", result); 10 + }); 11 + 12 + console.log("End");