Skip to content

Instantly share code, notes, and snippets.

@daurnimator
Created January 8, 2018 11:48
Show Gist options
  • Save daurnimator/5a7fa933e96e14333962093322e0ff95 to your computer and use it in GitHub Desktop.
Save daurnimator/5a7fa933e96e14333962093322e0ff95 to your computer and use it in GitHub Desktop.
Fengari table=>Object helper
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;
}
@daurnimator
Copy link
Author

This is an alternative to using js.createproxy(t, "object")

@roddypratt
Copy link

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.

@daurnimator
Copy link
Author

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