suite passes now with mcode->mach lowering
This commit is contained in:
@@ -9788,6 +9788,57 @@ static JSValue js_mach_eval_mcode (JSContext *ctx, JSValue this_val, int argc, J
|
||||
return result;
|
||||
}
|
||||
|
||||
/* mach_dump_mcode(name, mcode_json, env?) - compile mcode IR and dump bytecode disassembly */
|
||||
static JSValue js_mach_dump_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_dump_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_dump_mcode: failed to parse mcode JSON");
|
||||
}
|
||||
|
||||
if (!cJSON_GetObjectItemCaseSensitive (mcode, "filename"))
|
||||
cJSON_AddStringToObject (mcode, "filename", name);
|
||||
|
||||
MachCode *mc = mach_compile_mcode (mcode);
|
||||
cJSON_Delete (mcode);
|
||||
|
||||
if (!mc) {
|
||||
JS_FreeCString (ctx, name);
|
||||
return JS_ThrowInternalError (ctx, "mach_dump_mcode: compilation failed");
|
||||
}
|
||||
|
||||
JSValue env = (argc >= 3 && JS_IsGCObject (argv[2])) ? argv[2] : JS_NULL;
|
||||
|
||||
JSGCRef env_ref;
|
||||
JS_PushGCRef (ctx, &env_ref);
|
||||
env_ref.val = env;
|
||||
|
||||
/* Serialize to binary then dump */
|
||||
size_t bin_size;
|
||||
uint8_t *bin = JS_SerializeMachCode (mc, &bin_size);
|
||||
JS_FreeMachCode (mc);
|
||||
JS_DumpMachBin (ctx, bin, bin_size, env_ref.val);
|
||||
sys_free (bin);
|
||||
|
||||
JS_PopGCRef (ctx, &env_ref);
|
||||
JS_FreeCString (ctx, name);
|
||||
return JS_NULL;
|
||||
}
|
||||
|
||||
/* mach_compile_mcode_bin(name, mcode_json) - compile mcode IR to serialized binary blob */
|
||||
static JSValue js_mach_compile_mcode_bin (JSContext *ctx, JSValue this_val, int argc, JSValue *argv) {
|
||||
if (argc < 2 || !JS_IsText (argv[0]) || !JS_IsText (argv[1]))
|
||||
@@ -10920,6 +10971,7 @@ static void JS_AddIntrinsicBaseObjects (JSContext *ctx) {
|
||||
/* Core functions - using GC-safe helper */
|
||||
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, "mach_dump_mcode", js_mach_dump_mcode, 3);
|
||||
js_set_global_cfunc(ctx, "mach_compile_mcode_bin", js_mach_compile_mcode_bin, 2);
|
||||
js_set_global_cfunc(ctx, "stone", js_cell_stone, 1);
|
||||
js_set_global_cfunc(ctx, "length", js_cell_length, 1);
|
||||
|
||||
Reference in New Issue
Block a user