Skip to content

Commit bbd353f

Browse files
authored
update as to 10, add wasi, add sandbox (#29)
* update as to 10, add wasi, add sandbox * add cli flag preopens, override at {testname}.spec.wasi.js
1 parent 6e0743c commit bbd353f

File tree

6 files changed

+72
-39
lines changed

6 files changed

+72
-39
lines changed

package-lock.json

Lines changed: 26 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
"url": "https://github.com/AssemblyScript/node/issues"
1313
},
1414
"devDependencies": {
15-
"@as-pect/core": "^3.1.0-beta.3",
16-
"assemblyscript": "0.9.3",
15+
"@as-pect/core": "^3.2.2",
16+
"assemblyscript": "0.10.0",
1717
"glob": "^7.1.6"
1818
},
1919
"scripts": {
20-
"test": "node --experimental-wasi-unstable-preview1 tests/node"
20+
"test": "node --experimental-wasi-unstable-preview1 --experimental-wasm-bigint tests/node"
2121
},
2222
"dependencies": {}
2323
}

tests/fs.spec.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
test("an empty test", () => {
2+
3+
});

tests/fs.spec.wasi.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
env: process.env,
3+
argv: process.argv,
4+
preopens: {
5+
"./tests/sandbox": "./tests/sandbox"
6+
}
7+
};

tests/node.js

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ const { instantiateSync } = require("assemblyscript/lib/loader");
44
const { main } = require("assemblyscript/cli/asc");
55
const { parse } = require("assemblyscript/cli/util/options");
66
const path = require("path");
7-
const fs = require("fs").promises;
7+
const fssync = require("fs");
8+
const fs = fssync.promises;
89
const { WASI } = require("wasi");
910

1011
const promises = [];
@@ -21,13 +22,29 @@ const options = parse(process.argv.slice(2), {
2122
"type": "b",
2223
"alias": "u"
2324
},
25+
"preopens": {
26+
"description": "A set of preopened directories for wasi. Ex. ./dir=./dir",
27+
"type": "S",
28+
"alias": "p",
29+
"default": [],
30+
},
2431
});
2532

2633
if (options.unknown.length > 1) {
2734
console.error("Unknown options arguments: " + options.unknown.join(" "));
2835
process.exit(1);
2936
}
3037

38+
// calculate default preopens from cli
39+
const preopens = options.options.preopens.reduce(
40+
(obj, value) => {
41+
const [_, dir1, dir2] = /([^=])*=(.*)/.exec(value);
42+
obj[dir1] = dir2;
43+
return obj;
44+
},
45+
{}
46+
);
47+
3148
const reporter = new VerboseReporter();
3249
reporter.stderr = process.stderr;
3350
reporter.stdout = process.stdout;
@@ -40,7 +57,6 @@ const ascOptions = [
4057
relativeFromCwd(require.resolve("@as-pect/assembly/assembly/index.ts")),
4158
"--use", "ASC_RTRACE=1",
4259
"--explicitStart",
43-
"--validate",
4460
"--measure",
4561
"--lib", "assembly",
4662
"--transform", require.resolve("@as-pect/core/lib/transform/index.js"),
@@ -117,29 +133,36 @@ function runTest(fileName, type, binary, wat) {
117133
const basename = path.basename(fileName, ".ts");
118134
const fullName = path.join(dirname, basename)
119135
const watPath = `${fullName}.${type}.wat`;
136+
const wasiPath = `${fullName}.wasi.js`;
120137
const fileNamePath = `${fullName}.${type}.ts`;
121138

139+
// use either a custom wasi configuration, or the default one
140+
const wasiOptions = fssync.existsSync(wasiPath)
141+
? require(path.resolve(wasiPath))
142+
: {
143+
args: process.argv,
144+
env: process.env,
145+
preopens,
146+
};
147+
122148
// should not block testing
123149
promises.push(fs.writeFile(watPath, wat));
124150

151+
const wasi = new WASI(wasiOptions);
152+
125153
const context = new TestContext({
126154
fileName: fileNamePath, // set the fileName
127155
reporter, // use verbose reporter
128156
binary, // pass the binary to get function names
157+
wasi,
129158
});
130-
const wasi = new WASI({
131-
args: [],
132-
env: {},
133-
preopens: {
134-
// '/sandbox': '/some/real/path/that/wasm/can/access'
135-
}
136-
});
159+
137160
const imports = context.createImports({
138161
wasi_snapshot_preview1: wasi.wasiImport,
139162
});
140163

141164
const instance = instantiateSync(binary, imports);
142-
// TODO: wasi.start(instance);
165+
143166
process.stdout.write("\n");
144167
context.run(instance);
145168

tests/sandbox/test.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)