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 exceptions and throw

+93 -4
+85 -3
src/ant.c
··· 157 157 static jsval_t builtin_array_slice(struct js *js, jsval_t *args, int nargs); 158 158 static jsval_t builtin_array_join(struct js *js, jsval_t *args, int nargs); 159 159 static jsval_t builtin_array_includes(struct js *js, jsval_t *args, int nargs); 160 + static jsval_t builtin_Error(struct js *js, jsval_t *args, int nargs); 160 161 static jsval_t builtin_string_indexOf(struct js *js, jsval_t *args, int nargs); 161 162 static jsval_t builtin_string_substring(struct js *js, jsval_t *args, int nargs); 162 163 static jsval_t builtin_string_split(struct js *js, jsval_t *args, int nargs); ··· 378 379 if (vtype(value) == T_STR) { 379 380 jsoff_t slen, off = vstr(js, value, &slen); 380 381 n = (size_t) snprintf(js->errmsg, sizeof(js->errmsg), "Uncaught: %.*s\n", (int)slen, (char *)&js->mem[off]); 382 + } else if (vtype(value) == T_OBJ) { 383 + jsoff_t name_off = lkp(js, value, "name", 4); 384 + jsoff_t msg_off = lkp(js, value, "message", 7); 385 + 386 + const char *name_str = NULL; 387 + const char *msg_str = NULL; 388 + jsoff_t name_len = 0, msg_len = 0; 389 + 390 + if (name_off > 0) { 391 + jsval_t name_val = resolveprop(js, mkval(T_PROP, name_off)); 392 + if (vtype(name_val) == T_STR) { 393 + jsoff_t off = vstr(js, name_val, &name_len); 394 + name_str = (const char *)&js->mem[off]; 395 + } 396 + } 397 + 398 + if (msg_off > 0) { 399 + jsval_t msg_val = resolveprop(js, mkval(T_PROP, msg_off)); 400 + if (vtype(msg_val) == T_STR) { 401 + jsoff_t off = vstr(js, msg_val, &msg_len); 402 + msg_str = (const char *)&js->mem[off]; 403 + } 404 + } 405 + 406 + if (name_str && msg_str) { 407 + n = (size_t) snprintf(js->errmsg, sizeof(js->errmsg), "Uncaught %.*s: %.*s\n", (int)name_len, name_str, (int)msg_len, msg_str); 408 + } else if (name_str) { 409 + n = (size_t) snprintf(js->errmsg, sizeof(js->errmsg), "Uncaught %.*s\n", (int)name_len, name_str); 410 + } else if (msg_str) { 411 + n = (size_t) snprintf(js->errmsg, sizeof(js->errmsg), "Uncaught: %.*s\n", (int)msg_len, msg_str); 412 + } else { 413 + const char *str = js_str(js, value); 414 + n = (size_t) snprintf(js->errmsg, sizeof(js->errmsg), "Uncaught: %s\n", str); 415 + } 381 416 } else { 382 417 const char *str = js_str(js, value); 383 418 n = (size_t) snprintf(js->errmsg, sizeof(js->errmsg), "Uncaught: %s\n", str); ··· 3333 3368 js->consumed = 0; 3334 3369 3335 3370 if (exe) { 3371 + jsval_t super_constructor = js_mkundef(); 3372 + if (super_len > 0) { 3373 + jsval_t super_val = lookup(js, &js->code[super_off], super_len); 3374 + if (is_err(super_val)) return super_val; 3375 + super_constructor = resolveprop(js, super_val); 3376 + if (vtype(super_constructor) != T_FUNC && vtype(super_constructor) != T_CFUNC) { 3377 + return js_mkerr(js, "super class must be a constructor"); 3378 + } 3379 + } 3380 + 3336 3381 jsoff_t wrapper_size = 256; 3337 3382 if (constructor_body_start > 0) { 3338 3383 wrapper_size += constructor_body_end - constructor_body_start; ··· 3399 3444 if (is_err(code_key)) return code_key; 3400 3445 jsval_t res2 = setprop(js, func_obj, code_key, code_str); 3401 3446 if (is_err(res2)) return res2; 3447 + jsval_t func_scope = mkobj(js, (jsoff_t) vdata(js->scope)); 3448 + 3449 + if (super_len > 0) { 3450 + jsval_t super_key = js_mkstr(js, "super", 5); 3451 + if (is_err(super_key)) return super_key; 3452 + jsval_t res_super = setprop(js, func_scope, super_key, super_constructor); 3453 + if (is_err(res_super)) return res_super; 3454 + } 3455 + 3402 3456 jsval_t scope_key = js_mkstr(js, "__scope", 7); 3403 3457 if (is_err(scope_key)) return scope_key; 3404 - jsval_t res3 = setprop(js, func_obj, scope_key, js->scope); 3458 + jsval_t res3 = setprop(js, func_obj, scope_key, func_scope); 3405 3459 if (is_err(res3)) return res3; 3460 + 3406 3461 jsval_t constructor = mkval(T_FUNC, (unsigned long) vdata(func_obj)); 3407 3462 if (lkp(js, js->scope, class_name, class_name_len) > 0) { 3408 3463 return js_mkerr(js, "'%.*s' already declared", (int) class_name_len, class_name); ··· 3411 3466 if (is_err(x)) return x; 3412 3467 } 3413 3468 3414 - (void) super_off; 3415 - (void) super_len; 3416 3469 (void) class_body_start; 3417 3470 return js_mkundef(); 3418 3471 } ··· 3542 3595 } 3543 3596 3544 3597 return mkval(T_ARR, vdata(arr)); 3598 + } 3599 + 3600 + static jsval_t builtin_Error(struct js *js, jsval_t *args, int nargs) { 3601 + jsval_t err_obj = js->this_val; 3602 + bool use_this = (vtype(err_obj) == T_OBJ); 3603 + 3604 + if (!use_this) { 3605 + err_obj = mkobj(js, 0); 3606 + } 3607 + 3608 + jsval_t message = js_mkstr(js, "", 0); 3609 + if (nargs > 0) { 3610 + if (vtype(args[0]) == T_STR) { 3611 + message = args[0]; 3612 + } else { 3613 + const char *str = js_str(js, args[0]); 3614 + message = js_mkstr(js, str, strlen(str)); 3615 + } 3616 + } 3617 + 3618 + jsval_t message_key = js_mkstr(js, "message", 7); 3619 + setprop(js, err_obj, message_key, message); 3620 + 3621 + jsval_t name_key = js_mkstr(js, "name", 4); 3622 + jsval_t name_val = js_mkstr(js, "Error", 5); 3623 + setprop(js, err_obj, name_key, name_val); 3624 + 3625 + return err_obj; 3545 3626 } 3546 3627 3547 3628 static jsval_t builtin_object_keys(struct js *js, jsval_t *args, int nargs) { ··· 4495 4576 setprop(js, glob, js_mkstr(js, "Number", 6), js_mkfun(builtin_Number)); 4496 4577 setprop(js, glob, js_mkstr(js, "Boolean", 7), js_mkfun(builtin_Boolean)); 4497 4578 setprop(js, glob, js_mkstr(js, "Array", 5), js_mkfun(builtin_Array)); 4579 + setprop(js, glob, js_mkstr(js, "Error", 5), js_mkfun(builtin_Error)); 4498 4580 4499 4581 jsval_t p_proto = js_mkobj(js); 4500 4582 setprop(js, p_proto, js_mkstr(js, "then", 4), js_mkfun(builtin_promise_then));
+8 -1
tests/throw.cjs
··· 1 + class Meow extends Error { 2 + constructor() { 3 + super('meow'); 4 + this.name = 'MeowError'; 5 + } 6 + } 7 + 1 8 function meow() { 2 - throw 'meow'; 9 + throw new Meow(); 3 10 } 4 11 5 12 meow();