From ff51469dc48908b4c815086f76bbd5eab4d53545 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sun, 24 Dec 2023 00:15:23 +0800 Subject: [PATCH 1/2] change built-in Types to constexpr --- include/pocketpy/common.h | 3 +-- include/pocketpy/vm.h | 14 +++++++------ src/cffi.cpp | 2 +- src/vm.cpp | 42 +++++++++++++++++++-------------------- 4 files changed, 31 insertions(+), 30 deletions(-) diff --git a/include/pocketpy/common.h b/include/pocketpy/common.h index 85172aab..1459cbf7 100644 --- a/include/pocketpy/common.h +++ b/include/pocketpy/common.h @@ -174,8 +174,7 @@ struct Discarded { }; struct Type { int index; - Type(): index(-1) {} - Type(int index): index(index) {} + 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; } diff --git a/include/pocketpy/vm.h b/include/pocketpy/vm.h index f07a09b8..fd10974a 100644 --- a/include/pocketpy/vm.h +++ b/include/pocketpy/vm.h @@ -150,12 +150,14 @@ public: unsigned char* (*_import_handler)(const char*, int, int*); // for quick access - Type tp_object, tp_type, tp_int, tp_float, tp_bool, tp_str; - Type tp_list, tp_tuple; - Type tp_function, tp_native_func, tp_bound_method; - Type tp_slice, tp_range, tp_module; - Type tp_super, tp_exception, tp_bytes, tp_mappingproxy; - Type tp_dict, tp_property, tp_star_wrapper; + static constexpr Type tp_object=0, tp_type=1; + static constexpr Type tp_int=kTpIntIndex, tp_float=kTpFloatIndex, tp_bool=4, tp_str=5; + static constexpr Type tp_list=6, tp_tuple=7; + static constexpr Type tp_slice=8, tp_range=9, tp_module=10; + static constexpr Type tp_function=11, tp_native_func=12, tp_bound_method=13; + + static constexpr Type tp_super=14, tp_exception=15, tp_bytes=16, tp_mappingproxy=17; + static constexpr Type tp_dict=18, tp_property=19, tp_star_wrapper=20; PyObject* cached_object__new__; diff --git a/src/cffi.cpp b/src/cffi.cpp index c5684c4f..124bfbe5 100644 --- a/src/cffi.cpp +++ b/src/cffi.cpp @@ -161,7 +161,7 @@ void add_module_c(VM* vm){ }); PyObject* type; - Type type_t; + Type type_t = -1; #define BIND_PRIMITIVE(T, CNAME) \ vm->bind_func<1>(mod, CNAME "_", [](VM* vm, ArgsView args){ \ diff --git a/src/vm.cpp b/src/vm.cpp index 632aeeac..70f41a6e 100644 --- a/src/vm.cpp +++ b/src/vm.cpp @@ -708,29 +708,29 @@ void VM::_log_s_data(const char* title) { void VM::init_builtin_types(){ _all_types.push_back({heap._new(Type(1), Type(0)), -1, nullptr, "object", true}); _all_types.push_back({heap._new(Type(1), Type(1)), 0, nullptr, "type", false}); - tp_object = 0; tp_type = 1; - tp_int = _new_type_object("int"); - tp_float = _new_type_object("float"); - if(tp_int.index != kTpIntIndex || tp_float.index != kTpFloatIndex) FATAL_ERROR(); + PK_ASSERT(tp_int == _new_type_object("int")); + PK_ASSERT(tp_float == _new_type_object("float")); - tp_bool = _new_type_object("bool"); - tp_str = _new_type_object("str"); - tp_list = _new_type_object("list"); - tp_tuple = _new_type_object("tuple"); - tp_slice = _new_type_object("slice"); - tp_range = _new_type_object("range"); - tp_module = _new_type_object("module"); - tp_function = _new_type_object("function"); - tp_native_func = _new_type_object("native_func"); - tp_bound_method = _new_type_object("bound_method"); - tp_super = _new_type_object("super"); - tp_exception = _new_type_object("_Exception"); - tp_bytes = _new_type_object("bytes"); - tp_mappingproxy = _new_type_object("mappingproxy"); - tp_dict = _new_type_object("dict"); - tp_property = _new_type_object("property"); - tp_star_wrapper = _new_type_object("_star_wrapper"); + PK_ASSERT(tp_bool == _new_type_object("bool")); + PK_ASSERT(tp_str == _new_type_object("str")); + PK_ASSERT(tp_list == _new_type_object("list")); + PK_ASSERT(tp_tuple == _new_type_object("tuple")); + + PK_ASSERT(tp_slice == _new_type_object("slice")); + PK_ASSERT(tp_range == _new_type_object("range")); + PK_ASSERT(tp_module == _new_type_object("module")); + PK_ASSERT(tp_function == _new_type_object("function")); + PK_ASSERT(tp_native_func == _new_type_object("native_func")); + PK_ASSERT(tp_bound_method == _new_type_object("bound_method")); + + PK_ASSERT(tp_super == _new_type_object("super")); + PK_ASSERT(tp_exception == _new_type_object("_Exception")); + PK_ASSERT(tp_bytes == _new_type_object("bytes")); + PK_ASSERT(tp_mappingproxy == _new_type_object("mappingproxy")); + PK_ASSERT(tp_dict == _new_type_object("dict")); + PK_ASSERT(tp_property == _new_type_object("property")); + PK_ASSERT(tp_star_wrapper == _new_type_object("_star_wrapper")); this->None = heap._new(_new_type_object("NoneType")); this->NotImplemented = heap._new(_new_type_object("NotImplementedType")); From 03342a523004dd7c2a48779010f67c0de0e4d779 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sun, 24 Dec 2023 00:27:01 +0800 Subject: [PATCH 2/2] ... --- include/pocketpy/cffi.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/pocketpy/cffi.h b/include/pocketpy/cffi.h index a2937b06..a60cb11a 100644 --- a/include/pocketpy/cffi.h +++ b/include/pocketpy/cffi.h @@ -9,7 +9,7 @@ namespace pkpy { #define PY_CLASS(T, mod, name) \ static Type _type(VM* vm) { \ PK_LOCAL_STATIC const std::pair _path(#mod, #name); \ - return PK_OBJ_GET(Type, vm->_modules[_path.first]->attr()[_path.second]); \ + return PK_OBJ_GET(Type, vm->_modules[_path.first]->attr(_path.second)); \ } \ static void _check_type(VM* vm, PyObject* val){ \ if(!vm->isinstance(val, T::_type(vm))){ \