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.

refactor block position tracking for asi

+63 -14
+21 -14
src/ant.c
··· 11685 11685 return js_mkundef(); 11686 11686 } 11687 11687 11688 - static jsval_t js_block_or_stmt(struct js *js) { 11689 - if (next(js) == TOK_LBRACE) return js_block(js, !(js->flags & F_NOEXEC)); 11688 + static jsval_t js_block_or_stmt_pos(struct js *js, jsoff_t *end_pos) { 11689 + if (next(js) == TOK_LBRACE) { 11690 + jsval_t res = js_block(js, !(js->flags & F_NOEXEC)); 11691 + if (end_pos) *end_pos = js->pos; 11692 + return res; 11693 + } 11690 11694 uint8_t stmt_tok = js->tok; 11691 11695 jsval_t res = resolveprop(js, js_stmt(js)); 11692 11696 bool is_block_stmt = ( ··· 11696 11700 stmt_tok == TOK_SWITCH || stmt_tok == TOK_TRY || 11697 11701 stmt_tok == TOK_LBRACE || stmt_tok == TOK_ASYNC 11698 11702 ); 11703 + if (end_pos) { 11704 + *end_pos = (!is_block_stmt && !js->consumed) ? js->toff : js->pos; 11705 + } 11699 11706 if (!is_block_stmt) js->consumed = 0; 11700 11707 return res; 11708 + } 11709 + 11710 + static inline jsval_t js_block_or_stmt(struct js *js) { 11711 + return js_block_or_stmt_pos(js, NULL); 11701 11712 } 11702 11713 11703 11714 typedef struct { ··· 11786 11797 if (cond_true) res = blk; 11787 11798 if (exe && !cond_true) js->flags &= (uint8_t) ~F_NOEXEC; 11788 11799 11789 - if (lookahead(js) == TOK_ELSE) { 11790 - js->consumed = 1; 11791 - next(js); 11800 + if (next(js) == TOK_ELSE || lookahead(js) == TOK_ELSE) { 11801 + if (js->tok != TOK_ELSE) { js->consumed = 1; next(js); } 11792 11802 js->consumed = 1; 11793 11803 if (cond_true) js->flags |= F_NOEXEC; 11794 11804 blk = js_block_or_stmt(js); ··· 12287 12297 12288 12298 if (exe) loop_block_init(js, &forin_loop_ctx); 12289 12299 js->flags |= F_NOEXEC; 12290 - v = js_block_or_stmt(js); 12300 + v = js_block_or_stmt_pos(js, &body_end_pos); 12291 12301 if (is_err2(&v, &res)) goto done; 12292 12302 12293 - body_end_pos = js->pos; 12294 12303 body_end_token = js->tok; 12295 12304 body_end_consumed = js->consumed; 12296 12305 body_end_toff = js->toff; ··· 12339 12348 12340 12349 if (exe) loop_block_init(js, &forof_loop_ctx); 12341 12350 js->flags |= F_NOEXEC; 12342 - v = js_block_or_stmt(js); 12351 + v = js_block_or_stmt_pos(js, &body_end_pos); 12343 12352 if (is_err2(&v, &res)) goto done; 12344 12353 12345 - body_end_pos = js->pos; 12346 12354 body_end_token = js->tok; 12347 12355 body_end_consumed = js->consumed; 12348 12356 body_end_toff = js->toff; ··· 12417 12425 } 12418 12426 12419 12427 js->flags |= F_NOEXEC; 12420 - v = js_block_or_stmt(js); 12428 + v = js_block_or_stmt_pos(js, &pos4); 12421 12429 if (exe) js->flags = flags; 12422 12430 if (is_err2(&v, &res)) goto done; 12423 - pos4 = js->pos; 12424 12431 12425 12432 while (!(flags & F_NOEXEC)) { 12426 12433 js_gc_safepoint(js); ··· 12526 12533 js->flags |= F_NOEXEC; 12527 12534 } 12528 12535 12529 - v = js_block_or_stmt(js); 12536 + jsoff_t body_end; 12537 + v = js_block_or_stmt_pos(js, &body_end); 12530 12538 if (is_err(v)) { res = v; goto done; } 12531 - jsoff_t body_end = js->pos; 12532 12539 12533 12540 if (exe) { 12534 12541 while (true) { ··· 13369 13376 uint8_t preserve = 0; 13370 13377 if (js->flags & F_RETURN) preserve = js->flags & (F_RETURN | F_NOEXEC); 13371 13378 if (js->flags & F_THROW) preserve = js->flags & (F_THROW | F_NOEXEC); 13372 - js->flags = (flags & ~F_SWITCH) | preserve; 13379 + js->flags = flags | preserve; 13373 13380 13374 13381 return res; 13375 13382 }
+42
tests/test_asi_all.js
··· 1 + // for loop 2 + function testFor() { 3 + let x = 0 4 + for (let i = 0; i < 3; i++) x += 1 5 + return x 6 + } 7 + 8 + // while loop 9 + function testWhile() { 10 + let x = 0 11 + let i = 0 12 + while (i < 3) i++, x += 1 13 + return x 14 + } 15 + 16 + // if statement 17 + function testIf() { 18 + let x = 10 19 + if (x > 5) x = 42 20 + return x 21 + } 22 + 23 + // if-else 24 + function testIfElse() { 25 + let x = 3 26 + if (x > 5) x = 100 27 + else x = 200 28 + return x 29 + } 30 + 31 + // nested 32 + function testNested() { 33 + let sum = 0 34 + for (let i = 0; i < 2; i++) for (let j = 0; j < 2; j++) sum += 1 35 + return sum 36 + } 37 + 38 + console.log("for:", testFor()) // 3 39 + console.log("while:", testWhile()) // 3 40 + console.log("if:", testIf()) // 42 41 + console.log("if-else:", testIfElse()) // 200 42 + console.log("nested:", testNested()) // 4