-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathembedding.mts
More file actions
56 lines (45 loc) · 1.51 KB
/
embedding.mts
File metadata and controls
56 lines (45 loc) · 1.51 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
* This file showcases an embedding of a component that uses an import.
*
* The embedding (this NodeJS script) defines the required imports,
* instantiates the transpiled component with the import,
* and runs the component.
*/
/* global WebAssembly */
import { readFile } from 'node:fs/promises';
import * as wasiShim from '@bytecodealliance/preview2-shim/instantiation';
// If this import listed below is missing, please run `npm run transpile`
import { instantiate } from './dist/transpiled/component.js';
let _LOCAL_RESOURCE_ID = 0n;
/**
* This is our local implementation of the `example` WIT resource
*/
class LocalExample {
#id: bigint;
constructor() {
this.#id = ++_LOCAL_RESOURCE_ID;
console.log(`constructed [LocalExample(${this.#id})]!`);
}
hello(s: String) {
return `[LocalExample(${this.#id})] Hello ${s}!`;
}
[Symbol.dispose]() {
console.error(`disposing [LocalExample(${this.#id})]`);
}
}
async function main() {
// Build the imports the component requires
const imports = {
...new (wasiShim as any).WASIShim().getImportObject(),
'test:component/resources': {
Example: LocalExample,
},
};
// Instantiate the component with the custom imports
const loader = async (path: string) => {
const buf = await readFile(`./dist/transpiled/${path}`);
return await WebAssembly.compile(buf.buffer as ArrayBuffer);
};
const component = await instantiate(loader, imports);
}
await main();