bitburner-src/src/NetscriptJSEvaluator.ts

159 lines
6.0 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 { Script, ScriptURL } 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
}
const urlsToRevoke: ScriptURL[] = [];
let activeCompilations = 0;
/** Function to queue up revoking of script URLs. If there's no active compilation, just revoke it now. */
export const queueUrlRevoke = (url: ScriptURL) => {
if (!activeCompilations) return URL.revokeObjectURL(url);
urlsToRevoke.push(url);
};
/** Function to revoke any expired urls */
function triggerURLRevokes() {
if (activeCompilations === 0) {
// Revoke all pending revoke URLS
urlsToRevoke.forEach((url) => URL.revokeObjectURL(url));
// Remove all url strings from array
urlsToRevoke.length = 0;
}
}
2023-04-01 13:45:23 +02:00
// Webpack likes to turn the import into a require, which sort of
// but not really behaves like import. So we use a "magic comment"
// to disable that and leave it as a dynamic import.
//
// However, we need to be able to replace this implementation in tests. Ideally
// it would be fine, but Jest causes segfaults when using dynamic import: see
// https://github.com/nodejs/node/issues/35889 and
// https://github.com/facebook/jest/issues/11438
// import() is not a function, so it can't be replaced. We need this separate
// config object to provide a hook point.
export const config = {
doImport(url: ScriptURL): Promise<ScriptModule> {
2023-04-01 13:45:23 +02:00
return import(/*webpackIgnore:true*/ url);
},
};
export function compile(script: Script, scripts: Script[]): Promise<ScriptModule> {
// Return the module if it already exists
if (script.module) return script.module;
// While importing, use an existing url or generate a new one.
if (!script.url) script.url = generateScriptUrl(script, scripts, []);
activeCompilations++;
script.module = config
.doImport(script.url)
.catch((e) => {
script.invalidateModule();
console.error(`Error occurred while attempting to compile ${script.filename} on ${script.server}:`);
console.error(e);
throw e;
})
.finally(() => {
activeCompilations--;
triggerURLRevokes();
});
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
/** Add the necessary dependency relationships for a script.
* Dependents are used only for passing invalidation up an import tree, so only direct dependents need to be stored.
* Direct and indirect dependents need to have the current url/script added to their dependency map for error text.
*
* This should only be called once the script has an assigned URL. */
function addDependencyInfo(script: Script, dependents: Script[]) {
if (!script.url) throw new Error(`addDependencyInfo called without an assigned script URL (${script.filename})`);
if (dependents.length) {
script.dependents.add(dependents[dependents.length - 1]);
for (const dependent of dependents) dependent.dependencies.set(script.url, script);
2022-08-20 00:21:31 +02:00
}
}
/**
* @param script the script that needs a URL assigned
* @param scripts array of other scripts on the server
* @param dependents All scripts that were higher up in the import tree in a recursive call.
*/
function generateScriptUrl(script: Script, scripts: Script[], dependents: Script[]): ScriptURL {
// Early return for recursive calls where the script already has a URL
if (script.url) {
addDependencyInfo(script, dependents);
return script.url;
}
2022-01-20 22:11:48 +01:00
// Inspired by: https://stackoverflow.com/a/43834063/91401
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, {
ImportDeclaration(node: Node) {
// Push this import onto the stack to replace
if (!node.source) return;
importNodes.push({
filename: node.source.value,
start: node.source.range[0] + 1,
end: node.source.range[1] - 1,
});
},
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,
});
},
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,
});
},
});
// 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 newCode = 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 importedScript = scripts.find((s) => areImportsEquals(s.filename, filename));
if (!importedScript) continue;
importedScript.url = generateScriptUrl(importedScript, scripts, [...dependents, script]);
newCode = newCode.substring(0, node.start) + importedScript.url + newCode.substring(node.end);
}
2018-05-05 05:20:19 +02:00
newCode += `\n//# sourceURL=${script.server}/${script.filename}`;
// At this point we have the full code and can construct a new blob / assign the URL.
script.url = URL.createObjectURL(makeScriptBlob(newCode)) as ScriptURL;
addDependencyInfo(script, dependents);
return script.url;
2018-05-05 05:20:19 +02:00
}