From 3b30747fcde4d793cc54c9d2a47ad76f9856f25b Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Mon, 5 Dec 2022 17:33:21 +0800 Subject: [PATCH] up --- amalgamate.py | 2 +- src/__stl__.h | 1 - src/compiler.h | 4 +-- src/{shared_ptr.h => memory.h} | 64 ++++++++++++++++++++++++++++++++++ src/obj.h | 17 +-------- src/safestl.h | 2 +- src/str.h | 2 +- src/vm.h | 6 ++-- 8 files changed, 73 insertions(+), 25 deletions(-) rename src/{shared_ptr.h => memory.h} (60%) diff --git a/amalgamate.py b/amalgamate.py index e9e551cb..73bda214 100644 --- a/amalgamate.py +++ b/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"] diff --git a/src/__stl__.h b/src/__stl__.h index 9b559b1d..5516d2ae 100644 --- a/src/__stl__.h +++ b/src/__stl__.h @@ -8,7 +8,6 @@ #include #include -#include #include #include #include diff --git a/src/compiler.h b/src/compiler.h index fe2418b0..2aa71056 100644 --- a/src/compiler.h +++ b/src/compiler.h @@ -23,7 +23,7 @@ struct Loop { class Compiler { public: - std::unique_ptr parser; + pkpy::unique_ptr parser; std::stack<_Code> codes; std::stack 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( + this->parser = pkpy::make_unique( pkpy::make_shared(source, filename, mode) ); diff --git a/src/shared_ptr.h b/src/memory.h similarity index 60% rename from src/shared_ptr.h rename to src/memory.h index b9b43822..0549b6cf 100644 --- a/src/shared_ptr.h +++ b/src/memory.h @@ -88,4 +88,68 @@ namespace pkpy{ shared_ptr make_shared(Args&&... args) { return shared_ptr(new T(std::forward(args)...)); } + + + template + 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 + unique_ptr make_unique(Args&&... args) { + return unique_ptr(new T(std::forward(args)...)); + } }; \ No newline at end of file diff --git a/src/obj.h b/src/obj.h index 053b7687..28b12fa5 100644 --- a/src/obj.h +++ b/src/obj.h @@ -69,7 +69,6 @@ typedef std::variant _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); - } }; \ No newline at end of file diff --git a/src/safestl.h b/src/safestl.h index 9e15ccad..4bdf70ed 100644 --- a/src/safestl.h +++ b/src/safestl.h @@ -1,7 +1,7 @@ #pragma once #include "__stl__.h" -#include "shared_ptr.h" +#include "memory.h" #include "str.h" struct PyObject; diff --git a/src/str.h b/src/str.h index 274315b5..6d78f28f 100644 --- a/src/str.h +++ b/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); diff --git a/src/vm.h b/src/vm.h index 114fdc88..104c076a 100644 --- a/src/vm.h +++ b/src/vm.h @@ -25,7 +25,7 @@ class VM { std::atomic _stopFlag = false; std::vector _smallIntegers; // [-5, 256] protected: - std::deque< std::unique_ptr > callstack; + std::deque< pkpy::unique_ptr > 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)); + callstack.emplace_back(pkpy::unique_ptr(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(_native); obj->setType(type);