diff --git a/include/ant.h b/include/ant.h index 9f38e07..679cb05 100644 --- a/include/ant.h +++ b/include/ant.h @@ -72,7 +72,4 @@ void js_prop_iter_end(js_prop_iter_t *iter); jsval_t js_mkpromise(struct js *js); void js_resolve_promise(struct js *js, jsval_t promise, jsval_t value); -void js_reject_promise(struct js *js, jsval_t promise, jsval_t value); - -int js_has_pending_coroutines(void); -void js_process_coroutines(struct js *js); \ No newline at end of file +void js_reject_promise(struct js *js, jsval_t promise, jsval_t value); \ No newline at end of file diff --git a/meson.build b/meson.build index 580ce9f..4216e40 100644 --- a/meson.build +++ b/meson.build @@ -41,7 +41,7 @@ endif build_date = run_command('date', '+%Y-%m-%d', check: true).stdout().strip() version_conf = configuration_data() -version_conf.set('ANT_VERSION', '0.0.6.3') +version_conf.set('ANT_VERSION', '0.0.6.6') 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 7664c12..bcd5e88 100644 --- a/src/ant.c +++ b/src/ant.c @@ -7342,6 +7342,15 @@ jsval_t js_call(struct js *js, jsval_t func, jsval_t *args, int nargs) { const char *fn = (const char *) (&js->mem[fnoff]); jsoff_t fnpos = 1; + jsval_t saved_scope = js->scope; + jsoff_t scope_off = lkp(js, func_obj, "__scope", 7); + if (scope_off != 0) { + jsval_t closure_scope = resolveprop(js, mkval(T_PROP, scope_off)); + if (vtype(closure_scope) == T_OBJ) { + js->scope = closure_scope; + } + } + uint8_t saved_flags = js->flags; js->flags = 0; mkscope(js); @@ -7415,6 +7424,7 @@ jsval_t js_call(struct js *js, jsval_t func, jsval_t *args, int nargs) { js->this_val = saved_this; delscope(js); + js->scope = saved_scope; return res; } @@ -7466,9 +7476,6 @@ void js_prop_iter_end(js_prop_iter_t *iter) { } jsval_t js_mkpromise(struct js *js) { return mkpromise(js); } -int js_has_pending_coroutines(void) { return has_pending_coroutines() ? 1 : 0; } - -void js_process_coroutines(struct js *js) { (void)js; } void js_resolve_promise(struct js *js, jsval_t promise, jsval_t value) { resolve_promise(js, promise, value); } void js_reject_promise(struct js *js, jsval_t promise, jsval_t value) { reject_promise(js, promise, value); } diff --git a/tests/test_arrow_closure.cjs b/tests/test_arrow_closure.cjs new file mode 100644 index 0000000..fb692f2 --- /dev/null +++ b/tests/test_arrow_closure.cjs @@ -0,0 +1,31 @@ +// Test arrow function closures +console.log("Test 1: Basic arrow closure"); +function outer(x) { + return () => x; +} +const fn = outer(42); +console.log("Result:", fn()); + +console.log("\nTest 2: Arrow in setTimeout"); +function test2(value) { + setTimeout(() => { + console.log("Value in setTimeout:", value); + }, 10); +} +test2(123); + +console.log("\nTest 3: Promise with arrow closure"); +function test3(num) { + return new Promise((resolve) => { + setTimeout(() => { + console.log("Num in promise:", num); + resolve(num * 2); + }, 10); + }); +} + +test3(5).then(result => { + console.log("Promise resolved with:", result); +}); + +console.log("Synchronous code done"); diff --git a/tests/test_async_coroutines.cjs b/tests/test_async_coroutines.cjs new file mode 100644 index 0000000..0d66266 --- /dev/null +++ b/tests/test_async_coroutines.cjs @@ -0,0 +1,32 @@ +// Test async/await with coroutines +console.log("Starting async test"); + +async function delay(ms) { + return new Promise((resolve) => { + setTimeout(() => { + console.log(`Delayed ${ms}ms`); + resolve(`Result after ${ms}ms`); + }, ms); + }); +} + +async function main() { + console.log("Before await 1"); + const result1 = await delay(100); + console.log("After await 1:", result1); + + console.log("Before await 2"); + const result2 = await delay(50); + console.log("After await 2:", result2); + + return "All done!"; +} + +// Call the async function +main().then(result => { + console.log("Final result:", result); +}).catch(err => { + console.error("Error:", err); +}); + +console.log("After main() call (should execute before awaits)"); diff --git a/tests/test_promise_only.cjs b/tests/test_promise_only.cjs new file mode 100644 index 0000000..1132c01 --- /dev/null +++ b/tests/test_promise_only.cjs @@ -0,0 +1,12 @@ +console.log("Start"); + +new Promise((resolve) => { + setTimeout(() => { + console.log("Timer fired"); + resolve("Done"); + }, 100); +}).then(result => { + console.log("Promise resolved:", result); +}); + +console.log("End");