add pika_lua

This commit is contained in:
lyon 2023-05-14 10:43:02 +08:00
parent 6f7cf36574
commit 67f015a628
5 changed files with 99 additions and 21 deletions

23
examples/pika_lua/eval.py Normal file
View File

@ -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')

View File

@ -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 状态机,释放所有关联的资源
}

View File

@ -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__():...

View File

@ -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)

View File

@ -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):