From acaa0869435300d444ffe63005520c8b7267eff9 Mon Sep 17 00:00:00 2001 From: IlyaShurupov Date: Mon, 25 Mar 2024 11:36:29 +0300 Subject: [PATCH] clean up null object --- Objects/private/core/Object.cpp | 26 ++++++++++++++++------- Objects/private/core/ObjectSave.cpp | 2 +- Objects/private/core/ObjectsModule.cpp | 1 - Objects/private/primitives/NullObject.cpp | 21 +----------------- Objects/public/core/Object.hpp | 8 +++++++ Objects/public/primitives/NullObject.hpp | 22 ++----------------- 6 files changed, 30 insertions(+), 50 deletions(-) diff --git a/Objects/private/core/Object.cpp b/Objects/private/core/Object.cpp index 1d94786..126b89f 100644 --- a/Objects/private/core/Object.cpp +++ b/Objects/private/core/Object.cpp @@ -1,6 +1,7 @@ #include "core/Object.hpp" +#include "primitives/NullObject.hpp" #include "primitives/ClassObject.hpp" #include "primitives/MethodObject.hpp" @@ -11,9 +12,18 @@ using namespace obj; ObjectsContext* obj::gObjectsContext = nullptr; +ObjectsContext::ObjectsContext() { + memSetVal(sl_callbacks, SAVE_LOAD_MAX_CALLBACK_SLOTS * sizeof(save_load_callbacks*), 0); + interp = new Interpreter(); +} + +ObjectsContext::~ObjectsContext() { delete interp; } + void objects_api::initialize() { ASSERT(!gObjectsContext) gObjectsContext = new ObjectsContext(); + + gObjectsContext->nullObject = create(); } void objects_api::finalize() { @@ -44,13 +54,6 @@ void obj::load_string(ArchiverIn& file, std::string& out) { Object* ObjectMemAllocate(const ObjectType* type); void ObjectMemDeallocate(Object* object); -ObjectsContext::ObjectsContext() { - memSetVal(sl_callbacks, SAVE_LOAD_MAX_CALLBACK_SLOTS * sizeof(save_load_callbacks*), 0); - interp = new Interpreter(); -} - -ObjectsContext::~ObjectsContext() { delete interp; } - void hierarchy_copy(Object* self, const Object* in, const ObjectType* type); void hierarchy_construct(Object* self, const ObjectType* type); @@ -226,4 +229,11 @@ void objects_api::addTypeToGroup(ObjectType* type, InitialierList p bool objects_api::isType(const char* name) { return gObjectsContext->types.presents(name).isValid(); } -const ObjectType* objects_api::getType(const char* name) { return gObjectsContext->types.get(name); } \ No newline at end of file +const ObjectType* objects_api::getType(const char* name) { return gObjectsContext->types.get(name); } + +NullObject* objects_api::getNull() { return gObjectsContext->nullObject; } + +NullObject* objects_api::getNullReferenced() { + increaseReferenceCount(gObjectsContext->nullObject); + return gObjectsContext->nullObject; +} \ No newline at end of file diff --git a/Objects/private/core/ObjectSave.cpp b/Objects/private/core/ObjectSave.cpp index d456452..32ba70e 100644 --- a/Objects/private/core/ObjectSave.cpp +++ b/Objects/private/core/ObjectSave.cpp @@ -296,7 +296,7 @@ Object* objects_api::load(ArchiverIn& ndf, alni file_adress) { // check for null object if (out->type == &NullObject::TypeData) { ObjectMemDeallocate(out); - out = NdoNull_globalInstance; + out = getNull(); } // save heap adress in "loaded_file" diff --git a/Objects/private/core/ObjectsModule.cpp b/Objects/private/core/ObjectsModule.cpp index a93b233..2bb1bf0 100644 --- a/Objects/private/core/ObjectsModule.cpp +++ b/Objects/private/core/ObjectsModule.cpp @@ -49,7 +49,6 @@ static bool init(const ModuleManifest*) { } static void deinit(const ModuleManifest*) { - NullObject::uninit(); MethodObject::UnInitialize(); objects_api::finalize(); diff --git a/Objects/private/primitives/NullObject.cpp b/Objects/private/primitives/NullObject.cpp index 64a92f9..75a4294 100644 --- a/Objects/private/primitives/NullObject.cpp +++ b/Objects/private/primitives/NullObject.cpp @@ -5,31 +5,12 @@ using namespace tp; using namespace obj; -obj::NullObject* obj::NdoNull_globalInstance = nullptr; -bool uninit_flag = false; - -void NullObject::uninit() { - uninit_flag = true; - objects_api::destroy(NdoNull_globalInstance); - NdoNull_globalInstance = nullptr; -} - -void NullObject::destructor(Object* self) { - DEBUG_ASSERT(uninit_flag && "Only one the instance of NullObject exists and thus it can't be destroyed"); -} - std::string to_string(NullObject* self) { return "nullptr"; } alni to_int(NullObject* self) { return 0; } alnf to_float(NullObject* self) { return 0; } -obj::TypeMethods* tm_construct() { - auto out = new obj::TypeMethods(); - - return out; -} - struct ObjectTypeConversions NullObjectTypeConversions = { .from_int = nullptr, .from_float = nullptr, @@ -42,7 +23,7 @@ struct ObjectTypeConversions NullObjectTypeConversions = { struct ObjectType NullObject::TypeData = { .base = nullptr, .constructor = nullptr, - .destructor = NullObject::destructor, + .destructor = nullptr, .copy = nullptr, .size = sizeof(NullObject), .name = "null", diff --git a/Objects/public/core/Object.hpp b/Objects/public/core/Object.hpp index 5590c78..55ef6c9 100644 --- a/Objects/public/core/Object.hpp +++ b/Objects/public/core/Object.hpp @@ -34,6 +34,11 @@ namespace tp::obj { Interpreter* interp = nullptr; save_load_callbacks* sl_callbacks[SAVE_LOAD_MAX_CALLBACK_SLOTS]{}; alni sl_callbacks_load_idx = 0; + + struct NullObject* nullObject = nullptr; + + struct BoolObject* boolTrueObject = nullptr; + struct BoolObject* boolFalseObject = nullptr; }; struct objects_api { @@ -105,5 +110,8 @@ namespace tp::obj { static bool isType(const char* name); static const ObjectType* getType(const char* name); + + static NullObject* getNull(); + static NullObject* getNullReferenced(); }; } \ No newline at end of file diff --git a/Objects/public/primitives/NullObject.hpp b/Objects/public/primitives/NullObject.hpp index 60fb423..05c6a05 100644 --- a/Objects/public/primitives/NullObject.hpp +++ b/Objects/public/primitives/NullObject.hpp @@ -5,29 +5,11 @@ namespace tp::obj { - extern struct NullObject* NdoNull_globalInstance; - struct NullObject : Object { static ObjectType TypeData; - - static void destructor(Object* self); - - static void uninit(); - static Object* null_return() { - if (!NdoNull_globalInstance) { - NdoNull_globalInstance = objects_api::create(); - objects_api::increaseReferenceCount(NdoNull_globalInstance); - } - return (Object*) NdoNull_globalInstance; - } - - static Object* null_return_ref() { - objects_api::increaseReferenceCount(null_return()); - return NdoNull_globalInstance; - } }; -#define NDO_NULL NullObject::null_return() -#define NDO_NULL_REF NullObject::null_return_ref() +#define NDO_NULL objects_api::getNull() +#define NDO_NULL_REF objects_api::getNullReferenced() } \ No newline at end of file