new object creation interface

This commit is contained in:
IlyaShurupov 2024-03-25 10:25:25 +03:00
parent c737f41d8a
commit e937d189df
23 changed files with 103 additions and 84 deletions

View file

@ -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 <typename tObjectType>
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 <typename Type>
Type* create() {
return (Type*) NDO->create(Type::TypeData.name);
}
}

View file

@ -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<GlobalDef> globals2 = {});
void exec(
MethodObject* method,
ClassObject* self = nullptr,
DictObject* globals = nullptr,
InitialierList<GlobalDef> globals2 = {}
);
void stepBytecode();
void stepBytecodeIn();
void stepBytecodeOut();
bool finished();
bool finished() const;
void execAll(MethodObject* method, ClassObject* self = nullptr, DictObject* globals = nullptr, InitialierList<GlobalDef> globals2 = {});
void execAll(
MethodObject* method,
ClassObject* self = nullptr,
DictObject* globals = nullptr,
InitialierList<GlobalDef> globals2 = {}
);
};
}

View file

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