簡體   English   中英

從Lua在C中傳遞的訪問表,而不復制值

[英]Access Table passed from Lua in C without Copying the values

我想將表從Lua傳遞到C,然后訪問C中的所有值,而不將值從Lua地址空間復制到C堆棧。 有辦法嗎? 我想最小化值的副本。

我嘗試使用gettable(),但在那種情況下,該值被復制到堆棧的頂部。 因此,正在生成一個副本。 我不要這個 還有其他辦法嗎?

這是我的C代碼:

#include <lua.h>                               /* Always include this */
#include <lauxlib.h>                           /* Always include this */
#include <lualib.h>                            /* Always include this */
#include <malloc.h>

#define EXCEPTION_IS_NUMBER (-2)    //Passed a custom error no. to be returned in 
                                    //case of error
#define SUCCESS (0)

static int iquicksort(lua_State *L) {
    int k,len=0;
    len=lua_tointeger(L,-2);        //-2 specifies second element from top of stack.
                                    //So I have passed 2 elements from Lua to C,  first
                                    //is size of table and second table. So when they 
                                    //are pushed to stack, the size is second element
                                    //from top.So here I am storing it in variable len.
    int *q;
    int *p=(int *)malloc(len*sizeof(int));
    q=p;
    for(k=1;k<=len;k++)
    {
            lua_pushinteger(L,k);    //if I want to access a[2], where a is my table 
                                     //and 2 is the index, then '2' needs to be at top
                                     //of the stack and I need to pass the location of
                                     //'a' in stack as second argument to gettable().
                                     //So here Address of table was at top, I pushed 
                                     //the index on top, now address is second element
                                     //from top. So I passed it as '-2' in gettable
                                     //below. What gettable() does is that it fetches 
                                     //and copies that value at stack top. So I can 
                                     //use it from there.
            lua_gettable(L,-2);
            if(lua_isnumber(L,-1))   //Checking top value replaced by fxn is number...
            {
                    *p++=lua_tointeger(L,-1);   //Storing the values in array
            }
            else
            {
                    lua_pushinteger(L,EXCEPTION_IS_NUMBER);
                    return 1;
            }
            lua_pop(L,1);
    }
    p=q;
    sort(p,0,len-1);
    for(k=1;k<=len;k++)   //This fxn changes the value at prescribed location of table.
                          //here I am changing the values at Table's location...
                          //i.e. storing the sorted values in table.....
    {
            lua_pushinteger(L,k);
            lua_pushinteger(L,*p++);
            lua_settable(L,-3);
    }
    lua_pushinteger(L,SUCCESS);
    return 1;
}

//Simple quicksort of values.....
void sort(int *arr, int left,int right){
    int i = left, j = right;
    int tmp;
    int pivot = arr[(left + right) / 2];

  /* partition */
    while (i <= j) {
            while (arr[i] < pivot)
                    i++;
            while (arr[j] > pivot)
                    j--;
            if (i <= j) {
                    tmp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = tmp;
                    i++;
                    j--;
            }
    };

  /* recursion */
    if (left < j)
            sort(arr, left, j);
    if (i < right)
            sort(arr, i, right);
}

int luaopen_power(lua_State *L){
    lua_register(L,"quicksort",iquicksort);
    return 0;
}

我通過使用以下命令編譯該程序來生成共享庫:

gcc -Wall -shared -fPIC -o power.so -I/usr/local/include/lua5.1 -llua5.1 quicksort.c

這是用於調用此代碼的Lua代碼:-

require("power")
x={5,4,6,5,3,2,3,9}
print("Before quicksort call....")
t=quicksort(#x,x)
if t==0 then
        for i,v in ipairs(x) do print(i,v) end
else
        print(string.format("%s %d","Error occurred. Errorcode is:: ",t))
end

謝謝

Lua的C API僅復制低級C類型,例如數字和布爾值。 對於所有其他字符串(包括字符串),它使用指向內部Lua數據的指針。

我不確定gettable()將值復制到Lua堆棧中,我認為它會復制值的引用或指針...(尤其是當該值是表本身時,不會復制該表的內容) 。

考慮到Lua可能會進行魔術處理,我相信您的回答是否定的。

Lua是免費軟件,您可以下載它並查看源代碼 對於lua-5.2.0-rc4lua_gettable函數位於文件src/lapi.c

LUA_API void lua_gettable (lua_State *L, int idx) {
  StkId t;
  lua_lock(L);
  t = index2addr(L, idx);
  api_checkvalidindex(L, t);
  luaV_gettable(L, t, L->top - 1, L->top - 1);
  lua_unlock(L);
}

因此實際工作是通過src/lvm.c文件中的luaV_gettable完成的

void luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val) {
  int loop;
  for (loop = 0; loop < MAXTAGLOOP; loop++) {
    const TValue *tm;
    if (ttistable(t)) {  /* `t' is a table? */
      Table *h = hvalue(t);
      const TValue *res = luaH_get(h, key); /* do a primitive get */
      if (!ttisnil(res) ||  /* result is not nil? */
          (tm = fasttm(L, h->metatable, TM_INDEX)) == NULL) { /* or no TM? */
        setobj2s(L, val, res);
        return;
      }
      /* else will try the tag method */
    }
    else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_INDEX)))
      luaG_typeerror(L, t, "index");
    if (ttisfunction(tm)) {
      callTM(L, tm, t, key, val, 1);
      return;
    }
    t = tm;  /* else repeat with 'tm' */
  }
  luaG_runerror(L, "loop in gettable");
}

所以我認為答案是否定的。 但是,您可以修補或增強代碼。 我不明白為什么這個問題困擾您。 除非發生魔術(魔術(即metatable是Lua語義的重要組成部分)),否則僅復制(非常快速)簡單數據。 匯總數據內容未復制。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM