Przeglądaj źródła

rename `UNREACHABLE()` to `PK_UNREACHABLE()`

blueloveTH 2 lat temu
rodzic
commit
e150ffae5e

+ 2 - 2
3rd/cjson/src/cJSONw.cpp

@@ -50,7 +50,7 @@ static cJSON* convert_python_object_to_cjson(PyObject* obj, VM* vm){
     }else{
         vm->TypeError(fmt("unrecognized type ", obj_type_name(vm, obj_t).escape()));
     }
-    UNREACHABLE();
+    PK_UNREACHABLE();
 }
 
 
@@ -124,7 +124,7 @@ void add_module_cjson(VM* vm){
             const char* end = start;
             while(*end != '\0' && *end != '\n') end++;
             vm->IOError(fmt("cjson: ", std::string_view(start, end-start)));
-            UNREACHABLE();
+            PK_UNREACHABLE();
         }
         PyObject* output = convert_cjson_to_python_object(json, vm);
         cJSON_Delete(json);

+ 2 - 2
examples/abort-the-vm/user_config.h

@@ -66,10 +66,10 @@
 
 #ifdef _MSC_VER
 #define PK_ENABLE_COMPUTED_GOTO		0
-#define UNREACHABLE()				__assume(0)
+#define PK_UNREACHABLE()				__assume(0)
 #else
 #define PK_ENABLE_COMPUTED_GOTO		1
-#define UNREACHABLE()				__builtin_unreachable()
+#define PK_UNREACHABLE()				__builtin_unreachable()
 #endif
 
 

+ 2 - 2
include/pocketpy/config.h

@@ -78,10 +78,10 @@
 
 #ifdef _MSC_VER
 #define PK_ENABLE_COMPUTED_GOTO		0
-#define UNREACHABLE()				__assume(0)
+#define PK_UNREACHABLE()				__assume(0)
 #else
 #define PK_ENABLE_COMPUTED_GOTO		1
-#define UNREACHABLE()				__builtin_unreachable()
+#define PK_UNREACHABLE()				__builtin_unreachable()
 #endif
 
 

+ 1 - 1
include/pocketpy/namedict.h

@@ -49,7 +49,7 @@ struct SmallNameDict{
                 return true;
             }
         )
-        UNREACHABLE();
+        PK_UNREACHABLE();
     }
 
     V try_get(K key) const {

+ 0 - 1
include/pocketpy/vm.h

@@ -223,7 +223,6 @@ public:
     PyObject* _find_type_object(const Str& type);
 
     Type _type(const Str& type);
-    PyTypeInfo* _type_info(const Str& type);
     PyTypeInfo* _type_info(Type type);
     const PyTypeInfo* _inst_type_info(PyObject* obj);
 

+ 2 - 2
src/ceval.cpp

@@ -799,7 +799,7 @@ __NEXT_STEP:;
         }
         if(!isinstance(TOP(), tp_exception)){
             _builtin_error("TypeError", "exceptions must derive from Exception");
-            UNREACHABLE();
+            PK_UNREACHABLE();
         }
         _error(POPX());
     } DISPATCH();
@@ -855,7 +855,7 @@ __NEXT_STEP:;
 #undef DISPATCH_OP_CALL
 #undef CEVAL_STEP
 /**********************************************************************/
