mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 19:40:18 +00:00
backup
This commit is contained in:
parent
9af9d228dd
commit
2ba131a252
@ -6,7 +6,7 @@
|
|||||||
#include "pocketpy/objects/object.h"
|
#include "pocketpy/objects/object.h"
|
||||||
#include "pocketpy/pocketpy.h"
|
#include "pocketpy/pocketpy.h"
|
||||||
|
|
||||||
py_TValue* FastLocals__try_get_by_name(py_TValue* locals, const CodeObject* co, py_Name name);
|
void FastLocals__to_dict(py_TValue* locals, const CodeObject* co) PY_RETURN;
|
||||||
NameDict* FastLocals__to_namedict(py_TValue* locals, const CodeObject* co);
|
NameDict* FastLocals__to_namedict(py_TValue* locals, const CodeObject* co);
|
||||||
|
|
||||||
typedef struct ValueStack {
|
typedef struct ValueStack {
|
||||||
@ -17,7 +17,7 @@ typedef struct ValueStack {
|
|||||||
} ValueStack;
|
} ValueStack;
|
||||||
|
|
||||||
void ValueStack__ctor(ValueStack* self);
|
void ValueStack__ctor(ValueStack* self);
|
||||||
void ValueStack__clear(ValueStack* self);
|
void ValueStack__dtor(ValueStack* self);
|
||||||
|
|
||||||
typedef struct UnwindTarget {
|
typedef struct UnwindTarget {
|
||||||
struct UnwindTarget* next;
|
struct UnwindTarget* next;
|
||||||
@ -58,7 +58,7 @@ bool Frame__setglobal(Frame* self, py_Name name, py_TValue* val) PY_RAISE;
|
|||||||
int Frame__delglobal(Frame* self, py_Name name) PY_RAISE;
|
int Frame__delglobal(Frame* self, py_Name name) PY_RAISE;
|
||||||
|
|
||||||
int Frame__getlocal(Frame* self, py_Name name) PY_RAISE PY_RETURN;
|
int Frame__getlocal(Frame* self, py_Name name) PY_RAISE PY_RETURN;
|
||||||
int Frame__setlocal(Frame* self, py_Name name, py_TValue* val) PY_RAISE;
|
bool Frame__setlocal(Frame* self, py_Name name, py_TValue* val) PY_RAISE;
|
||||||
int Frame__dellocal(Frame* self, py_Name name) PY_RAISE;
|
int Frame__dellocal(Frame* self, py_Name name) PY_RAISE;
|
||||||
|
|
||||||
py_Ref Frame__getclosure(Frame* self, py_Name name);
|
py_Ref Frame__getclosure(Frame* self, py_Name name);
|
||||||
|
@ -131,7 +131,6 @@ py_Type pk_staticmethod__register();
|
|||||||
py_Type pk_classmethod__register();
|
py_Type pk_classmethod__register();
|
||||||
py_Type pk_generator__register();
|
py_Type pk_generator__register();
|
||||||
py_Type pk_namedict__register();
|
py_Type pk_namedict__register();
|
||||||
py_Type pk_locals__register();
|
|
||||||
py_Type pk_code__register();
|
py_Type pk_code__register();
|
||||||
|
|
||||||
py_TValue pk_builtins__register();
|
py_TValue pk_builtins__register();
|
||||||
|
@ -135,7 +135,6 @@ PK_API bool py_compile(const char* source,
|
|||||||
/// Python equivalent to `globals()`.
|
/// Python equivalent to `globals()`.
|
||||||
PK_API void py_newglobals(py_OutRef);
|
PK_API void py_newglobals(py_OutRef);
|
||||||
/// Python equivalent to `locals()`.
|
/// Python equivalent to `locals()`.
|
||||||
/// @return a temporary object, which expires on the associated function return.
|
|
||||||
PK_API void py_newlocals(py_OutRef);
|
PK_API void py_newlocals(py_OutRef);
|
||||||
|
|
||||||
/************* Values Creation *************/
|
/************* Values Creation *************/
|
||||||
|
@ -11,12 +11,21 @@ void ValueStack__ctor(ValueStack* self) {
|
|||||||
self->end = self->begin + PK_VM_STACK_SIZE;
|
self->end = self->begin + PK_VM_STACK_SIZE;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ValueStack__clear(ValueStack* self) { self->sp = self->begin; }
|
void ValueStack__dtor(ValueStack* self) { self->sp = self->begin; }
|
||||||
|
|
||||||
py_TValue* FastLocals__try_get_by_name(py_TValue* locals, const CodeObject* co, py_Name name) {
|
void FastLocals__to_dict(py_TValue* locals, const CodeObject* co) {
|
||||||
int index = c11_smallmap_n2i__get(&co->varnames_inv, name, -1);
|
py_StackRef dict = py_pushtmp();
|
||||||
if(index == -1) return NULL;
|
py_newdict(dict);
|
||||||
return &locals[index];
|
c11__foreach(c11_smallmap_n2i_KV, &co->varnames_inv, entry) {
|
||||||
|
py_TValue* value = &locals[entry->value];
|
||||||
|
if(!py_isnil(value)) {
|
||||||
|
bool ok = py_dict_setitem(dict, py_name2ref(entry->key), value);
|
||||||
|
assert(ok);
|
||||||
|
(void)ok;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
py_assign(py_retval(), dict);
|
||||||
|
py_pop();
|
||||||
}
|
}
|
||||||
|
|
||||||
NameDict* FastLocals__to_namedict(py_TValue* locals, const CodeObject* co) {
|
NameDict* FastLocals__to_namedict(py_TValue* locals, const CodeObject* co) {
|
||||||
@ -151,20 +160,13 @@ int Frame__delglobal(Frame* self, py_Name name) {
|
|||||||
|
|
||||||
int Frame__getlocal(Frame* self, py_Name name) {
|
int Frame__getlocal(Frame* self, py_Name name) {
|
||||||
if(self->is_locals_proxy) {
|
if(self->is_locals_proxy) {
|
||||||
py_StackRef p0 = py_peek(0);
|
if(self->locals->type == tp_locals) {
|
||||||
py_push(self->locals);
|
self = self->locals->_ptr;
|
||||||
py_pushmethod(__getitem__);
|
|
||||||
py_push(py_name2ref(name));
|
|
||||||
bool ok = py_vectorcall(1, 0);
|
|
||||||
if(!ok) {
|
|
||||||
if(py_matchexc(tp_KeyError)) {
|
|
||||||
py_clearexc(p0);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
return 1;
|
|
||||||
} else {
|
} else {
|
||||||
|
assert(self->locals->type == tp_dict);
|
||||||
|
return py_dict_getitem(self->locals, py_name2ref(name));
|
||||||
|
}
|
||||||
|
}
|
||||||
py_Ref slot = Frame__getlocal_noproxy(self, name);
|
py_Ref slot = Frame__getlocal_noproxy(self, name);
|
||||||
if(slot == NULL) return 0; // bad slot
|
if(slot == NULL) return 0; // bad slot
|
||||||
if(py_isnil(slot)) {
|
if(py_isnil(slot)) {
|
||||||
@ -173,42 +175,32 @@ int Frame__getlocal(Frame* self, py_Name name) {
|
|||||||
}
|
}
|
||||||
py_assign(py_retval(), slot);
|
py_assign(py_retval(), slot);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int Frame__setlocal(Frame* self, py_Name name, py_TValue* val) {
|
bool Frame__setlocal(Frame* self, py_Name name, py_TValue* val) {
|
||||||
if(self->is_locals_proxy) {
|
if(self->is_locals_proxy) {
|
||||||
py_push(self->locals);
|
if(self->locals->type == tp_locals) {
|
||||||
py_pushmethod(__setitem__);
|
self = self->locals->_ptr;
|
||||||
py_push(py_name2ref(name));
|
|
||||||
py_push(val);
|
|
||||||
bool ok = py_vectorcall(2, 0);
|
|
||||||
if(!ok) return -1;
|
|
||||||
return 1;
|
|
||||||
} else {
|
} else {
|
||||||
py_Ref slot = Frame__getlocal_noproxy(self, name);
|
assert(self->locals->type == tp_dict);
|
||||||
if(slot == NULL) return 0; // bad slot
|
return py_dict_setitem(self->locals, py_name2ref(name), val);
|
||||||
*slot = *val;
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
py_Ref slot = Frame__getlocal_noproxy(self, name);
|
||||||
|
if(slot == NULL) return false; // bad slot
|
||||||
|
*slot = *val;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Frame__dellocal(Frame* self, py_Name name) {
|
int Frame__dellocal(Frame* self, py_Name name) {
|
||||||
if(self->is_locals_proxy) {
|
if(self->is_locals_proxy) {
|
||||||
py_StackRef p0 = py_peek(0);
|
if(self->locals->type == tp_locals) {
|
||||||
py_push(self->locals);
|
self = self->locals->_ptr;
|
||||||
py_pushmethod(__delitem__);
|
|
||||||
py_push(py_name2ref(name));
|
|
||||||
bool ok = py_vectorcall(1, 0);
|
|
||||||
if(!ok) {
|
|
||||||
if(py_matchexc(tp_KeyError)) {
|
|
||||||
py_clearexc(p0);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
return 1;
|
|
||||||
} else {
|
} else {
|
||||||
|
assert(self->locals->type == tp_dict);
|
||||||
|
return py_dict_delitem(self->locals, py_name2ref(name));
|
||||||
|
}
|
||||||
|
}
|
||||||
py_Ref slot = Frame__getlocal_noproxy(self, name);
|
py_Ref slot = Frame__getlocal_noproxy(self, name);
|
||||||
if(slot == NULL) return 0; // bad slot
|
if(slot == NULL) return 0; // bad slot
|
||||||
if(py_isnil(slot)) {
|
if(py_isnil(slot)) {
|
||||||
@ -217,12 +209,13 @@ int Frame__dellocal(Frame* self, py_Name name) {
|
|||||||
}
|
}
|
||||||
py_newnil(slot);
|
py_newnil(slot);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
py_StackRef Frame__getlocal_noproxy(Frame* self, py_Name name) {
|
py_StackRef Frame__getlocal_noproxy(Frame* self, py_Name name) {
|
||||||
assert(!self->is_locals_proxy);
|
assert(!self->is_locals_proxy);
|
||||||
return FastLocals__try_get_by_name(self->locals, self->co, name);
|
int index = c11_smallmap_n2i__get(&self->co->varnames_inv, name, -1);
|
||||||
|
if(index == -1) return NULL;
|
||||||
|
return &self->locals[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
py_Ref Frame__getclosure(Frame* self, py_Name name) {
|
py_Ref Frame__getclosure(Frame* self, py_Name name) {
|
||||||
|
@ -127,7 +127,7 @@ void VM__ctor(VM* self) {
|
|||||||
validate(tp_Exception, pk_Exception__register());
|
validate(tp_Exception, pk_Exception__register());
|
||||||
validate(tp_bytes, pk_bytes__register());
|
validate(tp_bytes, pk_bytes__register());
|
||||||
validate(tp_namedict, pk_namedict__register());
|
validate(tp_namedict, pk_namedict__register());
|
||||||
validate(tp_locals, pk_locals__register());
|
validate(tp_locals, pk_newtype("locals", tp_object, NULL, NULL, false, true));
|
||||||
validate(tp_code, pk_code__register());
|
validate(tp_code, pk_code__register());
|
||||||
|
|
||||||
validate(tp_dict, pk_dict__register());
|
validate(tp_dict, pk_dict__register());
|
||||||
@ -258,7 +258,7 @@ void VM__dtor(VM* self) {
|
|||||||
ModuleDict__dtor(&self->modules);
|
ModuleDict__dtor(&self->modules);
|
||||||
TypeList__dtor(&self->types);
|
TypeList__dtor(&self->types);
|
||||||
FixedMemoryPool__dtor(&self->pool_frame);
|
FixedMemoryPool__dtor(&self->pool_frame);
|
||||||
ValueStack__clear(&self->stack);
|
ValueStack__dtor(&self->stack);
|
||||||
InternedNames__dtor(&self->names);
|
InternedNames__dtor(&self->names);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -511,11 +511,17 @@ void py_newlocals(py_Ref out) {
|
|||||||
py_newglobals(out);
|
py_newglobals(out);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(!frame->is_locals_proxy) {
|
if(frame->is_locals_proxy) {
|
||||||
pk_mappingproxy__locals(out, frame);
|
if(frame->locals->type == tp_locals) {
|
||||||
|
frame = frame->locals->_ptr;
|
||||||
} else {
|
} else {
|
||||||
|
assert(frame->locals->type == tp_dict);
|
||||||
*out = *frame->locals;
|
*out = *frame->locals;
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
FastLocals__to_dict(frame->locals, frame->co);
|
||||||
|
py_assign(out, py_retval());
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool _builtins_execdyn(const char* title, int argc, py_Ref argv, enum py_CompileMode mode) {
|
static bool _builtins_execdyn(const char* title, int argc, py_Ref argv, enum py_CompileMode mode) {
|
||||||
|
@ -116,59 +116,3 @@ void pk_mappingproxy__locals(py_Ref out, Frame* frame) {
|
|||||||
// locals() will expire when the frame is destroyed
|
// locals() will expire when the frame is destroyed
|
||||||
out->_ptr = frame;
|
out->_ptr = frame;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool locals__getitem__(int argc, py_Ref argv) {
|
|
||||||
PY_CHECK_ARGC(2);
|
|
||||||
PY_CHECK_ARG_TYPE(1, tp_str);
|
|
||||||
Frame* frame = argv->_ptr;
|
|
||||||
py_Name name = py_namev(py_tosv(py_arg(1)));
|
|
||||||
py_Ref slot = Frame__getlocal_noproxy(frame, name);
|
|
||||||
if(!slot || py_isnil(slot)) return KeyError(py_arg(1));
|
|
||||||
py_assign(py_retval(), slot);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool locals__setitem__(int argc, py_Ref argv) {
|
|
||||||
PY_CHECK_ARGC(3);
|
|
||||||
PY_CHECK_ARG_TYPE(1, tp_str);
|
|
||||||
Frame* frame = argv->_ptr;
|
|
||||||
py_Name name = py_namev(py_tosv(py_arg(1)));
|
|
||||||
py_Ref slot = Frame__getlocal_noproxy(frame, name);
|
|
||||||
if(!slot) return KeyError(py_arg(1));
|
|
||||||
py_assign(slot, py_arg(2));
|
|
||||||
py_newnone(py_retval());
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool locals__delitem__(int argc, py_Ref argv) {
|
|
||||||
PY_CHECK_ARGC(2);
|
|
||||||
PY_CHECK_ARG_TYPE(1, tp_str);
|
|
||||||
Frame* frame = argv->_ptr;
|
|
||||||
py_Name name = py_namev(py_tosv(py_arg(1)));
|
|
||||||
py_Ref res = Frame__getlocal_noproxy(frame, name);
|
|
||||||
if(!res || py_isnil(res)) return KeyError(py_arg(1));
|
|
||||||
py_newnil(res);
|
|
||||||
py_newnone(py_retval());
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool locals__contains__(int argc, py_Ref argv) {
|
|
||||||
PY_CHECK_ARGC(2);
|
|
||||||
PY_CHECK_ARG_TYPE(1, tp_str);
|
|
||||||
Frame* frame = argv->_ptr;
|
|
||||||
py_Name name = py_namev(py_tosv(py_arg(1)));
|
|
||||||
py_Ref slot = Frame__getlocal_noproxy(frame, name);
|
|
||||||
py_newbool(py_retval(), slot && !py_isnil(slot));
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
py_Type pk_locals__register() {
|
|
||||||
py_Type type = pk_newtype("locals", tp_object, NULL, NULL, false, true);
|
|
||||||
|
|
||||||
py_bindmagic(type, __getitem__, locals__getitem__);
|
|
||||||
py_bindmagic(type, __setitem__, locals__setitem__);
|
|
||||||
py_bindmagic(type, __delitem__, locals__delitem__);
|
|
||||||
py_bindmagic(type, __contains__, locals__contains__);
|
|
||||||
py_newnone(py_tpgetmagic(type, __hash__));
|
|
||||||
return type;
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user