summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
author“pengxuanzheng” <[email protected]>2022-06-24 09:01:28 +0000
committer“pengxuanzheng” <[email protected]>2022-06-24 09:07:56 +0000
commit626ea0318e338660c8e6dd5753f3a1a0c865be41 (patch)
tree7f4fe5a5d947cb374255accf3a112c687cccdbba /src
parentd229dcfa923d4ff7013c825fc4d2f2c4dbdb850a (diff)
✨ feat(TSG-11082): 运行脚本增加新的返回类型nilv1.1.0
Diffstat (limited to 'src')
-rw-r--r--src/tsg_lua_func.cpp196
-rw-r--r--src/tsg_lua_interface.h17
2 files changed, 150 insertions, 63 deletions
diff --git a/src/tsg_lua_func.cpp b/src/tsg_lua_func.cpp
index a24859b..4a95ab1 100644
--- a/src/tsg_lua_func.cpp
+++ b/src/tsg_lua_func.cpp
@@ -513,6 +513,7 @@ int tsg_lua_exec_file(tsg_lua_handle lua, const char *script, const char *in, si
int sharp = 0;
tsg_lua_clfactory_file_t lf;
jmp_buf *lua_exception;
+ int return_value = 0;
if (L == NULL)
{
@@ -537,6 +538,12 @@ int tsg_lua_exec_file(tsg_lua_handle lua, const char *script, const char *in, si
return ERR_IN_LEN_INVAILD;
}
+ if (out_type == NULL || *out_type < STRING || *out_type > BOOLEAN)
+ {
+ debuginfo("out_type is invailed", __FILE__, __LINE__);
+ return ERR_EXPECT_TYPE_IS_NIL;
+ }
+
lua_exception = (jmp_buf *)lua_getexdata(L);
lf.extraline = 0;
@@ -666,41 +673,53 @@ int tsg_lua_exec_file(tsg_lua_handle lua, const char *script, const char *in, si
return ERR_SCRIPT_EXEC_ERROR;
}
int num = lua_gettop(L);
- if (num < 2)
+ if (num == 0)
{
- char err_buf[255];
- sprintf((char *)err_buf,"num:%d", num);
- debuginfo(err_buf, __FILE__, __LINE__);
- return ERR_SCRIPT_RETURN_TOO_FEW;
+ //没有返回任何内容
+ debuginfo("script return nothing", __FILE__, __LINE__);
+ return ERR_RETURN_TYPE_NOT_MATCH_EXPECT;
+ }
+ else if (num == 2)
+ {
+ *out_len = lua_tonumber(L, -2);
+ if (*out_len < 1)
+ {
+ debuginfo("script out_len is 0", __FILE__, __LINE__);
+ return ERR_SCRIPT_RETURN_LEN_INVAILD;
+ }
}
- if (num > 2)
+ else if (num > 2)
{
char err_buf[255];
sprintf((char *)err_buf,"num:%d", num);
debuginfo(err_buf, __FILE__, __LINE__);
return ERR_SCRIPT_RETURN_TOO_MUCH;
}
- *out_len = lua_tonumber(L, -2);
- if (*out_len < 1)
- {
- debuginfo("script out_len is 0", __FILE__, __LINE__);
- return ERR_SCRIPT_RETURN_LEN_INVAILD;
- }
- switch(lua_type(L, -1))
+
+ size_t lua_ret_type = lua_type(L, -1);
+ size_t actual_out_type = NIL;
+
+ switch(lua_ret_type)
{
+ case LUA_TNIL:
+ debuginfo("script return nil", __FILE__, __LINE__);
+ actual_out_type = NIL;
+ *out_type = NIL;
+ return_value = ERR_RETUNR_NIL;
+ break;
case LUA_TSTRING:
memcpy(out, lua_tostring(L, -1), *out_len);
- *out_type = STRING;
+ actual_out_type = STRING;
break;
case LUA_TBOOLEAN:
out[0] = lua_toboolean(L, -1);
- //*out_len = 1;
- *out_type = BOOLEAN;
+ *out_len = 1;
+ actual_out_type = BOOLEAN;
break;
case LUA_TNUMBER:
*(long *)out = lua_tointeger(L, -1);
- //*out_len = 8;
- *out_type = INTEGER;
+ *out_len = 8;
+ actual_out_type = INTEGER;
break;
default:
char err_buf[255];
@@ -709,12 +728,20 @@ int tsg_lua_exec_file(tsg_lua_handle lua, const char *script, const char *in, si
*out_len = 0;
return ERR_SCRIPT_RETURN_TYPE_INVAILD;
}
+ if (actual_out_type != *out_type && actual_out_type != NIL)
+ {
+ char err_buf[255];
+ sprintf((char *)err_buf, "expect out_type is:%zu, actual out_type is:%zu", *out_type, actual_out_type);
+ debuginfo(err_buf, __FILE__, __LINE__);
+ *out_type = actual_out_type;
+ return_value = ERR_RETURN_TYPE_NOT_MATCH_EXPECT;
+ }
}else
{
return ERR_SCRIPT_EXEC_ERROR;
}
- return 0;
+ return return_value;
}
@@ -727,6 +754,7 @@ int tsg_lua_exec(tsg_lua_handle lua, const char *script, size_t script_len, cons
int sharp = 0;
tsg_lua_clfactory_buffer_t ls;
jmp_buf *lua_exception;
+ int return_value = 0;
memset(&ls, 0, sizeof(tsg_lua_clfactory_buffer_t));
@@ -753,6 +781,12 @@ int tsg_lua_exec(tsg_lua_handle lua, const char *script, size_t script_len, cons
return ERR_IN_LEN_INVAILD;
}
+ if (out_type == NULL || *out_type < STRING || *out_type > BOOLEAN)
+ {
+ debuginfo("out_type is invailed", __FILE__, __LINE__);
+ return ERR_EXPECT_TYPE_IS_NIL;
+ }
+
lua_exception = (jmp_buf *)lua_getexdata(L);
ls.s = script;
@@ -866,41 +900,53 @@ int tsg_lua_exec(tsg_lua_handle lua, const char *script, size_t script_len, cons
return ERR_SCRIPT_EXEC_ERROR;
}
int num = lua_gettop(L);
- if (num < 2)
+ if (num == 0)
{
- char err_buf[255];
- sprintf((char *)err_buf, "num:%d", num);
- debuginfo(err_buf, __FILE__, __LINE__);
- return ERR_SCRIPT_RETURN_TOO_FEW;
+ //没有返回任何内容
+ debuginfo("script return nothing", __FILE__, __LINE__);
+ return ERR_RETURN_TYPE_NOT_MATCH_EXPECT;
}
- if (num > 2)
+ else if (num == 2)
+ {
+ *out_len = lua_tonumber(L, -2);
+ if (*out_len < 1)
+ {
+ debuginfo("script out_len is 0", __FILE__, __LINE__);
+ return ERR_SCRIPT_RETURN_LEN_INVAILD;
+ }
+ }
+ else if (num > 2)
{
char err_buf[255];
sprintf((char *)err_buf, "num:%d", num);
debuginfo(err_buf, __FILE__, __LINE__);
return ERR_SCRIPT_RETURN_TOO_MUCH;
}
- *out_len = lua_tonumber(L, -2);
- if (*out_len < 1)
- {
- debuginfo("script out_len is 0", __FILE__, __LINE__);
- return ERR_SCRIPT_RETURN_LEN_INVAILD;
- }
- switch(lua_type(L, -1))
+
+ size_t lua_ret_type = lua_type(L, -1);
+ size_t actual_out_type = NIL;
+
+ switch(lua_ret_type)
{
+ case LUA_TNIL:
+ debuginfo("script return nil", __FILE__, __LINE__);
+ *out_type = NIL;
+ actual_out_type = NIL;
+ return_value = ERR_RETUNR_NIL;
+ break;
case LUA_TSTRING:
memcpy(out, lua_tostring(L, -1), *out_len);
- *out_type = STRING;
+ actual_out_type = STRING;
break;
case LUA_TBOOLEAN:
out[0] = lua_toboolean(L, -1);
- //*out_len = 1;
- *out_type = BOOLEAN;
+ *out_len = 1;
+ actual_out_type = BOOLEAN;
break;
case LUA_TNUMBER:
*(size_t *)out = lua_tointeger(L, -1);
- //*out_len = 8;
- *out_type = INTEGER;
+ *out_len = 8;
+ actual_out_type = INTEGER;
break;
default:
char err_buf[255];
@@ -909,12 +955,20 @@ int tsg_lua_exec(tsg_lua_handle lua, const char *script, size_t script_len, cons
*out_len = 0;
return ERR_SCRIPT_RETURN_TYPE_INVAILD;
}
+
+ if (actual_out_type != *out_type && actual_out_type != NIL)
+ {
+ char err_buf[255];
+ sprintf((char *)err_buf, "expect out_type is:%zu, actual out_type is:%zu ", *out_type, actual_out_type);
+ debuginfo(err_buf, __FILE__, __LINE__);
+ return_value = ERR_RETURN_TYPE_NOT_MATCH_EXPECT;
+ }
}else
{
return ERR_SCRIPT_EXEC_ERROR;
}
- return 0;
+ return return_value;
}
int tsg_lua_cache_script(tsg_lua_handle lua, const char *script, size_t script_len)
@@ -1191,6 +1245,7 @@ int tsg_lua_cache_exec(tsg_lua_handle lua, size_t script_id, const char *in, siz
{
lua_State *L = (lua_State *)lua;
jmp_buf *lua_exception;
+ int return_value = 0;
if (L == NULL)
{
@@ -1212,6 +1267,11 @@ int tsg_lua_cache_exec(tsg_lua_handle lua, size_t script_id, const char *in, siz
debuginfo("in_len is invailed", __FILE__, __LINE__);
return ERR_IN_LEN_INVAILD;
}
+ if (out_type == NULL || *out_type < STRING || *out_type > BOOLEAN)
+ {
+ debuginfo("out_type is invailed", __FILE__, __LINE__);
+ return ERR_EXPECT_TYPE_IS_NIL;
+ }
lua_settop(L, 0);
lua_exception = (jmp_buf *)lua_getexdata(L);
@@ -1236,14 +1296,22 @@ int tsg_lua_cache_exec(tsg_lua_handle lua, size_t script_id, const char *in, siz
return ERR_SCRIPT_EXEC_ERROR;
}
int num = lua_gettop(L);
- if (num < 2)
+ if (num == 0)
{
- char err_buf[255];
- sprintf((char *)err_buf,"num:%d", num);
- debuginfo(err_buf, __FILE__, __LINE__);
- return ERR_SCRIPT_RETURN_TOO_FEW;
+ //没有返回任何内容
+ debuginfo("script return nothing", __FILE__, __LINE__);
+ return ERR_RETURN_TYPE_NOT_MATCH_EXPECT;
}
- if (num > 2)
+ else if (num == 2)
+ {
+ *out_len = lua_tonumber(L, -2);
+ if (*out_len < 1)
+ {
+ debuginfo("script out_len is 0", __FILE__, __LINE__);
+ return ERR_SCRIPT_RETURN_LEN_INVAILD;
+ }
+ }
+ else if (num > 2)
{
char err_buf[255];
sprintf((char *)err_buf,"num:%d", num);
@@ -1251,27 +1319,30 @@ int tsg_lua_cache_exec(tsg_lua_handle lua, size_t script_id, const char *in, siz
return ERR_SCRIPT_RETURN_TOO_MUCH;
}
- *out_len = lua_tonumber(L, -2);
- if (*out_len < 1)
- {
- debuginfo("script out_len is 0", __FILE__, __LINE__);
- return ERR_SCRIPT_RETURN_LEN_INVAILD;
- }
- switch(lua_type(L, -1))
+ size_t lua_ret_type = lua_type(L, -1);
+ size_t actual_ret_type = 0;
+
+ switch(lua_ret_type)
{
+ case LUA_TNIL:
+ debuginfo("script return nil", __FILE__, __LINE__);
+ *out_type = NIL;
+ actual_ret_type = NIL;
+ return_value = ERR_RETUNR_NIL;
+ break;
case LUA_TSTRING:
memcpy(out, lua_tostring(L, -1), *out_len);
- *out_type = STRING;
- break;
+ actual_ret_type = STRING;
+ break;
case LUA_TBOOLEAN:
out[0] = lua_toboolean(L, -1);
- //*out_len = 1;
- *out_type = BOOLEAN;
+ *out_len = 1;
+ actual_ret_type = BOOLEAN;
break;
case LUA_TNUMBER:
*(size_t *)out = lua_tointeger(L, -1);
- //*out_len = 8;
- *out_type = INTEGER;
+ *out_len = 8;
+ actual_ret_type = INTEGER;
break;
default:
char err_buf[255];
@@ -1280,12 +1351,21 @@ int tsg_lua_cache_exec(tsg_lua_handle lua, size_t script_id, const char *in, siz
*out_len = 0;
return ERR_SCRIPT_RETURN_TYPE_INVAILD;
}
+
+ if (actual_ret_type != *out_type && actual_ret_type != NIL)
+ {
+ char err_buf[255];
+ sprintf((char *)err_buf, "expect out_type is:%zu, actual out_type is:%zu ", *out_type, actual_ret_type);
+ debuginfo(err_buf, __FILE__, __LINE__);
+ return_value = ERR_RETURN_TYPE_NOT_MATCH_EXPECT;
+ }
+
}else
{
return ERR_SCRIPT_EXEC_ERROR;
}
- return 0;
+ return return_value;
}
int tsg_destory_lua(tsg_lua_handle lua)
diff --git a/src/tsg_lua_interface.h b/src/tsg_lua_interface.h
index ae12b73..9c1f580 100644
--- a/src/tsg_lua_interface.h
+++ b/src/tsg_lua_interface.h
@@ -8,6 +8,7 @@
enum type
{
+ NIL = 0,
STRING = 1,
INTEGER,
BOOLEAN,
@@ -50,8 +51,11 @@ enum err_value
ERR_SCRIPT_ID_INVAILD = -33, /* 脚本ID无效 */
ERR_LUA_CACHE_FAILED = -34, /* 脚本缓存失败 */
ERR_MEM_NOT_ENOUGH = -35, /* 空间不足 */
- ERR_LUA_ID = -36, /* lua id 错误*/
- ERR_UNKNOWN = -37, /* 未知错误 */
+ ERR_LUA_ID = -36, /* lua id 错误*/
+ ERR_EXPECT_TYPE_IS_NIL = -37, /* 预期脚本返回类型为空 */
+ ERR_UNKNOWN = -38, /* 未知错误 */
+ ERR_RETUNR_NIL = 1, /* 脚本执行成功,但是返回空*/
+ ERR_RETURN_TYPE_NOT_MATCH_EXPECT = 2, /* 脚本执行成功,但是返回值类型与预期不符 */
};
typedef void* tsg_lua_handle;
@@ -70,7 +74,8 @@ tsg_lua_handle tsg_lua_vm_create();
* 输出参数:
* char *out 输出结果
* size_t *out_len 输出结果的长度
- * size_t *out_type 输出结果的类型
+ * 输入输出参数:
+ * size_t *out_type 要求输入一个不为NIL的预期类型, 调用完成后会输出结果的类型
* 返回值: int 成功,返回0;失败返回错误值err_value */
int tsg_lua_exec(tsg_lua_handle L, const char *script, size_t script_len, const char *in, size_t in_len, char *out, size_t *out_len, size_t *out_type);
@@ -82,7 +87,8 @@ int tsg_lua_exec(tsg_lua_handle L, const char *script, size_t script_len, const
* 输出参数:
* char *out 输出结果
* size_t *out_len 输出结果的长度
- * size_t *out_type 输出结果的类型
+ * 输入输出参数:
+ * size_t *out_type 要求输入一个不为NIL的预期类型, 调用完成后会输出结果的类型
* 返回值: int 成功,返回0;失败返回错误值err_value */
int tsg_lua_exec_file(tsg_lua_handle L, const char *script, const char *in, size_t in_len, char *out, size_t *out_len, size_t *out_type);
@@ -114,7 +120,8 @@ int tsg_lua_uncache_script(tsg_lua_handle L, size_t script_id);
* size_t in_len 输入待处理的数据的长度
* 输出参数: char *out 输出结果
* size_t *out_len 输出结果的长度
- * size_t *out_type 输出结果的类型
+ * 输入输出参数:
+ * size_t *out_type 要求输入一个不为NIL的预期类型, 调用完成后会输出结果的类型
* 返回值: int 成功,返回0;失败,返回错误值err_value */
int tsg_lua_cache_exec(tsg_lua_handle L, size_t script_id, const char *in, size_t in_len, char *out, size_t *out_len, size_t *out_type);