diff --git a/port/linux/.vscode/launch.json b/port/linux/.vscode/launch.json index c5dbca7e3..4dbcd599f 100644 --- a/port/linux/.vscode/launch.json +++ b/port/linux/.vscode/launch.json @@ -18,6 +18,7 @@ "program": "${workspaceFolder}/build/test/pikascript_test", // "program": "${workspaceFolder}/../build/src/boot/demo06-pikamain/pikascript_demo06-pikamain", "args": [ + "--gtest_filter=VM.RUN_AS", // "--gtest_filter=parser.add_a_b*", // "--gtest_filter=parser.method_void", // "--gtest_filter=parser.add_a_b", diff --git a/port/linux/test/VM-test.cpp b/port/linux/test/VM-test.cpp index 3e1988b86..e6221aaca 100644 --- a/port/linux/test/VM-test.cpp +++ b/port/linux/test/VM-test.cpp @@ -10,12 +10,11 @@ extern "C" { #include "dataQueue.h" #include "dataStrs.h" - /* test head */ VMParameters* pikaVM_runAsmWithPars(PikaObj* self, - VMParameters* locals, - VMParameters* globals, - char* pikaAsm); + VMParameters* locals, + VMParameters* globals, + char* pikaAsm); char* Parser_LineToAsm(Args* buffs, char* line, Stack* blockStack); char* Parser_multiLineToAsm(Args* outBuffs, char* multiLine); int32_t __clearInvokeQueues(VMParameters* locals); @@ -629,3 +628,34 @@ TEST(VM, EST) { EXPECT_EQ(pikaMemNow(), 0); } +TEST(VM, RUN_AS) { + char* pikaAsm = (char*) + "B0\n" + "0 RUN PikaStdLib.PikaObj\n" + "0 OUT as\n" + "B0\n" + "0 RAS as\n" + "B0\n" + "0 NUM 1\n" + "0 OUT x\n" + "B0\n" + "0 RAS $origin\n" + "B0\n" + "0 NUM 2\n" + "0 OUT x\n" + ; + PikaObj* self = newRootObj((char*)"", New_PikaMain); + pikaVM_runAsm(self, pikaAsm); + PikaObj* as = obj_getObj(self, (char*)"as", 0); + int x_as_ = obj_getInt(as, (char*)"x"); + int x_as = obj_getInt(self, (char*)"as.x"); + int x_origin = obj_getInt(self, (char*)"x"); + /* a is local, should not be exist in globals */ + EXPECT_EQ(x_as_, 1); + EXPECT_EQ(x_as, 1); + /* b is local, should not be exist in globals */ + EXPECT_EQ(x_origin, 2); + obj_deinit(self); + // obj_deinit(globals); + EXPECT_EQ(pikaMemNow(), 0); +} diff --git a/src/PikaVM.c b/src/PikaVM.c index 24e25a9a1..bf66b7f93 100644 --- a/src/PikaVM.c +++ b/src/PikaVM.c @@ -44,7 +44,6 @@ VMParameters* pikaVM_runAsmWithPars(PikaObj* self, struct VMState_t { VMParameters* locals; VMParameters* globals; - VMParameters* runAs; Queue* q0; Queue* q1; int32_t jmp; @@ -340,8 +339,8 @@ static Arg* __VM_OUT(PikaObj* self, arg_deinit(global_list_arg); } /* use RunAs object */ - if (vs->runAs != NULL) { - hostObj = vs->runAs; + if (args_isArgExist(vs->locals->list, "__runAs")) { + hostObj = args_getPtr(vs->locals->list, "__runAs"); } /* ouput arg to locals */ obj_setArg(hostObj, data, outArg); @@ -373,12 +372,12 @@ static Arg* VM_instruction_handler_OUT(PikaObj* self, VMState* vs, char* data) { static Arg* VM_instruction_handler_RAS(PikaObj* self, VMState* vs, char* data) { if (strEqu(data, "$origin")) { /* use origin object to run */ - vs->runAs = NULL; + obj_removeArg(vs->locals, "__runAs"); return NULL; } /* use "data" object to run */ PikaObj* runAs = obj_getObj(self, data, 0); - vs->runAs = runAs; + args_setPtr(vs->locals->list, "__runAs", runAs); return NULL; } @@ -580,9 +579,9 @@ static Arg* VM_instruction_handler_DEF(PikaObj* self, VMState* vs, char* data) { int offset = 0; int thisBlockDeepth = __getThisBlockDeepth(vs->ASM_start, vs->pc, &offset); PikaObj* hostObj = vs->locals; - if (NULL != vs->runAs) { - /* use RunAs object */ - hostObj = vs->runAs; + /* use RunAs object */ + if (args_isArgExist(vs->locals->list, "__runAs")) { + hostObj = args_getPtr(vs->locals->list, "__runAs"); } while (1) { if ((methodPtr[0] == 'B') && @@ -685,7 +684,6 @@ int32_t pikaVM_runAsmLine(PikaObj* self, VMState vs = { .locals = locals, .globals = globals, - .runAs = NULL, .q0 = NULL, .q1 = NULL, .jmp = 0,