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.

improve CommonJS function compilation with parameter handling

+76 -29
+1 -1
examples/results.txt
··· 1323 1323 compat-table/es2023/TypedArray.prototype.toReversed.js: OK 1324 1324 compat-table/es2023/TypedArray.prototype.toSorted.js: OK 1325 1325 compat-table/es2023/TypedArray.prototype.with.js: OK 1326 - compat-table/es2023/hashbang.js: SyntaxError: private field name expected 1326 + compat-table/es2023/hashbang.js: OK 1327 1327 compat-table/es2024/ArrayBuffer.prototype.detached.js: OK 1328 1328 compat-table/es2024/ArrayBuffer.prototype.transferToFixedLength.js: OK 1329 1329 compat-table/es2024/ArrayBuffer.prototype.transfer.js: OK
+11 -3
include/silver/compiler.h
··· 11 11 SV_COMPILE_REPL = 3, 12 12 } sv_compile_mode_t; 13 13 14 + typedef struct { 15 + const char *name; 16 + size_t len; 17 + } sv_param_t; 18 + 19 + #define SV_PARAM(name_literal) \ 20 + ((sv_param_t){ (name_literal), sizeof(name_literal) - 1 }) 21 + 14 22 sv_func_t *sv_compile( 15 23 ant_t *js, sv_ast_t *program, 16 24 sv_compile_mode_t mode, ··· 22 30 size_t len, bool is_async 23 31 ); 24 32 25 - sv_func_t *sv_compile_function_parts( 26 - ant_t *js, const char *params, 27 - size_t params_len, const char *body, 33 + sv_func_t *sv_compile_function_with_params( 34 + ant_t *js, const sv_param_t *params, 35 + int param_count, const char *body, 28 36 size_t body_len, bool is_async 29 37 ); 30 38
+10 -4
src/esm/commonjs.c
··· 105 105 ant_value_t filename_val, 106 106 ant_value_t dirname_val 107 107 ) { 108 - static const char *cjs_params = "require,module,exports,__filename,__dirname"; 109 - 110 - sv_func_t *compiled = sv_compile_function_parts( 108 + static const sv_param_t cjs_params[] = { 109 + SV_PARAM("require"), 110 + SV_PARAM("module"), 111 + SV_PARAM("exports"), 112 + SV_PARAM("__filename"), 113 + SV_PARAM("__dirname"), 114 + }; 115 + 116 + sv_func_t *compiled = sv_compile_function_with_params( 111 117 js, cjs_params, 112 - strlen(cjs_params), 118 + (int)(sizeof(cjs_params) / sizeof(cjs_params[0])), 113 119 code, code_len, 114 120 false 115 121 );
+54 -21
src/silver/compiler.c
··· 4186 4186 return func; 4187 4187 } 4188 4188 4189 - sv_func_t *sv_compile_function_parts( 4189 + sv_func_t *sv_compile_function_with_params( 4190 4190 ant_t *js, 4191 - const char *params, 4192 - size_t params_len, 4191 + const sv_param_t *params, 4192 + int param_count, 4193 4193 const char *body, 4194 4194 size_t body_len, 4195 4195 bool is_async 4196 4196 ) { 4197 - size_t source_len = params_len + body_len + 3; 4198 - char *source = malloc(source_len + 1); 4199 - if (!source) return NULL; 4197 + if (!body) { 4198 + body = ""; 4199 + body_len = 0; 4200 + } 4201 + 4202 + bool parse_strict = sv_vm_is_strict(js->vm); 4203 + sv_ast_t *program = sv_parse(js, body, (ant_offset_t)body_len, parse_strict); 4204 + if (!program) return NULL; 4205 + 4206 + static const char *k_top_name_function = "<function>"; 4207 + static const char *k_top_name_async_function = "<async function>"; 4208 + const char *top_name = is_async ? k_top_name_async_function : k_top_name_function; 4209 + 4210 + sv_ast_t top_fn; 4211 + memset(&top_fn, 0, sizeof(top_fn)); 4212 + 4213 + top_fn.type = N_FUNC; 4214 + top_fn.line = 1; 4215 + top_fn.str = top_name; 4216 + top_fn.len = (uint32_t)strlen(top_name); 4217 + top_fn.src_off = 0; 4218 + top_fn.src_end = (body_len > 0) ? (uint32_t)body_len : 0; 4219 + if (is_async) top_fn.flags |= FN_ASYNC; 4220 + 4221 + for (int i = 0; i < param_count; i++) { 4222 + const char *name = (params && params[i].name) ? params[i].name : ""; 4223 + size_t name_len = 0; 4224 + if (params && params[i].name) { 4225 + name_len = params[i].len ? params[i].len : strlen(name); 4226 + } 4227 + 4228 + sv_ast_t *ident = sv_ast_new(N_IDENT); 4229 + if (!ident) return NULL; 4200 4230 4201 - size_t pos = 0; 4202 - source[pos++] = '('; 4203 - if (params_len > 0 && params) { 4204 - memcpy(source + pos, params, params_len); 4205 - pos += params_len; 4231 + ident->str = name; 4232 + ident->len = (uint32_t)name_len; 4233 + ident->line = 1; 4234 + ident->col = 1; 4235 + sv_ast_list_push(&top_fn.args, ident); 4206 4236 } 4207 - source[pos++] = ')'; 4208 - source[pos++] = '{'; 4209 - if (body_len > 0 && body) { 4210 - memcpy(source + pos, body, body_len); 4211 - pos += body_len; 4212 - } 4213 - source[pos++] = '}'; 4214 - source[pos] = '\0'; 4237 + 4238 + top_fn.body = sv_ast_new(N_BLOCK); 4239 + if (!top_fn.body) return NULL; 4240 + top_fn.body->args = program->args; 4241 + 4242 + sv_compiler_t root = {0}; 4243 + root.js = js; 4244 + root.source = pin_source_text(body, (ant_offset_t)body_len); 4245 + root.source_len = (ant_offset_t)body_len; 4246 + root.mode = SV_COMPILE_SCRIPT; 4247 + root.is_strict = ((program->flags & FN_PARSE_STRICT) != 0); 4215 4248 4216 - sv_func_t *func = sv_compile_function(js, source, pos, is_async); 4217 - free(source); 4249 + sv_func_t *func = compile_function_body(&root, &top_fn, SV_COMPILE_SCRIPT); 4250 + if (js->thrown_exists || !func) return NULL; 4218 4251 return func; 4219 4252 }