|
| 1 | +// Flags: --experimental-wasi-unstable-preview1 --experimental-wasm-bigint |
| 2 | +'use strict'; |
| 3 | +require('../common'); |
| 4 | +const tmpdir = require('../common/tmpdir'); |
| 5 | +const { strictEqual } = require('assert'); |
| 6 | +const { closeSync, openSync, readFileSync, writeFileSync } = require('fs'); |
| 7 | +const { join } = require('path'); |
| 8 | +const { WASI } = require('wasi'); |
| 9 | +const modulePath = join(__dirname, 'wasm', 'stdin.wasm'); |
| 10 | +const buffer = readFileSync(modulePath); |
| 11 | +const stdinFile = join(tmpdir.path, 'stdin.txt'); |
| 12 | +const stdoutFile = join(tmpdir.path, 'stdout.txt'); |
| 13 | +const stderrFile = join(tmpdir.path, 'stderr.txt'); |
| 14 | + |
| 15 | +tmpdir.refresh(); |
| 16 | +// Write 33 x's. The test's buffer only holds 31 x's + a terminator. |
| 17 | +writeFileSync(stdinFile, 'x'.repeat(33)); |
| 18 | + |
| 19 | +const stdin = openSync(stdinFile, 'r'); |
| 20 | +const stdout = openSync(stdoutFile, 'a'); |
| 21 | +const stderr = openSync(stderrFile, 'a'); |
| 22 | +const wasi = new WASI({ stdin, stdout, stderr, returnOnExit: true }); |
| 23 | +const importObject = { wasi_snapshot_preview1: wasi.wasiImport }; |
| 24 | + |
| 25 | +(async () => { |
| 26 | + const { instance } = await WebAssembly.instantiate(buffer, importObject); |
| 27 | + |
| 28 | + strictEqual(wasi.start(instance), 0); |
| 29 | + closeSync(stdin); |
| 30 | + closeSync(stdout); |
| 31 | + closeSync(stderr); |
| 32 | + strictEqual(readFileSync(stdoutFile, 'utf8').trim(), 'x'.repeat(31)); |
| 33 | + strictEqual(readFileSync(stderrFile, 'utf8').trim(), ''); |
| 34 | +})(); |
0 commit comments