将 C 指针压入堆栈,得到 table
Pushing C pointer to stack, getting table
我在 C 代码中定义了两个函数:
static int luaMp4_load(lua_State *L){
LPP_Mp4 *ret = LPP_Mp4Load(luaL_checkstring(L, 1));
*pushMp4(L) = ret;
return(1);
}
static int luaMp4_play(lua_State *L){
LPP_Mp4Play(*toMp4(L, 1), luaL_checknumber(L, 2));
return 0;
}
在Lua中一一调用:
Mp4.load(movie)
Mp4:play(60)
函数 pushMp4
和 toMp4
是
LPP_Mp4** toMp4 (lua_State *L, int index){
LPP_Mp4** handle = (LPP_Mp4**)lua_touserdata(L, index);
if (handle == NULL) luaL_typerror(L, index, "Mp4");
return handle;
}
LPP_Mp4** pushMp4(lua_State *L) {
LPP_Mp4** newvalue = (LPP_Mp4**)lua_newuserdata(L, sizeof(LPP_Mp4*));
lua_getfield(L, LUA_REGISTRYINDEX, "Mp4");
lua_setmetatable(L, -2);
return newvalue;
}
问题是,我在 luaMp4_play
中得到一个 NULL
句柄,它表示堆栈第一个索引中的对象是 table
(而 Mp4
预期) - 在 luaL_typerror(lua_State *L, int narg, const char *tname)
:
内
const char *msg = lua_pushfstring(L, "%s expected, got %s",
tname, lua_typename(L, lua_type(L,(narg))));
我怎样才能摆脱它?
编辑:
LPP_Mp4
结构:
typedef struct {
struct mp4_read_struct reader;
Mp4AvcNalStruct nal;
struct mp4_video_read_output_struct v_packet;
Mp4AvcDecoderStruct *avc;
Mp4AvcCscStruct *csc;
} LPP_Mp4;
lua_touserdata
是一个 Lua API 库函数:
LUA_API void *lua_touserdata (lua_State *L, int idx) {
StkId o = index2adr(L, idx);
switch (ttype(o)) {
case LUA_TUSERDATA: return (rawuvalue(o) + 1);
case LUA_TLIGHTUSERDATA: return pvalue(o);
default: return NULL;
}
}
我对 C 代码中的 return(1);
感到困惑,并且毫不怀疑 Mp4:load()
returns 任何值
感谢 Etan Reisner 指向它,与:
一起工作
mv = Mp4.load(movie)
mv:play(60)
我在 C 代码中定义了两个函数:
static int luaMp4_load(lua_State *L){
LPP_Mp4 *ret = LPP_Mp4Load(luaL_checkstring(L, 1));
*pushMp4(L) = ret;
return(1);
}
static int luaMp4_play(lua_State *L){
LPP_Mp4Play(*toMp4(L, 1), luaL_checknumber(L, 2));
return 0;
}
在Lua中一一调用:
Mp4.load(movie)
Mp4:play(60)
函数 pushMp4
和 toMp4
是
LPP_Mp4** toMp4 (lua_State *L, int index){
LPP_Mp4** handle = (LPP_Mp4**)lua_touserdata(L, index);
if (handle == NULL) luaL_typerror(L, index, "Mp4");
return handle;
}
LPP_Mp4** pushMp4(lua_State *L) {
LPP_Mp4** newvalue = (LPP_Mp4**)lua_newuserdata(L, sizeof(LPP_Mp4*));
lua_getfield(L, LUA_REGISTRYINDEX, "Mp4");
lua_setmetatable(L, -2);
return newvalue;
}
问题是,我在 luaMp4_play
中得到一个 NULL
句柄,它表示堆栈第一个索引中的对象是 table
(而 Mp4
预期) - 在 luaL_typerror(lua_State *L, int narg, const char *tname)
:
const char *msg = lua_pushfstring(L, "%s expected, got %s",
tname, lua_typename(L, lua_type(L,(narg))));
我怎样才能摆脱它?
编辑:
LPP_Mp4
结构:
typedef struct {
struct mp4_read_struct reader;
Mp4AvcNalStruct nal;
struct mp4_video_read_output_struct v_packet;
Mp4AvcDecoderStruct *avc;
Mp4AvcCscStruct *csc;
} LPP_Mp4;
lua_touserdata
是一个 Lua API 库函数:
LUA_API void *lua_touserdata (lua_State *L, int idx) {
StkId o = index2adr(L, idx);
switch (ttype(o)) {
case LUA_TUSERDATA: return (rawuvalue(o) + 1);
case LUA_TLIGHTUSERDATA: return pvalue(o);
default: return NULL;
}
}
我对 C 代码中的 return(1);
感到困惑,并且毫不怀疑 Mp4:load()
returns 任何值
感谢 Etan Reisner 指向它,与:
mv = Mp4.load(movie)
mv:play(60)