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.

optimize tokenization cache for function bodies

+43 -14
+6 -14
src/ant.c
··· 6679 6679 } return false; 6680 6680 } 6681 6681 6682 - static parsed_func_t *get_or_parse_func(const char *fn, jsoff_t fnlen) { 6682 + static parsed_func_t *get_or_parse_func(struct js *js, const char *fn, jsoff_t fnlen) { 6683 6683 uint64_t h = hash_key(fn, fnlen); 6684 6684 parsed_func_t *cached = NULL; 6685 6685 HASH_FIND(hh, func_parse_cache, &h, sizeof(h), cached); ··· 6774 6774 pf->is_expr = !is_block; 6775 6775 pf->is_strict = is_strict_function_body(&fn[fnpos], pf->body_len); 6776 6776 pf->uses_arguments = code_uses_arguments(&fn[pf->body_start], pf->body_len); 6777 - pf->tokens = NULL; 6777 + pf->tokens = (pf->body_len > 0) ? tokenize_body(js, &fn[pf->body_start], pf->body_len) : NULL; 6778 6778 6779 6779 HASH_ADD(hh, func_parse_cache, code_hash, sizeof(pf->code_hash), pf); 6780 6780 return pf; ··· 6905 6905 js->pos = caller_pos; 6906 6906 js->scope = function_scope; 6907 6907 6908 - parsed_func_t *pf = get_or_parse_func(fn, fnlen); 6908 + parsed_func_t *pf = get_or_parse_func(js, fn, fnlen); 6909 6909 if (!pf) { 6910 6910 if (args_on_heap) free(args); 6911 6911 restore_saved_scope(js); ··· 6992 6992 int saved_token_stream_pos = js->token_stream_pos; 6993 6993 const char *saved_token_stream_code = js->token_stream_code; 6994 6994 6995 - if (!pf->is_expr && !pf->tokens && pf->body_len > 0) { 6996 - pf->tokens = tokenize_body(js, &fn[pf->body_start], pf->body_len); 6997 - } 6998 - 6999 - if (pf->tokens && !pf->is_expr) { 6995 + if (pf->tokens) { 7000 6996 js->token_stream = pf->tokens; 7001 6997 js->token_stream_pos = 0; 7002 6998 js->token_stream_code = &fn[pf->body_start]; ··· 23093 23089 utarray_push_back(global_scope_stack, &function_scope_offset); 23094 23090 hoist_var_declarations_from_slot(js, js->scope, func_obj); 23095 23091 23096 - parsed_func_t *pf = get_or_parse_func(fn, fnlen); 23092 + parsed_func_t *pf = get_or_parse_func(js, fn, fnlen); 23097 23093 if (!pf) { 23098 23094 if (global_scope_stack && utarray_len(global_scope_stack) > 0) utarray_pop_back(global_scope_stack); 23099 23095 delscope(js); js->scope = saved_scope; ··· 23160 23156 int saved_token_stream_pos = js->token_stream_pos; 23161 23157 const char *saved_token_stream_code = js->token_stream_code; 23162 23158 23163 - if (!pf->is_expr && !pf->tokens && pf->body_len > 0) { 23164 - pf->tokens = tokenize_body(js, &fn[pf->body_start], pf->body_len); 23165 - } 23166 - 23167 - if (pf->tokens && !pf->is_expr) { 23159 + if (pf->tokens) { 23168 23160 js->token_stream = pf->tokens; 23169 23161 js->token_stream_pos = 0; 23170 23162 js->token_stream_code = &fn[pf->body_start];
+37
tests/mandelbrot_bellard.cjs
··· 1 + /* Mandelbrot display on a color terminal 2 + (c) 2025 Fabrice Bellard 3 + MIT license 4 + */ 5 + function mandelbrot(center_x, center_y, scale, w, h, max_it) { 6 + var x1, y1, y2, i, x, y, cx, cy, fx, fy, i, t, c, s, c0; 7 + var colors = [14, 15, 7, 8, 0, 4, 12, 5, 13, 1, 9, 3, 11, 10, 2, 6]; 8 + fx = (scale * 0.5) / Math.min(w, h); 9 + fy = fx * 2; 10 + for (y1 = 0; y1 < h; y1++) { 11 + s = ''; 12 + for (x1 = 0; x1 < w; x1++) { 13 + for (y2 = 0; y2 < 2; y2++) { 14 + cx = (x1 - w * 0.5) * fx + center_x; 15 + cy = (y1 + y2 * 0.5 - h * 0.5) * fy + center_y; 16 + x = 0; 17 + y = 0; 18 + for (i = 0; i < max_it && x * x + y * y < 4; i++) { 19 + t = x * x - y * y + cx; 20 + y = 2 * x * y + cy; 21 + x = t; 22 + } 23 + if (i >= max_it) { 24 + c = 0; 25 + } else { 26 + c = colors[i % colors.length]; 27 + } 28 + if (y2 == 0) c0 = c; 29 + } 30 + s += '\x1b[' + (c0 >= 8 ? 82 + c0 : 30 + c0) + ';' + (c >= 8 ? 92 + c : 40 + c) + 'm\u2580'; 31 + } 32 + s += '\x1b[0m'; /* reset the colors */ 33 + console.log(s); 34 + } 35 + } 36 + 37 + mandelbrot(-0.75, 0.0, 2.0, 80, 25, 50);