Skip to content

Commit 5bcd097

Browse files
authored
fix: Support passing instantiated wasm instance to init() (#709)
Should address #693.
1 parent 589d9fa commit 5bcd097

File tree

3 files changed

+36
-23
lines changed

3 files changed

+36
-23
lines changed

patches/[email protected]

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
diff --git a/dist/binaries/yoga-wasm-esm.js b/dist/binaries/yoga-wasm-esm.js
22
new file mode 100644
3-
index 0000000000000000000000000000000000000000..ff4a571f5507c9be7ee1257222b6788b7ad3973a
3+
index 0000000000000000000000000000000000000000..f3bc3ec2cf0d70b514ab4f9a03bd3641eddf4225
44
--- /dev/null
55
+++ b/dist/binaries/yoga-wasm-esm.js
66
@@ -0,0 +1,67 @@
@@ -62,7 +62,7 @@ index 0000000000000000000000000000000000000000..ff4a571f5507c9be7ee1257222b6788b
6262
+argPackAdvance:0,fromWireType:function(){},toWireType:function(){}})},s:function(a,b,c,d,e){a=Bb[a];b=M(b);c=Ab(c);var f=[];E[d>>2]=Ea(f);return a(b,c,f,e)},t:function(a,b,c,d){a=Bb[a];b=M(b);c=Ab(c);a(b,c,null,d)},g:qb,m:function(a,b){var c=Db(a,b),d=c[0];b=d.name+"_$"+c.slice(1).map(function(g){return g.name}).join("_")+"$";var e=Eb[b];if(void 0!==e)return e;var f=Array(a-1);e=Cb((g,k,m,l)=>{for(var n=0,p=0;p<a-1;++p)f[p]=c[p+1].readValueFromPointer(l+n),n+=c[p+1].argPackAdvance;g=g[k].apply(g,
6363
+f);for(p=0;p<a-1;++p)c[p+1].ma&&c[p+1].ma(f[p]);if(!d.va)return d.toWireType(m,g)});return Eb[b]=e},D:function(a){4<a&&(J[a].ga+=1)},r:function(a){var b=M(a);Wa(b);qb(a)},c:function(){x("")},x:function(a,b,c){A.copyWithin(a,b,b+c)},w:function(a){var b=A.length;a>>>=0;if(2147483648<a)return!1;for(var c=1;4>=c;c*=2){var d=b*(1+.2/c);d=Math.min(d,a+100663296);var e=Math;d=Math.max(a,d);e=e.min.call(e,2147483648,d+(65536-d%65536)%65536);a:{try{fa.grow(e-ia.byteLength+65535>>>16);na();var f=1;break a}catch(g){}f=
6464
+void 0}if(f)return!0}return!1},z:function(){return 52},u:function(){return 70},y:function(a,b,c,d){for(var e=0,f=0;f<c;f++){var g=E[b>>2],k=E[b+4>>2];b+=8;for(var m=0;m<k;m++){var l=A[g+m],n=Fb[a];0===l||10===l?((1===a?ea:v)(z(n,0)),n.length=0):n.push(l)}e+=k}E[d>>2]=e;return 0}};
65-
+(function(){function a(e){h.asm=e.exports;fa=h.asm.E;na();oa=h.asm.J;qa.unshift(h.asm.F);F--;h.monitorRunDependencies&&h.monitorRunDependencies(F);0==F&&(null!==ta&&(clearInterval(ta),ta=null),G&&(e=G,G=null,e()))}function b(e){a(e.instance)}function c(e){return ya().then(function(f){return WebAssembly.instantiate(f,d)}).then(function(f){return f}).then(e,function(f){v("failed to asynchronously prepare wasm: "+f);x(f)})}var d={a:Jb};F++;h.monitorRunDependencies&&h.monitorRunDependencies(F);if(h.instantiateWasm)try{return h.instantiateWasm(d,
65+
+(function(){function a(e){h.asm=e.exports;fa=h.asm.E;na();oa=h.asm.J;qa.unshift(h.asm.F);F--;h.monitorRunDependencies&&h.monitorRunDependencies(F);0==F&&(null!==ta&&(clearInterval(ta),ta=null),G&&(e=G,G=null,e()))}function b(e){a(e.instance)}function c(e){return ya().then(function(f){return f instanceof WebAssembly.Instance ? f : WebAssembly.instantiate(f,d)}).then(function(f){return f}).then(e,function(f){v("failed to asynchronously prepare wasm: "+f);x(f)})}var d={a:Jb};F++;h.monitorRunDependencies&&h.monitorRunDependencies(F);if(h.instantiateWasm)try{return h.instantiateWasm(d,
6666
+a)}catch(e){v("Module.instantiateWasm callback failed with error: "+e),ca(e)}(function(){return w||"function"!=typeof WebAssembly.instantiateStreaming||ua(H)||"function"!=typeof fetch?c(b):fetch(H,{credentials:"same-origin"}).then(function(e){return WebAssembly.instantiateStreaming(e,d).then(b,function(f){v("wasm streaming compile failed: "+f);v("falling back to ArrayBuffer instantiation");return c(b)})})})().catch(ca);return{}})();
6767
+h.___wasm_call_ctors=function(){return(h.___wasm_call_ctors=h.asm.F).apply(null,arguments)};var Ka=h.___getTypeName=function(){return(Ka=h.___getTypeName=h.asm.G).apply(null,arguments)};h.__embind_initialize_bindings=function(){return(h.__embind_initialize_bindings=h.asm.H).apply(null,arguments)};var Ib=h._malloc=function(){return(Ib=h._malloc=h.asm.I).apply(null,arguments)},S=h._free=function(){return(S=h._free=h.asm.K).apply(null,arguments)};
6868
+h.dynCall_jiji=function(){return(h.dynCall_jiji=h.asm.L).apply(null,arguments)};var Kb;G=function Lb(){Kb||Mb();Kb||(G=Lb)};
@@ -73,7 +73,7 @@ index 0000000000000000000000000000000000000000..ff4a571f5507c9be7ee1257222b6788b
7373
+}
7474
diff --git a/dist/binaries/yoga.wasm b/dist/binaries/yoga.wasm
7575
new file mode 100644
76-
index 0000000000000000000000000000000000000000..785e89f902a4b5d49ce613e54e94ac1ac2bc7890
76+
index 0000000000000000000000000000000000000000..17bd09c0095754d41f152d06ee514f6328088ee2
7777
--- /dev/null
7878
+++ b/dist/binaries/yoga.wasm
7979
@@ -0,0 +1,259 @@

pnpm-lock.yaml

Lines changed: 8 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/yoga.external.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,34 @@
1-
import { loadYoga, type Yoga } from 'yoga-layout/load'
1+
import { loadYoga as loadYogaUntyped, type Yoga } from 'yoga-layout/load'
22

33
let resolveYoga: (yoga: Yoga) => void
44
const yogaPromise: Promise<Yoga> = new Promise((resolve) => {
55
resolveYoga = resolve
66
})
77

8-
export function init(yogaWasm: ArrayBuffer | Buffer) {
9-
// Not properly typed.
10-
;(loadYoga as any)(yogaWasm).then(resolveYoga)
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
20+
}
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)
31+
}
1132
}
1233

1334
export function getYoga() {

0 commit comments

Comments
 (0)