lua-users home
lua-l archive

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

Hi Robert,

The first table is the table of methods, then luaL_newmetatable creates another table that is the metatable and puts it in the Lua Registry.
The end result is something like:

Lua Registry = {
  Account = {
    __metatable = <the method table>
    __index     = <the method table>
    __gc        = <the C function LuaAccount::gc_account>

<the method table> = {
  deposit  = <the C function LuaAccount::deposit>
  withdraw = <the C function LuaAccount::withdraw>
  balance  = <the C function LuaAccount::balance>

It's okay to drop the tables off the stack once the structure is built because they are rooted in the Registry. Some of the later examples also add the method table to the globals so that you can add methods written in a Lua script.

The __metatable field is returned by getmetatable in Lua scripts.
It isn't necessary.  The __index field is the important one.
See and the associated thread.

- Peter

Robert Kücken wrote:
I have a few questions about the sample-code Simple Cpp Binding.
I don't understand the register-function.
First I create a new table in the stack, then I turn it into a metatable,
But why I assign to __metatable the value of methodtable?
And why I drop after this the metatable? First I create it and then I delete
Hm, that's all, I hope, anyone can answer the questions.
thank you

robert kuecken