tessel / colony-compiler

[UNMAINTAINED] "Compiles" JS to Lua to run on Colony.
Other
80 stars 16 forks source link

Needs documentation for the runtime JS shims in tessel/runtime #15

Open paulcuth opened 10 years ago

paulcuth commented 10 years ago

Hi,

I'm currently looking at using Colony outside of the Tessel ecosystem and I'm unable to output working Lua code.

For example, if we execute the following JavaScript in Node...

var fs = require('fs'),
    compiler = require('colony-compiler'),
    source = "function sum(a,b){return a+b;} module.exports = {sum:sum};";

fs.writeFile ('output.lua', compiler.colonize(source).source);

... it outputs the following Lua in output.lua (module wrapping omitted)...

local sum = sum;
sum = (function () local sum = nil; sum = function (this, a, b)
--[[18]] if true then return ((a)+(b)); end
end; sum.name = "sum"; return sum; end)();
--[[0]] 
--[[31]] module.exports = _obj({
  ["sum"]=sum
});

This fails to run in Lua 5.1, 5.2 and LuaJIT because it tries to add a name property to a function. It produces the following error:

lua: ./output.lua:10: attempt to index local 'sum' (a function value)

Is this expected behaviour? Am I using different versions of Lua than you? Do you have a custom build of Lua where adding properties to functions is allowed?

kevinmehall commented 10 years ago

The init code in the Tessel runtime modifies the Function metatable to simulate JavaScript semantics where everything is an object. Functions get a hidden table that holds properties that appear to be on the function.

https://github.com/tessel/runtime/blob/master/src/colony/lua/colony-init.lua#L280

You'll need the Lua in that directory to get a functioning JS environment.

kevinmehall commented 10 years ago

(I've edited the title for the actionable issue on this repository)