wasmoon icon indicating copy to clipboard operation
wasmoon copied to clipboard

Can't use constructors with JS interop

Open qgcarver opened this issue 1 year ago • 3 comments

If I try to pass in a function that takes in a constructor as a parameter, the closure I send in doesn't treat it that way.

lua.global.set("DOM",{
    new: (T=>{
        let Constructor = T;
        return (...a) => {
            let constructed = new Constructor(...a);
            return constructed;
        };
    }),
    window: window
    log: console.log
    exampleElement: document.getElementById("mycanvas")
}),

lua.doString(`
function f()
  local ImageConstructor = DOM.new(DOM.window.Image)
  DOM.log(ImageConstructor())
end
DOM.exampleElement.onclick = f
`)

I get an error saying n is not a constructor I tried currying and saving a local as seen above to try to work around this, but it seems like there's some kind of wrapping going on that's preventing that from working. It seems like a way to support constructors in the interop system itself would be good, (if it doesn't already exist and I just missed it.)

qgcarver avatar Sep 27 '24 14:09 qgcarver

Have you enabled the proxy?

ceifa avatar Nov 17 '24 14:11 ceifa

Have you enabled the proxy?

not sure I follow, but in the code I've been writing so far I've been using the JS objects without issue aside from this. In the sense that the stuff I imported works fine, including objects returned from functions like document.createElement whose members I seem to be able to access totally fine.

I had assumed the JS proxy objects were "enabled" by just being passed to Lua. If you enable the proxy per-object somehow I am not familiar with how to do that.

https://github.com/qgcarver/MoonBounce-web/blob/main/src/main.js#L882

qgcarver avatar Nov 17 '24 19:11 qgcarver

This happens because Image is typeof function but is not a function. That happens with some old js constructors. This is a fix I did on an other project:

https://github.com/ceifa/demoon/blob/main/src/legacyclasses.js

Not sure if this is fixable by wasmoon, probably we would have to mantain a list of classes of this type.

ceifa avatar Dec 24 '24 16:12 ceifa