-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathembedding.mts
More file actions
61 lines (52 loc) · 2.23 KB
/
embedding.mts
File metadata and controls
61 lines (52 loc) · 2.23 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
57
58
59
60
61
/**
* This file showcases an embedding of a component that exports a resource.
*
* The embedding (this NodeJS script) instantiates the component and
* makes use of the resources that were exported.
*
* One of the exported resources has dispose configured, but the other does not,
* note that the disposal behavior will be triggered from the embedder once the
* created resource goes out of scope.
*
*/
/* global WebAssembly */
import { readFile } from 'node:fs/promises';
import { 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';
async function main() {
// 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 shim = new WASIShim();
const { resources } = await instantiate(loader, {
// NOTE: While we can implement the shim once, we have to
// merge different versions of it to create a complete import object
// that the transpiled component can use.
//
// The lines below use the shim above *as* 0.2.7
// inject the same shim as various versions of 0.2.x
// which are required by the component and/or the build of
// StarlingMonkey underneath
//
// To figure out which versions are required,
// use `jco wit <component path>` and observe the versions of
// various wasi dependencies that are in use via imports/exports
//
...shim.getImportObject<'0.2.7'>({ asVersion: '0.2.7' }),
...shim.getImportObject<'0.2.3'>({ asVersion: '0.2.3' }),
...shim.getImportObject(),
});
// TODO: disposable resources are not yet implemented properly
// {
// using ex1 = new resources.Example();
// console.log('Hello from ex1:', ex1.hello('ex1'));
// }
const ex1 = new resources.Example();
console.log('Hello from ex1:', ex1.hello('ex1'));
const ex2 = new resources.ExampleNoDispose();
console.log('Hello from ex2:', ex2.hello('ex2'));
}
await main();