Skip to content

Commit

Permalink
Merge pull request danielyxie#3758 from danielyxie/api-wrap
Browse files Browse the repository at this point in the history
Fix a few bugs related to api wrapping
  • Loading branch information
hydroflame authored May 25, 2022
2 parents edef772 + 4dd7afa commit bef953b
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 64 deletions.
4 changes: 2 additions & 2 deletions dist/main.bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/main.bundle.js.map

Large diffs are not rendered by default.

42 changes: 21 additions & 21 deletions dist/vendor.bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/vendor.bundle.js.map

Large diffs are not rendered by default.

14 changes: 5 additions & 9 deletions src/NetscriptFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -950,7 +950,7 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
(ctx: NetscriptContext) =>
(fn: any, hostname: any = workerScript.hostname, ...scriptArgs: any[]): void => {
let runningScriptObj;
if (arguments.length === 0) {
if (fn === undefined) {
runningScriptObj = workerScript.scriptRef;
} else if (typeof fn === "number") {
runningScriptObj = getRunningScriptByPid(fn);
Expand Down Expand Up @@ -1248,12 +1248,9 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
(ctx: NetscriptContext) =>
async (scriptname: any, _hostname1: unknown, hostname2?: any): Promise<boolean> => {
const hostname1 = ctx.helper.string("hostname1", _hostname1);
if (arguments.length !== 2 && arguments.length !== 3) {
throw ctx.makeRuntimeErrorMsg("Takes 2 or 3 arguments");
}
if (scriptname && scriptname.constructor === Array) {
// Recursively call scp on all elements of array
const scripts: Array<string> = scriptname;
const scripts: string[] = scriptname;
if (scripts.length === 0) {
throw ctx.makeRuntimeErrorMsg("No scripts to copy");
}
Expand Down Expand Up @@ -1412,7 +1409,7 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {

// Get the grep filter, if one exists
let filter = "";
if (arguments.length >= 2) {
if (_grep !== undefined) {
filter = grep.toString();
}

Expand Down Expand Up @@ -1787,7 +1784,6 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
(_name: unknown, _ram: unknown): string => {
const name = ctx.helper.string("name", _name);
const ram = ctx.helper.number("ram", _ram);
if (arguments.length !== 2) throw ctx.makeRuntimeErrorMsg("Takes 2 arguments");
let hostnameStr = String(name);
hostnameStr = hostnameStr.replace(/\s+/g, "");
if (hostnameStr == "") {
Expand Down Expand Up @@ -2214,7 +2210,7 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
getScriptIncome:
(ctx: NetscriptContext) =>
(scriptname?: any, hostname?: any, ...args: any[]): any => {
if (arguments.length === 0) {
if (scriptname === undefined) {
const res = [];

// First element is total income of all currently running scripts
Expand All @@ -2241,7 +2237,7 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
getScriptExpGain:
(ctx: NetscriptContext) =>
(scriptname?: any, hostname?: any, ...args: any[]): number => {
if (arguments.length === 0) {
if (scriptname === undefined) {
let total = 0;
for (const ws of workerScripts.values()) {
total += ws.scriptRef.onlineExpGained / ws.scriptRef.onlineRunningTime;
Expand Down
61 changes: 31 additions & 30 deletions src/NetscriptFunctions/Flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,37 @@ import { toNative } from "./toNative";
import * as libarg from "arg";

export function Flags(vargs: string[]): any {
return function (data: any): any {
data = toNative(data);
// We always want the help flag.
const args: {
[key: string]: any;
} = {};
return () =>
(data: any): any => {
data = toNative(data);
// We always want the help flag.
const args: {
[key: string]: any;
} = {};

for (const d of data) {
let t: any = String;
if (typeof d[1] === "number") {
t = Number;
} else if (typeof d[1] === "boolean") {
t = Boolean;
} else if (Array.isArray(d[1])) {
t = [String];
for (const d of data) {
let t: any = String;
if (typeof d[1] === "number") {
t = Number;
} else if (typeof d[1] === "boolean") {
t = Boolean;
} else if (Array.isArray(d[1])) {
t = [String];
}
const numDashes = d[0].length > 1 ? 2 : 1;
args["-".repeat(numDashes) + d[0]] = t;
}
const numDashes = d[0].length > 1 ? 2 : 1;
args["-".repeat(numDashes) + d[0]] = t;
}
const ret = libarg(args, { argv: vargs });
for (const d of data) {
if (!ret.hasOwnProperty("--" + d[0]) || !ret.hasOwnProperty("-" + d[0])) ret[d[0]] = d[1];
}
for (const key of Object.keys(ret)) {
if (!key.startsWith("-")) continue;
const value = ret[key];
delete ret[key];
const numDashes = key.length === 2 ? 1 : 2;
ret[key.slice(numDashes)] = value;
}
return ret;
};
const ret = libarg(args, { argv: vargs });
for (const d of data) {
if (!ret.hasOwnProperty("--" + d[0]) || !ret.hasOwnProperty("-" + d[0])) ret[d[0]] = d[1];
}
for (const key of Object.keys(ret)) {
if (!key.startsWith("-")) continue;
const value = ret[key];
delete ret[key];
const numDashes = key.length === 2 ? 1 : 2;
ret[key.slice(numDashes)] = value;
}
return ret;
};
}

0 comments on commit bef953b

Please sign in to comment.