First commit

This commit is contained in:
2024-12-28 14:38:00 -06:00
commit 9c27f339be
56 changed files with 289761 additions and 0 deletions

199
src/vm/filereader.cpp Normal file
View File

@ -0,0 +1,199 @@
#include "CrossLang.hpp"
#include <cstring>
#include <filesystem>
#include <iostream>
namespace Tesses::CrossLang
{
TFile* TFile::Create(GCList& ls)
{
TFile* f = new TFile();
GC* _gc = ls.GetGC();
ls.Add(f);
_gc->Watch(f);
return f;
}
TFile* TFile::Create(GCList* ls)
{
TFile* f = new TFile();
GC* _gc = ls->GetGC();
ls->Add(f);
_gc->Watch(f);
return f;
}
void TFileChunk::Mark()
{
if(this->marked) return;
this->marked=true;
this->file->Mark();
}
TFileChunk* TFileChunk::Create(GCList& ls)
{
TFileChunk* chk = new TFileChunk();
GC* _gc = ls.GetGC();
ls.Add(chk);
_gc->Watch(chk);
return chk;
}
TFileChunk* TFileChunk::Create(GCList* ls)
{
TFileChunk* chk = new TFileChunk();
GC* _gc = ls->GetGC();
ls->Add(chk);
_gc->Watch(chk);
return chk;
}
void TFile::Mark()
{
if(this->marked) return;
this->marked=true;
for(auto item : this->chunks)
item->Mark();
}
void TFile::Skip(Tesses::Framework::Streams::Stream* stream,size_t len)
{
if(stream->CanSeek())
{
stream->Seek((int64_t)len,Tesses::Framework::Streams::SeekOrigin::Current);
}
else{
uint8_t* buffer=new uint8_t[len];
Ensure(stream,buffer,len);
delete buffer;
}
}
void TFile::Ensure(Tesses::Framework::Streams::Stream* stream, uint8_t* buffer,size_t len)
{
size_t read = stream->ReadBlock(buffer, len);
if(read < len) throw VMException("End of file, could not read " + std::to_string((int64_t)len) + " byte(s)., offset=" + std::to_string(stream->GetLength()));
}
std::string TFile::EnsureString(Tesses::Framework::Streams::Stream* stream)
{
auto len = EnsureInt(stream);
if(len == 0) return {};
std::string str={};
str.resize((size_t)len);
Ensure(stream,(uint8_t*)str.data(),str.size());
return str;
}
uint32_t TFile::EnsureInt(Tesses::Framework::Streams::Stream* stream)
{
uint8_t buffer[4];
Ensure(stream,buffer,4);
return BitConverter::ToUint32BE(buffer[0]);
}
std::string TFile::GetString(Tesses::Framework::Streams::Stream* stream)
{
uint32_t index=EnsureInt(stream);
if(index > this->strings.size()) throw VMException("String does not exist in TCrossVM file, expected string index: " + std::to_string(index) + ", total strings: " + std::to_string(this->strings.size()));
return this->strings[index];
}
void TFile::Load(GC* gc, Tesses::Framework::Streams::Stream* stream)
{
GCList ls(gc);
uint8_t main_header[18];
Ensure(stream,main_header,sizeof(main_header));
if(strncmp((const char*)main_header,"TCROSSVM",8) != 0) throw VMException("Invalid TCrossVM image.");
TVMVersion version(main_header+8);
if(version.CompareToRuntime() == 1)
{
throw VMException("Runtime is too old.");
}
TVMVersion v2(main_header+13);
this->version = v2;
size_t _len = (size_t)EnsureInt(stream);
char table_name[4];
for(size_t i = 0;i < _len; i++)
{
Ensure(stream,(uint8_t*)table_name,sizeof(table_name));
size_t tableLen = (size_t)EnsureInt(stream);
if(strncmp(table_name,"NAME",4) == 0)
{
this->name = GetString(stream);
}
else if(strncmp(table_name,"INFO",4) == 0)
{
this->info = GetString(stream);
}
else if(strncmp(table_name,"DEPS",4) == 0) //dependencies
{
std::string name = GetString(stream);
uint8_t version_bytes[5];
Ensure(stream,version_bytes,sizeof(version_bytes));
TVMVersion depVersion(version_bytes);
this->dependencies.push_back(std::pair<std::string,TVMVersion>(name, depVersion));
}
else if(strncmp(table_name,"RESO",4) == 0) //resources (using embed)
{
std::vector<uint8_t> data;
data.resize(tableLen);
Ensure(stream,data.data(), tableLen);
this->resources.push_back(data);
}
else if(strncmp(table_name,"CHKS",4) == 0) //chunks
{
size_t chunkCount = (size_t)EnsureInt(stream);
for(size_t j = 0; j < chunkCount; j++)
{
auto chunk = TFileChunk::Create(ls);
chunk->file = this;
size_t argCount = (size_t)EnsureInt(stream);
for(size_t k = 0; k < argCount; k++)
{
chunk->args.push_back(GetString(stream));
}
size_t len = (size_t)EnsureInt(stream);
chunk->code.resize(len);
Ensure(stream,chunk->code.data(),len);
//reader.ReadIntoBuffer(chunk->code);
this->chunks.push_back(chunk);
}
}
else if(strncmp(table_name,"FUNS",4) == 0) //functions
{
size_t funLength = (size_t)EnsureInt(stream);
for(size_t j = 0; j < funLength;j++)
{
std::vector<std::string> fnParts;
uint32_t fnPartsC = EnsureInt(stream);
for(uint32_t k = 0; k < fnPartsC; k++)
{
fnParts.push_back(GetString(stream));
}
uint32_t fnNumber = EnsureInt(stream);
this->functions.push_back(std::pair<std::vector<std::string>,uint32_t>(fnParts,fnNumber));
}
}
else if(strncmp(table_name,"STRS",4) == 0) //strings
{
size_t strsLen = (size_t)EnsureInt(stream);
for(size_t j = 0;j < strsLen;j++)
{
this->strings.push_back(EnsureString(stream));
}
}
else
{
Skip(stream,tableLen);
}
}
}
}

