clean up null object

This commit is contained in:
IlyaShurupov 2024-03-25 11:36:29 +03:00
parent 55daf0f303
commit acaa086943
6 changed files with 30 additions and 50 deletions

View file

@ -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<NullObject>();
}
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<const char*> 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); }
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;
}

View file

@ -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"

View file

@ -49,7 +49,6 @@ static bool init(const ModuleManifest*) {
}
static void deinit(const ModuleManifest*) {
NullObject::uninit();
MethodObject::UnInitialize();
objects_api::finalize();

View file

@ -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",

View file

@ -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();
};
}

View file

@ -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<NullObject>();
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()
}