mirror of
https://gitee.com/Lyon1998/pikapython.git
synced 2025-01-15 17:02:53 +08:00
support method_proxy for pika_lua
This commit is contained in:
parent
fc7c881ce5
commit
bb21c7b7fe
@ -28,16 +28,16 @@ Arg* PikaStdLib_SysObj_type(PikaObj* self, Arg* arg) {
|
||||
}
|
||||
ArgType type = arg_getType(arg);
|
||||
if (ARG_TYPE_INT == type) {
|
||||
return arg_copy(obj_getMethodArg(self, "int"));
|
||||
return arg_copy(obj_getMethodArgWithFullPath(self, "int"));
|
||||
}
|
||||
if (ARG_TYPE_FLOAT == type) {
|
||||
return arg_copy(obj_getMethodArg(self, "float"));
|
||||
return arg_copy(obj_getMethodArgWithFullPath(self, "float"));
|
||||
}
|
||||
if (ARG_TYPE_STRING == type) {
|
||||
return arg_copy(obj_getMethodArg(self, "str"));
|
||||
return arg_copy(obj_getMethodArgWithFullPath(self, "str"));
|
||||
}
|
||||
if (ARG_TYPE_BOOL == type) {
|
||||
return arg_copy(obj_getMethodArg(self, "bool"));
|
||||
return arg_copy(obj_getMethodArgWithFullPath(self, "bool"));
|
||||
}
|
||||
if (argType_isObject(type)) {
|
||||
PikaObj* obj = arg_getPtr(arg);
|
||||
@ -45,17 +45,17 @@ Arg* PikaStdLib_SysObj_type(PikaObj* self, Arg* arg) {
|
||||
PikaObj* New_PikaStdData_List(Args * args);
|
||||
/* list */
|
||||
if (clsptr == New_PikaStdData_List) {
|
||||
return arg_copy(obj_getMethodArg(self, "list"));
|
||||
return arg_copy(obj_getMethodArgWithFullPath(self, "list"));
|
||||
}
|
||||
/* dict */
|
||||
PikaObj* New_PikaStdData_Dict(Args * args);
|
||||
if (clsptr == New_PikaStdData_Dict) {
|
||||
return arg_copy(obj_getMethodArg(self, "dict"));
|
||||
return arg_copy(obj_getMethodArgWithFullPath(self, "dict"));
|
||||
}
|
||||
/* tuple */
|
||||
PikaObj* New_PikaStdData_Tuple(Args * args);
|
||||
if (clsptr == New_PikaStdData_Tuple) {
|
||||
return arg_copy(obj_getMethodArg(self, "tuple"));
|
||||
return arg_copy(obj_getMethodArgWithFullPath(self, "tuple"));
|
||||
}
|
||||
#if PIKA_TYPE_FULL_FEATURE_ENABLE
|
||||
Arg* aMethod = obj_getArg(obj, "__class__");
|
||||
@ -304,7 +304,7 @@ int PikaStdLib_SysObj_len(PikaObj* self, Arg* arg) {
|
||||
|
||||
if (arg_isObject(arg)) {
|
||||
PikaObj* arg_obj = arg_getPtr(arg);
|
||||
Arg* method_arg = obj_getMethodArg(arg_obj, "__len__");
|
||||
Arg* method_arg = obj_getMethodArgWithFullPath(arg_obj, "__len__");
|
||||
if (NULL != method_arg) {
|
||||
arg_deinit(method_arg);
|
||||
obj_removeArg(arg_obj, "@res_len");
|
||||
@ -591,7 +591,7 @@ Arg* PikaStdLib_SysObj_getattr(PikaObj* self, PikaObj* obj, char* name) {
|
||||
}
|
||||
Arg* arg = obj_getArg(obj, name);
|
||||
if (NULL == arg) {
|
||||
arg = obj_getMethodArg(obj, name);
|
||||
arg = obj_getMethodArgWithFullPath(obj, name);
|
||||
}
|
||||
if (NULL != arg) {
|
||||
res = arg_copy(arg);
|
||||
@ -627,7 +627,7 @@ int PikaStdLib_SysObj_hasattr(PikaObj* self, PikaObj* obj, char* name) {
|
||||
if (obj_isArgExist(obj, name)) {
|
||||
return 1;
|
||||
}
|
||||
Arg* method = obj_getMethodArg(obj, name);
|
||||
Arg* method = obj_getMethodArgWithFullPath(obj, name);
|
||||
if (NULL != method) {
|
||||
arg_deinit(method);
|
||||
return 1;
|
||||
|
@ -3,6 +3,10 @@
|
||||
#include "lauxlib.h"
|
||||
#include "lualib.h"
|
||||
|
||||
#if !PIKASCRIPT_VERSION_REQUIRE_MINIMUN(1, 12, 3)
|
||||
#error "PikaScript version require minimum 1.12.3"
|
||||
#endif
|
||||
|
||||
/* if there is already a lua_State, extern pika_L to it */
|
||||
lua_State* g_pika_L = NULL;
|
||||
|
||||
|
@ -28,34 +28,48 @@ 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 getVar(name: str):
|
||||
# print("getVar: %s" % name)
|
||||
return eval("return %s" % name)
|
||||
|
||||
|
||||
def setVar(name: str, value):
|
||||
# print("setVar: %s = %s" % (name, value))
|
||||
eval("%s = %s" % (name, value))
|
||||
|
||||
|
||||
class LuaModuleProxy:
|
||||
_name:str = ''
|
||||
_name: str = ''
|
||||
|
||||
def __init__(self, name: str):
|
||||
self._name = name
|
||||
|
||||
|
||||
def __getattr__(self, name: str):
|
||||
return getVar("%s.%s" % (self._name, name))
|
||||
|
||||
|
||||
def __setattr__(self, name: str, value) -> None:
|
||||
if (name == "_name"):
|
||||
setattr(self, name, value)
|
||||
else:
|
||||
setVar("%s.%s" % (self._name, name), value)
|
||||
|
||||
|
||||
def __proxy__(self, methodName, *args):
|
||||
strArgs = []
|
||||
for arg in args:
|
||||
strArgs.append(str(arg))
|
||||
|
||||
return eval("return %s.%s(%s)" % (self._name, methodName, ",".join(strArgs)))
|
||||
|
||||
|
||||
def require(module: str) -> LuaModuleProxy:
|
||||
try:
|
||||
eval("require('%s')" % module)
|
||||
|
3
port/linux/.vscode/launch.json
vendored
3
port/linux/.vscode/launch.json
vendored
@ -21,7 +21,8 @@
|
||||
// "--gtest_filter=parser.tuple_single"
|
||||
// "--gtest_filter=parser.split_slice"
|
||||
// "--gtest_filter=vm.var_global_run"
|
||||
"--gtest_filter=vm.var_global_module"
|
||||
// "--gtest_filter=modbus.rtu_master"
|
||||
"--gtest_filter=proxy.proxy3"
|
||||
],
|
||||
"stopAtEntry": false,
|
||||
"cwd": "${workspaceFolder}",
|
||||
|
@ -28,34 +28,48 @@ 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 getVar(name: str):
|
||||
# print("getVar: %s" % name)
|
||||
return eval("return %s" % name)
|
||||
|
||||
|
||||
def setVar(name: str, value):
|
||||
# print("setVar: %s = %s" % (name, value))
|
||||
eval("%s = %s" % (name, value))
|
||||
|
||||
|
||||
class LuaModuleProxy:
|
||||
_name:str = ''
|
||||
_name: str = ''
|
||||
|
||||
def __init__(self, name: str):
|
||||
self._name = name
|
||||
|
||||
|
||||
def __getattr__(self, name: str):
|
||||
return getVar("%s.%s" % (self._name, name))
|
||||
|
||||
|
||||
def __setattr__(self, name: str, value) -> None:
|
||||
if (name == "_name"):
|
||||
setattr(self, name, value)
|
||||
else:
|
||||
setVar("%s.%s" % (self._name, name), value)
|
||||
|
||||
|
||||
def __proxy__(self, methodName, *args):
|
||||
strArgs = []
|
||||
for arg in args:
|
||||
strArgs.append(str(arg))
|
||||
|
||||
return eval("return %s.%s(%s)" % (self._name, methodName, ",".join(strArgs)))
|
||||
|
||||
|
||||
def require(module: str) -> LuaModuleProxy:
|
||||
try:
|
||||
eval("require('%s')" % module)
|
||||
|
@ -28,16 +28,16 @@ Arg* PikaStdLib_SysObj_type(PikaObj* self, Arg* arg) {
|
||||
}
|
||||
ArgType type = arg_getType(arg);
|
||||
if (ARG_TYPE_INT == type) {
|
||||
return arg_copy(obj_getMethodArg(self, "int"));
|
||||
return arg_copy(obj_getMethodArgWithFullPath(self, "int"));
|
||||
}
|
||||
if (ARG_TYPE_FLOAT == type) {
|
||||
return arg_copy(obj_getMethodArg(self, "float"));
|
||||
return arg_copy(obj_getMethodArgWithFullPath(self, "float"));
|
||||
}
|
||||
if (ARG_TYPE_STRING == type) {
|
||||
return arg_copy(obj_getMethodArg(self, "str"));
|
||||
return arg_copy(obj_getMethodArgWithFullPath(self, "str"));
|
||||
}
|
||||
if (ARG_TYPE_BOOL == type) {
|
||||
return arg_copy(obj_getMethodArg(self, "bool"));
|
||||
return arg_copy(obj_getMethodArgWithFullPath(self, "bool"));
|
||||
}
|
||||
if (argType_isObject(type)) {
|
||||
PikaObj* obj = arg_getPtr(arg);
|
||||
@ -45,17 +45,17 @@ Arg* PikaStdLib_SysObj_type(PikaObj* self, Arg* arg) {
|
||||
PikaObj* New_PikaStdData_List(Args * args);
|
||||
/* list */
|
||||
if (clsptr == New_PikaStdData_List) {
|
||||
return arg_copy(obj_getMethodArg(self, "list"));
|
||||
return arg_copy(obj_getMethodArgWithFullPath(self, "list"));
|
||||
}
|
||||
/* dict */
|
||||
PikaObj* New_PikaStdData_Dict(Args * args);
|
||||
if (clsptr == New_PikaStdData_Dict) {
|
||||
return arg_copy(obj_getMethodArg(self, "dict"));
|
||||
return arg_copy(obj_getMethodArgWithFullPath(self, "dict"));
|
||||
}
|
||||
/* tuple */
|
||||
PikaObj* New_PikaStdData_Tuple(Args * args);
|
||||
if (clsptr == New_PikaStdData_Tuple) {
|
||||
return arg_copy(obj_getMethodArg(self, "tuple"));
|
||||
return arg_copy(obj_getMethodArgWithFullPath(self, "tuple"));
|
||||
}
|
||||
#if PIKA_TYPE_FULL_FEATURE_ENABLE
|
||||
Arg* aMethod = obj_getArg(obj, "__class__");
|
||||
@ -304,7 +304,7 @@ int PikaStdLib_SysObj_len(PikaObj* self, Arg* arg) {
|
||||
|
||||
if (arg_isObject(arg)) {
|
||||
PikaObj* arg_obj = arg_getPtr(arg);
|
||||
Arg* method_arg = obj_getMethodArg(arg_obj, "__len__");
|
||||
Arg* method_arg = obj_getMethodArgWithFullPath(arg_obj, "__len__");
|
||||
if (NULL != method_arg) {
|
||||
arg_deinit(method_arg);
|
||||
obj_removeArg(arg_obj, "@res_len");
|
||||
@ -591,7 +591,7 @@ Arg* PikaStdLib_SysObj_getattr(PikaObj* self, PikaObj* obj, char* name) {
|
||||
}
|
||||
Arg* arg = obj_getArg(obj, name);
|
||||
if (NULL == arg) {
|
||||
arg = obj_getMethodArg(obj, name);
|
||||
arg = obj_getMethodArgWithFullPath(obj, name);
|
||||
}
|
||||
if (NULL != arg) {
|
||||
res = arg_copy(arg);
|
||||
@ -627,7 +627,7 @@ int PikaStdLib_SysObj_hasattr(PikaObj* self, PikaObj* obj, char* name) {
|
||||
if (obj_isArgExist(obj, name)) {
|
||||
return 1;
|
||||
}
|
||||
Arg* method = obj_getMethodArg(obj, name);
|
||||
Arg* method = obj_getMethodArgWithFullPath(obj, name);
|
||||
if (NULL != method) {
|
||||
arg_deinit(method);
|
||||
return 1;
|
||||
|
@ -3,6 +3,10 @@
|
||||
#include "lauxlib.h"
|
||||
#include "lualib.h"
|
||||
|
||||
#if !PIKASCRIPT_VERSION_REQUIRE_MINIMUN(1, 12, 3)
|
||||
#error "PikaScript version require minimum 1.12.3"
|
||||
#endif
|
||||
|
||||
/* if there is already a lua_State, extern pika_L to it */
|
||||
lua_State* g_pika_L = NULL;
|
||||
|
||||
|
@ -1,3 +1,3 @@
|
||||
MajorVersion = "1"
|
||||
MinorVersion = "12"
|
||||
MicroVersion = "2"
|
||||
MicroVersion = "3"
|
||||
|
@ -177,7 +177,7 @@ int32_t obj_deinit(PikaObj* self) {
|
||||
#if PIKA_KERNAL_DEBUG_ENABLE
|
||||
self->isAlive = PIKA_FALSE;
|
||||
#endif
|
||||
Arg* del = obj_getMethodArg(self, "__del__");
|
||||
Arg* del = obj_getMethodArgWithFullPath(self, "__del__");
|
||||
if (NULL != del) {
|
||||
const uint8_t bytes[] = {
|
||||
0x04, 0x00, 0x00, 0x00, /* instruct array size */
|
||||
@ -562,9 +562,8 @@ exit:
|
||||
return method;
|
||||
}
|
||||
|
||||
Arg* _obj_getMethodArg(PikaObj* obj, char* methodPath, Arg* arg_reg) {
|
||||
Arg* _obj_getMethodArg(PikaObj* obj, char* methodName, Arg* arg_reg) {
|
||||
Arg* aMethod = NULL;
|
||||
char* methodName = strPointToLastToken(methodPath, '.');
|
||||
aMethod = obj_getArg(obj, methodName);
|
||||
if (NULL != aMethod) {
|
||||
aMethod = arg_copy_noalloc(aMethod, arg_reg);
|
||||
@ -575,12 +574,29 @@ exit:
|
||||
return aMethod;
|
||||
}
|
||||
|
||||
Arg* obj_getMethodArg(PikaObj* obj, char* methodPath) {
|
||||
return _obj_getMethodArg(obj, methodPath, NULL);
|
||||
Arg* _obj_getMethodArgWithFullPath(PikaObj* obj,
|
||||
char* methodPath,
|
||||
Arg* arg_reg) {
|
||||
char* methodName = strPointToLastToken(methodPath, '.');
|
||||
return _obj_getMethodArg(obj, methodName, arg_reg);
|
||||
}
|
||||
|
||||
Arg* obj_getMethodArg_noalloc(PikaObj* obj, char* methodPath, Arg* arg_reg) {
|
||||
return _obj_getMethodArg(obj, methodPath, arg_reg);
|
||||
Arg* obj_getMethodArgWithFullPath(PikaObj* obj, char* methodPath) {
|
||||
return _obj_getMethodArgWithFullPath(obj, methodPath, NULL);
|
||||
}
|
||||
|
||||
Arg* obj_getMethodArgWithFullPath_noalloc(PikaObj* obj,
|
||||
char* methodPath,
|
||||
Arg* arg_reg) {
|
||||
return _obj_getMethodArgWithFullPath(obj, methodPath, arg_reg);
|
||||
}
|
||||
|
||||
Arg* obj_getMethodArg(PikaObj* obj, char* methodName) {
|
||||
return _obj_getMethodArg(obj, methodName, NULL);
|
||||
}
|
||||
|
||||
Arg* obj_getMethodArg_noalloc(PikaObj* obj, char* methodName, Arg* arg_reg) {
|
||||
return _obj_getMethodArg(obj, methodName, arg_reg);
|
||||
}
|
||||
|
||||
NewFun obj_getClass(PikaObj* obj) {
|
||||
@ -928,7 +944,7 @@ char* methodArg_getName(Arg* method_arg, char* buffs, size_t size) {
|
||||
}
|
||||
|
||||
Method obj_getNativeMethod(PikaObj* self, char* method_name) {
|
||||
Arg* method_arg = obj_getMethodArg(self, method_name);
|
||||
Arg* method_arg = obj_getMethodArgWithFullPath(self, method_name);
|
||||
if (NULL == method_arg) {
|
||||
return NULL;
|
||||
}
|
||||
@ -977,6 +993,12 @@ void _update_proxy(PikaObj* self, char* name) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (!obj_getFlag(self, OBJ_FLAG_PROXY_METHOD)) {
|
||||
if (strEqu(name, "__proxy__")) {
|
||||
obj_setFlag(self, OBJ_FLAG_PROXY_METHOD);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void obj_saveMethodInfo(PikaObj* self, MethodInfo* tInfo) {
|
||||
@ -2428,7 +2450,8 @@ 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");
|
||||
pika_platform_printf(
|
||||
"Error: Not found LibObj, please execute obj_linkLibrary()\r\n");
|
||||
return;
|
||||
}
|
||||
pika_platform_printf(arg_getStr((Arg*)g_PikaObjState.helpModulesCmodule));
|
||||
@ -2477,7 +2500,7 @@ int obj_importModule(PikaObj* self, char* module_name) {
|
||||
|
||||
char* obj_toStr(PikaObj* self) {
|
||||
/* check method arg */
|
||||
Arg* aMethod = obj_getMethodArg(self, "__str__");
|
||||
Arg* aMethod = obj_getMethodArgWithFullPath(self, "__str__");
|
||||
if (NULL != aMethod) {
|
||||
arg_deinit(aMethod);
|
||||
/* clang-format off */
|
||||
|
@ -96,7 +96,7 @@ struct PikaObj {
|
||||
PikaObj* gcRoot;
|
||||
#endif
|
||||
uint8_t refcnt;
|
||||
uint8_t flag;
|
||||
uint16_t flag;
|
||||
};
|
||||
|
||||
typedef struct PikaGC PikaGC;
|
||||
@ -136,18 +136,19 @@ struct PikaObjState {
|
||||
#define OBJ_FLAG_GLOBALS 1 << 5
|
||||
#define OBJ_FLAG_GC_MARKED 1 << 6
|
||||
#define OBJ_FLAG_GC_ROOT 1 << 7
|
||||
#define OBJ_FLAG_PROXY_METHOD 1 << 8
|
||||
|
||||
#define PIKA_KEY_UP 0x41
|
||||
#define PIKA_KEY_DOWN 0x42
|
||||
#define PIKA_KEY_RIGHT 0x43
|
||||
#define PIKA_KEY_LEFT 0x44
|
||||
|
||||
static inline uint8_t obj_getFlag(PikaObj* self, uint8_t flag) {
|
||||
static inline uint8_t obj_getFlag(PikaObj* self, uint16_t flag) {
|
||||
pika_assert(self);
|
||||
return (self->flag & flag) == flag;
|
||||
}
|
||||
|
||||
static inline void obj_setFlag(PikaObj* self, uint8_t flag) {
|
||||
static inline void obj_setFlag(PikaObj* self, uint16_t flag) {
|
||||
pika_assert(self);
|
||||
self->flag |= flag;
|
||||
#if PIKA_KERNAL_DEBUG_ENABLE
|
||||
@ -157,7 +158,7 @@ static inline void obj_setFlag(PikaObj* self, uint8_t flag) {
|
||||
#endif
|
||||
}
|
||||
|
||||
static inline void obj_clearFlag(PikaObj* self, uint8_t flag) {
|
||||
static inline void obj_clearFlag(PikaObj* self, uint16_t flag) {
|
||||
self->flag &= ~flag;
|
||||
#if PIKA_KERNAL_DEBUG_ENABLE
|
||||
if (flag == OBJ_FLAG_GC_ROOT) {
|
||||
@ -284,8 +285,12 @@ PikaObj* obj_newObjFromConstructor(PikaObj* self,
|
||||
NewFun newClassFun);
|
||||
PikaObj* newRootObj(char* name, NewFun newObjFun);
|
||||
PikaObj* obj_getClassObj(PikaObj* obj);
|
||||
Arg* obj_getMethodArg(PikaObj* obj, char* methodPath);
|
||||
Arg* obj_getMethodArg_noalloc(PikaObj* obj, char* methodPath, Arg* arg_reg);
|
||||
Arg* obj_getMethodArg(PikaObj* obj, char* methodName);
|
||||
Arg* obj_getMethodArg_noalloc(PikaObj* obj, char* methodName, Arg* arg_reg);
|
||||
Arg* obj_getMethodArgWithFullPath(PikaObj* obj, char* methodPath);
|
||||
Arg* obj_getMethodArgWithFullPath_noalloc(PikaObj* obj,
|
||||
char* methodPath,
|
||||
Arg* arg_reg);
|
||||
|
||||
void obj_setErrorCode(PikaObj* self, int32_t errCode);
|
||||
int32_t obj_getErrorCode(PikaObj* self);
|
||||
|
81
src/PikaVM.c
81
src/PikaVM.c
@ -1414,11 +1414,12 @@ static int _get_n_input_with_unpack(VMState* vm, int n_used) {
|
||||
#define vars_or_keys_or_default (f.is_vars || f.is_keys || f.is_default)
|
||||
#define METHOD_TYPE_LIST_MAX_LEN PIKA_LINE_BUFF_SIZE
|
||||
static int VMState_loadArgsFromMethodArg(VMState* vm,
|
||||
PikaObj* method_host_obj,
|
||||
Args* locals,
|
||||
Arg* method_arg,
|
||||
char* method_name,
|
||||
int n_used) {
|
||||
PikaObj* oMethodHost,
|
||||
Args* aLoclas,
|
||||
Arg* aMethod,
|
||||
char* sMethodName,
|
||||
char* sProxyName,
|
||||
int iNumUsed) {
|
||||
int argc = 0;
|
||||
Arg** argv = (Arg**)pikaMalloc(sizeof(Arg*) * PIKA_ARG_NUM_MAX);
|
||||
char* buffs1 = (char*)pikaMalloc(METHOD_TYPE_LIST_MAX_LEN);
|
||||
@ -1427,14 +1428,14 @@ static int VMState_loadArgsFromMethodArg(VMState* vm,
|
||||
char* type_list_buff = NULL;
|
||||
/* get method type list */
|
||||
f.type_list =
|
||||
methodArg_getTypeList(method_arg, buffs1, METHOD_TYPE_LIST_MAX_LEN);
|
||||
methodArg_getTypeList(aMethod, buffs1, METHOD_TYPE_LIST_MAX_LEN);
|
||||
if (NULL == f.type_list) {
|
||||
pika_platform_printf(
|
||||
"OverflowError: type list is too long, please use bigger "
|
||||
"PIKA_LINE_BUFF_SIZE\r\n");
|
||||
pika_platform_panic_handle();
|
||||
}
|
||||
f.method_type = arg_getType(method_arg);
|
||||
f.method_type = arg_getType(aMethod);
|
||||
|
||||
/* get arg_num_pos */
|
||||
_type_list_parse(&f);
|
||||
@ -1443,12 +1444,17 @@ static int VMState_loadArgsFromMethodArg(VMState* vm,
|
||||
f.n_positional--;
|
||||
}
|
||||
|
||||
f.n_input = _get_n_input_with_unpack(vm, n_used);
|
||||
f.n_input = _get_n_input_with_unpack(vm, iNumUsed);
|
||||
|
||||
if (NULL != sProxyName) {
|
||||
/* method proxy takes the first arg as method name */
|
||||
f.n_input++;
|
||||
}
|
||||
|
||||
do {
|
||||
/* check arg num */
|
||||
if (f.method_type == ARG_TYPE_METHOD_NATIVE_CONSTRUCTOR ||
|
||||
f.method_type == ARG_TYPE_METHOD_CONSTRUCTOR || n_used != 0) {
|
||||
f.method_type == ARG_TYPE_METHOD_CONSTRUCTOR || iNumUsed != 0) {
|
||||
/* skip for constrctor */
|
||||
/* skip for variable args */
|
||||
/* n_used != 0 means it is a factory method */
|
||||
@ -1462,7 +1468,7 @@ static int VMState_loadArgsFromMethodArg(VMState* vm,
|
||||
"TypeError: %s() takes %d positional argument but %d "
|
||||
"were "
|
||||
"given\r\n",
|
||||
method_name, f.n_positional, f.n_input);
|
||||
sMethodName, f.n_positional, f.n_input);
|
||||
goto exit;
|
||||
}
|
||||
break;
|
||||
@ -1479,7 +1485,7 @@ static int VMState_loadArgsFromMethodArg(VMState* vm,
|
||||
"%d "
|
||||
"were "
|
||||
"given\r\n",
|
||||
method_name, f.n_positional, f.n_input);
|
||||
sMethodName, f.n_positional, f.n_input);
|
||||
goto exit;
|
||||
}
|
||||
break;
|
||||
@ -1493,7 +1499,7 @@ static int VMState_loadArgsFromMethodArg(VMState* vm,
|
||||
"TypeError: %s() takes from %d to %d positional "
|
||||
"arguments "
|
||||
"but %d were given\r\n",
|
||||
method_name, n_min, n_max, f.n_input);
|
||||
sMethodName, n_min, n_max, f.n_input);
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
@ -1544,8 +1550,13 @@ static int VMState_loadArgsFromMethodArg(VMState* vm,
|
||||
|
||||
/* load args */
|
||||
for (int i = 0; i < f.n_arg; i++) {
|
||||
Arg* call_arg = NULL;
|
||||
f.i_arg = f.n_arg - i;
|
||||
Arg* call_arg = stack_popArg_alloc(&(vm->stack));
|
||||
if (NULL != sProxyName && i == f.n_arg - 1) {
|
||||
call_arg = arg_newStr(sProxyName);
|
||||
} else {
|
||||
call_arg = stack_popArg_alloc(&(vm->stack));
|
||||
}
|
||||
_load_call_arg(vm, call_arg, &f, &i, &argc, argv);
|
||||
}
|
||||
|
||||
@ -1585,15 +1596,15 @@ static int VMState_loadArgsFromMethodArg(VMState* vm,
|
||||
|
||||
/* load 'self' as the first arg when call object method */
|
||||
if (f.method_type == ARG_TYPE_METHOD_OBJECT) {
|
||||
PikaObj* method_self = methodArg_getHostObj(method_arg);
|
||||
PikaObj* method_self = methodArg_getHostObj(aMethod);
|
||||
if (NULL == method_self) {
|
||||
method_self = method_host_obj;
|
||||
method_self = oMethodHost;
|
||||
}
|
||||
Arg* call_arg = arg_setRef(NULL, "self", method_self);
|
||||
pika_assert(call_arg != NULL);
|
||||
argv[argc++] = call_arg;
|
||||
}
|
||||
_loadLocalsFromArgv(locals, argc, argv);
|
||||
_loadLocalsFromArgv(aLoclas, argc, argv);
|
||||
exit:
|
||||
pikaFree(buffs1, METHOD_TYPE_LIST_MAX_LEN);
|
||||
pikaFree(buffs2, METHOD_TYPE_LIST_MAX_LEN);
|
||||
@ -1789,6 +1800,7 @@ static Arg* VM_instruction_handler_RUN(PikaObj* self,
|
||||
VMParameters* oSublocalsInit = NULL;
|
||||
char* sRunPath = data;
|
||||
char* sArgName = NULL;
|
||||
char* sProxyName = NULL;
|
||||
PikaObj* oMethodHost = NULL;
|
||||
PikaObj* oThis = NULL;
|
||||
Arg* aMethod = NULL;
|
||||
@ -1830,7 +1842,7 @@ static Arg* VM_instruction_handler_RUN(PikaObj* self,
|
||||
/* support for super() */
|
||||
if (strEqu(sRunPath, "super")) {
|
||||
sRunPath = _find_super_class_name(vm);
|
||||
sArgName = sRunPath;
|
||||
sArgName = strPointToLastToken(sRunPath, '.');
|
||||
vm->in_super = PIKA_TRUE;
|
||||
vm->super_invoke_deepth = VMState_getInvokeDeepthNow(vm);
|
||||
bSkipInit = PIKA_TRUE;
|
||||
@ -1916,24 +1928,37 @@ static Arg* VM_instruction_handler_RUN(PikaObj* self,
|
||||
|
||||
/* get method in object */
|
||||
if (NULL == aMethod) {
|
||||
aMethod = obj_getMethodArg_noalloc(oMethodHost, sRunPath, &arg_reg1);
|
||||
aMethod = obj_getMethodArg_noalloc(oMethodHost, sArgName, &arg_reg1);
|
||||
}
|
||||
|
||||
if (sArgName == sRunPath) {
|
||||
/* get method in locals */
|
||||
if (NULL == aMethod) {
|
||||
aMethod = obj_getMethodArg_noalloc(vm->locals, sRunPath, &arg_reg1);
|
||||
aMethod = obj_getMethodArg_noalloc(vm->locals, sArgName, &arg_reg1);
|
||||
}
|
||||
/* get method in global */
|
||||
if (NULL == aMethod) {
|
||||
aMethod =
|
||||
obj_getMethodArg_noalloc(vm->globals, sRunPath, &arg_reg1);
|
||||
obj_getMethodArg_noalloc(vm->globals, sArgName, &arg_reg1);
|
||||
if (aMethod != NULL) {
|
||||
oThis = vm->globals;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (NULL == aMethod) {
|
||||
/* get proxy method */
|
||||
if (obj_getFlag(oMethodHost, OBJ_FLAG_PROXY_METHOD)) {
|
||||
if (strCountSign(sArgName, '.') == 0) {
|
||||
/* __proxy__ takes the first arg as the method name */
|
||||
sProxyName = sArgName;
|
||||
sArgName = "__proxy__";
|
||||
aMethod =
|
||||
obj_getMethodArg_noalloc(oMethodHost, sArgName, &arg_reg1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* assert method exist */
|
||||
if (NULL == aMethod || ARG_TYPE_NONE == arg_getType(aMethod)) {
|
||||
/* error, method no found */
|
||||
@ -1956,8 +1981,8 @@ static Arg* VM_instruction_handler_RUN(PikaObj* self,
|
||||
oSublocals = New_Locals(NULL);
|
||||
|
||||
/* load args from vmState to sub_local->list */
|
||||
iNumUsed += VMState_loadArgsFromMethodArg(vm, oThis, oSublocals->list,
|
||||
aMethod, sRunPath, iNumUsed);
|
||||
iNumUsed += VMState_loadArgsFromMethodArg(
|
||||
vm, oThis, oSublocals->list, aMethod, sRunPath, sProxyName, iNumUsed);
|
||||
|
||||
/* load args failed */
|
||||
if (vm->error_code != 0) {
|
||||
@ -1986,14 +2011,15 @@ static Arg* VM_instruction_handler_RUN(PikaObj* self,
|
||||
/* init object */
|
||||
PikaObj* oNew = arg_getPtr(aReturn);
|
||||
obj_setName(oNew, sRunPath);
|
||||
Arg* aMethod = obj_getMethodArg_noalloc(oNew, "__init__", &arg_reg1);
|
||||
Arg* aMethod =
|
||||
obj_getMethodArgWithFullPath_noalloc(oNew, "__init__", &arg_reg1);
|
||||
oSublocalsInit = New_Locals(NULL);
|
||||
Arg* aReturnInit = NULL;
|
||||
if (NULL == aMethod) {
|
||||
goto init_exit;
|
||||
}
|
||||
VMState_loadArgsFromMethodArg(vm, oNew, oSublocalsInit->list, aMethod,
|
||||
"__init__", iNumUsed);
|
||||
"__init__", NULL, iNumUsed);
|
||||
/* load args failed */
|
||||
if (vm->error_code != 0) {
|
||||
goto init_exit;
|
||||
@ -2438,7 +2464,7 @@ static void _OPT_ADD(OperatorInfo* op) {
|
||||
return;
|
||||
}
|
||||
PikaObj* obj1 = arg_getPtr(op->a1);
|
||||
Arg* method_add = obj_getMethodArg(obj1, "__add__");
|
||||
Arg* method_add = obj_getMethodArgWithFullPath(obj1, "__add__");
|
||||
if (NULL == method_add) {
|
||||
VMState_setErrorCode(op->vm, PIKA_RES_ERR_OPERATION_FAILED);
|
||||
pika_platform_printf("TypeError: unsupported operand +\n");
|
||||
@ -2515,7 +2541,7 @@ static void _OPT_SUB(OperatorInfo* op) {
|
||||
return;
|
||||
}
|
||||
PikaObj* obj1 = arg_getPtr(op->a1);
|
||||
Arg* method_sub = obj_getMethodArg(obj1, "__sub__");
|
||||
Arg* method_sub = obj_getMethodArgWithFullPath(obj1, "__sub__");
|
||||
if (NULL == method_sub) {
|
||||
VMState_setErrorCode(op->vm, PIKA_RES_ERR_OPERATION_FAILED);
|
||||
pika_platform_printf("TypeError: unsupported operand +\n");
|
||||
@ -2783,7 +2809,8 @@ static Arg* VM_instruction_handler_OPT(PikaObj* self,
|
||||
#if !PIKA_NANO_ENABLE
|
||||
if (argType_isObject(op.t2)) {
|
||||
PikaObj* obj2 = arg_getPtr(op.a2);
|
||||
Arg* __contains__ = obj_getMethodArg(obj2, "__contains__");
|
||||
Arg* __contains__ =
|
||||
obj_getMethodArgWithFullPath(obj2, "__contains__");
|
||||
if (NULL != __contains__) {
|
||||
arg_deinit(__contains__);
|
||||
obj_setArg(obj2, "__others", op.a1);
|
||||
|
@ -1,5 +1,5 @@
|
||||
#define PIKA_VERSION_MAJOR 1
|
||||
#define PIKA_VERSION_MINOR 12
|
||||
#define PIKA_VERSION_MICRO 2
|
||||
#define PIKA_VERSION_MICRO 3
|
||||
|
||||
#define PIKA_EDIT_TIME "2023/05/03 22:24:57"
|
||||
#define PIKA_EDIT_TIME "2023/05/14 12:55:46"
|
||||
|
@ -2,7 +2,7 @@
|
||||
TEST_START
|
||||
#if !PIKA_NANO_ENABLE
|
||||
|
||||
TEST_RUN_SINGLE_FILE(lua, eval, "test/python/pika_lua/eval.py")
|
||||
TEST_RUN_SINGLE_FILE_PASS(lua, eval, "test/python/pika_lua/eval.py")
|
||||
|
||||
#endif
|
||||
TEST_END
|
||||
|
@ -594,6 +594,8 @@ TEST(module, REPL_stdtask) {
|
||||
|
||||
TEST_RUN_SINGLE_FILE(modbus, rtu_master_err, "test/python/modbus/rtu_master_err.py")
|
||||
|
||||
TEST_RUN_SINGLE_FILE_PASS(proxy, proxy3, "test/python/proxy/proxy3.py")
|
||||
|
||||
#endif
|
||||
|
||||
TEST_END
|
10
test/python/proxy/proxy3.py
Normal file
10
test/python/proxy/proxy3.py
Normal file
@ -0,0 +1,10 @@
|
||||
|
||||
class TestProxy:
|
||||
def __proxy__(self, name, value):
|
||||
return name, value
|
||||
|
||||
test = TestProxy()
|
||||
name, value = test.function1(123)
|
||||
assert name == 'function1'
|
||||
assert value == 123
|
||||
print('PASS')
|
Loading…
x
Reference in New Issue
Block a user