248
src/vm/gc.cpp Normal file
View File

@ -0,0 +1,248 @@
#include "CrossLang.hpp"
#include <chrono>
#include <iostream>
#include <unistd.h>
#if defined(CROSSLANG_ENABLE_SQLITE)
#include "../sqlite/sqlite3.h"
#if defined(GEKKO)
extern sqlite3_vfs *sqlite3_demovfs(void);
#endif
#endif
using namespace Tesses::Framework::Threading;
using namespace std::chrono;
namespace Tesses::CrossLang
{
bool GC::IsRunning()
{
bool run = this->running;
return run;
}
GC::GC()
{
#if defined(CROSSLANG_ENABLE_SQLITE)
sqlite3_initialize();
#if defined(GEKKO)
sqlite3_vfs_register(sqlite3_demovfs(),1);
#endif
#endif
}
TDictionary* CreateThread(GCList& ls, TCallable* callable,bool detached)
{
TDictionary* dict = TDictionary::Create(ls);
ThreadHandle* th = new ThreadHandle();
th->gc = ls.GetGC();
th->callable = callable;
th->hasInit=false;
th->hasReturned = false;
th->detached=detached;
ls.Add(th);
ls.GetGC()->Watch(th);
ls.GetGC()->BarrierBegin();
dict->SetValue("_internal", th);
dict->DeclareFunction(ls.GetGC(),"Join","Join thread",{},[th](GCList& _ls, std::vector<TObject> _args)-> TObject{
th->thrd->Join();
delete th->thrd;
if(th->hasReturned)
{
_ls.GetGC()->BarrierBegin();
auto v = th->returnValue;
_ls.Add(v);
_ls.GetGC()->BarrierEnd();
return v;
}
return Undefined();
});
dict->DeclareFunction(ls.GetGC(), "Detach","Detach thread",{},[th](GCList& _ls, std::vector<TObject> _args)-> TObject{
_ls.GetGC()->BarrierBegin();
th->detached=true;
_ls.GetGC()->BarrierEnd();
return Undefined();
});
dict->DeclareFunction(ls.GetGC(),"getFinished","Get whether thread has finished",{},[th](GCList& _ls, std::vector<TObject> _args)-> TObject{
return th->hasReturned;
});
ls.GetGC()->BarrierEnd();
th->thrd =new Thread([th]()->void {
GC* gc=th->gc;
GCList ls(gc);
ls.Add(th);
th->hasInit=true;
TObject cb = th->callable->Call(ls,{});
gc->BarrierBegin();
th->returnValue=cb;
gc->BarrierEnd();
th->hasReturned=true;
});
while(!th->hasInit);
return dict;
}
void GC::Start()
{
this->mtx=new Mutex();
this->running = true;
this->thrd = new Thread([this]()->void {
std::chrono::time_point<std::chrono::system_clock> last_frame, this_frame;
this_frame = system_clock::now();
last_frame = this_frame;
while(this->IsRunning())
{
this_frame = system_clock::now();
if((this_frame - last_frame) > 10s)
{
last_frame = this_frame;
this->Collect();
usleep(1000000);
}
}
GC::Collect();
});
}
bool GC::UsingNullThreads()
{
return false;
}
void GC::BarrierBegin()
{
this->mtx->Lock();
}
void GC::BarrierEnd()
{
this->mtx->Unlock();
}
void GC::Watch(TObject obj)
{
if(std::holds_alternative<THeapObjectHolder>(obj))
{
auto _item=std::get<THeapObjectHolder>(obj).obj;
this->BarrierBegin();
for(auto item : this->objects)
{
if(item == _item) {
this->BarrierEnd();
return;
}
}
this->objects.push_back(_item);
this->BarrierEnd();
}
}
void GC::Mark(TObject obj)
{
if(std::holds_alternative<THeapObjectHolder>(obj))
{
auto _item=std::get<THeapObjectHolder>(obj).obj;
_item->Mark();
}
}
void GC::Unwatch(TObject obj)
{
if(std::holds_alternative<THeapObjectHolder>(obj))
{
auto _item=std::get<THeapObjectHolder>(obj).obj;
this->BarrierBegin();
for(auto index = this->objects.begin();index<this->objects.end();index++)
{
if(*index == _item)
{
this->objects.erase(index);
continue;
}
}
this->BarrierEnd();
}
}
void GC::SetRoot(TObject obj)
{
if(std::holds_alternative<THeapObjectHolder>(obj))
{
auto _item=std::get<THeapObjectHolder>(obj).obj;
this->BarrierBegin();
for(auto item : this->roots)
{
if(item == _item) {
this->BarrierEnd();
return;
}
}
this->roots.push_back(_item);
this->BarrierEnd();
}
}
void GC::UnsetRoot(TObject obj)
{
if(std::holds_alternative<THeapObjectHolder>(obj))
{
auto _item=std::get<THeapObjectHolder>(obj).obj;
this->BarrierBegin();
for(auto index = this->roots.begin();index<this->roots.end();index++)
{
if(*index == _item)
{
this->roots.erase(index);
continue;
}
}
this->BarrierEnd();
}
}
GC::~GC()
{
GC::BarrierBegin();
this->roots.clear();
GC::BarrierEnd();
this->running=false;
this->thrd->Join();
delete this->thrd;
for(auto item : objects) delete item;
delete this->mtx;
}
void GC::Collect()
{
this->BarrierBegin();
for(auto item : this->objects)
{
item->marked=false;
}
for(auto item : this->roots)
{
item->Mark();
}
for(auto index = this->objects.begin();index < this->objects.end();index++)
{
THeapObject* o = *index;
if(!o->marked)
{
delete o;
this->objects.erase(index);
index--;
}
}
this->BarrierEnd();
}
};

