diff options
Diffstat (limited to 'src/lua_plugin_data.c')
| -rw-r--r-- | src/lua_plugin_data.c | 78 |
1 files changed, 41 insertions, 37 deletions
diff --git a/src/lua_plugin_data.c b/src/lua_plugin_data.c index fb6d466..128676f 100644 --- a/src/lua_plugin_data.c +++ b/src/lua_plugin_data.c @@ -16,6 +16,9 @@ * struct lua_context * lua_context_new; * int lua_context_push_stack; * void lua_context_free; + * + * 08-12 + * 1. 修改部分函数返回值, 使用统一的错误码, 方便统一处理 ************************************************************************/ #include "lua_plugin_manage_internal.h" @@ -24,9 +27,7 @@ * Input: | lua_State * | state | 入栈数据使用的状态机 * | struct lua_cdata * | cdata | 需要入栈的数据 * Output: - * Return: | -1 | 参数错误 - * | 1 | 入栈数据类型有误 - * | 0 | 入栈成功 + * Return: enum LUA_PLUGIN_RETURN * Description: 将一个数据元素入栈 */ int lua_cdata_push_stack( @@ -34,39 +35,42 @@ int lua_cdata_push_stack( struct lua_cdata *cdata) { if (__glibc_unlikely(!state || !cdata)) - return -1; + return PARAM_ERR; switch (cdata->cdata_type) { + /* 不同的数据类型入栈方式不同 */ case DATATYPE_NIL: lua_pushnil(state); - return 0; + return SUCCESS; case DATATYPE_BOOL: lua_pushboolean(state, cdata->cdata_bool); - return 0; + return SUCCESS; case DATATYPE_INT: lua_pushinteger(state, cdata->cdata_int); - return 0; + return SUCCESS; case DATATYPE_NUM: lua_pushnumber(state, cdata->cdata_num); - return 0; + return SUCCESS; case DATATYPE_STRING: - lua_pushstring(state, cdata->cdata_string); - return 0; + if (lua_pushstring(state, cdata->cdata_string) == NULL) + return DATA_PUSHSTACK_ERR; + return SUCCESS; case DATATYPE_TABLE: lua_rawgeti(state, LUA_REGISTRYINDEX, cdata->cdata_table); - return 0; + return SUCCESS; case DATATYPE_POINTER: lua_pushlightuserdata(state, (void *)cdata->cdata_pointer); - return 0; + return SUCCESS; case DATATYPE_CONTEXT: - lua_context_push_stack(cdata->cdata_context); - return 0; + if (lua_context_push_stack(state, cdata->cdata_context)) + return DATA_PUSHSTACK_ERR; + return SUCCESS; default: LOGERROR("can't recorgnize data type %d", cdata->cdata_type); } - return 1; + return DATA_TYPE_UNKNOWN; } /* @@ -74,9 +78,7 @@ int lua_cdata_push_stack( * Input: | lua_State * | state | 出栈数据使用的状态机 * | struct lua_cdata * | cdata | 保存出栈的元素 * Output: - * Return: | -1 | 参数错误 - * | -2 | 出栈数据类型有误 - * | 0 | 出栈成功 + * Return: enum LUA_PLUGIN_RETURN * Description: 将一个数据元素出栈, 出栈过程中无法出栈context与table类型 * TODO: 扩展类型, 支持更多数据类型 */ @@ -85,9 +87,9 @@ int lua_cdata_pop_stack( struct lua_cdata *cdata) { if (__glibc_unlikely(!state || !cdata)) - return -1; + return PARAM_ERR; if (!lua_gettop(state)) - return 0; + return DATA_POPSTACK_NODATA; switch (lua_type(state, -1)) { @@ -117,13 +119,13 @@ int lua_cdata_pop_stack( cdata->cdata_pointer = (void *)lua_topointer(state, -1); break; default: - /* 其他数据类型之后处理 */ + /* TODO: 其他数据类型之后处理 */ LOGERROR("other lua type can't pop out, %d", lua_type(state, -1)); - return -2; + return DATA_TYPE_UNKNOWN; } lua_pop(state, 1); - return 0; + return SUCCESS; } /* @@ -140,7 +142,6 @@ void lua_cdata_destory(struct lua_cdata *cdata) return; if (cdata->cdata_type == DATATYPE_STRING && cdata->cdata_string) free(cdata->cdata_string); - return; } @@ -159,11 +160,12 @@ struct lua_context *lua_context_new(lua_State *state) lua_newtable(state); int ref_id = luaL_ref(state, LUA_REGISTRYINDEX); +#ifdef LUAPLUGIN_BASIC_UNITTEST + LOGDEBUG("create new context, ref id is %d\n", ref_id); +#endif lua_settop(state, 0); if (ref_id == LUA_REFNIL) - { return NULL; - } struct lua_context *new_context = (struct lua_context *)calloc(1, sizeof(struct lua_context)); if (__glibc_unlikely(!new_context)) @@ -171,40 +173,42 @@ struct lua_context *lua_context_new(lua_State *state) luaL_unref(state, LUA_REGISTRYINDEX, ref_id); return NULL; } - new_context->context_state = state; + // new_context->context_state = state; new_context->context_ref_id = ref_id; return new_context; } /* * Function: lua_context_push_stack - * Input: | struct lua_context * | context | 待入栈的context结构 + * Input: | lua_State * | state | 将该context入栈 + * | struct lua_context * | context | 待入栈的context结构 * Output: * Return: | 0 | 入栈成功 * | -1 | 参数错误 * Description: 将一个context入栈, 实际入栈流程与table一致 */ -int lua_context_push_stack(struct lua_context *context) +int lua_context_push_stack(lua_State *state, struct lua_context *context) { - if (luai_unlikely(!context)) - return -1; + if (luai_unlikely(!state || !context)) + return PARAM_ERR; - lua_rawgeti(context->context_state, LUA_REGISTRYINDEX, context->context_ref_id); - return 0; + lua_rawgeti(state, LUA_REGISTRYINDEX, context->context_ref_id); + return SUCCESS; } /* * Function: lua_context_free - * Input: | struct lua_context * | context | 释放一个context + * Input: | lua_State * | state | 在该状态机中删除context + * | struct lua_context * | context | 释放一个context * Output: * Return: * Description: 释放一个context */ -void lua_context_free(struct lua_context *context) +void lua_context_free(lua_State *state, struct lua_context *context) { - if (__glibc_unlikely(!context)) + if (__glibc_unlikely(!state || !context)) return; - luaL_unref(context->context_state, LUA_REGISTRYINDEX, context->context_ref_id); + luaL_unref(state, LUA_REGISTRYINDEX, context->context_ref_id); free(context); return; }
\ No newline at end of file |
