From 924649a69ecbebbf60339f638e73a0595cfb0732 Mon Sep 17 00:00:00 2001 From: "Avi Halachmi (:avih)" Date: Thu, 30 Aug 2018 16:24:24 +0300 Subject: [PATCH] mujs shell: Add compile function. The new compile function compiles a source code string as a script and returns a callable object. The optional 'filename' argument is used to format error messages and stack traces. --- main.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/main.c b/main.c index e99770e..8734b6a 100644 --- a/main.c +++ b/main.c @@ -103,6 +103,13 @@ static void jsB_load(js_State *J) js_pushundefined(J); } +static void jsB_compile(js_State *J) +{ + const char *source = js_tostring(J, 1); + const char *filename = js_isdefined(J, 2) ? js_tostring(J, 2) : "[string]"; + js_loadstring(J, filename, source); +} + static void jsB_print(js_State *J) { int i, top = js_gettop(J); @@ -293,6 +300,9 @@ main(int argc, char **argv) js_newcfunction(J, jsB_load, "load", 1); js_setglobal(J, "load"); + js_newcfunction(J, jsB_compile, "compile", 2); + js_setglobal(J, "compile"); + js_newcfunction(J, jsB_print, "print", 0); js_setglobal(J, "print");