Skip to content
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

Get keys from Lua table #58

Open
kpassapk opened this issue Mar 11, 2021 · 1 comment
Open

Get keys from Lua table #58

kpassapk opened this issue Mar 11, 2021 · 1 comment

Comments

@kpassapk
Copy link

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)
@daurnimator
Copy link
Member

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants