-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_env_count.mjs
More file actions
30 lines (26 loc) · 1.03 KB
/
Copy pathtest_env_count.mjs
File metadata and controls
30 lines (26 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// SPDX-License-Identifier: PMPL-1.0-or-later
// ADR-015 S4b (#180) — env_count via WASI preview1 environ_sizes_get.
// Host stubs the import with a known count + buf_size; asserts the
// guest returns that count.
import assert from 'node:assert/strict';
import { readFile } from 'node:fs/promises';
const buf = await readFile('./tests/codegen/env_count.wasm');
let inst = null;
let observed = null;
const imports = {
wasi_snapshot_preview1: {
fd_write: () => 0,
environ_sizes_get: (envc_ptr, envbuf_ptr) => {
observed = { envc_ptr, envbuf_ptr };
const dv = new DataView(inst.exports.memory.buffer);
dv.setUint32(envc_ptr, 7, true); // 7 env vars
dv.setUint32(envbuf_ptr, 256, true); // unused by env_count
return 0;
},
},
};
inst = (await WebAssembly.instantiate(buf, imports)).instance;
const result = inst.exports.main();
assert.ok(observed, 'guest called environ_sizes_get');
assert.equal(result, 7, 'env_count returns the count the host wrote');
console.log('test_env_count.mjs OK');