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.

v8/node error shim

+125
+1
include/errors.h
··· 53 53 ant_value_t js_make_error_silent(ant_t *js, js_err_type_t err_type, const char *message); 54 54 55 55 ant_value_t js_capture_raw_stack(ant_t *js); 56 + ant_value_t js_build_callsite_array(ant_t *js); 56 57 ant_value_t js_throw(ant_t *js, ant_value_t value); 57 58 58 59 #define js_mkerr(js, ...) js_create_error(js, JS_ERR_TYPE, js_mkundef(), __VA_ARGS__)
+28
src/ant.c
··· 4947 4947 return arr; 4948 4948 } 4949 4949 4950 + static ant_value_t builtin_error_captureStackTrace(ant_t *js, ant_value_t *args, int nargs) { 4951 + if (nargs < 1 || !is_object_type(args[0])) { 4952 + return js_mkerr(js, "argument must be an object"); 4953 + } 4954 + 4955 + ant_value_t target = args[0]; 4956 + ant_value_t error_ctor = lkp_val(js, js->global, "Error", 5); 4957 + ant_value_t prep = js_mkundef(); 4958 + 4959 + if (vtype(error_ctor) == T_FUNC || vtype(error_ctor) == T_CFUNC) { 4960 + prep = lkp_val(js, js_func_obj(error_ctor), "prepareStackTrace", 17); 4961 + } 4962 + 4963 + if (vtype(prep) == T_FUNC || vtype(prep) == T_CFUNC) { 4964 + ant_value_t callsites = js_build_callsite_array(js); 4965 + ant_value_t prep_args[2] = { target, callsites }; 4966 + ant_value_t result = sv_vm_call(js->vm, js, prep, js_mkundef(), prep_args, 2, NULL, false); 4967 + if (js->thrown_exists) return js_mkundef(); 4968 + js_set(js, target, "stack", result); 4969 + js_set_descriptor(js, js_as_obj(target), "stack", 5, JS_DESC_W | JS_DESC_C); 4970 + } else js_capture_stack(js, target); 4971 + 4972 + return js_mkundef(); 4973 + } 4974 + 4950 4975 static ant_value_t builtin_error_isError(ant_t *js, ant_value_t *args, int nargs) { 4951 4976 if (nargs < 1) return js_false; 4952 4977 ant_value_t val = args[0]; ··· 11732 11757 set_slot(err_ctor_obj, SLOT_CFUNC, js_mkfun(builtin_Error)); 11733 11758 js_setprop_nonconfigurable(js, err_ctor_obj, "prototype", 9, error_proto); 11734 11759 js_setprop(js, err_ctor_obj, ANT_STRING("name"), ANT_STRING("Error")); 11760 + 11735 11761 ant_value_t err_ctor_func = js_obj_to_func(err_ctor_obj); 11736 11762 js_setprop(js, glob, ANT_STRING("Error"), err_ctor_func); 11737 11763 js_setprop(js, error_proto, js_mkstr(js, "constructor", 11), err_ctor_func); 11738 11764 js_set_descriptor(js, error_proto, "constructor", 11, JS_DESC_W | JS_DESC_C); 11739 11765 js_setprop(js, err_ctor_func, ANT_STRING("isError"), js_mkfun(builtin_error_isError)); 11766 + js_setprop(js, err_ctor_func, ANT_STRING("captureStackTrace"), js_mkfun(builtin_error_captureStackTrace)); 11767 + js_setprop(js, err_ctor_func, ANT_STRING("stackTraceLimit"), js_mknum(10)); 11740 11768 11741 11769 #define REGISTER_ERROR_SUBTYPE(name_str) do { \ 11742 11770 ant_value_t proto = js_mkobj(js); \
+96
src/errors.c
··· 854 854 return mkval(T_ERR, 0); 855 855 } 856 856 857 + enum { 858 + CS_FILE = 0, 859 + CS_LINE, 860 + CS_COL, 861 + CS_NAME 862 + }; 863 + 864 + static ant_value_t callsite_field(ant_t *js, int field) { 865 + ant_value_t data = js_get_slot(js->this_val, SLOT_DATA); 866 + if (vtype(data) != T_ARR) return js_mkundef(); 867 + return js_arr_get(js, data, field); 868 + } 869 + 870 + static ant_value_t callsite_getFileName(ant_t *js, ant_value_t *args, int nargs) { return callsite_field(js, CS_FILE); } 871 + static ant_value_t callsite_getLineNumber(ant_t *js, ant_value_t *args, int nargs) { return callsite_field(js, CS_LINE); } 872 + static ant_value_t callsite_getColumnNumber(ant_t *js, ant_value_t *args, int nargs) { return callsite_field(js, CS_COL); } 873 + static ant_value_t callsite_getFunctionName(ant_t *js, ant_value_t *args, int nargs) { return callsite_field(js, CS_NAME); } 874 + 875 + static ant_value_t callsite_getTypeName(ant_t *js, ant_value_t *args, int nargs) { return js_mknull(); } 876 + static ant_value_t callsite_getMethodName(ant_t *js, ant_value_t *args, int nargs) { return callsite_field(js, CS_NAME); } 877 + 878 + static ant_value_t callsite_isNative(ant_t *js, ant_value_t *args, int nargs) { return js_false; } 879 + static ant_value_t callsite_isToplevel(ant_t *js, ant_value_t *args, int nargs) { return js_false; } 880 + static ant_value_t callsite_isEval(ant_t *js, ant_value_t *args, int nargs) { return js_false; } 881 + static ant_value_t callsite_isConstructor(ant_t *js, ant_value_t *args, int nargs) { return js_false; } 882 + static ant_value_t callsite_getEvalOrigin(ant_t *js, ant_value_t *args, int nargs) { return js_mkundef(); } 883 + static ant_value_t callsite_getThis(ant_t *js, ant_value_t *args, int nargs) { return js_mkundef(); } 884 + 885 + static ant_value_t callsite_toString(ant_t *js, ant_value_t *args, int nargs) { 886 + ant_value_t name = callsite_field(js, CS_NAME); 887 + ant_value_t file = callsite_field(js, CS_FILE); 888 + ant_value_t line = callsite_field(js, CS_LINE); 889 + ant_value_t col = callsite_field(js, CS_COL); 890 + 891 + const char *n = js_str(js, name); 892 + const char *f = js_str(js, file); 893 + int l = vtype(line) == T_NUM ? (int)js_getnum(line) : 0; 894 + int c = vtype(col) == T_NUM ? (int)js_getnum(col) : 0; 895 + 896 + char buf[512]; 897 + int len = snprintf(buf, sizeof(buf), "%s (%s:%d:%d)", n, f, l, c); 898 + if (len < 0) len = 0; 899 + return js_mkstr(js, buf, (size_t)len); 900 + } 901 + 902 + typedef struct { 903 + ant_t *js; 904 + ant_value_t arr; 905 + ant_value_t proto; 906 + } callsite_build_ctx_t; 907 + 908 + static bool callsite_visit_frame(ant_t *js, const js_vm_frame_view_t *view, void *ctx) { 909 + callsite_build_ctx_t *c = (callsite_build_ctx_t *)ctx; 910 + 911 + ant_value_t data = mkarr(js); 912 + js_arr_push(js, data, js_mkstr(js, view->file, strlen(view->file))); 913 + js_arr_push(js, data, js_mknum((double)view->line)); 914 + js_arr_push(js, data, js_mknum((double)view->col)); 915 + js_arr_push(js, data, js_mkstr(js, view->name, strlen(view->name))); 916 + 917 + ant_value_t site = js_mkobj(js); 918 + js_set_proto_init(site, c->proto); 919 + js_set_slot(site, SLOT_DATA, data); 920 + 921 + js_arr_push(js, c->arr, site); 922 + return true; 923 + } 924 + 925 + ant_value_t js_build_callsite_array(ant_t *js) { 926 + ant_value_t proto = js_mkobj(js); 927 + 928 + js_set(js, proto, "getFileName", js_mkfun(callsite_getFileName)); 929 + js_set(js, proto, "getLineNumber", js_mkfun(callsite_getLineNumber)); 930 + js_set(js, proto, "getColumnNumber", js_mkfun(callsite_getColumnNumber)); 931 + js_set(js, proto, "getFunctionName", js_mkfun(callsite_getFunctionName)); 932 + js_set(js, proto, "getTypeName", js_mkfun(callsite_getTypeName)); 933 + js_set(js, proto, "getMethodName", js_mkfun(callsite_getMethodName)); 934 + js_set(js, proto, "isNative", js_mkfun(callsite_isNative)); 935 + js_set(js, proto, "isToplevel", js_mkfun(callsite_isToplevel)); 936 + js_set(js, proto, "isEval", js_mkfun(callsite_isEval)); 937 + js_set(js, proto, "isConstructor", js_mkfun(callsite_isConstructor)); 938 + js_set(js, proto, "getEvalOrigin", js_mkfun(callsite_getEvalOrigin)); 939 + js_set(js, proto, "getThis", js_mkfun(callsite_getThis)); 940 + js_set(js, proto, "toString", js_mkfun(callsite_toString)); 941 + 942 + ant_value_t arr = mkarr(js); 943 + callsite_build_ctx_t ctx = { js, arr, proto }; 944 + 945 + const char *file = (js->errsite.valid && js->errsite.filename) 946 + ? js->errsite.filename 947 + : (js->filename ? js->filename : "<eval>"); 948 + 949 + error_visit_vm_stack_frames(js, file, callsite_visit_frame, &ctx); 950 + return arr; 951 + } 952 + 857 953 void js_print_stack_trace_vm(ant_t *js, FILE *stream) { 858 954 const char *fallback_file = js->filename ? js->filename : "<unknown>"; 859 955 if (!stream) return;