sequentially-read-a-file as js module

This commit is contained in:
minjaesong
2023-01-08 16:23:50 +09:00
parent ca39263131
commit 132c31bda5
7 changed files with 237 additions and 290 deletions

View File

@@ -1060,20 +1060,6 @@ print = function(str) {
Object.freeze(unicode);
///////////////////////////////////////////////////////////////////////////////
// install other stuffs
var require = (absdir) => {
let moduleFile = files.open(absdir)
if (!moduleFile.exists) throw Error("No such file: " + absdir)
let moduleScript = moduleFile.sread()
return eval(`let exports = {}; ${moduleScript}; Object.freeze(exports)`)
}
var GL = require("A:/tvdos/include/gl.js")
let checkTerm = `if (sys.peek(-49)&1) throw new InterruptedException();`
let injectIntChk = (s, n) => {
// primitive way of injecting a code; will replace a JS string that matches the regex...
@@ -1085,6 +1071,21 @@ let injectIntChk = (s, n) => {
return k;
}
// install other stuffs
var require = (absdir) => {
let moduleFile = files.open(absdir)
if (!moduleFile.exists) throw Error("No such file: " + absdir)
let moduleScript = moduleFile.sread()
var intchkFunName = `tvdosSIGTERM_${generateRandomHashStr(16)}`;
return eval(`var ${intchkFunName} = function(){ ${checkTerm} };let exports = {}; ${injectIntChk(moduleScript, intchkFunName)}; Object.freeze(exports)`)
}
var GL = require("A:/tvdos/include/gl.js")
// @param cmdsrc JS source code
// @param args arguments for the program, must be Array, and args[0] is always the name of the program, e.g.
// for command line 'echo foo bar', args[0] must be 'echo'