This commit is contained in:
2026-02-11 14:41:37 -06:00
parent c1910ee1db
commit 8a84be65e1
9 changed files with 1325 additions and 43 deletions

View File

@@ -10249,6 +10249,55 @@ static JSValue js_mcode_run (JSContext *ctx, JSValue this_val, int argc, JSValue
return result;
}
/* mach_eval_mcode(name, mcode_json, env?) - compile mcode IR and run via register VM */
static JSValue js_mach_eval_mcode (JSContext *ctx, JSValue this_val, int argc, JSValue *argv) {
if (argc < 2 || !JS_IsText (argv[0]) || !JS_IsText (argv[1]))
return JS_ThrowTypeError (ctx, "mach_eval_mcode requires (name, mcode_json) text arguments");
const char *name = JS_ToCString (ctx, argv[0]);
if (!name) return JS_EXCEPTION;
const char *json_str = JS_ToCString (ctx, argv[1]);
if (!json_str) {
JS_FreeCString (ctx, name);
return JS_EXCEPTION;
}
cJSON *mcode = cJSON_Parse (json_str);
JS_FreeCString (ctx, json_str);
if (!mcode) {
JS_FreeCString (ctx, name);
return JS_ThrowSyntaxError (ctx, "mach_eval_mcode: failed to parse mcode JSON");
}
/* Set filename on the mcode root if not present */
if (!cJSON_GetObjectItemCaseSensitive (mcode, "filename"))
cJSON_AddStringToObject (mcode, "filename", name);
/* Compile mcode IR → MachCode binary */
MachCode *mc = mach_compile_mcode (mcode);
cJSON_Delete (mcode);
if (!mc) {
JS_FreeCString (ctx, name);
return JS_ThrowInternalError (ctx, "mach_eval_mcode: compilation failed");
}
JSValue env = (argc >= 3 && JS_IsObject (argv[2])) ? argv[2] : JS_NULL;
JSGCRef env_ref;
JS_PushGCRef (ctx, &env_ref);
env_ref.val = env;
JSCodeRegister *code = JS_LoadMachCode (ctx, mc, env_ref.val);
JS_FreeMachCode (mc);
JSValue result = JS_CallRegisterVM (ctx, code, ctx->global_obj, 0, NULL, env_ref.val, JS_NULL);
JS_PopGCRef (ctx, &env_ref);
JS_FreeCString (ctx, name);
return result;
}
/* ============================================================================
* stone() function - deep freeze with blob support
* ============================================================================
@@ -11353,6 +11402,7 @@ static void JS_AddIntrinsicBaseObjects (JSContext *ctx) {
js_set_global_cfunc(ctx, "mcode_run", js_mcode_run, 3);
js_set_global_cfunc(ctx, "mach_compile_ast", js_mach_compile_ast, 2);
js_set_global_cfunc(ctx, "mach_load", js_mach_load, 2);
js_set_global_cfunc(ctx, "mach_eval_mcode", js_mach_eval_mcode, 3);
js_set_global_cfunc(ctx, "stone", js_cell_stone, 1);
js_set_global_cfunc(ctx, "length", js_cell_length, 1);
js_set_global_cfunc(ctx, "call", js_cell_call, 3);