From 67f015a62892f886a46aeb5a6e8f1344317431c9 Mon Sep 17 00:00:00 2001 From: lyon Date: Sun, 14 May 2023 10:43:02 +0800 Subject: [PATCH] add pika_lua --- examples/pika_lua/eval.py | 23 ++++++++ package/pika_lua/_pika_lua.c | 69 +++++++++++++++++++---- package/pika_lua/_pika_lua.pyi | 10 +++- package/pika_lua/pika_lua.py | 10 +++- port/linux/package/pikascript/pika_lua.py | 8 ++- 5 files changed, 99 insertions(+), 21 deletions(-) create mode 100644 examples/pika_lua/eval.py diff --git a/examples/pika_lua/eval.py b/examples/pika_lua/eval.py new file mode 100644 index 000000000..752fd9695 --- /dev/null +++ b/examples/pika_lua/eval.py @@ -0,0 +1,23 @@ +import pika_lua + +# 测试数字 +assert pika_lua.eval('return 1 + 1') == 2 + +# 测试字符串 +assert pika_lua.eval('return "hello"') == "hello" + +# 测试布尔值 +assert pika_lua.eval('return true') == True + +# 测试nil +assert pika_lua.eval('return nil') == None + +# 测试变量赋值 +pika_lua.eval('x = 10') +assert pika_lua.eval('return x') == 10 + +# 测试函数调用 +pika_lua.eval('function add(a, b) return a + b end') +assert pika_lua.eval('return add(1, 2)') == 3 + +print('PASS') diff --git a/package/pika_lua/_pika_lua.c b/package/pika_lua/_pika_lua.c index 201ac9165..5bd5c12cf 100644 --- a/package/pika_lua/_pika_lua.c +++ b/package/pika_lua/_pika_lua.c @@ -1,39 +1,84 @@ -#include "pika_lua.h" +#include "_pika_lua.h" #include "dataStrs.h" #include "lauxlib.h" #include "lualib.h" /* if there is already a lua_State, extern pika_L to it */ -lua_State* pika_L = NULL; +lua_State* g_pika_L = NULL; void _pika_lua___init__(PikaObj* self) { /* not found exist lua_State, create One */ - if (NULL == pika_L) { - pika_L = luaL_newstate(); /* create state */ - luaL_openlibs(pika_L); + if (NULL == g_pika_L) { + g_pika_L = luaL_newstate(); /* create state */ + luaL_openlibs(g_pika_L); obj_setInt(self, "needfree", PIKA_TRUE); - __platform_printf("lua luached!\r\n"); + pika_debug("lua luached!\r\n"); } } -void _pika_lua_eval(PikaObj* self, char* cmd) { - int res = luaL_dostring(pika_L, cmd); +Arg* _pika_lua_eval(PikaObj* self, char* cmd) { + Args buffs = {0}; + int res = luaL_dostring(g_pika_L, cmd); + Arg* ret = NULL; if (LUA_OK != res) { obj_setErrorCode(self, PIKA_RES_ERR_OPERATION_FAILED); - obj_setSysOut(self, "Error: Lua dostring failed.\r\n"); + pika_platform_printf("Error: Lua dostring failed: \r\n> %s\r\n", + lua_tostring(g_pika_L, -1)); + ret = NULL; + goto __exit; } + if (!lua_gettop(g_pika_L)) { + ret = NULL; + goto __exit; + } + if (lua_isinteger(g_pika_L, -1)) { + ret = arg_newInt(lua_tointeger(g_pika_L, -1)); + goto __exit; + } + if (lua_isnumber(g_pika_L, -1)) { + ret = arg_newFloat(lua_tonumber(g_pika_L, -1)); + goto __exit; + } + if (lua_isstring(g_pika_L, -1)) { + ret = arg_newStr((char*)lua_tostring(g_pika_L, -1)); + goto __exit; + } + if (lua_isboolean(g_pika_L, -1)) { + ret = arg_newBool(lua_toboolean(g_pika_L, -1)); + goto __exit; + } + if (lua_isnil(g_pika_L, -1) || lua_isnoneornil(g_pika_L, -1)) { + ret = arg_newNull(); + goto __exit; + } +__exit: + if (NULL != ret) { + lua_pop(g_pika_L, 1); + } + strsDeinit(&buffs); + return ret; } void _pika_lua___exit__(PikaObj* self) { /* close the lua_State created */ if (PIKA_TRUE == obj_getInt(self, "needfree")) { - lua_close(pika_L); + lua_close(g_pika_L); } } -void _pika_lua_evalLine(PikaObj* self, char* line) { +Arg* _pika_lua_evalLine(PikaObj* self, char* line) { Args buffs = {0}; char* line_with_end = strsAppend(&buffs, line, "\n"); - pika_lua_eval(self, line_with_end); + Arg* ret = _pika_lua_eval(self, line_with_end); + goto __exit; +__exit: strsDeinit(&buffs); + return ret; +} + +PikaObj* _pika_lua_require(PikaObj* self, char* module) {} + +void _pika_lua___del__(PikaObj* self){ + pika_debug("lua close!\r\n"); + lua_close(g_pika_L); // 关闭 Lua 状态机,释放所有关联的资源 } diff --git a/package/pika_lua/_pika_lua.pyi b/package/pika_lua/_pika_lua.pyi index d45309180..a9bcfb9ae 100644 --- a/package/pika_lua/_pika_lua.pyi +++ b/package/pika_lua/_pika_lua.pyi @@ -4,10 +4,14 @@ from PikaObj import * def __init__(): ... -def eval(cmd: str): ... +def eval(cmd: str) -> any: ... -def evalLine(line: str): ... +def evalLine(line: str) -> any: ... +class LuaTable: + pass -def __exit__(): ... +def require(module: str)-> LuaTable: ... + +def __del__():... diff --git a/package/pika_lua/pika_lua.py b/package/pika_lua/pika_lua.py index 97b84c662..01cf1d409 100644 --- a/package/pika_lua/pika_lua.py +++ b/package/pika_lua/pika_lua.py @@ -1,9 +1,10 @@ import _pika_lua """ -## PikaPython lua auto binging module +PikaPython lua auto binging module + +- Import lua module as a python module -### Import lua module as a python module ``` python import pika_lua @@ -17,13 +18,14 @@ print(lua_math.pi) print(lua_math.sin(1)) ``` -### Eval lua code +- Eval lua code ``` python import pika_lua # eval lua code print(pika_lua.eval("print(1 + 1)")) +``` """ def eval(cmd: str): @@ -32,3 +34,5 @@ def eval(cmd: str): def evalLine(line: str): return _pika_lua.evalLine(line) +def require(module: str): + return _pika_lua.require(module) diff --git a/port/linux/package/pikascript/pika_lua.py b/port/linux/package/pikascript/pika_lua.py index ebffc3f27..01cf1d409 100644 --- a/port/linux/package/pikascript/pika_lua.py +++ b/port/linux/package/pikascript/pika_lua.py @@ -1,9 +1,10 @@ import _pika_lua """ -## PikaPython lua auto binging module +PikaPython lua auto binging module + +- Import lua module as a python module -### Import lua module as a python module ``` python import pika_lua @@ -17,13 +18,14 @@ print(lua_math.pi) print(lua_math.sin(1)) ``` -### Eval lua code +- Eval lua code ``` python import pika_lua # eval lua code print(pika_lua.eval("print(1 + 1)")) +``` """ def eval(cmd: str):