bitburner-src/src/NetscriptEvaluator.js

65 lines
2.1 KiB
JavaScript
Raw Normal View History

import { setTimeoutRef } from "./utils/SetTimeoutRef";
2019-02-20 09:42:27 +01:00
import { isValidIPAddress } from "../utils/helpers/isValidIPAddress";
import { isString } from "../utils/helpers/isString";
export function netscriptDelay(time, workerScript) {
return new Promise(function(resolve, reject) {
2019-02-20 09:42:27 +01:00
workerScript.delay = setTimeoutRef(() => {
workerScript.delay = null;
resolve();
}, time);
workerScript.delayResolve = resolve;
});
2017-06-05 06:48:37 +02:00
}
export function makeRuntimeRejectMsg(workerScript, msg, exp=null) {
var lineNum = "";
if (exp != null) {
var num = getErrorLineNumber(exp, workerScript);
lineNum = " (Line " + num + ")"
}
return "|"+workerScript.serverIp+"|"+workerScript.name+"|" + msg + lineNum;
}
export function resolveNetscriptRequestedThreads(workerScript, functionName, requestedThreads) {
const threads = workerScript.scriptRef.threads;
if (!requestedThreads) {
2019-05-11 11:20:09 +02:00
return (isNaN(threads) || threads < 1) ? 1 : threads;
}
const requestedThreadsAsInt = requestedThreads|0;
if (isNaN(requestedThreads) || requestedThreadsAsInt < 1) {
throw makeRuntimeRejectMsg(workerScript, `Invalid thread count passed to ${functionName}: ${requestedThreads}. Threads must be a positive number.`);
}
if (requestedThreads > threads) {
throw makeRuntimeRejectMsg(workerScript, `Too many threads requested by ${functionName}. Requested: ${requestedThreads}. Has: ${threads}.`);
}
return requestedThreadsAsInt;
}
export function getErrorLineNumber(exp, workerScript) {
var code = workerScript.scriptRef.codeCode();
//Split code up to the start of the node
try {
code = code.substring(0, exp.start);
return (code.match(/\n/g) || []).length + 1;
} catch(e) {
return -1;
}
}
export function isScriptErrorMessage(msg) {
if (!isString(msg)) {return false;}
let splitMsg = msg.split("|");
if (splitMsg.length != 4){
return false;
}
var ip = splitMsg[1];
if (!isValidIPAddress(ip)) {
return false;
}
return true;
}