REFACTOR: Post-PR changes to NetscriptHelpers.runOptions() (#442)

This commit is contained in:
David Walker 2023-03-23 09:01:20 -07:00 committed by GitHub
parent 2b54c6c9b9
commit 07b18edb5c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -183,22 +183,33 @@ function scriptArgs(ctx: NetscriptContext, args: unknown) {
}
function runOptions(ctx: NetscriptContext, threadOrOption: unknown): CompleteRunOptions {
const result: CompleteRunOptions = {
threads: 1 as PositiveInteger,
temporary: false,
};
function checkThreads(threads: unknown, argName: string) {
if (threads !== null && threads !== undefined) {
result.threads = positiveInteger(ctx, argName, threads);
}
}
if (typeof threadOrOption !== "object" || threadOrOption === null) {
return { threads: positiveInteger(ctx, "threads", threadOrOption ?? 1), temporary: false };
checkThreads(threadOrOption, "threads");
return result;
}
// Safe assertion since threadOrOption type has been narrowed to a non-null object
const options = threadOrOption as Unknownify<CompleteRunOptions>;
const threads = positiveInteger(ctx, "RunOptions.threads", options.threads ?? 1);
const temporary = !!options.temporary;
if (options.ramOverride === undefined || options.ramOverride === null) return { threads, temporary };
const ramOverride = number(ctx, "RunOptions.ramOverride", options.ramOverride);
if (ramOverride < RamCostConstants.Base) {
checkThreads(options.threads, "RunOptions.threads");
result.temporary = !!options.temporary;
if (options.ramOverride !== undefined && options.ramOverride !== null) {
result.ramOverride = number(ctx, "RunOptions.ramOverride", options.ramOverride);
if (result.ramOverride < RamCostConstants.Base) {
throw makeRuntimeErrorMsg(
ctx,
`RunOptions.ramOverride must be >= baseCost (${RamCostConstants.Base}), was ${ramOverride}`,
`RunOptions.ramOverride must be >= baseCost (${RamCostConstants.Base}), was ${result.ramOverride}`,
);
}
return { threads, temporary, ramOverride };
}
return result;
}
/** Convert multiple arguments for tprint or print into a single string. */