-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathimagewand.js
74 lines (62 loc) · 2.15 KB
/
imagewand.js
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
62
63
64
65
66
67
68
69
70
71
72
73
74
import { wasmWorker } from "./worker-proxy.js";
// Available ImageWand modes/binary types
const InstanceType = {
GO: "GO", // Golang
WORKER: "WORKER", // Golang (uses a worker); This was mostly an experiment
TINYGO: "TINYGO", // TinyGo
};
// TinyGo does not work well with strings (throws a `syscall/js.finalizeRef` error). In ImageWand case
// these could be replaced with integers (enum). There are hacks around the string usage, but it still leads
// to memory leaks https://github.com/tinygo-org/tinygo/issues/1140
export const formatGoNumber = {
jpg: 1,
png: 2,
gif: 3,
tiff: 4,
bmp: 5,
};
export const formatToNumber = (input) => formatGoNumber[input];
export const fromURL = (href) => {
const url = new URL(href);
const params = new URLSearchParams(url.search);
return ImageWand(params.get("type") || params.get("t"));
};
// Based on a t (binary type), it selects the correct wasm_exec.js and load the correct binary.
// This because TinyGo and Golang use different wasm_exec.js.
export const ImageWand = async (t) => {
if (!t) t = InstanceType.TINYGO;
switch (t.toUpperCase()) {
default:
case InstanceType.TINYGO: {
console.log('Starting as "tinygo"');
await import("./wasm-tinygo-exec.js");
const go = new window.Go();
const obj = await WebAssembly.instantiateStreaming(
fetch("/wasm/main-tinygo.wasm"),
go.importObject
);
const wasm = obj.instance;
go.run(wasm);
// uses the WASM binary exported functions
return Promise.resolve(wand);
}
case InstanceType.STANDARD: {
console.log('Starting as "standard"');
await import("./wasm-go-exec.js");
const go = new window.Go();
const result = await WebAssembly.instantiateStreaming(
fetch("/wasm/main-go.wasm"),
go.importObject
);
const inst = result.instance;
go.run(inst); // fire and forget
// uses the global `wand`
return Promise.resolve(wand);
}
case InstanceType.WORKER: {
console.log('Starting as "worker"');
await import("./wasm-go-exec.js");
return wasmWorker("/wasm/main-go.wasm", "/js/worker.js", "wand");
}
}
};