(feat) optional threads argument to netscript functions hack, weaken, grow

This commit is contained in:
Mason Dechaineux
2019-05-03 18:01:43 +10:00
committed by danielyxie
parent 585e1ac7aa
commit 44c26165f4
3 changed files with 32 additions and 11 deletions
+15
View File
@@ -155,6 +155,21 @@ export function makeRuntimeRejectMsg(workerScript, msg, exp=null) {
return "|"+workerScript.serverIp+"|"+workerScript.name+"|" + msg + lineNum;
}
export function resolveNetscriptRequestedThreads(workerScript, functionName, requestedThreads) {
const threads = workerScript.scriptRef.threads;
if (!requestedThreads) {
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;
}
//Run a script from inside a script using run() command
export function runScriptFromScript(server, scriptname, args, workerScript, threads=1) {
//Check if the script is already running