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.

whoops, I forgot to add 'await'

+503 -1
+23
src/ant.c
··· 2282 2282 (void) save_pos; 2283 2283 (void) save_tok; 2284 2284 return js_mktrue(); 2285 + } else if (next(js) == TOK_AWAIT) { 2286 + js->consumed = 1; 2287 + jsval_t expr = js_unary(js); 2288 + if (is_err(expr)) return expr; 2289 + if (js->flags & F_NOEXEC) return expr; 2290 + jsval_t resolved = resolveprop(js, expr); 2291 + if (vtype(resolved) != T_PROMISE) { 2292 + return resolved; 2293 + } 2294 + jsval_t p_obj = mkval(T_OBJ, vdata(resolved)); 2295 + jsoff_t state_off = lkp(js, p_obj, "__state", 7); 2296 + if (state_off == 0) return js_mkerr(js, "invalid promise state"); 2297 + int state = (int)tod(resolveprop(js, mkval(T_PROP, state_off))); 2298 + jsoff_t val_off = lkp(js, p_obj, "__value", 7); 2299 + if (val_off == 0) return js_mkerr(js, "invalid promise value"); 2300 + jsval_t val = resolveprop(js, mkval(T_PROP, val_off)); 2301 + if (state == 1) { 2302 + return val; 2303 + } else if (state == 2) { 2304 + return js_mkerr(js, "await: promise rejected"); 2305 + } else { 2306 + return js_mkerr(js, "await: promise not resolved"); 2307 + } 2285 2308 } else if (next(js) == TOK_NOT || js->tok == TOK_TILDA || js->tok == TOK_TYPEOF || 2286 2309 js->tok == TOK_VOID || js->tok == TOK_MINUS || js->tok == TOK_PLUS) { 2287 2310 uint8_t t = js->tok;
+1 -1
tests/server/server.cjs
··· 57 57 return void result.handler(ctx); 58 58 } 59 59 60 - return res.body('not found: ' + req.uri, 404); 60 + res.body('not found: ' + req.uri, 404); 61 61 } 62 62 63 63 Ant.println('started on http://localhost:8000');
+188
tests/test_async_await_integration.cjs
··· 1 + // Integration test for async/await functionality 2 + Ant.println('=== Async/Await Integration Tests ==='); 3 + 4 + // Test 1: Async function that returns a value, consumed with await 5 + Ant.println('\nTest 1: Async function consumed with await'); 6 + async function getData() { 7 + return 'data from async'; 8 + } 9 + 10 + async function consumeData() { 11 + const data = await getData(); 12 + Ant.println('Consumed: ' + data); 13 + return data; 14 + } 15 + 16 + consumeData().then(v => Ant.println('Integration Test 1: ' + v)); 17 + 18 + // Test 2: Chain of async/await functions 19 + Ant.println('\nTest 2: Chain of async/await'); 20 + async function step1() { 21 + const val = await Promise.resolve(10); 22 + return val * 2; 23 + } 24 + 25 + async function step2() { 26 + const val = await step1(); 27 + return val + 5; 28 + } 29 + 30 + async function step3() { 31 + const val = await step2(); 32 + return val * 3; 33 + } 34 + 35 + step3().then(v => Ant.println('Integration Test 2: ' + v)); 36 + 37 + // Test 3: Await with Promise.resolve and transformation 38 + Ant.println('\nTest 3: Await with transformations'); 39 + async function transform() { 40 + const a = await Promise.resolve(5); 41 + const b = await Promise.resolve(10); 42 + const c = await Promise.resolve(a + b); 43 + return c * 2; 44 + } 45 + 46 + transform().then(v => Ant.println('Integration Test 3: ' + v)); 47 + 48 + // Test 4: Async arrow with await 49 + Ant.println('\nTest 4: Async arrow with await'); 50 + const asyncArrow = async (x) => { 51 + const doubled = await Promise.resolve(x * 2); 52 + return doubled + 10; 53 + }; 54 + 55 + asyncArrow(5).then(v => Ant.println('Integration Test 4: ' + v)); 56 + 57 + // Test 5: Nested async calls with await 58 + Ant.println('\nTest 5: Nested async with await'); 59 + async function inner() { 60 + return await Promise.resolve('inner value'); 61 + } 62 + 63 + async function middle() { 64 + const val = await inner(); 65 + return 'middle wraps: ' + val; 66 + } 67 + 68 + async function outer() { 69 + const val = await middle(); 70 + return 'outer wraps: ' + val; 71 + } 72 + 73 + outer().then(v => Ant.println('Integration Test 5: ' + v)); 74 + 75 + // Test 6: Await in conditional branches 76 + Ant.println('\nTest 6: Conditional await'); 77 + async function conditionalAwait(flag) { 78 + if (flag) { 79 + const val = await Promise.resolve(100); 80 + return val; 81 + } else { 82 + const val = await Promise.resolve(200); 83 + return val; 84 + } 85 + } 86 + 87 + conditionalAwait(true).then(v => Ant.println('Integration Test 6a: ' + v)); 88 + conditionalAwait(false).then(v => Ant.println('Integration Test 6b: ' + v)); 89 + 90 + // Test 7: Multiple awaits with operations 91 + Ant.println('\nTest 7: Multiple awaits with math'); 92 + async function calculate() { 93 + const x = await Promise.resolve(3); 94 + const y = await Promise.resolve(4); 95 + const z = await Promise.resolve(5); 96 + return x * x + y * y; 97 + } 98 + 99 + calculate().then(v => Ant.println('Integration Test 7: ' + v)); 100 + 101 + // Test 8: Await with object manipulation 102 + Ant.println('\nTest 8: Await with objects'); 103 + async function objectTest() { 104 + const obj = await Promise.resolve({ name: 'test', value: 42 }); 105 + obj.value = obj.value * 2; 106 + return obj.value; 107 + } 108 + 109 + objectTest().then(v => Ant.println('Integration Test 8: ' + v)); 110 + 111 + // Test 9: Await with array manipulation 112 + Ant.println('\nTest 9: Await with arrays'); 113 + async function arrayTest() { 114 + const arr = await Promise.resolve([1, 2, 3]); 115 + const first = arr[0]; 116 + const second = arr[1]; 117 + return first + second; 118 + } 119 + 120 + arrayTest().then(v => Ant.println('Integration Test 9: ' + v)); 121 + 122 + // Test 10: Await in async method 123 + Ant.println('\nTest 10: Await in object method'); 124 + const calculator = { 125 + multiplier: 3, 126 + asyncMultiply: async function(n) { 127 + const base = await Promise.resolve(n); 128 + return base * this.multiplier; 129 + } 130 + }; 131 + 132 + calculator.asyncMultiply(7).then(v => Ant.println('Integration Test 10: ' + v)); 133 + 134 + // Test 11: Immediate await vs deferred 135 + Ant.println('\nTest 11: Immediate vs deferred await'); 136 + async function immediate() { 137 + return await Promise.resolve('immediate'); 138 + } 139 + 140 + async function deferred() { 141 + const promise = Promise.resolve('deferred'); 142 + const result = await promise; 143 + return result; 144 + } 145 + 146 + immediate().then(v => Ant.println('Integration Test 11a: ' + v)); 147 + deferred().then(v => Ant.println('Integration Test 11b: ' + v)); 148 + 149 + // Test 12: Await non-promise values directly 150 + Ant.println('\nTest 12: Await non-promises'); 151 + async function awaitNonPromise() { 152 + const a = await 10; 153 + const b = await 'string'; 154 + const c = await true; 155 + return a; 156 + } 157 + 158 + awaitNonPromise().then(v => Ant.println('Integration Test 12: ' + v)); 159 + 160 + // Test 13: Complex expression with await 161 + Ant.println('\nTest 13: Complex await expression'); 162 + async function complexExpr() { 163 + const result = (await Promise.resolve(5)) + (await Promise.resolve(3)) * 2; 164 + return result; 165 + } 166 + 167 + complexExpr().then(v => Ant.println('Integration Test 13: ' + v)); 168 + 169 + // Test 14: Await with string concatenation 170 + Ant.println('\nTest 14: Await with string ops'); 171 + async function stringOps() { 172 + const part1 = await Promise.resolve('Hello'); 173 + const part2 = await Promise.resolve(' '); 174 + const part3 = await Promise.resolve('Await!'); 175 + return part1 + part2 + part3; 176 + } 177 + 178 + stringOps().then(v => Ant.println('Integration Test 14: ' + v)); 179 + 180 + // Test 15: Return statement with await 181 + Ant.println('\nTest 15: Direct return await'); 182 + async function directReturn() { 183 + return await Promise.resolve('direct return'); 184 + } 185 + 186 + directReturn().then(v => Ant.println('Integration Test 15: ' + v)); 187 + 188 + Ant.println('\n=== All integration tests initiated ===');
+192
tests/test_await.cjs
··· 1 + // Test await functionality 2 + Ant.println('=== Await Tests ==='); 3 + 4 + // Test 1: Basic await with resolved promise 5 + Ant.println('\nTest 1: Basic await with resolved promise'); 6 + async function test1() { 7 + const result = await Promise.resolve(42); 8 + Ant.println('Awaited resolved promise: ' + result); 9 + return result; 10 + } 11 + test1().then(v => Ant.println('Test 1 returned: ' + v)); 12 + 13 + // Test 2: Await with Promise.resolve string 14 + Ant.println('\nTest 2: Await with string promise'); 15 + async function test2() { 16 + const msg = await Promise.resolve('hello world'); 17 + Ant.println('Awaited string: ' + msg); 18 + return msg; 19 + } 20 + test2().then(v => Ant.println('Test 2 returned: ' + v)); 21 + 22 + // Test 3: Await non-promise value (should return value directly) 23 + Ant.println('\nTest 3: Await non-promise value'); 24 + async function test3() { 25 + const value = await 100; 26 + Ant.println('Awaited non-promise: ' + value); 27 + return value; 28 + } 29 + test3().then(v => Ant.println('Test 3 returned: ' + v)); 30 + 31 + // Test 4: Multiple awaits in sequence 32 + Ant.println('\nTest 4: Multiple sequential awaits'); 33 + async function test4() { 34 + const a = await Promise.resolve(10); 35 + const b = await Promise.resolve(20); 36 + const c = await Promise.resolve(30); 37 + const sum = a + b + c; 38 + Ant.println('Sum of awaited values: ' + sum); 39 + return sum; 40 + } 41 + test4().then(v => Ant.println('Test 4 returned: ' + v)); 42 + 43 + // Test 5: Await in expression 44 + Ant.println('\nTest 5: Await in expression'); 45 + async function test5() { 46 + const result = (await Promise.resolve(5)) * 2; 47 + Ant.println('Awaited and multiplied: ' + result); 48 + return result; 49 + } 50 + test5().then(v => Ant.println('Test 5 returned: ' + v)); 51 + 52 + // Test 6: Await with conditional 53 + Ant.println('\nTest 6: Await with conditional'); 54 + async function test6(flag) { 55 + if (flag) { 56 + const v = await Promise.resolve('true branch'); 57 + return v; 58 + } else { 59 + const v = await Promise.resolve('false branch'); 60 + return v; 61 + } 62 + } 63 + test6(true).then(v => Ant.println('Test 6 (true): ' + v)); 64 + test6(false).then(v => Ant.println('Test 6 (false): ' + v)); 65 + 66 + // Test 7: Async arrow function with await 67 + Ant.println('\nTest 7: Async arrow function with await'); 68 + const test7 = async () => { 69 + const x = await Promise.resolve(7); 70 + return x * 7; 71 + }; 72 + test7().then(v => Ant.println('Test 7 returned: ' + v)); 73 + 74 + // Test 8: Async arrow with parameter and await 75 + Ant.println('\nTest 8: Async arrow with parameter'); 76 + const test8 = async (n) => { 77 + const result = await Promise.resolve(n); 78 + return result + 10; 79 + }; 80 + test8(5).then(v => Ant.println('Test 8 returned: ' + v)); 81 + 82 + // Test 9: Await with object property 83 + Ant.println('\nTest 9: Await with object'); 84 + async function test9() { 85 + const obj = await Promise.resolve({ x: 100, y: 200 }); 86 + Ant.println('Awaited object.x: ' + obj.x); 87 + return obj.y; 88 + } 89 + test9().then(v => Ant.println('Test 9 returned: ' + v)); 90 + 91 + // Test 10: Await with array 92 + Ant.println('\nTest 10: Await with array'); 93 + async function test10() { 94 + const arr = await Promise.resolve([1, 2, 3]); 95 + Ant.println('Awaited array[0]: ' + arr[0]); 96 + return arr[1]; 97 + } 98 + test10().then(v => Ant.println('Test 10 returned: ' + v)); 99 + 100 + // Test 11: Nested async functions with await 101 + Ant.println('\nTest 11: Nested async functions'); 102 + async function inner11() { 103 + return await Promise.resolve('inner result'); 104 + } 105 + async function outer11() { 106 + const result = await inner11(); 107 + return 'outer got: ' + result; 108 + } 109 + outer11().then(v => Ant.println('Test 11 returned: ' + v)); 110 + 111 + // Test 12: Await in async method 112 + Ant.println('\nTest 12: Await in async method'); 113 + const obj12 = { 114 + value: 50, 115 + asyncMethod: async function() { 116 + const multiplier = await Promise.resolve(2); 117 + return this.value * multiplier; 118 + } 119 + }; 120 + obj12.asyncMethod().then(v => Ant.println('Test 12 returned: ' + v)); 121 + 122 + // Test 13: Await with string concatenation 123 + Ant.println('\nTest 13: Await with string operations'); 124 + async function test13() { 125 + const first = await Promise.resolve('Hello'); 126 + const second = await Promise.resolve('World'); 127 + return first + ' ' + second; 128 + } 129 + test13().then(v => Ant.println('Test 13 returned: ' + v)); 130 + 131 + // Test 14: Await with nested promise chains 132 + Ant.println('\nTest 14: Await with chained values'); 133 + async function test14() { 134 + const a = await Promise.resolve(1); 135 + const b = await Promise.resolve(a + 1); 136 + const c = await Promise.resolve(b + 1); 137 + return c; 138 + } 139 + test14().then(v => Ant.println('Test 14 returned: ' + v)); 140 + 141 + // Test 15: Return await 142 + Ant.println('\nTest 15: Return await'); 143 + async function test15() { 144 + return await Promise.resolve('returned from await'); 145 + } 146 + test15().then(v => Ant.println('Test 15 returned: ' + v)); 147 + 148 + // Test 16: Await boolean value 149 + Ant.println('\nTest 16: Await boolean'); 150 + async function test16() { 151 + const result = await Promise.resolve(true); 152 + if (result) { 153 + return 'boolean was true'; 154 + } 155 + return 'boolean was false'; 156 + } 157 + test16().then(v => Ant.println('Test 16 returned: ' + v)); 158 + 159 + // Test 17: Await null 160 + Ant.println('\nTest 17: Await null'); 161 + async function test17() { 162 + const result = await Promise.resolve(null); 163 + return result; 164 + } 165 + test17().then(v => Ant.println('Test 17 returned (null): ' + v)); 166 + 167 + // Test 18: Await undefined 168 + Ant.println('\nTest 18: Await undefined'); 169 + async function test18() { 170 + const result = await Promise.resolve(undefined); 171 + return result; 172 + } 173 + test18().then(v => Ant.println('Test 18 returned (undefined): ' + v)); 174 + 175 + // Test 19: Async function expression with await 176 + Ant.println('\nTest 19: Async function expression'); 177 + const test19 = async function() { 178 + const x = await Promise.resolve(19); 179 + return x; 180 + }; 181 + test19().then(v => Ant.println('Test 19 returned: ' + v)); 182 + 183 + // Test 20: Await with arithmetic operations 184 + Ant.println('\nTest 20: Await with arithmetic'); 185 + async function test20() { 186 + const a = await Promise.resolve(10); 187 + const b = await Promise.resolve(5); 188 + return a + b * 2; 189 + } 190 + test20().then(v => Ant.println('Test 20 returned: ' + v)); 191 + 192 + Ant.println('\n=== Synchronous code finished ===');
+99
tests/test_void_operator.cjs
··· 1 + // Test void operator with promises and async functions 2 + Ant.println('=== Void Operator Tests ==='); 3 + 4 + // Test 1: void with async function (fire and forget) 5 + Ant.println('\nTest 1: void with async function'); 6 + async function asyncTask() { 7 + Ant.println('Async task executing'); 8 + return 'task result'; 9 + } 10 + 11 + // Using void indicates we don't care about the result/promise 12 + void asyncTask(); 13 + Ant.println('After void async call'); 14 + 15 + // Test 2: void with Promise.resolve 16 + Ant.println('\nTest 2: void with Promise.resolve'); 17 + void Promise.resolve(42).then(v => Ant.println('Promise resolved with: ' + v)); 18 + Ant.println('After void Promise.resolve'); 19 + 20 + // Test 3: void with regular function call 21 + Ant.println('\nTest 3: void with regular function'); 22 + function regularFunc() { 23 + Ant.println('Regular function called'); 24 + return 'regular result'; 25 + } 26 + const result = void regularFunc(); 27 + Ant.println('Result of void is: ' + result); 28 + 29 + // Test 4: void in expression 30 + Ant.println('\nTest 4: void in expression'); 31 + const x = 10; 32 + const y = void x; 33 + Ant.println('void x where x=10 is: ' + y); 34 + 35 + // Test 5: void with multiple expressions 36 + Ant.println('\nTest 5: void prevents await'); 37 + async function noAwait() { 38 + // void means we explicitly don't want to await this 39 + void Promise.resolve('not awaited'); 40 + Ant.println('Continued without awaiting'); 41 + return 'done'; 42 + } 43 + noAwait().then(v => Ant.println('noAwait returned: ' + v)); 44 + 45 + // Test 6: void vs await comparison 46 + Ant.println('\nTest 6: void vs await comparison'); 47 + async function withAwait() { 48 + const result = await Promise.resolve('awaited value'); 49 + Ant.println('With await: ' + result); 50 + return result; 51 + } 52 + 53 + async function withVoid() { 54 + void Promise.resolve('void value'); 55 + Ant.println('With void: undefined (not waiting)'); 56 + return 'immediate return'; 57 + } 58 + 59 + withAwait().then(v => Ant.println('withAwait result: ' + v)); 60 + withVoid().then(v => Ant.println('withVoid result: ' + v)); 61 + 62 + // Test 7: void with chained promise (fire and forget) 63 + Ant.println('\nTest 7: void with promise chain'); 64 + void Promise.resolve(1) 65 + .then(v => v + 1) 66 + .then(v => v + 1) 67 + .then(v => Ant.println('Chained promise result: ' + v)); 68 + Ant.println('Promise chain started but not awaited'); 69 + 70 + // Test 8: void return value 71 + Ant.println('\nTest 8: void always returns undefined'); 72 + function returnVoid() { 73 + return void 100; 74 + } 75 + const voidResult = returnVoid(); 76 + Ant.println('Function returning void 100: ' + voidResult); 77 + 78 + // Test 9: void with function expression 79 + Ant.println('\nTest 9: void with function expression'); 80 + void function() { 81 + Ant.println('IIFE with void executed'); 82 + }(); 83 + 84 + // Test 10: void indicating intentional non-handling 85 + Ant.println('\nTest 10: void for intentional non-handling'); 86 + async function backgroundTask() { 87 + return await Promise.resolve('background work done'); 88 + } 89 + 90 + async function mainTask() { 91 + // void explicitly shows we don't want to wait for backgroundTask 92 + void backgroundTask(); 93 + Ant.println('Main task continues immediately'); 94 + return 'main done'; 95 + } 96 + 97 + mainTask().then(v => Ant.println('mainTask result: ' + v)); 98 + 99 + Ant.println('\n=== Synchronous code finished ===');