Skip to content

Commit

Permalink
Allow overriding virtual functions per instance.
Browse files Browse the repository at this point in the history
This changes the way virtual function overrides are fetched so that it
is possible to override them directly on an instance, rather than
creating a derived class in Lua:

  x = X()
  x.virtual_function = function(self) ... end
  • Loading branch information
Daniel Wallin committed Dec 15, 2009
1 parent 132d443 commit 4b52671
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
8 changes: 3 additions & 5 deletions src/wrapper_base.cpp
Expand Up @@ -34,12 +34,10 @@ namespace luabind { namespace detail
LUABIND_API void do_call_member_selection(lua_State* L, char const* name)
{
object_rep* obj = static_cast<object_rep*>(lua_touserdata(L, -1));
lua_pop(L, 1); // pop self

obj->crep()->get_table(L); // push the crep table
lua_pushstring(L, name);
lua_gettable(L, -2);
lua_remove(L, -2); // remove the crep table
lua_pushstring(L, name);
lua_gettable(L, -2);
lua_replace(L, -2);

if (!is_luabind_function(L, -1))
return;
Expand Down
8 changes: 8 additions & 0 deletions test/test_abstract_base.cpp
Expand Up @@ -110,5 +110,13 @@ void test_main(lua_State* L)
"y:hello()\n");

DOSTRING(L, "call_hello(y)\n");

DOSTRING(L,
"x = abstract()\n"
"x.hello = function(self) return 'hello from instance' end\n"
"print(x.hello)\n"
"assert(x:hello() == 'hello from instance')\n"
"assert(call_hello(x) == 'hello from instance')\n"
);
}

0 comments on commit 4b52671

Please sign in to comment.