From e04d4e790ca8f9dfba29e8872c2af030ac6f9d99 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sat, 3 Aug 2024 14:46:24 +0800 Subject: [PATCH] ... --- include/pocketpy/common/sstream.h | 1 + src/common/sstream.c | 6 ++++++ src/interpreter/ceval.c | 8 ++------ src/public/modules.c | 6 ++---- src/public/py_dict.c | 4 +--- src/public/py_exception.c | 12 +++--------- src/public/py_list.c | 4 +--- src/public/py_number.c | 4 +--- src/public/py_object.c | 8 ++------ src/public/py_slice.c | 4 +--- src/public/py_str.c | 16 ++++------------ src/public/py_tuple.c | 4 +--- 12 files changed, 25 insertions(+), 52 deletions(-) diff --git a/include/pocketpy/common/sstream.h b/include/pocketpy/common/sstream.h index 08e9ca4f..ec0d09d2 100644 --- a/include/pocketpy/common/sstream.h +++ b/include/pocketpy/common/sstream.h @@ -30,6 +30,7 @@ void c11_sbuf__write_hex(c11_sbuf* self, unsigned char, bool non_zero); void c11_sbuf__write_ptr(c11_sbuf* self, void*); // Submit the stream and return the final string. The stream becomes invalid after this call c11_string* c11_sbuf__submit(c11_sbuf* self); +void c11_sbuf__py_submit(c11_sbuf* self, py_Ref out); void pk_vsprintf(c11_sbuf* ss, const char* fmt, va_list args); void pk_sprintf(c11_sbuf* ss, const char* fmt, ...); diff --git a/src/common/sstream.c b/src/common/sstream.c index 122eea98..7f82afba 100644 --- a/src/common/sstream.c +++ b/src/common/sstream.c @@ -147,6 +147,12 @@ c11_string* c11_sbuf__submit(c11_sbuf* self) { return retval; } +void c11_sbuf__py_submit(c11_sbuf *self, py_Ref out){ + c11_string* res = c11_sbuf__submit(self); + py_newstrn(out, res->data, res->size); + c11_string__delete(res); +} + void pk_vsprintf(c11_sbuf* ss, const char* fmt, va_list args) { while(*fmt) { char c = *fmt; diff --git a/src/interpreter/ceval.c b/src/interpreter/ceval.c index 72fa35ee..460d00e9 100644 --- a/src/interpreter/ceval.c +++ b/src/interpreter/ceval.c @@ -502,9 +502,7 @@ pk_FrameResult pk_VM__run_top_frame(pk_VM* self) { c11_sbuf__write_sv(&ss, py_tosv(&self->last_retval)); } SP() = begin; - c11_string* res = c11_sbuf__submit(&ss); - py_newstrn(SP()++, res->data, res->size); - c11_string__delete(res); + c11_sbuf__py_submit(&ss, SP()++); DISPATCH(); } /*****************************/ @@ -1109,8 +1107,6 @@ static bool format_object(py_Ref val, c11_sv spec) { } c11_string__delete(body); - c11_string* res = c11_sbuf__submit(&buf); - py_newstrn(py_retval(), res->data, res->size); - c11_string__delete(res); + c11_sbuf__py_submit(&buf, py_retval()); return true; } \ No newline at end of file diff --git a/src/public/modules.c b/src/public/modules.c index bbdc6ae4..2ed73157 100644 --- a/src/public/modules.c +++ b/src/public/modules.c @@ -109,10 +109,8 @@ static bool _py_builtins__hex(int argc, py_Ref argv) { c11_sbuf__write_hex(&ss, cpnt, non_zero); if(cpnt != 0) non_zero = false; } - // return VAR(ss.str()); - c11_string* res = c11_sbuf__submit(&ss); - py_newstrn(py_retval(), res->data, res->size); - c11_string__delete(res); + + c11_sbuf__py_submit(&ss, py_retval()); return true; } diff --git a/src/public/py_dict.c b/src/public/py_dict.c index ab37a956..025dc521 100644 --- a/src/public/py_dict.c +++ b/src/public/py_dict.c @@ -288,9 +288,7 @@ static bool _py_dict__repr__(int argc, py_Ref argv) { is_first = false; } c11_sbuf__write_char(&buf, '}'); - c11_string* res = c11_sbuf__submit(&buf); - py_newstrn(py_retval(), res->data, res->size); - c11_string__delete(res); + c11_sbuf__py_submit(&buf, py_retval()); return true; } diff --git a/src/public/py_exception.c b/src/public/py_exception.c index 96d07f74..d8169926 100644 --- a/src/public/py_exception.c +++ b/src/public/py_exception.c @@ -78,9 +78,7 @@ static bool _py_BaseException__repr__(int argc, py_Ref argv) { c11_sbuf__write_sv(&ss, py_tosv(py_retval())); } c11_sbuf__write_char(&ss, ')'); - c11_string* res = c11_sbuf__submit(&ss); - py_newstrn(py_retval(), res->data, res->size); - c11_string__delete(res); + c11_sbuf__py_submit(&ss, py_retval()); return true; } @@ -92,9 +90,7 @@ static bool _py_BaseException__str__(int argc, py_Ref argv) { if(!py_str(arg)) return false; c11_sbuf__write_sv(&ss, py_tosv(py_retval())); } - c11_string* res = c11_sbuf__submit(&ss); - py_newstrn(py_retval(), res->data, res->size); - c11_string__delete(res); + c11_sbuf__py_submit(&ss, py_retval()); return true; } @@ -172,10 +168,8 @@ bool py_exception(const char* name, const char* fmt, ...) { pk_vsprintf(&buf, fmt, args); va_end(args); - c11_string* res = c11_sbuf__submit(&buf); py_Ref message = py_pushtmp(); - py_newstrn(message, res->data, res->size); - c11_string__delete(res); + c11_sbuf__py_submit(&buf, message); py_Ref exc_type = py_getdict(&pk_current_vm->builtins, py_name(name)); if(exc_type == NULL) c11__abort("py_exception(): '%s' not found", name); diff --git a/src/public/py_list.c b/src/public/py_list.c index 6af84251..46b0c3e0 100644 --- a/src/public/py_list.c +++ b/src/public/py_list.c @@ -239,9 +239,7 @@ static bool _py_list__repr__(int argc, py_Ref argv) { if(i != self->count - 1) c11_sbuf__write_cstr(&buf, ", "); } c11_sbuf__write_char(&buf, ']'); - c11_string* res = c11_sbuf__submit(&buf); - py_newstrn(py_retval(), res->data, res->size); - c11_string__delete(res); + c11_sbuf__py_submit(&buf, py_retval()); return true; } diff --git a/src/public/py_number.c b/src/public/py_number.c index d7d208b2..8d7cf937 100644 --- a/src/public/py_number.c +++ b/src/public/py_number.c @@ -201,9 +201,7 @@ static bool _py_float__repr__(int argc, py_Ref argv) { c11_sbuf buf; c11_sbuf__ctor(&buf); c11_sbuf__write_f64(&buf, val, -1); - c11_string* res = c11_sbuf__submit(&buf); - py_newstrn(py_retval(), res->data, res->size); - c11_string__delete(res); + c11_sbuf__py_submit(&buf, py_retval()); return true; } diff --git a/src/public/py_object.c b/src/public/py_object.c index 0d886d6e..e2022813 100644 --- a/src/public/py_object.c +++ b/src/public/py_object.c @@ -40,9 +40,7 @@ static bool _py_object__repr__(int argc, py_Ref argv) { c11_sbuf buf; c11_sbuf__ctor(&buf); pk_sprintf(&buf, "<%t object at %p>", argv->type, argv->_obj); - c11_string* res = c11_sbuf__submit(&buf); - py_newstrn(py_retval(), res->data, res->size); - c11_string__delete(res); + c11_sbuf__py_submit(&buf, py_retval()); return true; } @@ -51,9 +49,7 @@ static bool _py_type__repr__(int argc, py_Ref argv) { c11_sbuf buf; c11_sbuf__ctor(&buf); pk_sprintf(&buf, "", py_totype(argv)); - c11_string* res = c11_sbuf__submit(&buf); - py_newstrn(py_retval(), res->data, res->size); - c11_string__delete(res); + c11_sbuf__py_submit(&buf, py_retval()); return true; } diff --git a/src/public/py_slice.c b/src/public/py_slice.c index 6029ef70..50da145b 100644 --- a/src/public/py_slice.c +++ b/src/public/py_slice.c @@ -38,9 +38,7 @@ static bool _py_slice__repr__(int argc, py_Ref argv) { if(i != 2) c11_sbuf__write_cstr(&buf, ", "); } c11_sbuf__write_char(&buf, ')'); - c11_string* res = c11_sbuf__submit(&buf); - py_newstrn(py_retval(), res->data, res->size); - c11_string__delete(res); + c11_sbuf__py_submit(&buf, py_retval()); return true; } diff --git a/src/public/py_str.c b/src/public/py_str.c index 83b1acac..2eebc86d 100644 --- a/src/public/py_str.c +++ b/src/public/py_str.c @@ -154,9 +154,7 @@ static bool _py_str__repr__(int argc, py_Ref argv) { c11_sbuf buf; c11_sbuf__ctor(&buf); c11_sbuf__write_quoted(&buf, py_tosv(&argv[0]), '\''); - c11_string* res = c11_sbuf__submit(&buf); - py_newstrn(py_retval(), res->data, res->size); - c11_string__delete(res); + c11_sbuf__py_submit(&buf, py_retval()); return true; } @@ -294,9 +292,7 @@ static bool _py_str__join(int argc, py_Ref argv) { c11_string* item = py_touserdata(&p[i]); c11_sbuf__write_cstrn(&buf, item->data, item->size); } - c11_string* res = c11_sbuf__submit(&buf); - py_newstrn(py_retval(), res->data, res->size); - c11_string__delete(res); + c11_sbuf__py_submit(&buf, py_retval()); return true; } @@ -391,9 +387,7 @@ static bool _py_str__zfill(int argc, py_Ref argv) { c11_sbuf__write_char(&buf, '0'); } c11_sbuf__write_sv(&buf, self); - c11_string* res = c11_sbuf__submit(&buf); - py_newstrn(py_retval(), res->data, res->size); - c11_string__delete(res); + c11_sbuf__py_submit(&buf, py_retval()); return true; } @@ -429,9 +423,7 @@ static bool _py_str__widthjust_impl(bool left, int argc, py_Ref argv) { } c11_sbuf__write_sv(&buf, self); } - c11_string* res = c11_sbuf__submit(&buf); - py_newstrn(py_retval(), res->data, res->size); - c11_string__delete(res); + c11_sbuf__py_submit(&buf, py_retval()); return true; } diff --git a/src/public/py_tuple.c b/src/public/py_tuple.c index 6cec6d8c..95a8aa6d 100644 --- a/src/public/py_tuple.c +++ b/src/public/py_tuple.c @@ -44,9 +44,7 @@ static bool _py_tuple__repr__(int argc, py_Ref argv) { } if(length == 1) c11_sbuf__write_char(&buf, ','); c11_sbuf__write_char(&buf, ')'); - c11_string* res = c11_sbuf__submit(&buf); - py_newstrn(py_retval(), res->data, res->size); - c11_string__delete(res); + c11_sbuf__py_submit(&buf, py_retval()); return true; }