diff --git a/qjs.c b/qjs.c index b4ee1c7..d4061c0 100644 --- a/qjs.c +++ b/qjs.c @@ -718,6 +718,32 @@ start: js_std_eval_binary(ctx, qjsc_repl, qjsc_repl_size, 0); } + //POLYFILLS FOR QJS FILES 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 = (moduleSpecifier) => import(moduleSpecifier).then(mod => mod.default || mod);\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); + if (standalone || compile_file) { if (JS_IsException(ret)) { ret = JS_GetException(ctx); diff --git a/repl.js b/repl.js index 3c73e1b..f50dc3d 100644 --- a/repl.js +++ b/repl.js @@ -26,35 +26,7 @@ import * as std from "qjs:std"; import * as os from "qjs:os"; import * as bjson from "qjs:bjson"; - -globalThis.global = globalThis -globalThis.safeGlobal = {} -global.console.error = console.log -global.console.warn = console.log - -if (typeof os !== 'undefined') { - globalThis.sleep = os.sleep - async function setTimeout2(func, ms) {globalThis.clearTimeout = false; await sleep(ms); if (!clearTimeout) { func(); } } - globalThis.setTimeout = setTimeout2 -} else { - console.error('os is not defined.') -} - -if (typeof std !== 'undefined') { - globalThis.urlGet = std.urlGet - globalThis.loadFile = std.loadFile - globalThis.printf = console.log - globalThis.evalFile = std.loadScript - globalThis.require = (moduleSpecifier) => import(moduleSpecifier).then(mod => mod.default || mod); - globalThis.getURL = std.urlGet -} else { - console.error('std is not defined.'); -} - (function(g) { - /* init polyfills */ - g = globalThis - /* add 'bjson', 'os' and 'std' bindings */ g.bjson = bjson; g.os = os;