diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index de61d9f7..b005a5d5 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -1,15 +1,17 @@ name: build on: - push: - paths-ignore: - - 'docs/**' - - 'web/**' - - '**.md' - pull_request: - paths-ignore: - - 'docs/**' - - 'web/**' - - '**.md' + push: + branches: [ main ] + paths-ignore: + - 'docs/**' + - 'web/**' + - '**.md' + pull_request: + branches: [ main ] + paths-ignore: + - 'docs/**' + - 'web/**' + - '**.md' jobs: build_win32_amalgamated: runs-on: windows-latest diff --git a/include/pocketpy/any.h b/include/pocketpy/any.h index f21478b4..0035b540 100644 --- a/include/pocketpy/any.h +++ b/include/pocketpy/any.h @@ -28,7 +28,7 @@ struct any{ any() : data(nullptr), _vt(nullptr) {} - operator bool() const { return _vt != nullptr; } + explicit operator bool() const { return _vt != nullptr; } template any(T&& value){ @@ -85,7 +85,7 @@ struct function{ function(): _impl(), _wrapper(nullptr) {} - operator bool() const { return _wrapper != nullptr; } + explicit operator bool() const { return _wrapper != nullptr; } template function(F&& f) : _impl(std::forward(f)){ @@ -110,7 +110,7 @@ struct lightfunction{ lightfunction() : _impl(nullptr), _wrapper(nullptr) {} - operator bool() const { return _wrapper != nullptr; } + explicit operator bool() const { return _wrapper != nullptr; } template lightfunction(const F& f){ diff --git a/include/pocketpy/common.h b/include/pocketpy/common.h index fc8798e8..68b172cd 100644 --- a/include/pocketpy/common.h +++ b/include/pocketpy/common.h @@ -102,7 +102,7 @@ struct Type { explicit constexpr Type(int index): index(index) {} bool operator==(Type other) const { return this->index == other.index; } bool operator!=(Type other) const { return this->index != other.index; } - operator int() const { return this->index; } + explicit operator bool() const { return index != -1; } }; #define PK_LAMBDA(x) ([](VM* vm, ArgsView args) { return x; }) @@ -177,19 +177,27 @@ struct PyVar final{ uint8_t flags; char _bytes[12]; - PyVar(): type(), is_sso(false), flags(0) { } - PyVar(std::nullptr_t): type(), is_sso(false), flags(0) { } - PyVar(Type type, bool is_sso): type(type), is_sso(is_sso), flags(0) { } - PyVar(Type type, PyObject* p): type(type), is_sso(false), flags(0) { as() = p; } + // uninitialized + PyVar(): type() { } + // zero initialized + PyVar(std::nullptr_t): type(), is_sso(false), flags(0), _bytes{0} { } + // PyObject* initialized (is_sso = false) + PyVar(Type type, PyObject* p): type(type), is_sso(false), flags(0) { + as() = p; + } + // SSO initialized (is_sso = true) + template + PyVar(Type type, T value): type(type), is_sso(true), flags(0) { + as() = value; + } template T& as() const { static_assert(!std::is_reference_v); - PK_DEBUG_ASSERT(is_sso) return *(T*)_bytes; } - operator bool() const { return type; } + explicit operator bool() const { return (bool)type; } bool operator==(const PyVar& other) const { return memcmp(this, &other, sizeof(PyVar)) == 0; @@ -199,14 +207,33 @@ struct PyVar final{ return memcmp(this, &other, sizeof(PyVar)) != 0; } - bool operator==(std::nullptr_t) const { return !type; } - bool operator!=(std::nullptr_t) const { return type; } + bool operator==(std::nullptr_t) const { return !(bool)type; } + bool operator!=(std::nullptr_t) const { return (bool)type; } + + PyObject* get() const { + PK_DEBUG_ASSERT(!is_sso) + return as(); + } + + PyObject* operator->() const { + PK_DEBUG_ASSERT(!is_sso) + return as(); + } - PyObject* get() const { return as(); } i64 hash() const { return as(); } - PyObject* operator->() const { return as(); } }; static_assert(sizeof(PyVar) == 16 && is_pod_v); } // namespace pkpy + + +// specialize std::less for PyVar +namespace std { + template<> + struct less { + bool operator()(const pkpy::PyVar& lhs, const pkpy::PyVar& rhs) const { + return memcmp(&lhs, &rhs, sizeof(pkpy::PyVar)) < 0; + } + }; +} \ No newline at end of file diff --git a/include/pocketpy/str.h b/include/pocketpy/str.h index 063035d9..420dfdb3 100644 --- a/include/pocketpy/str.h +++ b/include/pocketpy/str.h @@ -128,7 +128,7 @@ struct StrName { struct SStream{ PK_ALWAYS_PASS_BY_POINTER(SStream) - // pod_vector is allocated by pool64 so the buffer can be moved into Str without a copy + // pod_vector is allocated by pool128 so the buffer can be moved into Str without a copy pod_vector buffer; int _precision = -1; diff --git a/include/pocketpy/vm.h b/include/pocketpy/vm.h index 7880e413..34f1748d 100644 --- a/include/pocketpy/vm.h +++ b/include/pocketpy/vm.h @@ -366,10 +366,10 @@ public: void check_compatible_type(PyVar obj, Type type){ if(!isinstance(obj, type)) TypeError(type, _tp(obj)); } Type _tp(PyVar obj){ return obj.type; } - const PyTypeInfo* _tp_info(PyVar obj) { return &_all_types[_tp(obj)]; } - const PyTypeInfo* _tp_info(Type type) { return &_all_types[type]; } - PyVar _t(PyVar obj){ return _all_types[_tp(obj)].obj; } - PyVar _t(Type type){ return _all_types[type].obj; } + const PyTypeInfo* _tp_info(PyVar obj) { return &_all_types[_tp(obj).index]; } + const PyTypeInfo* _tp_info(Type type) { return &_all_types[type.index]; } + PyVar _t(PyVar obj){ return _all_types[_tp(obj).index].obj; } + PyVar _t(Type type){ return _all_types[type.index].obj; } #endif #if PK_REGION("User Type Registration") @@ -480,14 +480,10 @@ PyVar py_var(VM* vm, __T&& value){ return value ? vm->True : vm->False; }else if constexpr(is_integral_v){ // int - PyVar retval(VM::tp_int, true); - retval.as() = static_cast(value); - return retval; + return PyVar(VM::tp_int, static_cast(value)); }else if constexpr(is_floating_point_v){ // float - PyVar retval(VM::tp_float, true); - retval.as() = static_cast(value); - return retval; + return PyVar(VM::tp_float, static_cast(value)); }else if constexpr(std::is_pointer_v){ return from_void_p(vm, (void*)value); }else{ diff --git a/src/ceval.cpp b/src/ceval.cpp index 683ee46e..6f6684eb 100644 --- a/src/ceval.cpp +++ b/src/ceval.cpp @@ -898,9 +898,9 @@ __NEXT_STEP:; StrName _name(byte.arg); frame->_module->attr().set(_name, __curr_class); // call on_end_subclass - PyTypeInfo* ti = &_all_types[PK_OBJ_GET(Type, __curr_class)]; + PyTypeInfo* ti = &_all_types[PK_OBJ_GET(Type, __curr_class).index]; if(ti->base != tp_object){ - PyTypeInfo* base_ti = &_all_types[ti->base]; + PyTypeInfo* base_ti = &_all_types[ti->base.index]; if(base_ti->on_end_subclass) base_ti->on_end_subclass(this, ti); } __curr_class = nullptr; @@ -924,7 +924,7 @@ __NEXT_STEP:; PK_ASSERT(__curr_class != nullptr); StrName _name(byte.arg); Type type = PK_OBJ_GET(Type, __curr_class); - _all_types[type].annotated_fields.push_back(_name); + _all_types[type.index].annotated_fields.push_back(_name); } DISPATCH() /*****************************************/ case OP_WITH_ENTER: diff --git a/src/dataclasses.cpp b/src/dataclasses.cpp index 842b5873..212c3558 100644 --- a/src/dataclasses.cpp +++ b/src/dataclasses.cpp @@ -13,7 +13,7 @@ static void patch__init__(VM* vm, Type cls){ }); Type cls = vm->_tp(self); - const PyTypeInfo* cls_info = &vm->_all_types[cls]; + const PyTypeInfo* cls_info = &vm->_all_types[cls.index]; NameDict& cls_d = cls_info->obj->attr(); const auto& fields = cls_info->annotated_fields; @@ -46,7 +46,7 @@ static void patch__init__(VM* vm, Type cls){ static void patch__repr__(VM* vm, Type cls){ vm->bind__repr__(cls, [](VM* vm, PyVar _0) -> Str{ - const PyTypeInfo* cls_info = &vm->_all_types[vm->_tp(_0)]; + const PyTypeInfo* cls_info = &vm->_all_types[vm->_tp(_0).index]; const auto& fields = cls_info->annotated_fields; const NameDict& obj_d = _0->attr(); SStream ss; @@ -65,7 +65,7 @@ static void patch__repr__(VM* vm, Type cls){ static void patch__eq__(VM* vm, Type cls){ vm->bind__eq__(cls, [](VM* vm, PyVar _0, PyVar _1){ if(vm->_tp(_0) != vm->_tp(_1)) return vm->NotImplemented; - const PyTypeInfo* cls_info = &vm->_all_types[vm->_tp(_0)]; + const PyTypeInfo* cls_info = &vm->_all_types[vm->_tp(_0).index]; const auto& fields = cls_info->annotated_fields; for(StrName field: fields){ PyVar lhs = _0->attr(field); @@ -88,7 +88,7 @@ void add_module_dataclasses(VM* vm){ if(!cls_d.contains(__repr__)) patch__repr__(vm, cls); if(!cls_d.contains(__eq__)) patch__eq__(vm, cls); - const auto& fields = vm->_all_types[cls].annotated_fields; + const auto& fields = vm->_all_types[cls.index].annotated_fields; bool has_default = false; for(StrName field: fields){ if(cls_d.contains(field)){ diff --git a/src/pocketpy.cpp b/src/pocketpy.cpp index d29303cb..9394b7ae 100644 --- a/src/pocketpy.cpp +++ b/src/pocketpy.cpp @@ -104,7 +104,7 @@ void __init_builtins(VM* _vm) { StrName _1 = _type_name(vm, type); vm->TypeError("super(): " + _0.escape() + " is not an instance of " + _1.escape()); } - return vm->heap.gcnew(vm->tp_super, self_arg, vm->_all_types[type].base); + return vm->heap.gcnew(vm->tp_super, self_arg, vm->_all_types[type.index].base); }); _vm->bind_func(_vm->builtins, "staticmethod", 1, [](VM* vm, ArgsView args) { @@ -1502,7 +1502,7 @@ void VM::__post_init_builtin_types(){ bind_func(tp_module, __new__, -1, PK_ACTION(vm->NotImplementedError())); - _all_types[tp_module].m__getattr__ = [](VM* vm, PyVar obj, StrName name) -> PyVar{ + _all_types[tp_module.index].m__getattr__ = [](VM* vm, PyVar obj, StrName name) -> PyVar{ const Str& path = CAST(Str&, obj->attr(__path__)); return vm->py_import(_S(path, ".", name.sv()), false); }; @@ -1522,22 +1522,22 @@ void VM::__post_init_builtin_types(){ bind__repr__(tp_type, [](VM* vm, PyVar self) -> Str{ SStream ss; - const PyTypeInfo& info = vm->_all_types[PK_OBJ_GET(Type, self)]; + const PyTypeInfo& info = vm->_all_types[PK_OBJ_GET(Type, self).index]; ss << ""; return ss.str(); }); bind_property(_t(tp_object), "__class__", PK_LAMBDA(vm->_t(args[0]))); bind_property(_t(tp_type), "__base__", [](VM* vm, ArgsView args){ - const PyTypeInfo& info = vm->_all_types[PK_OBJ_GET(Type, args[0])]; - return info.base.index == -1 ? vm->None : vm->_all_types[info.base].obj; + const PyTypeInfo& info = vm->_all_types[PK_OBJ_GET(Type, args[0]).index]; + return info.base.index == -1 ? vm->None : vm->_all_types[info.base.index].obj; }); bind_property(_t(tp_type), "__name__", [](VM* vm, ArgsView args){ - const PyTypeInfo& info = vm->_all_types[PK_OBJ_GET(Type, args[0])]; + const PyTypeInfo& info = vm->_all_types[PK_OBJ_GET(Type, args[0]).index]; return VAR(info.name.sv()); }); bind_property(_t(tp_type), "__module__", [](VM* vm, ArgsView args){ - const PyTypeInfo& info = vm->_all_types[PK_OBJ_GET(Type, args[0])]; + const PyTypeInfo& info = vm->_all_types[PK_OBJ_GET(Type, args[0]).index]; if(info.mod == nullptr) return vm->None; return info.mod; }); diff --git a/src/str.cpp b/src/str.cpp index 5f2d38bc..a6abf25d 100644 --- a/src/str.cpp +++ b/src/str.cpp @@ -17,7 +17,7 @@ int utf8len(unsigned char c, bool suppress){ if(this->size < (int)sizeof(this->_inlined)){ \ this->data = this->_inlined; \ }else{ \ - this->data = (char*)pool64_alloc(this->size+1); \ + this->data = (char*)pool128_alloc(this->size+1); \ } #define PK_STR_COPY_INIT(__s) \ @@ -97,7 +97,7 @@ int utf8len(unsigned char c, bool suppress){ } Str& Str::operator=(const Str& other){ - if(!is_inlined()) pool64_dealloc(data); + if(!is_inlined()) pool128_dealloc(data); size = other.size; is_ascii = other.is_ascii; PK_STR_ALLOCATE() @@ -168,7 +168,7 @@ int utf8len(unsigned char c, bool suppress){ } Str::~Str(){ - if(!is_inlined()) pool64_dealloc(data); + if(!is_inlined()) pool128_dealloc(data); } Str Str::substr(int start, int len) const { diff --git a/src/vm.cpp b/src/vm.cpp index 952208ac..7fae50db 100644 --- a/src/vm.cpp +++ b/src/vm.cpp @@ -148,7 +148,7 @@ namespace pkpy{ do{ val = _t(cls)->attr().try_get(name); if(val != nullptr) return val; - cls = _all_types[cls].base; + cls = _all_types[cls.index].base; if(cls.index == -1) break; }while(true); return nullptr; @@ -161,7 +161,7 @@ namespace pkpy{ bool VM::issubclass(Type cls, Type base){ do{ if(cls == base) return true; - Type next = _all_types[cls].base; + Type next = _all_types[cls.index].base; if(next.index == -1) break; cls = next; }while(true); @@ -207,7 +207,7 @@ namespace pkpy{ PyVar VM::new_type_object(PyVar mod, StrName name, Type base, bool subclass_enabled){ PyVar obj = heap._new(tp_type, Type(_all_types.size())); - const PyTypeInfo& base_info = _all_types[base]; + const PyTypeInfo& base_info = _all_types[base.index]; if(!base_info.subclass_enabled){ Str error = _S("type ", base_info.name.escape(), " is not `subclass_enabled`"); throw std::runtime_error(error.c_str()); @@ -280,10 +280,10 @@ namespace pkpy{ bool VM::py_callable(PyVar obj){ Type cls = vm->_tp(obj); switch(cls.index){ - case VM::tp_function.index: return vm->True; - case VM::tp_native_func.index: return vm->True; - case VM::tp_bound_method.index: return vm->True; - case VM::tp_type.index: return vm->True; + case VM::tp_function.index: return true; + case VM::tp_native_func.index: return true; + case VM::tp_bound_method.index: return true; + case VM::tp_type.index: return true; } return vm->find_name_in_mro(cls, __call__) != nullptr; } @@ -501,7 +501,7 @@ i64 VM::py_hash(PyVar obj){ return CAST(i64, ret); } // if it is trivial `object`, return PK_BITS - if(ti == &_all_types[tp_object]) return obj.hash(); + if(ti == &_all_types[tp_object.index]) return obj.hash(); // otherwise, we check if it has a custom __eq__ other than object.__eq__ bool has_custom_eq = false; if(ti->m__eq__) has_custom_eq = true; @@ -868,7 +868,7 @@ void VM::__init_builtin_types(){ this->Ellipsis = heap._new(_new_type("ellipsis")); this->True = heap._new(tp_bool); this->False = heap._new(tp_bool); - this->StopIteration = _all_types[_new_type("StopIteration", tp_exception)].obj; + this->StopIteration = _all_types[_new_type("StopIteration", tp_exception).index].obj; this->builtins = new_module("builtins"); @@ -1182,7 +1182,7 @@ PyVar VM::getattr(PyVar obj, StrName name, bool throw_err){ if(cls_var != nullptr){ // bound method is non-data descriptor if(!is_tagged(cls_var)){ - switch(cls_var.type){ + switch(cls_var.type.index){ case tp_function.index: return VAR(BoundMethod(obj, cls_var)); case tp_native_func.index: @@ -1196,7 +1196,7 @@ PyVar VM::getattr(PyVar obj, StrName name, bool throw_err){ return cls_var; } - const PyTypeInfo* ti = &_all_types[objtype]; + const PyTypeInfo* ti = &_all_types[objtype.index]; if(ti->m__getattr__){ PyVar ret = ti->m__getattr__(this, obj, name); if(ret) return ret; @@ -1249,7 +1249,7 @@ PyVar VM::get_unbound_method(PyVar obj, StrName name, PyVar* self, bool throw_er if(cls_var != nullptr){ if(!is_tagged(cls_var)){ - switch(cls_var.type){ + switch(cls_var.type.index){ case tp_function.index: *self = obj; break; @@ -1267,7 +1267,7 @@ PyVar VM::get_unbound_method(PyVar obj, StrName name, PyVar* self, bool throw_er return cls_var; } - const PyTypeInfo* ti = &_all_types[objtype]; + const PyTypeInfo* ti = &_all_types[objtype.index]; if(fallback && ti->m__getattr__){ PyVar ret = ti->m__getattr__(this, obj, name); if(ret) return ret; @@ -1301,7 +1301,7 @@ void VM::setattr(PyVar obj, StrName name, PyVar value){ } } - const PyTypeInfo* ti = &_all_types[objtype]; + const PyTypeInfo* ti = &_all_types[objtype.index]; if(ti->m__setattr__){ ti->m__setattr__(this, obj, name, value); return; @@ -1429,7 +1429,7 @@ void ManagedHeap::mark() { } StrName _type_name(VM *vm, Type type){ - return vm->_all_types[type].name; + return vm->_all_types[type.index].name; } void _gc_mark_namedict(NameDict* t){ @@ -1439,14 +1439,14 @@ void _gc_mark_namedict(NameDict* t){ } void VM::bind__getitem__(Type type, PyVar (*f)(VM*, PyVar, PyVar)){ - _all_types[type].m__getitem__ = f; + _all_types[type.index].m__getitem__ = f; bind_func(type, __getitem__, 2, [](VM* vm, ArgsView args){ return lambda_get_userdata(args.begin())(vm, args[0], args[1]); }, f); } void VM::bind__setitem__(Type type, void (*f)(VM*, PyVar, PyVar, PyVar)){ - _all_types[type].m__setitem__ = f; + _all_types[type.index].m__setitem__ = f; bind_func(type, __setitem__, 3, [](VM* vm, ArgsView args){ lambda_get_userdata(args.begin())(vm, args[0], args[1], args[2]); return vm->None; @@ -1454,7 +1454,7 @@ void VM::bind__setitem__(Type type, void (*f)(VM*, PyVar, PyVar, PyVar)){ } void VM::bind__delitem__(Type type, void (*f)(VM*, PyVar, PyVar)){ - _all_types[type].m__delitem__ = f; + _all_types[type.index].m__delitem__ = f; bind_func(type, __delitem__, 2, [](VM* vm, ArgsView args){ lambda_get_userdata(args.begin())(vm, args[0], args[1]); return vm->None; @@ -1470,7 +1470,7 @@ PyVar VM::__pack_next_retval(unsigned n){ } void VM::bind__next__(Type type, unsigned (*f)(VM*, PyVar)){ - _all_types[type].m__next__ = f; + _all_types[type.index].m__next__ = f; bind_func(type, __next__, 1, [](VM* vm, ArgsView args){ int n = lambda_get_userdata(args.begin())(vm, args[0]); return vm->__pack_next_retval(n); @@ -1485,10 +1485,10 @@ void VM::bind__next__(Type type, PyVar (*f)(VM*, PyVar)){ } #define BIND_UNARY_SPECIAL(name) \ - void VM::bind##name(Type type, PyVar (*f)(VM*, PyVar)){ \ - _all_types[type].m##name = f; \ + void VM::bind##name(Type type, PyVar (*f)(VM*, PyVar)){ \ + _all_types[type.index].m##name = f; \ bind_func(type, name, 1, [](VM* vm, ArgsView args){ \ - return lambda_get_userdata(args.begin())(vm, args[0]); \ + return lambda_get_userdata(args.begin())(vm, args[0]);\ }, f); \ } BIND_UNARY_SPECIAL(__iter__) @@ -1497,7 +1497,7 @@ void VM::bind__next__(Type type, PyVar (*f)(VM*, PyVar)){ #undef BIND_UNARY_SPECIAL void VM::bind__str__(Type type, Str (*f)(VM*, PyVar)){ - _all_types[type].m__str__ = f; + _all_types[type.index].m__str__ = f; bind_func(type, __str__, 1, [](VM* vm, ArgsView args){ Str s = lambda_get_userdata(args.begin())(vm, args[0]); return VAR(s); @@ -1505,7 +1505,7 @@ void VM::bind__str__(Type type, Str (*f)(VM*, PyVar)){ } void VM::bind__repr__(Type type, Str (*f)(VM*, PyVar)){ - _all_types[type].m__repr__ = f; + _all_types[type.index].m__repr__ = f; bind_func(type, __repr__, 1, [](VM* vm, ArgsView args){ Str s = lambda_get_userdata(args.begin())(vm, args[0]); return VAR(s); @@ -1513,7 +1513,7 @@ void VM::bind__repr__(Type type, Str (*f)(VM*, PyVar)){ } void VM::bind__hash__(Type type, i64 (*f)(VM*, PyVar)){ - _all_types[type].m__hash__ = f; + _all_types[type.index].m__hash__ = f; bind_func(type, __hash__, 1, [](VM* vm, ArgsView args){ i64 ret = lambda_get_userdata(args.begin())(vm, args[0]); return VAR(ret); @@ -1521,7 +1521,7 @@ void VM::bind__hash__(Type type, i64 (*f)(VM*, PyVar)){ } void VM::bind__len__(Type type, i64 (*f)(VM*, PyVar)){ - _all_types[type].m__len__ = f; + _all_types[type.index].m__len__ = f; bind_func(type, __len__, 1, [](VM* vm, ArgsView args){ i64 ret = lambda_get_userdata(args.begin())(vm, args[0]); return VAR(ret); @@ -1531,7 +1531,7 @@ void VM::bind__len__(Type type, i64 (*f)(VM*, PyVar)){ #define BIND_BINARY_SPECIAL(name) \ void VM::bind##name(Type type, BinaryFuncC f){ \ - _all_types[type].m##name = f; \ + _all_types[type.index].m##name = f; \ bind_func(type, name, 2, [](VM* vm, ArgsView args){ \ return lambda_get_userdata(args.begin())(vm, args[0], args[1]);\ }, f); \