Separate objects context and interface
This commit is contained in:
parent
e937d189df
commit
55daf0f303
32 changed files with 362 additions and 384 deletions
|
|
@ -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<MethodObject>(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<MethodObject>(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<const char*> 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); }
|
||||
|
|
@ -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++;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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<StringObject>(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<StringObject>(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() {
|
||||
|
|
|
|||
|
|
@ -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");
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue