Method object refactor

This commit is contained in:
IlyaShurupov 2024-03-25 13:57:31 +03:00
parent add66e6cac
commit 7332e72bb2
25 changed files with 420 additions and 529 deletions

View file

@ -0,0 +1,74 @@
#include "primitives/BytecodeObject.hpp"
using namespace tp;
using namespace obj;
void BytecodeObject::constructor(BytecodeObject* self) {
new (&self->mBytecode) ByteCode();
self->mReadable = objects_api::create<StringObject>();
}
void BytecodeObject::copy(BytecodeObject* self, BytecodeObject* in) {
// TODO : implement
ASSERT(!"can not copy bytecode object")
}
void BytecodeObject::destructor(BytecodeObject* self) {
self->mBytecode.~ByteCode();
objects_api::destroy(self->mReadable);
}
tp::alni BytecodeObject::save_size(BytecodeObject* self) {
alni out = sizeof(alni); // string object
out += sizeof(alni); // constants size
out += self->mBytecode.mConstants.size() * sizeof(alni); // constant objects
out += SaveSizeCounter::calc(self->mBytecode.mInstructions);
return out;
}
void BytecodeObject::save(BytecodeObject* self, ArchiverOut& file) {
file << (alni) objects_api::save(file, self->mReadable);
file << (alni) self->mBytecode.mConstants.size();
for (auto const_obj : self->mBytecode.mConstants) {
file << (alni) objects_api::save(file, const_obj.data());
}
// mInstructions
file << self->mBytecode.mInstructions;
}
void BytecodeObject::load(ArchiverIn& file, obj::BytecodeObject* self) {
new (&self->mBytecode) ByteCode();
alni stringObjectAddress;
file >> stringObjectAddress;
self->mReadable = objects_api::cast<StringObject>(objects_api::load(file, stringObjectAddress));
alni consts_count;
file >> consts_count;
self->mBytecode.mConstants.reserve(consts_count);
for (auto const_obj : self->mBytecode.mConstants) {
alni consts_addr;
file >> consts_addr;
const_obj.data() = objects_api::load(file, consts_addr);
}
file >> self->mBytecode.mInstructions;
}
struct ObjectType BytecodeObject::TypeData = {
.base = nullptr,
.constructor = (object_constructor) BytecodeObject::constructor,
.destructor = (object_destructor) BytecodeObject::destructor,
.copy = (object_copy) BytecodeObject::copy,
.size = sizeof(BytecodeObject),
.name = "bytecode",
.conversions = nullptr,
.save_size = (object_save_size) BytecodeObject::save_size,
.save = (object_save) BytecodeObject::save,
.load = (object_load) BytecodeObject::load,
};

View file

@ -9,8 +9,8 @@ using namespace obj;
void ClassObject::constructor(ClassObject* self) {
self->members = objects_api::create<DictObject>();
self->addMember(NDO_NULL, "__init__");
self->addMember(NDO_NULL, "__del__");
self->addMember(objects_api::getNull(), "__init__");
self->addMember(objects_api::getNull(), "__del__");
}
void ClassObject::copy(ClassObject* self, const ClassObject* blueprint) {

View file

@ -38,7 +38,7 @@ void InterpreterObject::exec(obj::ClassObject* self, tp::InitialierList<Interpre
}
auto method = objects_api::cast<MethodObject>(target);
if (!method || !method->mScript->mBytecode.mInstructions.size()) {
if (!method || !method->mBytecodeLink->mBytecode.mInstructions.size()) {
return;
}
@ -61,7 +61,7 @@ void InterpreterObject::debug() {
}
auto method = objects_api::cast<MethodObject>(target);
if (!method || !method->mScript->mBytecode.mInstructions.size()) {
if (!method || !method->mBytecodeLink->mBytecode.mInstructions.size()) {
return;
}

View file

@ -1,46 +1,53 @@
#include "compiler/Functions.hpp"
#include "core/ScriptSection.hpp"
#include "primitives/MethodObject.hpp"
#include "compiler/Functions.hpp"
using namespace tp;
using namespace obj;
void MethodObject::constructor(MethodObject* self) {
self->mScript = obj::ScriptSection::globalHandle()->createScript();
// create empty bytecode
self->mBytecodeLink = objects_api::create<BytecodeObject>();
}
void MethodObject::copy(MethodObject* self, MethodObject* in) {
obj::ScriptSection::globalHandle()->changeScript(&self->mScript, &in->mScript);
objects_api::destroy(self->mBytecodeLink);
objects_api::increaseReferenceCount(in->mBytecodeLink);
self->mBytecodeLink = in->mBytecodeLink;
}
void MethodObject::destructor(MethodObject* self) { obj::ScriptSection::globalHandle()->abandonScript(self->mScript); }
void MethodObject::destructor(MethodObject* self) {
// deference
objects_api::destroy(self->mBytecodeLink);
}
tp::alni MethodObject::save_size(MethodObject* self) {
// script_table_file_address & script string object address
return sizeof(tp::alni);
// just reference to the bytecode
return sizeof(Object*);
}
void MethodObject::save(MethodObject* self, ArchiverOut& file_self) {
auto script_section = obj::ScriptSection::globalHandle();
// script_table_file_address
tp::alni script_table_file_address = script_section->get_script_file_adress(self->mScript);
file_self << script_table_file_address;
// save bytecode
file_self << objects_api::save(file_self, self->mBytecodeLink);
}
void MethodObject::load(ArchiverIn& file_self, obj::MethodObject* self) {
auto script_section = obj::ScriptSection::globalHandle();
// script_table_file_address
tp::alni script_table_file_address;
file_self >> script_table_file_address;
self->mScript = script_section->get_scritp_from_file_adress(script_table_file_address);
alni bytecodeAddress;
file_self >> bytecodeAddress;
auto bytecode = objects_api::load(file_self, bytecodeAddress);
self->mBytecodeLink = objects_api::cast<BytecodeObject>(bytecode);
}
void MethodObject::compile() { Compile(this); }
void MethodObject::compile() {
// call to compiler
Compile(this);
}
MethodObject* MethodObject::create(const std::string& script) {
auto out = objects_api::create<MethodObject>();
out->mScript->mReadable->val = script;
ASSERT(false)
out->compile();
return out;
}