lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Mon, Aug 24, 2009 at 1:14 PM, François Perrad wrote:
> lua-Coat is a Lua 5.1 port of Coat (http://www.sukria.net/perl/coat/), ...
> Comment, bug report, patch are welcome.

So if Moose is the ultimate class library for Perl, and Coat is a
quasi-subset Moose, and lua-Coat is a port of Coat to Lua, then does
that imply that lua-Coat is a quasi-subset of the ultimate class
library for Lua?

We can try it on the benchmark [3]

  -- added code
  function makeob5()
    require "Coat"
    class "ob5"
    has.data = {is='number',default=0}
    function method:test() self:data(self:data() + 1) end
    return _M()
  end
  addbenchmark("Object using lua-Coat", "ob:test()", makeob5())

Results:

  $ lua bench.lua            # using count = 10000000
  2.969   Standard (solid)
  3.406   Standard (metatable)
  3.64    Object using closures (PiL 16.4)
  2.25    Object using closures (noself)
  16.735  Object using lua-Coat
  1.469   Direct Access
  0.484   Local Variable

> basic_type = type
> local basic_type = basic_type
> local function object_type (obj)
>     local t = basic_type(obj)
>     if t == 'table' then
>         pcall(function () t = obj._CLASS or t end)
>     end
>     return t
> end
> _G.type = object_type

The above code implies that type {_CLASS='x'} == 'x', which is
inconsistent with the Lua specification [1].  This might not seem
important, but consider the case of some code that operates on a table
whose key names are generated from user input.  The above would
introduce subtle edge cases (or security holes).  The above also
implies that type(t) will now have the overhead of a pcall when t is a
table.  I recommend against redefining type().

> function before (class, name, func) .....
>     class[name] = function (...)
>         local result = func(...)
>         super(...)
>         return result
>     end

To handle multiple return values, you can use the CPS style [2].
However, I question whether before and after modifiers should alter
return values.  It appears Coat (in $modified_method_code) only does
so for around modifiers.

[1] http://www.lua.org/manual/5.1/manual.html#pdf-type
[2] http://lua-users.org/wiki/VarargTheSecondClassCitizen
[3] http://lua-users.org/wiki/ObjectBenchmarkTests