Просмотр исходного кода

change built-in Types to constexpr

blueloveTH 2 лет назад
Родитель
Сommit
ff51469dc4
4 измененных файлов с 33 добавлено и 32 удалено
  1. 1 2
      include/pocketpy/common.h
  2. 8 6
      include/pocketpy/vm.h
  3. 1 1
      src/cffi.cpp
  4. 23 23
      src/vm.cpp

+ 1 - 2
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; }

+ 8 - 6
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__;
 

+ 1 - 1
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){         \

+ 23 - 23
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>(Type(1), Type(0)), -1, nullptr, "object", true});
     _all_types.push_back({heap._new<Type>(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();
-
-    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_int == _new_type_object("int"));
+    PK_ASSERT(tp_float == _new_type_object("float"));
+
+    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<Dummy>(_new_type_object("NoneType"));
     this->NotImplemented = heap._new<Dummy>(_new_type_object("NotImplementedType"));