fix suboptimal code
Some checks are pending
ci / Linux (Ubuntu) (push) Waiting to run
ci / linux-asan (push) Waiting to run
ci / linux-msan (push) Waiting to run
ci / linux-ubsan (push) Waiting to run
ci / macOS (push) Waiting to run
ci / macos-asan (push) Waiting to run
ci / macos-ubsan (push) Waiting to run
ci / freebsd (push) Waiting to run
ci / qemu-alpine (arm32v6) (push) Waiting to run
ci / qemu-alpine (arm32v7) (push) Waiting to run
ci / qemu-alpine (arm64v8) (push) Waiting to run
ci / qemu-alpine (i386) (push) Waiting to run
ci / qemu-alpine (s390x) (push) Waiting to run
Some checks are pending
ci / Linux (Ubuntu) (push) Waiting to run
ci / linux-asan (push) Waiting to run
ci / linux-msan (push) Waiting to run
ci / linux-ubsan (push) Waiting to run
ci / macOS (push) Waiting to run
ci / macos-asan (push) Waiting to run
ci / macos-ubsan (push) Waiting to run
ci / freebsd (push) Waiting to run
ci / qemu-alpine (arm32v6) (push) Waiting to run
ci / qemu-alpine (arm32v7) (push) Waiting to run
ci / qemu-alpine (arm64v8) (push) Waiting to run
ci / qemu-alpine (i386) (push) Waiting to run
ci / qemu-alpine (s390x) (push) Waiting to run
This commit is contained in:
parent
d904850a06
commit
7b5659c307
4 changed files with 51 additions and 7 deletions
9
examples/fib_require.js
Normal file
9
examples/fib_require.js
Normal file
|
@ -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);
|
||||||
|
}
|
6
examples/hello-require.js
Normal file
6
examples/hello-require.js
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
/* example of JS module */
|
||||||
|
|
||||||
|
const fibModule = require("./fib_require.js");
|
||||||
|
|
||||||
|
console.log("Hello World");
|
||||||
|
console.log("fib(10)=", fib(10));
|
38
qjs.c
38
qjs.c
|
@ -98,6 +98,34 @@ static int eval_file(JSContext *ctx, const char *filename, int module)
|
||||||
eval_flags = JS_EVAL_TYPE_MODULE;
|
eval_flags = JS_EVAL_TYPE_MODULE;
|
||||||
else
|
else
|
||||||
eval_flags = JS_EVAL_TYPE_GLOBAL;
|
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), "<input>", JS_EVAL_TYPE_MODULE);
|
||||||
ret = eval_buf(ctx, buf, buf_len, filename, eval_flags);
|
ret = eval_buf(ctx, buf, buf_len, filename, eval_flags);
|
||||||
js_free(ctx, buf);
|
js_free(ctx, buf);
|
||||||
return ret;
|
return ret;
|
||||||
|
@ -520,15 +548,10 @@ int main(int argc, char **argv)
|
||||||
}
|
}
|
||||||
|
|
||||||
//POLYFILLS BEGIN
|
//POLYFILLS BEGIN
|
||||||
const char *pf = "async function require(x) {\n"
|
const char *pf = "globalThis.global = globalThis;\n"
|
||||||
" const y = await import(x);\n"
|
|
||||||
" return y;\n"
|
|
||||||
"}\n"
|
|
||||||
"globalThis.require = require;\n"
|
|
||||||
"globalThis.global = globalThis;\n"
|
|
||||||
"global.console.error = console.log\n"
|
"global.console.error = console.log\n"
|
||||||
"global.console.warn = 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"
|
"\n"
|
||||||
"if (typeof os !== 'undefined') {\n"
|
"if (typeof os !== 'undefined') {\n"
|
||||||
" globalThis.sleep = os.sleep;\n"
|
" globalThis.sleep = os.sleep;\n"
|
||||||
|
@ -543,6 +566,7 @@ int main(int argc, char **argv)
|
||||||
" globalThis.loadFile = std.loadFile;\n"
|
" globalThis.loadFile = std.loadFile;\n"
|
||||||
" globalThis.printf = console.log;\n"
|
" globalThis.printf = console.log;\n"
|
||||||
" globalThis.evalFile = std.loadScript;\n"
|
" globalThis.evalFile = std.loadScript;\n"
|
||||||
|
" globalThis.require = std.loadScript;\n"
|
||||||
" globalThis.getURL = std.urlGet;\n"
|
" globalThis.getURL = std.urlGet;\n"
|
||||||
"} else {\n"
|
"} else {\n"
|
||||||
" console.error('std is not defined.');\n"
|
" console.error('std is not defined.');\n"
|
||||||
|
|
5
test.js
Normal file
5
test.js
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
function test() {
|
||||||
|
breakFunction();
|
||||||
|
console.warn("lol it works!");
|
||||||
|
}
|
||||||
|
test()
|
Loading…
Reference in a new issue