This commit is contained in:
blueloveTH 2024-07-30 12:24:47 +08:00
parent 1bbba50003
commit db0acc854c
6 changed files with 18 additions and 25 deletions

View File

@ -38,9 +38,9 @@ typedef struct pk_VM {
py_TValue builtins; // builtins module py_TValue builtins; // builtins module
py_TValue main; // __main__ module py_TValue main; // __main__ module
void (*_ceval_on_step)(Frame*, Bytecode); void (*ceval_on_step)(Frame*, Bytecode);
unsigned char* (*_import_file)(const char*); unsigned char* (*import_file)(const char*);
void (*_stdout)(const char*, ...); void (*print)(const char*);
py_TValue last_retval; py_TValue last_retval;
py_TValue curr_exception; py_TValue curr_exception;

View File

@ -117,7 +117,8 @@ pk_FrameResult pk_VM__run_top_frame(pk_VM* self) {
if(TOP()->type != tp_NoneType) { if(TOP()->type != tp_NoneType) {
bool ok = py_repr(TOP()); bool ok = py_repr(TOP());
if(!ok) goto __ERROR; if(!ok) goto __ERROR;
self->_stdout("%s\n", py_tostr(&self->last_retval)); self->print(py_tostr(&self->last_retval));
self->print("\n");
} }
POP(); POP();
DISPATCH(); DISPATCH();

View File

@ -11,13 +11,7 @@
static unsigned char* pk_default_import_file(const char* path) { return NULL; } static unsigned char* pk_default_import_file(const char* path) { return NULL; }
static void pk_default_stdout(const char* fmt, ...) { static void pk_default_print(const char* data) { printf("%s", data); }
va_list args;
va_start(args, fmt);
vprintf(fmt, args);
va_end(args);
fflush(stdout);
}
static void pk_TypeInfo__ctor(pk_TypeInfo* self, static void pk_TypeInfo__ctor(pk_TypeInfo* self,
py_Name name, py_Name name,
@ -54,9 +48,9 @@ void pk_VM__ctor(pk_VM* self) {
self->builtins = *py_NIL; self->builtins = *py_NIL;
self->main = *py_NIL; self->main = *py_NIL;
self->_ceval_on_step = NULL; self->ceval_on_step = NULL;
self->_import_file = pk_default_import_file; self->import_file = pk_default_import_file;
self->_stdout = pk_default_stdout; self->print = pk_default_print;
self->last_retval = *py_NIL; self->last_retval = *py_NIL;
self->curr_exception = *py_NIL; self->curr_exception = *py_NIL;
@ -168,10 +162,10 @@ void pk_VM__ctor(pk_VM* self) {
"ImportError", "ImportError",
"AssertionError", "AssertionError",
"KeyError", "KeyError",
NULL, // sentinel NULL, // sentinel
}; };
const char** it = builtin_exceptions; const char** it = builtin_exceptions;
while(*it){ while(*it) {
py_Type type = pk_newtype(*it, tp_Exception, &self->builtins, NULL, false, true); py_Type type = pk_newtype(*it, tp_Exception, &self->builtins, NULL, false, true);
py_setdict(&self->builtins, py_name(*it), py_tpobject(type)); py_setdict(&self->builtins, py_name(*it), py_tpobject(type));
it++; it++;
@ -269,9 +263,7 @@ bool pk__parse_int_slice(py_Ref slice, int length, int* start, int* stop, int* s
bool pk__normalize_index(int* index, int length) { bool pk__normalize_index(int* index, int length) {
if(*index < 0) *index += length; if(*index < 0) *index += length;
if(*index < 0 || *index >= length) { if(*index < 0 || *index >= length) { return IndexError("%d not in [0, %d)", *index, length); }
return IndexError("%d not in [0, %d)", *index, length);
}
return true; return true;
} }
@ -285,9 +277,7 @@ py_Type pk_newtype(const char* name,
py_Type index = types->count; py_Type index = types->count;
pk_TypeInfo* ti = c11_vector__emplace(types); pk_TypeInfo* ti = c11_vector__emplace(types);
pk_TypeInfo__ctor(ti, py_name(name), index, base, module ? *module : *py_NIL); pk_TypeInfo__ctor(ti, py_name(name), index, base, module ? *module : *py_NIL);
if(!dtor && base){ if(!dtor && base) { dtor = c11__at(pk_TypeInfo, types, base)->dtor; }
dtor = c11__at(pk_TypeInfo, types, base)->dtor;
}
ti->dtor = dtor; ti->dtor = dtor;
ti->is_python = is_python; ti->is_python = is_python;
ti->is_sealed = is_sealed; ti->is_sealed = is_sealed;

View File

@ -193,7 +193,7 @@ static bool _py_builtins__print(int argc, py_Ref argv) {
} }
c11_sbuf__write_sv(&buf, end); c11_sbuf__write_sv(&buf, end);
c11_string* res = c11_sbuf__submit(&buf); c11_string* res = c11_sbuf__submit(&buf);
pk_current_vm->_stdout("%s", res->data); pk_current_vm->print(res->data);
c11_string__delete(res); c11_string__delete(res);
py_newnone(py_retval()); py_newnone(py_retval());
return true; return true;

View File

@ -122,7 +122,8 @@ bool py_checkexc() {
void py_printexc() { void py_printexc() {
char* msg = py_formatexc(); char* msg = py_formatexc();
if(!msg) return; if(!msg) return;
pk_current_vm->_stdout("%s\n", msg); pk_current_vm->print(msg);
pk_current_vm->print("\n");
free(msg); free(msg);
} }

View File

@ -168,7 +168,8 @@ static void disassemble(CodeObject* co) {
} }
c11_string* output = c11_sbuf__submit(&ss); c11_string* output = c11_sbuf__submit(&ss);
pk_current_vm->_stdout("%s\n", output->data); pk_current_vm->print(output->data);
pk_current_vm->print("\n");
c11_string__delete(output); c11_string__delete(output);
c11_vector__dtor(&jumpTargets); c11_vector__dtor(&jumpTargets);
} }