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

Why Lua tables aren't accepted as JS dictionaries? #44

Open
Egor-Skriptunoff opened this issue Mar 15, 2019 · 3 comments
Open

Why Lua tables aren't accepted as JS dictionaries? #44

Egor-Skriptunoff opened this issue Mar 15, 2019 · 3 comments

Comments

@Egor-Skriptunoff
Copy link

I want to translate this JS one-liner to Lua:

Split(['#Left', '#Right'], {gutterSize: 15, minSize: 270, sizes: [25, 75], snapOffset: 0, expandToMin: true});

This works, but is too bulky:

local window = js.global
local options = js.new(window.Object)
options.gutterSize = 15
options.minSize = 270
options.sizes = window:Array(25, 75)
options.snapOffset = 0
options.expandToMin = true
window:Split(window:Array('#Left', '#Right'), options)

This looks nice, but doesn't work:

local window = js.global
window:Split(window:Array('#Left', '#Right'), {gutterSize = 15, minSize = 270, sizes = window:Array(25, 75), snapOffset = 0, expandToMin = true})

Why Fengari doesn't convert Lua tables to JS dictionaries on-the-fly?
It's too boring to write this conversion manually.

If there are some reasons why such conversion could not be done automatically,
please add a function js.dict(lua_table) which will convert Lua-curly-braces into JS-curly-braces.
This function would make creating a JS dictionary on Lua side a lot easier:

window:Split(window:Array('#Left', '#Right'), js.dict{gutterSize = 15, minSize = 270, sizes = window:Array(25, 75), snapOffset = 0, expandToMin = true})

Yes, I know that I can write such function myself on Lua side, but this function is a "must-have" for every developer.

@daurnimator
Copy link
Member

daurnimator commented Mar 16, 2019

See https://github.com/fengari-lua/fengari-interop#createproxyx-type
Especially see the note:

Note that JavaScript coerces all types except Symbols to strings before using them as a key in an indexing operation.

In JS, if you do mything[1], then it converts it to a string before doing the index operation. Meaning that you see mything["1"]. This goes for all objects (not just numbers): if you do mything[{}] then you get mything["[object Object]"].
This really gets in the way of trying to use lua tables as JS objects.

Instead, the default conversion of lua objects to JS is more like a JS Map object, with .get, .set, .has methods etc.

please add a function js.dict(lua_table) which will convert Lua-curly-braces into JS-curly-braces.
This function would make creating a JS dictionary on Lua side a lot easier:

window:Split(window:Array('#Left', '#Right'), js.dict{gutterSize = 15, minSize = 270, sizes = window:Array(25, 75), snapOffset = 0, expandToMin = true})

Yes, I know that I can write such function myself on Lua side, but this function is a "must-have" for every developer.

I use such a function myself (https://gist.github.com/daurnimator/5a7fa933e96e14333962093322e0ff95/) e.g. here

So far fengari-interop doesn't have any "helpers"; so this would be quite a new concept for the library. Though you're probably right that it would be useful...

@kpassapk
Copy link

Is the default conversion of objects to JS is a WeakMap?

@daurnimator
Copy link
Member

daurnimator commented Mar 12, 2021

Is the default conversion of objects to JS is a WeakMap?

no, the default is an object with methods .get, .set etc, similar to a Map.

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

3 participants