From 5a9448dca16b8055315172b29cf681da08aa8692 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Mon, 5 Dec 2022 02:11:23 +0800 Subject: [PATCH] up --- amalgamate.py | 2 +- src/pocketpy.h | 8 ++++---- src/safestl.h | 3 ++- src/shared_ptr.h | 15 ++++++++++++++- src/vm.h | 8 ++++---- 5 files changed, 25 insertions(+), 11 deletions(-) diff --git a/amalgamate.py b/amalgamate.py index a5a5907d..8fa9af6c 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", "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"] diff --git a/src/pocketpy.h b/src/pocketpy.h index d80d1177..61431bbd 100644 --- a/src/pocketpy.h +++ b/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])); \ }); diff --git a/src/safestl.h b/src/safestl.h index d193e29d..e3147671 100644 --- a/src/safestl.h +++ b/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 PyVar; +typedef pkpy::shared_ptr PyVar; typedef PyVar PyVarOrNull; class PyVarList: public std::vector { diff --git a/src/shared_ptr.h b/src/shared_ptr.h index cebc22a5..b9b43822 100644 --- a/src/shared_ptr.h +++ b/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 diff --git a/src/vm.h b/src/vm.h index fbf51ebb..d848d2f9 100644 --- a/src/vm.h +++ b/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((_Int)0); + PyVar obj = pkpy::make_shared((_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(_native); + PyVar obj = pkpy::make_shared(_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((_Int)0); - _tp_type = std::make_shared((_Int)0); + _tp_object = pkpy::make_shared((_Int)0); + _tp_type = pkpy::make_shared((_Int)0); _types["object"] = _tp_object; _types["type"] = _tp_type;