fengari-lua / fengari-interop

Fengari <=> JS Interop
MIT License
98 stars 13 forks source link

js.typeof is broken #50

Open s-ol opened 5 years ago

s-ol commented 5 years ago

Minimal example, e.g. in the https://fengari.io REPL:

js.typeof(js.global)
Uncaught exception TypeError: Cannot read property 'funcOff' of undefined
    at rt (javascript:8)
    at Et (javascript:8)
    at It (javascript:8)
    at t (javascript:8)
    at t (javascript:8)
    at Object.t [as luaD_precall] (javascript:8)
    at Object.t.exports.luaV_execute (javascript:8)
    at ut (javascript:8)
    at Object.ht [as luaD_callnoyield] (javascript:8)
    at Object.b [as luaT_callTM] (javascript:8)
daurnimator commented 5 years ago

This seems to be a minifier bug. For some reason,


    "typeof": function(L) {
        let u = tojs(L, 1);
        lua_pushliteral(L, typeof u);
        return 1;
    }

is getting compiled to:

  "typeof": function _typeof(L) {
    var u = tojs(L, 1);
    lua_pushliteral(L, _typeof(u));
    return 1;
  }

Which ends up calling itself with invalid parameters.

Next steps:

  1. try with newer version of minifier/webpack
  2. play with syntax to see if there is a way around it
  3. change the code to avoid using typeof directly
mrunderhill89 commented 4 years ago

I've got the same issue, and it also seems to affect instanceof (same issue of calling itself instead of the internal function), so there is currently no way to check JS types from Lua. I'm working on a pull request right now, and aliasing it to jstypeof seems to work, though I haven't gotten jsinstanceof working yet.

Update: So instanceof was working after all, I just forgot to use class objects instead of strings on the right-hand side. Can't create pull requests, but the following code seems to fix typeof seamlessly.

    "jstypeof": function(L) {
        let u = tojs(L, 1);
        lua_pushliteral(L, typeof u);
        return 1;
    }
};
jslib["typeof"] = jslib["jstypeof"];