Created
January 8, 2018 11:48
-
-
Save daurnimator/5a7fa933e96e14333962093322e0ff95 to your computer and use it in GitHub Desktop.
Fengari table=>Object helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local js = require "js" | |
-- Helper to copy lua table to a new JavaScript Object | |
-- e.g. Object{mykey="myvalue"} | |
local function Object(t) | |
local o = js.new(js.global.Object) | |
for k, v in pairs(t) do | |
assert(type(k) == "string" or js.typeof(k) == "symbol", "JavaScript only has string and symbol keys") | |
o[k] = v | |
end | |
return o | |
end | |
return { | |
Object = Object; | |
} |
Would it possible for me to write a version of this code in JS rather than Lua? I haven't seen any examples of accessing Lua table members directly from JS - I understand I could make my JS code run the above via fengari, but that feels like I'd be crossing the language barrier an excessive number of times.
Would it possible for me to write a version of this code in JS rather than Lua?
Yes. See the fengari-interop source for how you might do so.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is an alternative to using
js.createproxy(t, "object")