support get return value for pika_lua.eval

This commit is contained in:
lyon 2023-05-14 10:39:43 +08:00
parent cefa730d35
commit 6f7cf36574
14 changed files with 233 additions and 116 deletions

View File

@ -6,7 +6,7 @@
/* if there is already a lua_State, extern pika_L to it */
lua_State* pika_L = NULL;
void pika_lua___init__(PikaObj* self) {
void _pika_lua___init__(PikaObj* self) {
/* not found exist lua_State, create One */
if (NULL == pika_L) {
pika_L = luaL_newstate(); /* create state */
@ -16,7 +16,7 @@ void pika_lua___init__(PikaObj* self) {
}
}
void pika_lua_eval(PikaObj* self, char* cmd) {
void _pika_lua_eval(PikaObj* self, char* cmd) {
int res = luaL_dostring(pika_L, cmd);
if (LUA_OK != res) {
obj_setErrorCode(self, PIKA_RES_ERR_OPERATION_FAILED);
@ -24,16 +24,16 @@ void pika_lua_eval(PikaObj* self, char* cmd) {
}
}
void pika_lua___exit__(PikaObj* self) {
void _pika_lua___exit__(PikaObj* self) {
/* close the lua_State created */
if (PIKA_TRUE == obj_getInt(self, "needfree")) {
lua_close(pika_L);
}
}
void pika_lua_evalLine(PikaObj* self, char* line) {
void _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);
strsDeinit(&buffs);
}
}

View File

@ -0,0 +1,34 @@
import _pika_lua
"""
## PikaPython lua auto binging module
### Import lua module as a python module
``` python
import pika_lua
# import lua module as a python module
lua_math = pika_lua.require("math")
# get vars from lua module
print(lua_math.pi)
# call lua function
print(lua_math.sin(1))
```
### Eval lua code
``` python
import pika_lua
# eval lua code
print(pika_lua.eval("print(1 + 1)"))
"""
def eval(cmd: str):
return _pika_lua.eval(cmd)
def evalLine(line: str):
return _pika_lua.evalLine(line)

View File

@ -0,0 +1,17 @@
from PikaObj import *
def __init__(): ...
def eval(cmd: str) -> any: ...
def evalLine(line: str) -> any: ...
class LuaTable:
pass
def require(module: str)-> LuaTable: ...
def __del__():...

View File

@ -0,0 +1,36 @@
import _pika_lua
"""
## PikaPython lua auto binging module
### Import lua module as a python module
``` python
import pika_lua
# import lua module as a python module
lua_math = pika_lua.require("math")
# get vars from lua module
print(lua_math.pi)
# call lua function
print(lua_math.sin(1))
```
### Eval lua code
``` python
import pika_lua
# eval lua code
print(pika_lua.eval("print(1 + 1)"))
"""
def eval(cmd: str):
return _pika_lua.eval(cmd)
def evalLine(line: str):
return _pika_lua.evalLine(line)
def require(module: str):
return _pika_lua.require(module)

View File

@ -1,13 +0,0 @@
from PikaObj import *
def __init__(): ...
def eval(cmd: str): ...
def evalLine(line: str): ...
def __exit__(): ...

View File

@ -0,0 +1,84 @@
#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* g_pika_L = NULL;
void _pika_lua___init__(PikaObj* self) {
/* not found exist lua_State, create One */
if (NULL == g_pika_L) {
g_pika_L = luaL_newstate(); /* create state */
luaL_openlibs(g_pika_L);
obj_setInt(self, "needfree", PIKA_TRUE);
pika_debug("lua luached!\r\n");
}
}
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);
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(g_pika_L);
}
}
Arg* _pika_lua_evalLine(PikaObj* self, char* line) {
Args buffs = {0};
char* line_with_end = strsAppend(&buffs, line, "\n");
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

@ -1,39 +0,0 @@
#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;
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);
obj_setInt(self, "needfree", PIKA_TRUE);
__platform_printf("lua luached!\r\n");
}
}
void pika_lua_eval(PikaObj* self, char* cmd) {
int res = luaL_dostring(pika_L, cmd);
if (LUA_OK != res) {
obj_setErrorCode(self, PIKA_RES_ERR_OPERATION_FAILED);
obj_setSysOut(self, "Error: Lua dostring failed.\r\n");
}
}
void pika_lua___exit__(PikaObj* self) {
/* close the lua_State created */
if (PIKA_TRUE == obj_getInt(self, "needfree")) {
lua_close(pika_L);
}
}
void 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);
strsDeinit(&buffs);
}

