Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test MemFS implementation by WebAssembly/wasi-testsuite #9

Merged
merged 11 commits into from
Mar 2, 2025
Merged
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
/lib
/examples/build
/examples/package-lock.json
6 changes: 3 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "third_party/wasi-test-suite"]
path = third_party/wasi-test-suite
url = https://github.com/caspervonb/wasi-test-suite
[submodule "third_party/wasi-testsuite"]
path = third_party/wasi-testsuite
url = https://github.com/WebAssembly/wasi-testsuite
39 changes: 0 additions & 39 deletions examples/package-lock.json

This file was deleted.

7 changes: 2 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"./lib/esm/platforms/crypto.js": "./lib/esm/platforms/crypto.browser.js"
},
"scripts": {
"build": "tsc -p tsconfig.esm.json && tsc -p tsconfig.cjs.json",
"test": "jest",
"build": "tsc -p tsconfig.esm.json && tsc -p tsconfig.cjs.json && echo '{ \"type\": \"module\" }' > lib/esm/package.json",
"test": "node --test test/*.test.mjs",
"format": "prettier --write ./src ./test",
"prepare": "npm run build"
},
Expand All @@ -32,11 +32,8 @@
"author": "SwiftWasm Team",
"license": "MIT",
"devDependencies": {
"@types/jest": "^28.1.4",
"@types/node": "^17.0.31",
"jest": "^28.1.2",
"prettier": "^3.5.2",
"ts-jest": "^28.0.5",
"typescript": "^4.6.4"
}
}
16 changes: 16 additions & 0 deletions src/abi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,22 @@ export class WASIAbi {
* The file descriptor or file refers to a regular file inode.
*/
static readonly WASI_FILETYPE_REGULAR_FILE = 4;
/**
* Create file if it does not exist.
*/
static readonly WASI_OFLAGS_CREAT = 1 << 0;
/**
* Open directory.
*/
static readonly WASI_OFLAGS_DIRECTORY = 1 << 1;
/**
* Fail if not a directory.
*/
static readonly WASI_OFLAGS_EXCL = 1 << 2;
/**
* Truncate to zero length.
*/
static readonly WASI_OFLAGS_TRUNC = 1 << 3;

static readonly IMPORT_FUNCTIONS = [
"args_get",
Expand Down
25 changes: 10 additions & 15 deletions src/features/all.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,24 @@
import { WASIAbi } from "../abi";
import { WASIFeatureProvider, WASIOptions } from "../options";
import { useArgs } from "./args";
import { useClock } from "./clock";
import { useEnviron } from "./environ";
import { useFS, useStdio } from "./fd";
import { useProc } from "./proc";
import { useRandom } from "./random";
import { WASIAbi } from "../abi.js";
import { WASIFeatureProvider, WASIOptions } from "../options.js";
import { useArgs } from "./args.js";
import { useClock } from "./clock.js";
import { useEnviron } from "./environ.js";
import { useMemoryFS } from "./fd.js";
import { useProc } from "./proc.js";
import { useRandom } from "./random.js";

type Options = (Parameters<typeof useFS>[0] | Parameters<typeof useStdio>[0]) &
Parameters<typeof useRandom>[0];
type Options = Parameters<typeof useMemoryFS>[0] & Parameters<typeof useRandom>[0];

export function useAll(useOptions: Options = {}): WASIFeatureProvider {
return (options: WASIOptions, abi: WASIAbi, memoryView: () => DataView) => {
const features = [
useMemoryFS(useOptions),
useEnviron,
useArgs,
useClock,
useProc,
useRandom(useOptions),
];
if ("fs" in useOptions) {
features.push(useFS({ fs: useOptions.fs }));
} else {
features.push(useStdio(useOptions));
}
return features.reduce((acc, fn) => {
return { ...acc, ...fn(options, abi, memoryView) };
}, {});
Expand Down
4 changes: 2 additions & 2 deletions src/features/args.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { WASIAbi } from "../abi";
import { WASIOptions } from "../options";
import { WASIAbi } from "../abi.js";
import { WASIOptions } from "../options.js";

/**
* A feature provider that provides `args_get` and `args_sizes_get`
Expand Down
4 changes: 2 additions & 2 deletions src/features/clock.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { WASIAbi } from "../abi";
import { WASIOptions } from "../options";
import { WASIAbi } from "../abi.js";
import { WASIOptions } from "../options.js";

/**
* A feature provider that provides `clock_res_get` and `clock_time_get` by JavaScript's Date.
Expand Down
4 changes: 2 additions & 2 deletions src/features/environ.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { WASIAbi } from "../abi";
import { WASIOptions } from "../options";
import { WASIAbi } from "../abi.js";
import { WASIOptions } from "../options.js";

/**
* A feature provider that provides `environ_get` and `environ_sizes_get`
Expand Down
Loading
Loading