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.

feat: add process module with cwd and argv support

+142 -30
+3 -1
include/runtime.h
··· 5 5 6 6 struct ant_runtime { 7 7 struct js *js; 8 + char **argv; 8 9 jsval_t ant_obj; 10 + int argc; 9 11 int crypto_initialized; 10 12 int external_event_loop_active; 11 13 }; 12 14 13 15 extern struct ant_runtime *const rt; 14 - struct ant_runtime *ant_runtime_init(struct js *js); 16 + struct ant_runtime *ant_runtime_init(struct js *js, int argc, char **argv); 15 17 16 18 #endif
+1 -1
meson.build
··· 74 74 build_date = run_command('date', '+%Y-%m-%d', check: true).stdout().strip() 75 75 76 76 version_conf = configuration_data() 77 - version_conf.set('ANT_VERSION', '0.1.0.28') 77 + version_conf.set('ANT_VERSION', '0.1.0.30') 78 78 version_conf.set('ANT_GIT_HASH', git_hash) 79 79 version_conf.set('ANT_BUILD_DATE', build_date) 80 80
+113 -25
src/ant.c
··· 249 249 bool owns_mem; // true if js owns the memory buffer (dynamic allocation) 250 250 jsoff_t max_size; // maximum allowed memory size (for dynamic growth) 251 251 bool had_newline; // true if newline was crossed before current token 252 + jsval_t thrown_value; // stores the actual thrown value for catch blocks 252 253 }; 253 254 254 255 enum { ··· 296 297 static jsoff_t propref_key(jsval_t v) { return (v >> 24U) & 0xffffffU; } 297 298 298 299 static const char *typestr(uint8_t t) { 299 - return t == T_CFUNC ? "function" : typestr_raw(t); 300 + if (t == T_CFUNC) return "function"; 301 + if (t == T_ARR) return "object"; 302 + return typestr_raw(t); 300 303 } 301 304 302 305 uint8_t vtype(jsval_t v) { ··· 401 404 static jsval_t builtin_Array_from(struct js *js, jsval_t *args, int nargs); 402 405 static jsval_t builtin_Array_of(struct js *js, jsval_t *args, int nargs); 403 406 static jsval_t builtin_Error(struct js *js, jsval_t *args, int nargs); 407 + static jsval_t builtin_EvalError(struct js *js, jsval_t *args, int nargs); 408 + static jsval_t builtin_RangeError(struct js *js, jsval_t *args, int nargs); 409 + static jsval_t builtin_ReferenceError(struct js *js, jsval_t *args, int nargs); 410 + static jsval_t builtin_SyntaxError(struct js *js, jsval_t *args, int nargs); 411 + static jsval_t builtin_TypeError(struct js *js, jsval_t *args, int nargs); 412 + static jsval_t builtin_URIError(struct js *js, jsval_t *args, int nargs); 413 + static jsval_t builtin_InternalError(struct js *js, jsval_t *args, int nargs); 404 414 static jsval_t js_import_stmt(struct js *js); 405 415 static jsval_t js_export_stmt(struct js *js); 406 416 static jsval_t builtin_import(struct js *js, jsval_t *args, int nargs); ··· 1702 1712 format_error_stack(js, &n, line, col, true, error_line, error_col); 1703 1713 1704 1714 js->flags |= F_THROW; 1715 + js->thrown_value = value; 1705 1716 js->pos = js->clen; 1706 1717 js->tok = TOK_EOF; 1707 1718 js->consumed = 0; ··· 6927 6938 strncpy(saved_errmsg, js->errmsg, sizeof(saved_errmsg) - 1); 6928 6939 saved_errmsg[sizeof(saved_errmsg) - 1] = '\0'; 6929 6940 6930 - jsval_t err_obj = mkobj(js, 0); 6931 - jsval_t msg_key = js_mkstr(js, "message", 7); 6932 - jsval_t name_key = js_mkstr(js, "name", 4); 6933 - 6934 - char *colon = strchr(saved_errmsg, ':'); 6935 - if (colon && strncmp(saved_errmsg, "Uncaught ", 9) == 0) { 6936 - char *type_start = saved_errmsg + 9; 6937 - size_t type_len = colon - type_start; 6938 - char *msg_start = colon + 2; 6939 - char *newline = strchr(msg_start, '\n'); 6940 - size_t msg_len = newline ? (size_t)(newline - msg_start) : strlen(msg_start); 6941 - 6942 - jsval_t name_val = js_mkstr(js, type_start, type_len); 6943 - jsval_t msg_val = js_mkstr(js, msg_start, msg_len); 6944 - setprop(js, err_obj, name_key, name_val); 6945 - setprop(js, err_obj, msg_key, msg_val); 6946 - } else { 6947 - jsval_t msg_val = js_mkstr(js, saved_errmsg, strlen(saved_errmsg)); 6948 - jsval_t name_val = js_mkstr(js, "Error", 5); 6949 - setprop(js, err_obj, name_key, name_val); 6950 - setprop(js, err_obj, msg_key, msg_val); 6951 - } 6952 - 6953 - exception_value = err_obj; 6941 + exception_value = js->thrown_value; 6942 + js->thrown_value = js_mkundef(); 6954 6943 js->errmsg[0] = '\0'; 6955 6944 } 6956 6945 ··· 8297 8286 8298 8287 return err_obj; 8299 8288 } 8289 + 8290 + #define DEFINE_ERROR_BUILTIN(name, name_str, name_len) \ 8291 + static jsval_t builtin_##name(struct js *js, jsval_t *args, int nargs) { \ 8292 + jsval_t err_obj = js->this_val; \ 8293 + bool use_this = (vtype(err_obj) == T_OBJ); \ 8294 + if (!use_this) err_obj = mkobj(js, 0); \ 8295 + jsval_t message = js_mkstr(js, "", 0); \ 8296 + if (nargs > 0) { \ 8297 + if (vtype(args[0]) == T_STR) { \ 8298 + message = args[0]; \ 8299 + } else { \ 8300 + const char *str = js_str(js, args[0]); \ 8301 + message = js_mkstr(js, str, strlen(str)); \ 8302 + } \ 8303 + } \ 8304 + setprop(js, err_obj, js_mkstr(js, "message", 7), message); \ 8305 + setprop(js, err_obj, js_mkstr(js, "name", 4), js_mkstr(js, name_str, name_len)); \ 8306 + return err_obj; \ 8307 + } 8308 + 8309 + DEFINE_ERROR_BUILTIN(EvalError, "EvalError", 9) 8310 + DEFINE_ERROR_BUILTIN(RangeError, "RangeError", 10) 8311 + DEFINE_ERROR_BUILTIN(ReferenceError, "ReferenceError", 14) 8312 + DEFINE_ERROR_BUILTIN(SyntaxError, "SyntaxError", 11) 8313 + DEFINE_ERROR_BUILTIN(TypeError, "TypeError", 9) 8314 + DEFINE_ERROR_BUILTIN(URIError, "URIError", 8) 8315 + DEFINE_ERROR_BUILTIN(InternalError, "InternalError", 13) 8316 + 8317 + #undef DEFINE_ERROR_BUILTIN 8300 8318 8301 8319 static jsval_t builtin_RegExp(struct js *js, jsval_t *args, int nargs) { 8302 8320 jsval_t regexp_obj = js->this_val; ··· 13926 13944 setprop(js, error_proto, js_mkstr(js, "name", 4), js_mkstr(js, "Error", 5)); 13927 13945 setprop(js, error_proto, js_mkstr(js, "message", 7), js_mkstr(js, "", 0)); 13928 13946 13947 + jsval_t evalerror_proto = js_mkobj(js); 13948 + set_proto(js, evalerror_proto, error_proto); 13949 + setprop(js, evalerror_proto, js_mkstr(js, "name", 4), js_mkstr(js, "EvalError", 9)); 13950 + 13951 + jsval_t rangeerror_proto = js_mkobj(js); 13952 + set_proto(js, rangeerror_proto, error_proto); 13953 + setprop(js, rangeerror_proto, js_mkstr(js, "name", 4), js_mkstr(js, "RangeError", 10)); 13954 + 13955 + jsval_t referenceerror_proto = js_mkobj(js); 13956 + set_proto(js, referenceerror_proto, error_proto); 13957 + setprop(js, referenceerror_proto, js_mkstr(js, "name", 4), js_mkstr(js, "ReferenceError", 14)); 13958 + 13959 + jsval_t syntaxerror_proto = js_mkobj(js); 13960 + set_proto(js, syntaxerror_proto, error_proto); 13961 + setprop(js, syntaxerror_proto, js_mkstr(js, "name", 4), js_mkstr(js, "SyntaxError", 11)); 13962 + 13963 + jsval_t typeerror_proto = js_mkobj(js); 13964 + set_proto(js, typeerror_proto, error_proto); 13965 + setprop(js, typeerror_proto, js_mkstr(js, "name", 4), js_mkstr(js, "TypeError", 9)); 13966 + 13967 + jsval_t urierror_proto = js_mkobj(js); 13968 + set_proto(js, urierror_proto, error_proto); 13969 + setprop(js, urierror_proto, js_mkstr(js, "name", 4), js_mkstr(js, "URIError", 8)); 13970 + 13971 + jsval_t internalerror_proto = js_mkobj(js); 13972 + set_proto(js, internalerror_proto, error_proto); 13973 + setprop(js, internalerror_proto, js_mkstr(js, "name", 4), js_mkstr(js, "InternalError", 13)); 13974 + 13929 13975 jsval_t date_proto = js_mkobj(js); 13930 13976 set_proto(js, date_proto, object_proto); 13931 13977 setprop(js, date_proto, js_mkstr(js, "getTime", 7), js_mkfun(builtin_Date_getTime)); ··· 14059 14105 setprop(js, err_ctor_obj, js_mkstr(js, "__native_func", 13), js_mkfun(builtin_Error)); 14060 14106 setprop(js, err_ctor_obj, js_mkstr(js, "prototype", 9), error_proto); 14061 14107 setprop(js, glob, js_mkstr(js, "Error", 5), mkval(T_FUNC, vdata(err_ctor_obj))); 14108 + 14109 + jsval_t evalerr_ctor_obj = mkobj(js, 0); 14110 + set_proto(js, evalerr_ctor_obj, function_proto); 14111 + setprop(js, evalerr_ctor_obj, js_mkstr(js, "__native_func", 13), js_mkfun(builtin_EvalError)); 14112 + setprop(js, evalerr_ctor_obj, js_mkstr(js, "prototype", 9), evalerror_proto); 14113 + setprop(js, glob, js_mkstr(js, "EvalError", 9), mkval(T_FUNC, vdata(evalerr_ctor_obj))); 14114 + 14115 + jsval_t rangeerr_ctor_obj = mkobj(js, 0); 14116 + set_proto(js, rangeerr_ctor_obj, function_proto); 14117 + setprop(js, rangeerr_ctor_obj, js_mkstr(js, "__native_func", 13), js_mkfun(builtin_RangeError)); 14118 + setprop(js, rangeerr_ctor_obj, js_mkstr(js, "prototype", 9), rangeerror_proto); 14119 + setprop(js, glob, js_mkstr(js, "RangeError", 10), mkval(T_FUNC, vdata(rangeerr_ctor_obj))); 14120 + 14121 + jsval_t referr_ctor_obj = mkobj(js, 0); 14122 + set_proto(js, referr_ctor_obj, function_proto); 14123 + setprop(js, referr_ctor_obj, js_mkstr(js, "__native_func", 13), js_mkfun(builtin_ReferenceError)); 14124 + setprop(js, referr_ctor_obj, js_mkstr(js, "prototype", 9), referenceerror_proto); 14125 + setprop(js, glob, js_mkstr(js, "ReferenceError", 14), mkval(T_FUNC, vdata(referr_ctor_obj))); 14126 + 14127 + jsval_t syntaxerr_ctor_obj = mkobj(js, 0); 14128 + set_proto(js, syntaxerr_ctor_obj, function_proto); 14129 + setprop(js, syntaxerr_ctor_obj, js_mkstr(js, "__native_func", 13), js_mkfun(builtin_SyntaxError)); 14130 + setprop(js, syntaxerr_ctor_obj, js_mkstr(js, "prototype", 9), syntaxerror_proto); 14131 + setprop(js, glob, js_mkstr(js, "SyntaxError", 11), mkval(T_FUNC, vdata(syntaxerr_ctor_obj))); 14132 + 14133 + jsval_t typeerr_ctor_obj = mkobj(js, 0); 14134 + set_proto(js, typeerr_ctor_obj, function_proto); 14135 + setprop(js, typeerr_ctor_obj, js_mkstr(js, "__native_func", 13), js_mkfun(builtin_TypeError)); 14136 + setprop(js, typeerr_ctor_obj, js_mkstr(js, "prototype", 9), typeerror_proto); 14137 + setprop(js, glob, js_mkstr(js, "TypeError", 9), mkval(T_FUNC, vdata(typeerr_ctor_obj))); 14138 + 14139 + jsval_t urierr_ctor_obj = mkobj(js, 0); 14140 + set_proto(js, urierr_ctor_obj, function_proto); 14141 + setprop(js, urierr_ctor_obj, js_mkstr(js, "__native_func", 13), js_mkfun(builtin_URIError)); 14142 + setprop(js, urierr_ctor_obj, js_mkstr(js, "prototype", 9), urierror_proto); 14143 + setprop(js, glob, js_mkstr(js, "URIError", 8), mkval(T_FUNC, vdata(urierr_ctor_obj))); 14144 + 14145 + jsval_t internerr_ctor_obj = mkobj(js, 0); 14146 + set_proto(js, internerr_ctor_obj, function_proto); 14147 + setprop(js, internerr_ctor_obj, js_mkstr(js, "__native_func", 13), js_mkfun(builtin_InternalError)); 14148 + setprop(js, internerr_ctor_obj, js_mkstr(js, "prototype", 9), internalerror_proto); 14149 + setprop(js, glob, js_mkstr(js, "InternalError", 13), mkval(T_FUNC, vdata(internerr_ctor_obj))); 14062 14150 14063 14151 jsval_t regex_ctor_obj = mkobj(js, 0); 14064 14152 set_proto(js, regex_ctor_obj, function_proto);
+1 -1
src/main.c
··· 182 182 } 183 183 184 184 if (gct->count > 0) js_setgct(js, gct->ival[0]); 185 - ant_runtime_init(js); 185 + ant_runtime_init(js, argc, argv); 186 186 187 187 init_builtin_module(); 188 188 init_buffer_module();
+20 -1
src/modules/process.c
··· 113 113 return obj; 114 114 } 115 115 116 - void init_process_module(void) { 116 + static jsval_t process_cwd(struct js *js, jsval_t *args, int nargs) { 117 + (void)args; 118 + (void)nargs; 119 + 120 + char cwd[4096]; 121 + if (getcwd(cwd, sizeof(cwd)) != NULL) { 122 + return js_mkstr(js, cwd, strlen(cwd)); 123 + } 124 + return js_mkundef(); 125 + } 126 + 127 + void init_process_module() { 117 128 struct js *js = rt->js; 118 129 119 130 jsval_t process_obj = js_mkobj(js); 120 131 jsval_t env_obj = js_mkobj(js); 132 + jsval_t argv_arr = js_mkarr(js); 121 133 122 134 js_set(js, process_obj, "env", env_obj); 123 135 js_set(js, process_obj, "exit", js_mkfun(process_exit)); ··· 154 166 load_dotenv_file(js, env_obj); 155 167 js_set_getter(js, env_obj, env_getter); 156 168 js_set(js, env_obj, "toObject", js_mkfun(env_to_object)); 169 + 170 + for (int i = 0; i < rt->argc; i++) { 171 + js_arr_push(js, argv_arr, js_mkstr(js, rt->argv[i], strlen(rt->argv[i]))); 172 + } 173 + 174 + js_set(js, process_obj, "argv", argv_arr); 175 + js_set(js, process_obj, "cwd", js_mkfun(process_cwd)); 157 176 158 177 js_set(js, process_obj, "@@toStringTag", js_mkstr(js, "process", 7)); 159 178 js_set(js, js_glob(js), "process", process_obj);
+4 -1
src/runtime.c
··· 6 6 7 7 struct ant_runtime *const rt = &runtime; 8 8 9 - struct ant_runtime *ant_runtime_init(struct js *js) { 9 + struct ant_runtime *ant_runtime_init(struct js *js, int argc, char **argv) { 10 10 runtime.js = js; 11 11 runtime.ant_obj = js_mkobj(js); 12 12 runtime.crypto_initialized = 0; 13 13 runtime.external_event_loop_active = 0; 14 + 15 + runtime.argc = argc; 16 + runtime.argv = argv; 14 17 15 18 js_set(js, js_glob(js), "Ant", runtime.ant_obj); 16 19 js_set(js, js_glob(js), "global", js_glob(js));