2019-05-18 00:51:28 +02:00
|
|
|
/**
|
|
|
|
* Implements RAM Calculation functionality.
|
|
|
|
*
|
|
|
|
* Uses the acorn.js library to parse a script's code into an AST and
|
|
|
|
* recursively walk through that AST, calculating RAM usage along
|
|
|
|
* the way
|
|
|
|
*/
|
2019-05-07 03:01:06 +02:00
|
|
|
import * as walk from "acorn-walk";
|
2021-10-29 05:04:26 +02:00
|
|
|
import acorn, { parse } from "acorn";
|
2019-03-05 02:40:28 +01:00
|
|
|
|
2019-05-12 04:20:20 +02:00
|
|
|
import { RamCalculationErrorCode } from "./RamCalculationErrorCodes";
|
|
|
|
|
2019-05-05 06:03:40 +02:00
|
|
|
import { RamCosts, RamCostConstants } from "../Netscript/RamCostGenerator";
|
2022-03-06 05:05:55 +01:00
|
|
|
import { Script } from "./Script";
|
2021-09-25 00:13:20 +02:00
|
|
|
import { WorkerScript } from "../Netscript/WorkerScript";
|
2021-12-23 21:55:58 +01:00
|
|
|
import { areImportsEquals } from "../Terminal/DirectoryHelpers";
|
2022-01-05 01:09:34 +01:00
|
|
|
import { IPlayer } from "../PersonObjects/IPlayer";
|
2019-03-05 02:40:28 +01:00
|
|
|
|
2022-01-05 22:41:48 +01:00
|
|
|
export interface RamUsageEntry {
|
2022-04-07 01:30:08 +02:00
|
|
|
type: "ns" | "dom" | "fn" | "misc";
|
2022-01-05 22:41:48 +01:00
|
|
|
name: string;
|
|
|
|
cost: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface RamCalculation {
|
|
|
|
cost: number;
|
|
|
|
entries?: RamUsageEntry[];
|
|
|
|
}
|
|
|
|
|
2019-03-05 02:40:28 +01:00
|
|
|
// These special strings are used to reference the presence of a given logical
|
|
|
|
// construct within a user script.
|
|
|
|
const specialReferenceIF = "__SPECIAL_referenceIf";
|
|
|
|
const specialReferenceFOR = "__SPECIAL_referenceFor";
|
|
|
|
const specialReferenceWHILE = "__SPECIAL_referenceWhile";
|
|
|
|
|
|
|
|
// The global scope of a script is registered under this key during parsing.
|
|
|
|
const memCheckGlobalKey = ".__GLOBAL__";
|
|
|
|
|
2019-05-18 00:51:28 +02:00
|
|
|
/**
|
|
|
|
* Parses code into an AST and walks through it recursively to calculate
|
|
|
|
* RAM usage. Also accounts for imported modules.
|
|
|
|
* @param {Script[]} otherScripts - All other scripts on the server. Used to account for imported scripts
|
|
|
|
* @param {string} codeCopy - The code being parsed
|
|
|
|
* @param {WorkerScript} workerScript - Object containing RAM costs of Netscript functions. Also used to
|
|
|
|
* keep track of what functions have/havent been accounted for
|
|
|
|
*/
|
2021-09-25 00:13:20 +02:00
|
|
|
async function parseOnlyRamCalculate(
|
2022-01-05 01:09:34 +01:00
|
|
|
player: IPlayer,
|
2021-09-25 00:13:20 +02:00
|
|
|
otherScripts: Script[],
|
|
|
|
code: string,
|
|
|
|
workerScript: WorkerScript,
|
2022-01-05 22:41:48 +01:00
|
|
|
): Promise<RamCalculation> {
|
2021-09-05 01:09:30 +02:00
|
|
|
try {
|
|
|
|
/**
|
|
|
|
* Maps dependent identifiers to their dependencies.
|
|
|
|
*
|
|
|
|
* The initial identifier is __SPECIAL_INITIAL_MODULE__.__GLOBAL__.
|
|
|
|
* It depends on all the functions declared in the module, all the global scopes
|
|
|
|
* of its imports, and any identifiers referenced in this global scope. Each
|
|
|
|
* function depends on all the identifiers referenced internally.
|
|
|
|
* We walk the dependency graph to calculate RAM usage, given that some identifiers
|
|
|
|
* reference Netscript functions which have a RAM cost.
|
|
|
|
*/
|
2021-09-25 00:13:20 +02:00
|
|
|
let dependencyMap: { [key: string]: string[] } = {};
|
2021-09-05 01:09:30 +02:00
|
|
|
|
|
|
|
// Scripts we've parsed.
|
|
|
|
const completedParses = new Set();
|
|
|
|
|
|
|
|
// Scripts we've discovered that need to be parsed.
|
2021-09-25 00:13:20 +02:00
|
|
|
const parseQueue: string[] = [];
|
2021-09-05 01:09:30 +02:00
|
|
|
|
|
|
|
// Parses a chunk of code with a given module name, and updates parseQueue and dependencyMap.
|
2021-09-25 00:13:20 +02:00
|
|
|
function parseCode(code: string, moduleName: string): void {
|
2021-09-05 01:09:30 +02:00
|
|
|
const result = parseOnlyCalculateDeps(code, moduleName);
|
|
|
|
completedParses.add(moduleName);
|
|
|
|
|
|
|
|
// Add any additional modules to the parse queue;
|
|
|
|
for (let i = 0; i < result.additionalModules.length; ++i) {
|
|
|
|
if (!completedParses.has(result.additionalModules[i])) {
|
|
|
|
parseQueue.push(result.additionalModules[i]);
|
2019-03-05 02:40:28 +01:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2019-03-05 02:40:28 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
// Splice all the references in
|
|
|
|
dependencyMap = Object.assign(dependencyMap, result.dependencyMap);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the initial module, which is the "main" script that is being run
|
|
|
|
const initialModule = "__SPECIAL_INITIAL_MODULE__";
|
|
|
|
parseCode(code, initialModule);
|
|
|
|
|
|
|
|
// Process additional modules, which occurs if the "main" script has any imports
|
|
|
|
while (parseQueue.length > 0) {
|
|
|
|
const nextModule = parseQueue.shift();
|
2021-09-25 00:13:20 +02:00
|
|
|
if (nextModule === undefined) throw new Error("nextModule should not be undefined");
|
2021-09-05 01:09:30 +02:00
|
|
|
|
|
|
|
// Additional modules can either be imported from the web (in which case we use
|
|
|
|
// a dynamic import), or from other in-game scripts
|
|
|
|
let code;
|
2021-09-09 05:47:34 +02:00
|
|
|
if (nextModule.startsWith("https://") || nextModule.startsWith("http://")) {
|
2021-09-05 01:09:30 +02:00
|
|
|
try {
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
|
|
|
const module = await eval("import(nextModule)");
|
|
|
|
code = "";
|
|
|
|
for (const prop in module) {
|
|
|
|
if (typeof module[prop] === "function") {
|
|
|
|
code += module[prop].toString() + ";\n";
|
2019-03-05 02:40:28 +01:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
} catch (e) {
|
2021-09-09 05:47:34 +02:00
|
|
|
console.error(`Error dynamically importing module from ${nextModule} for RAM calculations: ${e}`);
|
2022-01-05 22:41:48 +01:00
|
|
|
return { cost: RamCalculationErrorCode.URLImportError };
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!Array.isArray(otherScripts)) {
|
2021-09-09 05:47:34 +02:00
|
|
|
console.warn(`parseOnlyRamCalculate() not called with array of scripts`);
|
2022-01-05 22:41:48 +01:00
|
|
|
return { cost: RamCalculationErrorCode.ImportError };
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2019-03-05 02:40:28 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
let script = null;
|
2021-09-25 07:26:03 +02:00
|
|
|
const fn = nextModule.startsWith("./") ? nextModule.slice(2) : nextModule;
|
2021-09-05 01:09:30 +02:00
|
|
|
for (const s of otherScripts) {
|
2021-12-23 21:55:58 +01:00
|
|
|
if (areImportsEquals(s.filename, fn)) {
|
2021-09-05 01:09:30 +02:00
|
|
|
script = s;
|
|
|
|
break;
|
|
|
|
}
|
2019-03-05 02:40:28 +01:00
|
|
|
}
|
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
if (script == null) {
|
2022-01-05 22:41:48 +01:00
|
|
|
return { cost: RamCalculationErrorCode.ImportError }; // No such script on the server
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2019-03-05 02:40:28 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
code = script.code;
|
|
|
|
}
|
2019-03-05 02:40:28 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
parseCode(code, nextModule);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, walk the reference map and generate a ram cost. The initial set of keys to scan
|
|
|
|
// are those that start with __SPECIAL_INITIAL_MODULE__.
|
|
|
|
let ram = RamCostConstants.ScriptBaseRamCost;
|
2022-04-07 01:30:08 +02:00
|
|
|
const detailedCosts: RamUsageEntry[] = [
|
|
|
|
{ type: "misc", name: "baseCost", cost: RamCostConstants.ScriptBaseRamCost },
|
|
|
|
];
|
2021-09-09 05:47:34 +02:00
|
|
|
const unresolvedRefs = Object.keys(dependencyMap).filter((s) => s.startsWith(initialModule));
|
2021-09-05 01:09:30 +02:00
|
|
|
const resolvedRefs = new Set();
|
|
|
|
while (unresolvedRefs.length > 0) {
|
|
|
|
const ref = unresolvedRefs.shift();
|
2021-09-25 00:13:20 +02:00
|
|
|
if (ref === undefined) throw new Error("ref should not be undefined");
|
2021-09-05 01:09:30 +02:00
|
|
|
|
|
|
|
// Check if this is one of the special keys, and add the appropriate ram cost if so.
|
|
|
|
if (ref === "hacknet" && !resolvedRefs.has("hacknet")) {
|
|
|
|
ram += RamCostConstants.ScriptHacknetNodesRamCost;
|
2022-04-07 01:30:08 +02:00
|
|
|
detailedCosts.push({ type: "ns", name: "hacknet", cost: RamCostConstants.ScriptHacknetNodesRamCost });
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
if (ref === "document" && !resolvedRefs.has("document")) {
|
|
|
|
ram += RamCostConstants.ScriptDomRamCost;
|
2022-04-07 01:30:08 +02:00
|
|
|
detailedCosts.push({ type: "dom", name: "document", cost: RamCostConstants.ScriptDomRamCost });
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
if (ref === "window" && !resolvedRefs.has("window")) {
|
|
|
|
ram += RamCostConstants.ScriptDomRamCost;
|
2022-04-07 01:30:08 +02:00
|
|
|
detailedCosts.push({ type: "dom", name: "window", cost: RamCostConstants.ScriptDomRamCost });
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2021-12-04 05:06:04 +01:00
|
|
|
if (ref === "corporation" && !resolvedRefs.has("corporation")) {
|
|
|
|
ram += RamCostConstants.ScriptCorporationRamCost;
|
2022-04-07 01:30:08 +02:00
|
|
|
detailedCosts.push({ type: "ns", name: "corporation", cost: RamCostConstants.ScriptCorporationRamCost });
|
2021-12-04 05:06:04 +01:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
|
|
|
|
resolvedRefs.add(ref);
|
|
|
|
|
|
|
|
if (ref.endsWith(".*")) {
|
|
|
|
// A prefix reference. We need to find all matching identifiers.
|
|
|
|
const prefix = ref.slice(0, ref.length - 2);
|
2021-09-25 07:26:03 +02:00
|
|
|
for (const ident of Object.keys(dependencyMap).filter((k) => k.startsWith(prefix))) {
|
|
|
|
for (const dep of dependencyMap[ident] || []) {
|
2021-09-05 01:09:30 +02:00
|
|
|
if (!resolvedRefs.has(dep)) unresolvedRefs.push(dep);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// An exact reference. Add all dependencies of this ref.
|
2021-09-25 07:26:03 +02:00
|
|
|
for (const dep of dependencyMap[ref] || []) {
|
2021-09-05 01:09:30 +02:00
|
|
|
if (!resolvedRefs.has(dep)) unresolvedRefs.push(dep);
|
|
|
|
}
|
|
|
|
}
|
2019-03-05 02:40:28 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
// Check if this identifier is a function in the workerScript environment.
|
|
|
|
// If it is, then we need to get its RAM cost.
|
|
|
|
try {
|
2021-10-27 23:35:02 +02:00
|
|
|
function applyFuncRam(cost: any): number {
|
|
|
|
if (typeof cost === "number") {
|
|
|
|
return cost;
|
2022-01-05 01:09:34 +01:00
|
|
|
} else if (typeof cost === "function") {
|
|
|
|
return cost(player);
|
2021-09-05 01:09:30 +02:00
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only count each function once
|
|
|
|
if (workerScript.loadedFns[ref]) {
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
workerScript.loadedFns[ref] = true;
|
2019-03-05 02:40:28 +01:00
|
|
|
}
|
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
// This accounts for namespaces (Bladeburner, CodingCpntract, etc.)
|
|
|
|
let func;
|
2022-04-07 01:30:08 +02:00
|
|
|
let refDetail = "n/a";
|
2021-09-05 01:09:30 +02:00
|
|
|
if (ref in workerScript.env.vars.bladeburner) {
|
|
|
|
func = workerScript.env.vars.bladeburner[ref];
|
2022-01-05 22:41:48 +01:00
|
|
|
refDetail = `bladeburner.${ref}`;
|
2021-09-05 01:09:30 +02:00
|
|
|
} else if (ref in workerScript.env.vars.codingcontract) {
|
|
|
|
func = workerScript.env.vars.codingcontract[ref];
|
2022-01-05 22:41:48 +01:00
|
|
|
refDetail = `codingcontract.${ref}`;
|
2021-09-25 23:21:50 +02:00
|
|
|
} else if (ref in workerScript.env.vars.stanek) {
|
|
|
|
func = workerScript.env.vars.stanek[ref];
|
2022-01-05 22:41:48 +01:00
|
|
|
refDetail = `stanek.${ref}`;
|
2021-09-05 01:09:30 +02:00
|
|
|
} else if (ref in workerScript.env.vars.gang) {
|
|
|
|
func = workerScript.env.vars.gang[ref];
|
2022-01-05 22:41:48 +01:00
|
|
|
refDetail = `gang.${ref}`;
|
2021-09-05 01:09:30 +02:00
|
|
|
} else if (ref in workerScript.env.vars.sleeve) {
|
|
|
|
func = workerScript.env.vars.sleeve[ref];
|
2022-01-05 22:41:48 +01:00
|
|
|
refDetail = `sleeve.${ref}`;
|
2021-10-27 23:35:02 +02:00
|
|
|
} else if (ref in workerScript.env.vars.stock) {
|
|
|
|
func = workerScript.env.vars.stock[ref];
|
2022-01-05 22:41:48 +01:00
|
|
|
refDetail = `stock.${ref}`;
|
2021-12-20 19:38:21 +01:00
|
|
|
} else if (ref in workerScript.env.vars.ui) {
|
|
|
|
func = workerScript.env.vars.ui[ref];
|
2022-01-05 22:41:48 +01:00
|
|
|
refDetail = `ui.${ref}`;
|
2022-03-27 21:33:31 +02:00
|
|
|
} else if (ref in workerScript.env.vars.grafting) {
|
|
|
|
func = workerScript.env.vars.grafting[ref];
|
|
|
|
refDetail = `grafting.${ref}`;
|
2021-09-05 01:09:30 +02:00
|
|
|
} else {
|
|
|
|
func = workerScript.env.vars[ref];
|
2022-01-05 22:41:48 +01:00
|
|
|
refDetail = `${ref}`;
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2022-01-05 22:41:48 +01:00
|
|
|
const fnRam = applyFuncRam(func);
|
|
|
|
ram += fnRam;
|
2022-04-07 01:30:08 +02:00
|
|
|
detailedCosts.push({ type: "fn", name: refDetail, cost: fnRam });
|
2021-09-05 01:09:30 +02:00
|
|
|
} catch (error) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-03-05 02:40:28 +01:00
|
|
|
}
|
2022-04-07 01:30:08 +02:00
|
|
|
return { cost: ram, entries: detailedCosts.filter((e) => e.cost > 0) };
|
2021-09-05 01:09:30 +02:00
|
|
|
} catch (error) {
|
|
|
|
// console.info("parse or eval error: ", error);
|
|
|
|
// This is not unexpected. The user may be editing a script, and it may be in
|
|
|
|
// a transitory invalid state.
|
2022-01-05 22:41:48 +01:00
|
|
|
return { cost: RamCalculationErrorCode.SyntaxError };
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2019-03-05 02:40:28 +01:00
|
|
|
}
|
|
|
|
|
2021-10-29 05:04:26 +02:00
|
|
|
export function checkInfiniteLoop(code: string): number {
|
|
|
|
const ast = parse(code, { sourceType: "module", ecmaVersion: "latest" });
|
|
|
|
|
|
|
|
function nodeHasTrueTest(node: acorn.Node): boolean {
|
|
|
|
return node.type === "Literal" && (node as any).raw === "true";
|
|
|
|
}
|
|
|
|
|
|
|
|
function hasAwait(ast: acorn.Node): boolean {
|
|
|
|
let hasAwait = false;
|
|
|
|
walk.recursive(
|
|
|
|
ast,
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
AwaitExpression: () => {
|
|
|
|
hasAwait = true;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
|
|
|
return hasAwait;
|
|
|
|
}
|
|
|
|
|
|
|
|
let missingAwaitLine = -1;
|
|
|
|
walk.recursive(
|
|
|
|
ast,
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
WhileStatement: (node: acorn.Node, st: any, walkDeeper: walk.WalkerCallback<any>) => {
|
|
|
|
if (nodeHasTrueTest((node as any).test) && !hasAwait(node)) {
|
|
|
|
missingAwaitLine = (code.slice(0, node.start).match(/\n/g) || []).length + 1;
|
|
|
|
} else {
|
|
|
|
(node as any).body && walkDeeper((node as any).body, st);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
return missingAwaitLine;
|
|
|
|
}
|
|
|
|
|
2019-05-18 00:51:28 +02:00
|
|
|
/**
|
|
|
|
* Helper function that parses a single script. It returns a map of all dependencies,
|
|
|
|
* which are items in the code's AST that potentially need to be evaluated
|
|
|
|
* for RAM usage calculations. It also returns an array of additional modules
|
|
|
|
* that need to be parsed (i.e. are 'import'ed scripts).
|
|
|
|
*/
|
2021-09-25 00:13:20 +02:00
|
|
|
function parseOnlyCalculateDeps(code: string, currentModule: string): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
const ast = parse(code, { sourceType: "module", ecmaVersion: "latest" });
|
|
|
|
// Everything from the global scope goes in ".". Everything else goes in ".function", where only
|
|
|
|
// the outermost layer of functions counts.
|
|
|
|
const globalKey = currentModule + memCheckGlobalKey;
|
2021-09-25 00:13:20 +02:00
|
|
|
const dependencyMap: { [key: string]: Set<string> | undefined } = {};
|
|
|
|
dependencyMap[globalKey] = new Set<string>();
|
2021-09-05 01:09:30 +02:00
|
|
|
|
|
|
|
// If we reference this internal name, we're really referencing that external name.
|
|
|
|
// Filled when we import names from other modules.
|
2021-09-25 07:26:03 +02:00
|
|
|
const internalToExternal: { [key: string]: string | undefined } = {};
|
2021-09-05 01:09:30 +02:00
|
|
|
|
2021-09-25 07:26:03 +02:00
|
|
|
const additionalModules: string[] = [];
|
2021-09-05 01:09:30 +02:00
|
|
|
|
|
|
|
// References get added pessimistically. They are added for thisModule.name, name, and for
|
|
|
|
// any aliases.
|
2021-09-25 00:13:20 +02:00
|
|
|
function addRef(key: string, name: string): void {
|
2021-09-05 01:09:30 +02:00
|
|
|
const s = dependencyMap[key] || (dependencyMap[key] = new Set());
|
2021-09-25 00:13:20 +02:00
|
|
|
const external = internalToExternal[name];
|
|
|
|
if (external !== undefined) {
|
|
|
|
s.add(external);
|
2019-03-05 02:40:28 +01:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
s.add(currentModule + "." + name);
|
|
|
|
s.add(name); // For builtins like hack.
|
|
|
|
}
|
|
|
|
|
|
|
|
//A list of identifiers that resolve to "native Javascript code"
|
2021-09-09 05:47:34 +02:00
|
|
|
const objectPrototypeProperties = Object.getOwnPropertyNames(Object.prototype);
|
2021-09-05 01:09:30 +02:00
|
|
|
|
|
|
|
// If we discover a dependency identifier, state.key is the dependent identifier.
|
|
|
|
// walkDeeper is for doing recursive walks of expressions in composites that we handle.
|
2021-09-25 07:26:03 +02:00
|
|
|
function commonVisitors(): any {
|
2021-09-05 01:09:30 +02:00
|
|
|
return {
|
2021-09-25 00:13:20 +02:00
|
|
|
Identifier: (node: any, st: any) => {
|
2021-09-05 01:09:30 +02:00
|
|
|
if (objectPrototypeProperties.includes(node.name)) {
|
|
|
|
return;
|
2019-03-05 02:40:28 +01:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
addRef(st.key, node.name);
|
|
|
|
},
|
2021-09-25 00:13:20 +02:00
|
|
|
WhileStatement: (node: any, st: any, walkDeeper: any) => {
|
2021-09-05 01:09:30 +02:00
|
|
|
addRef(st.key, specialReferenceWHILE);
|
|
|
|
node.test && walkDeeper(node.test, st);
|
|
|
|
node.body && walkDeeper(node.body, st);
|
|
|
|
},
|
2021-09-25 00:13:20 +02:00
|
|
|
DoWhileStatement: (node: any, st: any, walkDeeper: any) => {
|
2021-09-05 01:09:30 +02:00
|
|
|
addRef(st.key, specialReferenceWHILE);
|
|
|
|
node.test && walkDeeper(node.test, st);
|
|
|
|
node.body && walkDeeper(node.body, st);
|
|
|
|
},
|
2021-09-25 00:13:20 +02:00
|
|
|
ForStatement: (node: any, st: any, walkDeeper: any) => {
|
2021-09-05 01:09:30 +02:00
|
|
|
addRef(st.key, specialReferenceFOR);
|
|
|
|
node.init && walkDeeper(node.init, st);
|
|
|
|
node.test && walkDeeper(node.test, st);
|
|
|
|
node.update && walkDeeper(node.update, st);
|
|
|
|
node.body && walkDeeper(node.body, st);
|
|
|
|
},
|
2021-09-25 00:13:20 +02:00
|
|
|
IfStatement: (node: any, st: any, walkDeeper: any) => {
|
2021-09-05 01:09:30 +02:00
|
|
|
addRef(st.key, specialReferenceIF);
|
|
|
|
node.test && walkDeeper(node.test, st);
|
|
|
|
node.consequent && walkDeeper(node.consequent, st);
|
|
|
|
node.alternate && walkDeeper(node.alternate, st);
|
|
|
|
},
|
2021-09-25 00:13:20 +02:00
|
|
|
MemberExpression: (node: any, st: any, walkDeeper: any) => {
|
2021-09-05 01:09:30 +02:00
|
|
|
node.object && walkDeeper(node.object, st);
|
|
|
|
node.property && walkDeeper(node.property, st);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
walk.recursive(
|
|
|
|
ast,
|
|
|
|
{ key: globalKey },
|
|
|
|
Object.assign(
|
|
|
|
{
|
2021-09-25 00:13:20 +02:00
|
|
|
ImportDeclaration: (node: any, st: any) => {
|
2021-09-05 01:09:30 +02:00
|
|
|
const importModuleName = node.source.value;
|
|
|
|
additionalModules.push(importModuleName);
|
|
|
|
|
|
|
|
// This module's global scope refers to that module's global scope, no matter how we
|
|
|
|
// import it.
|
2021-09-25 00:13:20 +02:00
|
|
|
const set = dependencyMap[st.key];
|
|
|
|
if (set === undefined) throw new Error("set should not be undefined");
|
|
|
|
set.add(importModuleName + memCheckGlobalKey);
|
2021-09-05 01:09:30 +02:00
|
|
|
|
|
|
|
for (let i = 0; i < node.specifiers.length; ++i) {
|
|
|
|
const spec = node.specifiers[i];
|
|
|
|
if (spec.imported !== undefined && spec.local !== undefined) {
|
|
|
|
// We depend on specific things.
|
2021-09-09 05:47:34 +02:00
|
|
|
internalToExternal[spec.local.name] = importModuleName + "." + spec.imported.name;
|
2021-09-05 01:09:30 +02:00
|
|
|
} else {
|
|
|
|
// We depend on everything.
|
2021-09-25 00:13:20 +02:00
|
|
|
const set = dependencyMap[st.key];
|
|
|
|
if (set === undefined) throw new Error("set should not be undefined");
|
|
|
|
set.add(importModuleName + ".*");
|
2019-03-05 02:40:28 +01:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2019-03-05 02:40:28 +01:00
|
|
|
},
|
2021-09-25 00:13:20 +02:00
|
|
|
FunctionDeclaration: (node: any) => {
|
2022-01-17 19:16:03 +01:00
|
|
|
// node.id will be null when using 'export default'. Add a module name indicating the default export.
|
|
|
|
const key = currentModule + "." + (node.id === null ? "__SPECIAL_DEFAULT_EXPORT__" : node.id.name);
|
2021-09-05 01:09:30 +02:00
|
|
|
walk.recursive(node, { key: key }, commonVisitors());
|
2019-03-05 02:40:28 +01:00
|
|
|
},
|
2021-09-05 01:09:30 +02:00
|
|
|
},
|
|
|
|
commonVisitors(),
|
|
|
|
),
|
|
|
|
);
|
2019-03-05 02:40:28 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
return { dependencyMap: dependencyMap, additionalModules: additionalModules };
|
2019-03-05 02:40:28 +01:00
|
|
|
}
|
|
|
|
|
2019-05-18 00:51:28 +02:00
|
|
|
/**
|
|
|
|
* Calculate's a scripts RAM Usage
|
|
|
|
* @param {string} codeCopy - The script's code
|
|
|
|
* @param {Script[]} otherScripts - All other scripts on the server.
|
|
|
|
* Used to account for imported scripts
|
|
|
|
*/
|
2021-09-25 00:13:20 +02:00
|
|
|
export async function calculateRamUsage(
|
2022-01-05 01:09:34 +01:00
|
|
|
player: IPlayer,
|
2021-09-25 00:13:20 +02:00
|
|
|
codeCopy: string,
|
|
|
|
otherScripts: Script[],
|
2022-01-05 22:41:48 +01:00
|
|
|
): Promise<RamCalculation> {
|
2021-09-05 01:09:30 +02:00
|
|
|
// We don't need a real WorkerScript for this. Just an object that keeps
|
|
|
|
// track of whatever's needed for RAM calculations
|
|
|
|
const workerScript = {
|
|
|
|
loadedFns: {},
|
|
|
|
env: {
|
|
|
|
vars: RamCosts,
|
|
|
|
},
|
2021-09-25 00:13:20 +02:00
|
|
|
} as WorkerScript;
|
2021-09-05 01:09:30 +02:00
|
|
|
|
|
|
|
try {
|
2022-01-05 01:09:34 +01:00
|
|
|
return await parseOnlyRamCalculate(player, otherScripts, codeCopy, workerScript);
|
2021-09-05 01:09:30 +02:00
|
|
|
} catch (e) {
|
|
|
|
console.error(`Failed to parse script for RAM calculations:`);
|
|
|
|
console.error(e);
|
2022-01-05 22:41:48 +01:00
|
|
|
return { cost: RamCalculationErrorCode.SyntaxError };
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2019-03-05 02:40:28 +01:00
|
|
|
}
|