Parcourir la source

replace `PyObject*` with `PyVar`

blueloveTH il y a 1 an
Parent
commit
f5de8b12b8
3 fichiers modifiés avec 40 ajouts et 40 suppressions
  1. 9 9
      3rd/cjson/src/cJSONw.cpp
  2. 30 30
      3rd/lua_bridge/src/lua_bridge.cpp
  3. 1 1
      include/pocketpy/common.h

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

@@ -3,8 +3,8 @@
 namespace pkpy{
 namespace pkpy{
 
 
 
 
-static cJSON* convert_python_object_to_cjson(PyObject* obj, VM* vm);
-static PyObject* convert_cjson_to_python_object(const cJSON * const item, VM* vm);
+static cJSON* convert_python_object_to_cjson(PyVar obj, VM* vm);
+static PyVar convert_cjson_to_python_object(const cJSON * const item, VM* vm);
 
 
 template<typename T>
 template<typename T>
 static cJSON* convert_list_to_cjson(const T& list, VM* vm){
 static cJSON* convert_list_to_cjson(const T& list, VM* vm){
@@ -17,13 +17,13 @@ static cJSON* convert_list_to_cjson(const T& list, VM* vm){
 
 
 static cJSON* covert_dict_to_cjson(const Dict& dict, VM* vm){
 static cJSON* covert_dict_to_cjson(const Dict& dict, VM* vm){
     cJSON *cjson_object = cJSON_CreateObject();
     cJSON *cjson_object = cJSON_CreateObject();
-    dict.apply([&](PyObject* key, PyObject* val){
+    dict.apply([&](PyVar key, PyVar val){
         cJSON_AddItemToObject(cjson_object, CAST(Str&, key).c_str(), convert_python_object_to_cjson(val, vm));
         cJSON_AddItemToObject(cjson_object, CAST(Str&, key).c_str(), convert_python_object_to_cjson(val, vm));
     });
     });
     return cjson_object;
     return cjson_object;
 }
 }
 
 
-static cJSON* convert_python_object_to_cjson(PyObject* obj, VM* vm){
+static cJSON* convert_python_object_to_cjson(PyVar obj, VM* vm){
     if(obj == vm->None) return cJSON_CreateNull();
     if(obj == vm->None) return cJSON_CreateNull();
     Type obj_t = vm->_tp(obj);
     Type obj_t = vm->_tp(obj);
     switch(obj_t){
     switch(obj_t){
@@ -41,7 +41,7 @@ static cJSON* convert_python_object_to_cjson(PyObject* obj, VM* vm){
 }
 }
 
 
 
 
-static PyObject* convert_cjson_to_list(const cJSON * const item, VM* vm){
+static PyVar convert_cjson_to_list(const cJSON * const item, VM* vm){
     List output;
     List output;
     cJSON *element = item->child;
     cJSON *element = item->child;
     while(element != NULL){
     while(element != NULL){
@@ -51,7 +51,7 @@ static PyObject* convert_cjson_to_list(const cJSON * const item, VM* vm){
     return VAR(std::move(output));
     return VAR(std::move(output));
 }
 }
 
 
-static PyObject* convert_cjson_to_dict(const cJSON* const item, VM* vm){
+static PyVar convert_cjson_to_dict(const cJSON* const item, VM* vm){
     Dict output(vm);
     Dict output(vm);
     cJSON *child = item->child;
     cJSON *child = item->child;
     while(child != NULL){
     while(child != NULL){
@@ -63,7 +63,7 @@ static PyObject* convert_cjson_to_dict(const cJSON* const item, VM* vm){
     return VAR(std::move(output));
     return VAR(std::move(output));
 }
 }
 
 
-static PyObject* convert_cjson_to_python_object(const cJSON * const item, VM* vm)
+static PyVar convert_cjson_to_python_object(const cJSON * const item, VM* vm)
 {
 {
     if (cJSON_IsString(item))
     if (cJSON_IsString(item))
     {
     {
@@ -91,7 +91,7 @@ static PyObject* convert_cjson_to_python_object(const cJSON * const item, VM* vm
 }
 }
 
 
 void add_module_cjson(VM* vm){
 void add_module_cjson(VM* vm){
-    PyObject* mod = vm->new_module("cjson");
+    PyVar mod = vm->new_module("cjson");
 
 
     PK_LOCAL_STATIC cJSON_Hooks hooks;
     PK_LOCAL_STATIC cJSON_Hooks hooks;
     hooks.malloc_fn = pool64_alloc;
     hooks.malloc_fn = pool64_alloc;
@@ -112,7 +112,7 @@ void add_module_cjson(VM* vm){
             while(*end != '\0' && *end != '\n') end++;
             while(*end != '\0' && *end != '\n') end++;
             vm->IOError(_S("cjson: ", std::string_view(start, end-start)));
             vm->IOError(_S("cjson: ", std::string_view(start, end-start)));
         }
         }
-        PyObject* output = convert_cjson_to_python_object(json, vm);
+        PyVar output = convert_cjson_to_python_object(json, vm);
         cJSON_Delete(json);
         cJSON_Delete(json);
         return output;
         return output;
     });
     });

+ 30 - 30
3rd/lua_bridge/src/lua_bridge.cpp

@@ -3,8 +3,8 @@
 namespace pkpy{
 namespace pkpy{
 
 
 static lua_State* _L;
 static lua_State* _L;
-static void lua_push_from_python(VM*, PyObject*);
-static PyObject* lua_popx_to_python(VM*);
+static void lua_push_from_python(VM*, PyVar);
+static PyVar lua_popx_to_python(VM*);
 
 
 template<typename T>
 template<typename T>
 static void table_apply(VM* vm, T f){
 static void table_apply(VM* vm, T f){
@@ -12,8 +12,8 @@ static void table_apply(VM* vm, T f){
     lua_pushnil(_L);                 // [key]
     lua_pushnil(_L);                 // [key]
     while(lua_next(_L, -2) != 0){    // [key, val]
     while(lua_next(_L, -2) != 0){    // [key, val]
         lua_pushvalue(_L, -2);       // [key, val, key]
         lua_pushvalue(_L, -2);       // [key, val, key]
-        PyObject* key = lua_popx_to_python(vm);
-        PyObject* val = lua_popx_to_python(vm);
+        PyVar key = lua_popx_to_python(vm);
+        PyVar val = lua_popx_to_python(vm);
         f(key, val);                // [key]
         f(key, val);                // [key]
     }
     }
     lua_pop(_L, 1);                  // []
     lua_pop(_L, 1);                  // []
@@ -38,24 +38,24 @@ struct PyLuaObject{
 };
 };
 
 
 struct PyLuaTable: PyLuaObject{
 struct PyLuaTable: PyLuaObject{
-    static void _register(VM* vm, PyObject* mod, PyObject* type){
+    static void _register(VM* vm, PyVar mod, PyVar type){
         Type t = PK_OBJ_GET(Type, type);
         Type t = PK_OBJ_GET(Type, type);
         PyTypeInfo* ti = &vm->_all_types[t];
         PyTypeInfo* ti = &vm->_all_types[t];
         ti->subclass_enabled = false;
         ti->subclass_enabled = false;
-        ti->m__getattr__ = [](VM* vm, PyObject* obj, StrName name){
+        ti->m__getattr__ = [](VM* vm, PyVar obj, StrName name){
             const PyLuaTable& self = _CAST(PyLuaTable&, obj);
             const PyLuaTable& self = _CAST(PyLuaTable&, obj);
             LUA_PROTECTED(
             LUA_PROTECTED(
                 lua_rawgeti(_L, LUA_REGISTRYINDEX, self.r);
                 lua_rawgeti(_L, LUA_REGISTRYINDEX, self.r);
                 std::string_view name_sv = name.sv();
                 std::string_view name_sv = name.sv();
                 lua_pushlstring(_L, name_sv.data(), name_sv.size());
                 lua_pushlstring(_L, name_sv.data(), name_sv.size());
                 lua_gettable(_L, -2);
                 lua_gettable(_L, -2);
-                PyObject* ret = lua_popx_to_python(vm);
+                PyVar ret = lua_popx_to_python(vm);
                 lua_pop(_L, 1);
                 lua_pop(_L, 1);
                 return ret;
                 return ret;
             )
             )
         };
         };
 
 
-        ti->m__setattr__ = [](VM* vm, PyObject* obj, StrName name, PyObject* val){
+        ti->m__setattr__ = [](VM* vm, PyVar obj, StrName name, PyVar val){
             const PyLuaTable& self = _CAST(PyLuaTable&, obj);
             const PyLuaTable& self = _CAST(PyLuaTable&, obj);
             LUA_PROTECTED(
             LUA_PROTECTED(
                 lua_rawgeti(_L, LUA_REGISTRYINDEX, self.r);
                 lua_rawgeti(_L, LUA_REGISTRYINDEX, self.r);
@@ -67,7 +67,7 @@ struct PyLuaTable: PyLuaObject{
             )
             )
         };
         };
 
 
-        ti->m__delattr__ = [](VM* vm, PyObject* obj, StrName name){
+        ti->m__delattr__ = [](VM* vm, PyVar obj, StrName name){
             const PyLuaTable& self = _CAST(PyLuaTable&, obj);
             const PyLuaTable& self = _CAST(PyLuaTable&, obj);
             LUA_PROTECTED(
             LUA_PROTECTED(
                 lua_rawgeti(_L, LUA_REGISTRYINDEX, self.r);
                 lua_rawgeti(_L, LUA_REGISTRYINDEX, self.r);
@@ -82,11 +82,11 @@ struct PyLuaTable: PyLuaObject{
 
 
         vm->bind_func(type, __new__, 1, [](VM* vm, ArgsView args){
         vm->bind_func(type, __new__, 1, [](VM* vm, ArgsView args){
             lua_newtable(_L);    // push an empty table onto the stack
             lua_newtable(_L);    // push an empty table onto the stack
-            PyObject* obj = vm->heap.gcnew<PyLuaTable>(PK_OBJ_GET(Type, args[0]));
+            PyVar obj = vm->heap.gcnew<PyLuaTable>(PK_OBJ_GET(Type, args[0]));
             return obj;
             return obj;
         });
         });
 
 
-        vm->bind__len__(t, [](VM* vm, PyObject* obj){
+        vm->bind__len__(t, [](VM* vm, PyVar obj){
             const PyLuaTable& self = _CAST(PyLuaTable&, obj);
             const PyLuaTable& self = _CAST(PyLuaTable&, obj);
             lua_rawgeti(_L, LUA_REGISTRYINDEX, self.r);
             lua_rawgeti(_L, LUA_REGISTRYINDEX, self.r);
             i64 len = 0;
             i64 len = 0;
@@ -96,19 +96,19 @@ struct PyLuaTable: PyLuaObject{
             return len;
             return len;
         });
         });
 
 
-        vm->bind__getitem__(t, [](VM* vm, PyObject* obj, PyObject* key){
+        vm->bind__getitem__(t, [](VM* vm, PyVar obj, PyVar key){
             const PyLuaTable& self = _CAST(PyLuaTable&, obj);
             const PyLuaTable& self = _CAST(PyLuaTable&, obj);
             LUA_PROTECTED(
             LUA_PROTECTED(
                 lua_rawgeti(_L, LUA_REGISTRYINDEX, self.r);
                 lua_rawgeti(_L, LUA_REGISTRYINDEX, self.r);
                 lua_push_from_python(vm, key);
                 lua_push_from_python(vm, key);
                 lua_gettable(_L, -2);
                 lua_gettable(_L, -2);
-                PyObject* ret = lua_popx_to_python(vm);
+                PyVar ret = lua_popx_to_python(vm);
                 lua_pop(_L, 1);
                 lua_pop(_L, 1);
                 return ret;
                 return ret;
             )
             )
         });
         });
 
 
-        vm->bind__setitem__(t, [](VM* vm, PyObject* obj, PyObject* key, PyObject* val){
+        vm->bind__setitem__(t, [](VM* vm, PyVar obj, PyVar key, PyVar val){
             const PyLuaTable& self = _CAST(PyLuaTable&, obj);
             const PyLuaTable& self = _CAST(PyLuaTable&, obj);
             LUA_PROTECTED(
             LUA_PROTECTED(
                 lua_rawgeti(_L, LUA_REGISTRYINDEX, self.r);
                 lua_rawgeti(_L, LUA_REGISTRYINDEX, self.r);
@@ -119,7 +119,7 @@ struct PyLuaTable: PyLuaObject{
             )
             )
         });
         });
 
 
-        vm->bind__delitem__(t, [](VM* vm, PyObject* obj, PyObject* key){
+        vm->bind__delitem__(t, [](VM* vm, PyVar obj, PyVar key){
             const PyLuaTable& self = _CAST(PyLuaTable&, obj);
             const PyLuaTable& self = _CAST(PyLuaTable&, obj);
             LUA_PROTECTED(
             LUA_PROTECTED(
                 lua_rawgeti(_L, LUA_REGISTRYINDEX, self.r);
                 lua_rawgeti(_L, LUA_REGISTRYINDEX, self.r);
@@ -130,7 +130,7 @@ struct PyLuaTable: PyLuaObject{
             )
             )
         });
         });
 
 
-        vm->bind__contains__(t, [](VM* vm, PyObject* obj, PyObject* key){
+        vm->bind__contains__(t, [](VM* vm, PyVar obj, PyVar key){
             const PyLuaTable& self = _CAST(PyLuaTable&, obj);
             const PyLuaTable& self = _CAST(PyLuaTable&, obj);
             LUA_PROTECTED(
             LUA_PROTECTED(
                 lua_rawgeti(_L, LUA_REGISTRYINDEX, self.r);
                 lua_rawgeti(_L, LUA_REGISTRYINDEX, self.r);
@@ -147,7 +147,7 @@ struct PyLuaTable: PyLuaObject{
             LUA_PROTECTED(
             LUA_PROTECTED(
                 lua_rawgeti(_L, LUA_REGISTRYINDEX, self.r);
                 lua_rawgeti(_L, LUA_REGISTRYINDEX, self.r);
                 List ret;
                 List ret;
-                table_apply(vm, [&](PyObject* key, PyObject* val){ ret.push_back(key); });
+                table_apply(vm, [&](PyVar key, PyVar val){ ret.push_back(key); });
                 lua_pop(_L, 1);
                 lua_pop(_L, 1);
                 return VAR(std::move(ret));
                 return VAR(std::move(ret));
             )
             )
@@ -158,7 +158,7 @@ struct PyLuaTable: PyLuaObject{
             LUA_PROTECTED(
             LUA_PROTECTED(
                 lua_rawgeti(_L, LUA_REGISTRYINDEX, self.r);
                 lua_rawgeti(_L, LUA_REGISTRYINDEX, self.r);
                 List ret;
                 List ret;
-                table_apply(vm, [&](PyObject* key, PyObject* val){ ret.push_back(val); });
+                table_apply(vm, [&](PyVar key, PyVar val){ ret.push_back(val); });
                 lua_pop(_L, 1);
                 lua_pop(_L, 1);
                 return VAR(std::move(ret));
                 return VAR(std::move(ret));
             )
             )
@@ -169,8 +169,8 @@ struct PyLuaTable: PyLuaObject{
             LUA_PROTECTED(
             LUA_PROTECTED(
                 lua_rawgeti(_L, LUA_REGISTRYINDEX, self.r);
                 lua_rawgeti(_L, LUA_REGISTRYINDEX, self.r);
                 List ret;
                 List ret;
-                table_apply(vm, [&](PyObject* key, PyObject* val){
-                    PyObject* item = VAR(Tuple(key, val));
+                table_apply(vm, [&](PyVar key, PyVar val){
+                    PyVar item = VAR(Tuple(key, val));
                     ret.push_back(item);
                     ret.push_back(item);
                 });
                 });
                 lua_pop(_L, 1);
                 lua_pop(_L, 1);
@@ -180,7 +180,7 @@ struct PyLuaTable: PyLuaObject{
     }
     }
 };
 };
 
 
-static PyObject* lua_popx_multi_to_python(VM* vm, int count){
+static PyVar lua_popx_multi_to_python(VM* vm, int count){
     if(count == 0){
     if(count == 0){
         return vm->None;
         return vm->None;
     }else if(count == 1){
     }else if(count == 1){
@@ -196,7 +196,7 @@ static PyObject* lua_popx_multi_to_python(VM* vm, int count){
 }
 }
 
 
 struct PyLuaFunction: PyLuaObject{
 struct PyLuaFunction: PyLuaObject{
-    static void _register(VM* vm, PyObject* mod, PyObject* type){
+    static void _register(VM* vm, PyVar mod, PyVar type){
         vm->bind_func(type, __call__, -1, [](VM* vm, ArgsView args){
         vm->bind_func(type, __call__, -1, [](VM* vm, ArgsView args){
             if(args.size() < 1) vm->TypeError("__call__ takes at least 1 argument");
             if(args.size() < 1) vm->TypeError("__call__ takes at least 1 argument");
             const PyLuaFunction& self = _CAST(PyLuaFunction&, args[0]);
             const PyLuaFunction& self = _CAST(PyLuaFunction&, args[0]);
@@ -217,7 +217,7 @@ struct PyLuaFunction: PyLuaObject{
     }
     }
 };
 };
 
 
-void lua_push_from_python(VM* vm, PyObject* val){
+void lua_push_from_python(VM* vm, PyVar val){
     if(val == vm->None){
     if(val == vm->None){
         lua_pushnil(_L);
         lua_pushnil(_L);
         return;
         return;
@@ -241,7 +241,7 @@ void lua_push_from_python(VM* vm, PyObject* val){
         case VM::tp_tuple.index: {
         case VM::tp_tuple.index: {
             lua_newtable(_L);
             lua_newtable(_L);
             int i = 1;
             int i = 1;
-            for(PyObject* obj: PK_OBJ_GET(Tuple, val)){
+            for(PyVar obj: PK_OBJ_GET(Tuple, val)){
                 lua_push_from_python(vm, obj);
                 lua_push_from_python(vm, obj);
                 lua_rawseti(_L, -2, i++);
                 lua_rawseti(_L, -2, i++);
             }
             }
@@ -250,7 +250,7 @@ void lua_push_from_python(VM* vm, PyObject* val){
         case VM::tp_list.index: {
         case VM::tp_list.index: {
             lua_newtable(_L);
             lua_newtable(_L);
             int i = 1;
             int i = 1;
-            for(PyObject* obj: PK_OBJ_GET(List, val)){
+            for(PyVar obj: PK_OBJ_GET(List, val)){
                 lua_push_from_python(vm, obj);
                 lua_push_from_python(vm, obj);
                 lua_rawseti(_L, -2, i++);
                 lua_rawseti(_L, -2, i++);
             }
             }
@@ -258,7 +258,7 @@ void lua_push_from_python(VM* vm, PyObject* val){
         }
         }
         case VM::tp_dict.index: {
         case VM::tp_dict.index: {
             lua_newtable(_L);
             lua_newtable(_L);
-            PK_OBJ_GET(Dict, val).apply([&](PyObject* key, PyObject* val){
+            PK_OBJ_GET(Dict, val).apply([&](PyVar key, PyVar val){
                 lua_push_from_python(vm, key);
                 lua_push_from_python(vm, key);
                 lua_push_from_python(vm, val);
                 lua_push_from_python(vm, val);
                 lua_settable(_L, -3);
                 lua_settable(_L, -3);
@@ -281,7 +281,7 @@ void lua_push_from_python(VM* vm, PyObject* val){
     vm->RuntimeError(_S("unsupported python type: ", _type_name(vm, t).escape()));
     vm->RuntimeError(_S("unsupported python type: ", _type_name(vm, t).escape()));
 }
 }
 
 
-PyObject* lua_popx_to_python(VM* vm) {
+PyVar lua_popx_to_python(VM* vm) {
     int type = lua_type(_L, -1);
     int type = lua_type(_L, -1);
     switch (type) {
     switch (type) {
         case LUA_TNIL: {
         case LUA_TNIL: {
@@ -304,11 +304,11 @@ PyObject* lua_popx_to_python(VM* vm) {
             return VAR(val);
             return VAR(val);
         }
         }
         case LUA_TTABLE: {
         case LUA_TTABLE: {
-            PyObject* obj = vm->new_user_object<PyLuaTable>();
+            PyVar obj = vm->new_user_object<PyLuaTable>();
             return obj;
             return obj;
         }
         }
         case LUA_TFUNCTION: {
         case LUA_TFUNCTION: {
-            PyObject* obj = vm->new_user_object<PyLuaFunction>();
+            PyVar obj = vm->new_user_object<PyLuaFunction>();
             return obj;
             return obj;
         }
         }
         default: {
         default: {
@@ -321,7 +321,7 @@ PyObject* lua_popx_to_python(VM* vm) {
 }
 }
 
 
 void initialize_lua_bridge(VM* vm, lua_State* newL){
 void initialize_lua_bridge(VM* vm, lua_State* newL){
-    PyObject* mod = vm->new_module("lua");
+    PyVar mod = vm->new_module("lua");
 
 
     if(_L != nullptr){
     if(_L != nullptr){
         throw std::runtime_error("lua bridge already initialized");
         throw std::runtime_error("lua bridge already initialized");

+ 1 - 1
include/pocketpy/common.h

@@ -126,7 +126,7 @@ struct Type {
 #endif
 #endif
 
 
 struct PyObject;
 struct PyObject;
-using PyVar = PyObject*;
+using PyVar = PyObject *;
 #define PK_BITS(p) (reinterpret_cast<i64>(p))
 #define PK_BITS(p) (reinterpret_cast<i64>(p))
 
 
 // is_pod_v<> for c++17 and c++20
 // is_pod_v<> for c++17 and c++20