diff --git a/Objects/applications/GUI.cpp b/Objects/applications/GUI.cpp index dd04199..b03e017 100644 --- a/Objects/applications/GUI.cpp +++ b/Objects/applications/GUI.cpp @@ -135,7 +135,7 @@ obj::Object* imgui_object_create_menu(obj::TypeGroups* type_group = nullptr) { if (childo->key == "null") { newo = NDO_NULL; } else { - newo = obj::NDO->create(childo->key); + newo = objects_api::createByName(childo->key.c_str()); } } } diff --git a/Objects/applications/GUIEntry.cpp b/Objects/applications/GUIEntry.cpp index ad52ec7..5b0c4e8 100644 --- a/Objects/applications/GUIEntry.cpp +++ b/Objects/applications/GUIEntry.cpp @@ -8,12 +8,9 @@ using namespace obj; class ExampleGUI : public Application { public: - ExampleGUI() { - gui.cd(NDO->create("dict"), "root"); - } + ExampleGUI() { gui.cd(objects_api::create(), "root"); } - void processFrame(EventHandler* eventHandler) override { - } + void processFrame(EventHandler* eventHandler) override {} void drawFrame(Canvas* canvas) override { canvas->rect({ { 0, 0 }, mWindow->getSize() }, RGBA(0.f, 0.f, 0.f, 1.f), 0); diff --git a/Objects/private/compiler/Functions.cpp b/Objects/private/compiler/Functions.cpp index 11a85d3..d5df6ee 100644 --- a/Objects/private/compiler/Functions.cpp +++ b/Objects/private/compiler/Functions.cpp @@ -127,7 +127,7 @@ void FunctionDefinition::EvalStatement(Statement* stm) { mLocals.put(func.mFunctionId, mConstants.get(func.mFunctionId)); // create and register const func object - auto function_obj = objects_api::cast(NDO->create("method")); + auto function_obj = objects_api::create(); auto method_const_obj = mConstants.addMethod(func.mFunctionId, function_obj); for (auto child_stm : stm_func_def->mStatements->mStatements) { @@ -155,7 +155,7 @@ void FunctionDefinition::EvalStatement(Statement* stm) { mLocals.put(func.mFunctionId, mConstants.get(func.mFunctionId)); // create and register const func object - auto function_obj = objects_api::cast(NDO->create("method")); + auto function_obj = objects_api::create(); auto method_const_obj = mConstants.addMethod(func.mFunctionId, function_obj); // compile function diff --git a/Objects/private/core/Object.cpp b/Objects/private/core/Object.cpp index 010d5ab..2646fcb 100644 --- a/Objects/private/core/Object.cpp +++ b/Objects/private/core/Object.cpp @@ -51,10 +51,8 @@ void objects_api::define(ObjectType* type) { type->type_methods.init(); } -Object* objects_api::create(const std::string& name) { - MODULE_SANITY_CHECK(gModuleObjects) - - const ObjectType* type = types.get(name); +Object* objects_api::create(const ObjectType* type) { + ASSERT(type); Object* obj_instance = ObjectMemAllocate(type); @@ -108,7 +106,7 @@ bool objects_api::compare(Object* first, Object* second) { } Object* objects_api::instantiate(Object* in) { - obj::Object* obj = NDO->create(in->type->name); + obj::Object* obj = objects_api::createByName(in->type->name); tp::obj::objects_api::copy(obj, in); return obj; } @@ -162,8 +160,8 @@ void objects_api::destroy(Object* in) const { return; } - if (in->refc > 1) { - in->refc--; + if (in->references > 1) { + in->references--; return; } diff --git a/Objects/private/core/ObjectSave.cpp b/Objects/private/core/ObjectSave.cpp index b6a9999..27c64aa 100644 --- a/Objects/private/core/ObjectSave.cpp +++ b/Objects/private/core/ObjectSave.cpp @@ -23,7 +23,7 @@ Object* ObjectMemAllocate(const ObjectType* type) { object->down = nullptr; object->flags = 0; - object->refc = (alni) 1; + object->references = (alni) 1; if (bottom) { bottom->down = object; @@ -69,7 +69,7 @@ void obj::assertNoLeaks() { printf("ERROR : not all objects are destroyed\n"); ualni idx = 0; for (Object* object = bottom; object; object = object->up) { - printf(" ===== Object - %llu. Ref count - %lli ===== \n", idx, object->refc); + printf(" ===== Object - %llu. Ref count - %lli ===== \n", idx, object->references); logTypeData(object->type); idx++; } diff --git a/Objects/private/interpreter/Interpreter.cpp b/Objects/private/interpreter/Interpreter.cpp index 52b86f4..f0380b0 100644 --- a/Objects/private/interpreter/Interpreter.cpp +++ b/Objects/private/interpreter/Interpreter.cpp @@ -52,7 +52,7 @@ void Interpreter::exec( } } -bool Interpreter::finished() { return !mCallStack.len(); } +bool Interpreter::finished() const { return !mCallStack.len(); } void Interpreter::stepBytecode() { halni call_depth = mCallStack.len(); @@ -236,7 +236,7 @@ void Interpreter::stepBytecodeIn() { { auto type = mOperandsStack.getOperand(); auto id = mOperandsStack.getOperand(); - mScopeStack.addLocal(NDO->create(type->val), id->val); + mScopeStack.addLocal(objects_api::createByName(type->val.c_str()), id->val); break; } @@ -253,12 +253,11 @@ void Interpreter::stepBytecodeIn() { case OpCode::OBJ_CREATE: { auto type = mOperandsStack.getOperand(); - Object* new_obj = nullptr; // basic types auto idx = NDO->types.presents(type->val); if (idx) { - auto new_obj = NDO->create(type->val); + Object* new_obj = objects_api::createByName(type->val.c_str()); mOperandsStack.push(new_obj); mScopeStack.addTemp(new_obj); @@ -286,7 +285,7 @@ void Interpreter::stepBytecodeIn() { case OpCode::CLASS_CONSTRUCT: { - auto class_obj = (ClassObject*) NDO->create("class"); + auto class_obj = NDO->create(); for (auto local : mScopeStack.getCurrentScope()->mLocals) { class_obj->addMember(local->val, local->key); diff --git a/Objects/private/primitives/BoolObject.cpp b/Objects/private/primitives/BoolObject.cpp index 482a1b6..f8ba824 100644 --- a/Objects/private/primitives/BoolObject.cpp +++ b/Objects/private/primitives/BoolObject.cpp @@ -10,7 +10,7 @@ void BoolObject::constructor(BoolObject* self) { self->val = false; } void BoolObject::copy(BoolObject* self, const BoolObject* in) { self->val = in->val; } BoolObject* BoolObject::create(bool in) { - auto out = objects_api::cast(NDO->create("bool")); + auto out = objects_api::create(); out->val = alni(in); return out; } diff --git a/Objects/private/primitives/ClassObject.cpp b/Objects/private/primitives/ClassObject.cpp index 5c2eed8..006cf61 100644 --- a/Objects/private/primitives/ClassObject.cpp +++ b/Objects/private/primitives/ClassObject.cpp @@ -7,14 +7,14 @@ using namespace tp; using namespace obj; void ClassObject::constructor(ClassObject* self) { - self->members = objects_api::cast(NDO->create("dict")); + self->members = objects_api::create(); self->addMember(NDO_NULL, "__init__"); self->addMember(NDO_NULL, "__del__"); } void ClassObject::copy(ClassObject* self, const ClassObject* blueprint) { - NDO->copy(self->members, blueprint->members); + objects_api::copy(self->members, blueprint->members); } void ClassObject::destructor(ClassObject* self) { NDO->destroy(self->members); } @@ -22,24 +22,24 @@ void ClassObject::destructor(ClassObject* self) { NDO->destroy(self->members); } void ClassObject::addMember(Object* obj, const std::string& id) { members->put(id, obj); } void ClassObject::createMember(const std::string& type, const std::string& id) { - auto newo = NDO->create(type); + auto newo = tp::obj::objects_api::createByName(type.c_str()); members->put(id, newo); } alni ClassObject::save_size(ClassObject* self) { - return sizeof(alni); // dict object adress + return sizeof(alni); // dict object address } void ClassObject::save(ClassObject* self, ArchiverOut& file_self) { - // save dictobject + // save dict object alni ndo_object_adress = NDO->save(file_self, self->members); file_self << ndo_object_adress; } void ClassObject::load(ArchiverIn& file_self, ClassObject* self) { - alni ndo_object_adress; - file_self >> ndo_object_adress; - self->members = objects_api::cast(NDO->load(file_self, ndo_object_adress)); + alni ndo_object_address; + file_self >> ndo_object_address; + self->members = objects_api::cast(NDO->load(file_self, ndo_object_address)); tp::obj::objects_api::increaseReferenceCount(self->members); } @@ -57,16 +57,17 @@ alni allocated_size_recursive(ClassObject* self) { return out; } -struct ObjectType ClassObject::TypeData = { .base = nullptr, - .constructor = (object_constructor) ClassObject::constructor, - .destructor = (object_destructor) ClassObject::destructor, - .copy = (object_copy) ClassObject::copy, - .size = sizeof(ClassObject), - .name = "class", - .save_size = (object_save_size) save_size, - .save = (object_save) save, - .load = (object_load) load, - .childs_retrival = (object_debug_all_childs_retrival) childs_retrival, - .allocated_size = (object_allocated_size) allocated_size, - .allocated_size_recursive = - (object_allocated_size_recursive) allocated_size_recursive }; +struct ObjectType ClassObject::TypeData = { + .base = nullptr, + .constructor = (object_constructor) ClassObject::constructor, + .destructor = (object_destructor) ClassObject::destructor, + .copy = (object_copy) ClassObject::copy, + .size = sizeof(ClassObject), + .name = "class", + .save_size = (object_save_size) save_size, + .save = (object_save) save, + .load = (object_load) load, + .childs_retrival = (object_debug_all_childs_retrival) childs_retrival, + .allocated_size = (object_allocated_size) allocated_size, + .allocated_size_recursive = (object_allocated_size_recursive) allocated_size_recursive, +}; diff --git a/Objects/private/primitives/ColorObject.cpp b/Objects/private/primitives/ColorObject.cpp index 44c1abc..aa4bbc3 100644 --- a/Objects/private/primitives/ColorObject.cpp +++ b/Objects/private/primitives/ColorObject.cpp @@ -9,7 +9,7 @@ void ColorObject::constructor(ColorObject* self) { self->mCol = tp::RGBA(1.f); } void ColorObject::copy(ColorObject* self, const ColorObject* in) { self->mCol = in->mCol; } ColorObject* ColorObject::create(tp::RGBA in) { - auto out = objects_api::cast(NDO->create("RGBA")); + auto out = objects_api::create(); out->mCol = in; return out; } diff --git a/Objects/private/primitives/EnumObject.cpp b/Objects/private/primitives/EnumObject.cpp index a11bf3a..a7921cd 100644 --- a/Objects/private/primitives/EnumObject.cpp +++ b/Objects/private/primitives/EnumObject.cpp @@ -139,7 +139,7 @@ bool obj::EnumObject::compare(EnumObject* first, EnumObject* second) { } EnumObject* obj::EnumObject::create(tp::InitialierList list) { - auto enum_object = (EnumObject*) obj::NDO->create("enum"); + auto enum_object = objects_api::create(); enum_object->init(list); return enum_object; } diff --git a/Objects/private/primitives/FloatObject.cpp b/Objects/private/primitives/FloatObject.cpp index d14dce6..2ac6f55 100644 --- a/Objects/private/primitives/FloatObject.cpp +++ b/Objects/private/primitives/FloatObject.cpp @@ -8,7 +8,7 @@ void FloatObject::constructor(FloatObject* self) { self->val = 0; } void FloatObject::copy(FloatObject* self, const FloatObject* in) { self->val = in->val; } FloatObject* FloatObject::create(alnf in) { - auto out = objects_api::cast(NDO->create("float")); + auto out = objects_api::create(); out->val = alnf(in); return out; } diff --git a/Objects/private/primitives/IntObject.cpp b/Objects/private/primitives/IntObject.cpp index 42c3c3c..2d94822 100644 --- a/Objects/private/primitives/IntObject.cpp +++ b/Objects/private/primitives/IntObject.cpp @@ -9,7 +9,7 @@ void IntObject::constructor(IntObject* self) { self->val = 0; } void IntObject::copy(IntObject* self, const IntObject* in) { self->val = in->val; } IntObject* IntObject::create(alni in) { - auto out = objects_api::cast(NDO->create("int")); + auto out = objects_api::create(); out->val = in; return out; } diff --git a/Objects/private/primitives/LinkObject.cpp b/Objects/private/primitives/LinkObject.cpp index ac2739f..8eb361f 100644 --- a/Objects/private/primitives/LinkObject.cpp +++ b/Objects/private/primitives/LinkObject.cpp @@ -13,7 +13,7 @@ void LinkObject::destructor(LinkObject* self) { void LinkObject::copy(LinkObject* self, const LinkObject* in) { self->setLink(in->link); } LinkObject* LinkObject::create(Object* in) { - auto out = objects_api::cast(NDO->create("link")); + auto out = objects_api::create(); return out; } diff --git a/Objects/private/primitives/MethodObject.cpp b/Objects/private/primitives/MethodObject.cpp index 4f2f971..646c824 100644 --- a/Objects/private/primitives/MethodObject.cpp +++ b/Objects/private/primitives/MethodObject.cpp @@ -60,7 +60,7 @@ void MethodObject::UnInitialize() { obj::ScriptSection::uninitialize(); } void MethodObject::compile() { Compile(this); } MethodObject* MethodObject::create(const std::string& script) { - auto out = (MethodObject*) NDO->create(MethodObject::TypeData.name); + auto out = objects_api::create(); out->mScript->mReadable->val = script; out->compile(); return out; diff --git a/Objects/private/primitives/Stringobject.cpp b/Objects/private/primitives/Stringobject.cpp index 123d51b..f080727 100644 --- a/Objects/private/primitives/Stringobject.cpp +++ b/Objects/private/primitives/Stringobject.cpp @@ -12,7 +12,7 @@ void StringObject::destructor(StringObject* self) { self->val.~basic_string(); } void StringObject::copy(StringObject* self, const StringObject* in) { self->val = in->val; } StringObject* StringObject::create(const std::string& in) { - auto out = objects_api::cast(NDO->create("str")); + auto out = objects_api::create(); out->val = in; return out; } diff --git a/Objects/private/primitives/TypeObject.cpp b/Objects/private/primitives/TypeObject.cpp index 45b6482..0d58fbe 100644 --- a/Objects/private/primitives/TypeObject.cpp +++ b/Objects/private/primitives/TypeObject.cpp @@ -6,7 +6,7 @@ using namespace tp; using namespace obj; TypeObject* TypeObject::create(const ObjectType* type) { - auto out = objects_api::cast(NDO->create("typeobject")); + auto out = objects_api::create(); out->mTypeRef = type; return out; } diff --git a/Objects/public/core/Object.hpp b/Objects/public/core/Object.hpp index d6675ed..0b0978c 100644 --- a/Objects/public/core/Object.hpp +++ b/Objects/public/core/Object.hpp @@ -29,13 +29,20 @@ namespace tp::obj { extern struct objects_api* NDO; + // base object struct Object { + // TODO : used for saving, can be removed Object* up; Object* down; alni flags; - alni refc; + // reference counting for memory management + alni references; + + // type information const struct ObjectType* type; + + // additional inherited data }; typedef void (*object_from_int)(Object* self, alni in); @@ -127,7 +134,6 @@ namespace tp::obj { ~objects_api(); void define(ObjectType* type); - Object* create(const std::string& name); static Object* copy(Object* self, const Object* in); static bool compare(Object* first, Object* second); static Object* instantiate(Object*); @@ -144,11 +150,11 @@ namespace tp::obj { void destroy(Object* in) const; - static inline void increaseReferenceCount(Object* in) { in->refc++; } - static inline alni getReferenceCount(Object* in) { return in->refc; } + 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->refc = count; } + static inline void setReferenceCount(Object* in, halni count) { in->references = count; } public: save_load_callbacks* sl_callbacks[SAVE_LOAD_MAX_CALLBACK_SLOTS]{}; @@ -179,14 +185,18 @@ namespace tp::obj { } return nullptr; } + + Object* create(const ObjectType* type); + + template + static tObjectType* create() { + return (tObjectType*) NDO->create(&tObjectType::TypeData); + } + + static Object* createByName(const char* name) { return NDO->create(NDO->types.get(name)); } }; void logTypeData(const ObjectType* type); void assertNoLeaks(); ualni getObjCount(); - - template - Type* create() { - return (Type*) NDO->create(Type::TypeData.name); - } } \ No newline at end of file diff --git a/Objects/public/interpreter/Interpreter.hpp b/Objects/public/interpreter/Interpreter.hpp index 98fad8c..ff0c0bb 100644 --- a/Objects/public/interpreter/Interpreter.hpp +++ b/Objects/public/interpreter/Interpreter.hpp @@ -14,16 +14,29 @@ namespace tp::obj { bool mIsTypeMethod = false; const TypeMethod* mTypeMethod = nullptr; - typedef struct { Object* obj; std::string id; } GlobalDef; + typedef struct { + Object* obj; + std::string id; + } GlobalDef; - void exec(MethodObject* method, ClassObject* self = nullptr, DictObject* globals = nullptr, InitialierList globals2 = {}); + void exec( + MethodObject* method, + ClassObject* self = nullptr, + DictObject* globals = nullptr, + InitialierList globals2 = {} + ); void stepBytecode(); void stepBytecodeIn(); void stepBytecodeOut(); - bool finished(); + bool finished() const; - void execAll(MethodObject* method, ClassObject* self = nullptr, DictObject* globals = nullptr, InitialierList globals2 = {}); + void execAll( + MethodObject* method, + ClassObject* self = nullptr, + DictObject* globals = nullptr, + InitialierList globals2 = {} + ); }; } \ No newline at end of file diff --git a/Objects/public/primitives/NullObject.hpp b/Objects/public/primitives/NullObject.hpp index b6e4d55..51297e5 100644 --- a/Objects/public/primitives/NullObject.hpp +++ b/Objects/public/primitives/NullObject.hpp @@ -8,13 +8,14 @@ namespace tp::obj { extern struct NullObject* NdoNull_globalInstance; struct NullObject : Object { + static ObjectType TypeData; static void destructor(Object* self); - static ObjectType TypeData; + static void uninit(); static Object* null_return() { if (!NdoNull_globalInstance) { - NdoNull_globalInstance = (NullObject*) NDO->create("null"); + NdoNull_globalInstance = NDO->create(); NDO->increaseReferenceCount(NdoNull_globalInstance); } return (Object*) NdoNull_globalInstance; @@ -26,7 +27,7 @@ namespace tp::obj { } }; - #define NDO_NULL NullObject::null_return() - #define NDO_NULL_REF NullObject::null_return_ref() +#define NDO_NULL NullObject::null_return() +#define NDO_NULL_REF NullObject::null_return_ref() } \ No newline at end of file diff --git a/Objects/tests/TestCompiler.cpp b/Objects/tests/TestCompiler.cpp index 10a6b0a..232e417 100644 --- a/Objects/tests/TestCompiler.cpp +++ b/Objects/tests/TestCompiler.cpp @@ -18,7 +18,7 @@ SUITE(Compiler) { { // no errors - auto method = objects_api::cast(NDO->create("method")); + auto method = objects_api::create(); method->mScript->mReadable->val = "print 1 * 20 + 10;"; method->compile(); @@ -30,7 +30,7 @@ SUITE(Compiler) { { // with errors - auto method = objects_api::cast(NDO->create("method")); + auto method = objects_api::create(); method->mScript->mReadable->val = "print undefinedVariable;"; method->compile(); diff --git a/Objects/tests/TestCore.cpp b/Objects/tests/TestCore.cpp index fb08077..c082da9 100644 --- a/Objects/tests/TestCore.cpp +++ b/Objects/tests/TestCore.cpp @@ -13,7 +13,7 @@ SUITE(Core) { objTestModule.initialize(); { - auto integer = objects_api::cast(NDO->create("int")); + auto integer = objects_api::create(); integer->val = 10; diff --git a/Objects/tests/TestInterpreter.cpp b/Objects/tests/TestInterpreter.cpp index 08346db..171496c 100644 --- a/Objects/tests/TestInterpreter.cpp +++ b/Objects/tests/TestInterpreter.cpp @@ -49,8 +49,8 @@ SUITE(Interpreter) { objTestModule.initialize(); { - auto method = objects_api::cast(NDO->create("method")); - auto interpreter = objects_api::cast(NDO->create("interpreter")); + auto method = objects_api::create(); + auto interpreter = objects_api::create(); interpreter->getMember("target method")->setLink(method); @@ -71,8 +71,8 @@ SUITE(Interpreter) { objTestModule.initialize(); { - auto method = objects_api::cast(NDO->create("method")); - auto interpreter = objects_api::cast(NDO->create("interpreter")); + auto method = objects_api::create(); + auto interpreter = objects_api::create(); interpreter->getMember("target method")->setLink(method); @@ -102,8 +102,8 @@ SUITE(Interpreter) { { auto compileStartCount = getObjCount(); - auto method = objects_api::cast(NDO->create("method")); - auto interpreter = objects_api::cast(NDO->create("interpreter")); + auto method = objects_api::create(); + auto interpreter = objects_api::create(); interpreter->getMember("target method")->setLink(method); @@ -143,8 +143,8 @@ SUITE(Interpreter) { objTestModule.initialize(); { - auto method = objects_api::cast(NDO->create("method")); - auto interpreter = objects_api::cast(NDO->create("interpreter")); + auto method = objects_api::create(); + auto interpreter = objects_api::create(); interpreter->getMember("target method")->setLink(method); diff --git a/Objects/tests/TestPrimitives.cpp b/Objects/tests/TestPrimitives.cpp index e369b3f..366ac52 100644 --- a/Objects/tests/TestPrimitives.cpp +++ b/Objects/tests/TestPrimitives.cpp @@ -16,10 +16,10 @@ SUITE(PrimitiveObjects) { objTestModule.initialize(); { - auto integer = objects_api::cast(NDO->create("int")); + auto integer = objects_api::create(); integer->val = 10; - auto dict = objects_api::cast(NDO->create("dict")); + auto dict = objects_api::create(); dict->put("val", integer); @@ -27,7 +27,7 @@ SUITE(PrimitiveObjects) { auto dictLoaded = objects_api::cast(NDO->load("dict.o")); - CHECK(dictLoaded->presents("val").isValid()); + REQUIRE CHECK(dictLoaded->presents("val").isValid()); CHECK(objects_api::cast(dictLoaded->get("val"))->val == 10); NDO->destroy(dict);