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.

add Ant.match

+89 -9
+1
include/internal.h
··· 384 384 385 385 bool js_is_prototype_of(ant_t *js, ant_value_t proto_obj, ant_value_t obj); 386 386 ant_value_t builtin_object_isPrototypeOf(ant_t *js, ant_value_t *args, int nargs); 387 + ant_value_t builtin_object_freeze(ant_t *js, ant_value_t *args, int nargs); 387 388 388 389 void js_module_eval_ctx_push(ant_t *js, ant_module_t *ctx); 389 390 void js_module_eval_ctx_pop(ant_t *js, ant_module_t *ctx);
+2 -1
include/modules/symbol.h
··· 48 48 X(observable, "Symbol.observable") \ 49 49 X(toPrimitive, "Symbol.toPrimitive") \ 50 50 X(species, "Symbol.species") \ 51 - X(unscopables, "Symbol.unscopables") 51 + X(unscopables, "Symbol.unscopables") \ 52 + X(default, "Symbol.default") 52 53 53 54 #define DECL_GET_SYM(name, _desc) ant_value_t get_##name##_sym(void); 54 55 WELLKNOWN_SYMBOLS(DECL_GET_SYM)
+4 -6
src/ant.c
··· 6150 6150 return target; 6151 6151 } 6152 6152 6153 - static ant_value_t builtin_object_freeze(ant_t *js, ant_value_t *args, int nargs) { 6153 + ant_value_t builtin_object_freeze(ant_t *js, ant_value_t *args, int nargs) { 6154 6154 if (nargs == 0) return js_mkundef(); 6155 6155 6156 6156 ant_value_t obj = args[0]; ··· 6167 6167 for (uint32_t i = 0; i < count; i++) { 6168 6168 const ant_shape_prop_t *prop = ant_shape_prop_at(ptr->shape, i); 6169 6169 if (!prop) continue; 6170 - 6170 + 6171 6171 uint8_t attrs = ant_shape_get_attrs(ptr->shape, i); 6172 6172 attrs &= (uint8_t)~ANT_PROP_ATTR_WRITABLE; 6173 6173 attrs &= (uint8_t)~ANT_PROP_ATTR_CONFIGURABLE; 6174 - 6174 + 6175 6175 if (prop->type == ANT_SHAPE_KEY_STRING) { 6176 6176 const char *key = prop->key.interned; 6177 6177 size_t klen = strlen(key); 6178 6178 if (is_internal_prop(key, klen)) continue; 6179 6179 ant_shape_set_attrs_interned(ptr->shape, key, attrs); 6180 - } else { 6181 - ant_shape_set_attrs_symbol(ptr->shape, prop->key.sym_off, attrs); 6182 - } 6180 + } else ant_shape_set_attrs_symbol(ptr->shape, prop->key.sym_off, attrs); 6183 6181 } 6184 6182 6185 6183 ptr->frozen = 1;
+54
src/modules/builtin.c
··· 23 23 #include "silver/engine.h" 24 24 #include "modules/builtin.h" 25 25 #include "modules/buffer.h" 26 + #include "modules/symbol.h" 26 27 27 28 static struct { 28 29 ant_t *js; ··· 305 306 return result; 306 307 } 307 308 309 + static inline ant_value_t match_resolve_arm(ant_t *js, ant_value_t arm, ant_value_t value) { 310 + if (!is_callable(arm)) return arm; 311 + return sv_vm_call(js->vm, js, arm, js_mkundef(), &value, 1, NULL, false); 312 + } 313 + 314 + static ant_value_t js_match(ant_t *js, ant_value_t *args, int nargs) { 315 + if (nargs < 2) return js_mkerr(js, "Ant.match() requires 2 arguments"); 316 + 317 + ant_value_t value = args[0]; 318 + ant_value_t arms = args[1]; 319 + 320 + if (is_callable(arms)) { 321 + arms = sv_vm_call(js->vm, js, arms, js_mkundef(), &value, 1, NULL, false); 322 + if (is_err(arms)) return arms; 323 + } 324 + 325 + if (!is_object_type(arms)) return js_mkundef(); 326 + ant_value_t value_str = js_tostring_val(js, value); 327 + const char *vs = js_getstr(js, value_str, NULL); 328 + 329 + ant_iter_t iter = js_prop_iter_begin(js, arms); 330 + ant_value_t guard_arm = js_mkundef(); 331 + 332 + ant_value_t key = js_mkundef(); 333 + ant_value_t arm = js_mkundef(); 334 + 335 + while (js_prop_iter_next_val(&iter, &key, &arm)) { 336 + if (vtype(key) == T_SYMBOL) continue; 337 + 338 + const char *ks = js_getstr(js, key, NULL); 339 + if (!ks) continue; 340 + 341 + if (strcmp(ks, vs) == 0) { 342 + js_prop_iter_end(&iter); 343 + return match_resolve_arm(js, arm, value); 344 + } 345 + 346 + if ( 347 + strcmp(ks, "true") == 0 348 + && vtype(guard_arm) == T_UNDEF 349 + ) guard_arm = arm; 350 + } 351 + 352 + js_prop_iter_end(&iter); 353 + if (vtype(guard_arm) != T_UNDEF) return match_resolve_arm(js, guard_arm, value); 354 + 355 + ant_value_t fallback = js_get_sym(js, arms, get_default_sym()); 356 + if (vtype(fallback) != T_UNDEF) return match_resolve_arm(js, fallback, value); 357 + 358 + return js_mkundef(); 359 + } 360 + 308 361 void init_builtin_module() { 309 362 ant_t *js = rt->js; 310 363 ant_value_t ant_obj = rt->ant_obj; 311 364 365 + js_set(js, ant_obj, "match", js_mkfun(js_match)); 312 366 js_set(js, ant_obj, "stats", js_mkfun(js_stats_fn)); 313 367 js_set(js, ant_obj, "signal", js_mkfun(js_signal)); 314 368 js_set(js, ant_obj, "sleep", js_mkfun(js_sleep));
+24
src/silver/compiler.c
··· 2285 2285 if (compile_call_is_proto_intrinsic(c, node, has_spread)) 2286 2286 return; 2287 2287 2288 + if ( 2289 + !has_spread && node->args.count >= 2 && 2290 + callee->type == N_MEMBER && 2291 + is_ident_name(callee->left, "Ant") && 2292 + resolve_local(c, "Ant", 3) == -1 && 2293 + callee->right && callee->right->type == N_IDENT && 2294 + callee->right->len == 5 && memcmp(callee->right->str, "match", 5) == 0 && 2295 + node->args.items[1]->type == N_OBJECT 2296 + ) { 2297 + sv_ast_t *obj = node->args.items[1]; 2298 + sv_ast_t *param = sv_ast_new(N_IDENT); 2299 + 2300 + param->str = "$"; param->len = 1; 2301 + sv_ast_t *arrow = sv_ast_new(N_FUNC); 2302 + 2303 + arrow->flags = FN_ARROW; 2304 + arrow->body = obj; 2305 + arrow->line = obj->line; arrow->col = obj->col; 2306 + arrow->src_off = obj->src_off; arrow->src_end = obj->src_end; 2307 + 2308 + sv_ast_list_push(&arrow->args, param); 2309 + node->args.items[1] = arrow; 2310 + } 2311 + 2288 2312 if (!has_spread && is_ident_name(callee, "eval")) { 2289 2313 if (node->args.count > 0) 2290 2314 compile_expr(c, node->args.items[0]);
+4 -2
src/snapshot.c
··· 2 2 3 3 #include "ant.h" 4 4 #include "snapshot.h" 5 + #include "runtime.h" 5 6 #include "internal.h" 6 7 #include "snapshot_data.h" 7 8 #include "gc/objects.h" ··· 10 11 if (!js) return js_mkundef(); 11 12 12 13 const char *src = (const char *)ant_snapshot_source; 13 - size_t len = ant_snapshot_source_len; 14 + ant_value_t result = js_eval_bytecode(js, src, ant_snapshot_source_len); 14 15 15 - ant_value_t result = js_eval_bytecode(js, src, len); 16 16 gc_pin_existing_objects(js); 17 + builtin_object_freeze(js, &rt->ant_obj, 1); 18 + 17 19 return vtype(result) == T_ERR ? result : js_true; 18 20 } 19 21