mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-22 20:40:18 +00:00
up
This commit is contained in:
parent
efd98e6a6a
commit
bb67505613
@ -9,6 +9,8 @@ extern "C" {
|
|||||||
|
|
||||||
typedef uint16_t StrName;
|
typedef uint16_t StrName;
|
||||||
|
|
||||||
|
#define py_name(name) pk_StrName__map(#name)
|
||||||
|
|
||||||
uint16_t pk_StrName__map(const char*);
|
uint16_t pk_StrName__map(const char*);
|
||||||
uint16_t pk_StrName__map2(c11_string);
|
uint16_t pk_StrName__map2(c11_string);
|
||||||
const char* pk_StrName__rmap(uint16_t index);
|
const char* pk_StrName__rmap(uint16_t index);
|
||||||
|
@ -11,7 +11,7 @@
|
|||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
PyVar* FastLocals__try_get_by_name(PyVar* locals, const CodeObject* co, StrName name);
|
PyVar* FastLocals__try_get_by_name(PyVar* locals, const CodeObject* co, py_Name name);
|
||||||
pk_NameDict* FastLocals__to_namedict(PyVar* locals, const CodeObject* co);
|
pk_NameDict* FastLocals__to_namedict(PyVar* locals, const CodeObject* co);
|
||||||
|
|
||||||
typedef struct ValueStack {
|
typedef struct ValueStack {
|
||||||
@ -46,7 +46,7 @@ typedef struct Frame {
|
|||||||
} Frame;
|
} Frame;
|
||||||
|
|
||||||
|
|
||||||
Frame* Frame__new(const CodeObject* co, PyObject* module, PyObject* function, PyVar* p0, PyVar* locals, const CodeObject* locals_co);
|
Frame* Frame__new(const CodeObject* co, const PyVar* module, const PyVar* function, PyVar* p0, PyVar* locals, const CodeObject* locals_co);
|
||||||
void Frame__delete(Frame* self);
|
void Frame__delete(Frame* self);
|
||||||
|
|
||||||
PK_INLINE int Frame__ip(const Frame* self){
|
PK_INLINE int Frame__ip(const Frame* self){
|
||||||
@ -64,14 +64,14 @@ PK_INLINE int Frame__iblock(const Frame* self){
|
|||||||
}
|
}
|
||||||
|
|
||||||
PK_INLINE pk_NameDict* Frame__f_globals(Frame* self){
|
PK_INLINE pk_NameDict* Frame__f_globals(Frame* self){
|
||||||
return self->module->dict;
|
return PyObject__dict(self->module);
|
||||||
}
|
}
|
||||||
|
|
||||||
PK_INLINE PyVar* Frame__f_globals_try_get(Frame* self, StrName name){
|
PK_INLINE PyVar* Frame__f_globals_try_get(Frame* self, py_Name name){
|
||||||
return pk_NameDict__try_get(self->module->dict, name);
|
return pk_NameDict__try_get(Frame__f_globals(self), name);
|
||||||
}
|
}
|
||||||
|
|
||||||
PyVar* Frame__f_closure_try_get(Frame* self, StrName name);
|
PyVar* Frame__f_closure_try_get(Frame* self, py_Name name);
|
||||||
|
|
||||||
int Frame__prepare_jump_exception_handler(Frame* self, ValueStack*);
|
int Frame__prepare_jump_exception_handler(Frame* self, ValueStack*);
|
||||||
void Frame__prepare_jump_break(Frame* self, ValueStack*, int);
|
void Frame__prepare_jump_break(Frame* self, ValueStack*, int);
|
||||||
|
@ -28,8 +28,8 @@ void pk_ManagedHeap__collect_if_needed(pk_ManagedHeap* self);
|
|||||||
int pk_ManagedHeap__collect(pk_ManagedHeap* self);
|
int pk_ManagedHeap__collect(pk_ManagedHeap* self);
|
||||||
int pk_ManagedHeap__sweep(pk_ManagedHeap* self);
|
int pk_ManagedHeap__sweep(pk_ManagedHeap* self);
|
||||||
|
|
||||||
PyObject* pk_ManagedHeap__new(pk_ManagedHeap* self, Type type, int size);
|
PyObject* pk_ManagedHeap__new(pk_ManagedHeap* self, Type type, int slots, int size);
|
||||||
PyObject* pk_ManagedHeap__gcnew(pk_ManagedHeap* self, Type type, int size);
|
PyObject* pk_ManagedHeap__gcnew(pk_ManagedHeap* self, Type type, int slots, int size);
|
||||||
|
|
||||||
// external implementation
|
// external implementation
|
||||||
void pk_ManagedHeap__mark(pk_ManagedHeap* self);
|
void pk_ManagedHeap__mark(pk_ManagedHeap* self);
|
||||||
|
@ -9,7 +9,7 @@ extern "C" {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
typedef struct pk_TypeInfo{
|
typedef struct pk_TypeInfo{
|
||||||
StrName name;
|
py_Name name;
|
||||||
Type base;
|
Type base;
|
||||||
|
|
||||||
PyVar self; // the type object itself
|
PyVar self; // the type object itself
|
||||||
@ -38,7 +38,7 @@ typedef struct pk_TypeInfo{
|
|||||||
py_CFunction on_end_subclass; // for enum module
|
py_CFunction on_end_subclass; // for enum module
|
||||||
} pk_TypeInfo;
|
} pk_TypeInfo;
|
||||||
|
|
||||||
void pk_TypeInfo__ctor(pk_TypeInfo* self, StrName name, Type base, PyObject* obj, const PyVar* module, bool subclass_enabled);
|
void pk_TypeInfo__ctor(pk_TypeInfo* self, py_Name name, Type base, PyObject* obj, const PyVar* module, bool subclass_enabled);
|
||||||
void pk_TypeInfo__dtor(pk_TypeInfo* self);
|
void pk_TypeInfo__dtor(pk_TypeInfo* self);
|
||||||
|
|
||||||
typedef struct pk_VM {
|
typedef struct pk_VM {
|
||||||
|
@ -11,17 +11,24 @@ typedef struct PyObject{
|
|||||||
Type type; // we have a duplicated type here for convenience
|
Type type; // we have a duplicated type here for convenience
|
||||||
bool gc_is_large;
|
bool gc_is_large;
|
||||||
bool gc_marked;
|
bool gc_marked;
|
||||||
pk_NameDict* dict; // gc will delete this on destruction
|
int slots; // number of slots in the object
|
||||||
} PyObject;
|
} PyObject;
|
||||||
|
|
||||||
static_assert(sizeof(PyObject) <= 16, "!(sizeof(PyObject) <= 16)");
|
// slots >= 0, allocate N slots
|
||||||
|
// slots == -1, allocate a dict
|
||||||
|
|
||||||
#define PyObject__value_ptr(self) ((char*)self + 16)
|
// | 8 bytes HEADER | <N slots> | <value>
|
||||||
#define PyObject__as(T, self) (T*)(PyObject__value_ptr(self))
|
// | 8 bytes HEADER | <dict> | <value>
|
||||||
#define PK_OBJ_GET(T, val) (*(T*)(PyObject__value_ptr((val)._obj)))
|
|
||||||
#define PK_OBJ_SIZEOF(T) (sizeof(T) + 16)
|
|
||||||
|
|
||||||
PyObject* PyObject__new(Type type, int size);
|
static_assert(sizeof(PyObject) <= 8, "!(sizeof(PyObject) <= 8)");
|
||||||
|
|
||||||
|
PyVar* PyObject__slots(PyObject* self);
|
||||||
|
pk_NameDict* PyObject__dict(PyObject* self);
|
||||||
|
void* PyObject__value(PyObject* self);
|
||||||
|
|
||||||
|
#define PK_OBJ_HEADER_SIZE(slots) ((slots)>=0 ? 8+sizeof(PyVar)*(slots) : 8+sizeof(pk_NameDict))
|
||||||
|
|
||||||
|
PyObject* PyObject__new(Type type, int slots, int size);
|
||||||
void PyObject__delete(PyObject* self);
|
void PyObject__delete(PyObject* self);
|
||||||
|
|
||||||
PK_INLINE PyVar PyVar__fromobj(PyObject* obj){
|
PK_INLINE PyVar PyVar__fromobj(PyObject* obj){
|
||||||
|
@ -10,7 +10,14 @@ extern "C" {
|
|||||||
typedef struct PyObject PyObject;
|
typedef struct PyObject PyObject;
|
||||||
typedef struct PyVar PyVar;
|
typedef struct PyVar PyVar;
|
||||||
typedef struct pk_VM pk_VM;
|
typedef struct pk_VM pk_VM;
|
||||||
typedef struct py_Error py_Error;
|
typedef uint16_t py_Name;
|
||||||
|
typedef int16_t Type;
|
||||||
|
typedef PyVar* py_Ref;
|
||||||
|
typedef int (*py_CFunction)(const py_Ref, int);
|
||||||
|
|
||||||
|
typedef struct py_Error{
|
||||||
|
Type type;
|
||||||
|
} py_Error;
|
||||||
|
|
||||||
typedef enum BindType {
|
typedef enum BindType {
|
||||||
BindType_FUNCTION,
|
BindType_FUNCTION,
|
||||||
@ -18,55 +25,76 @@ typedef enum BindType {
|
|||||||
BindType_CLASSMETHOD,
|
BindType_CLASSMETHOD,
|
||||||
} BindType;
|
} BindType;
|
||||||
|
|
||||||
typedef int (*py_CFunction)(const PyVar*, int);
|
|
||||||
|
|
||||||
typedef uint16_t StrName;
|
|
||||||
typedef int16_t Type;
|
|
||||||
|
|
||||||
extern pk_VM* pk_current_vm;
|
extern pk_VM* pk_current_vm;
|
||||||
|
|
||||||
|
|
||||||
void py_initialize();
|
void py_initialize();
|
||||||
// void py_switch_vm(const char* name);
|
// void py_switch_vm(const char* name);
|
||||||
void py_finalize();
|
void py_finalize();
|
||||||
|
|
||||||
py_Error* py_exec_simple(const char*);
|
int py_exec_simple(const char*);
|
||||||
py_Error* py_eval_simple(const char*, PyVar*);
|
int py_eval_simple(const char*, py_Ref);
|
||||||
|
|
||||||
/* py_error */
|
/* py_error */
|
||||||
void py_Error__print(const py_Error*);
|
py_Error* py_getlasterror();
|
||||||
void py_Error__delete(py_Error*);
|
void py_Error__print(py_Error*);
|
||||||
|
|
||||||
int py_eq(const PyVar*, const PyVar*);
|
int py_eq(const py_Ref, const py_Ref);
|
||||||
int py_le(const PyVar*, const PyVar*);
|
int py_le(const py_Ref, const py_Ref);
|
||||||
int py_hash(const PyVar*, int64_t* out);
|
int py_hash(const py_Ref, int64_t* out);
|
||||||
|
|
||||||
/* py_var */
|
/* py_var */
|
||||||
void py_new_int(PyVar*, int64_t);
|
void py_new_int(py_Ref, int64_t);
|
||||||
void py_new_float(PyVar*, double);
|
void py_new_float(py_Ref, double);
|
||||||
void py_new_bool(PyVar*, bool);
|
void py_new_bool(py_Ref, bool);
|
||||||
void py_new_str(PyVar*, const char*);
|
void py_new_str(py_Ref, const char*);
|
||||||
void py_new_strn(PyVar*, const char*, int);
|
void py_new_strn(py_Ref, const char*, int);
|
||||||
void py_new_fstr(PyVar*, const char*, ...);
|
void py_new_fstr(py_Ref, const char*, ...);
|
||||||
void py_new_bytes(PyVar*, const uint8_t*, int);
|
void py_new_bytes(py_Ref, const uint8_t*, int);
|
||||||
void py_new_none(PyVar*);
|
void py_new_none(py_Ref);
|
||||||
void py_new_null(PyVar*);
|
void py_new_null(py_Ref);
|
||||||
|
|
||||||
|
void py_new_tuple(PyVar, int);
|
||||||
|
|
||||||
// new style decl-based function
|
// new style decl-based function
|
||||||
void py_new_function(PyVar*, py_CFunction, const char* sig, BindType bt);
|
void py_new_function(py_Ref, py_CFunction, const char* sig, BindType bt);
|
||||||
void py_new_function2(PyVar*, py_CFunction, const char* sig, BindType bt, const char* docstring, const PyVar* userdata);
|
void py_new_function2(py_Ref, py_CFunction, const char* sig, BindType bt, const char* docstring, const py_Ref userdata);
|
||||||
// old style argc-based function
|
// old style argc-based function
|
||||||
void py_new_nativefunc(PyVar*, py_CFunction, int argc, BindType bt);
|
void py_new_nativefunc(py_Ref, py_CFunction, int argc, BindType bt);
|
||||||
void py_new_nativefunc2(PyVar*, py_CFunction, int argc, BindType bt, const char* docstring, const PyVar* userdata);
|
void py_new_nativefunc2(py_Ref, py_CFunction, int argc, BindType bt, const char* docstring, const py_Ref userdata);
|
||||||
|
|
||||||
int py_setattr(PyVar* self, StrName name, const PyVar* val);
|
|
||||||
|
|
||||||
PyVar py_new_module(const char* name);
|
py_Ref py_new_module(const char* name);
|
||||||
PyVar pk_new_module(const char* name, const char* package);
|
const py_Ref py_import(const char* name);
|
||||||
|
|
||||||
// Type pk_new_type
|
|
||||||
|
|
||||||
#define py_isnull(self) ((self)->type == 0)
|
#define py_isnull(self) ((self)->type == 0)
|
||||||
|
|
||||||
|
|
||||||
|
/// Sets the name of the object to the given value.
|
||||||
|
void py_setdict(py_Ref self, py_Name name, const py_Ref val);
|
||||||
|
/// Returns a reference to the name of the object.
|
||||||
|
py_Ref py_getdict(const py_Ref self, py_Name name);
|
||||||
|
|
||||||
|
/// Sets the i-th slot of the object to the given value.
|
||||||
|
void py_setslot(py_Ref self, int i, const py_Ref val);
|
||||||
|
/// Returns a reference to the i-th slot of the object.
|
||||||
|
py_Ref py_getslot(const py_Ref self, int i);
|
||||||
|
|
||||||
|
/// Sets the attribute of the object to the given value.
|
||||||
|
/// This is equivalent to `self.name = val`.
|
||||||
|
/// Returns 0 | err
|
||||||
|
int py_setattr(py_Ref self, py_Name name, const py_Ref val);
|
||||||
|
|
||||||
|
/// Gets the attribute of the object.
|
||||||
|
/// This is equivalent to `self.name`.
|
||||||
|
/// Returns 0 | err
|
||||||
|
int py_getattr(const py_Ref self, py_Name name, py_Ref out);
|
||||||
|
|
||||||
|
void py_pushref(const py_Ref src);
|
||||||
|
void py_copyref(const py_Ref src, py_Ref dst);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -10,7 +10,7 @@ void ValueStack__clear(ValueStack* self) {
|
|||||||
self->sp = self->begin;
|
self->sp = self->begin;
|
||||||
}
|
}
|
||||||
|
|
||||||
PyVar* FastLocals__try_get_by_name(PyVar* locals, const CodeObject* co, StrName name){
|
PyVar* FastLocals__try_get_by_name(PyVar* locals, const CodeObject* co, py_Name name){
|
||||||
int index = c11_smallmap_n2i__get(&co->varnames_inv, name, -1);
|
int index = c11_smallmap_n2i__get(&co->varnames_inv, name, -1);
|
||||||
if(index == -1) return NULL;
|
if(index == -1) return NULL;
|
||||||
return &locals[index];
|
return &locals[index];
|
||||||
@ -39,14 +39,14 @@ void UnwindTarget__delete(UnwindTarget* self){
|
|||||||
free(self);
|
free(self);
|
||||||
}
|
}
|
||||||
|
|
||||||
Frame* Frame__new(const CodeObject* co, PyObject* module, PyObject* function, PyVar* p0, PyVar* locals, const CodeObject* locals_co){
|
Frame* Frame__new(const CodeObject* co, const PyVar* module, const PyVar* function, PyVar* p0, PyVar* locals, const CodeObject* locals_co){
|
||||||
static_assert(sizeof(Frame) <= kPoolFrameBlockSize, "!(sizeof(Frame) <= kPoolFrameBlockSize)");
|
static_assert(sizeof(Frame) <= kPoolFrameBlockSize, "!(sizeof(Frame) <= kPoolFrameBlockSize)");
|
||||||
Frame* self = PoolFrame_alloc();
|
Frame* self = PoolFrame_alloc();
|
||||||
self->f_back = NULL;
|
self->f_back = NULL;
|
||||||
self->ip = (Bytecode*)co->codes.data - 1;
|
self->ip = (Bytecode*)co->codes.data - 1;
|
||||||
self->co = co;
|
self->co = co;
|
||||||
self->module = module;
|
self->module = module->_obj;
|
||||||
self->function = function;
|
self->function = function->_obj;
|
||||||
self->p0 = p0;
|
self->p0 = p0;
|
||||||
self->locals = locals;
|
self->locals = locals;
|
||||||
self->locals_co = locals_co;
|
self->locals_co = locals_co;
|
||||||
|
@ -88,21 +88,23 @@ int pk_ManagedHeap__sweep(pk_ManagedHeap *self){
|
|||||||
return freed;
|
return freed;
|
||||||
}
|
}
|
||||||
|
|
||||||
PyObject* pk_ManagedHeap__new(pk_ManagedHeap *self, Type type, int size){
|
PyObject* pk_ManagedHeap__new(pk_ManagedHeap *self, Type type, int slots, int size){
|
||||||
PyObject* obj = PyObject__new(type, size);
|
PyObject* obj = PyObject__new(type, slots, size);
|
||||||
c11_vector__push(PyObject*, &self->no_gc, obj);
|
c11_vector__push(PyObject*, &self->no_gc, obj);
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
PyObject* pk_ManagedHeap__gcnew(pk_ManagedHeap *self, Type type, int size){
|
PyObject* pk_ManagedHeap__gcnew(pk_ManagedHeap *self, Type type, int slots, int size){
|
||||||
PyObject* obj = PyObject__new(type, size);
|
PyObject* obj = PyObject__new(type, slots, size);
|
||||||
c11_vector__push(PyObject*, &self->gen, obj);
|
c11_vector__push(PyObject*, &self->gen, obj);
|
||||||
self->gc_counter++;
|
self->gc_counter++;
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
PyObject* PyObject__new(Type type, int size){
|
PyObject* PyObject__new(Type type, int slots, int size){
|
||||||
|
assert(slots >= 0 || slots == -1);
|
||||||
PyObject* self;
|
PyObject* self;
|
||||||
|
size += PK_OBJ_HEADER_SIZE(slots);
|
||||||
if(size <= kPoolObjectBlockSize){
|
if(size <= kPoolObjectBlockSize){
|
||||||
self = PoolObject_alloc();
|
self = PoolObject_alloc();
|
||||||
self->gc_is_large = false;
|
self->gc_is_large = false;
|
||||||
@ -112,6 +114,6 @@ PyObject* PyObject__new(Type type, int size){
|
|||||||
}
|
}
|
||||||
self->type = type;
|
self->type = type;
|
||||||
self->gc_marked = false;
|
self->gc_marked = false;
|
||||||
self->dict = NULL;
|
self->slots = slots;
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,7 @@ static void pk_default_stderr(const char* s){
|
|||||||
fflush(stderr);
|
fflush(stderr);
|
||||||
}
|
}
|
||||||
|
|
||||||
void pk_TypeInfo__ctor(pk_TypeInfo *self, StrName name, Type base, PyObject* obj, const PyVar* module, bool subclass_enabled){
|
void pk_TypeInfo__ctor(pk_TypeInfo *self, py_Name name, Type base, PyObject* obj, const PyVar* module, bool subclass_enabled){
|
||||||
memset(self, 0, sizeof(pk_TypeInfo));
|
memset(self, 0, sizeof(pk_TypeInfo));
|
||||||
|
|
||||||
self->name = name;
|
self->name = name;
|
||||||
@ -25,14 +25,14 @@ void pk_TypeInfo__ctor(pk_TypeInfo *self, StrName name, Type base, PyObject* obj
|
|||||||
self->module = module ? *module : PY_NULL;
|
self->module = module ? *module : PY_NULL;
|
||||||
self->subclass_enabled = subclass_enabled;
|
self->subclass_enabled = subclass_enabled;
|
||||||
|
|
||||||
c11_vector__ctor(&self->annotated_fields, sizeof(StrName));
|
c11_vector__ctor(&self->annotated_fields, sizeof(py_Name));
|
||||||
}
|
}
|
||||||
|
|
||||||
void pk_TypeInfo__dtor(pk_TypeInfo *self){
|
void pk_TypeInfo__dtor(pk_TypeInfo *self){
|
||||||
c11_vector__dtor(&self->annotated_fields);
|
c11_vector__dtor(&self->annotated_fields);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int _hello(const PyVar* args, int argc){
|
static int _hello(const py_Ref args, int argc){
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -40,7 +40,7 @@ static void do_builtin_bindings(){
|
|||||||
pk_VM* vm = pk_current_vm;
|
pk_VM* vm = pk_current_vm;
|
||||||
|
|
||||||
py_new_nativefunc(&vm->reg[0], _hello, 2, BindType_FUNCTION);
|
py_new_nativefunc(&vm->reg[0], _hello, 2, BindType_FUNCTION);
|
||||||
py_setattr(&vm->builtins, pk_StrName__map("hello"), &vm->reg[0]);
|
py_setdict(&vm->builtins, py_name("hello"), &vm->reg[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
void pk_VM__ctor(pk_VM* self){
|
void pk_VM__ctor(pk_VM* self){
|
||||||
@ -69,19 +69,19 @@ void pk_VM__ctor(pk_VM* self){
|
|||||||
ValueStack__ctor(&self->stack);
|
ValueStack__ctor(&self->stack);
|
||||||
|
|
||||||
self->True = (PyVar){.type=tp_bool, .is_ptr=true, .extra=1,
|
self->True = (PyVar){.type=tp_bool, .is_ptr=true, .extra=1,
|
||||||
._obj=pk_ManagedHeap__gcnew(&self->heap, tp_bool, 1),
|
._obj=pk_ManagedHeap__gcnew(&self->heap, tp_bool, 0, 1),
|
||||||
};
|
};
|
||||||
self->False = (PyVar){.type=tp_bool, .is_ptr=true, .extra=0,
|
self->False = (PyVar){.type=tp_bool, .is_ptr=true, .extra=0,
|
||||||
._obj=pk_ManagedHeap__gcnew(&self->heap, tp_bool, 1),
|
._obj=pk_ManagedHeap__gcnew(&self->heap, tp_bool, 0, 1),
|
||||||
};
|
};
|
||||||
self->None = (PyVar){.type=tp_none_type, .is_ptr=true,
|
self->None = (PyVar){.type=tp_none_type, .is_ptr=true,
|
||||||
._obj=pk_ManagedHeap__gcnew(&self->heap, tp_none_type, 1),
|
._obj=pk_ManagedHeap__gcnew(&self->heap, tp_none_type, 0, 1),
|
||||||
};
|
};
|
||||||
self->NotImplemented = (PyVar){.type=tp_not_implemented_type, .is_ptr=true,
|
self->NotImplemented = (PyVar){.type=tp_not_implemented_type, .is_ptr=true,
|
||||||
._obj=pk_ManagedHeap__gcnew(&self->heap, tp_not_implemented_type, 1),
|
._obj=pk_ManagedHeap__gcnew(&self->heap, tp_not_implemented_type, 0, 1),
|
||||||
};
|
};
|
||||||
self->Ellipsis = (PyVar){.type=tp_ellipsis, .is_ptr=true,
|
self->Ellipsis = (PyVar){.type=tp_ellipsis, .is_ptr=true,
|
||||||
._obj=pk_ManagedHeap__gcnew(&self->heap, tp_ellipsis, 1),
|
._obj=pk_ManagedHeap__gcnew(&self->heap, tp_ellipsis, 0, 1),
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Init Builtin Types */
|
/* Init Builtin Types */
|
||||||
@ -132,7 +132,7 @@ void pk_VM__ctor(pk_VM* self){
|
|||||||
#undef validate
|
#undef validate
|
||||||
|
|
||||||
self->StopIteration = c11__at(pk_TypeInfo, &self->types, tp_stop_iteration)->self;
|
self->StopIteration = c11__at(pk_TypeInfo, &self->types, tp_stop_iteration)->self;
|
||||||
self->builtins = py_new_module("builtins");
|
self->builtins = *py_new_module("builtins");
|
||||||
|
|
||||||
/* Setup Public Builtin Types */
|
/* Setup Public Builtin Types */
|
||||||
Type public_types[] = {
|
Type public_types[] = {
|
||||||
@ -147,13 +147,13 @@ void pk_VM__ctor(pk_VM* self){
|
|||||||
for(int i=0; i<PK_ARRAY_COUNT(public_types); i++){
|
for(int i=0; i<PK_ARRAY_COUNT(public_types); i++){
|
||||||
Type t = public_types[i];
|
Type t = public_types[i];
|
||||||
pk_TypeInfo* ti = c11__at(pk_TypeInfo, &self->types, t);
|
pk_TypeInfo* ti = c11__at(pk_TypeInfo, &self->types, t);
|
||||||
pk_NameDict__set(self->builtins._obj->dict, ti->name, ti->self);
|
py_setdict(&self->builtins, ti->name, &ti->self);
|
||||||
}
|
}
|
||||||
pk_NameDict__set(self->builtins._obj->dict, pk_StrName__map("NotImplemented"), self->NotImplemented);
|
py_setdict(&self->builtins, py_name("NotImplemented"), &self->NotImplemented);
|
||||||
|
|
||||||
/* Do Buildin Bindings*/
|
/* Do Buildin Bindings*/
|
||||||
do_builtin_bindings();
|
do_builtin_bindings();
|
||||||
self->main = py_new_module("__main__");
|
self->main = *py_new_module("__main__");
|
||||||
}
|
}
|
||||||
|
|
||||||
void pk_VM__dtor(pk_VM* self){
|
void pk_VM__dtor(pk_VM* self){
|
||||||
@ -186,17 +186,18 @@ pk_FrameResult pk_VM__run_top_frame(pk_VM* self){
|
|||||||
Type pk_VM__new_type(pk_VM* self, const char* name, Type base, const PyVar* module, bool subclass_enabled){
|
Type pk_VM__new_type(pk_VM* self, const char* name, Type base, const PyVar* module, bool subclass_enabled){
|
||||||
Type type = self->types.count;
|
Type type = self->types.count;
|
||||||
pk_TypeInfo* ti = c11_vector__emplace(&self->types);
|
pk_TypeInfo* ti = c11_vector__emplace(&self->types);
|
||||||
PyObject* typeobj = pk_ManagedHeap__gcnew(&self->heap, tp_type, PK_OBJ_SIZEOF(Type));
|
PyObject* typeobj = pk_ManagedHeap__gcnew(&self->heap, tp_type, 0, sizeof(Type));
|
||||||
*PyObject__as(Type, typeobj) = type;
|
Type* value = PyObject__value(typeobj);
|
||||||
pk_TypeInfo__ctor(ti, pk_StrName__map(name), base, typeobj, module, subclass_enabled);
|
*value = type;
|
||||||
|
pk_TypeInfo__ctor(ti, py_name(name), base, typeobj, module, subclass_enabled);
|
||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
|
|
||||||
/****************************************/
|
/****************************************/
|
||||||
void PyObject__delete(PyObject *self){
|
void PyObject__delete(PyObject *self){
|
||||||
pk_TypeInfo* ti = c11__getitem(pk_TypeInfo*, &pk_current_vm->types, self->type);
|
pk_TypeInfo* ti = c11__getitem(pk_TypeInfo*, &pk_current_vm->types, self->type);
|
||||||
if(ti->dtor) ti->dtor(PyObject__value_ptr(self));
|
if(ti->dtor) ti->dtor(PyObject__value(self));
|
||||||
if(self->dict) pk_NameDict__delete(self->dict);
|
if(self->slots == -1) pk_NameDict__dtor(PyObject__dict(self));
|
||||||
if(self->gc_is_large){
|
if(self->gc_is_large){
|
||||||
free(self);
|
free(self);
|
||||||
}else{
|
}else{
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
#include "pocketpy/objects/dict.h"
|
#include "pocketpy/objects/dict.h"
|
||||||
#include "pocketpy/common/utils.h"
|
#include "pocketpy/common/utils.h"
|
||||||
|
#include <stdint.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@ -113,14 +114,18 @@ static void pkpy_Dict__extendht(pkpy_Dict* self) {
|
|||||||
struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, i);
|
struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, i);
|
||||||
if(pkpy_Var__is_null(&entry->key)) continue;
|
if(pkpy_Var__is_null(&entry->key)) continue;
|
||||||
|
|
||||||
int rhash = DICT_HASH_TRANS(py_hash(&entry->key));
|
int64_t out;
|
||||||
|
int err = py_hash(&entry->key, &out);
|
||||||
|
int rhash = DICT_HASH_TRANS(out);
|
||||||
int h = pkpy_Dict__probe0(self, entry->key, rhash);
|
int h = pkpy_Dict__probe0(self, entry->key, rhash);
|
||||||
pkpy_Dict__htset(self, h, i);
|
pkpy_Dict__htset(self, h, i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool pkpy_Dict__set(pkpy_Dict* self, PyVar key, PyVar val) {
|
bool pkpy_Dict__set(pkpy_Dict* self, PyVar key, PyVar val) {
|
||||||
int hash = DICT_HASH_TRANS(py_hash(&key));
|
int64_t out;
|
||||||
|
int err = py_hash(&key, &out);
|
||||||
|
int hash = DICT_HASH_TRANS(out);
|
||||||
int h = pkpy_Dict__probe1(self, key, hash);
|
int h = pkpy_Dict__probe1(self, key, hash);
|
||||||
|
|
||||||
int idx = pkpy_Dict__htget(self, h);
|
int idx = pkpy_Dict__htget(self, h);
|
||||||
@ -155,7 +160,9 @@ bool pkpy_Dict__set(pkpy_Dict* self, PyVar key, PyVar val) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool pkpy_Dict__contains(const pkpy_Dict* self, PyVar key) {
|
bool pkpy_Dict__contains(const pkpy_Dict* self, PyVar key) {
|
||||||
int hash = DICT_HASH_TRANS(py_hash(&key));
|
int64_t out;
|
||||||
|
int err = py_hash(&key, &out);
|
||||||
|
int hash = DICT_HASH_TRANS(out);
|
||||||
int h = pkpy_Dict__probe1(self, key, hash);
|
int h = pkpy_Dict__probe1(self, key, hash);
|
||||||
|
|
||||||
int idx = pkpy_Dict__htget(self, h);
|
int idx = pkpy_Dict__htget(self, h);
|
||||||
@ -187,7 +194,9 @@ static bool pkpy_Dict__refactor(pkpy_Dict* self) {
|
|||||||
struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, i);
|
struct pkpy_DictEntry* entry = &c11__getitem(struct pkpy_DictEntry, &self->_entries, i);
|
||||||
if(pkpy_Var__is_null(&entry->key)) continue;
|
if(pkpy_Var__is_null(&entry->key)) continue;
|
||||||
|
|
||||||
int rhash = DICT_HASH_TRANS(py_hash(&entry->key));
|
int64_t out;
|
||||||
|
py_hash(&entry->key, &out);
|
||||||
|
int rhash = DICT_HASH_TRANS(out);
|
||||||
int h = pkpy_Dict__probe0(self, entry->key, rhash);
|
int h = pkpy_Dict__probe0(self, entry->key, rhash);
|
||||||
pkpy_Dict__htset(self, h, i);
|
pkpy_Dict__htset(self, h, i);
|
||||||
}
|
}
|
||||||
@ -195,7 +204,9 @@ static bool pkpy_Dict__refactor(pkpy_Dict* self) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool pkpy_Dict__del(pkpy_Dict* self, PyVar key) {
|
bool pkpy_Dict__del(pkpy_Dict* self, PyVar key) {
|
||||||
int hash = DICT_HASH_TRANS(py_hash(&key));
|
int64_t out;
|
||||||
|
int err = py_hash(&key, &out);
|
||||||
|
int hash = DICT_HASH_TRANS(out);
|
||||||
int h = pkpy_Dict__probe1(self, key, hash);
|
int h = pkpy_Dict__probe1(self, key, hash);
|
||||||
int idx = pkpy_Dict__htget(self, h), null = pkpy_Dict__idx_null(self);
|
int idx = pkpy_Dict__htget(self, h), null = pkpy_Dict__idx_null(self);
|
||||||
if(idx == null) return false;
|
if(idx == null) return false;
|
||||||
@ -208,7 +219,9 @@ bool pkpy_Dict__del(pkpy_Dict* self, PyVar key) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const PyVar *pkpy_Dict__try_get(const pkpy_Dict* self, PyVar key) {
|
const PyVar *pkpy_Dict__try_get(const pkpy_Dict* self, PyVar key) {
|
||||||
int hash = DICT_HASH_TRANS(py_hash(&key));
|
int64_t out;
|
||||||
|
int err = py_hash(&key, &out);
|
||||||
|
int hash = DICT_HASH_TRANS(out);
|
||||||
int h = pkpy_Dict__probe1(self, key, hash);
|
int h = pkpy_Dict__probe1(self, key, hash);
|
||||||
|
|
||||||
int idx = pkpy_Dict__htget(self, h);
|
int idx = pkpy_Dict__htget(self, h);
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
#include "pocketpy/objects/object.h"
|
#include "pocketpy/objects/object.h"
|
||||||
|
#include "pocketpy/pocketpy.h"
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
void PyVar__ctor3(PyVar* self, PyObject* existing){
|
void PyVar__ctor3(PyVar* self, PyObject* existing){
|
||||||
assert(existing);
|
assert(existing);
|
||||||
@ -6,3 +8,17 @@ void PyVar__ctor3(PyVar* self, PyObject* existing){
|
|||||||
self->is_ptr = true;
|
self->is_ptr = true;
|
||||||
self->_obj = existing;
|
self->_obj = existing;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void* PyObject__value(PyObject* self){
|
||||||
|
return (char*)self + PK_OBJ_HEADER_SIZE(self->slots);
|
||||||
|
}
|
||||||
|
|
||||||
|
pk_NameDict* PyObject__dict(PyObject* self){
|
||||||
|
assert(self->slots == -1);
|
||||||
|
return (pk_NameDict*)((char*)self + 8);
|
||||||
|
}
|
||||||
|
|
||||||
|
PyVar* PyObject__slots(PyObject* self){
|
||||||
|
assert(self->slots >= 0);
|
||||||
|
return (PyVar*)((char*)self + 8);
|
||||||
|
}
|
@ -14,12 +14,12 @@ void py_initialize(){
|
|||||||
pk_VM__ctor(&pk_default_vm);
|
pk_VM__ctor(&pk_default_vm);
|
||||||
}
|
}
|
||||||
|
|
||||||
py_Error* py_exec_simple(const char* source){
|
int py_exec_simple(const char* source){
|
||||||
CodeObject* co = NULL;
|
CodeObject* co = NULL;
|
||||||
pk_VM* vm = pk_current_vm;
|
pk_VM* vm = pk_current_vm;
|
||||||
Frame* frame = Frame__new(
|
Frame* frame = Frame__new(
|
||||||
co,
|
co,
|
||||||
vm->main,
|
&vm->main,
|
||||||
NULL,
|
NULL,
|
||||||
vm->stack.sp,
|
vm->stack.sp,
|
||||||
vm->stack.sp,
|
vm->stack.sp,
|
||||||
@ -27,11 +27,19 @@ py_Error* py_exec_simple(const char* source){
|
|||||||
);
|
);
|
||||||
pk_VM__push_frame(vm, frame);
|
pk_VM__push_frame(vm, frame);
|
||||||
pk_FrameResult res = pk_VM__run_top_frame(vm);
|
pk_FrameResult res = pk_VM__run_top_frame(vm);
|
||||||
if(res == RES_ERROR) return vm->last_error;
|
if(res == RES_ERROR) return vm->last_error->type;
|
||||||
if(res == RES_RETURN) return NULL; // vm->last_retval;
|
if(res == RES_RETURN) return 0; // vm->last_retval;
|
||||||
assert(0); // unreachable
|
assert(0); // unreachable
|
||||||
}
|
}
|
||||||
|
|
||||||
|
py_Error* py_getlasterror(){
|
||||||
|
return pk_current_vm->last_error;
|
||||||
|
}
|
||||||
|
|
||||||
|
void py_Error__print(py_Error* self){
|
||||||
|
abort();
|
||||||
|
}
|
||||||
|
|
||||||
void py_finalize(){
|
void py_finalize(){
|
||||||
pk_VM__dtor(&pk_default_vm);
|
pk_VM__dtor(&pk_default_vm);
|
||||||
pk_current_vm = NULL;
|
pk_current_vm = NULL;
|
||||||
@ -39,31 +47,17 @@ void py_finalize(){
|
|||||||
Pools_finalize();
|
Pools_finalize();
|
||||||
}
|
}
|
||||||
|
|
||||||
void py_newint(PyVar* self, int64_t val){
|
void py_setdict(py_Ref self, py_Name name, const py_Ref val){
|
||||||
|
pk_NameDict__set(
|
||||||
|
PyObject__dict(self->_obj),
|
||||||
|
name,
|
||||||
|
*val
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
void py_newint(py_Ref self, int64_t val){
|
||||||
self->type = tp_int;
|
self->type = tp_int;
|
||||||
self->is_ptr = false;
|
self->is_ptr = false;
|
||||||
self->_i64 = val;
|
self->_i64 = val;
|
||||||
}
|
}
|
||||||
|
|
||||||
void py_newfloat(PyVar* self, double val){
|
|
||||||
self->type = tp_float;
|
|
||||||
self->is_ptr = false;
|
|
||||||
self->_f64 = val;
|
|
||||||
}
|
|
||||||
|
|
||||||
void py_newbool(PyVar* self, bool val){
|
|
||||||
// return a global singleton
|
|
||||||
}
|
|
||||||
|
|
||||||
void py_newnone(PyVar* self){
|
|
||||||
// return a heap object
|
|
||||||
}
|
|
||||||
|
|
||||||
void py_newstr(PyVar* self, const char* val){
|
|
||||||
// return a heap object
|
|
||||||
}
|
|
||||||
|
|
||||||
void py_newstr2(PyVar*, const char*, int);
|
|
||||||
|
|
||||||
void py_newbytes(PyVar*, const uint8_t*, int);
|
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "pocketpy.h"
|
#include "pocketpy.h"
|
||||||
|
#include "pocketpy/pocketpy.h"
|
||||||
|
|
||||||
char* read_file(const char* path) {
|
char* read_file(const char* path) {
|
||||||
FILE* file = fopen(path, "r");
|
FILE* file = fopen(path, "r");
|
||||||
@ -27,10 +28,9 @@ int main(int argc, char** argv) {
|
|||||||
char* source = read_file(argv[1]);
|
char* source = read_file(argv[1]);
|
||||||
py_initialize();
|
py_initialize();
|
||||||
|
|
||||||
py_Error* err = py_exec_simple(source);
|
if(py_exec_simple(source)){
|
||||||
if(err){
|
py_Error* err = py_getlasterror();
|
||||||
py_Error__print(err);
|
py_Error__print(err);
|
||||||
py_Error__delete(err);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
py_finalize();
|
py_finalize();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user