-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wasi: allow WASI stdio to be configured
This commit adds stdin, stderr, and stdout options to WASI, which allow the stdio streams to be configured. PR-URL: #33544 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: David Carlier <[email protected]> Reviewed-By: Juan José Arboleda <[email protected]> Reviewed-By: Zeyu Yang <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: James M Snell <[email protected]>
- Loading branch information
1 parent
a3b6095
commit 8adfb54
Showing
5 changed files
with
71 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Flags: --experimental-wasi-unstable-preview1 --experimental-wasm-bigint | ||
'use strict'; | ||
require('../common'); | ||
const tmpdir = require('../common/tmpdir'); | ||
const { strictEqual } = require('assert'); | ||
const { closeSync, openSync, readFileSync, writeFileSync } = require('fs'); | ||
const { join } = require('path'); | ||
const { WASI } = require('wasi'); | ||
const modulePath = join(__dirname, 'wasm', 'stdin.wasm'); | ||
const buffer = readFileSync(modulePath); | ||
const stdinFile = join(tmpdir.path, 'stdin.txt'); | ||
const stdoutFile = join(tmpdir.path, 'stdout.txt'); | ||
const stderrFile = join(tmpdir.path, 'stderr.txt'); | ||
|
||
tmpdir.refresh(); | ||
// Write 33 x's. The test's buffer only holds 31 x's + a terminator. | ||
writeFileSync(stdinFile, 'x'.repeat(33)); | ||
|
||
const stdin = openSync(stdinFile, 'r'); | ||
const stdout = openSync(stdoutFile, 'a'); | ||
const stderr = openSync(stderrFile, 'a'); | ||
const wasi = new WASI({ stdin, stdout, stderr, returnOnExit: true }); | ||
const importObject = { wasi_snapshot_preview1: wasi.wasiImport }; | ||
|
||
(async () => { | ||
const { instance } = await WebAssembly.instantiate(buffer, importObject); | ||
|
||
strictEqual(wasi.start(instance), 0); | ||
closeSync(stdin); | ||
closeSync(stdout); | ||
closeSync(stderr); | ||
strictEqual(readFileSync(stdoutFile, 'utf8').trim(), 'x'.repeat(31)); | ||
strictEqual(readFileSync(stderrFile, 'utf8').trim(), ''); | ||
})(); |