fix leaks

This commit is contained in:
blueloveTH 2024-07-04 23:47:32 +08:00
parent 3da176fbfb
commit 68bc6ee269
4 changed files with 27 additions and 34 deletions

View File

@ -1076,6 +1076,11 @@ typedef struct AttribExpr {
py_Name name; py_Name name;
} AttribExpr; } AttribExpr;
void AttribExpr__dtor(Expr* self_) {
AttribExpr* self = (AttribExpr*)self_;
vtdelete(self->child);
}
void AttribExpr__emit_(Expr* self_, Ctx* ctx) { void AttribExpr__emit_(Expr* self_, Ctx* ctx) {
AttribExpr* self = (AttribExpr*)self_; AttribExpr* self = (AttribExpr*)self_;
vtemit_(self->child, ctx); vtemit_(self->child, ctx);
@ -1117,6 +1122,7 @@ AttribExpr* AttribExpr__new(int line, Expr* child, py_Name name) {
.emit_store = AttribExpr__emit_store, .emit_store = AttribExpr__emit_store,
.emit_inplace = AttribExpr__emit_inplace, .emit_inplace = AttribExpr__emit_inplace,
.emit_istore = AttribExpr__emit_istore, .emit_istore = AttribExpr__emit_istore,
.dtor = AttribExpr__dtor,
.is_attrib = true}; .is_attrib = true};
static_assert_expr_size(AttribExpr); static_assert_expr_size(AttribExpr);
AttribExpr* self = PoolExpr_alloc(); AttribExpr* self = PoolExpr_alloc();
@ -1218,7 +1224,6 @@ void Ctx__dtor(Ctx* self) {
for(int i = 0; i < self->s_expr.count; i++) { for(int i = 0; i < self->s_expr.count; i++) {
vtdelete(c11__getitem(Expr*, &self->s_expr, i)); vtdelete(c11__getitem(Expr*, &self->s_expr, i));
} }
c11_vector__clear(&self->s_expr);
c11_vector__dtor(&self->s_expr); c11_vector__dtor(&self->s_expr);
c11_smallmap_n2i__dtor(&self->global_names); c11_smallmap_n2i__dtor(&self->global_names);
c11_smallmap_s2n__dtor(&self->co_consts_string_dedup_map); c11_smallmap_s2n__dtor(&self->co_consts_string_dedup_map);
@ -1379,8 +1384,8 @@ void Ctx__emit_store_name(Ctx* self, NameScope scope, py_Name name, int line) {
void Ctx__s_emit_top(Ctx* self) { void Ctx__s_emit_top(Ctx* self) {
Expr* top = c11_vector__back(Expr*, &self->s_expr); Expr* top = c11_vector__back(Expr*, &self->s_expr);
vtemit_(top, self); vtemit_(top, self);
c11_vector__pop(&self->s_expr);
vtdelete(top); vtdelete(top);
c11_vector__pop(&self->s_expr);
} }
// push // push
@ -1394,15 +1399,16 @@ int Ctx__s_size(Ctx* self) { return self->s_expr.count; }
// pop -> delete // pop -> delete
void Ctx__s_pop(Ctx* self) { void Ctx__s_pop(Ctx* self) {
vtdelete(c11_vector__back(Expr*, &self->s_expr)); Expr* top = c11_vector__back(Expr*, &self->s_expr);
vtdelete(top);
c11_vector__pop(&self->s_expr); c11_vector__pop(&self->s_expr);
} }
// pop move // pop move
Expr* Ctx__s_popx(Ctx* self) { Expr* Ctx__s_popx(Ctx* self) {
Expr* e = c11_vector__back(Expr*, &self->s_expr); Expr* top = c11_vector__back(Expr*, &self->s_expr);
c11_vector__pop(&self->s_expr); c11_vector__pop(&self->s_expr);
return e; return top;
} }
/* compiler.c */ /* compiler.c */
@ -2313,10 +2319,8 @@ Error* pk_compile(pk_SourceData_ src, CodeObject* out) {
CodeObject__ctor(out, src, c11_string__sv(src->filename)); CodeObject__ctor(out, src, c11_string__sv(src->filename));
err = Compiler__compile(&compiler, out); err = Compiler__compile(&compiler, out);
if(err) { if(err) {
// if error occurs, dispose the code object // dispose the code object if error occurs
CodeObject__dtor(out); CodeObject__dtor(out);
} else {
assert(out->codes.count);
} }
Compiler__dtor(&compiler); Compiler__dtor(&compiler);
return err; return err;

View File

@ -5,7 +5,6 @@
#include "pocketpy/pocketpy.h" #include "pocketpy/pocketpy.h"
#include <stdbool.h> #include <stdbool.h>
static bool stack_binaryop(pk_VM* self, py_Name op, py_Name rop); static bool stack_binaryop(pk_VM* self, py_Name op, py_Name rop);
#define DISPATCH() \ #define DISPATCH() \
@ -267,9 +266,7 @@ pk_FrameResult pk_VM__run_top_frame(pk_VM* self) {
// fallback to getattr // fallback to getattr
int res = py_getattr(TOP(), byte.arg, TOP()); int res = py_getattr(TOP(), byte.arg, TOP());
if(res != 1) { if(res != 1) {
if(res == 0){ if(res == 0) { AttributeError(TOP(), byte.arg); }
AttributeError(TOP(), byte.arg);
}
goto __ERROR; goto __ERROR;
} }
} }
@ -669,17 +666,11 @@ pk_FrameResult pk_VM__run_top_frame(pk_VM* self) {
DISPATCH(); DISPATCH();
} }
// case OP_UNARY_STAR: TOP() = VAR(StarWrapper(byte.arg, TOP())); DISPATCH(); // case OP_UNARY_STAR: TOP() = VAR(StarWrapper(byte.arg, TOP())); DISPATCH();
// case OP_UNARY_INVERT: { case OP_UNARY_INVERT: {
// PyVar _0; if(!py_callmagic(__invert__, 1, TOP())) goto __ERROR;
// auto _ti = _tp_info(TOP()); *TOP() = self->last_retval;
// if(_ti->m__invert__) DISPATCH();
// _0 = _ti->m__invert__(this, TOP()); }
// else
// _0 = call_method(TOP(), __invert__);
// TOP() = _0;
// DISPATCH();
// }
default: PK_UNREACHABLE(); default: PK_UNREACHABLE();
} }

View File

@ -59,6 +59,8 @@ int main(int argc, char** argv) {
py_finalize(); py_finalize();
free(source); free(source);
return 0;
__HELP: __HELP:
printf("Usage: pocketpy [filename]\n"); printf("Usage: pocketpy [filename]\n");
return 0; return 0;

View File

@ -98,7 +98,3 @@ assert (-4)**13 == -67108864
assert ~3 == -4 assert ~3 == -4
assert ~-3 == 2 assert ~-3 == 2
assert ~0 == -1 assert ~0 == -1
# test __str__, __repr__
assert str(1) == '1'
assert repr(1) == '1'