Skip to content

Commit

Permalink
fix: various fs support (bytecodealliance#267)
Browse files Browse the repository at this point in the history
* fix: various fs support

* lint fixes
  • Loading branch information
guybedford authored Nov 21, 2023
1 parent 839d8f3 commit 1a9e5cd
Show file tree
Hide file tree
Showing 114 changed files with 626 additions and 606 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ package-lock.json
/docs/book
/src/**/*.d.ts
/src/**/*.d.ts.map
/tests/rundir
4 changes: 2 additions & 2 deletions crates/js-component-bindgen/src/function_bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1038,7 +1038,7 @@ impl Bindgen for FunctionBindgen<'_> {
let to_result_string = self.intrinsic(Intrinsic::ToResultString);
uwriteln!(
self.src,
"console.trace(`{prefix} return {}`);",
"console.error(`{prefix} return {}`);",
if sig_results_length > 0 || !results.is_empty() {
format!("result=${{{to_result_string}(ret)}}")
} else {
Expand Down Expand Up @@ -1074,7 +1074,7 @@ impl Bindgen for FunctionBindgen<'_> {
let to_result_string = self.intrinsic(Intrinsic::ToResultString);
uwriteln!(
self.src,
"console.trace(`{prefix} return {}`);",
"console.error(`{prefix} return {}`);",
if results_length > 0 || !results.is_empty() {
format!("result=${{{to_result_string}(ret)}}")
} else {
Expand Down
2 changes: 1 addition & 1 deletion crates/js-component-bindgen/src/transpile_bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1269,7 +1269,7 @@ impl<'a> Instantiator<'a, '_> {
.collect::<Vec<String>>();
uwriteln!(
self.src.js,
"console.trace(`{tracing_prefix} call {}`);",
"console.error(`{tracing_prefix} call {}`);",
event_fields.join(", ")
);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/preview2-shim/lib/io/worker-io.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ if (DEBUG) {
ret = e;
throw ret;
} finally {
console.trace(
process._rawDebug(
(num & CALL_MASK) >> CALL_SHIFT,
num & CALL_TYPE_MASK,
id,
Expand Down
33 changes: 21 additions & 12 deletions packages/preview2-shim/lib/io/worker-thread.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { runAsWorker } from "../synckit/index.js";
import { FILE, STDOUT, STDERR, STDIN } from "./stream-types.js";
import { createReadStream } from "node:fs";
import { createReadStream, createWriteStream } from "node:fs";
import { Readable } from "node:stream";
import { hrtime } from "node:process";
import {
Expand Down Expand Up @@ -64,7 +64,6 @@ unfinishedStreams.set(++streamCnt, {
});

/**
*
* @param {number} streamId
* @param {NodeJS.ReadableStream | NodeJS.WritableStream} stream
*/
Expand All @@ -79,7 +78,6 @@ function streamError(streamId, stream, err) {
}

/**
*
* @param {number} streamId
* @returns {{ stream: NodeJS.ReadableStream | NodeJS.WritableStream, flushPromise: Promise<void> | null }}
*/
Expand Down Expand Up @@ -153,9 +151,21 @@ function handle(call, id, payload) {
stream.on("end", () => void stream.emit("readable"));
return streamCnt;
}
case OUTPUT_STREAM_CREATE | FILE:
throw new Error("todo: file write");

case OUTPUT_STREAM_CREATE | FILE: {
const { fd, offset } = payload;
const stream = createWriteStream(null, {
fd,
autoClose: false,
emitClose: false,
highWaterMark: 64 * 1024,
start: Number(offset)
});
unfinishedStreams.set(++streamCnt, {
flushPromise: null,
stream,
});
return streamCnt;
}
// Generic call implementations (streams + polls)
default:
switch (call & CALL_MASK) {
Expand Down Expand Up @@ -246,13 +256,12 @@ function handle(call, id, payload) {
new Error("Cannot write more than permitted writable length")
);
}

return new Promise((resolve, reject) => {
stream.write(payload, (err) =>
err
? reject(streamError(id, stream, err))
: resolve(BigInt(payload.byteLength))
);
stream.write(payload, err => {
if (err)
return void reject(streamError(id, stream, err));
resolve(BigInt(payload.byteLength));
});
});
}
case OUTPUT_STREAM_FLUSH: {
Expand Down
Loading

0 comments on commit 1a9e5cd

Please sign in to comment.