|
| 1 | +import { join } from "node:path"; |
| 2 | + |
| 3 | +import { suite, test, assert } from "vitest"; |
| 4 | + |
| 5 | +import { WASIShim } from "@bytecodealliance/preview2-shim/instantiation"; |
| 6 | + |
| 7 | +import { setupAsyncTest } from "./helpers.js"; |
| 8 | +import { LOCAL_TEST_COMPONENTS_DIR } from "./common.js"; |
| 9 | + |
| 10 | +suite("non-wizered raw initialize export", () => { |
| 11 | + test("sync", async () => { |
| 12 | + const name = "non-wizered-init"; |
| 13 | + // NOTE: simply importing the instance will run the initialization function, |
| 14 | + // and it should not fail |
| 15 | + const { instance, cleanup } = await setupAsyncTest({ |
| 16 | + component: { |
| 17 | + name, |
| 18 | + path: join(LOCAL_TEST_COMPONENTS_DIR, `${name}.wasm`), |
| 19 | + imports: { |
| 20 | + ...new WASIShim({ sandbox: { env: { TEST: "YES" } } }).getImportObject(), |
| 21 | + "jco:test-components/get-string": { |
| 22 | + getString() { |
| 23 | + return "FROM IMPORT"; |
| 24 | + }, |
| 25 | + }, |
| 26 | + }, |
| 27 | + }, |
| 28 | + jco: { |
| 29 | + transpile: { |
| 30 | + extraArgs: { |
| 31 | + minify: false, |
| 32 | + }, |
| 33 | + }, |
| 34 | + }, |
| 35 | + }); |
| 36 | + |
| 37 | + assert.strictEqual(instance["jco:test-components/local-run-string"].run(), "YES"); |
| 38 | + assert.strictEqual(instance["jco:test-components/get-string"].getString(), "FROM IMPORT"); |
| 39 | + |
| 40 | + await cleanup(); |
| 41 | + }); |
| 42 | + |
| 43 | + // TODO(breaking): remove this once manually specified async is removed |
| 44 | + test("legacy async", async () => { |
| 45 | + if (typeof WebAssembly?.Suspending !== "function") { |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + const name = "non-wizered-init"; |
| 50 | + // NOTE: simply importing the instance will run the initialization function, |
| 51 | + // and it should not fail |
| 52 | + const { instance, cleanup } = await setupAsyncTest({ |
| 53 | + component: { |
| 54 | + name, |
| 55 | + path: join(LOCAL_TEST_COMPONENTS_DIR, `${name}.wasm`), |
| 56 | + imports: { |
| 57 | + ...new WASIShim({ sandbox: { env: { TEST: "YES" } } }).getImportObject(), |
| 58 | + "jco:test-components/get-string": { |
| 59 | + async getString() { |
| 60 | + return "FROM IMPORT"; |
| 61 | + }, |
| 62 | + }, |
| 63 | + }, |
| 64 | + }, |
| 65 | + jco: { |
| 66 | + transpile: { |
| 67 | + extraArgs: { |
| 68 | + minify: false, |
| 69 | + asyncMode: "jspi", |
| 70 | + asyncImports: ["jco:test-components/get-string#get-string"], |
| 71 | + asyncExports: ["jco:test-components/get-string#get-string"], |
| 72 | + }, |
| 73 | + }, |
| 74 | + }, |
| 75 | + }); |
| 76 | + |
| 77 | + assert.strictEqual(await instance["jco:test-components/get-string"].getString(), "FROM IMPORT"); |
| 78 | + |
| 79 | + await cleanup(); |
| 80 | + }); |
| 81 | +}); |
0 commit comments