implement settrace

This commit is contained in:
blueloveTH 2025-03-06 11:59:50 +08:00
parent 483e6fcb57
commit db7c577c94
2 changed files with 17 additions and 5 deletions

View File

@ -66,7 +66,13 @@ static bool stack_format_object(VM* self, c11_sv spec);
FrameResult res = VM__vectorcall(self, (argc), (kwargc), true); \ FrameResult res = VM__vectorcall(self, (argc), (kwargc), true); \
switch(res) { \ switch(res) { \
case RES_RETURN: PUSH(&self->last_retval); break; \ case RES_RETURN: PUSH(&self->last_retval); break; \
case RES_CALL: frame = self->top_frame; goto __NEXT_FRAME; \ case RES_CALL: { \
frame = self->top_frame; \
if(self->trace_info.tracefunc) { \
self->trace_info.tracefunc((py_Frame*)frame, TRACE_EVENT_CALL); \
} \
goto __NEXT_FRAME; \
} \
case RES_ERROR: goto __ERROR; \ case RES_ERROR: goto __ERROR; \
default: c11__unreachable(); \ default: c11__unreachable(); \
} \ } \
@ -751,6 +757,9 @@ FrameResult VM__run_top_frame(VM* self) {
} else { } else {
py_newnone(&self->last_retval); py_newnone(&self->last_retval);
} }
if(self->trace_info.tracefunc) {
self->trace_info.tracefunc((py_Frame*)frame, TRACE_EVENT_RETURN);
}
VM__pop_frame(self); VM__pop_frame(self);
if(frame == base_frame) { // [ frameBase<- ] if(frame == base_frame) { // [ frameBase<- ]
return RES_RETURN; return RES_RETURN;

View File

@ -17,7 +17,6 @@ typedef struct BaseException {
c11_vector /*T=BaseExceptionFrame*/ stacktrace; c11_vector /*T=BaseExceptionFrame*/ stacktrace;
} BaseException; } BaseException;
void py_BaseException__stpush(py_Ref self, SourceData_ src, int lineno, const char* func_name) { void py_BaseException__stpush(py_Ref self, SourceData_ src, int lineno, const char* func_name) {
BaseException* ud = py_touserdata(self); BaseException* ud = py_touserdata(self);
if(ud->stacktrace.length >= 7) return; if(ud->stacktrace.length >= 7) return;
@ -84,13 +83,13 @@ static bool _py_BaseException__str__(int argc, py_Ref argv) {
return true; return true;
} }
static bool BaseException_args(int argc, py_Ref argv){ static bool BaseException_args(int argc, py_Ref argv) {
PY_CHECK_ARGC(1); PY_CHECK_ARGC(1);
py_Ref arg = py_getslot(argv, 0); py_Ref arg = py_getslot(argv, 0);
if(!py_isnil(arg)) { if(!py_isnil(arg)) {
py_Ref p = py_newtuple(py_retval(), 1); py_Ref p = py_newtuple(py_retval(), 1);
p[0] = *arg; p[0] = *arg;
}else{ } else {
py_newtuple(py_retval(), 0); py_newtuple(py_retval(), 0);
} }
return true; return true;
@ -101,7 +100,7 @@ static bool StopIteration_value(int argc, py_Ref argv) {
py_Ref arg = py_getslot(argv, 0); py_Ref arg = py_getslot(argv, 0);
if(py_isnil(arg)) { if(py_isnil(arg)) {
py_newnone(py_retval()); py_newnone(py_retval());
}else{ } else {
py_assign(py_retval(), arg); py_assign(py_retval(), arg);
} }
return true; return true;
@ -252,6 +251,10 @@ bool py_raise(py_Ref exc) {
} }
vm->curr_exception = *exc; vm->curr_exception = *exc;
vm->is_curr_exc_handled = false; vm->is_curr_exc_handled = false;
if(vm->trace_info.tracefunc && !py_istype(exc, tp_StopIteration)) {
Frame* frame = vm->top_frame;
vm->trace_info.tracefunc((py_Frame*)frame, TRACE_EVENT_EXCEPTION);
}
return false; return false;
} }