This commit is contained in:
blueloveTH 2024-07-10 01:19:56 +08:00
parent 614fc27f03
commit b89414a5d7
12 changed files with 99 additions and 95 deletions

View File

@ -15,10 +15,11 @@ typedef struct pk_TypeInfo {
py_TValue self; py_TValue self;
py_TValue module; // the module where the type is defined py_TValue module; // the module where the type is defined
bool subclass_enabled;
bool is_python; // is it a python class? (not derived from c object)
bool is_sealed; // can it be subclassed?
void (*dtor)(void*); void (*dtor)(void*);
void (*gc_mark)(void*);
c11_vector /*T=py_Name*/ annotated_fields; c11_vector /*T=py_Name*/ annotated_fields;
@ -28,14 +29,6 @@ typedef struct pk_TypeInfo {
py_TValue magic[64]; py_TValue magic[64];
} pk_TypeInfo; } pk_TypeInfo;
void pk_TypeInfo__ctor(pk_TypeInfo* self,
py_Name name,
py_Type index,
py_Type base,
const py_TValue* module,
bool subclass_enabled);
void pk_TypeInfo__dtor(pk_TypeInfo* self);
typedef struct pk_VM { typedef struct pk_VM {
Frame* top_frame; Frame* top_frame;
@ -82,11 +75,12 @@ typedef enum pk_FrameResult {
pk_FrameResult pk_VM__run_top_frame(pk_VM* self); pk_FrameResult pk_VM__run_top_frame(pk_VM* self);
py_Type pk_VM__new_type(pk_VM* self, py_Type pk_newtype(const char* name,
const char* name, py_Type base,
py_Type base, const py_GlobalRef module,
const py_TValue* module, void (*dtor)(void*),
bool subclass_enabled); bool is_python,
bool is_sealed);
pk_FrameResult pk_VM__vectorcall(pk_VM* self, uint16_t argc, uint16_t kwargc, bool opcall); pk_FrameResult pk_VM__vectorcall(pk_VM* self, uint16_t argc, uint16_t kwargc, bool opcall);

View File

@ -92,6 +92,13 @@ void py_newslice(py_Ref);
// old style argc-based function // old style argc-based function
void py_newnativefunc(py_Ref out, py_CFunction); void py_newnativefunc(py_Ref out, py_CFunction);
/// Create a new type.
/// @param name name of the type.
/// @param base base type.
/// @param module module where the type is defined. Use NULL for built-in types.
/// @param dtor destructor function. Use NULL if not needed.
py_Type py_newtype(const char* name, py_Type base, const py_GlobalRef module, void (*dtor)(void*));
/// Create a new object. /// Create a new object.
/// @param out output reference. /// @param out output reference.
/// @param type type of the object. /// @param type type of the object.

View File

@ -27,12 +27,11 @@ static void pk_default_stderr(const char* fmt, ...) {
fflush(stderr); fflush(stderr);
} }
void pk_TypeInfo__ctor(pk_TypeInfo* self, static void pk_TypeInfo__ctor(pk_TypeInfo* self,
py_Name name, py_Name name,
py_Type index, py_Type index,
py_Type base, py_Type base,
const py_TValue* module, const py_TValue* module) {
bool subclass_enabled) {
memset(self, 0, sizeof(pk_TypeInfo)); memset(self, 0, sizeof(pk_TypeInfo));
self->name = name; self->name = name;
@ -49,12 +48,10 @@ void pk_TypeInfo__ctor(pk_TypeInfo* self,
}; };
self->module = module ? *module : PY_NIL; self->module = module ? *module : PY_NIL;
self->subclass_enabled = subclass_enabled;
c11_vector__ctor(&self->annotated_fields, sizeof(py_Name)); c11_vector__ctor(&self->annotated_fields, sizeof(py_Name));
} }
void pk_TypeInfo__dtor(pk_TypeInfo* self) { c11_vector__dtor(&self->annotated_fields); } static void pk_TypeInfo__dtor(pk_TypeInfo* self) { c11_vector__dtor(&self->annotated_fields); }
void pk_VM__ctor(pk_VM* self) { void pk_VM__ctor(pk_VM* self) {
self->top_frame = NULL; self->top_frame = NULL;
@ -82,17 +79,19 @@ void pk_VM__ctor(pk_VM* self) {
/* Init Builtin Types */ /* Init Builtin Types */
// 0: unused // 0: unused
pk_TypeInfo__ctor(c11_vector__emplace(&self->types), 0, 0, 0, NULL, false); void* placeholder = c11_vector__emplace(&self->types);
memset(placeholder, 0, sizeof(pk_TypeInfo));
#define validate(t, expr) \ #define validate(t, expr) \
if(t != (expr)) abort() if(t != (expr)) abort()
validate(tp_object, pk_VM__new_type(self, "object", 0, NULL, true)); validate(tp_object, pk_newtype("object", 0, NULL, NULL, true, false));
validate(tp_type, pk_VM__new_type(self, "type", 1, NULL, false)); validate(tp_type, pk_newtype("type", 1, NULL, NULL, false, true));
pk_object__register(); pk_object__register();
validate(tp_int, pk_VM__new_type(self, "int", tp_object, NULL, false)); validate(tp_int, pk_newtype("int", tp_object, NULL, NULL, false, true));
validate(tp_float, pk_VM__new_type(self, "float", tp_object, NULL, false)); validate(tp_float, pk_newtype("float", tp_object, NULL, NULL, false, true));
validate(tp_bool, pk_VM__new_type(self, "bool", tp_object, NULL, false)); validate(tp_bool, pk_newtype("bool", tp_object, NULL, NULL, false, true));
pk_number__register(); pk_number__register();
validate(tp_str, pk_str__register()); validate(tp_str, pk_str__register());
@ -105,32 +104,32 @@ void pk_VM__ctor(pk_VM* self) {
validate(tp_slice, pk_slice__register()); validate(tp_slice, pk_slice__register());
validate(tp_range, pk_range__register()); validate(tp_range, pk_range__register());
validate(tp_range_iterator, pk_range_iterator__register()); validate(tp_range_iterator, pk_range_iterator__register());
validate(tp_module, pk_VM__new_type(self, "module", tp_object, NULL, false)); validate(tp_module, pk_newtype("module", tp_object, NULL, NULL, false, true));
validate(tp_function, pk_function__register()); validate(tp_function, pk_function__register());
validate(tp_nativefunc, pk_nativefunc__register()); validate(tp_nativefunc, pk_nativefunc__register());
validate(tp_boundmethod, pk_VM__new_type(self, "boundmethod", tp_object, NULL, false)); validate(tp_boundmethod, pk_newtype("boundmethod", tp_object, NULL, NULL, false, true));
validate(tp_super, pk_VM__new_type(self, "super", tp_object, NULL, false)); validate(tp_super, pk_newtype("super", tp_object, NULL, NULL, false, true));
validate(tp_BaseException, pk_BaseException__register()); validate(tp_BaseException, pk_BaseException__register());
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_mappingproxy, pk_VM__new_type(self, "mappingproxy", tp_object, NULL, false)); validate(tp_mappingproxy, pk_newtype("mappingproxy", tp_object, NULL, NULL, false, true));
validate(tp_dict, pk_VM__new_type(self, "dict", tp_object, NULL, true)); validate(tp_dict, pk_newtype("dict", tp_object, NULL, NULL, false, false));
validate(tp_property, pk_VM__new_type(self, "property", tp_object, NULL, false)); validate(tp_property, pk_newtype("property", tp_object, NULL, NULL, false, true));
validate(tp_star_wrapper, pk_VM__new_type(self, "star_wrapper", tp_object, NULL, false)); validate(tp_star_wrapper, pk_newtype("star_wrapper", tp_object, NULL, NULL, false, true));
validate(tp_staticmethod, pk_VM__new_type(self, "staticmethod", tp_object, NULL, false)); validate(tp_staticmethod, pk_newtype("staticmethod", tp_object, NULL, NULL, false, true));
validate(tp_classmethod, pk_VM__new_type(self, "classmethod", tp_object, NULL, false)); validate(tp_classmethod, pk_newtype("classmethod", tp_object, NULL, NULL, false, true));
validate(tp_NoneType, pk_VM__new_type(self, "NoneType", tp_object, NULL, false)); validate(tp_NoneType, pk_newtype("NoneType", tp_object, NULL, NULL, false, true));
validate(tp_NotImplementedType, validate(tp_NotImplementedType,
pk_VM__new_type(self, "NotImplementedType", tp_object, NULL, false)); pk_newtype("NotImplementedType", tp_object, NULL, NULL, false, true));
validate(tp_ellipsis, pk_VM__new_type(self, "ellipsis", tp_object, NULL, false)); validate(tp_ellipsis, pk_newtype("ellipsis", tp_object, NULL, NULL, false, true));
validate(tp_SyntaxError, pk_VM__new_type(self, "SyntaxError", tp_Exception, NULL, false)); validate(tp_SyntaxError, pk_newtype("SyntaxError", tp_Exception, NULL, NULL, false, true));
validate(tp_StopIteration, pk_VM__new_type(self, "StopIteration", tp_Exception, NULL, false)); validate(tp_StopIteration, pk_newtype("StopIteration", tp_Exception, NULL, NULL, false, true));
#undef validate #undef validate
self->builtins = pk_builtins__register(); self->builtins = pk_builtins__register();
@ -174,6 +173,7 @@ void pk_VM__dtor(pk_VM* self) {
// clear frames // clear frames
// ... // ...
pk_NameDict__dtor(&self->modules); pk_NameDict__dtor(&self->modules);
c11__foreach(pk_TypeInfo, &self->types, ti) pk_TypeInfo__dtor(ti);
c11_vector__dtor(&self->types); c11_vector__dtor(&self->types);
ValueStack__clear(&self->stack); ValueStack__clear(&self->stack);
} }
@ -255,22 +255,28 @@ bool pk__normalize_index(int* index, int length) {
return true; return true;
} }
py_Type pk_VM__new_type(pk_VM* self, py_Type pk_newtype(const char* name,
const char* name, py_Type base,
py_Type base, const py_GlobalRef module,
const py_TValue* module, void (*dtor)(void*),
bool subclass_enabled) { bool is_python,
py_Type index = self->types.count; bool is_sealed) {
pk_TypeInfo* ti = c11_vector__emplace(&self->types); c11_vector* types = &pk_current_vm->types;
pk_TypeInfo__ctor(ti, py_name(name), index, base, module, subclass_enabled); py_Type index = types->count;
pk_TypeInfo* ti = c11_vector__emplace(types);
pk_TypeInfo__ctor(ti, py_name(name), index, base, module);
ti->dtor = dtor;
ti->is_python = is_python;
ti->is_sealed = is_sealed;
return index; return index;
} }
bool __prepare_py_call(py_TValue* buffer, py_Type py_newtype(const char* name, py_Type base, const py_GlobalRef module, void (*dtor)(void*)) {
py_Ref argv, return pk_newtype(name, base, module, dtor, false, false);
py_Ref p1, }
int kwargc,
const FuncDecl* decl) { static bool
__prepare_py_call(py_TValue* buffer, py_Ref argv, py_Ref p1, int kwargc, const FuncDecl* decl) {
const CodeObject* co = &decl->code; const CodeObject* co = &decl->code;
int decl_argc = decl->args.count; int decl_argc = decl->args.count;

View File

@ -163,10 +163,8 @@ py_TValue pk_builtins__register() {
} }
py_Type pk_function__register() { py_Type pk_function__register() {
pk_VM* vm = pk_current_vm; py_Type type =
py_Type type = pk_VM__new_type(vm, "function", tp_object, NULL, false); pk_newtype("function", tp_object, NULL, (void (*)(void*))Function__dtor, false, true);
pk_TypeInfo* ti = c11__at(pk_TypeInfo, &vm->types, type);
ti->dtor = (void (*)(void*))Function__dtor;
return type; return type;
} }
@ -177,8 +175,7 @@ static bool _py_nativefunc__repr(int argc, py_Ref argv) {
} }
py_Type pk_nativefunc__register() { py_Type pk_nativefunc__register() {
pk_VM* vm = pk_current_vm; py_Type type = pk_newtype("nativefunc", tp_object, NULL, NULL, false, true);
py_Type type = pk_VM__new_type(vm, "nativefunc", tp_object, NULL, false);
py_bindmagic(type, __repr__, _py_nativefunc__repr); py_bindmagic(type, __repr__, _py_nativefunc__repr);
return type; return type;
} }

View File

@ -64,8 +64,7 @@ static bool _py_array_iterator__next__(int argc, py_Ref argv) {
} }
py_Type pk_array_iterator__register() { py_Type pk_array_iterator__register() {
pk_VM* vm = pk_current_vm; py_Type type = pk_newtype("array_iterator", tp_object, NULL, NULL, false, true);
py_Type type = pk_VM__new_type(vm, "array_iterator", tp_object, NULL, false);
py_bindmagic(type, __new__, _py_array_iterator__new__); py_bindmagic(type, __new__, _py_array_iterator__new__);
py_bindmagic(type, __iter__, _py_array_iterator__iter__); py_bindmagic(type, __iter__, _py_array_iterator__iter__);

View File

@ -17,6 +17,11 @@ typedef struct BaseException {
c11_vector /*T=BaseExceptionFrame*/ stacktrace; c11_vector /*T=BaseExceptionFrame*/ stacktrace;
} BaseException; } BaseException;
static void BaseException__dtor(void* ud) {
BaseException* self = (BaseException*)ud;
c11_vector__dtor(&self->stacktrace);
}
static bool _py_BaseException__new__(int argc, py_Ref argv) { static bool _py_BaseException__new__(int argc, py_Ref argv) {
py_Type cls = py_totype(argv); py_Type cls = py_totype(argv);
BaseException* ud = py_newobject(py_retval(), cls, 1, sizeof(BaseException)); BaseException* ud = py_newobject(py_retval(), cls, 1, sizeof(BaseException));
@ -65,8 +70,8 @@ static bool _py_BaseException__str__(int argc, py_Ref argv) {
} }
py_Type pk_BaseException__register() { py_Type pk_BaseException__register() {
pk_VM* vm = pk_current_vm; py_Type type = pk_newtype("BaseException", tp_object, NULL, BaseException__dtor, false, false);
py_Type type = pk_VM__new_type(vm, "BaseException", tp_object, NULL, true);
py_bindmagic(type, __new__, _py_BaseException__new__); py_bindmagic(type, __new__, _py_BaseException__new__);
py_bindmagic(type, __init__, _py_BaseException__init__); py_bindmagic(type, __init__, _py_BaseException__init__);
py_bindmagic(type, __repr__, _py_BaseException__repr__); py_bindmagic(type, __repr__, _py_BaseException__repr__);
@ -75,7 +80,6 @@ py_Type pk_BaseException__register() {
} }
py_Type pk_Exception__register() { py_Type pk_Exception__register() {
pk_VM* vm = pk_current_vm; py_Type type = pk_newtype("Exception", tp_BaseException, NULL, NULL, false, true);
py_Type type = pk_VM__new_type(vm, "Exception", tp_BaseException, NULL, true);
return type; return type;
} }

View File

@ -64,7 +64,7 @@ void py_list__insert(py_Ref self, int i, const py_Ref val) {
c11_vector__insert(py_TValue, userdata, i, *val); c11_vector__insert(py_TValue, userdata, i, *val);
} }
void py_list__reverse(py_Ref self){ void py_list__reverse(py_Ref self) {
List* userdata = py_touserdata(self); List* userdata = py_touserdata(self);
c11__reverse(py_TValue, userdata); c11__reverse(py_TValue, userdata);
} }
@ -405,10 +405,8 @@ static bool _py_list__iter__(int argc, py_Ref argv) {
} }
py_Type pk_list__register() { py_Type pk_list__register() {
pk_VM* vm = pk_current_vm; py_Type type =
py_Type type = pk_VM__new_type(vm, "list", tp_object, NULL, false); pk_newtype("list", tp_object, NULL, (void (*)(void*))c11_vector__dtor, false, true);
pk_TypeInfo* ti = c11__at(pk_TypeInfo, &vm->types, type);
ti->dtor = (void (*)(void*))c11_vector__dtor;
py_bindmagic(type, __len__, _py_list__len__); py_bindmagic(type, __len__, _py_list__len__);
py_bindmagic(type, __eq__, _py_list__eq__); py_bindmagic(type, __eq__, _py_list__eq__);

View File

@ -3,8 +3,12 @@
#include "pocketpy/pocketpy.h" #include "pocketpy/pocketpy.h"
static bool _py_object__new__(int argc, py_Ref argv) { static bool _py_object__new__(int argc, py_Ref argv) {
assert(argc >= 1); if(argc == 0) return TypeError("object.__new__(): not enough arguments");
py_Type cls = py_totype(py_arg(0)); py_Type cls = py_totype(py_arg(0));
pk_TypeInfo* ti = c11__at(pk_TypeInfo, &pk_current_vm->types, cls);
if(!ti->is_python) {
return TypeError("object.__new__(%t) is not safe, use %t.__new__()", cls, cls);
}
py_newobject(py_retval(), cls, 0, 0); py_newobject(py_retval(), cls, 0, 0);
return true; return true;
} }
@ -54,7 +58,9 @@ static bool _py_type__repr__(int argc, py_Ref argv) {
} }
void pk_object__register() { void pk_object__register() {
// use staticmethod
py_bindmagic(tp_object, __new__, _py_object__new__); py_bindmagic(tp_object, __new__, _py_object__new__);
py_bindmagic(tp_object, __hash__, _py_object__hash__); py_bindmagic(tp_object, __hash__, _py_object__hash__);
py_bindmagic(tp_object, __eq__, _py_object__eq__); py_bindmagic(tp_object, __eq__, _py_object__eq__);
py_bindmagic(tp_object, __ne__, _py_object__ne__); py_bindmagic(tp_object, __ne__, _py_object__ne__);

View File

@ -47,8 +47,7 @@ static bool _py_range__iter__(int argc, py_Ref argv) {
} }
py_Type pk_range__register() { py_Type pk_range__register() {
pk_VM* vm = pk_current_vm; py_Type type = pk_newtype("range", tp_object, NULL, NULL, false, true);
py_Type type = pk_VM__new_type(vm, "range", tp_object, NULL, false);
py_bindmagic(type, __new__, _py_range__new__); py_bindmagic(type, __new__, _py_range__new__);
py_bindmagic(type, __iter__, _py_range__iter__); py_bindmagic(type, __iter__, _py_range__iter__);
@ -89,8 +88,7 @@ static bool _py_range_iterator__next__(int argc, py_Ref argv) {
} }
py_Type pk_range_iterator__register() { py_Type pk_range_iterator__register() {
pk_VM* vm = pk_current_vm; py_Type type = pk_newtype("range_iterator", tp_object, NULL, NULL, false, true);
py_Type type = pk_VM__new_type(vm, "range_iterator", tp_object, NULL, false);
py_bindmagic(type, __new__, _py_range_iterator__new__); py_bindmagic(type, __new__, _py_range_iterator__new__);
py_bindmagic(type, __iter__, _py_range_iterator__iter__); py_bindmagic(type, __iter__, _py_range_iterator__iter__);

View File

@ -5,8 +5,7 @@
#include "pocketpy/objects/object.h" #include "pocketpy/objects/object.h"
#include "pocketpy/interpreter/vm.h" #include "pocketpy/interpreter/vm.h"
void py_newslice(py_Ref out) {
void py_newslice(py_Ref out){
pk_VM* vm = pk_current_vm; pk_VM* vm = pk_current_vm;
PyObject* obj = pk_ManagedHeap__gcnew(&vm->heap, tp_slice, 3, 0); PyObject* obj = pk_ManagedHeap__gcnew(&vm->heap, tp_slice, 3, 0);
out->type = tp_slice; out->type = tp_slice;
@ -14,8 +13,8 @@ void py_newslice(py_Ref out){
out->_obj = obj; out->_obj = obj;
} }
static bool _py_slice__new__(int argc, py_Ref argv){ static bool _py_slice__new__(int argc, py_Ref argv) {
PY_CHECK_ARGC(1+3); PY_CHECK_ARGC(1 + 3);
py_Ref slice = py_retval(); py_Ref slice = py_retval();
py_newslice(slice); py_newslice(slice);
py_setslot(slice, 0, py_arg(1)); py_setslot(slice, 0, py_arg(1));
@ -24,7 +23,7 @@ static bool _py_slice__new__(int argc, py_Ref argv){
return true; return true;
} }
static bool _py_slice__repr__(int argc, py_Ref argv){ static bool _py_slice__repr__(int argc, py_Ref argv) {
c11_sbuf buf; c11_sbuf buf;
c11_sbuf__ctor(&buf); c11_sbuf__ctor(&buf);
c11_sbuf__write_cstr(&buf, "slice("); c11_sbuf__write_cstr(&buf, "slice(");
@ -45,9 +44,8 @@ static bool _py_slice__repr__(int argc, py_Ref argv){
return true; return true;
} }
py_Type pk_slice__register(){ py_Type pk_slice__register() {
pk_VM* vm = pk_current_vm; py_Type type = pk_newtype("slice", tp_object, NULL, NULL, false, true);
py_Type type = pk_VM__new_type(vm, "slice", tp_object, NULL, false);
py_bindmagic(type, __new__, _py_slice__new__); py_bindmagic(type, __new__, _py_slice__new__);
py_bindmagic(type, __repr__, _py_slice__repr__); py_bindmagic(type, __repr__, _py_slice__repr__);

View File

@ -463,8 +463,7 @@ static bool _py_str__index(int argc, py_Ref argv) {
} }
py_Type pk_str__register() { py_Type pk_str__register() {
pk_VM* vm = pk_current_vm; py_Type type = pk_newtype("str", tp_object, NULL, NULL, false, true);
py_Type type = pk_VM__new_type(vm, "str", tp_object, NULL, false);
// no need to dtor because the memory is controlled by the object // no need to dtor because the memory is controlled by the object
py_bindmagic(tp_str, __new__, _py_str__new__); py_bindmagic(tp_str, __new__, _py_str__new__);
@ -534,8 +533,8 @@ static bool _py_str_iterator__next__(int argc, py_Ref argv) {
} }
py_Type pk_str_iterator__register() { py_Type pk_str_iterator__register() {
pk_VM* vm = pk_current_vm; py_Type type = pk_newtype("str_iterator", tp_object, NULL, NULL, false, true);
py_Type type = pk_VM__new_type(vm, "str_iterator", tp_object, NULL, false);
py_bindmagic(type, __new__, _py_str_iterator__new__); py_bindmagic(type, __new__, _py_str_iterator__new__);
py_bindmagic(type, __iter__, _py_str_iterator__iter__); py_bindmagic(type, __iter__, _py_str_iterator__iter__);
py_bindmagic(type, __next__, _py_str_iterator__next__); py_bindmagic(type, __next__, _py_str_iterator__next__);
@ -543,8 +542,7 @@ py_Type pk_str_iterator__register() {
} }
py_Type pk_bytes__register() { py_Type pk_bytes__register() {
pk_VM* vm = pk_current_vm; py_Type type = pk_newtype("bytes", tp_object, NULL, NULL, false, true);
py_Type type = pk_VM__new_type(vm, "bytes", tp_object, NULL, false);
// no need to dtor because the memory is controlled by the object // no need to dtor because the memory is controlled by the object
return type; return type;
} }

View File

@ -126,8 +126,7 @@ static bool _py_tuple__iter__(int argc, py_Ref argv) {
} }
py_Type pk_tuple__register() { py_Type pk_tuple__register() {
pk_VM* vm = pk_current_vm; py_Type type = pk_newtype("tuple", tp_object, NULL, NULL, false, true);
py_Type type = pk_VM__new_type(vm, "tuple", tp_object, NULL, false);
py_bindmagic(type, __len__, _py_tuple__len__); py_bindmagic(type, __len__, _py_tuple__len__);
py_bindmagic(type, __repr__, _py_tuple__repr__); py_bindmagic(type, __repr__, _py_tuple__repr__);