LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Lua C API Question (https://www.linuxquestions.org/questions/programming-9/lua-c-api-question-896061/)

MTK358 08-07-2011 12:54 PM

Lua C API Question
 
Quote:

Originally Posted by http://www.lua.org/pil/28.3.html
Code:

static const struct luaL_reg arraylib_f [] = {
      {"new", newarray},
      {NULL, NULL}
    };
   
    static const struct luaL_reg arraylib_m [] = {
      {"set", setarray},
      {"get", getarray},
      {"size", getsize},
      {NULL, NULL}
    };

The new version of luaopen_array, the function that opens the library, has to create the metatable, to assign it to its own __index field, to register all methods there, and to create and fill the array table:

Code:

int luaopen_array (lua_State *L) {
      luaL_newmetatable(L, "LuaBook.array");
   
      lua_pushstring(L, "__index");
      lua_pushvalue(L, -2);  /* pushes the metatable */
      lua_settable(L, -3);  /* metatable.__index = metatable */
   
      luaL_openlib(L, NULL, arraylib_m, 0);
   
      luaL_openlib(L, "array", arraylib_f, 0);
      return 1;
    }


I don't understand, what's the point of the "metatable.__index = metatable" part? It doesn't make any sense to me.

corp769 08-07-2011 03:20 PM

That part is just saying that it is taking metatable.__index and setting the value for that table. The = metatable just means that it is using the pointer of L; lua_settable(L, -3) is the actual code, whereas metatable.__index is just saying what is happening. Notice the /* */ around it... Sorry if it doesn't make sense, but that is the absolute best I can do to explain it at a lower level without getting into the pointer structure for L and how it is being set.

Cheers,

Josh

MTK358 08-10-2011 03:54 PM

I didn't know that anyone replied intil I tried looking for this thread again just in case. The list of unread threads on "My LQ" is so glitchy...

Quote:

Originally Posted by corp769 (Post 4436175)
That part is just saying that it is taking metatable.__index and setting the value for that table. The = metatable just means that it is using the pointer of L; lua_settable(L, -3) is the actual code, whereas metatable.__index is just saying what is happening. Notice the /* */ around it... Sorry if it doesn't make sense, but that is the absolute best I can do to explain it at a lower level without getting into the pointer structure for L and how it is being set.

I understand what it does. I don't understand why wou would want to do it.

Why would you want to set the metatable's __index field to the metatable itself?

MTK358 08-13-2011 01:53 PM

Code:

Example = {}

-- if I comment the following line out, it fails to get methods using the ":"
-- syntax. Why is that?
Example.__index = Example

function Example.new(value)
    self = {}
    setmetatable(self, Example)
    self:setvalue(value)
    return self;
end

function Example:setvalue(value)
    self.value = value
end

function Example:getvalue(value)
    return self.value
end

i = Example.new(5)
print(i:getvalue())

See the comment in the above code.

MTK358 08-14-2011 02:06 PM

It says that the __index metamethod (which can be a table instead of a function) is used when you try to get a table's member that doesn't exist. In that case, I would expect that setting the __index field of a table's metatable to the table's metatable would make it so that you can call methods using the "." operator instead of ":". But it doean't work that way. What am I missing here?


All times are GMT -5. The time now is 10:51 PM.