Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
some fixes to node-wasi
diff --git a/run.js b/run.js
index 87928f1..94f353d 100755
--- a/run.js
+++ b/run.js
@@ -30,9 +30,11 @@ USAGE:
const bin = fs.readFileSync(process.argv[2]);
const mod = new WebAssembly.Module(bin);
+const preopenDirectories = {};
+preopenDirectories[process.cwd()] = process.cwd();
const wasi = new WASI({
- preopenDirectories: { '.': '.' },
+ preopenDirectories,
env: process.env,
args: process.argv.slice(2),
});
diff --git a/src/index.js b/src/index.js
index cc9b597..75dcd1f 100644
--- a/src/index.js
+++ b/src/index.js
@@ -442,6 +442,27 @@ const wrap = (f) => (...args) => {
}
};
+const getFiletype = (stats) => {
+ switch (true) {
+ case stats.isBlockDevice():
+ return WASI_FILETYPE_BLOCK_DEVICE;
+ case stats.isCharacterDevice():
+ return filetype = WASI_FILETYPE_CHARACTER_DEVICE;
+ case stats.isDirectory():
+ return WASI_FILETYPE_DIRECTORY;
+ case stats.isFIFO():
+ return WASI_FILETYPE_SOCKET_STREAM;
+ case stats.isFile():
+ return WASI_FILETYPE_REGULAR_FILE;
+ case stats.isSocket():
+ return WASI_FILETYPE_SOCKET_STREAM;
+ case stats.isSymbolicLink():
+ return WASI_FILETYPE_SYMBOLIC_LINK;
+ default:
+ return WASI_FILETYPE_UNKNOWN;
+ }
+};
+
const stat = (wasi, fd) => {
const entry = wasi.FD_MAP.get(fd);
if (!entry) {
@@ -599,7 +620,7 @@ class WASI {
let coffset = environ;
let offset = environBuf;
const envProcessed = Object.entries(env)
- .map(([key, value]) => `${key}=${value}`);
+ .map(([key, value]) => `${key}=${value}\0`);
envProcessed.forEach((e) => {
this.view.setUint32(coffset, offset, true);
coffset += 4;
@@ -612,7 +633,7 @@ class WASI {
const envProcessed = Object.entries(env)
.map(([key, value]) => `${key}=${value}`);
const size = envProcessed.reduce((acc, e) =>
- acc + Buffer.byteLength(e), 0);
+ acc + Buffer.byteLength(e) + 1, 0);
this.view.setUint32(environCount, envProcessed.length, true);
this.view.setUint32(environBufSize, size, true);
return WASI_ESUCCESS;
@@ -917,7 +938,7 @@ class WASI {
bufPtr += 8;
this.view.setBigUint64(bufPtr, BigInt(rstats.ino), true);
bufPtr += 8;
- this.view.setUint8(bufPtr, stats.filetype);
+ this.view.setUint8(bufPtr, getFiletype(rstats));
bufPtr += 4;
this.view.setUint32(bufPtr, Number(rstats.nlink), true);
bufPtr += 4;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.