fengari-lua / fengari-interop

Fengari <=> JS Interop
MIT License
97 stars 13 forks source link

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

Open Egor-Skriptunoff opened 5 years ago

Egor-Skriptunoff commented 5 years ago

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 commented 5 years ago

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 commented 3 years ago

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

daurnimator commented 3 years ago

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.