Reputation: 12018
I'd like to be able to make a program for ComputerCraft using MoonScript, but due to the way that CC is sandboxed to prevent security issues on Minecraft servers and such, I can't require moonscript directly and run moonscript code from there. I have to convert my moonscript code to lua.
This is problematic, however, due to the fact that the class implementation for moonscript is very big, and not very filesize-conservative. When I type "class Bacon", it outputs this for lua:
local Bacon
do
local _parent_0 = nil
local _base_0 = { }
_base_0.__index = _base_0
if _parent_0 then
setmetatable(_base_0, _parent_0.__base)
end
local _class_0 = setmetatable({
__init = function(self, ...)
if _parent_0 then
return _parent_0.__init(self, ...)
end
end,
__base = _base_0,
__name = "Bacon",
__parent = _parent_0
}, {
__index = function(cls, name)
local val = rawget(_base_0, name)
if val == nil and _parent_0 then
return _parent_0[name]
else
return val
end
end,
__call = function(cls, ...)
local _self_0 = setmetatable({}, _base_0)
cls.__init(_self_0, ...)
return _self_0
end
})
_base_0.__class = _class_0
if _parent_0 and _parent_0.__inherited then
_parent_0.__inherited(_parent_0, _class_0)
end
Bacon = _class_0
return _class_0
end
And this is for every class implementation, which is kind of ridiculous. Is there any way I can shorten this in my moonscript code?
Upvotes: 1
Views: 1915
Reputation: 29573
There is no way that you can shorten this in your moonscript code. But you might be able to post-process the output manually rather trivially to refactor common portions. Is it worth doing? Consider this:
If you really want use the moonscript output, I think your only option is to refactor it manually. The code you posted can be put in a function and only two lines need to be changed to parametrize for class name.
Note that there several advantages to leaving the Lua code duplication generated by moonscript in your Lua files, at least for oft-used operations (class definition is probably not one of them, but moonscript does way more than that):
Upvotes: 1
Reputation: 1852
The amount of generated code has been cleaned up quite a bit in the latest version, 0.2.4: http://leafo.net/posts/moonscript_v024.html#code_generation_changes
A class now minimally looks like this
local Hello
do
local _base_0 = { }
_base_0.__index = _base_0
local _class_0 = setmetatable({
__init = function() end,
__base = _base_0,
__name = "Hello"
}, {
__index = _base_0,
__call = function(cls, ...)
local _self_0 = setmetatable({}, _base_0)
cls.__init(_self_0, ...)
return _self_0
end
})
_base_0.__class = _class_0
Hello = _class_0
end
Upvotes: 7
Reputation: 4311
Just looking at the code I can remove some dead paths due to _parent_0
being nil...
local Bacon
do
local _base_0 = { }
_base_0.__index = _base_0
local _class_0 = setmetatable({
__init = function(self, ...)
end,
__base = _base_0,
__name = "Bacon",
}, {
__index = function(cls, name)
return rawget(_base_0, name)
end,
__call = function(cls, ...)
local _self_0 = setmetatable({}, _base_0)
cls.__init(_self_0, ...)
return _self_0
end
})
_base_0.__class = _class_0
Bacon = _class_0
return _class_0
end
You can find a static analyser to do this for you.
Otherwise if it's purely code size (in bytes) that concerns you, then use a compressor (e.g. Squish)
Upvotes: 1