diff --git a/examples/fib_require.js b/examples/fib_require.js new file mode 100644 index 0000000..eb98fec --- /dev/null +++ b/examples/fib_require.js @@ -0,0 +1,9 @@ +function fib(n) +{ + if (n <= 0) + return 0; + else if (n == 1) + return 1; + else + return fib(n - 1) + fib(n - 2); +} diff --git a/examples/hello-require.js b/examples/hello-require.js new file mode 100644 index 0000000..65dadfb --- /dev/null +++ b/examples/hello-require.js @@ -0,0 +1,6 @@ +/* example of JS module */ + +const fibModule = require("./fib_require.js"); + +console.log("Hello World"); +console.log("fib(10)=", fib(10)); diff --git a/qjs.c b/qjs.c index 0b56bf5..d9409b8 100644 --- a/qjs.c +++ b/qjs.c @@ -98,6 +98,34 @@ static int eval_file(JSContext *ctx, const char *filename, int module) eval_flags = JS_EVAL_TYPE_MODULE; else eval_flags = JS_EVAL_TYPE_GLOBAL; + + //POLYFILLS BEGIN + const char *pf = "globalThis.global = globalThis;\n" + "global.console.error = console.log\n" + "global.console.warn = console.log\n" + "globalThis.breakFunction = () => { throw new Error('Function Break'); };\n" + "\n" + "if (typeof os !== 'undefined') {\n" + " globalThis.sleep = os.sleep;\n" + " async function setTimeout2(func, ms) {globalThis.clearTimeout = false; await sleep(ms); if (!clearTimeout) { func(); } }\n" + " globalThis.setTimeout = setTimeout2\n" + "} else {\n" + " console.error('os is not defined.');\n" + "}\n" + "\n" + "if (typeof std !== 'undefined') {\n" + " globalThis.urlGet = std.urlGet;\n" + " globalThis.loadFile = std.loadFile;\n" + " globalThis.printf = console.log;\n" + " globalThis.evalFile = std.loadScript;\n" + " globalThis.require = std.loadScript;\n" + " globalThis.getURL = std.urlGet;\n" + "} else {\n" + " console.error('std is not defined.');\n" + "}\n"; + + + eval_buf(ctx, pf, strlen(pf), "", JS_EVAL_TYPE_MODULE); ret = eval_buf(ctx, buf, buf_len, filename, eval_flags); js_free(ctx, buf); return ret; @@ -520,15 +548,10 @@ int main(int argc, char **argv) } //POLYFILLS BEGIN - const char *pf = "async function require(x) {\n" - " const y = await import(x);\n" - " return y;\n" - "}\n" - "globalThis.require = require;\n" - "globalThis.global = globalThis;\n" + const char *pf = "globalThis.global = globalThis;\n" "global.console.error = console.log\n" "global.console.warn = console.log\n" - "globalThis.break = () => { throw new Error('Break'); };\n" + "globalThis.breakFunction = () => { throw new Error('Function Break'); };\n" "\n" "if (typeof os !== 'undefined') {\n" " globalThis.sleep = os.sleep;\n" @@ -543,6 +566,7 @@ int main(int argc, char **argv) " globalThis.loadFile = std.loadFile;\n" " globalThis.printf = console.log;\n" " globalThis.evalFile = std.loadScript;\n" + " globalThis.require = std.loadScript;\n" " globalThis.getURL = std.urlGet;\n" "} else {\n" " console.error('std is not defined.');\n" diff --git a/test.js b/test.js new file mode 100644 index 0000000..1be0311 --- /dev/null +++ b/test.js @@ -0,0 +1,5 @@ +function test() { +breakFunction(); +console.warn("lol it works!"); +} +test()