blueloveTH 3 ani în urmă
părinte
comite
3b30747fcd
8 a modificat fișierele cu 73 adăugiri și 25 ștergeri
  1. 1 1
      amalgamate.py
  2. 0 1
      src/__stl__.h
  3. 2 2
      src/compiler.h
  4. 64 0
      src/memory.h
  5. 1 16
      src/obj.h
  6. 1 1
      src/safestl.h
  7. 1 1
      src/str.h
  8. 3 3
      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", "shared_ptr.h", "str.h", "hash_table8.hpp", "safestl.h", "builtins.h", "error.h"],
+	["__stl__.h", "memory.h", "str.h", "hash_table8.hpp", "safestl.h", "builtins.h", "error.h"],
 	["obj.h", "iter.h", "parser.h", "pointer.h", "codeobject.h"],
 	["vm.h", "compiler.h", "repl.h"],
 	["pocketpy.h"]

+ 0 - 1
src/__stl__.h

@@ -8,7 +8,6 @@
 
 #include <sstream>
 #include <regex>
-#include <memory>
 #include <variant>
 #include <stack>
 #include <cmath>

+ 2 - 2
src/compiler.h

@@ -23,7 +23,7 @@ struct Loop {
 
 class Compiler {
 public:
-    std::unique_ptr<Parser> parser;
+    pkpy::unique_ptr<Parser> parser;
     std::stack<_Code> codes;
     std::stack<Loop> loops;
     bool isCompilingClass = false;
@@ -45,7 +45,7 @@ public:
 
     Compiler(VM* vm, const char* source, _Str filename, CompileMode mode){
         this->vm = vm;
-        this->parser = std::make_unique<Parser>(
+        this->parser = pkpy::make_unique<Parser>(
             pkpy::make_shared<SourceMetadata>(source, filename, mode)
         );
 

+ 64 - 0
src/shared_ptr.h → src/memory.h

@@ -88,4 +88,68 @@ namespace pkpy{
     shared_ptr<T> make_shared(Args&&... args) {
         return shared_ptr<T>(new T(std::forward<Args>(args)...));
     }
+
+
+    template <typename T>
+    class unique_ptr {
+        T* ptr;
+
+    public:
+        unique_ptr() : ptr(nullptr) {}
+        unique_ptr(T* ptr) : ptr(ptr) {}
+        unique_ptr(const unique_ptr& other) = delete;
+        unique_ptr(unique_ptr&& other) : ptr(other.ptr) {
+            other.ptr = nullptr;
+        }
+        ~unique_ptr() {
+            delete ptr;
+        }
+
+        bool operator==(const unique_ptr& other) const {
+            return ptr == other.ptr;
+        }
+
+        bool operator!=(const unique_ptr& other) const {
+            return ptr != other.ptr;
+        }
+
+        bool operator==(std::nullptr_t) const {
+            return ptr == nullptr;
+        }
+
+        bool operator!=(std::nullptr_t) const {
+            return ptr != nullptr;
+        }
+
+        unique_ptr& operator=(const unique_ptr& other) = delete;
+
+        unique_ptr& operator=(unique_ptr&& other) {
+            if (this != &other) {
+                delete ptr;
+                ptr = other.ptr;
+                other.ptr = nullptr;
+            }
+            return *this;
+        }
+
+        T& operator*() const {
+            return *ptr;
+        }
+        T* operator->() const {
+            return ptr;
+        }
+        T* get() const {
+            return ptr;
+        }
+
+        void reset(){
+            delete ptr;
+            ptr = nullptr;
+        }
+    };
+
+    template <typename T, typename... Args>
+    unique_ptr<T> make_unique(Args&&... args) {
+        return unique_ptr<T>(new T(std::forward<Args>(args)...));
+    }
 };

+ 1 - 16
src/obj.h

@@ -69,7 +69,6 @@ typedef std::variant<PyVar,_Int,_Float,bool,_Str,PyVarList,_CppFunc,_Func,pkpy::
 
 const int VALUE_SIZE = sizeof(_Value);
 
-static std::vector<void*> _objPool;
 
 struct PyObject {
     PyVarDict attribs;
@@ -82,7 +81,7 @@ struct PyObject {
 
     inline void setType(const PyVar& type){
         this->_type = type;
-        this->attribs[__class__] = type;
+        // this->attribs[__class__] = type;
     }
 
     // currently __name__ is only used for 'type'
@@ -97,18 +96,4 @@ struct PyObject {
 
     PyObject(const _Value& val): _native(val) {}
     PyObject(_Value&& val): _native(std::move(val)) {}
-
-    void* operator new(size_t size){
-        if(_objPool.empty()){
-            return ::operator new(size);
-        }else{
-            void* ptr = _objPool.back();
-            _objPool.pop_back();
-            return ptr;
-        }
-    }
-
-    void operator delete(void* ptr){
-        _objPool.push_back(ptr);
-    }
 };

+ 1 - 1
src/safestl.h

@@ -1,7 +1,7 @@
 #pragma once
 
 #include "__stl__.h"
-#include "shared_ptr.h"
+#include "memory.h"
 #include "str.h"
 
 struct PyObject;

+ 1 - 1
src/str.h

@@ -243,7 +243,7 @@ namespace std {
     };
 }
 
-const _Str& __class__ = _Str("__class__"_c);
+// const _Str& __class__ = _Str("__class__"_c);
 const _Str& __base__ = _Str("__base__"_c);
 const _Str& __new__ = _Str("__new__"_c);
 const _Str& __iter__ = _Str("__iter__"_c);

+ 3 - 3
src/vm.h

@@ -25,7 +25,7 @@ class VM {
     std::atomic<bool> _stopFlag = false;
     std::vector<PyVar> _smallIntegers;      // [-5, 256]
 protected:
-    std::deque< std::unique_ptr<Frame> > callstack;
+    std::deque< pkpy::unique_ptr<Frame> > callstack;
     PyVarDict _modules;                     // loaded modules
     std::map<_Str, _Code> _lazyModules;     // lazy loaded modules
     PyVar __py2py_call_signal;
@@ -563,7 +563,7 @@ public:
             throw RuntimeError("RecursionError", "maximum recursion depth exceeded", _cleanErrorAndGetSnapshots());
         }
         Frame* frame = new Frame(code.get(), _module, locals);
-        callstack.emplace_back(std::unique_ptr<Frame>(frame));
+        callstack.emplace_back(pkpy::unique_ptr<Frame>(frame));
         return frame;
     }
 
@@ -608,7 +608,7 @@ public:
         return obj;
     }
 
-    PyVar newObject(PyVar type, _Value _native) {
+    PyVar newObject(PyVar type, const _Value& _native) {
         __checkType(type, _tp_type);
         PyVar obj = pkpy::make_shared<PyObject>(_native);
         obj->setType(type);