bitburner-src/src/NetscriptJSEvaluator.ts

189 lines
7.6 KiB
TypeScript
Raw Normal View History

/**
* Uses the acorn.js library to parse a script's code into an AST and
* recursively walk through that AST to replace import urls with blobs
*/
import * as walk from "acorn-walk";
import { parse } from "acorn";
import { ScriptUrl } from "./Script/ScriptUrl";
2021-09-24 23:07:53 +02:00
import { Script } from "./Script/Script";
2021-12-23 21:55:58 +01:00
import { areImportsEquals } from "./Terminal/DirectoryHelpers";
2022-08-20 00:21:31 +02:00
import { ScriptModule } from "./Script/ScriptModule";
2018-05-05 05:20:19 +02:00
2022-07-16 05:34:27 +02:00
// Acorn type def is straight up incomplete so we have to fill with our own.
export type Node = any;
2018-05-05 05:20:19 +02:00
// Makes a blob that contains the code of a given script.
2021-09-24 23:07:53 +02:00
function makeScriptBlob(code: string): Blob {
2021-09-05 01:09:30 +02:00
return new Blob([code], { type: "text/javascript" });
2018-05-05 05:20:19 +02:00
}
2022-08-29 08:41:17 +02:00
export async function compile(script: Script, scripts: Script[]): Promise<ScriptModule> {
2022-08-20 00:21:31 +02:00
//!shouldCompile ensures that script.module is non-null, hence the "as".
if (!shouldCompile(script, scripts)) return script.module as Promise<ScriptModule>;
script.queueCompile = true;
//If we're already in the middle of compiling (script.module has not resolved yet), wait for the previous compilation to finish
//If script.module is null, this does nothing.
await script.module;
2022-10-09 07:25:31 +02:00
//If multiple compiles were called on the same script before a compilation could be completed this ensures only one compilation is actually performed.
2022-08-20 00:21:31 +02:00
if (!script.queueCompile) return script.module as Promise<ScriptModule>;
script.queueCompile = false;
const uurls = _getScriptUrls(script, scripts, []);
const url = uurls[uurls.length - 1].url;
if (script.url && script.url !== url) URL.revokeObjectURL(script.url);
if (script.dependencies.length > 0) script.dependencies.forEach((dep) => URL.revokeObjectURL(dep.url));
script.url = uurls[uurls.length - 1].url;
2021-10-15 18:47:43 +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.
2022-03-31 03:40:51 +02:00
script.module = new Promise((resolve) => resolve(eval("import(uurls[uurls.length - 1].url)")));
2021-10-15 18:47:43 +02:00
script.dependencies = uurls;
2022-08-20 00:21:31 +02:00
return script.module;
2021-10-15 18:47:43 +02:00
}
2021-10-15 05:39:30 +02:00
function isDependencyOutOfDate(filename: string, scripts: Script[], scriptModuleSequenceNumber: number): boolean {
const depScript = scripts.find((s) => s.filename == filename);
// If the script is not present on the server, we should recompile, if only to get any necessary
// compilation errors.
if (!depScript) return true;
const depIsMoreRecent = depScript.moduleSequenceNumber > scriptModuleSequenceNumber;
return depIsMoreRecent;
}
/** Returns whether we should compile the script parameter.
*
* @param {Script} script
* @param {Script[]} scripts
*/
2021-09-24 23:07:53 +02:00
function shouldCompile(script: Script, scripts: Script[]): boolean {
2022-07-20 04:44:45 +02:00
if (!script.module) return true;
2022-08-20 00:21:31 +02:00
if (script.dependencies.some((dep) => isDependencyOutOfDate(dep.filename, scripts, script.moduleSequenceNumber))) {
script.module = null;
return true;
}
return false;
}
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 2.3 consider: Possibly reusing modules when imported in different locations. Per previous notes, this may
// require a topo-sort then url-izing from leaf-most to root-most.
/**
* @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.
2021-09-24 23:07:53 +02:00
function _getScriptUrls(script: Script, scripts: Script[], seen: Script[]): ScriptUrl[] {
2021-09-05 01:09:30 +02:00
// Inspired by: https://stackoverflow.com/a/43834063/91401
const urlStack: ScriptUrl[] = [];
// Seen contains the dependents of the current script. Make sure we include that in the script dependents.
for (const dependent of seen) {
2022-01-18 20:02:12 +01:00
if (!script.dependents.some((s) => s.server === dependent.server && s.filename == dependent.filename)) {
script.dependents.push({ server: dependent.server, filename: dependent.filename });
}
}
2021-09-05 01:09:30 +02:00
seen.push(script);
try {
// Replace every import statement with an import to a blob url containing
// the corresponding script. E.g.
//
// import {foo} from "bar.js";
//
// becomes
//
// import {foo} from "blob://<uuid>"
//
// Where the blob URL contains the script content.
// Parse the code into an ast tree
2022-07-16 05:34:27 +02:00
const ast = parse(script.code, { sourceType: "module", ecmaVersion: "latest", ranges: true });
interface importNode {
filename: string;
start: number;
end: number;
}
const importNodes: importNode[] = [];
// Walk the nodes of this tree and find any import declaration statements.
walk.simple(ast, {
2022-07-16 05:34:27 +02:00
ImportDeclaration(node: Node) {
// Push this import onto the stack to replace
2022-07-16 05:34:27 +02:00
if (!node.source) return;
importNodes.push({
filename: node.source.value,
start: node.source.range[0] + 1,
2022-01-18 20:02:12 +01:00
end: node.source.range[1] - 1,
});
},
2022-07-16 05:34:27 +02:00
ExportNamedDeclaration(node: Node) {
if (!node.source) return;
importNodes.push({
filename: node.source.value,
start: node.source.range[0] + 1,
end: node.source.range[1] - 1,
});
},
2022-07-16 05:34:27 +02:00
ExportAllDeclaration(node: Node) {
if (!node.source) return;
importNodes.push({
filename: node.source.value,
start: node.source.range[0] + 1,
end: node.source.range[1] - 1,
});
2022-01-18 20:02:12 +01:00
},
});
// Sort the nodes from last start index to first. This replaces the last import with a blob first,
// preventing the ranges for other imports from being shifted.
importNodes.sort((a, b) => b.start - a.start);
let transformedCode = script.code;
// Loop through each node and replace the script name with a blob url.
for (const node of importNodes) {
const filename = node.filename.startsWith("./") ? node.filename.substring(2) : node.filename;
// Find the corresponding script.
const matchingScripts = scripts.filter((s) => areImportsEquals(s.filename, filename));
if (matchingScripts.length === 0) continue;
const [importedScript] = matchingScripts;
2022-01-20 22:11:48 +01:00
const urls = _getScriptUrls(importedScript, scripts, seen);
// The top url in the stack is the replacement import file for this script.
urlStack.push(...urls);
const blob = urls[urls.length - 1].url;
// Replace the blob inside the import statement.
transformedCode = transformedCode.substring(0, node.start) + blob + transformedCode.substring(node.end);
}
2018-05-05 05:20:19 +02:00
2021-09-05 01:09:30 +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
2022-04-25 19:29:59 +02:00
transformedCode += `\n//# sourceURL=${script.server}/${script.filename}`;
2022-01-20 22:11:48 +01:00
const blob = URL.createObjectURL(makeScriptBlob(transformedCode));
2021-12-13 01:39:53 +01:00
// Push the blob URL onto the top of the stack.
urlStack.push(new ScriptUrl(script.filename, blob, script.moduleSequenceNumber));
2021-09-05 01:09:30 +02:00
return urlStack;
} catch (err) {
// If there is an error, we need to clean up the URLs.
2022-01-16 01:45:03 +01:00
for (const url of urlStack) URL.revokeObjectURL(url.url);
2021-09-05 01:09:30 +02:00
throw err;
} finally {
seen.pop();
}
2018-05-05 05:20:19 +02:00
}