[Unexpected Behavior] InvokeFunctionReference returns "Bad Cast" during NUI callback

I get this error from time to time, and I always manage to fix it, but I never am sure quite how, even though it takes many hours if not days.

I’m pretty convinced it can’t just be a me thing, though.

JS Code:

Lua Code that Crashes:

Lua code for the functioning callback:
image

F8 output:

I’ve tried changing the javascript code to instead of axios.all, using Promise.All, and two seperate axios.posts with no luck.

I’ve also tried debugging the broken LUA callback. Encapsulating rv in curly braces and un-curly braced both return this error. Calling cb() without an argument fails silently, resulting in the axios.all never being called. calling cb({}) results in further unexpected behavior down the line I would expect is a problem in my javascript code,

I have no idea where to begin with this, other than to rewrite the affected methods from scratch in case I’m just burnt and blind.

Has anyone else run into this and subsequently fixed it? If so, how?

Javascript functions cannot be used as callbacks for Lua, and vice-versa. (iirc)

Okay, so why does cb(threads) [type: table] work while cb(rv) [type: table] does not?

I’m not trying to pass functions into callbacks by any means!

What’s the point of cb(threads) even? It never gets used, so just remove it, as I said (iirc) you cannot use JS functions in Lua and vice-versa.

what? that’s nonsense

It does get used and you can’t ‘just remove it’! Did you even read the docs or are you just talking nonsense?

For a NUI callback you always have to call the cb, and in this case the user actually wants to return actual data so again you’re speaking utter crap.

Can you provide a repro? I guess some type from MessagePack doesn’t get mapped to JSON correctly.

Well I’ve had trouble with that before, I tried to use a Lua function as callback for JS and it wouldn’t let me, that was ages ago tho.

Maybe I got something wrong on my end, anyways I told OP "iirc"

This should do it.

nui_badcall.zip (1.8 KB)

1 Like

Should be fixed on latest.

1 Like

Few additional comments (likely to put on the back burner, as they are mainly for non-funcrefs, I suppose):

  1. Replace local index = 991 in the repro with an index that would not fit with within a 32-bit integer, e.g., local index = 2147483647 + 1.

  2. Maps in msgpack are not statically typed and each key has their own type data (Here is how its handled in the msgpack-c example). Someone may run into an issue of the first “key” not being a string (given the unordered nature of Lua tables), or a map with Integer key 1 and String key “1” (there could probably be some deterministic rules here?);