LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-07-2011, 12:54 PM   #1
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
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.
 
Old 08-07-2011, 03:20 PM   #2
corp769
Guru
 
Registered: Apr 2005
Posts: 5,807

Rep: Reputation: 996Reputation: 996Reputation: 996Reputation: 996Reputation: 996Reputation: 996Reputation: 996Reputation: 996
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
 
Old 08-10-2011, 03:54 PM   #3
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Original Poster
Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
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 View Post
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?

Last edited by MTK358; 08-10-2011 at 03:56 PM.
 
Old 08-13-2011, 01:53 PM   #4
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Original Poster
Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
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.
 
Old 08-14-2011, 02:06 PM   #5
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Original Poster
Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
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?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Awesome[WM] related LUA question implosions Linux - Desktop 0 08-03-2011 05:42 AM
[SOLVED] Undefined reference error when using Lua API MTK358 Programming 8 07-20-2011 07:14 PM
C/C++ embedding Lua with Gtk question PatrickNew Programming 1 09-01-2008 06:05 AM
quick question: API mismatch MikeOfAustin Linux - Software 1 05-21-2007 02:30 PM
Stupid win32 API question.... pld Programming 12 04-02-2004 03:46 PM


All times are GMT -5. The time now is 01:13 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration