lua-users home
lua-l archive

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

A limited proposal:

For userdata, and userdata only, OP_SELF would be defined as looking for a
__getmethod metatable entry before trying __index. (I chose the name
__getmethod rather than __methcall since it doesn't actually do the call.)

Put this together with protection for userdata metatables and you have a way
to let userdata avoid a type-test on the first parameter to a method call.

To avoid disabling other features, this would need to be combined with: obj, msg, ... )

    methods.pcall( obj, msg, ... )

    methods.has( obj, msg )

    methods.bind( ob, msg )

These would allow the method function to remain safe from escaping. All that
is really needed is bind, but it is also the most expensive.

All of these functions should also work for methods implemented in the
classical form.

The C API could probably also use:

    lua_getmethod( L, tableIndex )

This would expect the method name to be on the top of the stack and would
leave the method or nil on the top of the stack.


P.S. Is there a particular rationale in the Lua C API for which things
return a boolean and which things simply leave nil on top of the stack?
lua_gettable leaves nil. lua_getmetatable returns a boolean.