Skip to content

Commit

Permalink
Merge pull request wasmerio#368 from wasmerio/michael/sdk-38-providin…
Browse files Browse the repository at this point in the history
…g-stdin-up-front-seems-to-always-read-the-same-bytes

Pick up wasmerio#4355 to fix stdin
  • Loading branch information
Michael Bryan authored Dec 15, 2023
2 parents 4677927 + e96757f commit 7ea45ca
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 53 deletions.
45 changes: 23 additions & 22 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ tracing = { version = "0.1", features = ["log", "release_max_level_debug"] }
tracing-futures = { version = "0.2" }
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
url = "2.4.0"
virtual-fs = { version = "0.9.0", default-features = false }
virtual-fs = { version = "0.10.0", default-features = false }
virtual-net = { version = "0.6.0", default-features = false, features = ["remote"] }
wasm-bindgen = { version = "0.2" }
wasm-bindgen-derive = "0.2.1"
wasm-bindgen-downcast = "0.1"
wasm-bindgen-futures = "0.4"
wasm-bindgen-test = "0.3.37"
wasmer = { version = "4.2.2", default-features = false, features = ["js", "js-default", "tracing", "wasm-types-polyfill", "enable-serde"] }
wasmer-wasix = { version = "0.16", default-features = false, features = ["js", "js-default"] }
wasmer = { version = "4.2.4", default-features = false, features = ["js", "js-default", "tracing", "wasm-types-polyfill", "enable-serde"] }
wasmer-wasix = { version = "0.17", default-features = false, features = ["js", "js-default"] }
webc = "5.3.0"

