cpgf如何实现lua script binding的?

Lib:

https://github.com/cpgf/cpgf/tree/master

代码

以下是operator的实现函数

int UserData_operator(lua_State * L)
{
ENTER_LUA() GOperatorGlueDataPointer glueData = static_cast<GGlueDataWrapper *>(lua_touserdata(L, lua_upvalueindex()))->getAs<GOperatorGlueData>(); return invokeOperator(glueData->getBindingContext(), glueData->getObjectData(), glueData->getMetaClass(), glueData->getOp()); LEAVE_LUA(L, return )
}

可见cpgf 设置 一个GGlueDataWrapper *指针作为upvalue,从而可以方便的调用相关实现

而注册函数如下

由于对lua api不很熟悉,这段代码还没有完全搞懂

有时间仔细研究下

void helperBindOperator(const GContextPointer & context, const GObjectGlueDataPointer & objectData, IMetaClass * metaClass, GMetaOpType op)
{
lua_State * L = getLuaState(context); for(size_t i = 0; i < sizeof(metaOpTypes) / sizeof(metaOpTypes[0]); ++i) {
if(metaOpTypes[i] == op) {
GOperatorGlueDataPointer operatorData(context->newOperatorGlueData(objectData, metaClass, op));
GLuaRefUserData * glueUserData = operatorData->getUserDataAs<GLuaRefUserData>();
if(glueUserData != nullptr) {
glueUserData->get();
if(! lua_isnil(L, -1)) {
return;
}
lua_pop(L, 1);
} lua_pushstring(L, luaOperators[i]);
void * userData = lua_newuserdata(L, getGlueDataWrapperSize<GOperatorGlueData>());
newGlueDataWrapper(userData, operatorData); lua_newtable(L);
setMetaTableSignature(L);
setMetaTableGC(L);
lua_setmetatable(L, -2); lua_pushcclosure(L, &UserData_operator, 1);
lua_rawset(L, -3); glueUserData = new GLuaRefUserData(L, false);
operatorData->setUserData(glueUserData);
glueUserData->retainAndPop(); glueUserData->get(); return;
}
}
}

  

上一篇:win10安装MySQL免安装版


下一篇:Lua与javascript的差异