mirror of
https://github.com/pocketpy/pocketpy
synced 2025-12-11 12:40:18 +00:00
Compare commits
2 Commits
8720c22c8a
...
2d5e515ae7
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2d5e515ae7 | ||
|
|
8b0e1697c0 |
@ -51,7 +51,8 @@ typedef struct VM {
|
|||||||
|
|
||||||
py_TValue reg[8]; // users' registers
|
py_TValue reg[8]; // users' registers
|
||||||
|
|
||||||
py_TValue* __curr_class;
|
py_StackRef __curr_class;
|
||||||
|
py_StackRef __curr_function;
|
||||||
py_TValue __vectorcall_buffer[PK_MAX_CO_VARNAMES];
|
py_TValue __vectorcall_buffer[PK_MAX_CO_VARNAMES];
|
||||||
|
|
||||||
ManagedHeap heap;
|
ManagedHeap heap;
|
||||||
@ -76,10 +77,10 @@ bool pk_wrapper__self(int argc, py_Ref argv);
|
|||||||
bool pk_wrapper__NotImplementedError(int argc, py_Ref argv);
|
bool pk_wrapper__NotImplementedError(int argc, py_Ref argv);
|
||||||
|
|
||||||
typedef enum FrameResult {
|
typedef enum FrameResult {
|
||||||
RES_RETURN,
|
RES_ERROR = 0,
|
||||||
RES_CALL,
|
RES_RETURN = 1,
|
||||||
RES_YIELD,
|
RES_CALL = 2,
|
||||||
RES_ERROR,
|
RES_YIELD = 3,
|
||||||
} FrameResult;
|
} FrameResult;
|
||||||
|
|
||||||
FrameResult VM__run_top_frame(VM* self);
|
FrameResult VM__run_top_frame(VM* self);
|
||||||
|
|||||||
@ -310,8 +310,9 @@ PK_EXPORT void py_setslot(py_Ref self, int i, py_Ref val);
|
|||||||
|
|
||||||
/************* Inspection *************/
|
/************* Inspection *************/
|
||||||
|
|
||||||
/// Get the current `function` object from the stack.
|
/// Get the current `function` object on the stack.
|
||||||
/// Return `NULL` if not available.
|
/// Return `NULL` if not available.
|
||||||
|
/// NOTE: This function should be placed at the beginning of your decl-based bindings.
|
||||||
PK_EXPORT py_StackRef py_inspect_currentfunction();
|
PK_EXPORT py_StackRef py_inspect_currentfunction();
|
||||||
/// Get the current `module` object where the code is executed.
|
/// Get the current `module` object where the code is executed.
|
||||||
/// Return `NULL` if not available.
|
/// Return `NULL` if not available.
|
||||||
@ -586,6 +587,7 @@ enum py_MagicNames {
|
|||||||
};
|
};
|
||||||
|
|
||||||
enum py_PredefinedTypes {
|
enum py_PredefinedTypes {
|
||||||
|
tp_nil = 0,
|
||||||
tp_object = 1,
|
tp_object = 1,
|
||||||
tp_type, // py_Type
|
tp_type, // py_Type
|
||||||
tp_int,
|
tp_int,
|
||||||
|
|||||||
@ -70,6 +70,7 @@ void VM__ctor(VM* self) {
|
|||||||
self->is_curr_exc_handled = false;
|
self->is_curr_exc_handled = false;
|
||||||
|
|
||||||
self->__curr_class = NULL;
|
self->__curr_class = NULL;
|
||||||
|
self->__curr_function = NULL;
|
||||||
|
|
||||||
ManagedHeap__ctor(&self->heap, self);
|
ManagedHeap__ctor(&self->heap, self);
|
||||||
ValueStack__ctor(&self->stack);
|
ValueStack__ctor(&self->stack);
|
||||||
@ -346,7 +347,7 @@ static bool
|
|||||||
return TypeError("%s() takes %d positional arguments but %d were given",
|
return TypeError("%s() takes %d positional arguments but %d were given",
|
||||||
co->name->data,
|
co->name->data,
|
||||||
decl_argc,
|
decl_argc,
|
||||||
p1 - argv);
|
(int)(p1 - argv));
|
||||||
}
|
}
|
||||||
|
|
||||||
py_TValue* t = argv;
|
py_TValue* t = argv;
|
||||||
@ -419,10 +420,9 @@ FrameResult VM__vectorcall(VM* self, uint16_t argc, uint16_t kwargc, bool opcall
|
|||||||
// [unbound, self, args..., kwargs...]
|
// [unbound, self, args..., kwargs...]
|
||||||
}
|
}
|
||||||
|
|
||||||
py_Ref argv = py_isnil(p0 + 1) ? p0 + 2 : p0 + 1;
|
py_Ref argv = p0 + 1 + (int)py_isnil(p0 + 1);
|
||||||
|
|
||||||
if(p0->type == tp_function) {
|
if(p0->type == tp_function) {
|
||||||
/*****************_py_call*****************/
|
|
||||||
// check stack overflow
|
// check stack overflow
|
||||||
if(self->stack.sp > self->stack.end) {
|
if(self->stack.sp > self->stack.end) {
|
||||||
py_exception(tp_StackOverflowError, "");
|
py_exception(tp_StackOverflowError, "");
|
||||||
@ -441,18 +441,22 @@ FrameResult VM__vectorcall(VM* self, uint16_t argc, uint16_t kwargc, bool opcall
|
|||||||
memcpy(argv, self->__vectorcall_buffer, co->nlocals * sizeof(py_TValue));
|
memcpy(argv, self->__vectorcall_buffer, co->nlocals * sizeof(py_TValue));
|
||||||
// submit the call
|
// submit the call
|
||||||
if(!fn->cfunc) {
|
if(!fn->cfunc) {
|
||||||
|
// python function
|
||||||
VM__push_frame(self, Frame__new(co, &fn->module, p0, argv, true));
|
VM__push_frame(self, Frame__new(co, &fn->module, p0, argv, true));
|
||||||
return opcall ? RES_CALL : VM__run_top_frame(self);
|
return opcall ? RES_CALL : VM__run_top_frame(self);
|
||||||
} else {
|
} else {
|
||||||
|
// decl-based binding
|
||||||
|
self->__curr_function = p0;
|
||||||
bool ok = py_callcfunc(fn->cfunc, co->nlocals, argv);
|
bool ok = py_callcfunc(fn->cfunc, co->nlocals, argv);
|
||||||
self->stack.sp = p0;
|
self->stack.sp = p0;
|
||||||
|
self->__curr_function = NULL;
|
||||||
return ok ? RES_RETURN : RES_ERROR;
|
return ok ? RES_RETURN : RES_ERROR;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case FuncType_SIMPLE:
|
case FuncType_SIMPLE:
|
||||||
if(p1 - argv != fn->decl->args.length) {
|
if(p1 - argv != fn->decl->args.length) {
|
||||||
const char* fmt = "%s() takes %d positional arguments but %d were given";
|
const char* fmt = "%s() takes %d positional arguments but %d were given";
|
||||||
TypeError(fmt, co->name->data, fn->decl->args.length, p1 - argv);
|
TypeError(fmt, co->name->data, fn->decl->args.length, (int)(p1 - argv));
|
||||||
return RES_ERROR;
|
return RES_ERROR;
|
||||||
}
|
}
|
||||||
if(kwargc) {
|
if(kwargc) {
|
||||||
@ -634,7 +638,7 @@ void pk_print_stack(VM* self, Frame* frame, Bytecode byte) {
|
|||||||
c11_sbuf__ctor(&buf);
|
c11_sbuf__ctor(&buf);
|
||||||
for(py_Ref p = self->stack.begin; p != sp; p++) {
|
for(py_Ref p = self->stack.begin; p != sp; p++) {
|
||||||
switch(p->type) {
|
switch(p->type) {
|
||||||
case 0: c11_sbuf__write_cstr(&buf, "nil"); break;
|
case tp_nil: c11_sbuf__write_cstr(&buf, "nil"); break;
|
||||||
case tp_int: c11_sbuf__write_i64(&buf, p->_i64); break;
|
case tp_int: c11_sbuf__write_i64(&buf, p->_i64); break;
|
||||||
case tp_float: c11_sbuf__write_f64(&buf, p->_f64, -1); break;
|
case tp_float: c11_sbuf__write_f64(&buf, p->_f64, -1); break;
|
||||||
case tp_bool: c11_sbuf__write_cstr(&buf, p->_bool ? "True" : "False"); break;
|
case tp_bool: c11_sbuf__write_cstr(&buf, p->_bool ? "True" : "False"); break;
|
||||||
|
|||||||
@ -153,6 +153,7 @@ void py_clearexc(py_StackRef p0) {
|
|||||||
vm->curr_exception = *py_NIL;
|
vm->curr_exception = *py_NIL;
|
||||||
vm->is_curr_exc_handled = false;
|
vm->is_curr_exc_handled = false;
|
||||||
vm->__curr_class = NULL;
|
vm->__curr_class = NULL;
|
||||||
|
vm->__curr_function = NULL;
|
||||||
if(p0) vm->stack.sp = p0;
|
if(p0) vm->stack.sp = p0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -70,9 +70,7 @@ void py_setslot(py_Ref self, int i, py_Ref val) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
py_StackRef py_inspect_currentfunction(){
|
py_StackRef py_inspect_currentfunction(){
|
||||||
Frame* frame = pk_current_vm->top_frame;
|
return pk_current_vm->__curr_function;
|
||||||
if(!frame || !frame->has_function) return NULL;
|
|
||||||
return frame->p0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
py_GlobalRef py_inspect_currentmodule(){
|
py_GlobalRef py_inspect_currentmodule(){
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user