2019-04-11 10:37:40 +02:00
|
|
|
import { setTimeoutRef } from "./utils/SetTimeoutRef";
|
2019-02-20 09:42:27 +01:00
|
|
|
|
2019-04-11 10:37:40 +02:00
|
|
|
import { isValidIPAddress } from "../utils/helpers/isValidIPAddress";
|
|
|
|
import { isString } from "../utils/helpers/isString";
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2019-02-06 08:06:48 +01:00
|
|
|
export function netscriptDelay(time, workerScript) {
|
2017-11-02 22:47:09 +01:00
|
|
|
return new Promise(function(resolve, reject) {
|
2019-02-20 09:42:27 +01:00
|
|
|
workerScript.delay = setTimeoutRef(() => {
|
2017-11-02 22:47:09 +01:00
|
|
|
workerScript.delay = null;
|
|
|
|
resolve();
|
|
|
|
}, time);
|
|
|
|
workerScript.delayResolve = resolve;
|
|
|
|
});
|
2017-06-05 06:48:37 +02:00
|
|
|
}
|
|
|
|
|
2019-02-06 08:06:48 +01:00
|
|
|
export function makeRuntimeRejectMsg(workerScript, msg, exp=null) {
|
2018-03-03 22:05:33 +01:00
|
|
|
var lineNum = "";
|
|
|
|
if (exp != null) {
|
|
|
|
var num = getErrorLineNumber(exp, workerScript);
|
|
|
|
lineNum = " (Line " + num + ")"
|
2016-11-17 23:25:40 +01:00
|
|
|
}
|
2018-03-03 22:05:33 +01:00
|
|
|
return "|"+workerScript.serverIp+"|"+workerScript.name+"|" + msg + lineNum;
|
2017-05-15 08:35:09 +02:00
|
|
|
}
|
|
|
|
|
2019-05-03 10:01:43 +02:00
|
|
|
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;
|
2019-05-03 10:01:43 +02:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-12-14 00:52:32 +01:00
|
|
|
|
2019-02-06 08:06:48 +01:00
|
|
|
export function getErrorLineNumber(exp, workerScript) {
|
|
|
|
var code = workerScript.scriptRef.codeCode();
|
2017-09-05 03:03:29 +02:00
|
|
|
|
|
|
|
//Split code up to the start of the node
|
2018-03-03 22:05:33 +01:00
|
|
|
try {
|
|
|
|
code = code.substring(0, exp.start);
|
|
|
|
return (code.match(/\n/g) || []).length + 1;
|
|
|
|
} catch(e) {
|
|
|
|
return -1;
|
|
|
|
}
|
2017-09-05 03:03:29 +02:00
|
|
|
}
|
|
|
|
|
2019-02-06 08:06:48 +01:00
|
|
|
export function isScriptErrorMessage(msg) {
|
2017-08-30 19:44:29 +02:00
|
|
|
if (!isString(msg)) {return false;}
|
|
|
|
let splitMsg = msg.split("|");
|
2017-05-01 07:39:48 +02:00
|
|
|
if (splitMsg.length != 4){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
var ip = splitMsg[1];
|
|
|
|
if (!isValidIPAddress(ip)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|