-
Notifications
You must be signed in to change notification settings - Fork 29.7k
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 <anna@addaleax.net> Reviewed-By: David Carlier <devnexen@gmail.com> Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com> Reviewed-By: Zeyu Yang <himself65@outlook.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: James M Snell <jasnell@gmail.com>
- Loading branch information
1 parent
6d25b57
commit af14c1f
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(), ''); | ||
})(); |