내가 가지고이 사람의 문제 : 대신 weren 히 내 다른 방법의 내 유저 데이터의 메타 테이블의 __index을 설정할 때, 그것은 항상 게터라는 것을 특징으로 Lua userdata array access and methods루아 유저 데이터 : 동시 배열 액세스 및 방법이 없습니다
, 메타 이벤트를 선언했다. 위의 링크에 대한 해결책은 루아이며, 나는 비효율적 인 C 구현을 시도했지만 관계없이 새로운 메소드가 더 이상 인수를 취할 수 없다는 새로운 문제가 발생하고 다음과 같은 오류가 발생합니다.
attempt to call method 'asTable' (a table value)
//Methods, many of which are overridden Lua meta-events (with the underscores)
static const struct luaL_reg vallib_m [] = {
{"asTable", PushLuaTable}, //these functions are not called
{"asCopy", CopyLuaVal},
{"__newindex", SetLuaVal},
{"__index", GetLuaVal},
{"__tostring", ValToString},
{"__gc", GarbageCollectVal},
{"__metatable", HideMetaTable},
{NULL, NULL}
};
//Static library functions
static const struct luaL_reg vallib_f [] = {
{"specialprint", PrintVals},
{NULL, NULL}
};
int luaopen_custom(lua_State *L)
{
luaL_newmetatable(L, "custom.Value");
lua_pushstring(L, "__index");
lua_pushvalue(L, -2); /* pushes the metatable */
lua_settable(L, -3); /* metatable.__index = metatable */
luaL_register(L, NULL, vallib_m);
luaL_register(L, "special", vallib_f);
return 0;
}
이 루아 문에
: 나는 모든 것을 설정하는 방법
print_r(c:asTable())
입니다 0
그런 다음 기본적으로 (__index를 통해) 호출되는 getter에서 먼저 호출 할 다른 이벤트를 확인하고 다음과 같이 컨트롤을 전달합니다. 스택의 함수 이름을 포함하는 인수를 제거합니다.
//TODO: this is a tentative fix, I would rather do this with metatables
//checking for methods
if (lua_isstring(L, 2))
{
field = luaL_checkstring(L, 2);
if (unlikely(!field))
{
reporter->Warning("Fail in getter -- bad string as method attempt");
return LUA_FAILURE;
}
if (strcmp(field, "asTable") == 0)
{
lua_remove(L, 2); //delete string "asTable"
return PushLuaTable(L);
}
else if (strcmp(field, "asCopy") == 0)
{
lua_remove(L, 2); //delete string "asCopy"
return CopyLuaVal(L);
}
//... other methods.
else
{
//Insert string back into stack??
}
}
그것은 상관없이 많은 인수가 전달되는 방법의 함수로 내 방법을 치료하고, 심지어 어떤 괄호 또는 콜론이있는 경우 오류가 발생하지 않습니다. (c.asTable
에 의해 접근 할 수 있습니다.이 메소드는 아무런 인자도 필요하지 않지만 메소드를 추가 할 계획입니다. 내 C getter를 통해 이러한 함수를 호출하고 메타 테이블을 사용하여이를 해결하십시오. 가능한 경우 C API를 사용하여 예제를 제공하십시오. 이미 Lua에 StackOverflow 솔루션이 있지만 C로 변환 할 수 없습니다. .
는 [이 않음]의 두 번째 부분을 참조 (http://stackoverflow.com/questions/29957701/how-to-store-a-value-type-in-a-userdata/29967658#29967658). 도움이 – siffiejoe
, 감사 – Leo
약간 복잡하지만 - 그래서 내 경우에는 내가 부를 것이다 moon_propindex (L, vallib_m, setLuaVal?,?)? 좀 더 일반적인 상황에서 정교하게 수 그리고 해결책로 표시됩니다 경우가 좋은 것 – Leo