11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
41cb0ef41Sopenharmony_ciconst { strictEqual } = require('assert');
51cb0ef41Sopenharmony_ciconst { closeSync, openSync, readFileSync, writeFileSync } = require('fs');
61cb0ef41Sopenharmony_ciconst { join } = require('path');
71cb0ef41Sopenharmony_ciconst { WASI } = require('wasi');
81cb0ef41Sopenharmony_ciconst modulePath = join(__dirname, 'wasm', 'stdin.wasm');
91cb0ef41Sopenharmony_ciconst buffer = readFileSync(modulePath);
101cb0ef41Sopenharmony_ciconst stdinFile = join(tmpdir.path, 'stdin.txt');
111cb0ef41Sopenharmony_ciconst stdoutFile = join(tmpdir.path, 'stdout.txt');
121cb0ef41Sopenharmony_ciconst stderrFile = join(tmpdir.path, 'stderr.txt');
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_citmpdir.refresh();
151cb0ef41Sopenharmony_ci// Write 33 x's. The test's buffer only holds 31 x's + a terminator.
161cb0ef41Sopenharmony_ciwriteFileSync(stdinFile, 'x'.repeat(33));
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ciconst stdin = openSync(stdinFile, 'r');
191cb0ef41Sopenharmony_ciconst stdout = openSync(stdoutFile, 'a');
201cb0ef41Sopenharmony_ciconst stderr = openSync(stderrFile, 'a');
211cb0ef41Sopenharmony_ciconst wasi = new WASI({ stdin, stdout, stderr, returnOnExit: true });
221cb0ef41Sopenharmony_ciconst importObject = { wasi_snapshot_preview1: wasi.wasiImport };
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci(async () => {
251cb0ef41Sopenharmony_ci  const { instance } = await WebAssembly.instantiate(buffer, importObject);
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci  strictEqual(wasi.start(instance), 0);
281cb0ef41Sopenharmony_ci  closeSync(stdin);
291cb0ef41Sopenharmony_ci  closeSync(stdout);
301cb0ef41Sopenharmony_ci  closeSync(stderr);
311cb0ef41Sopenharmony_ci  strictEqual(readFileSync(stdoutFile, 'utf8').trim(), 'x'.repeat(31));
321cb0ef41Sopenharmony_ci  strictEqual(readFileSync(stderrFile, 'utf8').trim(), '');
331cb0ef41Sopenharmony_ci})().then(common.mustCall());
34