From 4c6ec93f4195c50549e8ce942365d2354bedd628 Mon Sep 17 00:00:00 2001 From: lyon1998 <645275593@qq.com> Date: Mon, 18 Oct 2021 16:08:28 +0800 Subject: [PATCH] args_set can update type --- port/linux/.vscode/launch.json | 2 +- port/linux/test/object-test.cpp | 17 +++++++----- port/linux/test/queue-test.cpp | 2 ++ src/BaseObj.c | 34 +++++++++++------------- src/BaseObj.h | 1 + src/PikaObj.c | 46 +++++++-------------------------- src/dataArgs.c | 12 +-------- 7 files changed, 40 insertions(+), 74 deletions(-) diff --git a/port/linux/.vscode/launch.json b/port/linux/.vscode/launch.json index 7d71f90f1..86211df91 100644 --- a/port/linux/.vscode/launch.json +++ b/port/linux/.vscode/launch.json @@ -11,7 +11,7 @@ "program": "${workspaceFolder}/build/test/pikascript_test", // "program": "${workspaceFolder}/../build/src/boot/demo06-pikamain/pikascript_demo06-pikamain", "args": [ - // "--gtest_filter=stack*" + "--gtest_filter=queueObj.currentObj" // "--gtest_filter=parser*" // "--gtest_filter=VM.Run_add_1_2_3" // "--gtest_filter=block.ifrun2" diff --git a/port/linux/test/object-test.cpp b/port/linux/test/object-test.cpp index 2475d50d6..a39b3d5b5 100644 --- a/port/linux/test/object-test.cpp +++ b/port/linux/test/object-test.cpp @@ -87,6 +87,16 @@ PikaObj* New_MYROOT1(Args* args) { return self; } +TEST(object_test, test10) { + PikaObj* root = newRootObj((char*)"root", New_MYROOT1); + PikaObj* usart = obj_getObj(root, (char*)"usart", 0); + PikaObj* context = obj_getContext(usart); + EXPECT_EQ(context, root); + obj_run(root, (char*)"usart.send('hello world')"); + obj_deinit(root); + EXPECT_EQ(pikaMemNow(), 0); +} + TEST(object_test, test1) { PikaObj* process = newRootObj((char*)"sys", PikaStdLib_SysObj); float floatTest = 12.231; @@ -166,13 +176,6 @@ TEST(object_test, test9) { EXPECT_EQ(pikaMemNow(), 0); } -TEST(object_test, test10) { - PikaObj* root = newRootObj((char*)"root", New_MYROOT1); - obj_run(root, (char*)"usart.send('hello world')"); - obj_deinit(root); - EXPECT_EQ(pikaMemNow(), 0); -} - TEST(object_test, noMethod) { PikaObj* root = newRootObj((char*)"root", New_MYROOT1); obj_runNoRes(root, (char*)"noDefindMethod()"); diff --git a/port/linux/test/queue-test.cpp b/port/linux/test/queue-test.cpp index 8a52a433f..b02d81e67 100644 --- a/port/linux/test/queue-test.cpp +++ b/port/linux/test/queue-test.cpp @@ -133,6 +133,8 @@ TEST(queueObj, currentObj) { queueObj_init(q); queueObj_pushObj(q, (char*)"type1"); + PikaObj* currentObj = queueObj_getCurrentObj(q); + char* type = args_getType(q->attributeList, (char*)"0"); obj_setInt(queueObj_getCurrentObj(q), (char*)"test", 1); queueObj_pushObj(q, (char*)"type2"); diff --git a/src/BaseObj.c b/src/BaseObj.c index 2182b943f..3a9a431fd 100644 --- a/src/BaseObj.c +++ b/src/BaseObj.c @@ -15,24 +15,7 @@ static void* getClassPtr(PikaObj* classObj, char* classPath) { return obj_getPtr(classObj, ptrPath); } -int32_t obj_newObj(PikaObj* self, - char* objName, - char* className, - NewFun newFunPtr) { - /* class means subprocess init */ - Args* buffs = New_strBuff(); - - /* class means subprocess init */ - char* mataObjName = strsAppend(buffs, "[mate]", objName); - obj_setPtr(self, mataObjName, newFunPtr); - /* add void process Ptr, no inited */ - args_setObjectWithClass(self->attributeList, objName, className, NULL); - - args_deinit(buffs); - return 0; -} - -Arg* arg_setMetaObj(char* objName, char* className, void* objPtr) { +Arg* arg_setMetaObj(char* objName, char* className, NewFun objPtr) { Args* buffs = New_strBuff(); char* typeWithClass = strsAppend(buffs, "_class-[mate]", className); Arg* argNew = New_arg(NULL); @@ -41,6 +24,20 @@ Arg* arg_setMetaObj(char* objName, char* className, void* objPtr) { return argNew; } +int32_t obj_newObj(PikaObj* self, + char* objName, + char* className, + NewFun newFunPtr) { + /* class means subprocess init */ + + /* add mate Obj, no inited */ + Arg* mateObj = arg_setMetaObj(objName, className, newFunPtr); + NewFun newFunInMate = arg_getPtr(mateObj); + args_setArg(self->attributeList, mateObj); + return 0; +} + + static void print(PikaObj* self, Args* args) { obj_setErrorCode(self, 0); char* res = args_print(args, "val"); @@ -53,7 +50,6 @@ static void print(PikaObj* self, Args* args) { obj_setSysOut(self, res); } - PikaObj* BaseObj(Args* args) { PikaObj* self = TinyObj(args); class_defineMethod(self, "print(val:any)", print); diff --git a/src/BaseObj.h b/src/BaseObj.h index 1939aa925..1e4320086 100644 --- a/src/BaseObj.h +++ b/src/BaseObj.h @@ -18,4 +18,5 @@ int32_t obj_newObj(PikaObj* self, char* objName, char* className, NewFun newFunPtr); +Arg* arg_setMetaObj(char* objName, char* className, NewFun objPtr); #endif diff --git a/src/PikaObj.c b/src/PikaObj.c index acdea134b..7a83180d0 100644 --- a/src/PikaObj.c +++ b/src/PikaObj.c @@ -190,17 +190,6 @@ int32_t obj_load(PikaObj* self, Args* args, char* name) { return 0; } -int32_t obj_setObjWithoutClass(PikaObj* self, char* objName, void* newFun) { - /* class means subprocess init */ - Args* buffs = New_strBuff(); - char* mataObjName = strsAppend(buffs, "[mate]", objName); - obj_setPtr(self, mataObjName, newFun); - /* add void object Ptr, no inited */ - args_setObjectWithClass(self->attributeList, objName, "none", NULL); - args_deinit(buffs); - return 0; -} - int32_t obj_addOther(PikaObj* self, char* subObjectName, void* new_ObjectFun) { Args* initArgs = New_args(NULL); args_setPtr(initArgs, "_ctx", self); @@ -264,12 +253,10 @@ char* obj_getClassPath(PikaObj* objHost, Args* buffs, char* objName) { } void* getNewClassObjFunByName(PikaObj* obj, char* name) { - Args* buffs = New_strBuff(); - char* classPath = strsAppend(buffs, "[mate]", name); + char* classPath = name; /* init the subprocess */ void* (*newClassObjFun)(Args * initArgs) = args_getPtr(obj->attributeList, classPath); - args_deinit(buffs); return newClassObjFun; } @@ -291,14 +278,6 @@ PikaObj* removeMethodInfo(PikaObj* thisClass) { return thisClass; } -static void removeClassLoader(PikaObj* obj) { - PikaObj* classLoader = args_getPtr(obj->attributeList, "_clsld"); - if (NULL != classLoader) { - obj_deinit(classLoader); - args_removeArg(obj->attributeList, "_clsld"); - } -} - PikaObj* newRootObj(char* name, NewFun newObjFun) { PikaObj* thisClass = obj_getClassObjByNewFun(NULL, name, newObjFun); PikaObj* newObj = removeMethodInfo(thisClass); @@ -316,13 +295,11 @@ PikaObj* initObj(PikaObj* obj, char* name) { } PikaObj* thisClass = obj_getClassObjByNewFun(obj, name, newObjFun); PikaObj* newObj = removeMethodInfo(thisClass); - /* delete [mate] */ - obj_removeArg(obj, strsAppend(buffs, "[mate]", name)); - /* delete "_clsld" object */ - removeClassLoader(newObj); - char* type = args_getType(obj->attributeList, name); - args_setPtrWithType(obj->attributeList, name, type, newObj); + char* mateObjType = args_getType(obj->attributeList, name); + char* pureType = strsGetLastToken(buffs, mateObjType, ']'); + char* objType = strsAppend(buffs, "_class-", pureType); + args_setPtrWithType(obj->attributeList, name, objType, newObj); res = obj_getPtr(obj, name); goto exit; exit: @@ -334,19 +311,16 @@ PikaObj* obj_getObjDirect(PikaObj* self, char* name) { if (NULL == self) { return NULL; } - - /* check subprocess */ - if (NULL == args_getPtr(self->attributeList, name)) { - /* no inited subprocess, check subprocess init fun*/ - return initObj(self, name); - } - - /* finded subscribe, check type*/ + /* finded object, check type*/ char* type = args_getType(self->attributeList, name); if (!strIsStartWith(type, "_class")) { /* type error, could not found subprocess */ return NULL; } + /* found mate Object */ + if (strIsStartWith(type, "_class-[mate]")) { + return initObj(self, name); + } return obj_getPtr(self, name); } diff --git a/src/dataArgs.c b/src/dataArgs.c index 9d7d38cc4..08541139b 100644 --- a/src/dataArgs.c +++ b/src/dataArgs.c @@ -159,6 +159,7 @@ int32_t updateArg(Args* self, Arg* argNew) { nodeToUpdate = arg_setContent(nodeToUpdate, arg_getContent(argNew), arg_getContentSize(argNew)); + nodeToUpdate = arg_setType(nodeToUpdate, arg_getType(argNew)); // update privior link, because arg_getContent would free origin pointer if (NULL == priorNode) { self->firstNode = nodeToUpdate; @@ -339,17 +340,6 @@ int32_t args_setPtrWithType(Args* self, char* name, char* type, void* objPtr) { return 0; } -int32_t args_setObjectWithClass(Args* self, - char* objName, - char* className, - void* objPtr) { - Args* buffs = New_strBuff(); - char* typeWithClass = strsAppend(buffs, "_class-", className); - args_setPtrWithType(self, objName, typeWithClass, objPtr); - args_deinit(buffs); - return 0; -} - int32_t args_foreach(Args* self, int32_t (*eachHandle)(Arg* argEach, Args* handleArgs), Args* handleArgs) {