2019-04-11 10:37:40 +02:00
|
|
|
import { makeRuntimeRejectMsg } from "./NetscriptEvaluator";
|
2019-06-02 21:21:08 +02:00
|
|
|
import { Script } from "./Script/Script";
|
2018-05-05 05:20:19 +02:00
|
|
|
|
|
|
|
// Makes a blob that contains the code of a given script.
|
|
|
|
export function makeScriptBlob(code) {
|
|
|
|
return new Blob([code], {type: "text/javascript"});
|
|
|
|
}
|
|
|
|
|
2019-06-02 21:21:08 +02:00
|
|
|
class ScriptUrl {
|
|
|
|
/**
|
|
|
|
* @param {string} filename
|
|
|
|
* @param {string} url
|
|
|
|
*/
|
|
|
|
constructor(filename, url) {
|
|
|
|
this.filename = filename;
|
|
|
|
this.url = url;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-05 05:20:19 +02:00
|
|
|
// Begin executing a user JS script, and return a promise that resolves
|
|
|
|
// or rejects when the script finishes.
|
|
|
|
// - script is a script to execute (see Script.js). We depend only on .filename and .code.
|
|
|
|
// scripts is an array of other scripts on the server.
|
|
|
|
// env is the global environment that should be visible to all the scripts
|
|
|
|
// (i.e. hack, grow, etc.).
|
|
|
|
// When the promise returned by this resolves, we'll have finished
|
|
|
|
// running the main function of the script.
|
2018-05-17 19:10:12 +02:00
|
|
|
export async function executeJSScript(scripts = [], workerScript) {
|
2018-05-12 02:45:40 +02:00
|
|
|
let loadedModule;
|
2019-06-02 21:21:08 +02:00
|
|
|
let urls = null;
|
2018-05-17 19:10:12 +02:00
|
|
|
let script = workerScript.getScript();
|
2019-06-02 21:21:08 +02:00
|
|
|
if (shouldCompile(script, scripts)) {
|
2018-05-12 02:45:40 +02:00
|
|
|
// The URL at the top is the one we want to import. It will
|
|
|
|
// recursively import all the other modules in the urlStack.
|
|
|
|
//
|
|
|
|
// Webpack likes to turn the import into a require, which sort of
|
|
|
|
// but not really behaves like import. Particularly, it cannot
|
|
|
|
// load fully dynamic content. So we hide the import from webpack
|
|
|
|
// by placing it inside an eval call.
|
2019-06-02 21:21:08 +02:00
|
|
|
urls = _getScriptUrls(script, scripts, []);
|
2021-03-10 07:27:14 +01:00
|
|
|
script.url = urls[urls.length - 1].url;
|
2019-06-03 05:03:20 +02:00
|
|
|
script.module = new Promise(resolve => resolve(eval('import(urls[urls.length - 1].url)')));
|
2019-06-02 21:21:08 +02:00
|
|
|
script.dependencies = urls.map(u => u.filename);
|
2018-05-12 02:45:40 +02:00
|
|
|
}
|
2019-05-25 20:20:24 +02:00
|
|
|
loadedModule = await script.module;
|
2018-05-12 02:45:40 +02:00
|
|
|
|
|
|
|
let ns = workerScript.env.vars;
|
2018-05-05 05:20:19 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
// TODO: putting await in a non-async function yields unhelpful
|
|
|
|
// "SyntaxError: unexpected reserved word" with no line number information.
|
|
|
|
if (!loadedModule.main) {
|
2018-07-31 03:01:09 +02:00
|
|
|
throw makeRuntimeRejectMsg(workerScript, `${script.filename} cannot be run because it does not have a main function.`);
|
2018-05-05 05:20:19 +02:00
|
|
|
}
|
2018-05-13 08:06:44 +02:00
|
|
|
return loadedModule.main(ns);
|
2018-05-05 05:20:19 +02:00
|
|
|
} finally {
|
2018-05-12 02:45:40 +02:00
|
|
|
// Revoke the generated URLs
|
2019-06-02 21:21:08 +02:00
|
|
|
if (urls != null) {
|
|
|
|
for (const b in urls) URL.revokeObjectURL(b.url);
|
2018-05-12 02:45:40 +02:00
|
|
|
}
|
2018-05-05 05:20:19 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-06-02 21:21:08 +02:00
|
|
|
/** Returns whether we should compile the script parameter.
|
|
|
|
*
|
|
|
|
* @param {Script} script
|
|
|
|
* @param {Script[]} scripts
|
|
|
|
*/
|
|
|
|
function shouldCompile(script, scripts) {
|
|
|
|
if (script.module === "") return true;
|
|
|
|
return script.dependencies.some(dep => {
|
|
|
|
const depScript = scripts.find(s => s.filename == dep);
|
|
|
|
|
|
|
|
// If the script is not present on the server, we should recompile, if only to get any necessary
|
|
|
|
// compilation errors.
|
|
|
|
if (!depScript) return true;
|
|
|
|
|
2019-06-03 02:17:27 +02:00
|
|
|
const depIsMoreRecent = depScript.moduleSequenceNumber > script.moduleSequenceNumber
|
2019-06-02 21:21:08 +02:00
|
|
|
return depIsMoreRecent;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-05-05 05:20:19 +02:00
|
|
|
// Gets a stack of blob urls, the top/right-most element being
|
|
|
|
// the blob url for the named script on the named server.
|
|
|
|
//
|
|
|
|
// - script -- the script for whom we are getting a URL.
|
|
|
|
// - scripts -- all the scripts available on this server
|
|
|
|
// - seen -- The modules above this one -- to prevent mutual dependency.
|
|
|
|
//
|
|
|
|
// TODO We don't make any effort to cache a given module when it is imported at
|
|
|
|
// different parts of the tree. That hasn't presented any problem with during
|
|
|
|
// testing, but it might be an idea for the future. Would require a topo-sort
|
|
|
|
// then url-izing from leaf-most to root-most.
|
2019-06-02 21:21:08 +02:00
|
|
|
/**
|
|
|
|
* @param {Script} script
|
|
|
|
* @param {Script[]} scripts
|
|
|
|
* @param {Script[]} seen
|
|
|
|
* @returns {ScriptUrl[]} All of the compiled scripts, with the final one
|
|
|
|
* in the list containing the blob corresponding to
|
|
|
|
* the script parameter.
|
|
|
|
*/
|
|
|
|
// BUG: apparently seen is never consulted. Oops.
|
2018-10-03 19:50:39 +02:00
|
|
|
export function _getScriptUrls(script, scripts, seen) {
|
2018-05-05 05:20:19 +02:00
|
|
|
// Inspired by: https://stackoverflow.com/a/43834063/91401
|
2019-06-02 21:21:08 +02:00
|
|
|
/** @type {ScriptUrl[]} */
|
2018-05-05 05:20:19 +02:00
|
|
|
const urlStack = [];
|
|
|
|
seen.push(script);
|
|
|
|
try {
|
|
|
|
// Replace every import statement with an import to a blob url containing
|
|
|
|
// the corresponding script. E.g.
|
|
|
|
//
|
2018-06-26 19:38:55 +02:00
|
|
|
// import {foo} from "bar.js";
|
2018-05-05 05:20:19 +02:00
|
|
|
//
|
|
|
|
// becomes
|
|
|
|
//
|
|
|
|
// import {foo} from "blob://<uuid>"
|
|
|
|
//
|
|
|
|
// Where the blob URL contains the script content.
|
2018-10-27 03:28:49 +02:00
|
|
|
let transformedCode = script.code.replace(/((?:from|import)\s+(?:'|"))(?:\.\/)?([^'"]+)('|";)/g,
|
2018-05-05 05:20:19 +02:00
|
|
|
(unmodified, prefix, filename, suffix) => {
|
|
|
|
const isAllowedImport = scripts.some(s => s.filename == filename);
|
|
|
|
if (!isAllowedImport) return unmodified;
|
|
|
|
|
|
|
|
// Find the corresponding script.
|
|
|
|
const [importedScript] = scripts.filter(s => s.filename == filename);
|
|
|
|
|
|
|
|
// Try to get a URL for the requested script and its dependencies.
|
2018-05-12 02:45:40 +02:00
|
|
|
const urls = _getScriptUrls(importedScript, scripts, seen);
|
2018-05-05 05:20:19 +02:00
|
|
|
|
|
|
|
// The top url in the stack is the replacement import file for this script.
|
|
|
|
urlStack.push(...urls);
|
2019-06-02 21:21:08 +02:00
|
|
|
return [prefix, urls[urls.length - 1].url, suffix].join('');
|
2018-10-03 19:50:39 +02:00
|
|
|
}
|
|
|
|
);
|
2018-05-05 05:20:19 +02:00
|
|
|
|
2018-10-23 20:55:42 +02:00
|
|
|
// We automatically define a print function() in the NetscriptJS module so that
|
|
|
|
// accidental calls to window.print() do not bring up the "print screen" dialog
|
|
|
|
transformedCode += `\n\nfunction print() {throw new Error("Invalid call to window.print(). Did you mean to use Netscript's print()?");}`
|
2018-05-13 08:06:44 +02:00
|
|
|
|
2018-05-05 05:20:19 +02:00
|
|
|
// If we successfully transformed the code, create a blob url for it and
|
|
|
|
// push that URL onto the top of the stack.
|
2019-06-02 21:21:08 +02:00
|
|
|
urlStack.push(new ScriptUrl(script.filename, URL.createObjectURL(makeScriptBlob(transformedCode))));
|
2018-05-05 05:20:19 +02:00
|
|
|
return urlStack;
|
|
|
|
} catch (err) {
|
|
|
|
// If there is an error, we need to clean up the URLs.
|
|
|
|
for (const url in urlStack) URL.revokeObjectURL(url);
|
|
|
|
throw err;
|
|
|
|
} finally {
|
|
|
|
seen.pop();
|
|
|
|
}
|
|
|
|
}
|