Would be nice at some point to have an official overview of how this works. I did find https://www.npmjs.org/package/lua-colony#readme to be helpful although maybe a bit has changed since then.
AFAICT, compilation is more of a syntax conversion — e.g. JavaScript uses braces, Lua uses "end"; plus a few tricks/conventions to pull off "various things" (?). But once you've got the code roughed into shape, the rest of the work is done by "monkeypatched" Lua builtins (primitive strings/numbers/etc and object/array-ish tables) with methods and operators to the point where they ± match JavaScript's behavior.
JavaScript code like ""+{abc:42,fn:function () {}}.abc more or less becomes:
((""))+(_obj({
["abc"]=(42),
["fn"]=(function (this)
--[[28]] if true then return (false); end
end)
}).abc)
… and the + operator ends up doing JavaScript-ish string concatentation due to a custom .__add metatable method on the Lua side (code in runtime's colony-init.lua). The .abc property lookup similarly goes through js_proto_get via a custom .__index method^Wmetamethod.
Would be nice at some point to have an official overview of how this works. I did find https://www.npmjs.org/package/lua-colony#readme to be helpful although maybe a bit has changed since then.
AFAICT, compilation is more of a syntax conversion — e.g. JavaScript uses braces, Lua uses "end"; plus a few tricks/conventions to pull off "various things" (?). But once you've got the code roughed into shape, the rest of the work is done by "monkeypatched" Lua builtins (primitive strings/numbers/etc and object/array-ish tables) with methods and operators to the point where they ± match JavaScript's behavior.
JavaScript code like
""+{abc:42,fn:function () {}}.abcmore or less becomes:… and the
+operator ends up doing JavaScript-ish string concatentation due to a custom.__addmetatable method on the Lua side (code in runtime's colony-init.lua). The.abcproperty lookup similarly goes through js_proto_get via a custom.__indexmethod^Wmetamethod.