This commit is contained in:
blueloveTH 2024-08-08 17:39:10 +08:00
parent ad1482370b
commit 41a938386a
5 changed files with 42 additions and 24 deletions

View File

@ -46,7 +46,6 @@ typedef struct VM {
py_TValue reg[8]; // users' registers py_TValue reg[8]; // users' registers
py_TValue* __curr_class; py_TValue* __curr_class;
FuncDecl_ __dynamic_func_decl;
py_TValue __vectorcall_buffer[PK_MAX_CO_VARNAMES]; py_TValue __vectorcall_buffer[PK_MAX_CO_VARNAMES];
ManagedHeap heap; ManagedHeap heap;

View File

@ -75,6 +75,15 @@ bool py_exec(const char* source,
enum py_CompileMode mode, enum py_CompileMode mode,
py_Ref module) PY_RAISE; py_Ref module) PY_RAISE;
/// Run a source string in dynamic mode.
/// Assume `globals()` and `locals()` are pushed to the stack.
/// After the execution, the result will be set to `py_retval()`.
/// The stack size will be reduced by 2.
bool py_execdynamic(const char* source,
const char* filename,
enum py_CompileMode mode,
py_Ref module) PY_RAISE;
/************* Values Creation *************/ /************* Values Creation *************/
/// Create an `int` object. /// Create an `int` object.

View File

@ -290,14 +290,8 @@ FrameResult VM__run_top_frame(VM* self) {
if(slot != NULL) { if(slot != NULL) {
*slot = *TOP(); // store in locals if possible *slot = *TOP(); // store in locals if possible
} else { } else {
// Function& func = frame->_callable->as<Function>(); NameError(name);
// if(func.decl == __dynamic_func_decl) { goto __ERROR;
// assert(func._closure != nullptr);
// func._closure->set(_name, _0);
// } else {
// NameError(_name);
// goto __ERROR;
// }
} }
} else { } else {
py_setdict(frame->module, name, TOP()); py_setdict(frame->module, name, TOP());
@ -351,14 +345,8 @@ FrameResult VM__run_top_frame(VM* self) {
if(slot) { if(slot) {
py_newnil(slot); py_newnil(slot);
} else { } else {
// Function& func = frame->_callable->as<Function>(); NameError(name);
// if(func.decl == __dynamic_func_decl) { goto __ERROR;
// assert(func._closure != nullptr);
// bool ok = func._closure->del(_name);
// if(!ok) vm->NameError(_name);
// } else {
// vm->NameError(_name);
// }
} }
} else { } else {
bool ok = py_deldict(frame->module, name); bool ok = py_deldict(frame->module, name);

View File

@ -71,7 +71,6 @@ void VM__ctor(VM* self) {
self->is_stopiteration = false; self->is_stopiteration = false;
self->__curr_class = NULL; self->__curr_class = NULL;
self->__dynamic_func_decl = NULL;
ManagedHeap__ctor(&self->heap, self); ManagedHeap__ctor(&self->heap, self);
ValueStack__ctor(&self->stack); ValueStack__ctor(&self->stack);
@ -212,7 +211,6 @@ void VM__ctor(VM* self) {
} }
void VM__dtor(VM* self) { void VM__dtor(VM* self) {
if(self->__dynamic_func_decl) { PK_DECREF(self->__dynamic_func_decl); }
// destroy all objects // destroy all objects
ManagedHeap__dtor(&self->heap); ManagedHeap__dtor(&self->heap);
// clear frames // clear frames

View File

@ -77,10 +77,14 @@ const char* pk_opname(Opcode op) {
return OP_NAMES[op]; return OP_NAMES[op];
} }
bool py_exec(const char* source, const char* filename, enum py_CompileMode mode, py_Ref module) { static bool _py_exec(const char* source,
const char* filename,
enum py_CompileMode mode,
py_Ref module,
bool is_dynamic) {
VM* vm = pk_current_vm; VM* vm = pk_current_vm;
CodeObject co; CodeObject co;
SourceData_ src = SourceData__rcnew(source, filename, mode, false); SourceData_ src = SourceData__rcnew(source, filename, mode, is_dynamic);
Error* err = pk_compile(src, &co); Error* err = pk_compile(src, &co);
if(err) { if(err) {
py_exception(tp_SyntaxError, err->msg); py_exception(tp_SyntaxError, err->msg);
@ -93,7 +97,13 @@ bool py_exec(const char* source, const char* filename, enum py_CompileMode mode,
if(!module) module = &vm->main; if(!module) module = &vm->main;
Frame* frame = Frame__new(&co, module, false, vm->stack.sp, vm->stack.sp); py_StackRef sp = vm->stack.sp;
if(is_dynamic) {
// [globals, locals]
sp -= 2;
}
Frame* frame = Frame__new(&co, module, false, sp, sp);
VM__push_frame(vm, frame); VM__push_frame(vm, frame);
FrameResult res = VM__run_top_frame(vm); FrameResult res = VM__run_top_frame(vm);
CodeObject__dtor(&co); CodeObject__dtor(&co);
@ -103,6 +113,17 @@ bool py_exec(const char* source, const char* filename, enum py_CompileMode mode,
c11__unreachedable(); c11__unreachedable();
} }
bool py_exec(const char* source, const char* filename, enum py_CompileMode mode, py_Ref module) {
return _py_exec(source, filename, mode, module, false);
}
bool py_execdynamic(const char* source,
const char* filename,
enum py_CompileMode mode,
py_Ref module) {
return _py_exec(source, filename, mode, module, true);
}
bool py_call(py_Ref f, int argc, py_Ref argv) { bool py_call(py_Ref f, int argc, py_Ref argv) {
if(f->type == tp_nativefunc) { if(f->type == tp_nativefunc) {
return py_callcfunc(f->_cfunc, argc, argv); return py_callcfunc(f->_cfunc, argc, argv);
@ -120,8 +141,11 @@ bool py_callcfunc(py_CFunction f, int argc, py_Ref argv) {
py_newnil(py_retval()); py_newnil(py_retval());
bool ok = f(argc, argv); bool ok = f(argc, argv);
if(!ok) return false; if(!ok) return false;
if(py_peek(0) != p0) c11__abort("py_CFunction corrupts the stack! Did you forget to call `py_pop()`?"); if(py_peek(0) != p0)
if(py_isnil(py_retval())) c11__abort("py_CFunction returns nothing! Did you forget to call `py_newnone(py_retval())`?"); c11__abort("py_CFunction corrupts the stack! Did you forget to call `py_pop()`?");
if(py_isnil(py_retval()))
c11__abort(
"py_CFunction returns nothing! Did you forget to call `py_newnone(py_retval())`?");
return true; return true;
} }