mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-04-23 01:32:55 +02:00
more work on bn13
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
* the way
|
||||
*/
|
||||
import * as walk from "acorn-walk";
|
||||
import { parse } from "acorn";
|
||||
import acorn, { parse } from "acorn";
|
||||
|
||||
import { RamCalculationErrorCode } from "./RamCalculationErrorCodes";
|
||||
|
||||
@@ -162,23 +162,9 @@ async function parseOnlyRamCalculate(
|
||||
// Check if this identifier is a function in the workerScript environment.
|
||||
// If it is, then we need to get its RAM cost.
|
||||
try {
|
||||
function applyFuncRam(func: any): number {
|
||||
if (typeof func === "function") {
|
||||
try {
|
||||
let res;
|
||||
if (func.constructor.name === "AsyncFunction") {
|
||||
res = 0; // Async functions will always be 0 RAM
|
||||
} else {
|
||||
res = func.apply(null, []);
|
||||
}
|
||||
if (typeof res === "number") {
|
||||
return res;
|
||||
}
|
||||
return 0;
|
||||
} catch (e) {
|
||||
console.error(`Error applying function: ${e}`);
|
||||
return 0;
|
||||
}
|
||||
function applyFuncRam(cost: any): number {
|
||||
if (typeof cost === "number") {
|
||||
return cost;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
@@ -203,6 +189,8 @@ async function parseOnlyRamCalculate(
|
||||
func = workerScript.env.vars.gang[ref];
|
||||
} else if (ref in workerScript.env.vars.sleeve) {
|
||||
func = workerScript.env.vars.sleeve[ref];
|
||||
} else if (ref in workerScript.env.vars.stock) {
|
||||
func = workerScript.env.vars.stock[ref];
|
||||
} else {
|
||||
func = workerScript.env.vars[ref];
|
||||
}
|
||||
@@ -220,6 +208,45 @@ async function parseOnlyRamCalculate(
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
@@ -228,7 +255,6 @@ async function parseOnlyRamCalculate(
|
||||
*/
|
||||
function parseOnlyCalculateDeps(code: string, currentModule: string): any {
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user