-
-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Passing Lua tables to JS functions using JSON #70
Comments
It seems like you're looking for |
Thanks for the reply, I'm stilll a bit lost :( Would I use the createproxy in Lua? Because I'm getting this error The part I'm really lost is how to retrieve the values of the table in the L state by idx. |
Yes. you use it to make lua tables look and act like javascript objects; partially controlled via the lua metatable. |
Just in case anyone ends up here with a similar issue, I was able to resolve it by using a Lua JSON library and the following code. I appended this code before the execution of the Lua code that will be passing a Lua table to JavaScript.
Then when I was getting the arguments from the stack in JavaScript land I used that function as follows.
From there the JavaScript function will receive the Lua table argument as a JSON string which can be converted to object with JSON.parse |
I use this recursive function, which basically extends read = (L, idx) => {
const type = fengari.lua.lua_type(L, idx);
if (type == fengari.lua.LUA_TTABLE) {
const type1 = fengari.lua.lua_geti(L, idx, 1); // Check value type at index 1
fengari.lua.lua_pop(L, 1);
if (type1 != fengari.lua.LUA_TNIL) { // Value at index 1 is not NIL, I read this as array
const array = [];
for (let i = 1; ; i++) {
const typei = fengari.lua.lua_geti(L, idx, i);
const value = read(L, -1);
fengari.lua.lua_pop(L, 1);
if (typei == fengari.lua.LUA_TNIL) break; // Stop searching when an element is NIL
array.push(value);
}
return array;
} else {
const object = {};
fengari.lua.lua_pushnil(L); // https://www.lua.org/manual/5.3/manual.html#lua_next
const t = idx < 0 ? idx - 1 : idx; // New table index, because of the key at the top of the stack
while (fengari.lua.lua_next(L, t) != 0) {
const key = fengari.lua.lua_tojsstring(L, -2);
const value = read(L, -1);
fengari.lua.lua_pop(L, 1);
object[key] = value;
}
return object;
}
} else if (type == fengari.lua.LUA_TNIL) {
return null;
} else {
return fengari.interop.tojs(L, idx);
}
} |
Yeah, we were doing something similar but we were having a lot of problems with nested complex objects like the kind you'll use to send HTTP payloads. We needed a way to work them in Lua as tables to create/update keys and then send them to JavaScript and still be able to work with those objects. That's why serializing as JSON seemed like the best option but since we define a lot of functions we were missing this piece to automatically transform the arguments without doing anything special on Lua code. |
The way I've usually done this is using javascript objects from the start. https://gist.github.com/daurnimator/5a7fa933e96e14333962093322e0ff95/ |
I have an use case where I want to be able to pass lua tables to javascript functions using fengari-interop. Soon enough I noticed that they are passed as weakmaps making impossible to work with them in javascript without knowing in advance the keys of the object passed.
I found a workaround this by converting the lua table to a JSON string using (https://github.com/rxi/json.lua) in the Lua code before calling the javascript function inside Lua and then converting the JSON string to JS object in javascript side.
This works fine but I need to create "proxy" methods to allow the users to use this functions transparently.
Ex.
The users can pass a Lua table to the 'public' API is context.set which then internally calls another context._set with the JSON string as argument. The issue here is that everytime we have a new agument or a new function we need to add this 'proxy' functions.
Is there a way to modify the fengari-interop default conversion for LUA_TTABLE arguments to become regular strings (JSON)?
I appreciate any pointer for this issue, and thanks again for this library :)
The text was updated successfully, but these errors were encountered: