diff --git a/debug/js.c b/debug/js.c index 32b94ef2..c8af212f 100644 --- a/debug/js.c +++ b/debug/js.c @@ -1,4 +1,5 @@ #include "cell.h" +#include "cJSON.h" JSC_CCALL(os_mem_limit, JS_SetMemoryLimit(JS_GetRuntime(js), js2number(js,argv[0]))) JSC_CCALL(os_max_stacksize, JS_SetMaxStackSize(JS_GetRuntime(js), js2number(js,argv[0]))) @@ -13,7 +14,6 @@ JSC_CCALL(os_calc_mem, JS_SetPropertyStr(js,ret,"memory_used_size",number2js(js,mu.memory_used_size)); JS_SetPropertyStr(js,ret,"malloc_count",number2js(js,mu.malloc_count)); JS_SetPropertyStr(js,ret,"memory_used_count",number2js(js,mu.memory_used_count)); - /* atom_count and atom_size removed - atoms are now just strings */ JS_SetPropertyStr(js,ret,"str_count",number2js(js,mu.str_count)); JS_SetPropertyStr(js,ret,"str_size",number2js(js,mu.str_size)); JS_SetPropertyStr(js,ret,"obj_count",number2js(js,mu.obj_count)); @@ -35,47 +35,14 @@ JSC_CCALL(os_calc_mem, JS_SetPropertyStr(js,ret,"binary_object_size",number2js(js,mu.binary_object_size)); ) -// Evaluate a string of JavaScript code in the current QuickJS context. +// Evaluate a string via MACH VM JSC_SSCALL(os_eval, if (!str2) return JS_ThrowReferenceError(js, "Second argument should be the script."); if (!str) return JS_ThrowReferenceError(js, "First argument should be the name of the script."); - JSValue bytecode = JS_Compile(js, str2, strlen(str2), str); - if (JS_IsException(bytecode)) return bytecode; - ret = JS_Integrate(js, bytecode, JS_NULL); -) - -// Compile a string of JavaScript code into a function object. -JSC_SSCALL(js_compile, - if (!str2) return JS_ThrowReferenceError(js, "Second argument should be the script."); - if (!str) return JS_ThrowReferenceError(js, "First argument should be the name of the script."); - ret = JS_Compile(js, str2, strlen(str2), str); -) - -// Link compiled bytecode with environment and execute. -JSC_CCALL(js_integrate, - JSValue env = (argc > 1 && !JS_IsNull(argv[1])) ? argv[1] : JS_NULL; - ret = JS_Integrate(js, argv[0], env); -) - -// Compile a function object into a bytecode blob. -JSC_CCALL(js_compile_blob, - size_t size; - uint8_t *data = JS_WriteObject(js, &size, argv[0], JS_WRITE_OBJ_BYTECODE); - if (!data) { - return JS_ThrowInternalError(js, "Failed to serialize bytecode"); - } - ret = js_new_blob_stoned_copy(js, data, size); - js_free(js, data); -) - -// Compile a bytecode blob into a function object. -JSC_CCALL(js_compile_unblob, - size_t size; - void *data = js_get_blob_data(js, &size, argv[0]); - if (data == -1) return JS_EXCEPTION; - if (!data) return JS_ThrowReferenceError(js, "No data present in blob."); - - return JS_ReadObject(js, data, size, JS_READ_OBJ_BYTECODE); + cJSON *ast = JS_ASTTree(str2, strlen(str2), str); + if (!ast) return JS_ThrowSyntaxError(js, "eval: failed to parse"); + ret = JS_RunMachTree(js, ast, JS_NULL); + cJSON_Delete(ast); ) // Disassemble a function object into a string. @@ -93,10 +60,6 @@ static const JSCFunctionListEntry js_js_funcs[] = { MIST_FUNC_DEF(os, mem_limit, 1), MIST_FUNC_DEF(os, max_stacksize, 1), MIST_FUNC_DEF(os, eval, 2), - MIST_FUNC_DEF(js, compile, 2), - MIST_FUNC_DEF(js, integrate, 2), - MIST_FUNC_DEF(js, compile_blob, 1), - MIST_FUNC_DEF(js, compile_unblob, 1), MIST_FUNC_DEF(js, disassemble, 1), MIST_FUNC_DEF(js, fn_info, 1), }; @@ -105,4 +68,4 @@ JSValue js_js_use(JSContext *js) { JSValue mod = JS_NewObject(js); JS_SetPropertyFunctionList(js,mod,js_js_funcs,countof(js_js_funcs)); return mod; -} \ No newline at end of file +} diff --git a/meson.build b/meson.build index f515a62a..d2e9617f 100644 --- a/meson.build +++ b/meson.build @@ -45,7 +45,6 @@ src += [ # core 'qjs_actor.c', 'miniz.c', 'runtime.c', - 'cell_js.c', 'tokenize.c', 'parse.c', 'mach.c', diff --git a/source/cell.c b/source/cell.c index 56367f2b..a2169087 100644 --- a/source/cell.c +++ b/source/cell.c @@ -288,113 +288,11 @@ static int run_test_suite(size_t heap_size) } /* Run an immediate script string */ -static int run_eval(const char *script_or_file, int print_bytecode, int use_bootstrap_env) -{ - if (!find_cell_shop()) return 1; - - /* Check if argument is a file path */ - struct stat st; - char *script = NULL; - char *allocated_script = NULL; - const char *filename = ""; - - if (stat(script_or_file, &st) == 0 && S_ISREG(st.st_mode)) { - /* It's a file, read its contents */ - FILE *f = fopen(script_or_file, "r"); - if (!f) { - printf("Failed to open file: %s\n", script_or_file); - return 1; - } - allocated_script = malloc(st.st_size + 1); - if (!allocated_script) { - fclose(f); - printf("Failed to allocate memory for script\n"); - return 1; - } - size_t read_size = fread(allocated_script, 1, st.st_size, f); - fclose(f); - allocated_script[read_size] = '\0'; - script = allocated_script; - filename = script_or_file; - } else { - /* Treat as inline script */ - script = (char *)script_or_file; - } - - JSRuntime *rt = JS_NewRuntime(); - if (!rt) { - printf("Failed to create JS runtime\n"); - free(allocated_script); - return 1; - } - - JSContext *ctx = JS_NewContext(rt); - if (!ctx) { - printf("Failed to create JS context\n"); - JS_FreeRuntime(rt); - free(allocated_script); - return 1; - } - - int result = 0; - - JSGCRef bytecode_ref; - JS_PushGCRef(ctx, &bytecode_ref); - bytecode_ref.val = JS_Compile(ctx, script, strlen(script), filename); - if (JS_IsException(bytecode_ref.val)) { - uncaught_exception(ctx, bytecode_ref.val); - JS_PopGCRef(ctx, &bytecode_ref); - result = 1; - } else { - if (print_bytecode) { - printf("=== Compiled Bytecode ===\n"); - JS_DumpFunctionBytecode(ctx, bytecode_ref.val); - } - JSValue env = JS_NULL; - if (use_bootstrap_env) { - JSGCRef env_ref, json_ref, nota_ref, wota_ref; - JS_PushGCRef(ctx, &env_ref); - JS_PushGCRef(ctx, &json_ref); - JS_PushGCRef(ctx, ¬a_ref); - JS_PushGCRef(ctx, &wota_ref); - env_ref.val = JS_NewObject(ctx); - /* Create modules with GC rooting, then stone them */ - json_ref.val = js_json_use(ctx); - nota_ref.val = js_nota_use(ctx); - wota_ref.val = js_wota_use(ctx); - JS_SetPropertyStr(ctx, env_ref.val, "json", JS_Stone(ctx, json_ref.val)); - JS_SetPropertyStr(ctx, env_ref.val, "nota", JS_Stone(ctx, nota_ref.val)); - JS_SetPropertyStr(ctx, env_ref.val, "wota", JS_Stone(ctx, wota_ref.val)); - env = JS_Stone(ctx, env_ref.val); - JS_PopGCRef(ctx, &wota_ref); - JS_PopGCRef(ctx, ¬a_ref); - JS_PopGCRef(ctx, &json_ref); - JS_PopGCRef(ctx, &env_ref); - } - JSValue v = JS_Integrate(ctx, bytecode_ref.val, env); - JS_PopGCRef(ctx, &bytecode_ref); - if (JS_IsException(v)) { - uncaught_exception(ctx, v); - result = 1; - } else { - JS_FreeValue(ctx, v); - } - } - - JS_FreeContext(ctx); - JS_FreeRuntime(rt); - free(allocated_script); - return result; -} - static void print_usage(const char *prog) { printf("Usage: %s [options]