diff --git a/port/linux/test/pikaMain-test.cpp b/port/linux/test/pikaMain-test.cpp index d9b65ecd4..e61300dfa 100644 --- a/port/linux/test/pikaMain-test.cpp +++ b/port/linux/test/pikaMain-test.cpp @@ -1259,3 +1259,25 @@ TEST(pikaMain, for_if_continue_byte_code) { /* mem check */ EXPECT_EQ(pikaMemNow(), 0); } + +// TEST(pikaMain, print_in_def_byte_code) { +// /* init */ +// pikaMemInfo.heapUsedMax = 0; +// PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain); +// /* run */ +// __platform_printf((char*)"BEGIN\r\n"); +// pikaVM_run_enableByteCode(pikaMain, (char*) +// "def test_print():\n" +// " print('test')\n" +// "test_print()\n" +// ); +// /* collect */ +// /* assert */ +// /* should only print once, so the second log (log_buff[1]) shuold be '\0' */ +// EXPECT_STREQ(log_buff[0], "test\r\n"); +// EXPECT_STREQ(log_buff[1], "BEGIN\r\n"); +// /* deinit */ +// obj_deinit(pikaMain); +// /* mem check */ +// EXPECT_EQ(pikaMemNow(), 0); +// } diff --git a/src/PikaVM.c b/src/PikaVM.c index 7f40f4f0d..0684020fe 100644 --- a/src/PikaVM.c +++ b/src/PikaVM.c @@ -698,18 +698,42 @@ static Arg* VM_instruction_handler_DEF(PikaObj* self, VMState* vs, char* data) { is_in_class = 1; } int offset = 0; - while (1) { - if ((methodPtr[0] == 'B') && - (methodPtr[1] - '0' == thisBlockDeepth + 1)) { - if (is_in_class) { - class_defineObjectMethod(hostObj, data, (Method)methodPtr); - } else { - class_defineMethod(hostObj, data, (Method)methodPtr); + if (vs->ASM_start == NULL) { + /* byteCode */ + while (1) { + InstructUnit* ins_unit_now = + instructArray_getByOffset(vs->ins_array, vs->pc_i + offset); + if (!instructUnit_getIsNewLine(ins_unit_now)) { + offset += instructUnit_getSize(); + continue; } - break; + if (instructUnit_getBlockDeepth(ins_unit_now) == + thisBlockDeepth + 1) { + if (is_in_class) { + class_defineObjectMethod(hostObj, data, + (Method)ins_unit_now); + } else { + class_defineMethod(hostObj, data, (Method)ins_unit_now); + } + break; + } + offset += instructUnit_getSize(); + } + } else { + /* Asm */ + while (1) { + if ((methodPtr[0] == 'B') && + (methodPtr[1] - '0' == thisBlockDeepth + 1)) { + if (is_in_class) { + class_defineObjectMethod(hostObj, data, (Method)methodPtr); + } else { + class_defineMethod(hostObj, data, (Method)methodPtr); + } + break; + } + offset += __gotoNextLine(methodPtr); + methodPtr = vs->pc + offset; } - offset += __gotoNextLine(methodPtr); - methodPtr = vs->pc + offset; } return NULL; }