MISC: Make spawn able to have 0 delay (#1333)

This eliminates a hole where spawn was unrelaible, because other scripts
could jump in and steal the RAM. It's not an API break, because 0 used
to be an invalid value.
This commit is contained in:
David Walker
2024-06-28 18:41:41 -07:00
committed by GitHub
parent 06d742a7f3
commit 1c20a24079
6 changed files with 39 additions and 13 deletions
+8 -3
View File
@@ -93,7 +93,7 @@ export interface CompleteRunOptions {
}
/** SpawnOptions with non-optional, type-validated members, for passing between internal functions. */
export interface CompleteSpawnOptions extends CompleteRunOptions {
spawnDelay: PositiveInteger;
spawnDelay: number;
}
/** HGWOptions with non-optional, type-validated members, for passing between internal functions. */
export interface CompleteHGWOptions {
@@ -189,11 +189,16 @@ function runOptions(ctx: NetscriptContext, threadOrOption: unknown): CompleteRun
}
function spawnOptions(ctx: NetscriptContext, threadOrOption: unknown): CompleteSpawnOptions {
const result: CompleteSpawnOptions = { spawnDelay: 10000 as PositiveInteger, ...runOptions(ctx, threadOrOption) };
const result: CompleteSpawnOptions = { spawnDelay: 10000, ...runOptions(ctx, threadOrOption) };
if (typeof threadOrOption !== "object" || !threadOrOption) return result;
// Safe assertion since threadOrOption type has been narrowed to a non-null object
const { spawnDelay } = threadOrOption as Unknownify<CompleteSpawnOptions>;
if (spawnDelay !== undefined) result.spawnDelay = positiveInteger(ctx, "spawnDelayMsec", spawnDelay);
if (spawnDelay !== undefined) {
result.spawnDelay = number(ctx, "spawnDelay", spawnDelay);
if (result.spawnDelay < 0) {
throw errorMessage(ctx, `spawnDelay must be non-negative, got ${spawnDelay}`);
}
}
return result;
}