ref: 924649a69ecbebbf60339f638e73a0595cfb0732
parent: 84858c335ed5fe584c7c731f5090f8175f66a675
author: Avi Halachmi (:avih) <avihpit@yahoo.com>
date: Thu Aug 30 12:24:24 EDT 2018
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.
--- a/main.c
+++ b/main.c
@@ -103,6 +103,13 @@
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);
@@ -292,6 +299,9 @@
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");