From 24f9d8acae6089618969efb0d112e4d5584e83aa Mon Sep 17 00:00:00 2001 From: IlyaShurupov Date: Mon, 25 Mar 2024 10:37:31 +0300 Subject: [PATCH] Separate objects context and interface --- Objects/applications/GUI.cpp | 38 ++--- Objects/private/compiler/Constants.cpp | 12 +- Objects/private/core/Object.cpp | 47 ++++-- Objects/private/core/ObjectSave.cpp | 36 +++-- Objects/private/core/ObjectsModule.cpp | 80 ++++----- Objects/private/core/ScriptSection.cpp | 20 ++- Objects/private/core/TypeMethods.cpp | 6 +- Objects/private/interpreter/CallStack.cpp | 4 +- Objects/private/interpreter/Interpreter.cpp | 62 +++---- Objects/private/interpreter/ScopeStack.cpp | 14 +- Objects/private/primitives/ClassObject.cpp | 8 +- Objects/private/primitives/DictObject.cpp | 14 +- Objects/private/primitives/LinkObject.cpp | 14 +- Objects/private/primitives/ListObject.cpp | 16 +- Objects/private/primitives/MethodObject.cpp | 4 +- Objects/private/primitives/NullObject.cpp | 2 +- Objects/private/primitives/TypeObject.cpp | 16 +- Objects/public/core/Object.hpp | 171 +++++--------------- Objects/public/core/ObjectArchiver.hpp | 19 ++- Objects/public/core/ObjectType.hpp | 72 +++++++++ Objects/public/core/ScriptSection.hpp | 4 +- Objects/public/core/TypeGroups.hpp | 5 +- Objects/public/interpreter/ByteCode.hpp | 2 +- Objects/public/interpreter/ScopeStack.hpp | 6 +- Objects/public/primitives/ClassObject.hpp | 4 +- Objects/public/primitives/ListObject.hpp | 1 + Objects/public/primitives/NullObject.hpp | 6 +- Objects/tests/TestCompiler.cpp | 12 +- Objects/tests/TestCore.cpp | 16 +- Objects/tests/TestInterpreter.cpp | 23 ++- Objects/tests/TestParser.cpp | 1 - Objects/tests/TestPrimitives.cpp | 11 +- 32 files changed, 362 insertions(+), 384 deletions(-) create mode 100644 Objects/public/core/ObjectType.hpp diff --git a/Objects/applications/GUI.cpp b/Objects/applications/GUI.cpp index b03e017..171ead3 100644 --- a/Objects/applications/GUI.cpp +++ b/Objects/applications/GUI.cpp @@ -92,22 +92,22 @@ void ImGui::HoverPopupEnd(ImGui::PopupData& in) { obj::ObjectsGUI::~ObjectsGUI() { if (mClipboard) { - obj::NDO->destroy(mClipboard); + obj::objects_api::destroy(mClipboard); } for (auto iter : mViewStack) { - obj::NDO->destroy(iter->obj); + obj::objects_api::destroy(iter->obj); } } void obj::ObjectsGUI::setClipboard(obj::Object* obj) { if (mClipboard) { - obj::NDO->destroy(mClipboard); + obj::objects_api::destroy(mClipboard); } mClipboard = obj; if (mClipboard) { - obj::NDO->increaseReferenceCount(obj); + obj::objects_api::increaseReferenceCount(obj); } } @@ -118,7 +118,7 @@ obj::Object* imgui_object_create_menu(obj::TypeGroups* type_group = nullptr) { obj::Object* newo = nullptr; if (!type_group) { - type_group = &obj::NDO->type_groups; + type_group = &obj::gObjectsContext->type_groups; } for (auto childo : *type_group->getChilds()) { @@ -143,17 +143,17 @@ obj::Object* imgui_object_create_menu(obj::TypeGroups* type_group = nullptr) { return newo; } -obj::ObjectsGUI::ObjectsGUI() { assert(obj::NDO && "Objects library is not initialized"); } +obj::ObjectsGUI::ObjectsGUI() { assert(gObjectsContext && "Objects library is not initialized"); } void obj::ObjectsGUI::cd(obj::Object* child, const std::string& name) { mActive = child; mViewStack.pushBack({ mActive, name }); - obj::NDO->increaseReferenceCount(child); + obj::objects_api::increaseReferenceCount(child); } void obj::ObjectsGUI::cdup() { if (mViewStack.length() > 1) { - obj::NDO->destroy(mViewStack.last()->data.obj); + obj::objects_api::destroy(mViewStack.last()->data.obj); mViewStack.popBack(); mActive = mViewStack.last()->data.obj; @@ -461,7 +461,7 @@ obj::ObjectsGUI::ViewStackNode obj::ObjectsGUI::enumView(obj::EnumObject* obj) { for (tp::uhalni idx = 0; idx < obj->nentries; idx++) { if (ImGui::Selectable(obj->getItemName(idx))) { - obj::NDO->set(obj, tp::alni(idx)); + obj::objects_api::set(obj, tp::alni(idx)); } } @@ -590,11 +590,11 @@ void obj::ObjectsGUI::dictViewEdit(obj::DictObject* dict, const std::string& key if (bool(idx)) { // Notify("Object with such name Already Exists"); } else { - obj::NDO->increaseReferenceCount(obj); + obj::objects_api::increaseReferenceCount(obj); dict->remove(key); auto id = std::string(mNameEdit); dict->put(id, obj); - obj::NDO->destroy(obj); + obj::objects_api::destroy(obj); } } } @@ -918,7 +918,7 @@ void obj::ObjectsGUI::explorer() { } if (ImGui::Selectable("Instantiate ")) { - setClipboard(obj::NDO->instantiate(curretn_object)); + setClipboard(obj::objects_api::instantiate(curretn_object)); // Notify("Object copied to clipboard"); } @@ -934,12 +934,12 @@ void obj::ObjectsGUI::explorer() { bool load_object = ImGui::Button("Load Object"); if (save_object) { - obj::NDO->save(curretn_object, path_str, compressed); + obj::objects_api::save(curretn_object, path_str, compressed); // Notify("Object saved"); } if (load_object) { - obj::Object* loadedo = obj::NDO->load(path_str); + obj::Object* loadedo = obj::objects_api::load(path_str); if (loadedo) { setClipboard(loadedo); // Notify("Object copied to clipboard"); @@ -1008,7 +1008,7 @@ void obj::ObjectsGUI::properties(const obj::ObjectType* type, bool top_of_tree_v assert(mActive); if (mActive->type != type) return; - ImGui::Text(" RefCount : %i", obj::NDO->getReferenceCount(mActive)); + ImGui::Text(" RefCount : %i", obj::objects_api::getReferenceCount(mActive)); ImGui::Text(" Type : %s", type->name); @@ -1043,14 +1043,14 @@ void obj::ObjectsGUI::properties(const obj::ObjectType* type, bool top_of_tree_v ImGui::Text("RAM : "); ImGui::Indent(); ImGui::Text("Only Structure : %i bytes", type->size); - ImGui::Text("With Non-Object Links : %i bytes", obj::NDO->objsize_ram(mActive)); - ImGui::Text("Full Size Recursive : %i bytes", obj::NDO->objsize_ram_recursive(mActive)); + ImGui::Text("With Non-Object Links : %i bytes", obj::objects_api::objsize_ram(mActive)); + ImGui::Text("Full Size Recursive : %i bytes", obj::objects_api::objsize_ram_recursive(mActive)); ImGui::Unindent(); ImGui::Text("Disk :"); ImGui::Indent(); - ImGui::Text("Only Structure : %i bytes", obj::NDO->objsize_file(mActive)); - ImGui::Text("With Object Links : %i bytes", obj::NDO->objsize_file_recursive(mActive)); + ImGui::Text("Only Structure : %i bytes", obj::objects_api::objsize_file(mActive)); + ImGui::Text("With Object Links : %i bytes", obj::objects_api::objsize_file_recursive(mActive)); ImGui::Unindent(); // ImGui::TreePop(); diff --git a/Objects/private/compiler/Constants.cpp b/Objects/private/compiler/Constants.cpp index 211330b..5f2e9e8 100644 --- a/Objects/private/compiler/Constants.cpp +++ b/Objects/private/compiler/Constants.cpp @@ -12,22 +12,22 @@ ConstObject::ConstObject(Object* mObj) : ConstObjectsPool::~ConstObjectsPool() { if (mDelete) { for (auto obj : mStrings) { - obj::NDO->destroy(obj->val->mObj); + objects_api::destroy(obj->val->mObj); } for (auto obj : mIntegers) { - obj::NDO->destroy(obj->val->mObj); + objects_api::destroy(obj->val->mObj); } for (auto obj : mFloats) { - obj::NDO->destroy(obj->val->mObj); + objects_api::destroy(obj->val->mObj); } for (auto obj : mMethods) { - obj::NDO->destroy(obj->val->mObj); + objects_api::destroy(obj->val->mObj); } if (mBoolFalse.mObj) { - obj::NDO->destroy(mBoolFalse.mObj); + objects_api::destroy(mBoolFalse.mObj); } if (mBoolTrue.mObj) { - obj::NDO->destroy(mBoolTrue.mObj); + objects_api::destroy(mBoolTrue.mObj); } } for (auto obj : mStrings) { diff --git a/Objects/private/core/Object.cpp b/Objects/private/core/Object.cpp index 2646fcb..1d94786 100644 --- a/Objects/private/core/Object.cpp +++ b/Objects/private/core/Object.cpp @@ -9,6 +9,21 @@ using namespace tp; using namespace obj; +ObjectsContext* obj::gObjectsContext = nullptr; + +void objects_api::initialize() { + ASSERT(!gObjectsContext) + gObjectsContext = new ObjectsContext(); +} + +void objects_api::finalize() { + assertNoLeaks(); + + ASSERT(gObjectsContext) + delete gObjectsContext; + gObjectsContext = nullptr; +} + void obj::save_string(ArchiverOut& file, const std::string& string) { file << string.size(); file.writeBytes(string.c_str(), string.size()); @@ -29,24 +44,22 @@ void obj::load_string(ArchiverIn& file, std::string& out) { Object* ObjectMemAllocate(const ObjectType* type); void ObjectMemDeallocate(Object* object); -objects_api* obj::NDO = nullptr; - -void hierarchy_copy(Object* self, const Object* in, const ObjectType* type); -void hierarchy_construct(Object* self, const ObjectType* type); - -objects_api::objects_api() { +ObjectsContext::ObjectsContext() { memSetVal(sl_callbacks, SAVE_LOAD_MAX_CALLBACK_SLOTS * sizeof(save_load_callbacks*), 0); interp = new Interpreter(); } -objects_api::~objects_api() { delete interp; } +ObjectsContext::~ObjectsContext() { delete interp; } + +void hierarchy_copy(Object* self, const Object* in, const ObjectType* type); +void hierarchy_construct(Object* self, const ObjectType* type); void objects_api::define(ObjectType* type) { MODULE_SANITY_CHECK(gModuleObjects) - DEBUG_ASSERT(NDO && "using uninitialized objects api") - DEBUG_ASSERT(!types.presents(type->name) && "Type Redefinition") - types.put(type->name, type); + DEBUG_ASSERT(gObjectsContext && "using uninitialized objects api") + DEBUG_ASSERT(!gObjectsContext->types.presents(type->name) && "Type Redefinition") + gObjectsContext->types.put(type->name, type); type->type_methods.init(); } @@ -73,7 +86,7 @@ Object* objects_api::create(const ObjectType* type) { auto constructor_obj = classobj->members->getSlotVal(idx); auto constructor_method = objects_api::cast(constructor_obj); if (constructor_method) { - interp->execAll(constructor_method, classobj); + gObjectsContext->interp->execAll(constructor_method, classobj); } } } @@ -154,7 +167,7 @@ std::string objects_api::toString(Object* self) { return self->type->conversions->to_string(self); } -void objects_api::destroy(Object* in) const { +void objects_api::destroy(Object* in) { if (!in) { return; @@ -173,7 +186,7 @@ void objects_api::destroy(Object* in) const { auto constructor_obj = classobj->members->getSlotVal(idx); auto constructor_method = objects_api::cast(constructor_obj); if (constructor_method) { - interp->execAll(constructor_method, classobj); + gObjectsContext->interp->execAll(constructor_method, classobj); } } } @@ -206,3 +219,11 @@ void hierarchy_construct(Object* self, const ObjectType* type) { type->constructor(self); } } + +void objects_api::addTypeToGroup(ObjectType* type, InitialierList path, alni cur_arg) { + gObjectsContext->type_groups.addType(type, path, cur_arg); +} + +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 diff --git a/Objects/private/core/ObjectSave.cpp b/Objects/private/core/ObjectSave.cpp index 27c64aa..d456452 100644 --- a/Objects/private/core/ObjectSave.cpp +++ b/Objects/private/core/ObjectSave.cpp @@ -54,7 +54,7 @@ void ObjectMemDeallocate(Object* object) { count--; } -void obj::logTypeData(const ObjectType* type) { +void objects_api::logTypeData(const ObjectType* type) { printf("type - %s\n", type->name); if (type->base) { printf("Based on "); @@ -62,9 +62,9 @@ void obj::logTypeData(const ObjectType* type) { } } -ualni obj::getObjCount() { return count; } +ualni objects_api::getObjCount() { return count; } -void obj::assertNoLeaks() { +void objects_api::assertNoLeaks() { if (bottom) { printf("ERROR : not all objects are destroyed\n"); ualni idx = 0; @@ -284,7 +284,7 @@ Object* objects_api::load(ArchiverIn& ndf, alni file_adress) { std::string type_name; load_string(ndf, type_name); - const ObjectType* object_type = NDO->types.get(type_name); + const ObjectType* object_type = gObjectsContext->types.get(type_name); Object* out = ObjectMemAllocate(object_type); if (!out) { @@ -329,16 +329,18 @@ bool objects_api::save(Object* in, const std::string& path, bool compressed) { // pre allocate for (alni i = 0; i < SAVE_LOAD_MAX_CALLBACK_SLOTS; i++) { - if (sl_callbacks[i]) { - DEBUG_ASSERT(sl_callbacks[i]->size); - ndf.setFreeAddress(ndf.getFreeAddress() + sl_callbacks[i]->size(sl_callbacks[i]->self, ndf)); + if (gObjectsContext->sl_callbacks[i]) { + DEBUG_ASSERT(gObjectsContext->sl_callbacks[i]->size); + ndf.setFreeAddress( + ndf.getFreeAddress() + gObjectsContext->sl_callbacks[i]->size(gObjectsContext->sl_callbacks[i]->self, ndf) + ); } } // pre-save for (alni i = 0; i < SAVE_LOAD_MAX_CALLBACK_SLOTS; i++) { - if (sl_callbacks[i] && sl_callbacks[i]->pre_save) { - sl_callbacks[i]->pre_save(sl_callbacks[i]->self, ndf); + if (gObjectsContext->sl_callbacks[i] && gObjectsContext->sl_callbacks[i]->pre_save) { + gObjectsContext->sl_callbacks[i]->pre_save(gObjectsContext->sl_callbacks[i]->self, ndf); } } @@ -346,8 +348,8 @@ bool objects_api::save(Object* in, const std::string& path, bool compressed) { // post-save for (alni i = 0; i < SAVE_LOAD_MAX_CALLBACK_SLOTS; i++) { - if (sl_callbacks[i] && sl_callbacks[i]->post_save) { - sl_callbacks[i]->post_save(sl_callbacks[i]->self, ndf); + if (gObjectsContext->sl_callbacks[i] && gObjectsContext->sl_callbacks[i]->post_save) { + gObjectsContext->sl_callbacks[i]->post_save(gObjectsContext->sl_callbacks[i]->self, ndf); } } @@ -404,8 +406,8 @@ Object* objects_api::load(const std::string& path) { // preload for (alni i = 0; i < SAVE_LOAD_MAX_CALLBACK_SLOTS; i++) { - if (sl_callbacks[i] && sl_callbacks[i]->pre_load) { - sl_callbacks[i]->pre_load(sl_callbacks[i]->self, ndf); + if (gObjectsContext->sl_callbacks[i] && gObjectsContext->sl_callbacks[i]->pre_load) { + gObjectsContext->sl_callbacks[i]->pre_load(gObjectsContext->sl_callbacks[i]->self, ndf); } } @@ -413,8 +415,8 @@ Object* objects_api::load(const std::string& path) { // post for (alni i = 0; i < SAVE_LOAD_MAX_CALLBACK_SLOTS; i++) { - if (sl_callbacks[i] && sl_callbacks[i]->post_save) { - sl_callbacks[i]->post_load(sl_callbacks[i]->self, ndf); + if (gObjectsContext->sl_callbacks[i] && gObjectsContext->sl_callbacks[i]->post_save) { + gObjectsContext->sl_callbacks[i]->post_load(gObjectsContext->sl_callbacks[i]->self, ndf); } } @@ -432,6 +434,6 @@ Object* objects_api::load(const std::string& path) { } void objects_api::add_sl_callbacks(save_load_callbacks* in) { - sl_callbacks[sl_callbacks_load_idx] = in; - sl_callbacks_load_idx++; + gObjectsContext->sl_callbacks[gObjectsContext->sl_callbacks_load_idx] = in; + gObjectsContext->sl_callbacks_load_idx++; } diff --git a/Objects/private/core/ObjectsModule.cpp b/Objects/private/core/ObjectsModule.cpp index 95e1cec..a93b233 100644 --- a/Objects/private/core/ObjectsModule.cpp +++ b/Objects/private/core/ObjectsModule.cpp @@ -1,58 +1,44 @@ #include "compiler/Functions.hpp" -#include "primitives/BoolObject.hpp" -#include "primitives/ClassObject.hpp" -#include "primitives/ColorObject.hpp" -#include "primitives/DictObject.hpp" -#include "primitives/EnumObject.hpp" -#include "primitives/FloatObject.hpp" -#include "primitives/InterpreterObject.hpp" -#include "primitives/IntObject.hpp" -#include "primitives/LinkObject.hpp" -#include "primitives/ListObject.hpp" -#include "primitives/MethodObject.hpp" -#include "primitives/NullObject.hpp" -#include "primitives/StringObject.hpp" -#include "primitives/TypeObject.hpp" +#include "primitives/PrimitiveObjects.hpp" using namespace tp; using namespace obj; static void defineTypes() { - NDO->define(&DictObject::TypeData); - NDO->define(&IntObject::TypeData); - NDO->define(&LinkObject::TypeData); - NDO->define(&ListObject::TypeData); - NDO->define(&NullObject::TypeData); - NDO->define(&StringObject::TypeData); - NDO->define(&BoolObject::TypeData); - NDO->define(&FloatObject::TypeData); - NDO->define(&EnumObject::TypeData); - NDO->define(&ClassObject::TypeData); - NDO->define(&ColorObject::TypeData); - NDO->define(&InterpreterObject::TypeData); - NDO->define(&TypeObject::TypeData); + objects_api::define(&DictObject::TypeData); + objects_api::define(&IntObject::TypeData); + objects_api::define(&LinkObject::TypeData); + objects_api::define(&ListObject::TypeData); + objects_api::define(&NullObject::TypeData); + objects_api::define(&StringObject::TypeData); + objects_api::define(&BoolObject::TypeData); + objects_api::define(&FloatObject::TypeData); + objects_api::define(&EnumObject::TypeData); + objects_api::define(&ClassObject::TypeData); + objects_api::define(&ColorObject::TypeData); + objects_api::define(&InterpreterObject::TypeData); + objects_api::define(&TypeObject::TypeData); } static void defineGroups() { - NDO->type_groups.addType(&DictObject::TypeData, { "Primitives" }); - NDO->type_groups.addType(&IntObject::TypeData, { "Primitives" }); - NDO->type_groups.addType(&LinkObject::TypeData, { "Primitives" }); - NDO->type_groups.addType(&ListObject::TypeData, { "Primitives" }); - NDO->type_groups.addType(&NullObject::TypeData, { "Primitives" }); - NDO->type_groups.addType(&StringObject::TypeData, { "Primitives" }); - NDO->type_groups.addType(&BoolObject::TypeData, { "Primitives" }); - NDO->type_groups.addType(&FloatObject::TypeData, { "Primitives" }); - NDO->type_groups.addType(&EnumObject::TypeData, { "Primitives" }); - NDO->type_groups.addType(&ClassObject::TypeData, { "Primitives" }); - NDO->type_groups.addType(&ColorObject::TypeData, { "Primitives" }); - NDO->type_groups.addType(&InterpreterObject::TypeData, { "scripting" }); + objects_api::addTypeToGroup(&DictObject::TypeData, { "Primitives" }); + objects_api::addTypeToGroup(&IntObject::TypeData, { "Primitives" }); + objects_api::addTypeToGroup(&LinkObject::TypeData, { "Primitives" }); + objects_api::addTypeToGroup(&ListObject::TypeData, { "Primitives" }); + objects_api::addTypeToGroup(&NullObject::TypeData, { "Primitives" }); + objects_api::addTypeToGroup(&StringObject::TypeData, { "Primitives" }); + objects_api::addTypeToGroup(&BoolObject::TypeData, { "Primitives" }); + objects_api::addTypeToGroup(&FloatObject::TypeData, { "Primitives" }); + objects_api::addTypeToGroup(&EnumObject::TypeData, { "Primitives" }); + objects_api::addTypeToGroup(&ClassObject::TypeData, { "Primitives" }); + objects_api::addTypeToGroup(&ColorObject::TypeData, { "Primitives" }); + objects_api::addTypeToGroup(&InterpreterObject::TypeData, { "scripting" }); } static bool init(const ModuleManifest*) { - if (!NDO) NDO = new objects_api(); - obj::initialize(); + objects_api::initialize(); MethodObject::Initialize(); @@ -63,20 +49,12 @@ static bool init(const ModuleManifest*) { } static void deinit(const ModuleManifest*) { - NullObject::uninit(); MethodObject::UnInitialize(); - obj::finalize(); - - assertNoLeaks(); - - delete NDO; - NDO = nullptr; + objects_api::finalize(); } -static ModuleManifest* sModuleDependencies[] = { - nullptr -}; +static ModuleManifest* sModuleDependencies[] = { nullptr }; ModuleManifest obj::gModuleObjects = ModuleManifest("Objects", init, deinit, sModuleDependencies); diff --git a/Objects/private/core/ScriptSection.cpp b/Objects/private/core/ScriptSection.cpp index 18fec5e..f6cc8c5 100644 --- a/Objects/private/core/ScriptSection.cpp +++ b/Objects/private/core/ScriptSection.cpp @@ -30,13 +30,13 @@ Script* ScriptSection::createScript() { new (out) Script(); out->mReadable = obj::StringObject::create(""); - obj::NDO->increaseReferenceCount(out->mReadable); + objects_api::increaseReferenceCount(out->mReadable); return out; } void ScriptSection::delete_script(Script* script) { script->~Script(); - obj::NDO->destroy(script->mReadable); + objects_api::destroy(script->mReadable); free((((script_data_head*) script) - 1)); } @@ -99,7 +99,7 @@ void ScriptSection::save_script_table_to_file(ScriptSection* self, ArchiverOut& for (auto iter : self->mScripts) { // scripts string obj ref - auto obj_addres = obj::NDO->save(file, iter->mReadable); + auto obj_addres = objects_api::save(file, iter->mReadable); file << obj_addres; // constants length @@ -108,7 +108,7 @@ void ScriptSection::save_script_table_to_file(ScriptSection* self, ArchiverOut& for (auto const_obj : iter->mBytecode.mConstants) { // constant object addres - auto obj_addres = obj::NDO->save(file, const_obj.data()); + auto obj_addres = objects_api::save(file, const_obj.data()); file << obj_addres; } @@ -140,15 +140,15 @@ void load_constants(ScriptSection* self, ArchiverIn& file, alni start_addr) { alni str_addr; file >> str_addr; - NDO->destroy(script->mReadable); // we already have string object in the script when creating script - script->mReadable = objects_api::cast(obj::NDO->load(file, str_addr)); + objects_api::destroy(script->mReadable); // we already have string object in the script when creating script + script->mReadable = objects_api::cast(objects_api::load(file, str_addr)); file.setAddress(file.getAddress() + sizeof(alni)); // constants length for (auto const_obj : script->mBytecode.mConstants) { alni consts_addr; file >> consts_addr; - const_obj.data() = obj::NDO->load(file, consts_addr); + const_obj.data() = objects_api::load(file, consts_addr); } // instructions @@ -192,7 +192,7 @@ void ScriptSection::load_script_table_from_file(ScriptSection* self, ArchiverIn& file >> consts_addr; // skiping - // new_script->mBytecode.mConstants[j] = obj::NDO->load(file, consts_addr); + // new_script->mBytecode.mConstants[j] = objects_api::load(file, consts_addr); } // mInstructions @@ -238,10 +238,8 @@ void ScriptSection::initialize() { ASSERT(!gScriptSection); gScriptSection = new ScriptSection(); - ASSERT(obj::NDO && "Forgot to initialize objects?"); - slcb_script_section.self = gScriptSection; - obj::NDO->add_sl_callbacks(&slcb_script_section); + objects_api::add_sl_callbacks(&slcb_script_section); } void ScriptSection::uninitialize() { diff --git a/Objects/private/core/TypeMethods.cpp b/Objects/private/core/TypeMethods.cpp index fab757d..234406c 100644 --- a/Objects/private/core/TypeMethods.cpp +++ b/Objects/private/core/TypeMethods.cpp @@ -23,7 +23,7 @@ TypeMethod obj::gDefaultTypeMethods[] = { .exec = [](const TypeMethod* tm) { if (tm->self->type->conversions && tm->self->type->conversions->to_string) { - tm->ret.obj = StringObject::create(NDO->toString(tm->self)); + tm->ret.obj = StringObject::create(objects_api::toString(tm->self)); } }, .ret = { "string object", nullptr }, @@ -34,7 +34,7 @@ TypeMethod obj::gDefaultTypeMethods[] = { .exec = [](const TypeMethod* tm) { if (tm->self->type->conversions && tm->self->type->conversions->to_float) { - tm->ret.obj = FloatObject::create(NDO->toFloat(tm->self)); + tm->ret.obj = FloatObject::create(objects_api::toFloat(tm->self)); } }, .ret = { "string object", nullptr }, @@ -107,7 +107,7 @@ halni TypeMethods::nMethods() const { return mNMethods; } void TypeMethod::operator()(Interpreter* interp) const { for (auto i = 1; i <= mNargs; i++) { args[mNargs - i].obj = interp->mOperandsStack.getOperand(); - // NDO->refinc(args[i].obj); + // objects_api::refinc(args[i].obj); ASSERT(args[mNargs - i].obj && "expected an argument"); } diff --git a/Objects/private/interpreter/CallStack.cpp b/Objects/private/interpreter/CallStack.cpp index 06dbc9e..c55e1c6 100644 --- a/Objects/private/interpreter/CallStack.cpp +++ b/Objects/private/interpreter/CallStack.cpp @@ -15,13 +15,13 @@ void CallStack::enter(const CallStack::CallFrame& frame) { frame.mMethod->mScript->mBytecode.mArgumentsLoaded = 0; frame.mMethod->mScript->mBytecode.mInstructionIdx = 0; - obj::NDO->increaseReferenceCount(frame.mMethod); + objects_api::increaseReferenceCount(frame.mMethod); mStack.append(frame); } void CallStack::leave() { auto frame = mStack.last(); - obj::NDO->destroy(frame.mMethod); + objects_api::destroy(frame.mMethod); mStack.pop(); if (mStack.size()) { diff --git a/Objects/private/interpreter/Interpreter.cpp b/Objects/private/interpreter/Interpreter.cpp index f0380b0..b501a9d 100644 --- a/Objects/private/interpreter/Interpreter.cpp +++ b/Objects/private/interpreter/Interpreter.cpp @@ -167,7 +167,7 @@ void Interpreter::stepBytecodeIn() { case OpCode::JUMP_IF: { auto cond = mOperandsStack.getOperand(); - if (NDO->toBool(cond)) { + if (objects_api::toBool(cond)) { bytecode->mInstructionIdx += param(bytecode); } else { skip_param(bytecode); @@ -178,7 +178,7 @@ void Interpreter::stepBytecodeIn() { case OpCode::JUMP_IF_NOT: { auto cond = mOperandsStack.getOperand(); - if (!NDO->toBool(cond)) { + if (!objects_api::toBool(cond)) { bytecode->mInstructionIdx += param(bytecode); } else { skip_param(bytecode); @@ -195,7 +195,7 @@ void Interpreter::stepBytecodeIn() { case OpCode::JUMP_IF_R: { auto cond = mOperandsStack.getOperand(); - if (NDO->toBool(cond)) { + if (objects_api::toBool(cond)) { bytecode->mInstructionIdx -= param(bytecode); } else { skip_param(bytecode); @@ -206,7 +206,7 @@ void Interpreter::stepBytecodeIn() { case OpCode::JUMP_IF_NOT_R: { auto cond = mOperandsStack.getOperand(); - if (!NDO->toBool(cond)) { + if (!objects_api::toBool(cond)) { bytecode->mInstructionIdx -= param(bytecode); } else { skip_param(bytecode); @@ -224,7 +224,7 @@ void Interpreter::stepBytecodeIn() { { auto obj = mOperandsStack.getOperand(); if (obj->type->conversions && obj->type->conversions->to_string) { - auto str = NDO->toString(obj); + auto str = objects_api::toString(obj); printf("%s\n", str.c_str()); } else { printf("Object with type '%s' has no string representation.\n", obj->type->name); @@ -245,7 +245,7 @@ void Interpreter::stepBytecodeIn() { auto id = mOperandsStack.getOperand(); auto obj = mOperandsStack.getOperand(); mScopeStack.addLocal(obj, id->val); - NDO->increaseReferenceCount(obj); + objects_api::increaseReferenceCount(obj); // mScopeStack.popTemp(); break; } @@ -255,7 +255,7 @@ void Interpreter::stepBytecodeIn() { auto type = mOperandsStack.getOperand(); // basic types - auto idx = NDO->types.presents(type->val); + auto idx = objects_api::isType(type->val.c_str()); if (idx) { Object* new_obj = objects_api::createByName(type->val.c_str()); @@ -285,7 +285,7 @@ void Interpreter::stepBytecodeIn() { case OpCode::CLASS_CONSTRUCT: { - auto class_obj = NDO->create(); + auto class_obj = objects_api::create(); for (auto local : mScopeStack.getCurrentScope()->mLocals) { class_obj->addMember(local->val, local->key); @@ -333,7 +333,7 @@ void Interpreter::stepBytecodeIn() { auto argument = mOperandsStack.getOperand(); while (argument) { - NDO->increaseReferenceCount(argument); + objects_api::increaseReferenceCount(argument); auto argument_id = bytecode->mConstants[loadConstDataIdx(bytecode)]; auto id = objects_api::cast(argument_id); @@ -391,7 +391,7 @@ void Interpreter::stepBytecodeIn() { ASSERT(left->type == right->type && "addition of different types is not implemented"); ASSERT(left->type->arithmetics && left->type->arithmetics->add && "cannot add object of this type"); - auto res = NDO->instantiate(left); + auto res = objects_api::instantiate(left); res->type->arithmetics->add(res, right); mScopeStack.addTemp(res); @@ -406,7 +406,7 @@ void Interpreter::stepBytecodeIn() { ASSERT(left->type == right->type && "addition of different types is not implemented"); ASSERT(left->type->arithmetics && left->type->arithmetics->add && "cannot add object of this type"); - auto res = NDO->instantiate(left); + auto res = objects_api::instantiate(left); res->type->arithmetics->sub(res, right); mScopeStack.addTemp(res); @@ -421,7 +421,7 @@ void Interpreter::stepBytecodeIn() { ASSERT(left->type == right->type && "addition of different types is not implemented"); ASSERT(left->type->arithmetics && left->type->arithmetics->add && "cannot add object of this type"); - auto res = NDO->instantiate(left); + auto res = objects_api::instantiate(left); res->type->arithmetics->mul(res, right); mScopeStack.addTemp(res); @@ -436,7 +436,7 @@ void Interpreter::stepBytecodeIn() { ASSERT(left->type == right->type && "addition of different types is not implemented"); ASSERT(left->type->arithmetics && left->type->arithmetics->add && "cannot add object of this type"); - auto res = NDO->instantiate(left); + auto res = objects_api::instantiate(left); res->type->arithmetics->div(res, right); mScopeStack.addTemp(res); @@ -448,7 +448,7 @@ void Interpreter::stepBytecodeIn() { { auto left = mOperandsStack.getOperand(); auto right = mOperandsStack.getOperand(); - NDO->copy(left, right); + objects_api::copy(left, right); break; } @@ -495,7 +495,7 @@ void Interpreter::stepBytecodeIn() { case OpCode::OBJ_LOAD: { auto path = mOperandsStack.getOperand(); - auto obj = NDO->load(path->val); + auto obj = objects_api::load(path->val); mScopeStack.addTemp(obj); mOperandsStack.push(obj); break; @@ -504,14 +504,14 @@ void Interpreter::stepBytecodeIn() { { auto path = mOperandsStack.getOperand(); auto target = mOperandsStack.getOperand(); - NDO->save(target, path->val); + objects_api::save(target, path->val); break; } case OpCode::AND: { - auto left = NDO->toBool(mOperandsStack.getOperand()); - auto right = NDO->toBool(mOperandsStack.getOperand()); + auto left = objects_api::toBool(mOperandsStack.getOperand()); + auto right = objects_api::toBool(mOperandsStack.getOperand()); auto out = BoolObject::create(left && right); mScopeStack.addTemp(out); mOperandsStack.push(out); @@ -519,8 +519,8 @@ void Interpreter::stepBytecodeIn() { } case OpCode::OR: { - auto left = NDO->toBool(mOperandsStack.getOperand()); - auto right = NDO->toBool(mOperandsStack.getOperand()); + auto left = objects_api::toBool(mOperandsStack.getOperand()); + auto right = objects_api::toBool(mOperandsStack.getOperand()); auto out = BoolObject::create(left || right); mScopeStack.addTemp(out); mOperandsStack.push(out); @@ -528,7 +528,7 @@ void Interpreter::stepBytecodeIn() { } case OpCode::NOT: { - auto inv = NDO->toBool(mOperandsStack.getOperand()); + auto inv = objects_api::toBool(mOperandsStack.getOperand()); auto out = BoolObject::create(!inv); mScopeStack.addTemp(out); mOperandsStack.push(out); @@ -538,7 +538,7 @@ void Interpreter::stepBytecodeIn() { { auto left = mOperandsStack.getOperand(); auto right = mOperandsStack.getOperand(); - auto out = BoolObject::create(!NDO->compare(left, right)); + auto out = BoolObject::create(!objects_api::compare(left, right)); mScopeStack.addTemp(out); mOperandsStack.push(out); break; @@ -547,7 +547,7 @@ void Interpreter::stepBytecodeIn() { { auto left = mOperandsStack.getOperand(); auto right = mOperandsStack.getOperand(); - auto out = BoolObject::create(NDO->compare(left, right)); + auto out = BoolObject::create(objects_api::compare(left, right)); mScopeStack.addTemp(out); mOperandsStack.push(out); break; @@ -555,8 +555,8 @@ void Interpreter::stepBytecodeIn() { case OpCode::MORE: { - auto left = NDO->toFloat(mOperandsStack.getOperand()); - auto right = NDO->toFloat(mOperandsStack.getOperand()); + auto left = objects_api::toFloat(mOperandsStack.getOperand()); + auto right = objects_api::toFloat(mOperandsStack.getOperand()); auto out = BoolObject::create(left > right); mScopeStack.addTemp(out); mOperandsStack.push(out); @@ -564,8 +564,8 @@ void Interpreter::stepBytecodeIn() { } case OpCode::LESS: { - auto left = NDO->toFloat(mOperandsStack.getOperand()); - auto right = NDO->toFloat(mOperandsStack.getOperand()); + auto left = objects_api::toFloat(mOperandsStack.getOperand()); + auto right = objects_api::toFloat(mOperandsStack.getOperand()); auto out = BoolObject::create(left < right); mScopeStack.addTemp(out); mOperandsStack.push(out); @@ -573,8 +573,8 @@ void Interpreter::stepBytecodeIn() { } case OpCode::EQUAL_OR_MORE: { - auto left = NDO->toFloat(mOperandsStack.getOperand()); - auto right = NDO->toFloat(mOperandsStack.getOperand()); + auto left = objects_api::toFloat(mOperandsStack.getOperand()); + auto right = objects_api::toFloat(mOperandsStack.getOperand()); auto out = BoolObject::create(left >= right); mScopeStack.addTemp(out); mOperandsStack.push(out); @@ -582,8 +582,8 @@ void Interpreter::stepBytecodeIn() { } case OpCode::EQUAL_OR_LESS: { - auto left = NDO->toFloat(mOperandsStack.getOperand()); - auto right = NDO->toFloat(mOperandsStack.getOperand()); + auto left = objects_api::toFloat(mOperandsStack.getOperand()); + auto right = objects_api::toFloat(mOperandsStack.getOperand()); auto out = BoolObject::create(left <= right); mScopeStack.addTemp(out); mOperandsStack.push(out); diff --git a/Objects/private/interpreter/ScopeStack.cpp b/Objects/private/interpreter/ScopeStack.cpp index bb40c80..7ce9189 100644 --- a/Objects/private/interpreter/ScopeStack.cpp +++ b/Objects/private/interpreter/ScopeStack.cpp @@ -6,10 +6,10 @@ using namespace obj; Scope::~Scope() { for (auto local : mLocals) { - obj::NDO->destroy(local->val); + objects_api::destroy(local->val); } for (auto tmp : mTemps) { - obj::NDO->destroy(tmp.data()); + objects_api::destroy(tmp.data()); } } @@ -47,18 +47,18 @@ void ScopeStack::leaveScope() { } void ScopeStack::addTemp(obj::Object* tmp) { - obj::NDO->increaseReferenceCount(tmp); + objects_api::increaseReferenceCount(tmp); mBuff[mIdx - 1].mTemps.pushBack(tmp); } void ScopeStack::popTemp() { - obj::NDO->destroy(mBuff[mIdx - 1].mTemps.last()->data); + objects_api::destroy(mBuff[mIdx - 1].mTemps.last()->data); mBuff[mIdx - 1].mTemps.popBack(); } void ScopeStack::addTempReturn(obj::Object* ret) { if (mIdx >= 2) { - obj::NDO->increaseReferenceCount(ret); + objects_api::increaseReferenceCount(ret); mBuff[mIdx - 2].mTemps.pushBack(ret); } } @@ -68,9 +68,9 @@ void ScopeStack::addLocal(obj::Object* local, const std::string& id) { Scope::ObjDict& locals = mBuff[mIdx - 1].mLocals; auto idx = locals.presents(id); if (idx) { - obj::NDO->destroy(locals.getSlotVal(idx)); + objects_api::destroy(locals.getSlotVal(idx)); } - obj::NDO->increaseReferenceCount(local); + objects_api::increaseReferenceCount(local); locals.put(id, local); } diff --git a/Objects/private/primitives/ClassObject.cpp b/Objects/private/primitives/ClassObject.cpp index 006cf61..5efa4a3 100644 --- a/Objects/private/primitives/ClassObject.cpp +++ b/Objects/private/primitives/ClassObject.cpp @@ -17,7 +17,7 @@ void ClassObject::copy(ClassObject* self, const ClassObject* blueprint) { objects_api::copy(self->members, blueprint->members); } -void ClassObject::destructor(ClassObject* self) { NDO->destroy(self->members); } +void ClassObject::destructor(ClassObject* self) { objects_api::destroy(self->members); } void ClassObject::addMember(Object* obj, const std::string& id) { members->put(id, obj); } @@ -32,14 +32,14 @@ alni ClassObject::save_size(ClassObject* self) { void ClassObject::save(ClassObject* self, ArchiverOut& file_self) { // save dict object - alni ndo_object_adress = NDO->save(file_self, self->members); + alni ndo_object_adress = objects_api::save(file_self, self->members); file_self << ndo_object_adress; } void ClassObject::load(ArchiverIn& file_self, ClassObject* self) { alni ndo_object_address; file_self >> ndo_object_address; - self->members = objects_api::cast(NDO->load(file_self, ndo_object_address)); + self->members = objects_api::cast(objects_api::load(file_self, ndo_object_address)); tp::obj::objects_api::increaseReferenceCount(self->members); } @@ -53,7 +53,7 @@ alni allocated_size(ClassObject* self) { return sizeof(DictObject*); } alni allocated_size_recursive(ClassObject* self) { alni out = sizeof(DictObject*); - out += NDO->objsize_ram_recursive_util(self->members, self->members->type); + out += objects_api::objsize_ram_recursive_util(self->members, self->members->type); return out; } diff --git a/Objects/private/primitives/DictObject.cpp b/Objects/private/primitives/DictObject.cpp index bdb3f80..f71f9f3 100644 --- a/Objects/private/primitives/DictObject.cpp +++ b/Objects/private/primitives/DictObject.cpp @@ -12,14 +12,14 @@ void DictObject::copy(DictObject* self, const DictObject* src) { constructor(self); for (auto item : src->items) { - auto instance = NDO->instantiate(item->val); + auto instance = objects_api::instantiate(item->val); self->items.put(item->key, instance); } } void DictObject::destructor(DictObject* self) { for (auto item : self->items) { - NDO->destroy(item->val); + objects_api::destroy(item->val); } self->items.~Map(); } @@ -51,7 +51,7 @@ void DictObject::save(DictObject* self, ArchiverOut& file_self) { // save hashmap pairs for (auto item : self->items) { // item val - alni ndo_object_adress = NDO->save(file_self, item->val); + alni ndo_object_adress = objects_api::save(file_self, item->val); file_self << ndo_object_adress; // item key @@ -70,7 +70,7 @@ void DictObject::load(ArchiverIn& file_self, DictObject* self) { // read val alni ndo_object_adress; file_self >> ndo_object_adress; - Object* val = NDO->load(file_self, ndo_object_adress); + Object* val = objects_api::load(file_self, ndo_object_adress); // read key value std::string key; @@ -105,21 +105,21 @@ alni DictObject::allocated_size(DictObject* self) { alni DictObject::allocated_size_recursive(DictObject* self) { alni out = allocated_size(self); for (auto item : self->items) { - out += NDO->objsize_ram_recursive_util(item->val, item->val->type); + out += objects_api::objsize_ram_recursive_util(item->val, item->val->type); } return out; } void DictObject::put(const std::string& str, Object* obj) { DEBUG_ASSERT(obj); - NDO->increaseReferenceCount(obj); + objects_api::increaseReferenceCount(obj); items.put(str, obj); } void DictObject::remove(const std::string& str) { auto idx = items.presents(str); if (idx) { - NDO->destroy(items.getSlotVal(idx)); + objects_api::destroy(items.getSlotVal(idx)); items.remove(str); } } diff --git a/Objects/private/primitives/LinkObject.cpp b/Objects/private/primitives/LinkObject.cpp index 8eb361f..4dc8d44 100644 --- a/Objects/private/primitives/LinkObject.cpp +++ b/Objects/private/primitives/LinkObject.cpp @@ -7,7 +7,7 @@ using namespace obj; void LinkObject::constructor(LinkObject* self) { self->link = nullptr; } void LinkObject::destructor(LinkObject* self) { - if (self->link) NDO->destroy(self->link); + if (self->link) objects_api::destroy(self->link); } void LinkObject::copy(LinkObject* self, const LinkObject* in) { self->setLink(in->link); } @@ -21,7 +21,7 @@ alni LinkObject::save_size(LinkObject* self) { return sizeof(alni); } void LinkObject::save(LinkObject* self, ArchiverOut& file_self) { if (self->link != nullptr) { - alni link_object_save_adress = NDO->save(file_self, self->link); + alni link_object_save_adress = objects_api::save(file_self, self->link); file_self << link_object_save_adress; } else { alni null = -1; @@ -37,8 +37,8 @@ void LinkObject::load(ArchiverIn& file_self, LinkObject* self) { if (saved_object_adress == -1) { self->link = nullptr; } else { - self->link = NDO->load(file_self, saved_object_adress); - NDO->increaseReferenceCount(self->link); + self->link = objects_api::load(file_self, saved_object_adress); + objects_api::increaseReferenceCount(self->link); } } @@ -53,7 +53,7 @@ alni LinkObject::allocated_size(LinkObject* self) { return sizeof(Object*); } alni LinkObject::allocated_size_recursive(LinkObject* self) { alni out = sizeof(Object*); if (self->link) { - out += NDO->objsize_ram_recursive_util(self->link, self->link->type); + out += objects_api::objsize_ram_recursive_util(self->link, self->link->type); } return out; } @@ -61,8 +61,8 @@ alni LinkObject::allocated_size_recursive(LinkObject* self) { Object* LinkObject::getLink() { return link; } void LinkObject::setLink(Object* obj) { - if (link) NDO->destroy(link); - if (obj) NDO->increaseReferenceCount(obj); + if (link) objects_api::destroy(link); + if (obj) objects_api::increaseReferenceCount(obj); link = obj; } diff --git a/Objects/private/primitives/ListObject.cpp b/Objects/private/primitives/ListObject.cpp index 0defbbe..69e5068 100644 --- a/Objects/private/primitives/ListObject.cpp +++ b/Objects/private/primitives/ListObject.cpp @@ -18,7 +18,7 @@ void ListObject::copy(ListObject* in, const ListObject* target) { void ListObject::destructor(ListObject* in) { for (auto item : in->items) { - NDO->destroy(item.data()); + objects_api::destroy(item.data()); } in->items.removeAll(); } @@ -33,7 +33,7 @@ void ListObject::save(ListObject* self, ArchiverOut& file_self) { file_self << len; for (auto item : self->items) { - alni ndo_object_adress = NDO->save(file_self, item.data()); + alni ndo_object_adress = objects_api::save(file_self, item.data()); file_self << ndo_object_adress; } } @@ -47,7 +47,7 @@ void ListObject::load(ArchiverIn& file_self, ListObject* self) { for (alni i = 0; i < len; i++) { alni ndo_object_adress; file_self >> ndo_object_adress; - self->pushBack(NDO->load(file_self, ndo_object_adress)); + self->pushBack(objects_api::load(file_self, ndo_object_adress)); } } @@ -71,30 +71,30 @@ alni ListObject::allocated_size_recursive(ListObject* self) { ASSERT(false) // alni out = self->items.sizeAllocatedMem(); for (auto item : self->items) { - // out += NDO->objsize_ram_recursive_util(item.data(), item->type); + // out += objects_api::objsize_ram_recursive_util(item.data(), item->type); } // return out; return 0; } void ListObject::pushBack(Object* obj) { - obj::NDO->increaseReferenceCount(obj); + obj::objects_api::increaseReferenceCount(obj); items.pushBack(obj); } void ListObject::pushFront(Object* obj) { - obj::NDO->increaseReferenceCount(obj); + obj::objects_api::increaseReferenceCount(obj); items.pushFront(obj); } void ListObject::delNode(tp::List::Node* node) { - obj::NDO->destroy(node->data); + obj::objects_api::destroy(node->data); items.deleteNode(node); } void ListObject::popBack() { auto obj = items.last(); - if (obj) obj::NDO->destroy(obj->data); + if (obj) obj::objects_api::destroy(obj->data); items.popBack(); } diff --git a/Objects/private/primitives/MethodObject.cpp b/Objects/private/primitives/MethodObject.cpp index 646c824..47aca6c 100644 --- a/Objects/private/primitives/MethodObject.cpp +++ b/Objects/private/primitives/MethodObject.cpp @@ -51,8 +51,8 @@ void MethodObject::load(ArchiverIn& file_self, obj::MethodObject* self) { void MethodObject::Initialize() { obj::ScriptSection::initialize(); - NDO->define(&MethodObject::TypeData); - obj::NDO->type_groups.addType(&MethodObject::TypeData, { "Primitives" }); + objects_api::define(&MethodObject::TypeData); + objects_api::addTypeToGroup(&MethodObject::TypeData, { "Primitives" }); } void MethodObject::UnInitialize() { obj::ScriptSection::uninitialize(); } diff --git a/Objects/private/primitives/NullObject.cpp b/Objects/private/primitives/NullObject.cpp index be71074..64a92f9 100644 --- a/Objects/private/primitives/NullObject.cpp +++ b/Objects/private/primitives/NullObject.cpp @@ -10,7 +10,7 @@ bool uninit_flag = false; void NullObject::uninit() { uninit_flag = true; - NDO->destroy(NdoNull_globalInstance); + objects_api::destroy(NdoNull_globalInstance); NdoNull_globalInstance = nullptr; } diff --git a/Objects/private/primitives/TypeObject.cpp b/Objects/private/primitives/TypeObject.cpp index 0d58fbe..ffe4098 100644 --- a/Objects/private/primitives/TypeObject.cpp +++ b/Objects/private/primitives/TypeObject.cpp @@ -16,13 +16,11 @@ static alni save_size(TypeObject* self) { return save_string_size(self->mTypeRef static void save(TypeObject* self, ArchiverOut& file_self) { save_string(file_self, self->mTypeRef->name); } static void load(ArchiverIn& file_self, TypeObject* self) { - std::string nameid; - load_string(file_self, nameid); + std::string name; + load_string(file_self, name); - auto idx = NDO->types.presents(nameid); - - if (idx) { - self->mTypeRef = NDO->types.getSlotVal(idx); + if (objects_api::isType(name.c_str())) { + self->mTypeRef = objects_api::getType(name.c_str()); } else { self->mTypeRef = &NullObject::TypeData; } @@ -31,10 +29,8 @@ static void load(ArchiverIn& file_self, TypeObject* self) { static alni allocated_size(TypeObject* self) { return sizeof(alni); } static void from_string(TypeObject* self, const std::string& in) { - auto idx = NDO->types.presents(in); - - if (idx) { - self->mTypeRef = NDO->types.getSlotVal(idx); + if (objects_api::isType(in.c_str())) { + self->mTypeRef = objects_api::getType(in.c_str()); } else { self->mTypeRef = &NullObject::TypeData; } diff --git a/Objects/public/core/Object.hpp b/Objects/public/core/Object.hpp index 0b0978c..5590c78 100644 --- a/Objects/public/core/Object.hpp +++ b/Objects/public/core/Object.hpp @@ -1,35 +1,15 @@ #pragma once -#include "Common.hpp" -#include "Map.hpp" -#include "List.hpp" -#include "Buffer.hpp" -#include "LocalConnection.hpp" - -#include "core/TypeGroups.hpp" -#include "core/TypeMethods.hpp" - -#include "ObjectArchiver.hpp" -#include "SizeCounter.hpp" - -#include - -#include "core/ObjectArchiver.hpp" - -/* Steps to create custom Object: -define name of object -define base type -define struct members -implement construct, destruct and copy methods */ +#include "ObjectType.hpp" namespace tp::obj { + // TODO : reconsider static variables extern ModuleManifest gModuleObjects; + extern struct ObjectsContext* gObjectsContext; - extern struct objects_api* NDO; - - // base object + // Base object struct Object { // TODO : used for saving, can be removed Object* up; @@ -40,100 +20,27 @@ namespace tp::obj { alni references; // type information - const struct ObjectType* type; + const ObjectType* type; // additional inherited data }; - typedef void (*object_from_int)(Object* self, alni in); - typedef void (*object_from_float)(Object* self, alnf in); - typedef void (*object_from_string)(Object* self, const std::string& in); - typedef std::string (*object_to_string)(Object* self); - typedef alni (*object_to_int)(Object* self); - typedef alnf (*object_to_float)(Object* self); - - struct ObjectTypeConversions { - object_from_int from_int; - object_from_float from_float; - object_from_string from_string; - object_to_string to_string; - object_to_int to_int; - object_to_float to_float; - }; - - typedef void (*object_add)(Object* self, Object* operand); - typedef void (*object_sub)(Object* self, Object* operand); - typedef void (*object_mul)(Object* self, Object* operand); - typedef void (*object_div)(Object* self, Object* operand); - - struct ObjectTypeArithmetics { - object_add add; - object_sub sub; - object_mul mul; - object_div div; - }; - - typedef void (*object_constructor)(Object* self); - typedef void (*object_destructor)(Object* self); - typedef void (*object_copy)(Object* self, const Object* target); - - typedef alni (*object_save_size)(Object* self); - typedef void (*object_save)(Object*, ArchiverOut&); - typedef void (*object_load)(ArchiverIn&, Object*); - - typedef bool (*object_compare)(Object*, Object*); - - typedef Buffer (*object_debug_all_childs_retrival)(Object*); - typedef alni (*object_allocated_size)(Object*); // default value = type->size - sizeof(ObjectType*) - typedef alni (*object_allocated_size_recursive)(Object*); // default value = object_allocated_size - - struct ObjectType { - const ObjectType* base = nullptr; - object_constructor constructor = nullptr; - object_destructor destructor = nullptr; - object_copy copy = nullptr; - alni size = 0; - const char* name = nullptr; - const ObjectTypeConversions* conversions = nullptr; - const ObjectTypeArithmetics* arithmetics = nullptr; - object_save_size save_size = nullptr; - object_save save = nullptr; - object_load load = nullptr; - object_compare comparison = nullptr; - void* vtable = nullptr; - const char* description = nullptr; - object_debug_all_childs_retrival childs_retrival = nullptr; - object_allocated_size allocated_size = nullptr; - object_allocated_size_recursive allocated_size_recursive = nullptr; - TypeMethods type_methods; - }; - -#define SAVE_LOAD_MAX_CALLBACK_SLOTS 100 - typedef void(pre_save_callback)(void* self, ArchiverOut&); - typedef void(pre_load_callback)(void* self, ArchiverIn&); - typedef void(post_save_callback)(void* self, ArchiverOut&); - typedef void(post_load_callback)(void* self, ArchiverIn&); - typedef alni(slcb_size_callback)(void* self, ArchiverOut&); - - struct save_load_callbacks { - void* self; - pre_save_callback* pre_save; - slcb_size_callback* size; - pre_load_callback* pre_load; - post_save_callback* post_save; - post_load_callback* post_load; - }; - - struct objects_api { + struct ObjectsContext { + ObjectsContext(); + ~ObjectsContext(); Map types; TypeGroups type_groups; Interpreter* interp = nullptr; + save_load_callbacks* sl_callbacks[SAVE_LOAD_MAX_CALLBACK_SLOTS]{}; + alni sl_callbacks_load_idx = 0; + }; - objects_api(); - ~objects_api(); + struct objects_api { + static void initialize(); + static void finalize(); - void define(ObjectType* type); + static void define(ObjectType* type); static Object* copy(Object* self, const Object* in); static bool compare(Object* first, Object* second); static Object* instantiate(Object*); @@ -146,33 +53,28 @@ namespace tp::obj { static bool toBool(Object* self); static std::string toString(Object* self); - void clear_object_flags(); + static void clear_object_flags(); - void destroy(Object* in) const; + static void destroy(Object* in); static inline void increaseReferenceCount(Object* in) { in->references++; } static inline alni getReferenceCount(Object* in) { return in->references; } - private: static inline void setReferenceCount(Object* in, halni count) { in->references = count; } - public: - save_load_callbacks* sl_callbacks[SAVE_LOAD_MAX_CALLBACK_SLOTS]{}; - alni sl_callbacks_load_idx = 0; + static void add_sl_callbacks(save_load_callbacks*); - void add_sl_callbacks(save_load_callbacks*); + static alni objsize_file(Object* self); + static alni objsize_file_recursive(Object* self); - alni objsize_file(Object* self); - alni objsize_file_recursive(Object* self); + static alni objsize_ram(Object* self); + static alni objsize_ram_recursive_util(Object* self, const ObjectType* type, bool different_object = true); + static alni objsize_ram_recursive(Object* self); - alni objsize_ram(Object* self); - alni objsize_ram_recursive_util(Object* self, const ObjectType* type, bool different_object = true); - alni objsize_ram_recursive(Object* self); - - bool save(Object*, const std::string& path, bool compressed = true); - Object* load(const std::string& path); - alni save(ArchiverOut&, Object*); - Object* load(ArchiverIn&, alni file_adress); + static bool save(Object*, const std::string& path, bool compressed = true); + static Object* load(const std::string& path); + static alni save(ArchiverOut&, Object*); + static Object* load(ArchiverIn&, alni file_adress); template static Type* cast(const Object* in) { @@ -186,17 +88,22 @@ namespace tp::obj { return nullptr; } - Object* create(const ObjectType* type); + static Object* create(const ObjectType* type); template static tObjectType* create() { - return (tObjectType*) NDO->create(&tObjectType::TypeData); + return (tObjectType*) create(&tObjectType::TypeData); } - static Object* createByName(const char* name) { return NDO->create(NDO->types.get(name)); } - }; + static Object* createByName(const char* name) { return create(gObjectsContext->types.get(name)); } - void logTypeData(const ObjectType* type); - void assertNoLeaks(); - ualni getObjCount(); + static void logTypeData(const ObjectType* type); + static void assertNoLeaks(); + static ualni getObjCount(); + + static void addTypeToGroup(ObjectType* type, InitialierList path, alni cur_arg = 0); + + static bool isType(const char* name); + static const ObjectType* getType(const char* name); + }; } \ No newline at end of file diff --git a/Objects/public/core/ObjectArchiver.hpp b/Objects/public/core/ObjectArchiver.hpp index 65ca203..9442111 100644 --- a/Objects/public/core/ObjectArchiver.hpp +++ b/Objects/public/core/ObjectArchiver.hpp @@ -2,9 +2,10 @@ #include "ObjectArchiver.hpp" #include "LocalConnection.hpp" +#include "SizeCounter.hpp" namespace tp::obj { - template + template class Archiver : public ArchiverTemplate { LocalConnection mConnection; ualni mAddress = 0; @@ -53,4 +54,20 @@ namespace tp::obj { void save_string(ArchiverOut& file, const std::string& string); ualni save_string_size(const std::string& string); void load_string(ArchiverIn& file, std::string& out); + +#define SAVE_LOAD_MAX_CALLBACK_SLOTS 100 + typedef void(pre_save_callback)(void* self, ArchiverOut&); + typedef void(pre_load_callback)(void* self, ArchiverIn&); + typedef void(post_save_callback)(void* self, ArchiverOut&); + typedef void(post_load_callback)(void* self, ArchiverIn&); + typedef alni(slcb_size_callback)(void* self, ArchiverOut&); + + struct save_load_callbacks { + void* self; + pre_save_callback* pre_save; + slcb_size_callback* size; + pre_load_callback* pre_load; + post_save_callback* post_save; + post_load_callback* post_load; + }; } \ No newline at end of file diff --git a/Objects/public/core/ObjectType.hpp b/Objects/public/core/ObjectType.hpp new file mode 100644 index 0000000..22f5464 --- /dev/null +++ b/Objects/public/core/ObjectType.hpp @@ -0,0 +1,72 @@ +#pragma once + +#include "ObjectArchiver.hpp" +#include "TypeMethods.hpp" +#include "TypeGroups.hpp" + +namespace tp::obj { + struct Object; + + typedef void (*object_from_int)(Object* self, alni in); + typedef void (*object_from_float)(Object* self, alnf in); + typedef void (*object_from_string)(Object* self, const std::string& in); + typedef std::string (*object_to_string)(Object* self); + typedef alni (*object_to_int)(Object* self); + typedef alnf (*object_to_float)(Object* self); + + struct ObjectTypeConversions { + object_from_int from_int; + object_from_float from_float; + object_from_string from_string; + object_to_string to_string; + object_to_int to_int; + object_to_float to_float; + }; + + typedef void (*object_add)(Object* self, Object* operand); + typedef void (*object_sub)(Object* self, Object* operand); + typedef void (*object_mul)(Object* self, Object* operand); + typedef void (*object_div)(Object* self, Object* operand); + + struct ObjectTypeArithmetics { + object_add add; + object_sub sub; + object_mul mul; + object_div div; + }; + + typedef void (*object_constructor)(Object* self); + typedef void (*object_destructor)(Object* self); + typedef void (*object_copy)(Object* self, const Object* target); + + typedef alni (*object_save_size)(Object* self); + typedef void (*object_save)(Object*, ArchiverOut&); + typedef void (*object_load)(ArchiverIn&, Object*); + + typedef bool (*object_compare)(Object*, Object*); + + typedef Buffer (*object_debug_all_childs_retrival)(Object*); + typedef alni (*object_allocated_size)(Object*); // default value = type->size - sizeof(ObjectType*) + typedef alni (*object_allocated_size_recursive)(Object*); // default value = object_allocated_size + + struct ObjectType { + const ObjectType* base = nullptr; + object_constructor constructor = nullptr; + object_destructor destructor = nullptr; + object_copy copy = nullptr; + alni size = 0; + const char* name = nullptr; + const ObjectTypeConversions* conversions = nullptr; + const ObjectTypeArithmetics* arithmetics = nullptr; + object_save_size save_size = nullptr; + object_save save = nullptr; + object_load load = nullptr; + object_compare comparison = nullptr; + void* vtable = nullptr; + const char* description = nullptr; + object_debug_all_childs_retrival childs_retrival = nullptr; + object_allocated_size allocated_size = nullptr; + object_allocated_size_recursive allocated_size_recursive = nullptr; + TypeMethods type_methods; + }; +} \ No newline at end of file diff --git a/Objects/public/core/ScriptSection.hpp b/Objects/public/core/ScriptSection.hpp index cae0f4c..9fe937a 100644 --- a/Objects/public/core/ScriptSection.hpp +++ b/Objects/public/core/ScriptSection.hpp @@ -1,6 +1,7 @@ #pragma once #include "primitives/MethodObject.hpp" +#include "List.hpp" namespace tp::obj { @@ -8,7 +9,7 @@ namespace tp::obj { struct ScriptSection { List mScripts; - + Script* createScript(); void changeScript(Script** current_script, Script** new_script); void abandonScript(Script* script); @@ -23,7 +24,6 @@ namespace tp::obj { static ScriptSection* globalHandle(); private: - static obj::save_load_callbacks slcb_script_section; void delete_script(Script* script); diff --git a/Objects/public/core/TypeGroups.hpp b/Objects/public/core/TypeGroups.hpp index bf2ac6f..288c2b3 100644 --- a/Objects/public/core/TypeGroups.hpp +++ b/Objects/public/core/TypeGroups.hpp @@ -12,7 +12,6 @@ namespace tp::obj { class TypeGroups { public: - typedef Map Dict; TypeGroups(); @@ -21,7 +20,8 @@ namespace tp::obj { explicit TypeGroups(bool is_group); - void addType(ObjectType* type, InitialierList path, alni cur_arg = 0); + void addType(ObjectType* type, InitialierList path, alni cur_arg); + void setType(ObjectType* type); bool isGroup(); Dict* getChilds(); @@ -29,7 +29,6 @@ namespace tp::obj { ~TypeGroups(); private: - bool is_group; union { Dict childs; diff --git a/Objects/public/interpreter/ByteCode.hpp b/Objects/public/interpreter/ByteCode.hpp index 46aeaf0..f8a9dfa 100644 --- a/Objects/public/interpreter/ByteCode.hpp +++ b/Objects/public/interpreter/ByteCode.hpp @@ -21,7 +21,7 @@ namespace tp::obj { ~ByteCode() { for (auto const_obj : mConstants) { - NDO->destroy(const_obj.data()); + objects_api::destroy(const_obj.data()); } } }; diff --git a/Objects/public/interpreter/ScopeStack.hpp b/Objects/public/interpreter/ScopeStack.hpp index a02c34c..167ae17 100644 --- a/Objects/public/interpreter/ScopeStack.hpp +++ b/Objects/public/interpreter/ScopeStack.hpp @@ -2,6 +2,7 @@ #include "core/Object.hpp" #include "Map.hpp" +#include "List.hpp" namespace tp::obj { @@ -18,12 +19,9 @@ namespace tp::obj { class ScopeStack { - enum : alni { - MAX_STACK_SIZE = 1024 * 4 - }; + enum : alni { MAX_STACK_SIZE = 1024 * 4 }; public: - Scope* mBuff; ualni mIdx; ualni mIterIdx; diff --git a/Objects/public/primitives/ClassObject.hpp b/Objects/public/primitives/ClassObject.hpp index e699200..2d6148b 100644 --- a/Objects/public/primitives/ClassObject.hpp +++ b/Objects/public/primitives/ClassObject.hpp @@ -21,9 +21,9 @@ namespace tp::obj { template Type* createMember(const std::string& id) { - auto out = NDO->create(Type::TypeData.name); + auto out = objects_api::create(Type::TypeData.name); addMember(out, id); - NDO->destroy(out); + objects_api::destroy(out); return (Type*) out; } diff --git a/Objects/public/primitives/ListObject.hpp b/Objects/public/primitives/ListObject.hpp index 00d9b26..3c49818 100644 --- a/Objects/public/primitives/ListObject.hpp +++ b/Objects/public/primitives/ListObject.hpp @@ -2,6 +2,7 @@ #pragma once #include "core/Object.hpp" +#include "List.hpp" namespace tp::obj { diff --git a/Objects/public/primitives/NullObject.hpp b/Objects/public/primitives/NullObject.hpp index 51297e5..60fb423 100644 --- a/Objects/public/primitives/NullObject.hpp +++ b/Objects/public/primitives/NullObject.hpp @@ -15,14 +15,14 @@ namespace tp::obj { static void uninit(); static Object* null_return() { if (!NdoNull_globalInstance) { - NdoNull_globalInstance = NDO->create(); - NDO->increaseReferenceCount(NdoNull_globalInstance); + NdoNull_globalInstance = objects_api::create(); + objects_api::increaseReferenceCount(NdoNull_globalInstance); } return (Object*) NdoNull_globalInstance; } static Object* null_return_ref() { - NDO->increaseReferenceCount(null_return()); + objects_api::increaseReferenceCount(null_return()); return NdoNull_globalInstance; } }; diff --git a/Objects/tests/TestCompiler.cpp b/Objects/tests/TestCompiler.cpp index 232e417..e8f9b83 100644 --- a/Objects/tests/TestCompiler.cpp +++ b/Objects/tests/TestCompiler.cpp @@ -2,11 +2,7 @@ #include "ObjectTests.hpp" #include "compiler/Functions.hpp" -#include "parser/Parser.hpp" -#include "core/Object.hpp" #include "interpreter/Interpreter.hpp" -#include "primitives/InterpreterObject.hpp" -#include "primitives/LinkObject.hpp" #include "primitives/MethodObject.hpp" using namespace tp; @@ -23,9 +19,9 @@ SUITE(Compiler) { method->mScript->mReadable->val = "print 1 * 20 + 10;"; method->compile(); - NDO->destroy(method); + objects_api::destroy(method); - assertNoLeaks(); + objects_api::assertNoLeaks(); } { @@ -35,9 +31,9 @@ SUITE(Compiler) { method->mScript->mReadable->val = "print undefinedVariable;"; method->compile(); - NDO->destroy(method); + objects_api::destroy(method); - assertNoLeaks(); + objects_api::assertNoLeaks(); } objTestModule.deinitialize(); diff --git a/Objects/tests/TestCore.cpp b/Objects/tests/TestCore.cpp index c082da9..4cacd29 100644 --- a/Objects/tests/TestCore.cpp +++ b/Objects/tests/TestCore.cpp @@ -1,8 +1,6 @@ #include "ObjectTests.hpp" - #include "core/Object.hpp" - #include "primitives/IntObject.hpp" using namespace tp; @@ -17,19 +15,19 @@ SUITE(Core) { integer->val = 10; - printf("%s\n", NDO->toString(integer).c_str()); + printf("%s\n", objects_api::toString(integer).c_str()); - NDO->save(integer, "tmp.o"); - auto savedInt = NDO->load("tmp.o"); + objects_api::save(integer, "tmp.o"); + auto savedInt = objects_api::load("tmp.o"); - printf("%s\n", NDO->toString(savedInt).c_str()); + printf("%s\n", objects_api::toString(savedInt).c_str()); - CHECK(NDO->compare(integer, savedInt)); + CHECK(objects_api::compare(integer, savedInt)); CHECK(objects_api::cast(savedInt)); CHECK(integer->val == objects_api::cast(savedInt)->val); - NDO->destroy(integer); - NDO->destroy(savedInt); + objects_api::destroy(integer); + objects_api::destroy(savedInt); } objTestModule.deinitialize(); diff --git a/Objects/tests/TestInterpreter.cpp b/Objects/tests/TestInterpreter.cpp index 171496c..fc25ac7 100644 --- a/Objects/tests/TestInterpreter.cpp +++ b/Objects/tests/TestInterpreter.cpp @@ -2,7 +2,6 @@ #include "ObjectTests.hpp" #include "compiler/Functions.hpp" -#include "core/Object.hpp" #include "interpreter/Interpreter.hpp" #include "primitives/InterpreterObject.hpp" #include "primitives/LinkObject.hpp" @@ -59,7 +58,7 @@ SUITE(Interpreter) { interpreter->exec(); - NDO->destroy(interpreter); + objects_api::destroy(interpreter); printf("\n"); } @@ -81,14 +80,14 @@ SUITE(Interpreter) { interpreter->exec(); - NDO->save(interpreter, "interp.o"); + objects_api::save(interpreter, "interp.o"); - auto interpreterLoaded = objects_api::cast(NDO->load("interp.o")); + auto interpreterLoaded = objects_api::cast(objects_api::load("interp.o")); interpreterLoaded->exec(); - NDO->destroy(interpreterLoaded); - NDO->destroy(interpreter); + objects_api::destroy(interpreterLoaded); + objects_api::destroy(interpreter); printf("\n"); } @@ -100,7 +99,7 @@ SUITE(Interpreter) { objTestModule.initialize(); { - auto compileStartCount = getObjCount(); + auto compileStartCount = objects_api::getObjCount(); auto method = objects_api::create(); auto interpreter = objects_api::create(); @@ -111,10 +110,10 @@ SUITE(Interpreter) { method->mScript->mReadable->val = script; method->compile(); - auto startCount = getObjCount(); + auto startCount = objects_api::getObjCount(); interpreter->exec(); - if (getObjCount() != startCount) { + if (objects_api::getObjCount() != startCount) { CHECK(false && "Mem leaks in interpreter"); } @@ -129,9 +128,9 @@ SUITE(Interpreter) { exec("var k : int;"); exec("var k : int; print k;"); - NDO->destroy(interpreter); + objects_api::destroy(interpreter); - if (getObjCount() != compileStartCount) { + if (objects_api::getObjCount() != compileStartCount) { CHECK(false && "Mem leaks in compiler and interpreter"); } } @@ -153,7 +152,7 @@ SUITE(Interpreter) { interpreter->exec(); - NDO->destroy(interpreter); + objects_api::destroy(interpreter); } objTestModule.deinitialize(); diff --git a/Objects/tests/TestParser.cpp b/Objects/tests/TestParser.cpp index 793b92c..701363a 100644 --- a/Objects/tests/TestParser.cpp +++ b/Objects/tests/TestParser.cpp @@ -1,6 +1,5 @@ #include "ObjectTests.hpp" - #include "parser/Parser.hpp" using namespace tp; diff --git a/Objects/tests/TestPrimitives.cpp b/Objects/tests/TestPrimitives.cpp index 366ac52..afed2b1 100644 --- a/Objects/tests/TestPrimitives.cpp +++ b/Objects/tests/TestPrimitives.cpp @@ -2,10 +2,7 @@ #include "ObjectTests.hpp" #include "compiler/Functions.hpp" -#include "core/Object.hpp" #include "interpreter/Interpreter.hpp" -#include "primitives/InterpreterObject.hpp" -#include "primitives/LinkObject.hpp" #include "primitives/MethodObject.hpp" using namespace tp; @@ -23,15 +20,15 @@ SUITE(PrimitiveObjects) { dict->put("val", integer); - NDO->save(dict, "dict.o"); + objects_api::save(dict, "dict.o"); - auto dictLoaded = objects_api::cast(NDO->load("dict.o")); + auto dictLoaded = objects_api::cast(objects_api::load("dict.o")); REQUIRE CHECK(dictLoaded->presents("val").isValid()); CHECK(objects_api::cast(dictLoaded->get("val"))->val == 10); - NDO->destroy(dict); - NDO->destroy(dictLoaded); + objects_api::destroy(dict); + objects_api::destroy(dictLoaded); } objTestModule.deinitialize();