在C中使用从Lua传递的访问表而无需复制值
2011-12-2 6:49:36
收藏:0
阅读:109
评论:2
我想将一个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
谢谢
原文链接 https://stackoverflow.com/questions/8352400
点赞
stackoverflow用户107090
Lua的C API只复制低级别的C类型,如数字和布尔值。对于所有其他类型,包括字符串,它都使用指向内部Lua数据的指针。
2011-12-02 11:13:11
评论区的留言会收到邮件通知哦~
推荐文章
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?
- addEventListener 返回 nil Lua
- Lua中获取用户配置主目录的跨平台方法
- 如何编写 Lua 模式将字符串(嵌套数组)转换为真正的数组?
我不确定
gettable()
是否将值复制到 Lua 栈中,我认为它复制的是该值的引用或指针……(特别是当该值本身是一个表时,该表的内容没有被复制)。鉴于 Lua 可能会执行魔法处理,我认为你的答案是否定的。
由于 Lua 是 自由软件,你可以下载它并且查看源代码。在
lua-5.2.0-rc4
中,lua_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"); }
我认为答案是否定的。但是,你可以修补或增强代码。我不明白为什么这个问题会困扰你。只有简单的数据被复制(非常快),除非魔法发生(而魔法,即元表,是 Lua 语义的重要组成部分);聚合数据内容不会被复制。