[dependencies.web-sys]
Expand Down
2 changes: 1 addition & 1 deletion src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ impl RunOptions {

let stdin = match self.read_stdin() {
Some(stdin) => {
let f = virtual_fs::StaticFile::new(stdin.into());
let f = virtual_fs::StaticFile::new(stdin);
builder.set_stdin(Box::new(f));
None
}
Expand Down
5 changes: 4 additions & 1 deletion src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ const DEFAULT_PROGRAM_NAME: &str = "wasm";
/// [component-model]: https://github.com/WebAssembly/component-model
#[wasm_bindgen(js_name = "runWasix")]
pub async fn run_wasix(wasm_module: WasmModule, config: RunOptions) -> Result<Instance, Error> {
let _span = tracing::debug_span!("run").entered();
run_wasix_inner(wasm_module, config).await
}

#[tracing::instrument(level = "debug", skip_all)]
async fn run_wasix_inner(wasm_module: WasmModule, config: RunOptions) -> Result<Instance, Error> {
let runtime = config.runtime().resolve()?.into_inner();

let program_name = config
Expand Down
2 changes: 1 addition & 1 deletion src/wasmer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ fn setup_tty(options: &SpawnOptions, tty_options: TtyOptions) -> TerminalMode {
// Handle the simple (non-interactive) case first.
if let Some(stdin) = options.read_stdin() {
return TerminalMode::NonInteractive {
stdin: virtual_fs::StaticFile::new(stdin.into()),
stdin: virtual_fs::StaticFile::new(stdin),
};
}

Expand Down
16 changes: 16 additions & 0 deletions tests/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,22 @@ describe("Wasmer.spawn", function () {
expect(output.stderr.length).to.equal(0);
});

it("Can pass stdin to a dumb echo program", async () => {
const pkg = await Wasmer.fromRegistry(
"christoph/[email protected]",
);
const instance = await pkg.commands["stdinout-loop"].run({
stdin: "Hello\nWorld!\n",
});

const output = await instance.wait();

console.log({...output});
expect(output.ok).to.be.true;
expect(output.code).to.equal(0);
expect(output.stderr).to.equal("Hello\n\nWorld!\n\n");
});

it("Can communicate with a dumb echo program", async () => {
// First, start our program in the background
const pkg = await Wasmer.fromRegistry(
Expand Down
50 changes: 25 additions & 25 deletions tests/run.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { expect } from "@esm-bundle/chai";
import {
Runtime,
runWasix,
wat2wasm,
Wasmer,
Expand All @@ -9,17 +8,12 @@ import {
Directory,
} from "..";

let runtime: Runtime;
const decoder = new TextDecoder("utf-8");
const encoder = new TextEncoder();

const initialized = (async () => {
await init();
initializeLogger("warn");
runtime = new Runtime();
})();

describe("run", function () {
describe.skip("run", function () {
this.timeout("60s").beforeAll(async () => await initialized);

it("can execute a noop program", async () => {
Expand All @@ -32,7 +26,7 @@ describe("run", function () {
const wasm = wat2wasm(noop);
const module = await WebAssembly.compile(wasm);

const instance = await runWasix(module, { program: "noop", runtime });
const instance = await runWasix(module, { program: "noop" });
const output = await instance.wait();

expect(output.ok).to.be.true;
Expand All @@ -42,12 +36,10 @@ describe("run", function () {
it("can start quickjs", async () => {
const pkg = await Wasmer.fromRegistry("saghul/[email protected]");
const quickjs = pkg.commands["quickjs"].binary();
const module = await WebAssembly.compile(quickjs);

const instance = await runWasix(module, {
const instance = await runWasix(quickjs, {
program: "quickjs",
args: ["--eval", "console.log('Hello, World!')"],
runtime,
});
const output = await instance.wait();

Expand All @@ -62,16 +54,14 @@ describe("run", function () {
await dir.writeFile("/file.txt", "");
const pkg = await Wasmer.fromRegistry("saghul/[email protected]");
const quickjs = pkg.commands["quickjs"].binary();
const module = await WebAssembly.compile(quickjs);

const instance = await runWasix(module, {
const instance = await runWasix(quickjs, {
program: "quickjs",
args: [
"--std",
"--eval",
`[dirs] = os.readdir("/"); console.log(dirs.join("\\n"))`,
],
runtime,
mount: {
"/mount": dir,
},
Expand All @@ -86,19 +76,17 @@ describe("run", function () {

it("can read files", async () => {
const tmp = new Directory();
await tmp.writeFile("/file.txt", encoder.encode("Hello, World!"));
await tmp.writeFile("/file.txt", "Hello, World!");
const pkg = await Wasmer.fromRegistry("saghul/[email protected]");
const quickjs = pkg.commands["quickjs"].binary();
const module = await WebAssembly.compile(quickjs);

const instance = await runWasix(module, {
const instance = await runWasix(quickjs, {
program: "quickjs",
args: [
"--std",
"--eval",
`console.log(std.open('/tmp/file.txt', "r").readAsString())`,
],
runtime,
mount: {
"/tmp": tmp,
},
Expand All @@ -114,16 +102,14 @@ describe("run", function () {
it("can read files mounted using DirectoryInit", async () => {
const pkg = await Wasmer.fromRegistry("saghul/[email protected]");
const quickjs = pkg.commands["quickjs"].binary();
const module = await WebAssembly.compile(quickjs);

const instance = await runWasix(module, {
const instance = await runWasix(quickjs, {
program: "quickjs",
args: [
"--std",
"--eval",
`console.log(std.open('/tmp/file.txt', "r").readAsString())`,
],
runtime,
mount: {
"/tmp": {
"file.txt": "Hello, World!",
Expand All @@ -142,17 +128,15 @@ describe("run", function () {
const dir = new Directory();
const pkg = await Wasmer.fromRegistry("saghul/[email protected]");
const quickjs = pkg.commands["quickjs"].binary();
const module = await WebAssembly.compile(quickjs);
const script = `
const f = std.open('/mount/file.txt', 'w');
f.puts('Hello, World!');
f.close();
`;

const instance = await runWasix(module, {
const instance = await runWasix(quickjs, {
program: "quickjs",
args: ["--std", "--eval", script],
runtime,
mount: {
"/mount": dir,
},
Expand All @@ -162,4 +146,20 @@ describe("run", function () {
expect(output.ok).to.be.true;
expect(await dir.readTextFile("/file.txt")).to.equal("Hello, World!");
});
});

it("can accept strings as stdin", async () => {
const pkg = await Wasmer.fromRegistry("saghul/[email protected]");
const quickjs = pkg.commands["quickjs"].binary();

const instance = await runWasix(quickjs, {
program: "quickjs",
args: ["--interactive", "--std"],
stdin: "console.log('Hello, World!');\nstd.exit(42)\n",
});
const output = await instance.wait();

expect(output.code).to.equal(42);
expect(output.stdout).to.contain("Hello, World!\n");
expect(output.stderr).to.be.empty;
});
})

0 comments on commit 7ea45ca

Please sign in to comment.