Skip to content

Commit 2d98f1c

Browse files
committed
fix bug: can not run the function defind from module
1 parent 5120da9 commit 2d98f1c

File tree

9 files changed

+63
-13
lines changed

9 files changed

+63
-13
lines changed

package/cJSON/cJSON_Utils.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
#include "cJSON_Utils.h"
22

3-
PikaObj* cJSON_Utils_parse(PikaObj* self) {}
4-
PikaObj* cJSON_Utils_createObject(PikaObj *self){}
3+
PikaObj* cJSON_Utils_parse(PikaObj* self) {
4+
return NULL;
5+
}
6+
7+
PikaObj* cJSON_Utils_createObject(PikaObj *self){
8+
return NULL;
9+
}

package/cJSON/cJSON_cJSON.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "cJSON_cJSON.h"
22

33
char* cJSON_cJSON_print(PikaObj *self){
4-
4+
return NULL;
55
}
66

77
void cJSON_cJSON___init__(PikaObj *self){

port/linux/.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"program": "${workspaceFolder}/build/test/pikascript_test",
1212
// "program": "${workspaceFolder}/build/boot/demo06-pikamain/pikascript_demo06-pikamain",
1313
"args": [
14-
// "--gtest_filter=parser.slice_12lkj"
14+
"--gtest_filter=pikaMain.module_import_from_module"
1515
],
1616
"stopAtEntry": false,
1717
"cwd": "${workspaceFolder}",
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
#include "cJSON_Utils.h"
22

3-
PikaObj* cJSON_Utils_parse(PikaObj* self) {}
4-
PikaObj* cJSON_Utils_createObject(PikaObj *self){}
3+
PikaObj* cJSON_Utils_parse(PikaObj* self) {
4+
return NULL;
5+
}
6+
7+
PikaObj* cJSON_Utils_createObject(PikaObj *self){
8+
return NULL;
9+
}

port/linux/package/pikascript/pikascript-lib/cJSON/cJSON_cJSON.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "cJSON_cJSON.h"
22

33
char* cJSON_cJSON_print(PikaObj *self){
4-
4+
return NULL;
55
}
66

77
void cJSON_cJSON___init__(PikaObj *self){
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
import test_module2
22
import test_module3
3+
4+
35
def mytest():
46
print('test_module_1_hello')
7+
8+
9+
def test_module_import():
10+
print('in test module 2')
11+
test_module2.mytest()

port/linux/test/pikaMain-test.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2164,3 +2164,36 @@ TEST(pikaMain, string_str) {
21642164
obj_deinit(self);
21652165
EXPECT_EQ(pikaMemNow(), 0);
21662166
}
2167+
2168+
TEST(pikaMain, module_import_from_module) {
2169+
pikaMemInfo.heapUsedMax = 0;
2170+
PikaObj* self = newRootObj("pikaMain", New_PikaMain);
2171+
extern unsigned char pikaModules_py_a[];
2172+
obj_linkLibrary(self, pikaModules_py_a);
2173+
__platform_printf("BEGIN\r\n");
2174+
obj_run(self,
2175+
"import test_module1\n"
2176+
"test_module1.test_module_import()\n"
2177+
);
2178+
EXPECT_STREQ(log_buff[0], "test_module_2_hello\r\n");
2179+
EXPECT_STREQ(log_buff[1], "in test module 2\r\n");
2180+
EXPECT_STREQ(log_buff[2], "BEGIN\r\n");
2181+
obj_deinit(self);
2182+
EXPECT_EQ(pikaMemNow(), 0);
2183+
}
2184+
2185+
TEST(pikaMain, module_1_module2_test) {
2186+
pikaMemInfo.heapUsedMax = 0;
2187+
PikaObj* self = newRootObj("pikaMain", New_PikaMain);
2188+
extern unsigned char pikaModules_py_a[];
2189+
obj_linkLibrary(self, pikaModules_py_a);
2190+
__platform_printf("BEGIN\r\n");
2191+
obj_run(self,
2192+
"import test_module1\n"
2193+
"test_module1.test_module2.mytest()\n"
2194+
);
2195+
EXPECT_STREQ(log_buff[0], "test_module_2_hello\r\n");
2196+
EXPECT_STREQ(log_buff[1], "BEGIN\r\n");
2197+
obj_deinit(self);
2198+
EXPECT_EQ(pikaMemNow(), 0);
2199+
}

src/PikaObj.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1025,7 +1025,7 @@ int obj_runModule(PikaObj* self, char* module_name) {
10251025

10261026
int obj_importModule(PikaObj* self, char* module_name) {
10271027
/* import bytecode of the module */
1028-
uint8_t* bytecode = obj_getByteCodeFromModule(self, module_name);
1028+
uint8_t* bytecode = obj_getByteCodeFromModule(__pikaMain, module_name);
10291029
if (NULL == bytecode) {
10301030
return 1;
10311031
}

src/PikaVM.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -948,12 +948,11 @@ static Arg* VM_instruction_handler_IMP(PikaObj* self, VMState* vs, char* data) {
948948
return NULL;
949949
}
950950
/* import module from '__lib' */
951-
if (0 != obj_importModule(__pikaMain, data)) {
952-
VMState_setErrorCode(vs, 3);
953-
__platform_printf("ModuleNotFoundError: No module named '%s'\r\n",
954-
data);
951+
if (0 == obj_importModule(self, data)) {
955952
return NULL;
956953
}
954+
VMState_setErrorCode(vs, 3);
955+
__platform_printf("ModuleNotFoundError: No module named '%s'\r\n", data);
957956
return NULL;
958957
}
959958

@@ -1393,7 +1392,8 @@ void VMState_solveUnusedStack(VMState* vs) {
13931392
continue;
13941393
}
13951394
if (argType_isObject(type)) {
1396-
__platform_printf("<object at 0x%02x>\r\n", (uintptr_t)arg_getPtr(arg));
1395+
__platform_printf("<object at 0x%02x>\r\n",
1396+
(uintptr_t)arg_getPtr(arg));
13971397
}
13981398
if (type == ARG_TYPE_INT) {
13991399
__platform_printf("%d\r\n", (int)arg_getInt(arg));

0 commit comments

Comments
 (0)