blueloveTH 3 лет назад
Родитель
Сommit
5a9448dca1
5 измененных файлов с 25 добавлено и 11 удалено
  1. 1 1
      amalgamate.py
  2. 4 4
      src/pocketpy.h
  3. 2 1
      src/safestl.h
  4. 14 1
      src/shared_ptr.h
  5. 4 4
      src/vm.h

+ 1 - 1
amalgamate.py

@@ -2,7 +2,7 @@ with open("src/opcodes.h", "rt", encoding='utf-8') as f:
 	OPCODES_TEXT = f.read()
 
 pipeline = [
-	["__stl__.h", "str.h", "safestl.h", "builtins.h", "error.h"],
+	["__stl__.h", "shared_ptr.h", "str.h", "safestl.h", "builtins.h", "error.h"],
 	["obj.h", "iter.h", "parser.h", "pointer.h", "codeobject.h"],
 	["vm.h", "compiler.h", "repl.h"],
 	["pocketpy.h"]

+ 4 - 4
src/pocketpy.h

@@ -15,11 +15,11 @@
         }                                                                                               \
     });
 
-#define BIND_NUM_LOGICAL_OPT(name, op, fallback)                                                        \
-    _vm->bindMethodMulti({"int","float"}, #name, [](VM* vm, const pkpy::ArgList& args){               \
+#define BIND_NUM_LOGICAL_OPT(name, op, is_eq)                                                           \
+    _vm->bindMethodMulti({"int","float"}, #name, [](VM* vm, const pkpy::ArgList& args){                 \
         if(!vm->isIntOrFloat(args[0], args[1])){                                                        \
-            if constexpr(fallback) return vm->PyBool(args[0] op args[1]);                               \
-            vm->typeError("unsupported operand type(s) for " #op );                        \
+            if constexpr(is_eq) return vm->PyBool(args[0] == args[1]);                                  \
+            vm->typeError("unsupported operand type(s) for " #op );                                     \
         }                                                                                               \
         return vm->PyBool(vm->numToFloat(args[0]) op vm->numToFloat(args[1]));                          \
     });

+ 2 - 1
src/safestl.h

@@ -1,10 +1,11 @@
 #pragma once
 
 #include "__stl__.h"
+#include "shared_ptr.h"
 #include "str.h"
 
 struct PyObject;
-typedef std::shared_ptr<PyObject> PyVar;
+typedef pkpy::shared_ptr<PyObject> PyVar;
 typedef PyVar PyVarOrNull;
 
 class PyVarList: public std::vector<PyVar> {

+ 14 - 1
src/shared_ptr.h

@@ -16,6 +16,13 @@ namespace pkpy{
     public:
         shared_ptr() : count(nullptr), ptr(nullptr) {}
         shared_ptr(T* ptr) : count(new int(1)), ptr(ptr) {}
+        shared_ptr(const shared_ptr& other) : count(other.count), ptr(other.ptr) {
+            if(count) (*count)++;
+        }
+        shared_ptr(shared_ptr&& other) : count(other.count), ptr(other.ptr) {
+            other.count = nullptr;
+            other.ptr = nullptr;
+        }
         ~shared_ptr() {
             if (count && --(*count) == 0) _delete();
         }
@@ -45,7 +52,7 @@ namespace pkpy{
             }
             return *this;
         }
-        
+
         shared_ptr& operator=(shared_ptr&& other) {
             if (this != &other) {
                 if (count && --(*count) == 0) _delete();
@@ -69,6 +76,12 @@ namespace pkpy{
         int use_count() const {
             return count ? *count : 0;
         }
+
+        void reset(){
+            if (count && --(*count) == 0) _delete();
+            count = nullptr;
+            ptr = nullptr;
+        }
     };
 
     template <typename T, typename... Args>

+ 4 - 4
src/vm.h

@@ -599,7 +599,7 @@ public:
 
     PyVar newClassType(_Str name, PyVar base=nullptr) {
         if(base == nullptr) base = _tp_object;
-        PyVar obj = std::make_shared<PyObject>((_Int)0);
+        PyVar obj = pkpy::make_shared<PyObject>((_Int)0);
         obj->setType(_tp_type);
         setAttr(obj, __base__, base);
         _types[name] = obj;
@@ -608,7 +608,7 @@ public:
 
     PyVar newObject(PyVar type, _Value _native) {
         __checkType(type, _tp_type);
-        PyVar obj = std::make_shared<PyObject>(_native);
+        PyVar obj = pkpy::make_shared<PyObject>(_native);
         obj->setType(type);
         return obj;
     }
@@ -794,8 +794,8 @@ public:
     inline const PyVar& PyBool(bool value){return value ? True : False;}
 
     void initializeBuiltinClasses(){
-        _tp_object = std::make_shared<PyObject>((_Int)0);
-        _tp_type = std::make_shared<PyObject>((_Int)0);
+        _tp_object = pkpy::make_shared<PyObject>((_Int)0);
+        _tp_type = pkpy::make_shared<PyObject>((_Int)0);
 
         _types["object"] = _tp_object;
         _types["type"] = _tp_type;