add py_callbacks

This commit is contained in:
blueloveTH 2024-08-18 20:07:27 +08:00
parent 2d5e515ae7
commit f9d6321a09
9 changed files with 26 additions and 25 deletions

View File

@ -41,9 +41,7 @@ typedef struct 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); py_Callbacks callbacks;
char* (*import_file)(const char*);
void (*print)(const char*);
py_TValue last_retval; py_TValue last_retval;
py_TValue curr_exception; py_TValue curr_exception;

View File

@ -120,15 +120,6 @@ void FuncDecl__add_arg(FuncDecl* self, py_Name name);
void FuncDecl__add_kwarg(FuncDecl* self, py_Name name, const py_TValue* value); void FuncDecl__add_kwarg(FuncDecl* self, py_Name name, const py_TValue* value);
void FuncDecl__add_starred_arg(FuncDecl* self, py_Name name); void FuncDecl__add_starred_arg(FuncDecl* self, py_Name name);
void FuncDecl__add_starred_kwarg(FuncDecl* self, py_Name name); void FuncDecl__add_starred_kwarg(FuncDecl* self, py_Name name);
FuncDecl_ FuncDecl__build(c11_sv name,
c11_sv* args,
int argc,
c11_sv starred_arg,
c11_sv* kwargs,
int kwargc,
py_Ref kwdefaults, // a tuple contains default values
c11_sv starred_kwarg,
const char* docstring);
// runtime function // runtime function
typedef struct Function { typedef struct Function {

View File

@ -34,6 +34,14 @@ typedef struct c11_sv {
int size; int size;
} c11_sv; } c11_sv;
/// A struct contains the callbacks of the VM.
typedef struct py_Callbacks {
/// Used by `__import__` to load source code of a module.
char* (*importfile)(const char*);
/// Used by `print` to output a string.
void (*print)(const char*);
} py_Callbacks;
#define PY_RAISE #define PY_RAISE
#define PY_RETURN #define PY_RETURN
@ -84,6 +92,8 @@ PK_EXPORT int py_currentvm();
PK_EXPORT void py_switchvm(int index); PK_EXPORT void py_switchvm(int index);
/// Set `sys.argv`. Used for storing command-line arguments. /// Set `sys.argv`. Used for storing command-line arguments.
PK_EXPORT void py_sys_setargv(int argc, char** argv); PK_EXPORT void py_sys_setargv(int argc, char** argv);
/// Setup the callbacks for the current VM.
PK_EXPORT py_Callbacks* py_callbacks();
/// Run a source string. /// Run a source string.
/// @param source source string. /// @param source source string.

View File

@ -120,8 +120,8 @@ FrameResult VM__run_top_frame(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->print(py_tostr(&self->last_retval)); self->callbacks.print(py_tostr(&self->last_retval));
self->print("\n"); self->callbacks.print("\n");
} }
POP(); POP();
DISPATCH(); DISPATCH();

View File

@ -9,7 +9,7 @@
#include "pocketpy/pocketpy.h" #include "pocketpy/pocketpy.h"
#include <stdbool.h> #include <stdbool.h>
static char* pk_default_import_file(const char* path) { static char* pk_default_importfile(const char* path) {
#if PK_ENABLE_OS #if PK_ENABLE_OS
FILE* f = fopen(path, "rb"); FILE* f = fopen(path, "rb");
if(f == NULL) return NULL; if(f == NULL) return NULL;
@ -61,9 +61,8 @@ void VM__ctor(VM* self) {
self->builtins = *py_NIL; self->builtins = *py_NIL;
self->main = *py_NIL; self->main = *py_NIL;
self->ceval_on_step = NULL; self->callbacks.importfile = pk_default_importfile;
self->import_file = pk_default_import_file; self->callbacks.print = pk_default_print;
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;

View File

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

View File

@ -76,6 +76,9 @@ void py_sys_setargv(int argc, char** argv) {
} }
} }
py_Callbacks* py_getcallbacks() { return &pk_current_vm->callbacks; }
const char* pk_opname(Opcode op) { const char* pk_opname(Opcode op) {
const static char* OP_NAMES[] = { const static char* OP_NAMES[] = {
#define OPCODE(name) #name, #define OPCODE(name) #name,

View File

@ -130,12 +130,12 @@ int py_import(const char* path_cstr) {
goto __SUCCESS; goto __SUCCESS;
} }
data = vm->import_file(filename->data); data = vm->callbacks.importfile(filename->data);
if(data != NULL) goto __SUCCESS; if(data != NULL) goto __SUCCESS;
c11_string__delete(filename); c11_string__delete(filename);
filename = c11_string__new3("%s/__init__.py", slashed_path->data); filename = c11_string__new3("%s/__init__.py", slashed_path->data);
data = vm->import_file(filename->data); data = vm->callbacks.importfile(filename->data);
if(data != NULL) goto __SUCCESS; if(data != NULL) goto __SUCCESS;
c11_string__delete(filename); c11_string__delete(filename);
@ -290,7 +290,7 @@ static bool 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->print(res->data); pk_current_vm->callbacks.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

@ -160,8 +160,8 @@ void py_clearexc(py_StackRef p0) {
void py_printexc() { void py_printexc() {
char* msg = py_formatexc(); char* msg = py_formatexc();
if(!msg) return; if(!msg) return;
pk_current_vm->print(msg); pk_current_vm->callbacks.print(msg);
pk_current_vm->print("\n"); pk_current_vm->callbacks.print("\n");
free(msg); free(msg);
} }