args_set can update type

This commit is contained in:
lyon1998 2021-10-18 16:08:28 +08:00
parent 95cfbf6caa
commit 4c6ec93f41
7 changed files with 40 additions and 74 deletions

View File

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

View File

@ -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()");

View File

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

View File

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

View File

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

View File

@ -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]<objName> */
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);
}

View File

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