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 unboxed object support to built-in constructors

+20 -56
+1 -1
meson.build
··· 96 96 build_date = run_command('date', '+%Y-%m-%d', check: true).stdout().strip() 97 97 98 98 version_conf = configuration_data() 99 - version_conf.set('ANT_VERSION', '0.3.2.20') 99 + version_conf.set('ANT_VERSION', '0.3.2.21') 100 100 version_conf.set('ANT_GIT_HASH', git_hash) 101 101 version_conf.set('ANT_BUILD_DATE', build_date) 102 102
+19 -55
src/ant.c
··· 692 692 return (tok >= TOK_ASYNC && tok <= TOK_GLOBAL_THIS) || tok == TOK_TYPEOF; 693 693 } 694 694 695 + static inline bool is_unboxed_obj(struct js *js, jsval_t val, jsval_t expected_proto) { 696 + if (vtype(val) != T_OBJ) return false; 697 + if (vtype(get_slot(js, val, SLOT_PRIMITIVE)) != T_UNDEF) return false; 698 + jsval_t proto = get_slot(js, val, SLOT_PROTO); 699 + return vdata(proto) == vdata(expected_proto); 700 + } 701 + 695 702 static bool is_valid_arrow_param_tok(uint8_t tok) { 696 703 static const uint64_t bits[4] = { 697 704 0x0004000000000F0Cull, ··· 4286 4293 return 0; 4287 4294 } 4288 4295 4289 - static inline jsoff_t lkp_inline(struct js *js, jsval_t obj, const char *buf, size_t len) { 4296 + static inline jsoff_t lkp(struct js *js, jsval_t obj, const char *buf, size_t len) { 4290 4297 const char *search_intern = intern_string(buf, len); 4291 4298 if (!search_intern) return 0; 4292 4299 return lkp_interned(js, obj, search_intern, len); ··· 4314 4321 return 0; 4315 4322 } 4316 4323 4317 - static jsoff_t lkp(struct js *js, jsval_t obj, const char *buf, size_t len) { 4318 - return lkp_inline(js, obj, buf, len); 4319 - } 4320 4324 static jsoff_t lkp_with_getter(struct js *js, jsval_t obj, const char *buf, size_t len, jsval_t *getter_out, bool *has_getter_out) { 4321 4325 *has_getter_out = false; 4322 4326 *getter_out = js_mkundef(); ··· 11406 11410 sval = js_mkstr(js, str, strlen(str)); 11407 11411 } 11408 11412 } 11409 - if (vtype(js->this_val) == T_OBJ && vtype(get_slot(js, js->this_val, SLOT_PRIMITIVE)) == T_UNDEF) { 11413 + jsval_t string_proto = js_get_ctor_proto(js, "String", 6); 11414 + if (is_unboxed_obj(js, js->this_val, string_proto)) { 11410 11415 set_slot(js, js->this_val, SLOT_PRIMITIVE, sval); 11411 - jsval_t proto = get_ctor_proto(js, "String", 6); 11412 - if (vtype(proto) == T_OBJ) set_proto(js, js->this_val, proto); 11413 11416 jsoff_t slen; 11414 11417 vstr(js, sval, &slen); 11415 11418 setprop(js, js->this_val, js_mkstr(js, "length", 6), tov((double)slen)); ··· 11474 11477 } 11475 11478 11476 11479 static jsval_t builtin_Number(struct js *js, jsval_t *args, int nargs) { 11477 - jsval_t nval; 11478 - if (nargs == 0) { 11479 - nval = tov(0.0); 11480 - } else { 11481 - jsval_t arg = args[0]; 11482 - if (vtype(arg) == T_NUM) { 11483 - nval = arg; 11484 - } else if (vtype(arg) == T_BOOL) { 11485 - nval = tov(vdata(arg) ? 1.0 : 0.0); 11486 - } else if (vtype(arg) == T_STR) { 11487 - jsoff_t len; 11488 - jsoff_t off = vstr(js, arg, &len); 11489 - const char *str = (char *) &js->mem[off]; 11490 - while (*str == ' ' || *str == '\t' || *str == '\n' || *str == '\r') str++; 11491 - if (*str == '\0') { 11492 - nval = tov(0.0); 11493 - } else { 11494 - char *end; 11495 - double val = strtod(str, &end); 11496 - while (*end == ' ' || *end == '\t' || *end == '\n' || *end == '\r') end++; 11497 - if (end == str || *end != '\0') { 11498 - nval = tov(NAN); 11499 - } else { 11500 - nval = tov(val); 11501 - } 11502 - } 11503 - } else if (vtype(arg) == T_NULL || vtype(arg) == T_UNDEF) { 11504 - nval = tov(0.0); 11505 - } else { 11506 - nval = tov(0.0); 11507 - } 11508 - } 11509 - if (vtype(js->this_val) == T_OBJ && vtype(get_slot(js, js->this_val, SLOT_PRIMITIVE)) == T_UNDEF) { 11480 + jsval_t nval = tov(nargs > 0 ? js_to_number(js, args[0]) : 0.0); 11481 + jsval_t number_proto = js_get_ctor_proto(js, "Number", 6); 11482 + if (is_unboxed_obj(js, js->this_val, number_proto)) { 11510 11483 set_slot(js, js->this_val, SLOT_PRIMITIVE, nval); 11511 - jsval_t proto = get_ctor_proto(js, "Number", 6); 11512 - if (vtype(proto) == T_OBJ) set_proto(js, js->this_val, proto); 11513 11484 } 11514 11485 return nval; 11515 11486 } 11516 11487 11517 11488 static jsval_t builtin_Boolean(struct js *js, jsval_t *args, int nargs) { 11518 11489 jsval_t bval = mkval(T_BOOL, nargs > 0 && js_truthy(js, args[0]) ? 1 : 0); 11519 - if (vtype(js->this_val) == T_OBJ && vtype(get_slot(js, js->this_val, SLOT_PRIMITIVE)) == T_UNDEF) { 11490 + jsval_t boolean_proto = js_get_ctor_proto(js, "Boolean", 7); 11491 + if (is_unboxed_obj(js, js->this_val, boolean_proto)) { 11520 11492 set_slot(js, js->this_val, SLOT_PRIMITIVE, bval); 11521 - jsval_t proto = get_ctor_proto(js, "Boolean", 7); 11522 - if (vtype(proto) == T_OBJ) set_proto(js, js->this_val, proto); 11523 11493 } 11524 11494 return bval; 11525 11495 } 11526 11496 11527 11497 static jsval_t builtin_Object(struct js *js, jsval_t *args, int nargs) { 11528 - if (nargs == 0) { 11529 - if (vtype(js->this_val) == T_OBJ) return js->this_val; 11498 + if (nargs == 0 || vtype(args[0]) == T_NULL || vtype(args[0]) == T_UNDEF) { 11499 + jsval_t obj_proto = js_get_ctor_proto(js, "Object", 6); 11500 + if (is_unboxed_obj(js, js->this_val, obj_proto)) return js->this_val; 11530 11501 return js_mkobj(js); 11531 11502 } 11532 11503 11533 11504 jsval_t arg = args[0]; 11534 - if (vtype(arg) == T_NULL || vtype(arg) == T_UNDEF) { 11535 - if (vtype(js->this_val) == T_OBJ) return js->this_val; 11536 - return js_mkobj(js); 11537 - } 11538 - 11539 11505 uint8_t t = vtype(arg); 11540 - if (t == T_OBJ || t == T_ARR || t == T_FUNC) { 11541 - return arg; 11542 - } 11543 11506 11507 + if (t == T_OBJ || t == T_ARR || t == T_FUNC) return arg; 11544 11508 if (t == T_STR || t == T_NUM || t == T_BOOL || t == T_BIGINT) { 11545 11509 jsval_t wrapper = js_mkobj(js); 11546 11510 if (is_err(wrapper)) return wrapper;