diff options
| author | liuxueli <[email protected]> | 2022-08-29 17:28:48 +0800 |
|---|---|---|
| committer | liuxueli <[email protected]> | 2022-08-29 17:28:48 +0800 |
| commit | ab3c9a36461a3f3f146c012cfaab713cc8d32a6a (patch) | |
| tree | 29a702cd909113aa654d03d25d9e90dc05b67c8a | |
| parent | a076662ade64ace4c1019fd726b5077c5df76abe (diff) | |
重新定义lua头文件
| -rw-r--r-- | src/tsg_lua_interface.h | 217 |
1 files changed, 40 insertions, 177 deletions
diff --git a/src/tsg_lua_interface.h b/src/tsg_lua_interface.h index 8e370d7..99c0ee0 100644 --- a/src/tsg_lua_interface.h +++ b/src/tsg_lua_interface.h @@ -7,7 +7,7 @@ #define __TSG_LUA_INTERFACE__ #include <stddef.h> -enum type +enum lua_type { NIL = 0, STRING = 1, @@ -15,166 +15,48 @@ enum type BOOLEAN, }; -struct lua_arg_t +struct lua_data { - size_t type; - size_t len; //仅当type == STRING时有效 + enum lua_type type; + size_t len; union{ - char *str; + char *string; long num; bool flag; }; }; -struct lua_KV_t -{ - char *key; - char *value; -}; +struct lua_vm; -struct lua_multi_KV_t -{ - int KV_num; - struct lua_KV_t *KV; -}; +/* function name: tsg_lua_vm_create + * input: char *name the name of virtual machine, default: "TSG" + * return value: tsg_lua_handle successed,return a virtual machine; failed, return NULL*/ +struct lua_vm *lua_vm_create(const char *vm_name); -struct lua_data_t -{ - size_t len; - char *data; -}; +int lua_remove_function(struct lua_vm *vm, const char *lua_function); -enum err_value -{ - ERR_LUAVM_ISNULL = -1, /*The lua vm is not existed */ - ERR_SCRIPT_ISNULL = -2, /* The entered script is null */ - ERR_INPUT_ISNULL = -3, /* The entered data is null */ - ERR_IN_LEN_INVAILD = -4, /* The length of entered data less then 1*/ - ERR_SCRIPT_RETURN_LEN_INVAILD = -5, /* The return value of the script has a length of 0 */ - ERR_SCRIPT_RETURN_TOO_MUCH = -6, /* Too many return values of the script */ - ERR_SCRIPT_RETURN_TOO_FEW = -7, /* Too few return values of the script */ - ERR_SCRIPT_RETURN_TYPE_INVAILD = -8, /* The script return a value which type is error */ - ERR_SCRIPT_SYNTAX_ERROR = -9, /* Script syntax error */ - ERR_SCRIPT_END_EXPECTED = -10, /* Script "end" expected */ - ERR_SCRIPT_THEN_EXPECTED = -11, /* Script "then" expected */ - ERR_SCRIPT_DO_EXPECTED = -12, /* Script "do" expected */ - ERR_SCRIPT_EOF_EXPECTED = -13, /* Script "eof" expected */ - ERR_SCRIPT_EQUAL_IN_EXPECTED = -14, /* Scprit "=" or "in" expected */ - ERR_SCRIPT_UNEXPECTED_SYMBOL = -15, /* Script funexpected symbol */ - ERR_SCRIPT_NAME_EXPECTED = -16, /* Script "<name>" expected */ - ERR_SCRIPT_RIGHT_CURVES_BRACKET_EXPECTED = -17, /* Script ")" expected */ - ERR_SCRIPT_RIGHT_CURLY_BRACKET_EXPECTED = -18, /* Script "}" expected */ - ERR_SCRIPT_EXEC_ERROR = -19, /* Script run error */ - ERR_SCRIPT_STRING_EXPECTED_BUT_NIL = -20, /* Script bad argument, string expected, got nil */ - ERR_SCRIPT_STRING_EXPECTED_BUT_BOOL = -21, /* Script bad argument, string expected, got boolean */ - ERR_SCRIPT_STRING_EXPECTED_BUT_TABLE = -22, /* Script bad argument, string expected, got table */ - ERR_SCRIPT_CALL_GLOBAL_BUT_NIL = -23, /* Script attempt to call global value which is a nil value */ - ERR_SCRIPT_NOT_EXIT = -24, /* The script is not existed */ - ERR_SCRIPT_BYTECODE_NO_HEADER = -25, /* The bytecode script has no header */ - ERR_SCRIPT_BYTECODE_BAD_HEADER = -26, /* The bytecode script has a bad header */ - ERR_SCRIPT_BYTECODE_VERSION_UNSUPPORTED = -27, /* Not support the bytecode version */ - ERR_SCRIPT_BYTECODE_ONLY_SUPPORT_LUAJIT = -28, /* Only support the bytecode script of luajit */ - ERR_SCRIPT_BYTECODE_NOT_SUPPORT_BIG_ENDIAN = -29, /* Not support the bytecoode script building with BIG ENDIAN */ - ERR_SCRIPT_BYTECODE_NOT_SUPPORT_DEBUG = -30, /* Not support the bytecode script building with Debug mode */ - ERR_SCRIPT_IS_BAD = -31, /* The script is bad */ - ERR_SCRIPT_ID_INVAILD = -32, /* The script id is invaild*/ - ERR_LUA_CACHE_FAILED = -33, /* Cache script failed */ - ERR_MEM_NOT_ENOUGH = -34, /* The memory of lua is not enough */ - ERR_EXPECT_TYPE_IS_NIL = -35, /* The caller expect script return a nil value */ - ERR_LUA_FUNCTION_ARGC = -36, /* lua cFunction, the number of input parameters does not match expection */ - ERR_LUA_FUNCTION_ARGV = -37, /* lua cFunction, the type of input paramters does not match expection */ - ERR_PARAMETER = -38, /* Paramter error */ - ERR_LUA_PRIVATE_INFO_IS_NIL = -39, /* The private info of lua is nil */ - ERR_LUA_SET_CONTEXT_FAILED = -40, /* lua malloc context failed */ - ERR_SCRIPT_TIMEOUT = -41, /* Time out */ - ERR_UNKNOWN = -42, /* Unkown */ - ERR_RETUNR_NIL = 1, /* Script run successed, but return a nil value*/ - ERR_RETURN_TYPE_NOT_MATCH_EXPECT = 2, /* Script run successed,but the type of return value does not match expection */ -}; - -struct lua_State; -typedef lua_State* tsg_lua_handle; - -/* function name: tsg_lua_vm_create - * return value: tsg_lua_handle successed,return a virtual machine; failed, return NULL*/ -tsg_lua_handle tsg_lua_vm_create(); - -/* function name: tsg_lua_exec - * input: tsg_lua_handle L a virtual machine - * char *script script waiting to be executed, which must be script`s content - * size_t *script_len the length of script waiting to be executed - * char *in data waiting to be handled - * size_t in_len the length of data waiting to be handled - * output: char *out out value - * size_t *out_len the length of out value - * input-output: size_t *out_type Requires input of an expected type that is not NIL, the type of the result that will be output after the call is complete - * return value: int successed return 0; failed return error code */ -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); - -/* function name: tsg_lua_exec_file - * input: tsg_lua_handle L a virtual machine - * char *script script waiting to be executed, which mush be script`s path - * char *in data waiting to be handled - * size_t in_len the length of data waiting to be handled - * output: char *out out value - * size_t *out_len the length of out value - * input-output: size_t *out_type Requires input of an expected type that is not NIL, the type of the result that will be output after the call is complete - * return value: int successed return 0; failed return error code */ -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); - -/* function name: tsg_lua_cache_script - * input: tsg_lua_handle L a virtual machine - * char *script script waiting to be cached, which must be script`s content - * size_t *script_len the length of script waiting to be cached - * - * return value: int successed, return a script id which is bigger than 1; failed, return error code */ -int tsg_lua_cache_script(tsg_lua_handle L, const char *script, size_t script_len); - -/* function name: tsg_lua_cache_script_file - * input: tsg_lua_handle L a virtual machine - * char *script script waiting to be cached, which must be script`s path - * size_t *script_len the length of script waiting to be cached - * - * return value: int successed, return a script id which is bigger than 1; failed, return error code */ -int tsg_lua_cache_script_file(tsg_lua_handle L, const char *script); - -/* function name: tsg_unchache_script - * input: tsg_lua_handle L a virtual machine - * size_t script_id a script id - * return value: int successed, return 0; failed, return error code */ -int tsg_lua_uncache_script(tsg_lua_handle L, size_t script_id); - -/* function name: tsg_cache_exec - * input: tsg_lua_handle L a virtual machine - * size_t script_id a script id - * char *in data waiting to be handled - * size_t in_len the length of data waiting to be handled - * output: char *out out value - * size_t *out_len the length of out value - * input-output: size_t *out_type Requires input of an expected type that is not NIL, the type of the result that will be output after the call is complete - * return value: int successed, return 0; failed, return error code */ -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); +const char *lua_get_last_error_string(struct lua_vm *vm); /* function name: tsg_destory_lua * input: tsg_lua_handle L a virtual machine * return value: int successed, return 0; failed, return error code */ -int tsg_destory_lua(tsg_lua_handle L); +int lua_vm_destory(struct lua_vm *vm); + +struct lua_cbinding_params; +struct lua_cbinding_params *lua_cbinding_get_last_params(struct lua_vm *vm); +struct lua_data *lua_cbinging_get_next_para(struct lua_cbinding_params *params); +void lua_cbinding_free_params(struct lua_cbinding_params *params); -int c_pull_param_from_lua(tsg_lua_handle lua, int *argc, lua_arg_t **argv); -int free_param_form_lua(int argc, lua_arg_t *argv); -int c_push_num_into_lua(tsg_lua_handle lua, long num); -int c_push_bool_into_lua(tsg_lua_handle lua, bool flag); -int c_push_nil_into_lua(tsg_lua_handle lua); -int c_push_string_into_lua(tsg_lua_handle lua, const char *str, size_t len); -int c_push_table_into_lua(tsg_lua_handle lua, const char **key_list, const char **value_list, size_t list_len); +int lua_cbinding_return(struct lua_vm *vm, struct lua_data *data); +int lua_cbinding_return_pair(struct lua_vm *vm, struct lua_data *data1, struct lua_data *data2); /* function name: lua_get_userdata * description: get C userdata * input: tsg_lua_handle L a virtual machine * return value: int successed, return 0; failed, return error code */ -void *lua_get_userdata(tsg_lua_handle L); +void *lua_get_exec_userdata(struct lua_vm *vm); -typedef int (*lua_function_ptr)(tsg_lua_handle L); +typedef int (*lua_cbinding_func_ptr)(struct lua_vm *vm); /* function name: lua_register_function * description: Regist a c function to lua * input: tsg_lua_handle L a virtual machine @@ -182,21 +64,28 @@ typedef int (*lua_function_ptr)(tsg_lua_handle L); * input: const char *function_name function name * input: lua_function_ptr function function pointer * return value: int successed, return 0; failed return 1 */ -int lua_register_function(tsg_lua_handle L, const char *function_set, const char *function_name, lua_function_ptr function); +int lua_register_cbinding(struct lua_vm *vm, const char *namespace, const char *lua_func_name, lua_cbinding_func_ptr c_func_ptr); -struct lua_script_context_t; -typedef lua_script_context_t * lua_script_context; +struct lua_context; /* function name: lua_script_context_malloc * input: tsg_lua_handle L * return value: lua_script_context failed, return NULL; successed, return a context*/ -lua_script_context lua_script_context_malloc(tsg_lua_handle L); +struct lua_context *lua_context_create(struct lua_vm *vm, const char *ctx_name); /* function name: lua_script_context_free * input: tsg_lua_handle L * input: lua_script_context context context waiting to free * return value: int successed, return 0; failed, return -1 */ -int lua_script_context_free(tsg_lua_handle L, lua_script_context context); +int lua_context_destroy(struct lua_vm *vm, struct lua_context *ctx); + +/* function name: tsg_lua_cache_script + * input: tsg_lua_handle L a virtual machine + * char *script script waiting to be cached, which must be script`s content + * size_t *script_len the length of script waiting to be cached + * + * return value: int successed, return a script id which is bigger than 1; failed, return error code */ +size_t lua_cache_script(struct lua_vm *vm, const char *script, size_t script_len); /* function name: lua_cache_exec * input: tsg_lua_handle L a virtual machine @@ -207,38 +96,12 @@ int lua_script_context_free(tsg_lua_handle L, lua_script_context context); * size_t timeout_ms Maximum time to run the script.if timeout_ms > 0, jit.off; timeout_ms == 0, jit.on, not expired. * output: struct lua_arg_t *outvalue Requires input of an expected type that is not NIL, the type of the result that will be output after the call is complete * return value: int successed, return 0; failed, return error code */ -int lua_cache_exec(tsg_lua_handle L, size_t script_id, struct lua_data_t in, void *userdata, lua_script_context context, size_t timeout_ms, struct lua_arg_t *outvalue); - -/* function name: lua_exec - * input: tsg_lua_handle L a virtual machine - * struct lua_data_t script script waiting to be executed, which must be script`s content - * struct lua_data_t in data waiting to be handled - * void *userdata can get the userdata with lua_get_userdata(L) - * lua_script_context context Can accessed with lua-name.context in lua script - * size_t timeout_ms Maximum time to run the script.if timeout_ms > 0, jit.off; timeout_ms == 0, jit.on, not expired. - * output: struct lua_arg_t *outvalue Requires input of an expected type that is not NIL, the type of the result that will be output after the call is complete - * return value: int successed, return 0; failed, return error code */ -int lua_exec(tsg_lua_handle L, struct lua_data_t script, struct lua_data_t in, void *userdata, lua_script_context context, size_t timeout_ms, struct lua_arg_t *outvalue); +int lua_cache_exec(struct lua_vm *vm, size_t script_id, struct lua_data *in, void *userdata, struct lua_context *ctx, size_t timeout_ms, struct lua_data *out); -/* function name: lua_exec_file - * input: tsg_lua_handle L a virtual machine - * struct lua_data_t script script waiting to be executed, which must be script`s path - * struct lua_data_t in data waiting to be handled - * void *userdata can get the userdata with lua_get_userdata(L) - * lua_script_context context Can accessed with lua-name.context in lua script - * size_t timeout_ms Maximum time to run the script.if timeout_ms > 0, jit.off; timeout_ms == 0, jit.on, not expired. - * output: struct lua_arg_t *outvalue Requires input of an expected type that is not NIL, the type of the result that will be output after the call is complete - * return value: int successed, return 0; failed, return error code */ -int lua_exec_file(tsg_lua_handle L, const char *script, struct lua_data_t in, void *userdata, lua_script_context context, size_t timeout_ms, struct lua_arg_t *outvalue); - -/* function name: tsg_lua_vm_create - * input: char *name the name of virtual machine, default: "TSG" - * return value: tsg_lua_handle successed,return a virtual machine; failed, return NULL*/ -tsg_lua_handle tsg_lua_vm_create_with_name(const char *name); - -int lua_get_error_code(tsg_lua_handle L); - -int lua_remove_cmd(tsg_lua_handle L, const char *cmd); +/* function name: tsg_unchache_script + * input: tsg_lua_handle L a virtual machine + * size_t script_id a script id + * return value: int successed, return 0; failed, return error code */ +int lua_uncache_script(struct lua_vm *vm, size_t script_id); -int lua_set_script_timeout(tsg_lua_handle L, int second); #endif |
