-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgofmt_web.js
More file actions
85 lines (71 loc) · 2.24 KB
/
gofmt_web.js
File metadata and controls
85 lines (71 loc) · 2.24 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/* @ts-self-types="./gofmt_web.d.ts" */
import { format as _format } from "./gofmt_binding.js";
let wasm, wasmModule;
async function load(module, imports) {
if (typeof Response === "function" && module instanceof Response) {
if (typeof WebAssembly.instantiateStreaming === "function") {
try {
return await WebAssembly.instantiateStreaming(module, imports);
} catch (e) {
const validResponse = module.ok && expectedResponseType(module.type);
if (validResponse && module.headers.get("Content-Type") !== "application/wasm") {
console.warn(
"`WebAssembly.instantiateStreaming` failed because your server does not serve Wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n",
e,
);
} else {
throw e;
}
}
}
const bytes = await module.arrayBuffer();
return await WebAssembly.instantiate(bytes, imports);
} else {
const instance = await WebAssembly.instantiate(module, imports);
if (instance instanceof WebAssembly.Instance) {
return { instance, module };
} else {
return instance;
}
}
function expectedResponseType(type) {
switch (type) {
case "basic":
case "cors":
case "default":
return true;
}
return false;
}
}
function finalize_init(instance, module) {
((wasm = instance.exports), (wasmModule = module));
wasm._initialize();
return wasm;
}
export function initSync(buffer_or_module) {
if (wasm !== void 0) return wasm;
if (!(buffer_or_module instanceof WebAssembly.Module)) {
buffer_or_module = new WebAssembly.Module(buffer_or_module);
}
const instance = new WebAssembly.Instance(buffer_or_module);
return finalize_init(instance, buffer_or_module);
}
export default async function initAsync(init_input) {
if (wasm !== void 0) return wasm;
if (init_input === void 0) {
init_input = new URL("gofmt.wasm", import.meta.url);
}
if (
typeof init_input === "string" ||
(typeof Request === "function" && init_input instanceof Request) ||
(typeof URL === "function" && init_input instanceof URL)
) {
init_input = fetch(init_input);
}
const { instance, module } = await load(await init_input);
return finalize_init(instance, module);
}
export function format(source) {
return _format(wasm, source);
}