befor add native_method

This commit is contained in:
lyon1998 2022-03-14 13:43:07 +08:00
parent 8d84cb6423
commit c2a459a0ab
2 changed files with 56 additions and 10 deletions

View File

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

View File

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