From c56444556d8ad5a983a63826355da7eca8b9013d Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Wed, 25 Feb 2026 23:30:32 -0600 Subject: [PATCH] fix log in engine err --- source/cell.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/source/cell.c b/source/cell.c index fb51b9f4..59f84152 100644 --- a/source/cell.c +++ b/source/cell.c @@ -313,6 +313,26 @@ void actor_disrupt(JSContext *ctx) JSValue js_core_internal_os_use(JSContext *js); JSValue js_core_json_use(JSContext *js); +/* Engine-env log proxy: routes log("channel", [msg]) through JS_Log. + Before set_log is called, JS_Log falls back to stderr. + After set_log, JS_Log forwards to the engine's JS log function. + This exists so mcode-generated type-check error paths (which always + access 'log' as an intrinsic) work inside engine.cm itself. */ +static JSValue js_engine_log(JSContext *js, JSValue self, + int argc, JSValue *argv) { + if (argc < 2) return JS_NULL; + const char *channel = JS_ToCString(js, argv[0]); + if (!channel) return JS_NULL; + JSValue msg_val = JS_GetPropertyNumber(js, argv[1], 0); + const char *msg = JS_ToCString(js, msg_val); + if (msg) { + JS_Log(js, channel, "%s", msg); + JS_FreeCString(js, msg); + } + JS_FreeCString(js, channel); + return JS_NULL; +} + void script_startup(JSContext *js) { if (!g_runtime) { @@ -414,6 +434,8 @@ void script_startup(JSContext *js) } tmp = shop_path ? JS_NewString(js, shop_path) : JS_NULL; JS_SetPropertyStr(js, env_ref.val, "shop_path", tmp); + tmp = JS_NewCFunction(js, js_engine_log, "log", 2); + JS_SetPropertyStr(js, env_ref.val, "log", tmp); // Stone the environment JSValue hidden_env = JS_Stone(js, env_ref.val); @@ -761,6 +783,8 @@ int cell_init(int argc, char **argv) } JS_SetPropertyStr(ctx, env_ref.val, "args", args_ref.val); JS_DeleteGCRef(ctx, &args_ref); + tmp = JS_NewCFunction(ctx, js_engine_log, "log", 2); + JS_SetPropertyStr(ctx, env_ref.val, "log", tmp); JSValue hidden_env = JS_Stone(ctx, env_ref.val); g_crash_ctx = ctx;