fengari-interop icon indicating copy to clipboard operation
fengari-interop copied to clipboard

js.typeof is broken

Open s-ol opened this issue 6 years ago • 2 comments

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)

s-ol avatar Oct 11 '19 09:10 s-ol

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

daurnimator avatar Oct 11 '19 16:10 daurnimator

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"];

mrunderhill89 avatar Mar 11 '20 21:03 mrunderhill89