View File

@ -695,10 +695,6 @@ PikaObj* newRootObj(char* name, NewFun newObjFun) {
signal(SIGHUP, signal_handler); // 捕获 SIGHUP 信号
signal(SIGSEGV, signal_handler); // 捕获 SIGHUP 信号
signal(SIGABRT, signal_handler);
signal(SIGQUIT, signal_handler);
signal(SIGKILL, signal_handler);
signal(SIGTRAP, signal_handler);
signal(SIGHUP, signal_handler);
enable_raw_mode();
#endif
PikaObj* newObj = newNormalObj(newObjFun);
@ -2427,10 +2423,6 @@ PikaObj* obj_linkLibrary(PikaObj* self, uint8_t* library_bytes) {
void obj_printModules(PikaObj* self) {
LibObj* lib = obj_getObj(self, "@lib");
if (lib == NULL) {
pika_platform_printf("Error: Not found LibObj, please execute obj_linkLibrary()\r\n");
return;
}
pika_platform_printf(arg_getStr((Arg*)g_PikaObjState.helpModulesCmodule));
LibObj_printModules(lib);
}

View File

@ -203,9 +203,7 @@ static enum StmtType Lexer_matchStmtType(char* right) {
TOKEN_literal == cs.token1.type ||
strEqu(cs.token1.pyload, "]") ||
strEqu(cs.token1.pyload, ")")) {
/* keep the last one of the chain or slice */
is_get_slice = PIKA_TRUE;
is_get_chain = PIKA_FALSE;
goto iter_continue;
}
/* ( <,> | <=> ) + <[> */
@ -226,9 +224,7 @@ static enum StmtType Lexer_matchStmtType(char* right) {
if (strIsStartWith(cs.token1.pyload, ".")) {
if (cs.iter_index != 1) {
/* keep the last one of the chain or slice */
is_get_chain = PIKA_TRUE;
is_get_slice = PIKA_FALSE;
goto iter_continue;
}
}
@ -274,14 +270,14 @@ static enum StmtType Lexer_matchStmtType(char* right) {
stmtType = STMT_operator;
goto exit;
}
if (is_get_slice) {
stmtType = STMT_slice;
goto exit;
}
if (is_get_chain) {
stmtType = STMT_chain;
goto exit;
}
if (is_get_slice) {
stmtType = STMT_slice;
goto exit;
}
if (is_get_list) {
stmtType = STMT_list;
goto exit;
@ -1759,10 +1755,10 @@ AST* AST_parseStmt(AST* ast, char* stmt) {
/* solve method chain */
if (STMT_chain == stmtType) {
char* sHost = strsCopy(&buffs, right);
char* sMethodStmt = Parser_popLastSubStmt(&buffs, &sHost, ".");
AST_parseSubStmt(ast, sHost);
AST_parseStmt(ast, sMethodStmt);
char* stmt = strsCopy(&buffs, right);
char* lastStmt = Parser_popLastSubStmt(&buffs, &stmt, ".");
AST_parseSubStmt(ast, stmt);
AST_parseStmt(ast, lastStmt);
goto exit;
}

View File

@ -178,10 +178,7 @@ static int _no_buff_vprintf(char* fmt, va_list args) {
if (*fmt == '%') {
++fmt;
if (*fmt == 's') {
char* str = va_arg(args, char*);
if (str == NULL) {
str = "(null)";
}
const char* str = va_arg(args, const char*);
int len = strlen(str);
written += len;
for (int i = 0; i < len; i++) {

View File

@ -806,14 +806,6 @@ static Arg* VM_instruction_handler_SLC(PikaObj* self,
if (n_input == 3) {
Arg* end = stack_popArg_alloc(&vm->stack);
Arg* start = stack_popArg_alloc(&vm->stack);
if (arg_getType(start) != ARG_TYPE_INT ||
arg_getType(end) != ARG_TYPE_INT) {
VMState_setErrorCode(vm, PIKA_RES_ERR_INVALID_PARAM);
pika_platform_printf("TypeError: slice indices must be integers\n");
arg_deinit(end);
arg_deinit(start);
return arg_newNull();
}
Arg* obj = stack_popArg_alloc(&vm->stack);
Arg* res = _vm_slice(vm, self, end, obj, start, 1);
arg_deinit(end);
@ -999,16 +991,14 @@ static Arg* VM_instruction_handler_REF(PikaObj* self,
aRes = _obj_getProp(oHost, arg_name);
}
/* find res in globals */
if (arg_path == arg_name) {
if (NULL == aRes) {
aRes = args_getArg(vm->globals->list, arg_name);
}
/* find res in globlas */
if (NULL == aRes) {
aRes = args_getArg(vm->globals->list, arg_name);
}
/* find res in globals prop */
if (NULL == aRes) {
aRes = _obj_getProp(vm->globals, arg_name);
}
/* find res in globals prop */
if (NULL == aRes) {
aRes = _obj_getProp(vm->globals, arg_name);
}
/* proxy */
@ -1788,7 +1778,6 @@ static Arg* VM_instruction_handler_RUN(PikaObj* self,
VMParameters* oSublocals = NULL;
VMParameters* oSublocalsInit = NULL;
char* sRunPath = data;
char* sArgName = NULL;
PikaObj* oMethodHost = NULL;
PikaObj* oThis = NULL;
Arg* aMethod = NULL;
@ -1802,10 +1791,6 @@ static Arg* VM_instruction_handler_RUN(PikaObj* self,
.try_result = TRY_RESULT_NONE};
pika_assert(NULL != vm->run_state);
if (NULL != sRunPath) {
sArgName = strPointToLastToken(sRunPath, '.');
}
/* inhert */
if (vm->pc - 2 * (int)instructUnit_getSize() >= 0) {
if (CLS == VMstate_getInstructWithOffset(
@ -1830,7 +1815,6 @@ static Arg* VM_instruction_handler_RUN(PikaObj* self,
/* support for super() */
if (strEqu(sRunPath, "super")) {
sRunPath = _find_super_class_name(vm);
sArgName = sRunPath;
vm->in_super = PIKA_TRUE;
vm->super_invoke_deepth = VMState_getInvokeDeepthNow(vm);
bSkipInit = PIKA_TRUE;
@ -1919,18 +1903,16 @@ static Arg* VM_instruction_handler_RUN(PikaObj* self,
aMethod = obj_getMethodArg_noalloc(oMethodHost, sRunPath, &arg_reg1);
}
if (sArgName == sRunPath) {
/* get method in locals */
if (NULL == aMethod) {
aMethod = obj_getMethodArg_noalloc(vm->locals, sRunPath, &arg_reg1);
}
/* get method in global */
if (NULL == aMethod) {
aMethod =
obj_getMethodArg_noalloc(vm->globals, sRunPath, &arg_reg1);
if (aMethod != NULL) {
oThis = vm->globals;
}
/* get method in locals */
if (NULL == aMethod) {
aMethod = obj_getMethodArg_noalloc(vm->locals, sRunPath, &arg_reg1);
}
/* get method in global */
if (NULL == aMethod) {
aMethod = obj_getMethodArg_noalloc(vm->globals, sRunPath, &arg_reg1);
if (aMethod != NULL) {
oThis = vm->globals;
}
}

8
test/lua-test.cpp Normal file
View File

@ -0,0 +1,8 @@
#include "test_common.h"
TEST_START
#if !PIKA_NANO_ENABLE
TEST_RUN_SINGLE_FILE(lua, eval, "test/python/pika_lua/eval.py")
#endif
TEST_END

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