Skip to content

Commit 6b7dc6f

Browse files
utilize union type in useAll parameter
1 parent d374dd8 commit 6b7dc6f

File tree

4 files changed

+19
-26
lines changed

4 files changed

+19
-26
lines changed

src/features/all.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,19 @@ import { useEnviron } from "./environ";
66
import { useFS, useStdio } from "./fd";
77
import { useProc } from "./proc";
88
import { useRandom } from "./random";
9-
import { defaultRandomFillSync } from "../platforms/crypto"
109

11-
export function useAll(useOptions: { fs?: any, randomFillSync: (buffer: Uint8Array) => void } = {
12-
fs: undefined, randomFillSync: defaultRandomFillSync,
13-
}): WASIFeatureProvider {
10+
type Options = (Parameters<typeof useFS>[0] | Parameters<typeof useStdio>[0]) & Parameters<typeof useRandom>[0]
11+
12+
export function useAll(useOptions: Options = {}): WASIFeatureProvider {
1413
return (options: WASIOptions, abi: WASIAbi, memoryView: () => DataView) => {
1514
const features = [
1615
useEnviron, useArgs, useClock, useProc,
17-
useRandom({ randomFillSync: useOptions.randomFillSync })
16+
useRandom(useOptions)
1817
];
19-
if (useOptions.fs) {
18+
if ("fs" in useOptions) {
2019
features.push(useFS({ fs: useOptions.fs }));
2120
} else {
22-
features.push(useStdio());
21+
features.push(useStdio(useOptions));
2322
}
2423
return features.reduce((acc, fn) => {
2524
return { ...acc, ...fn(options, abi, memoryView) };

src/features/fd.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -108,20 +108,16 @@ export class ReadableTextProxy implements FdEntry {
108108
*/
109109
export function useStdio(
110110
useOptions: {
111-
stdin: () => string,
112-
stdout: (lines: string) => void,
113-
stderr: (lines: string) => void,
114-
} = {
115-
stdin: () => { return "" },
116-
stdout: console.log,
117-
stderr: console.error,
118-
}
111+
stdin?: () => string,
112+
stdout?: (lines: string) => void,
113+
stderr?: (lines: string) => void,
114+
} = {}
119115
): WASIFeatureProvider {
120116
return (options, abi, memoryView) => {
121117
const fdTable = [
122-
new ReadableTextProxy(useOptions.stdin),
123-
new WritableTextProxy(useOptions.stdout),
124-
new WritableTextProxy(useOptions.stderr),
118+
new ReadableTextProxy(useOptions.stdin || (() => { return "" })),
119+
new WritableTextProxy(useOptions.stdout || console.log),
120+
new WritableTextProxy(useOptions.stderr || console.error),
125121
]
126122
return {
127123
fd_prestat_get: (fd: number, buf: number) => {

src/features/random.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,17 @@ import { defaultRandomFillSync } from "../platforms/crypto"
77
*/
88
export function useRandom(
99
useOptions: {
10-
randomFillSync: (buffer: Uint8Array) => void,
11-
} = {
12-
randomFillSync: defaultRandomFillSync,
13-
}
10+
randomFillSync?: (buffer: Uint8Array) => void,
11+
} = {}
1412
): WASIFeatureProvider {
13+
const randomFillSync = useOptions.randomFillSync || defaultRandomFillSync;
1514
return (options, abi, memoryView) => {
1615
return {
1716
random_get: (bufferOffset: number, length: number) => {
1817
const view = memoryView();
1918

2019
const buffer = new Uint8Array(view.buffer, bufferOffset, length);
21-
useOptions.randomFillSync(buffer);
20+
randomFillSync(buffer);
2221

2322
return WASIAbi.WASI_ESUCCESS;
2423
},

test/wasi-test-suite/harness.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { WASI, useEnviron, useArgs, useClock, useProc, useRandom, useStdio } from "../../src/index";
1+
import { WASI, useAll } from "../../src/index";
22
import { WASIAbi } from "../../src/abi";
33
import { existsSync } from "fs";
44
import { readFile } from "fs/promises";
@@ -15,8 +15,7 @@ export async function runTest(filePath: string) {
1515
return await readFile(path, "utf8");
1616
})()
1717
const features = [
18-
useEnviron, useArgs, useClock, useProc,
19-
useRandom(), useStdio({
18+
useAll({
2019
stdin: () => {
2120
const result = stdin;
2221
stdin = "";

0 commit comments

Comments
 (0)