73
src/vm/gclist.cpp Normal file
View File

@ -0,0 +1,73 @@
#include "CrossLang.hpp"
namespace Tesses::CrossLang
{
GCList::GCList(GC* gc)
{
gc->BarrierBegin();
this->gc = gc;
gc->SetRoot(this);
gc->BarrierEnd();
}
GCList::GCList(GC& gc)
{
gc.BarrierBegin();
this->gc = &gc;
gc.SetRoot(this);
gc.BarrierEnd();
}
GC* GCList::GetGC()
{
return this->gc;
}
void GCList::Remove(TObject obj)
{
if(std::holds_alternative<THeapObjectHolder>(obj))
{
auto _item=std::get<THeapObjectHolder>(obj).obj;
this->gc->BarrierBegin();
for(auto index = this->items.begin();index<this->items.end();index++)
{
if(*index == _item)
{
this->items.erase(index);
continue;
}
}
this->gc->BarrierEnd();
}
}
void GCList::Add(TObject obj)
{
if(std::holds_alternative<THeapObjectHolder>(obj))
{
auto _item=std::get<THeapObjectHolder>(obj).obj;
this->gc->BarrierBegin();
for(auto item : this->items)
{
if(item == _item) {
this->gc->BarrierEnd();
return;
}
}
this->items.push_back(_item);
this->gc->BarrierEnd();
}
}
void GCList::Mark()
{
this->marked=true;
for(auto item : this->items)
{
item->Mark();
}
}
GCList::~GCList()
{
gc->BarrierBegin();
this->gc->UnsetRoot(this);
gc->BarrierEnd();
}
}

4200
src/vm/vm.cpp Normal file

File diff suppressed because it is too large Load Diff