建议:lua bind对string二进制数据参数做改进

现在最新的3.0 rc0中lua绑定对string类型参数只能支持到c风格字符串(即0结尾的字符串)
当在项目中加入中加入自定义的参数时,可能需要用string传递一些二进制数据
建议cocos库中tolua代码做如下改进:

tolua_to.c 增加
TOLUA_API const char* tolua_tolstring(lua_State* L, int narg, const char* def, size_t* len)
{
if(lua_gettop(L)<abs(narg))
{
len = strlen(def);
return def;
}
else
{
return lua_tolstring(L, narg, len);
}
}

TOLUA_API const char* tolua_tofieldlstring
(lua_State* L, int lo, int index, const char* def, size_t* len)
{
const char* v;
lua_pushnumber(L,index);
lua_gettable(L,lo);
v = lua_isnil(L,-1) ? def : lua_tolstring(L,-1,len);
lua_pop(L,1);
return v;
}

tolua_push.c 增加:
TOLUA_API void tolua_pushlstring (lua_State* L, const char* value, size_t len)
{
if (value == NULL)
lua_pushnil(L);
else
lua_pushlstring(L, value, len);
}

tolua++.h中 做修改:
#ifdef __cplusplus
static inline const char* tolua_tocppstring (lua_State* L, int narg, const char* def, size_t* len = NULL) {
const char* s;
if(len == NULL)
s = tolua_tostring(L, narg, def);
else
s = tolua_tolstring(L, narg, def, len);
return s?s:def;
};

static inline const char* tolua_tofieldcppstring (lua_State* L, int lo, int index, const char* def, size_t* len = NULL) {
const char* s;
if(len == NULL)
s = tolua_tofieldstring(L, lo, index, def);
else
s = tolua_tofieldlstring(L, lo, index, def, len);
return s?s:"";
};

#else
#define tolua_tocppstring tolua_tostring
#define tolua_tofieldcppstring tolua_tofieldstring
#endif

在自定义的c++代码中,如果用到string类型参数传递二进制数据
就使用带长度字段的tolua_tocppstring
cocos2dx中原有的代码不受影响

感谢您的意见,我会update到反馈表,并提供给引擎组做参考。谢谢!

请问这个特性修改过了吗?