zoukankan      html  css  js  c++  java
  • Lua.LearningLua.5-document-for-luaL_findtable-function

    Learning Lua: 5 - Document for luaL_findtable() function

    文档中没有找到luaL_findtable()函数的说明,这里尝试补充。 

    1 LUALIB_API const char *luaL_findtable (lua_State *L, int idx,
    2                                        const char *fname, int szhint);

    @brief

    luaL_findtable()在由参数idx标识的target-table中查找名字为fname的table。 fname支持"tableA.tableB.tableC"这种

    级联的方式来命名要查找的table,其中在target-table中以"tableA"为key的field的值需要是table类型,否则会返回"tableA"。

    @parameters

    L: lua_State

    idx: target-table在lua_State对象L中的位置

    fname: 在target-table中所要查找的table的名字。支持"GrandfatherTableName.ParentTableName.TableName"这种形式。

    szhint: 如果查找的table不存在,则会新创建fname对应的table,szhint是创建新table时non-array elements的提示值。参见"lua_createtable()"。

    @return

    成功:返回NULL; L中的top位置(即: -1的位置)会保留找到的table。

    异常:返回fname中有问题的部分; L 会被复原为调用luaL_findtable()前的状态。 

    luaL_findtable()代码如下:

     1 LUALIB_API const char *luaL_findtable (lua_State *L, int idx,
     2                                        const char *fname, int szhint) {
     3   const char *e;
     4   lua_pushvalue(L, idx); // lua_pushvalue(): Pushes a copy of the element at the given valid index onto the stack. // A
     5   do {
     6     e = strchr(fname, '.');
     7     if (e == NULL) e = fname + strlen(fname);
     8     lua_pushlstring(L, fname, e - fname); // lua_pushlstring(): Pushes the string pointed to by s with size len onto the stack. // B
     9     lua_rawget(L, -2);    // lua_rawget(): Similar to lua_gettable, but does a raw access  // C
    10     if (lua_isnil(L, -1)) {  /* no such field? */
    11       lua_pop(L, 1);  /* remove this nil */
    12       lua_createtable(L, 0, (*e == '.' ? 1 : szhint)); /* new table for field */   // D
    13       lua_pushlstring(L, fname, e - fname);
    14       lua_pushvalue(L, -2);
    15       lua_settable(L, -4);  /* set new table into field */       // E
    16     }
    17     else if (!lua_istable(L, -1)) {  /* field has a non-table value? */
    18       lua_pop(L, 2);  /* remove table and value */
    19       return fname;  /* return problematic part of the name */
    20     }
    21     lua_remove(L, -2);  /* remove previous table */    // F
    22     fname = e + 1;
    23   } while (*e == '.');
    24   return NULL;
    25 }

    lua_gettable()

    1 void lua_gettable (lua_State *L, int index);

    "Pushes onto the stack the value t[k], where t is the value at the given valid index and k is the value at the top of the stack.

    This function pops the key from the stack (putting the resulting value in its place). As in Lua, this function may trigger a metamethod for the "index" event " Ref[1]

    lua_createtable()

    1 void lua_createtable (lua_State *L, int narr, int nrec);

    "Creates a new empty table and pushes it onto the stack. The new table has space pre-allocated for narr array elements and nrec non-array elements." Ref[1]

    lua_settable()

    1 void lua_settable (lua_State *L, int index);

    "Does the equivalent to t[k] = v, where t is the value at the given valid index, v is the value at the top of the stack, and k is the value just below the top.

    This function pops both the key and the value from the stack. As in Lua, this function may trigger a metamethod for the "newindex" event" Ref[1] 

    lua_remove()

    1 void lua_remove (lua_State *L, int index);

    "Removes the element at the given valid index, shifting down the elements above this index to fill the gap. Cannot be called with a pseudo-index, because a pseudo-index is not an actual stack position." Ref[1] 


    Reference 

    1. http://www.lua.org/manual/5.1/manual.html

  • 相关阅读:
    #pragma
    I/0概念介绍
    Android Eclipse 源码工程 调试
    SHELL四则运算和比较
    Android源码中添加 修改应用
    android ubuntu9.10 源码的编译 Eclipse工程 Contacts编译 应用加载
    【Wonder原创】LogMiner使用实践
    【转】Oracle入门教程,新手必读
    【Wonder原创】关于MSSQL通过DBLink访问Oracle问题
    【杂记】SQLServer
  • 原文地址:https://www.cnblogs.com/cwgk/p/4729665.html
Copyright © 2011-2022 走看看