-            UNREACHABLE();
+            PK_UNREACHABLE();
         }catch(HandledException){
             continue;
         }catch(UnhandledException){

+ 1 - 1
src/expr.cpp

@@ -611,7 +611,7 @@ namespace pkpy{
             case TK("!="):  ctx->emit_(OP_COMPARE_NE, BC_NOARG, line);  break;
             case TK(">"):   ctx->emit_(OP_COMPARE_GT, BC_NOARG, line);  break;
             case TK(">="):  ctx->emit_(OP_COMPARE_GE, BC_NOARG, line);  break;
-            default: UNREACHABLE();
+            default: PK_UNREACHABLE();
         }
         // [b, RES]
         int index = ctx->emit_(OP_SHORTCUT_IF_FALSE_OR_POP, BC_NOARG, line);

+ 8 - 16
src/vm.cpp

@@ -1,4 +1,5 @@
 #include "pocketpy/vm.h"
+#include "pocketpy/config.h"
 
 namespace pkpy{
 
@@ -27,7 +28,7 @@ namespace pkpy{
                 first = false;
                 if(!is_non_tagged_type(k, vm->tp_str)){
                     vm->TypeError(fmt("json keys must be string, got ", obj_type_name(vm, vm->_tp(k))));
-                    UNREACHABLE();
+                    PK_UNREACHABLE();
                 }
                 ss << _CAST(Str&, k).escape(false) << ": ";
                 write_object(v);
@@ -57,7 +58,7 @@ namespace pkpy{
                 write_dict(_CAST(Dict&, obj));
             }else{
                 vm->TypeError(fmt("unrecognized type ", obj_type_name(vm, obj_t).escape()));
-                UNREACHABLE();
+                PK_UNREACHABLE();
             }
         }
 
@@ -236,15 +237,6 @@ namespace pkpy{
         return PK_OBJ_GET(Type, obj);
     }
 
-    PyTypeInfo* VM::_type_info(const Str& type){
-        PyObject* obj = builtins->attr().try_get_likely_found(type);
-        if(obj == nullptr){
-            for(auto& t: _all_types) if(t.name == type) return &t;
-            PK_FATAL_ERROR();
-        }
-        return &_all_types[PK_OBJ_GET(Type, obj)];
-    }
-
     PyTypeInfo* VM::_type_info(Type type){
         return &_all_types[type];
     }
@@ -476,7 +468,7 @@ i64 VM::py_hash(PyObject* obj){
     }
     if(has_custom_eq){
         TypeError(fmt("unhashable type: ", ti->name.escape()));
-        return 0;
+        PK_UNREACHABLE();
     }else{
         return PK_BITS(obj);
     }
@@ -532,7 +524,7 @@ PyObject* VM::_format_string(Str spec, PyObject* obj){
         }
     }catch(...){
         ValueError("invalid format specifer");
-        UNREACHABLE();
+        PK_UNREACHABLE();
     }
 
     if(type != 'f' && dot >= 0) ValueError("precision not allowed in the format specifier");
@@ -816,7 +808,7 @@ void VM::_prepare_py_call(PyObject** buffer, ArgsView args, ArgsView kwargs, con
         vm->TypeError(fmt(
             co->name, "() takes ", decl_argc, " positional arguments but ", args.size(), " were given"
         ));
-        UNREACHABLE();
+        PK_UNREACHABLE();
     }
 
     int i = 0;
@@ -925,11 +917,11 @@ PyObject* VM::vectorcall(int ARGC, int KWARGC, bool op_call){
                 TypeError(fmt(
                     co->name, "() takes ", decl->args.size(), " positional arguments but ", args.size(), " were given"
                 ));
-                UNREACHABLE();
+                PK_UNREACHABLE();
             }
             if(!kwargs.empty()){
                 TypeError(fmt(co->name, "() takes no keyword arguments"));
-                UNREACHABLE();
+                PK_UNREACHABLE();
             }
             s_data.reset(_base + co_nlocals);
             int i = 0;

+ 4 - 0
tests/04_str.py

@@ -1,12 +1,16 @@
 assert 'testing' == 'test' + 'ing'
 assert 'testing' != 'test' + 'ing2'
 assert 'testing' < 'test' + 'ing2'
+assert 'testing' <= 'test' + 'ing2'
 assert 'testing5' > 'test' + 'ing1'
+assert 'testing5' >= 'test' + 'ing1'
 
 # test + *=
 assert 'abc' + 'def' == 'abcdef'
 assert 'abc' * 3 == 'abcabcabc'
 
+assert repr('\\\n\t\'\r\b\x48') == r"'\\\n\t\'\r\bH'"
+
 a = ''
 b = 'test'
 c ='test'

+ 13 - 1
tests/80_json.py

@@ -70,4 +70,16 @@ assert repr([1, 2, 3]) == '[1, 2, 3]'
 assert repr([1]) == '[1]'
 assert json.dumps([]) == '[]'
 assert json.dumps([1, 2, 3]) == '[1, 2, 3]'
-assert json.dumps([1]) == '[1]'
+assert json.dumps([1]) == '[1]'
+
+try:
+    json.dumps({1: 2})
+    assert False
+except TypeError:
+    assert True
+
+try:
+    json.dumps(type)
+    assert False
+except TypeError:
+    assert True