Skip to content

Commit da24a68

Browse files
committed
support __del__() for C Module
1 parent eac28d2 commit da24a68

File tree

7 files changed

+51
-4
lines changed

7 files changed

+51
-4
lines changed

port/linux/package/pikascript/PikaMath.pyi

+1
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ class Operator(TinyObj):
1616
def OR(self, flag1: int, flag2: int) -> int: ...
1717
def NOT(self, flag: int) -> int: ...
1818
def __str__(self) -> str: ...
19+
def __del__(self): ...
+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import imp
22
from PikaObj import *
3+
import json
34

45

56
class cJSON(TinyObj):
67
def __init__(self): ...
8+
def print(self) -> str: ...
9+
10+
class Utils(TinyObj):
11+
def parse(self) -> cJSON: ...
12+
def createObject(self) -> cJSON: ...

port/linux/package/pikascript/pikascript-lib/PikaMath/PikaMath_Operator.c

+4
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,7 @@ char* PikaMath_Operator___str__(PikaObj *self){
4646
obj_setStr(self, "__buf", "test");
4747
return obj_getStr(self, "__buf");
4848
}
49+
50+
void PikaMath_Operator___del__(PikaObj *self){
51+
__platform_printf("del operator...\r\n");
52+
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
#include "cJSON_cJSON.h"
22

3-
void cJSON_cJSON___init__(PikaObj* self) {}
3+
char* cJSON_cJSON_print(PikaObj *self){
4+
5+
}
6+
7+
void cJSON_cJSON___init__(PikaObj *self){
8+
9+
}

port/linux/test/compile-test.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -565,3 +565,9 @@ TEST(compiler, __str__) {
565565
Parser_compilePyToBytecodeArray(lines);
566566
EXPECT_EQ(pikaMemNow(), 0);
567567
}
568+
569+
TEST(compiler, __del__) {
570+
char* lines = "__del__()";
571+
Parser_compilePyToBytecodeArray(lines);
572+
EXPECT_EQ(pikaMemNow(), 0);
573+
}

port/linux/test/pikaMain-test.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -1789,6 +1789,7 @@ TEST(pikaMain, CModule__str__) {
17891789
EXPECT_STREQ(log_buff[2], "BEGIN\r\n");
17901790
/* deinit */
17911791
obj_deinit(self);
1792+
EXPECT_STREQ(log_buff[0], "del operator...\r\n");
17921793
EXPECT_EQ(pikaMemNow(), 0);
17931794
}
17941795

@@ -2162,4 +2163,4 @@ TEST(pikaMain, string_str) {
21622163
/* deinit */
21632164
obj_deinit(self);
21642165
EXPECT_EQ(pikaMemNow(), 0);
2165-
}
2166+
}

src/PikaObj.c

+25-2
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ char* fast_itoa(char* buf, uint32_t val) {
9797
return &p[val < 10];
9898
}
9999

100-
int32_t obj_deinit(PikaObj* self) {
100+
static int32_t obj_deinit_no_del(PikaObj* self) {
101101
/* free the list */
102102
args_deinit(self->list);
103103
/* free the pointer */
@@ -106,6 +106,22 @@ int32_t obj_deinit(PikaObj* self) {
106106
return 0;
107107
}
108108

109+
int32_t obj_deinit(PikaObj* self) {
110+
Arg* del = obj_getMethodArg(self, "__del__");
111+
if (NULL != del) {
112+
const uint8_t bytes[] = {
113+
0x04, 0x00, /* instruct array size */
114+
0x00, 0x82, 0x01, 0x00, /* instruct array */
115+
0x09, 0x00, /* const pool size */
116+
0x00, 0x5f, 0x5f, 0x64, 0x65,
117+
0x6c, 0x5f, 0x5f, 0x00, /* const pool */
118+
};
119+
pikaVM_runByteCode(self, (uint8_t*)bytes);
120+
arg_deinit(del);
121+
}
122+
return obj_deinit_no_del(self);
123+
}
124+
109125
int32_t obj_enable(PikaObj* self) {
110126
obj_setInt(self, "isEnable", 1);
111127
return 0;
@@ -327,14 +343,21 @@ Arg* obj_getMethodArg(PikaObj* obj, char* methodPath) {
327343
goto exit;
328344
}
329345
methodHostClass = obj_getClassObj(obj);
346+
if (NULL == methodHostClass) {
347+
method = NULL;
348+
goto exit;
349+
}
330350
method = arg_copy(obj_getArg(methodHostClass, methodName));
331-
obj_deinit(methodHostClass);
351+
obj_deinit_no_del(methodHostClass);
332352
exit:
333353
return method;
334354
}
335355

336356
PikaObj* obj_getClassObj(PikaObj* obj) {
337357
NewFun classPtr = (NewFun)obj_getPtr(obj, "_clsptr");
358+
if (NULL == classPtr) {
359+
return NULL;
360+
}
338361
PikaObj* classObj = obj_getClassObjByNewFun(obj, "", classPtr);
339362
return classObj;
340363
}

0 commit comments

Comments
 (0)