You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I’m not 100% sure I dug out the very core issue here, but I put a sufficiently minimal repro in a gist, which will hopefully help analyze the bug.
I import a function which takes a string as a parameter, which is working fine with as-bind. However, some of my functions rely on this being the object the imported function is defined on. So I thought I’d bind() all functions before importing. as-bind handles that, too. Then I thought I should do the binding lazily, so I added a Proxy that does the binding on GET and caches it in a Map. Now, as-bind is failing to do its job and I just get a pointer.
import{AsBind}from"as-bind";// Bundler magic compiles my ASC for meimportwasmUrlfrom"asc:./main.ts";// Manually binds all methods found on `obj` to `obj.functionmanualbind(obj){constreturnobj={};for(constkeyofObject.keys(obj)){if(typeofobj[key]==="function"){returnobj[key]=obj[key].bind(obj);}returnobj[key]=obj[key];}returnreturnobj;}// *Lazily* binds all methods found on `obj` to `obj.functionproxybind(obj){constbindCache=newMap();returnnewProxy(obj,{get(target,p){if(bindCache.has(p)){returnbindCache.get(p);}if(typeoftarget?.[p]==="function"){constbound=target[p].bind(target);bindCache.set(p,bound);returnbound;}returntarget[p];},});}functionmyalert(s){alert(`message: ${JSON.stringify(s)}, has a this: ${this!==null}`);}asyncfunctionmain(){// Works fine!constinstance=awaitAsBind.instantiate(fetch(wasmUrl),{main: { myalert },});instance.exports.run("vanilla");// Works fine!constinstance2=awaitAsBind.instantiate(fetch(wasmUrl),{main: manualbind({ myalert }),});instance2.exports.run("manualbind");// Breaks. I only get a pointer value and not the string.constinstance3=awaitAsBind.instantiate(fetch(wasmUrl),{main: proxybind({ myalert }),});instance3.exports.run("proxybind");}main();
Can you take a look and let me know if it’s something that I am doing wrong or something we can fix in as-bind?
The text was updated successfully, but these errors were encountered:
So, I'll be honest, I'm not super strong at using Proxies. So I'm not quite sure where to start fixing this 😯 But! Perhaps it's because we aren't using arrow functions to bind our stuff? But, we need that so we can set function properties 🤔
Let me know if this helps, or if you need me to poke around a bit more 😯 I can buckle down and try to figure out how Proxies work haha! 😂
I’m not 100% sure I dug out the very core issue here, but I put a sufficiently minimal repro in a gist, which will hopefully help analyze the bug.
I import a function which takes a
string
as a parameter, which is working fine withas-bind
. However, some of my functions rely onthis
being the object the imported function is defined on. So I thought I’dbind()
all functions before importing.as-bind
handles that, too. Then I thought I should do the binding lazily, so I added aProxy
that does the binding onGET
and caches it in aMap
. Now,as-bind
is failing to do its job and I just get a pointer.Can you take a look and let me know if it’s something that I am doing wrong or something we can fix in
as-bind
?The text was updated successfully, but these errors were encountered: