Compare commits

...

3 Commits

Author SHA1 Message Date
blueloveTH
f530d8d111 Update modules.c 2025-03-06 19:53:45 +08:00
blueloveTH
8be90c3ba5 ... 2025-03-06 19:48:04 +08:00
blueloveTH
6b61c85dac ... 2025-03-06 17:02:05 +08:00
8 changed files with 83 additions and 39 deletions

View File

@ -20,7 +20,7 @@
typedef struct TraceInfo {
SourceLocation prev_loc;
py_TraceFunc tracefunc;
py_TraceFunc func;
} TraceInfo;
typedef struct VM {
@ -44,7 +44,7 @@ typedef struct VM {
void* ctx; // user-defined context
py_StackRef curr_class;
py_StackRef curr_function;
py_StackRef curr_decl_based_function;
TraceInfo trace_info;
py_TValue vectorcall_buffer[PK_MAX_CO_VARNAMES];

View File

@ -55,9 +55,9 @@ typedef struct py_Frame py_Frame;
// An enum for tracing events.
enum py_TraceEvent {
TRACE_EVENT_LINE,
TRACE_EVENT_CALL,
TRACE_EVENT_RETURN,
TRACE_EVENT_EXCEPTION,
TRACE_EVENT_PUSH,
TRACE_EVENT_POP,
};
typedef void (*py_TraceFunc)(py_Frame* frame, enum py_TraceEvent);
@ -108,6 +108,12 @@ PK_API void py_sys_settrace(py_TraceFunc func);
/// Setup the callbacks for the current VM.
PK_API py_Callbacks* py_callbacks();
/// Get the current source location of the frame.
PK_API const char* py_Frame_sourceloc(py_Frame* frame, int* lineno);
/// Get the function object of the frame.
/// Returns `NULL` if not available.
PK_API py_StackRef py_Frame_function(py_Frame* frame);
/// Run a source string.
/// @param source source string.
/// @param filename filename (for error messages).
@ -385,7 +391,9 @@ PK_API py_StackRef py_inspect_currentfunction();
/// Get the current `module` object where the code is executed.
/// Return `NULL` if not available.
PK_API py_GlobalRef py_inspect_currentmodule();
/// Get the current frame object.
/// Return `NULL` if not available.
PK_API py_Frame* py_inspect_currentframe();
/************* Bindings *************/
/// Bind a function to the object via "decl-based" style.

View File

@ -66,13 +66,7 @@ static bool stack_format_object(VM* self, c11_sv spec);
FrameResult res = VM__vectorcall(self, (argc), (kwargc), true); \
switch(res) { \
case RES_RETURN: PUSH(&self->last_retval); break; \
case RES_CALL: { \
frame = self->top_frame; \
if(self->trace_info.tracefunc) { \
self->trace_info.tracefunc(frame, TRACE_EVENT_CALL); \
} \
goto __NEXT_FRAME; \
} \
case RES_CALL: frame = self->top_frame; goto __NEXT_FRAME; \
case RES_ERROR: goto __ERROR; \
default: c11__unreachable(); \
} \
@ -105,14 +99,14 @@ FrameResult VM__run_top_frame(VM* self) {
__NEXT_STEP:
byte = codes[frame->ip];
if(self->trace_info.tracefunc) {
if(self->trace_info.func) {
SourceLocation loc = Frame__source_location(frame);
SourceLocation prev_loc = self->trace_info.prev_loc;
if(loc.lineno != prev_loc.lineno || loc.src != prev_loc.src) {
if(prev_loc.src) PK_DECREF(prev_loc.src);
PK_INCREF(loc.src);
self->trace_info.prev_loc = loc;
self->trace_info.tracefunc(frame, TRACE_EVENT_LINE);
self->trace_info.func(frame, TRACE_EVENT_LINE);
}
}
@ -757,9 +751,6 @@ FrameResult VM__run_top_frame(VM* self) {
} else {
py_newnone(&self->last_retval);
}
if(self->trace_info.tracefunc) {
self->trace_info.tracefunc(frame, TRACE_EVENT_RETURN);
}
VM__pop_frame(self);
if(frame == base_frame) { // [ frameBase<- ]
return RES_RETURN;
@ -1459,7 +1450,7 @@ static bool stack_format_object(VM* self, c11_sv spec) {
void py_sys_settrace(py_TraceFunc func) {
TraceInfo* info = &pk_current_vm->trace_info;
info->tracefunc = func;
info->func = func;
if(info->prev_loc.src) {
PK_DECREF(info->prev_loc.src);
info->prev_loc.src = NULL;

View File

@ -48,11 +48,11 @@ UnwindTarget* UnwindTarget__new(UnwindTarget* next, int iblock, int offset) {
void UnwindTarget__delete(UnwindTarget* self) { PK_FREE(self); }
py_Frame* Frame__new(const CodeObject* co,
py_StackRef p0,
py_GlobalRef module,
py_Ref globals,
py_Ref locals,
bool is_locals_special) {
py_StackRef p0,
py_GlobalRef module,
py_Ref globals,
py_Ref locals,
bool is_locals_special) {
assert(module->type == tp_module);
assert(globals->type == tp_module || globals->type == tp_dict);
if(is_locals_special) {
@ -121,6 +121,7 @@ void Frame__gc_mark(py_Frame* self) {
int Frame__lineno(const py_Frame* self) {
int ip = self->ip;
if(ip < 0) return 0;
return c11__getitem(BytecodeEx, &self->co->codes_ex, ip).lineno;
}
@ -180,4 +181,16 @@ SourceLocation Frame__source_location(py_Frame* self) {
loc.lineno = Frame__lineno(self);
loc.src = self->co->src;
return loc;
}
const char* py_Frame_sourceloc(py_Frame* self, int* lineno) {
SourceLocation loc = Frame__source_location(self);
*lineno = loc.lineno;
return loc.src->filename->data;
}
py_StackRef py_Frame_function(py_Frame* self) {
if(self->is_locals_special) return NULL;
assert(self->p0->type == tp_function);
return self->p0;
}

View File

@ -75,7 +75,7 @@ void VM__ctor(VM* self) {
self->ctx = NULL;
self->curr_class = NULL;
self->curr_function = NULL;
self->curr_decl_based_function = NULL;
memset(&self->trace_info, 0, sizeof(TraceInfo));
FixedMemoryPool__ctor(&self->pool_frame, sizeof(py_Frame), 32);
@ -265,13 +265,14 @@ void VM__dtor(VM* self) {
void VM__push_frame(VM* self, py_Frame* frame) {
frame->f_back = self->top_frame;
self->top_frame = frame;
if(self->trace_info.func) self->trace_info.func(frame, TRACE_EVENT_PUSH);
}
void VM__pop_frame(VM* self) {
assert(self->top_frame);
py_Frame* frame = self->top_frame;
if(self->trace_info.func) self->trace_info.func(frame, TRACE_EVENT_POP);
// reset stack pointer
self->stack.sp = frame->p0;
// pop frame and delete
self->top_frame = frame->f_back;
@ -491,10 +492,10 @@ FrameResult VM__vectorcall(VM* self, uint16_t argc, uint16_t kwargc, bool opcall
return opcall ? RES_CALL : VM__run_top_frame(self);
} else {
// decl-based binding
self->curr_function = p0;
self->curr_decl_based_function = p0;
bool ok = py_callcfunc(fn->cfunc, co->nlocals, argv);
self->stack.sp = p0;
self->curr_function = NULL;
self->curr_decl_based_function = NULL;
return ok ? RES_RETURN : RES_ERROR;
}
}
@ -520,10 +521,10 @@ FrameResult VM__vectorcall(VM* self, uint16_t argc, uint16_t kwargc, bool opcall
return opcall ? RES_CALL : VM__run_top_frame(self);
} else {
// decl-based binding
self->curr_function = p0;
self->curr_decl_based_function = p0;
bool ok = py_callcfunc(fn->cfunc, co->nlocals, argv);
self->stack.sp = p0;
self->curr_function = NULL;
self->curr_decl_based_function = NULL;
return ok ? RES_RETURN : RES_ERROR;
}
case FuncType_GENERATOR: {

View File

@ -797,10 +797,34 @@ static bool function__doc__(int argc, py_Ref argv) {
return true;
}
static bool function__name__(int argc, py_Ref argv) {
PY_CHECK_ARGC(1);
Function* func = py_touserdata(py_arg(0));
py_newstr(py_retval(), func->decl->code.name->data);
return true;
}
static bool function__repr__(int argc, py_Ref argv) {
// <function f at 0x10365b9c0>
PY_CHECK_ARGC(1);
Function* func = py_touserdata(py_arg(0));
c11_sbuf buf;
c11_sbuf__ctor(&buf);
c11_sbuf__write_cstr(&buf, "<function ");
c11_sbuf__write_cstr(&buf, func->decl->code.name->data);
c11_sbuf__write_cstr(&buf, " at ");
c11_sbuf__write_ptr(&buf, func);
c11_sbuf__write_char(&buf, '>');
c11_sbuf__py_submit(&buf, py_retval());
return true;
}
py_Type pk_function__register() {
py_Type type =
pk_newtype("function", tp_object, NULL, (void (*)(void*))Function__dtor, false, true);
py_bindproperty(type, "__doc__", function__doc__, NULL);
py_bindproperty(type, "__name__", function__name__, NULL);
py_bindmagic(type, __repr__, function__repr__);
return type;
}

View File

@ -154,7 +154,7 @@ void py_clearexc(py_StackRef p0) {
vm->is_curr_exc_handled = false;
/* Don't clear this, because StopIteration() may corrupt the class definition */
// vm->curr_class = NULL;
vm->curr_function = NULL;
vm->curr_decl_based_function = NULL;
if(p0) vm->stack.sp = p0;
}
@ -251,9 +251,10 @@ bool py_raise(py_Ref exc) {
}
vm->curr_exception = *exc;
vm->is_curr_exc_handled = false;
if(vm->trace_info.tracefunc && !py_istype(exc, tp_StopIteration)) {
if(vm->trace_info.func && !py_istype(exc, tp_StopIteration)) {
py_Frame* frame = vm->top_frame;
vm->trace_info.tracefunc(frame, TRACE_EVENT_EXCEPTION);
vm->trace_info.func(frame, TRACE_EVENT_EXCEPTION);
}
return false;
}

View File

@ -29,15 +29,15 @@ void py_setdict(py_Ref self, py_Name name, py_Ref val) {
}
}
py_ItemRef py_emplacedict(py_Ref self, py_Name name){
py_ItemRef py_emplacedict(py_Ref self, py_Name name) {
py_setdict(self, name, py_NIL());
return py_getdict(self, name);
}
bool py_applydict(py_Ref self, bool (*f)(py_Name, py_Ref, void *), void *ctx){
bool py_applydict(py_Ref self, bool (*f)(py_Name, py_Ref, void*), void* ctx) {
assert(self && self->is_ptr);
NameDict* dict = PyObject__dict(self->_obj);
for(int i = 0; i < dict->length; i++){
for(int i = 0; i < dict->length; i++) {
NameDict_KV* kv = c11__at(NameDict_KV, dict, i);
bool ok = f(kv->key, &kv->value, ctx);
if(!ok) return false;
@ -68,16 +68,22 @@ void py_setslot(py_Ref self, int i, py_Ref val) {
PyObject__slots(self->_obj)[i] = *val;
}
py_StackRef py_inspect_currentfunction(){
return pk_current_vm->curr_function;
py_StackRef py_inspect_currentfunction() {
VM* vm = pk_current_vm;
if(vm->curr_decl_based_function) { return vm->curr_decl_based_function; }
py_Frame* frame = vm->top_frame;
if(!frame || frame->is_locals_special) return NULL;
return frame->p0;
}
py_GlobalRef py_inspect_currentmodule(){
py_GlobalRef py_inspect_currentmodule() {
py_Frame* frame = pk_current_vm->top_frame;
if(!frame) return NULL;
return frame->module;
}
py_Frame* py_inspect_currentframe() { return pk_current_vm->top_frame; }
void py_assign(py_Ref dst, py_Ref src) { *dst = *src; }
/* Stack References */
@ -111,7 +117,7 @@ void py_pushnone() {
py_newnone(vm->stack.sp++);
}
void py_pushname(py_Name name){
void py_pushname(py_Name name) {
VM* vm = pk_current_vm;
py_newint(vm->stack.sp++, name);
}