diff options
| author | “pengxuanzheng” <[email protected]> | 2022-07-05 08:41:09 +0000 |
|---|---|---|
| committer | “pengxuanzheng” <[email protected]> | 2022-07-05 09:02:52 +0000 |
| commit | e075fb586aa9e72226be6888ded7a2eba72b8cd4 (patch) | |
| tree | fee7238e7f4dcb7a3a6b3d36051e89dacd5659fa | |
| parent | 226e79434842626346ae378f408531d2d19059c7 (diff) | |
✨ feat(TSG-11123): 增加单元测试,并修改单元测试中发现的错误
| -rw-r--r-- | gtest/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | gtest/gtest_tsg_lua_exec_with_context.cpp | 180 | ||||
| -rw-r--r-- | gtest/gtest_tsg_lua_register_function.cpp | 88 | ||||
| -rw-r--r-- | gtest/script/exec_with_context.lua | 19 | ||||
| -rw-r--r-- | gtest/script/get_header.lua | 25 | ||||
| -rw-r--r-- | gtest/script/get_other.lua | 14 | ||||
| -rw-r--r-- | src/tsg_lua_func.cpp | 116 | ||||
| -rw-r--r-- | src/tsg_lua_interface.h | 17 |
8 files changed, 391 insertions, 70 deletions
diff --git a/gtest/CMakeLists.txt b/gtest/CMakeLists.txt index 69b9d3f..fda6e7d 100644 --- a/gtest/CMakeLists.txt +++ b/gtest/CMakeLists.txt @@ -11,7 +11,7 @@ link_libraries(tsglua gtest gtest_main pthread) add_definitions(-g -W -Wall) add_executable(gtest_tsg_lua ${SRCS}) -#add_executable(gtest_tsg_lua_cache_script ./gtest_tsg_lua_cache_script.cpp) +#add_executable(gtest_tsg_lua_exec_with_context ./gtest_tsg_lua_exec_with_context.cpp) add_dependencies(gtest_tsg_lua ${lib_name}_shared gtest) #target_link_libraries(gtest_tsg_lua tsglua gtest gtest_main pthread) diff --git a/gtest/gtest_tsg_lua_exec_with_context.cpp b/gtest/gtest_tsg_lua_exec_with_context.cpp new file mode 100644 index 0000000..095bc25 --- /dev/null +++ b/gtest/gtest_tsg_lua_exec_with_context.cpp @@ -0,0 +1,180 @@ +#include <gtest/gtest.h> +#include "tsg_lua_interface.h" + +static char buffer[4096]; +static char *get_script_buffer(const char *script) +{ + FILE *fp = fopen(script, "r"); + if (fp) + { + int num = fread(buffer, 1, 4096, fp); + if (num != 0) + { + buffer[num] = 0; + fclose(fp); + return buffer; + } + fclose(fp); + } + return NULL; +} + +int get_userdata(tsg_lua_handle L) +{ + void *ud = lua_get_userdata(L); + c_push_string_into_lua(L, (const char *)ud, strlen((const char *)ud)); + return 1; +} + +TEST(lua_exec_file_with_context, normal) +{ + tsg_lua_handle L = tsg_lua_vm_create_with_name("TEST"); + + const char *ud = "hello world."; + const char *script = "./script/exec_with_context.lua"; + lua_data_t in; + in.data = (char *)"This is a test"; + in.len = strlen(in.data); + lua_arg_t out; + out.type = STRING; + out.len = 1024; + out.str = (char *)calloc(1, 1024); + + lua_register_function(L, "get", "get_userdata", get_userdata); + lua_script_context context = NULL; + lua_set_script_context(L, &context); + int ret = lua_exec_file_with_context(L, script, in, (void *)ud, context, &out); + const char *str = "userdata:hello world., context.count:1, context.message:This is first called"; + EXPECT_EQ(0, ret); + EXPECT_EQ(strlen(str), out.len); + EXPECT_EQ(STRING, out.type); + EXPECT_STREQ(str, out.str); + bzero(out.str, 1024); + + ud = "hello lua."; + ret = lua_exec_file_with_context(L, script, in, (void *)ud, context, &out); + str = "userdata:hello lua., context.count:2, context.message:This is not first called"; + EXPECT_EQ(0, ret); + EXPECT_EQ(strlen(str), out.len); + EXPECT_EQ(STRING, out.type); + EXPECT_STREQ(str, out.str); + bzero(out.str, 1024); + + lua_script_context context2 = NULL; + lua_set_script_context(L, &context2); + ret = lua_exec_file_with_context(L, script, in, (void *)ud, context2, &out); + str = "userdata:hello lua., context.count:1, context.message:This is first called"; + EXPECT_EQ(0, ret); + EXPECT_EQ(strlen(str), out.len); + EXPECT_EQ(STRING, out.type); + EXPECT_STREQ(str, out.str); + bzero(out.str, 1024); + + lua_unset_script_context(L, context); + lua_unset_script_context(L, context2); + tsg_destory_lua(L); + free(out.str); +} + +TEST(lua_exec_with_context, normal) +{ + tsg_lua_handle L = tsg_lua_vm_create_with_name("TEST"); + + const char *ud = "hello world."; + lua_data_t in; + in.data = (char *)"This is a test"; + in.len = strlen(in.data); + lua_arg_t out; + out.type = STRING; + out.len = 1024; + out.str = (char *)calloc(1, 1024); + lua_data_t script; + script.data = get_script_buffer("./script/exec_with_context.lua"); + script.len = strlen(script.data); + + lua_register_function(L, "get", "get_userdata", get_userdata); + lua_script_context context = NULL; + lua_set_script_context(L, &context); + int ret = lua_exec_with_context(L, script, in, (void *)ud, context, &out); + const char *str = "userdata:hello world., context.count:1, context.message:This is first called"; + EXPECT_EQ(0, ret); + EXPECT_EQ(strlen(str), out.len); + EXPECT_EQ(STRING, out.type); + EXPECT_STREQ(str, out.str); + bzero(out.str, 1024); + + ud = "hello lua."; + ret = lua_exec_with_context(L, script, in, (void *)ud, context, &out); + str = "userdata:hello lua., context.count:2, context.message:This is not first called"; + EXPECT_EQ(0, ret); + EXPECT_EQ(strlen(str), out.len); + EXPECT_EQ(STRING, out.type); + EXPECT_STREQ(str, out.str); + bzero(out.str, 1024); + + lua_script_context context2 = NULL; + lua_set_script_context(L, &context2); + ret = lua_exec_with_context(L, script, in, (void *)ud, context2, &out); + str = "userdata:hello lua., context.count:1, context.message:This is first called"; + EXPECT_EQ(0, ret); + EXPECT_EQ(strlen(str), out.len); + EXPECT_EQ(STRING, out.type); + EXPECT_STREQ(str, out.str); + bzero(out.str, 1024); + + lua_unset_script_context(L, context); + lua_unset_script_context(L, context2); + tsg_destory_lua(L); + free(out.str); +} + +TEST(lua_cache_exec_with_context, normal) +{ + tsg_lua_handle L = tsg_lua_vm_create_with_name("TEST"); + + const char *ud = "hello world."; + lua_data_t in; + in.data = (char *)"This is a test"; + in.len = strlen(in.data); + lua_arg_t out; + out.type = STRING; + out.len = 1024; + out.str = (char *)calloc(1, 1024); + const char *script = get_script_buffer("./script/exec_with_context.lua"); + int script_id = tsg_lua_cache_script(L, script, strlen(script)); + + lua_register_function(L, "get", "get_userdata", get_userdata); + lua_script_context context = NULL; + lua_set_script_context(L, &context); + int ret = lua_cache_exec_with_context(L, script_id, in, (void *)ud, context, &out); + const char *str = "userdata:hello world., context.count:1, context.message:This is first called"; + EXPECT_EQ(0, ret); + EXPECT_EQ(strlen(str), out.len); + EXPECT_EQ(STRING, out.type); + EXPECT_STREQ(str, out.str); + bzero(out.str, 1024); + + ud = "hello lua."; + ret = lua_cache_exec_with_context(L, script_id, in, (void *)ud, context, &out); + str = "userdata:hello lua., context.count:2, context.message:This is not first called"; + EXPECT_EQ(0, ret); + EXPECT_EQ(strlen(str), out.len); + EXPECT_EQ(STRING, out.type); + EXPECT_STREQ(str, out.str); + bzero(out.str, 1024); + + lua_script_context context2 = NULL; + lua_set_script_context(L, &context2); + ret = lua_cache_exec_with_context(L, script_id, in, (void *)ud, context2, &out); + str = "userdata:hello lua., context.count:1, context.message:This is first called"; + EXPECT_EQ(0, ret); + EXPECT_EQ(strlen(str), out.len); + EXPECT_EQ(STRING, out.type); + EXPECT_STREQ(str, out.str); + bzero(out.str, 1024); + + lua_unset_script_context(L, context); + lua_unset_script_context(L, context2); + tsg_destory_lua(L); + free(out.str); +} diff --git a/gtest/gtest_tsg_lua_register_function.cpp b/gtest/gtest_tsg_lua_register_function.cpp index c783448..9612dd1 100644 --- a/gtest/gtest_tsg_lua_register_function.cpp +++ b/gtest/gtest_tsg_lua_register_function.cpp @@ -33,6 +33,44 @@ int set_world(tsg_lua_handle L) return 1; } +int get_req_header(tsg_lua_handle L) +{ + const char *key[] = {"token", "User-Agent", "Accept", "Host", "Accept-Encoding", + "Connection", "Content-Length", "Content-Type"}; + const char *value[] = {"c21f969b5f03d33d43e04f8f136e7682", " PostmanRuntime/7.26.8","*/*", "192.168.40.223:9098", + "gzip, deflate, br", "keep-alive", "7020", "text/markdown"}; + c_push_table_into_lua(L, key, value, 8); + + return 1; +} + +int get_rsp_header(tsg_lua_handle L) +{ + const char *key[] = {"Content-Type", "Content-Length", "Data"}; + const char *value[] = {"application/xml;charset=UTF-8", "0", "Wed, 16 Dec 2020 08:45:55 GMT"}; + c_push_table_into_lua(L, key, value, 3); + + return 1; +} + +int get_result(tsg_lua_handle L) +{ + c_push_bool_into_lua(L, false); + return 1; +} + +int get_nil(tsg_lua_handle L) +{ + c_push_nil_into_lua(L); + return 1; +} + +int get_time(tsg_lua_handle L) +{ + c_push_num_into_lua(L, 0x12345678); + return 1; +} + TEST(lua_register_function, normal) { tsg_lua_handle L = NULL; @@ -53,4 +91,54 @@ TEST(lua_register_function, normal) EXPECT_EQ(12, out_len); EXPECT_EQ(STRING, out_type); EXPECT_STREQ("hello world.", out); + tsg_destory_lua(L); +} + +TEST(lua_register_function, get_header) +{ + tsg_lua_handle L = NULL; + char out[1024]; + size_t out_len = 0; + size_t out_type = STRING; + + L = tsg_lua_vm_create(); + EXPECT_NE((void *)NULL, L); + lua_register_function(L, "req", "get_header", get_req_header); + lua_register_function(L, "rsp", "get_header", get_rsp_header); + + const char *script = "./script/get_header.lua"; + memset(out, 0, 1024); + int ret = tsg_lua_exec_file(L, script, "hello", strlen("hello"), out, &out_len, &out_type); + + const char *result = "req_header:Host:192.168.40.223:9098,Content-Type:text/markdown,Content-Length:7020,Connection:keep-alive,Accept:*/*,User-Agent: PostmanRuntime/7.26.8,token:c21f969b5f03d33d43e04f8f136e7682,Accept-Encoding:gzip, deflate, br; rsp_header:nil,Content-Length:0,Content-Type:application/xml;charset=UTF-8,Data:Wed, 16 Dec 2020 08:45:55 GMT"; + EXPECT_EQ(0, ret); + EXPECT_EQ(strlen(result), out_len); + EXPECT_EQ(STRING, out_type); + EXPECT_STREQ(result, out); + tsg_destory_lua(L); +} + +TEST(lua_register_function, other) +{ + tsg_lua_handle L = NULL; + char out[1024]; + size_t out_len = 0; + size_t out_type = STRING; + + L = tsg_lua_vm_create(); + EXPECT_NE((void *)NULL, L); + lua_register_function(L, "get", "get_result", get_result); + lua_register_function(L, "get", "get_nil", get_nil); + lua_register_function(L, "get", "get_time", get_time); + + const char *script = "./script/get_other.lua"; + memset(out, 0, 1024); + int ret = tsg_lua_exec_file(L, script, "hello", strlen("hello"), out, &out_len, &out_type); + + const char *result = "result:false, time:305419896, is nil"; + EXPECT_EQ(0, ret); + EXPECT_EQ(strlen(result), out_len); + EXPECT_EQ(STRING, out_type); + EXPECT_STREQ(result, out); + tsg_destory_lua(L); } diff --git a/gtest/script/exec_with_context.lua b/gtest/script/exec_with_context.lua new file mode 100644 index 0000000..f7464ba --- /dev/null +++ b/gtest/script/exec_with_context.lua @@ -0,0 +1,19 @@ +local data = TEST.data +local context = TEST.context + +if not context.count then + context.count = 1 +else + context.count = context.count + 1 +end + +if not context.message then + context.message = "This is first called" +else + context.message = "This is not first called" +end + +local ud = TEST.get.get_userdata() +local str = string.format("userdata:%s, context.count:%d, context.message:%s", ud, context.count, context.message) + +return string.len( str ), str diff --git a/gtest/script/get_header.lua b/gtest/script/get_header.lua new file mode 100644 index 0000000..104ddd0 --- /dev/null +++ b/gtest/script/get_header.lua @@ -0,0 +1,25 @@ +local data = TSG.data + +local req_header = nil +local header = TSG.req.get_header() +for k, v in pairs(header) do + if not req_header then + req_header = string.format( "%s:%s", k, v) + else + req_header = string.format( "%s,%s:%s",req_header, k, v) + end +end + +local rsp_header = nil +header = TSG.rsp.get_header() +for k, v in pairs(header) do + if not req_header then + rsp_header = string.format("%s:%s", k, v) + else + rsp_header = string.format("%s,%s:%s", rsp_header, k, v) + end +end + +local ret = string.format( "req_header:%s; rsp_header:%s", req_header, rsp_header) +-- print(ret) +return string.len(ret), ret
\ No newline at end of file diff --git a/gtest/script/get_other.lua b/gtest/script/get_other.lua new file mode 100644 index 0000000..02c1fa7 --- /dev/null +++ b/gtest/script/get_other.lua @@ -0,0 +1,14 @@ +local data = TSG.data +local result = "true" +if not TSG.get.get_result() then + result = "false" +end + +local flag = "not nil" +if not TSG.get.get_nil() then + flag = "is nil" +end + +local str = string.format("result:%s, time:%d, %s", result, TSG.get.get_time(), flag) + +return string.len(str), str
\ No newline at end of file diff --git a/src/tsg_lua_func.cpp b/src/tsg_lua_func.cpp index e4ffc8d..aee6b72 100644 --- a/src/tsg_lua_func.cpp +++ b/src/tsg_lua_func.cpp @@ -142,7 +142,7 @@ static exec_error_massage_t exec_error[] = static void debuginfo(const char *info, const char *file, size_t line) { #ifdef TSG_LUA_DEBUG - printf("error: [%s:%ld]%s\n", file, line, info); + printf("error: [%s:%zu]%s\n", file, line, info); #else (void)info; (void)file; @@ -515,14 +515,18 @@ tsg_lua_handle tsg_lua_vm_create_with_name(const char *name) lua_setfield(L, -2, "memmem"); lua_setglobal(L, name); #if 1 - const char *code = "local ffi = require(\"ffi\")\n"\ + char code[1024]; + memset(code, 0, 1024); + snprintf(code, 1023, "local ffi = require(\"ffi\")\n"\ "ffi.cdef[[char *memmem(const char *haystack, size_t haystacklen, const char *needle, size_t needlelen);]]\n"\ - "TSG.ffi = ffi\n"\ - "TSG.C = ffi.C"; + "%s.ffi = ffi\n"\ + "%s.C = ffi.C", name, name); if (luaL_dostring(L, code)) { const char *err = lua_tostring(L, -1); debuginfo(err, __FILE__, __LINE__); + free(lua_exception); + free(lua_info); lua_close(L); return NULL; } @@ -535,9 +539,8 @@ tsg_lua_handle tsg_lua_vm_create() return tsg_lua_vm_create_with_name((const char *)NULL); } -int tsg_lua_exec_file(tsg_lua_handle lua, const char *script, const char *in, size_t in_len, char *out, size_t *out_len, size_t *out_type) +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) { - lua_State *L = (lua_State *)lua; const char *err = NULL; int ret = 0; int i = 0; @@ -687,7 +690,7 @@ int tsg_lua_exec_file(tsg_lua_handle lua, const char *script, const char *in, si } /* 输入待处理数据 */ - lua_getglobal(L, "TSG"); + lua_getglobal(L, lua_info->lua_name); lua_pushlstring(L, in, in_len); lua_setfield(L, -2, "data"); lua_settop(L, 1); @@ -778,9 +781,8 @@ int tsg_lua_exec_file(tsg_lua_handle lua, const char *script, const char *in, si } -int tsg_lua_exec(tsg_lua_handle lua, const char *script, size_t script_len, const char *in, size_t in_len, char *out, size_t *out_len, size_t *out_type) +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) { - lua_State *L = (lua_State *)lua; const char *err = NULL; int ret = 0; int i = 0; @@ -917,7 +919,7 @@ int tsg_lua_exec(tsg_lua_handle lua, const char *script, size_t script_len, cons } /* 输入待处理数据 */ - lua_getglobal(L, "TSG"); + lua_getglobal(L, lua_info->lua_name); lua_pushlstring(L, in, in_len); lua_setfield(L, -2, "data"); lua_settop(L, 1); @@ -1006,14 +1008,13 @@ int tsg_lua_exec(tsg_lua_handle lua, const char *script, size_t script_len, cons return return_value; } -int tsg_lua_cache_script(tsg_lua_handle lua, const char *script, size_t script_len) +int tsg_lua_cache_script(tsg_lua_handle L, const char *script, size_t script_len) { size_t script_id = 0; const char *err = NULL; int ret = 0; int i = 0; int sharp = 0; - lua_State *L = (lua_State *)lua; tsg_lua_clfactory_buffer_t ls; if (L == NULL) @@ -1125,14 +1126,13 @@ int tsg_lua_cache_script(tsg_lua_handle lua, const char *script, size_t script_l return script_id; } -int tsg_lua_cache_script_file(tsg_lua_handle lua, const char *script) +int tsg_lua_cache_script_file(tsg_lua_handle L, const char *script) { size_t script_id; const char *err = NULL; int ret = 0; int sharp = 0; tsg_lua_clfactory_file_t lf; - lua_State *L = (lua_State *)lua; if (L == NULL) @@ -1261,10 +1261,8 @@ int tsg_lua_cache_script_file(tsg_lua_handle lua, const char *script) return script_id; } -int tsg_lua_uncache_script(tsg_lua_handle lua, size_t script_id) +int tsg_lua_uncache_script(tsg_lua_handle L, size_t script_id) { - lua_State *L = (lua_State *)lua; - if (L == NULL) { debuginfo("lua VM is null.", __FILE__, __LINE__); @@ -1276,9 +1274,8 @@ int tsg_lua_uncache_script(tsg_lua_handle lua, size_t script_id) return 0; } -int tsg_lua_cache_exec(tsg_lua_handle lua, size_t script_id, const char *in, size_t in_len, char *out, size_t *out_len, size_t *out_type) +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) { - lua_State *L = (lua_State *)lua; struct lua_private_info_t *lua_info = NULL; jmp_buf *lua_exception; int return_value = 0; @@ -1314,7 +1311,7 @@ int tsg_lua_cache_exec(tsg_lua_handle lua, size_t script_id, const char *in, siz lua_exception = lua_info->lua_exception; lua_rawgeti(L, LUA_REGISTRYINDEX, script_id); - lua_getglobal(L, "TSG"); + lua_getglobal(L, lua_info->lua_name); lua_pushlstring(L, in, in_len); lua_setfield(L, -2, "data"); lua_settop(L, 1); @@ -1405,10 +1402,8 @@ int tsg_lua_cache_exec(tsg_lua_handle lua, size_t script_id, const char *in, siz return return_value; } -int tsg_destory_lua(tsg_lua_handle lua) +int tsg_destory_lua(tsg_lua_handle L) { - lua_State *L = (lua_State *)lua; - jmp_buf *lua_exception = NULL; struct lua_private_info_t *lua_info = NULL; if (L == NULL) @@ -1421,7 +1416,7 @@ int tsg_destory_lua(tsg_lua_handle lua) { if (lua_info->lua_exception != NULL) { - free(lua_exception); + free(lua_info->lua_exception); } free(lua_info); } @@ -1430,9 +1425,8 @@ int tsg_destory_lua(tsg_lua_handle lua) return 0; } -int c_pull_param_from_lua(tsg_lua_handle lua, int *argc, struct lua_arg_t **argv) +int c_pull_param_from_lua(tsg_lua_handle L, int *argc, struct lua_arg_t **argv) { - lua_State *L = (lua_State *)lua; if (L == NULL) { return ERR_PARAMETER; @@ -1481,6 +1475,9 @@ int c_pull_param_from_lua(tsg_lua_handle lua, int *argc, struct lua_arg_t **argv int free_param_form_lua(int argc, lua_arg_t **argv) { if (argc == 0 || *argv == NULL) + { + return 0; + } for(int i = 0; i < argc; i++) { @@ -1501,9 +1498,8 @@ int free_param_form_lua(int argc, lua_arg_t **argv) return 0; } -int c_push_string_into_lua(tsg_lua_handle lua, const char *str, size_t len) +int c_push_string_into_lua(tsg_lua_handle L, const char *str, size_t len) { - lua_State *L = (lua_State *)lua; if (L == NULL || str == NULL) { return ERR_PARAMETER; @@ -1513,9 +1509,8 @@ int c_push_string_into_lua(tsg_lua_handle lua, const char *str, size_t len) return 0; } -int c_push_num_into_lua(tsg_lua_handle lua, long num) +int c_push_num_into_lua(tsg_lua_handle L, long num) { - lua_State *L = (lua_State *)lua; if (L == NULL ) { return ERR_PARAMETER; @@ -1525,21 +1520,27 @@ int c_push_num_into_lua(tsg_lua_handle lua, long num) return 0; } -int c_push_bool_into_lua(tsg_lua_handle lua, bool flag) +int c_push_bool_into_lua(tsg_lua_handle L, bool flag) { - lua_State *L = (lua_State *)lua; if (L == NULL) { return ERR_PARAMETER; } - lua_pushinteger(L, flag); + if (flag == false) + { + lua_pushnil(L); + } + else + { + lua_pushinteger(L, flag); + } + return 0; } -int c_push_nil_into_lua(tsg_lua_handle lua) +int c_push_nil_into_lua(tsg_lua_handle L) { - lua_State *L = (lua_State *)lua; if (L == NULL) { return ERR_PARAMETER; @@ -1549,9 +1550,8 @@ int c_push_nil_into_lua(tsg_lua_handle lua) return 0; } -int c_push_table_into_lua(tsg_lua_handle lua, const char **key_list, const char **value_list, size_t list_len) +int c_push_table_into_lua(tsg_lua_handle L, const char **key_list, const char **value_list, size_t list_len) { - lua_State *L = (lua_State *)lua; if (L == NULL || key_list == NULL || value_list == NULL) { return ERR_PARAMETER; @@ -1562,13 +1562,13 @@ int c_push_table_into_lua(tsg_lua_handle lua, const char **key_list, const char { if (key_list[i] != NULL && value_list[i] != NULL) { - // lua_pushliteral(L, key_list[i]); - // lua_pushliteral(L, value_list[i]); + lua_pushlstring(L, key_list[i], strlen(key_list[i])); + lua_pushlstring(L, value_list[i], strlen(value_list[i])); lua_rawset(L, -3); } } - return 0; + return 1; } void *lua_get_userdata(tsg_lua_handle L) @@ -1577,7 +1577,7 @@ void *lua_get_userdata(tsg_lua_handle L) if (lua_info != NULL) { - return lua_info->lua_name; + return lua_info->userdata; } else { @@ -1585,9 +1585,8 @@ void *lua_get_userdata(tsg_lua_handle L) } } -int lua_register_function(tsg_lua_handle lua, const char *function_set, const char *function_name, lua_function_ptr const function) +int lua_register_function(tsg_lua_handle L, const char *function_set, const char *function_name, lua_function_ptr const function) { - lua_State *L = (lua_State *)lua; if (L == NULL || function_set == NULL || function_name == NULL || function == NULL) { return ERR_PARAMETER; @@ -1619,13 +1618,14 @@ int lua_register_function(tsg_lua_handle lua, const char *function_set, const ch return 0; } -struct lua_script_context +struct lua_script_context_t { int context_id; }; -int lua_set_script_context(tsg_lua_handle L, struct lua_script_context *context) + +int lua_set_script_context(tsg_lua_handle L, struct lua_script_context_t **context) { - if (L == NULL || context == NULL) + if (L == NULL || *context != NULL) { return ERR_PARAMETER; } @@ -1636,23 +1636,16 @@ int lua_set_script_context(tsg_lua_handle L, struct lua_script_context *context) { return ERR_LUA_SET_CONTEXT_FAILED; } - - // struct lua_private_info_t *lua_info = lua_getexdata(L); - // if (lua_info == NULL) - // { - // return ERR_LUA_PRIVATE_INFO_IS_NIL; - // } - - // lua_getglobal(L, lua_info->lua_name); - // lua_rawgeti(L, LUA_REGISTRYINDEX, context_id); - // lua_setfield(L, -2, "context"); + lua_settop(L, 0); - context->context_id = context_id; + *context = (struct lua_script_context_t *)malloc(sizeof(struct lua_script_context_t *)); + (*context)->context_id = context_id; + return 0; } -int lua_unset_script_context(tsg_lua_handle L, struct lua_script_context *context) +int lua_unset_script_context(tsg_lua_handle L, struct lua_script_context_t *context) { if (L == NULL) { @@ -1665,11 +1658,12 @@ int lua_unset_script_context(tsg_lua_handle L, struct lua_script_context *contex } luaL_unref(L, LUA_REGISTRYINDEX, context->context_id); + free(context); return 0; } -int lua_cache_exec_with_context(tsg_lua_handle L, size_t script_id, struct lua_data_t in, void *userdata, lua_script_context *context, struct lua_arg_t *outvalue) +int lua_cache_exec_with_context(tsg_lua_handle L, size_t script_id, struct lua_data_t in, void *userdata, lua_script_context context, struct lua_arg_t *outvalue) { if (L == NULL) { @@ -1699,7 +1693,7 @@ int lua_cache_exec_with_context(tsg_lua_handle L, size_t script_id, struct lua_d return tsg_lua_cache_exec(L, script_id, in.data, in.len, outvalue->str, &outvalue->len, &outvalue->type); } -int lua_exec_with_context(tsg_lua_handle L, struct lua_data_t script, struct lua_data_t in, void *userdata, lua_script_context *context, struct lua_arg_t *outvalue) +int lua_exec_with_context(tsg_lua_handle L, struct lua_data_t script, struct lua_data_t in, void *userdata, lua_script_context context, struct lua_arg_t *outvalue) { if (L == NULL) { @@ -1730,7 +1724,7 @@ int lua_exec_with_context(tsg_lua_handle L, struct lua_data_t script, struct lua return tsg_lua_exec(L, script.data, script.len, in.data, in.len, outvalue->str, &outvalue->len, &outvalue->type); } -int lua_exec_file_with_context(tsg_lua_handle L, const char *script, struct lua_data_t in, void *userdata, lua_script_context *context, struct lua_arg_t *outvalue) +int lua_exec_file_with_context(tsg_lua_handle L, const char *script, struct lua_data_t in, void *userdata, lua_script_context context, struct lua_arg_t *outvalue) { if (L == NULL) { diff --git a/src/tsg_lua_interface.h b/src/tsg_lua_interface.h index 0beef81..3642012 100644 --- a/src/tsg_lua_interface.h +++ b/src/tsg_lua_interface.h @@ -189,18 +189,19 @@ typedef int (*lua_function_ptr)(tsg_lua_handle L); * 返回值: int 成功,返回0;失败返回 -1。*/ int lua_register_function(tsg_lua_handle L, const char *function_set, const char *function_name, lua_function_ptr function); -struct lua_script_context; +struct lua_script_context_t; +typedef lua_script_context_t * lua_script_context; /* 函数名: lua_set_script_context * 输入参数: tsg_lua_handle L - * 输出参数: lua_script_context *context_id 成功时返回lua context + * 输出参数: lua_script_context **context 成功时返回lua context * 返回值: int 成功,返回0;失败返回 -1*/ -int lua_set_script_context(tsg_lua_handle L, struct lua_script_context *context); +int lua_set_script_context(tsg_lua_handle L, lua_script_context *context); /* 函数名: lua_unset_script_context * 输入参数: tsg_lua_handle L * 输出参数: lua_script_context *context_id 成功时回收lua context * 返回值: int 成功,返回0;失败返回 -1*/ -int lua_unset_script_context(tsg_lua_handle L, struct lua_script_context *context); +int lua_unset_script_context(tsg_lua_handle L, lua_script_context context); /* 函数名: lua_cache_exec_with_context * 输入参数: tsg_lua_handle L @@ -210,7 +211,7 @@ int lua_unset_script_context(tsg_lua_handle L, struct lua_script_context *contex struct lua_script_context *context lua上下文 * 输出参数: struct lua_arg_t *outvalue 脚本执行成功返回的内容 * 返回值: int 成功,返回0;失败返回 -1*/ -int lua_cache_exec_with_context(tsg_lua_handle L, size_t script_id, struct lua_data_t in, void *userdata, lua_script_context *context, struct lua_arg_t *outvalue); +int lua_cache_exec_with_context(tsg_lua_handle L, size_t script_id, struct lua_data_t in, void *userdata, lua_script_context context, struct lua_arg_t *outvalue); /* 函数名: lua_cache_exec_with_context * 输入参数: tsg_lua_handle L @@ -220,7 +221,7 @@ int lua_cache_exec_with_context(tsg_lua_handle L, size_t script_id, struct lua_d struct lua_script_context *context lua上下文 * 输出参数: struct lua_arg_t *outvalue 脚本执行成功返回的内容 * 返回值: int 成功,返回0;失败返回 -1*/ -int lua_exec_with_context(tsg_lua_handle L, struct lua_data_t script, struct lua_data_t in, void *userdata, lua_script_context *context, struct lua_arg_t *outvalue); +int lua_exec_with_context(tsg_lua_handle L, struct lua_data_t script, struct lua_data_t in, void *userdata, lua_script_context context, struct lua_arg_t *outvalue); /* 函数名: lua_cache_exec_with_context * 输入参数: tsg_lua_handle L @@ -230,11 +231,11 @@ int lua_exec_with_context(tsg_lua_handle L, struct lua_data_t script, struct lua struct lua_script_context *context lua上下文文 * 输出参数: struct lua_arg_t *outvalue 脚本执行成功返回的内容 * 返回值: int 成功,返回0;失败返回 -1*/ -int lua_exec_file_with_context(tsg_lua_handle L, struct lua_data_t script, struct lua_data_t in, void *userdata, lua_script_context *context, struct lua_arg_t *outvalue); +int lua_exec_file_with_context(tsg_lua_handle L, const char *script, struct lua_data_t in, void *userdata, lua_script_context context, struct lua_arg_t *outvalue); /* 函数名:tsg_lua_vm_create_with_name * 返回值:tsg_lua_handle 成功,返回一个虚拟机, * 输入参数: char *name 虚拟机名称,可以在lua中找到该名字的全局变量,建议大写字母开头。如果为空,默认填写"TSG" * 失败,返回NULL */ -tsg_lua_handle tsg_lua_vm_create_with_name(char *name); +tsg_lua_handle tsg_lua_vm_create_with_name(const char *name); #endif |
