|
1 | 1 | import { loadYoga as loadYogaUntyped, type Yoga } from 'yoga-layout/load' |
2 | 2 |
|
| 3 | +const loadYoga = loadYogaUntyped as (options: { |
| 4 | + wasmBinary?: ArrayBuffer | ArrayBufferLike |
| 5 | + instantiateWasm?: ( |
| 6 | + imports: WebAssembly.Imports, |
| 7 | + successCallback: (instance: WebAssembly.Instance) => void |
| 8 | + ) => WebAssembly.Exports | false | undefined |
| 9 | +}) => Promise<Yoga> |
| 10 | + |
3 | 11 | let resolveYoga: (yoga: Yoga) => void |
4 | | -const yogaPromise: Promise<Yoga> = new Promise((resolve) => { |
| 12 | +let rejectYoga: (error: unknown) => void |
| 13 | +const yogaPromise: Promise<Yoga> = new Promise((resolve, reject) => { |
5 | 14 | resolveYoga = resolve |
| 15 | + rejectYoga = reject |
6 | 16 | }) |
7 | 17 |
|
8 | | -const loadYoga = loadYogaUntyped as ( |
9 | | - wasm: ArrayBuffer | ArrayBufferLike | WebAssembly.Instance |
10 | | -) => Promise<Yoga> |
11 | | - |
12 | | -export function init( |
13 | | - yogaWasm: |
14 | | - | ArrayBuffer |
15 | | - | ArrayBufferLike |
16 | | - | Buffer |
17 | | - | WebAssembly.Instance |
18 | | - | { |
19 | | - instance: WebAssembly.Instance |
| 18 | +export type InitInput = |
| 19 | + | RequestInfo |
| 20 | + | URL |
| 21 | + | Response |
| 22 | + | BufferSource |
| 23 | + | Buffer |
| 24 | + | WebAssembly.Module |
| 25 | + |
| 26 | +async function loadWasm( |
| 27 | + input: InitInput, |
| 28 | + imports: WebAssembly.Imports |
| 29 | +): Promise<WebAssembly.WebAssemblyInstantiatedSource> { |
| 30 | + let source: Response | BufferSource | Buffer | WebAssembly.Module = input |
| 31 | + |
| 32 | + if ( |
| 33 | + typeof source === 'string' || |
| 34 | + (typeof Request === 'function' && source instanceof Request) || |
| 35 | + (typeof URL === 'function' && source instanceof URL) |
| 36 | + ) { |
| 37 | + source = await fetch(source) |
| 38 | + } |
| 39 | + |
| 40 | + if (typeof Response === 'function' && source instanceof Response) { |
| 41 | + if (typeof WebAssembly.instantiateStreaming === 'function') { |
| 42 | + try { |
| 43 | + return await WebAssembly.instantiateStreaming(source, imports) |
| 44 | + } catch (e) { |
| 45 | + if (source.headers.get('Content-Type') !== 'application/wasm') { |
| 46 | + console.warn( |
| 47 | + '`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', |
| 48 | + e |
| 49 | + ) |
| 50 | + } |
20 | 51 | } |
21 | | -) { |
22 | | - // Buffer |
23 | | - if ('buffer' in yogaWasm) { |
24 | | - loadYoga(yogaWasm.buffer).then(resolveYoga) |
25 | | - } else if ('instance' in yogaWasm) { |
26 | | - // { instance: WebAssembly.Instance } |
27 | | - loadYoga(yogaWasm.instance).then(resolveYoga) |
28 | | - } else { |
29 | | - // ArrayBuffer or WebAssembly.Instance |
30 | | - loadYoga(yogaWasm).then(resolveYoga) |
| 52 | + } |
| 53 | + |
| 54 | + const bytes = await source.arrayBuffer() |
| 55 | + return await WebAssembly.instantiate(bytes, imports) |
31 | 56 | } |
| 57 | + |
| 58 | + const instantiated = (await WebAssembly.instantiate( |
| 59 | + 'buffer' in source |
| 60 | + ? source.buffer.slice( |
| 61 | + source.byteOffset, |
| 62 | + source.byteOffset + source.byteLength |
| 63 | + ) |
| 64 | + : source, |
| 65 | + imports |
| 66 | + )) as WebAssembly.Instance | WebAssembly.WebAssemblyInstantiatedSource |
| 67 | + |
| 68 | + if (instantiated instanceof WebAssembly.Instance) { |
| 69 | + return { instance: instantiated, module: source as WebAssembly.Module } |
| 70 | + } |
| 71 | + |
| 72 | + return instantiated |
| 73 | +} |
| 74 | + |
| 75 | +export function init(input: InitInput) { |
| 76 | + loadYoga({ |
| 77 | + instantiateWasm(imports, successCallback) { |
| 78 | + loadWasm(input, imports) |
| 79 | + .then(({ instance }) => { |
| 80 | + successCallback(instance) |
| 81 | + }) |
| 82 | + .catch(rejectYoga) |
| 83 | + |
| 84 | + return {} |
| 85 | + }, |
| 86 | + }) |
| 87 | + .then(resolveYoga) |
| 88 | + .catch(rejectYoga) |
32 | 89 | } |
33 | 90 |
|
34 | 91 | export function getYoga() { |
|
0 commit comments