-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.ts
130 lines (111 loc) · 4.68 KB
/
index.ts
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
export = {
/** Check support WebAssembly version */
support(version = 1) {
return exists && check(Uint32Array.of(0x6D736100, version))
},
/** Check support streaming compilation and instantiation */
get supportStreaming() { return exists && has(WA.instantiateStreaming) },
feature: {
/** Check support JavaScript BigInt to WebAssembly i64 integration (--experimental-wasm-bigint) */
get bigInt() { return check(bigIntWasm, true) },
/** Check support bulk memory operations (--experimental-wasm-bulk-memory) */
get bulk() { return check(bulkWasm) },
/** Check support exception handling (--experimental-wasm-eh) */
get exceptions() { return check(exceptionsWasm) },
/** Check support 64-bit memory (--experimental-wasm-memory64) */
get memory64() { return check(memory64Wasm) },
/** Check support import & export of mutable global (--experimental-wasm-mut-global) */
get mutableGlobal() { return check(mutableGlobalWasm) },
/** Check support multi values (--experimental-wasm-mv) */
get multiValue() { return check(multiValueWasm) },
/** Check support non-trapping float-to-int conversions (--experimental-wasm-sat-f2i-conversions) */
get saturateConversions() { return check(saturateConversionsWasm) },
/** Check support zero and sign extensions (--experimental-wasm-se) */
get signExtensions() { return check(signExtensionsWasm) },
/** Check support tail call optiminations (--experimental-wasm-return-call) */
get tailCall() { return check(tailCallWasm) },
/** Check support threads and atomics (--experimental-wasm-threads) */
get threads() { return check(threadsWasm) },
/** Check support SIMD (--experimental-wasm-simd) */
get simd() { return check(simdWasm) },
/** Check support basic reference types "externref" (--experimental-wasm-reftypes) */
get references() { return check(referencesWasm) },
/** Check support Type Reflection (--experimental-wasm-type-reflection) */
get typeReflection() { return exists && has((<any>WA.Memory).type) },
/** Check support typed function references and closures (pre-proposal) */
get funcReferences() { return exists && has((<any>WA).Function) },
/* TODO
* - GC
* - Web IDL Bindings (Host binding) ?
*/
}
}
function check(
wasm: ArrayBufferView,
exec?: boolean
) {
if (!exists) return false
const buffer = wasm.buffer
let ok = cache.get(buffer)
if (ok == null) {
if ((ok = WA.validate(buffer)) && exec) {
try {
(new WA.Instance(
new WA.Module(buffer)
).exports['0'] as Function)()
} catch { ok = false }
}
cache.set(buffer, ok!)
}
return ok!
}
const WA = (<any>this).WebAssembly || globalThis.WebAssembly
const exists = typeof WA === 'object'
const has = (entity: unknown) => typeof entity !== 'undefined'
const u8 = (...bytes: number[]) => Uint8Array.of(0, 97, 115, 109, 1, 0, 0, 0, ...bytes)
// const u16 = (...bytes: number[]) => Uint16Array.of(24832, 28019, 1, 0, ...bytes)
const u32 = (...bytes: number[]) => Uint32Array.of(0x6D736100, 1, ...bytes)
const u32a = (...bytes: number[]) => u32(1610679297, 33751040, ...bytes, 40239360, 259)
const u8a = (...bytes: number[]) => u8(1, 4, 1, 96, 0, 0, 3, 2, 1, 0, ...bytes, 11, 0, 10, 4, 110, 97, 109, 101, 2, 3, 1, 0, 0)
const u16a = (...bytes: number[]) => Uint16Array.of(24832, 28019, 1, 0, 1025, 24577, 0, 515, 1, ...bytes)
const u16b = (...bytes: number[]) => u16a(...bytes, 2842, 4096, 28164, 28001, 357, 260, 256, 560, 259, 0)
const cache = new WeakMap<ArrayBufferLike, boolean>()
const bigIntWasm = u32(
1610679553, 58589440, 117440770, 805372165, 101318656, 1107297281, 268438272,
1835101700, 17039717, 36700416, 259
)
const memory64Wasm = u8(
5, 3, 1, 4, 1, 0, 8, 4, 110, 97, 109, 101, 2, 1, 0
)
const bulkWasm = u16a(
773, 1, 2561, 269, 11, 65, 65, 65, 3068, 2816, 2560, 28164, 28001, 613, 259, 0
)
const exceptionsWasm = u32(
1610679297, 33751040, 134873089, 100664833, 185276736
)
const mutableGlobalWasm = u8(
2, 8, 1, 1, 97, 1, 98, 3, 127, 1, 6, 6, 1, 127, 1, 65, 0, 11, 7, 5, 1, 1, 97,
3, 1, 0, 8, 4, 110, 97, 109, 101, 2, 1, 0
)
const multiValueWasm = Uint16Array.of(
24832, 28019, 1, 0, 1537, 24577, 512, 32639, 515, 1, 2058,
1537, 16640, 16640, 2816, 2560, 28164, 28001, 613, 259, 0
)
const saturateConversionsWasm = u16b(
3082, 2561, 17152, 0, 0, 252
)
const signExtensionsWasm = u16b(
2058, 1537, 16640, 49152
)
const tailCallWasm = u32a(
101318657, 301990913, 268438272, 1835101700, 17039717
)
const threadsWasm = u8a(
5, 4, 1, 3, 1, 1, 10, 7, 1, 5, 0, 254, 3, 0
)
const simdWasm = u32a(
84344833, 6357249, 17369600, 4259847, 186257917, 1845758464
)
const referencesWasm = u8a(
10, 7, 1, 5, 0, 208, 112, 26
)