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

Get keys from Lua table

Open kpassapk opened this issue 4 years ago • 1 comments

I'm finding that when I convert a Lua table to JS, I can't examine its keys. Is there a way of doing this?

{ foo = 1, bar = 2}

In Javascript, I would like to type:

# ... convert above Lua to js using tojs() as object o
Object.keys(o)

kpassapk avatar Mar 11 '21 17:03 kpassapk

Object.keys(o)

Object.keys is only able to return strings and symbols. As lua tables main contain keys of any type, this is not suitable as a default. (see #39)

However, you can use js.createProxy and a custom metamethod if you need to create an object for a specific API. Or at least I thought you could.... I'm trying js.createproxy(setmetatable({foo=1, bar=2}, {__index=print, ownKeys=function(o) return js.global:Array("foo","bar") end})) but Object.keys on it returns an empty array (firefox 86.0)

daurnimator avatar Mar 12 '21 02:03 daurnimator