Note that there are some explanatory texts on larger screens.

plurals
  1. POAccess Table passed from Lua in C without Copying the values
    text
    copied!<p>I want to pass a table from Lua to C and then access all the values in C without copying the values from Lua address space to C stack. Is there a way of doing this?? I want to minimize the copy of values.</p> <p>I tried using gettable() but in that case, the value is copied onto top of stack. So a copy is getting generated. I don't want this. Is there any other way??</p> <p>Here is my C code:-</p> <pre><code>#include &lt;lua.h&gt; /* Always include this */ #include &lt;lauxlib.h&gt; /* Always include this */ #include &lt;lualib.h&gt; /* Always include this */ #include &lt;malloc.h&gt; #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&lt;=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&lt;=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 &lt;= j) { while (arr[i] &lt; pivot) i++; while (arr[j] &gt; pivot) j--; if (i &lt;= j) { tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; i++; j--; } }; /* recursion */ if (left &lt; j) sort(arr, left, j); if (i &lt; right) sort(arr, i, right); } int luaopen_power(lua_State *L){ lua_register(L,"quicksort",iquicksort); return 0; } </code></pre> <p>I generated a shared library by compiling this program using the following command:-</p> <pre><code>gcc -Wall -shared -fPIC -o power.so -I/usr/local/include/lua5.1 -llua5.1 quicksort.c </code></pre> <p>Here is the Lua code for calling this:-</p> <pre><code>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 </code></pre> <p>Thanks</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload