mirror of
https://gitee.com/Lyon1998/pikapython.git
synced 2025-01-29 17:22:56 +08:00
init freeObj in the constructor
This commit is contained in:
parent
89a683c6ce
commit
1bf4b687ca
1
port/linux/.vscode/launch.json
vendored
1
port/linux/.vscode/launch.json
vendored
@ -18,6 +18,7 @@
|
||||
"program": "${workspaceFolder}/build/test/pikascript_test",
|
||||
// "program": "${workspaceFolder}/../build/src/boot/demo06-pikamain/pikascript_demo06-pikamain",
|
||||
"args": [
|
||||
"--gtest_filter=pikaMain.class_demo_2",
|
||||
// "--gtest_filter=pikaMain.list__set__",
|
||||
// "--gtest_filter=parser._3_3",
|
||||
// "--gtest_filter=VM.a_3",
|
||||
|
@ -42,8 +42,5 @@ int32_t obj_newObj(PikaObj* self,
|
||||
NewFun newFunPtr);
|
||||
Arg* arg_setMetaObj(char* objName, char* className, NewFun objPtr);
|
||||
void baseobj_print(PikaObj* self, Args* args);
|
||||
int32_t class_defineConstructor(PikaObj* self,
|
||||
char* declearation,
|
||||
Method methodPtr);
|
||||
|
||||
#endif
|
||||
|
@ -500,7 +500,7 @@ int32_t class_defineConstructor(PikaObj* self,
|
||||
char* declearation,
|
||||
Method methodPtr) {
|
||||
return __class_defineMethodWithType(self, declearation, methodPtr,
|
||||
ARG_TYPE_NATIVE_METHOD, NULL);
|
||||
ARG_TYPE_CONSTRUCTOR_METHOD, NULL);
|
||||
}
|
||||
|
||||
/* define a native method as default */
|
||||
|
@ -152,6 +152,10 @@ int32_t class_defineStaticMethod(PikaObj* self,
|
||||
Method methodPtr,
|
||||
ByteCodeFrame* bytecode_frame);
|
||||
|
||||
int32_t class_defineConstructor(PikaObj* self,
|
||||
char* declearation,
|
||||
Method methodPtr);
|
||||
|
||||
int32_t obj_removeArg(PikaObj* self, char* argPath);
|
||||
int32_t obj_isArgExist(PikaObj* self, char* argPath);
|
||||
PikaObj* obj_getClassObjByNewFun(PikaObj* self, char* name, NewFun newClassFun);
|
||||
|
58
src/PikaVM.c
58
src/PikaVM.c
@ -195,8 +195,8 @@ static Arg* VM_instruction_handler_RUN(PikaObj* self, VMState* vs, char* data) {
|
||||
char* type_list;
|
||||
char* sys_out;
|
||||
Arg* call_arg = NULL;
|
||||
uint8_t arg_num_dec;
|
||||
uint8_t arg_num_input;
|
||||
uint8_t arg_num_dec;
|
||||
uint8_t arg_num_input;
|
||||
uint8_t call_arg_index = 0;
|
||||
ByteCodeFrame* method_bytecodeFrame;
|
||||
/* return arg directly */
|
||||
@ -259,13 +259,20 @@ static Arg* VM_instruction_handler_RUN(PikaObj* self, VMState* vs, char* data) {
|
||||
arg_num_dec--;
|
||||
}
|
||||
arg_num_input = VMState_getInputArgNum(vs);
|
||||
if (arg_num_dec != arg_num_input) {
|
||||
VMState_setErrorCode(vs, 3);
|
||||
__platform_printf(
|
||||
"TypeError: %s() takes %d positional argument but %d were "
|
||||
"given\r\n",
|
||||
data, arg_num_dec, arg_num_input);
|
||||
goto RUN_exit;
|
||||
|
||||
/* check arg num */
|
||||
if (method_type == ARG_TYPE_CONSTRUCTOR_METHOD) {
|
||||
/* not check decleard arg num for constrctor */
|
||||
} else {
|
||||
/* check arg num decleard and input */
|
||||
if (arg_num_dec != arg_num_input) {
|
||||
VMState_setErrorCode(vs, 3);
|
||||
__platform_printf(
|
||||
"TypeError: %s() takes %d positional argument but %d were "
|
||||
"given\r\n",
|
||||
data, arg_num_dec, arg_num_input);
|
||||
goto RUN_exit;
|
||||
}
|
||||
}
|
||||
|
||||
/* load pars */
|
||||
@ -293,6 +300,12 @@ static Arg* VM_instruction_handler_RUN(PikaObj* self, VMState* vs, char* data) {
|
||||
method_ptr(method_host_obj, sub_locals->list);
|
||||
/* get method return */
|
||||
return_arg = arg_copy(args_getArg(sub_locals->list, (char*)"return"));
|
||||
} else if (method_type == ARG_TYPE_CONSTRUCTOR_METHOD) {
|
||||
/* native method */
|
||||
method_ptr(method_host_obj, sub_locals->list);
|
||||
/* get method return */
|
||||
return_arg = arg_copy(args_getArg(sub_locals->list, (char*)"return"));
|
||||
|
||||
} else {
|
||||
/* static method and object method */
|
||||
/* byteCode */
|
||||
@ -305,6 +318,29 @@ static Arg* VM_instruction_handler_RUN(PikaObj* self, VMState* vs, char* data) {
|
||||
/* get method return */
|
||||
return_arg = arg_copy(args_getArg(sub_locals->list, (char*)"return"));
|
||||
}
|
||||
|
||||
if (arg_getType(return_arg) == ARG_TYPE_FREE_OBJECT) {
|
||||
/* init object */
|
||||
PikaObj* new_obj = arg_getPtr(return_arg);
|
||||
/* run __init__() when init obj */
|
||||
Arg* init_method_arg = NULL;
|
||||
init_method_arg = obj_getMethodArg(new_obj, "__init__");
|
||||
if (NULL != init_method_arg) {
|
||||
arg_deinit(init_method_arg);
|
||||
// pikaVM_runAsm(new_obj,
|
||||
// "B0\n"
|
||||
// "0 RUN __init__\n");
|
||||
const uint8_t bytes[] = {
|
||||
0x04, 0x00, /* instruct array size */
|
||||
0x00, 0x82, 0x01, 0x00, /* instruct array */
|
||||
0x0a, 0x00, /* const pool size */
|
||||
0x00, 0x5f, 0x5f, 0x69, 0x6e,
|
||||
0x69, 0x74, 0x5f, 0x5f, 0x00, /* const pool */
|
||||
};
|
||||
pikaVM_runByteCode(new_obj, (uint8_t*)bytes);
|
||||
}
|
||||
}
|
||||
|
||||
/* transfer sysOut */
|
||||
sys_out = obj_getSysOut(method_host_obj);
|
||||
if (NULL != sys_out) {
|
||||
@ -369,8 +405,7 @@ static Arg* __VM_OUT(PikaObj* self,
|
||||
}
|
||||
/* ouput arg to locals */
|
||||
obj_setArg_noCopy(hostObj, data, outArg);
|
||||
if ((ARG_TYPE_MATE_OBJECT == outArg_type) ||
|
||||
(ARG_TYPE_FREE_OBJECT == outArg_type)) {
|
||||
if (ARG_TYPE_MATE_OBJECT == outArg_type) {
|
||||
if (is_init_obj == IS_INIT_OBJ_TRUE) {
|
||||
/* found a mate_object */
|
||||
/* init object */
|
||||
@ -1258,7 +1293,6 @@ VMParameters* pikaVM_runByteCodeWithState(PikaObj* self,
|
||||
return locals;
|
||||
}
|
||||
|
||||
|
||||
VMParameters* pikaVM_runByteCodeFrame(PikaObj* self,
|
||||
ByteCodeFrame* byteCode_frame) {
|
||||
return pikaVM_runByteCodeWithState(self, self, self, byteCode_frame, 0);
|
||||
|
@ -44,13 +44,13 @@ typedef enum {
|
||||
ARG_TYPE_MATE_OBJECT,
|
||||
ARG_TYPE_FREE_OBJECT,
|
||||
ARG_TYPE_NATIVE_METHOD,
|
||||
ARG_TYPE_CONSTRUCTOR_METHOD,
|
||||
ARG_TYPE_OBJECT_METHOD,
|
||||
ARG_TYPE_STATIC_METHOD,
|
||||
ARG_TYPE_STRUCT,
|
||||
ARG_TYPE_HEAP_STRUCT,
|
||||
} ArgType;
|
||||
|
||||
|
||||
typedef void (*StructDeinitFun)(void* struct_);
|
||||
|
||||
typedef struct __arg __arg;
|
||||
|
Loading…
x
Reference in New Issue
Block a user