blueloveTH 1 год назад
Родитель
Сommit
e04d4e790c

+ 1 - 0
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, ...);

+ 6 - 0
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;

+ 2 - 6
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;
 }

+ 2 - 4
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;
 }
 

+ 1 - 3
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;
 }
 

+ 3 - 9
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);

+ 1 - 3
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;
 }
 

+ 1 - 3
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;
 }
 

+ 2 - 6
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, "<class '%t'>", 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;
 }
 

+ 1 - 3
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;
 }
 

+ 4 - 12
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;
 }
 

+ 1 - 3
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;
 }