From 28eff630c63fee1e8cec7a4128939b130425d905 Mon Sep 17 00:00:00 2001 From: Mike Nolan Date: Fri, 1 May 2026 11:28:36 -0500 Subject: [PATCH] Rework for git.tesses.org, GC* is std::shared_ptr maybe will fix crash during exit --- cmake/sources.cmake | 1 - include/CrossLang.hpp | 53 +------ src/runtime_methods/io.cpp | 12 +- src/runtime_methods/std.cpp | 29 ++-- src/types/relativefs.cpp | 275 ----------------------------------- src/vm/bc/executemethod2.cpp | 46 +++++- src/vm/bc/getfield.cpp | 23 +++ src/vm/bc/setfield.cpp | 28 ++++ 8 files changed, 123 insertions(+), 344 deletions(-) delete mode 100644 src/types/relativefs.cpp diff --git a/cmake/sources.cmake b/cmake/sources.cmake index bf5259d..d52ee02 100644 --- a/cmake/sources.cmake +++ b/cmake/sources.cmake @@ -49,7 +49,6 @@ src/types/streamheapobject.cpp src/types/class.cpp src/types/classenvironment.cpp src/types/random.cpp -src/types/relativefs.cpp src/vm/filereader.cpp src/vm/gc.cpp src/vm/gclist.cpp diff --git a/include/CrossLang.hpp b/include/CrossLang.hpp index 6bafaac..f8ae5d3 100644 --- a/include/CrossLang.hpp +++ b/include/CrossLang.hpp @@ -41,52 +41,7 @@ namespace Tesses::CrossLang { constexpr std::string_view VMName = "CrossLangVM"; constexpr std::string_view VMHowToGet = "https://crosslang.tesseslanguage.com/"; - class RelativeFilesystem : public Tesses::Framework::Filesystem::VFS - { - private: - std::shared_ptr vfs; - Tesses::Framework::Filesystem::VFSPath path; - Tesses::Framework::Threading::Mutex mtx; - - public: - RelativeFilesystem(std::shared_ptr vfs, Tesses::Framework::Filesystem::VFSPath working); - std::shared_ptr OpenFile(Tesses::Framework::Filesystem::VFSPath path, std::string mode); - - void CreateDirectory(Tesses::Framework::Filesystem::VFSPath path); - void DeleteDirectory(Tesses::Framework::Filesystem::VFSPath path); - - - void DeleteFile(Tesses::Framework::Filesystem::VFSPath path); - void CreateSymlink(Tesses::Framework::Filesystem::VFSPath existingFile, Tesses::Framework::Filesystem::VFSPath symlinkFile); - Tesses::Framework::Filesystem::VFSPathEnumerator EnumeratePaths(Tesses::Framework::Filesystem::VFSPath path); - void CreateHardlink(Tesses::Framework::Filesystem::VFSPath existingFile, Tesses::Framework::Filesystem::VFSPath newName); - - void MoveFile(Tesses::Framework::Filesystem::VFSPath src, Tesses::Framework::Filesystem::VFSPath dest); - - void MoveDirectory(Tesses::Framework::Filesystem::VFSPath src, Tesses::Framework::Filesystem::VFSPath dest); - Tesses::Framework::Filesystem::VFSPath ReadLink(Tesses::Framework::Filesystem::VFSPath path); - std::string VFSPathToSystem(Tesses::Framework::Filesystem::VFSPath path); - Tesses::Framework::Filesystem::VFSPath SystemToVFSPath(std::string path); - - void SetDate(Tesses::Framework::Filesystem::VFSPath path, Tesses::Framework::Date::DateTime lastWrite, Tesses::Framework::Date::DateTime lastAccess); - bool Stat(Tesses::Framework::Filesystem::VFSPath path, Tesses::Framework::Filesystem::StatData& stat); - bool StatVFS(Tesses::Framework::Filesystem::VFSPath path, Tesses::Framework::Filesystem::StatVFSData& vfsData); - - void Chmod(Tesses::Framework::Filesystem::VFSPath path, uint32_t mode); - void Chown(Tesses::Framework::Filesystem::VFSPath path, uint32_t uid, uint32_t gid); - - void Lock(Tesses::Framework::Filesystem::VFSPath path); - void Unlock(Tesses::Framework::Filesystem::VFSPath path); - - Tesses::Framework::Filesystem::FIFOCreationResult CreateFIFO(Tesses::Framework::Filesystem::VFSPath path, uint32_t mod); - Tesses::Framework::Filesystem::VFSPath GetWorking(); - void SetWorking(Tesses::Framework::Filesystem::VFSPath working); - std::shared_ptr GetVFS(); - protected: - std::shared_ptr CreateWatcher(std::shared_ptr vfs, Tesses::Framework::Filesystem::VFSPath path); - - }; - + /** * @brief Escape a crosslang string (for generating source code) * @@ -1822,7 +1777,7 @@ class GC : public std::enable_shared_from_this { public: EnvironmentPermissions(); Tesses::Framework::Filesystem::VFSPath sqliteOffsetPath; - std::shared_ptr localfs; + std::shared_ptr localfs; bool canRegisterEverything; bool canRegisterConsole; bool canRegisterIO; @@ -1886,10 +1841,10 @@ class GC : public std::enable_shared_from_this { static void RegisterHelpers(std::shared_ptr gc, TRootEnvironment* env); static void RegisterUuid(std::shared_ptr gc, TRootEnvironment* env); public: - static void RegisterStd(std::shared_ptr gc, TRootEnvironment* env, std::shared_ptr localfs); + static void RegisterStd(std::shared_ptr gc, TRootEnvironment* env, std::shared_ptr localfs); static void RegisterStd(std::shared_ptr gc, TRootEnvironment* env); static void RegisterConsole(std::shared_ptr gc,TRootEnvironment* env); - static void RegisterIO(std::shared_ptr gc,TRootEnvironment* env, std::shared_ptr local); + static void RegisterIO(std::shared_ptr gc,TRootEnvironment* env, std::shared_ptr local); static void RegisterIO(std::shared_ptr gc,TRootEnvironment* env, bool enableLocalFS=true); static void RegisterNet(std::shared_ptr gc, TRootEnvironment* env); static void RegisterSqlite(std::shared_ptr gc,TRootEnvironment* env); diff --git a/src/runtime_methods/io.cpp b/src/runtime_methods/io.cpp index 9951962..0daa9a4 100644 --- a/src/runtime_methods/io.cpp +++ b/src/runtime_methods/io.cpp @@ -264,14 +264,14 @@ namespace Tesses::CrossLang { if(enable) { - RegisterIO(gc,env,std::make_shared(Tesses::Framework::Filesystem::LocalFS, Tesses::Framework::Filesystem::VFSPath::GetAbsoluteCurrentDirectory())); + RegisterIO(gc,env,std::make_shared(Tesses::Framework::Filesystem::LocalFS, Tesses::Framework::Filesystem::VFSPath::GetAbsoluteCurrentDirectory())); } else { RegisterIO(gc,env,nullptr); } } - void TStd::RegisterIO(std::shared_ptr gc,TRootEnvironment* env,std::shared_ptr fs) + void TStd::RegisterIO(std::shared_ptr gc,TRootEnvironment* env,std::shared_ptr fs) { env->permissions.canRegisterIO=true; @@ -332,7 +332,13 @@ namespace Tesses::CrossLang Tesses::Framework::Filesystem::VFSPath path; if(GetArgumentAsPath(args,0,path)) { - fs->SetWorking(path); + if(path.relative) + { + fs->SetWorking(path.MakeAbsolute(fs->GetWorking())); + } + else { + fs->SetWorking(path); + } } return path; }); diff --git a/src/runtime_methods/std.cpp b/src/runtime_methods/std.cpp index 9dd28d4..eb1fc5f 100644 --- a/src/runtime_methods/std.cpp +++ b/src/runtime_methods/std.cpp @@ -864,20 +864,12 @@ namespace Tesses::CrossLang auto vfs = std::get>(_obj); if(vfs != nullptr) { - auto rfs = std::dynamic_pointer_cast(vfs); + auto rfs = std::dynamic_pointer_cast(vfs); auto localVFS = std::dynamic_pointer_cast(vfs); auto mountableVFS = std::dynamic_pointer_cast(vfs); auto subFS = std::dynamic_pointer_cast(vfs); auto tempFS = std::dynamic_pointer_cast(vfs); - if(rfs) - { - auto fs=rfs->GetVFS(); - if(fs) - { - return GetObjectTypeString(fs); - } - return "RelativeFilesystem"; - } + if(rfs) return "RelativeFilesystem"; if(localVFS != nullptr) return "LocalFilesystem"; if(subFS != nullptr) return "SubdirFilesystem"; if(mountableVFS != nullptr) return "MountableFilesystem"; @@ -1301,6 +1293,17 @@ namespace Tesses::CrossLang return Tesses::Framework::TF_Timer(empty, 1000L, false); } + static TObject New_RelativeFilesystem(GCList& ls, std::vector args) + { + std::shared_ptr vfs; + Tesses::Framework::Filesystem::VFSPath path; + if(GetArgument(args,0,vfs) && GetArgumentAsPath(args,1,path) && !path.relative) + { + return std::make_shared(vfs,path); + } + return nullptr; + } + void TStd::RegisterRoot(std::shared_ptr gc, TRootEnvironment* env) { GCList ls(gc); @@ -1363,6 +1366,7 @@ namespace Tesses::CrossLang newTypes->DeclareFunction(gc, "SubdirFilesystem","Create a subdir filesystem",{"fs","subdir"}, New_SubdirFilesystem); newTypes->DeclareFunction(gc, "MemoryStream","Create a memory stream",{"writable"}, New_MemoryStream); newTypes->DeclareFunction(gc, "Filesystem","Create filesystem", {"fs"},New_Filesystem); + newTypes->DeclareFunction(gc, "RelativeFilesystem", "Create relativefs",{"parent", "absoluteDir"}, New_RelativeFilesystem); newTypes->DeclareFunction(gc, "TempFS","Create a temp directory",{"",""}, New_TempFS); newTypes->DeclareFunction(gc, "Timer", "Create a timer",{"$cb","$interval","$enabled"}, New_Timer); newTypes->DeclareFunction(gc, "Stream","Create stream", {"strm"},New_Stream); @@ -1519,14 +1523,15 @@ namespace Tesses::CrossLang } void TStd::RegisterStd(std::shared_ptr gc, TRootEnvironment* env) { - RegisterStd(gc, env, std::make_shared(Tesses::Framework::Filesystem::LocalFS, Tesses::Framework::Filesystem::VFSPath::GetAbsoluteCurrentDirectory())); + RegisterStd(gc, env, std::make_shared(Tesses::Framework::Filesystem::LocalFS, Tesses::Framework::Filesystem::VFSPath::GetAbsoluteCurrentDirectory())); } - void TStd::RegisterStd(std::shared_ptr gc, TRootEnvironment* env,std::shared_ptr localfs) + void TStd::RegisterStd(std::shared_ptr gc, TRootEnvironment* env,std::shared_ptr localfs) { env->permissions.canRegisterEverything=true; RegisterEnv(gc, env); RegisterRoot(gc,env); RegisterPath(gc,env); + RegisterConsole(gc, env); RegisterIO(gc, env, localfs); RegisterNet(gc, env); diff --git a/src/types/relativefs.cpp b/src/types/relativefs.cpp deleted file mode 100644 index 605a743..0000000 --- a/src/types/relativefs.cpp +++ /dev/null @@ -1,275 +0,0 @@ -#include "CrossLang.hpp" - -namespace Tesses::CrossLang -{ - RelativeFilesystem::RelativeFilesystem(std::shared_ptr vfs, Tesses::Framework::Filesystem::VFSPath working) - { - this->vfs = vfs; - this->path = working; - } - std::shared_ptr RelativeFilesystem::OpenFile(Tesses::Framework::Filesystem::VFSPath path, std::string mode) - { - if(path.relative) - { - return this->vfs->OpenFile(path.MakeAbsolute(GetWorking()), mode); - } - else - { - return this->vfs->OpenFile(path,mode); - } - } - - void RelativeFilesystem::CreateDirectory(Tesses::Framework::Filesystem::VFSPath path) - { - if(path.relative) - { - this->vfs->CreateDirectory(path.MakeAbsolute(GetWorking())); - } - else - { - this->vfs->CreateDirectory(path); - } - } - void RelativeFilesystem::DeleteDirectory(Tesses::Framework::Filesystem::VFSPath path) - { - if(path.relative) - { - this->vfs->DeleteDirectory(path.MakeAbsolute(GetWorking())); - } - else - { - this->vfs->DeleteDirectory(path); - } - } - - - void RelativeFilesystem::DeleteFile(Tesses::Framework::Filesystem::VFSPath path) - { - if(path.relative) - { - this->vfs->DeleteFile(path.MakeAbsolute(GetWorking())); - } - else - { - this->vfs->DeleteFile(path); - } - } - void RelativeFilesystem::CreateSymlink(Tesses::Framework::Filesystem::VFSPath existingFile, Tesses::Framework::Filesystem::VFSPath symlinkFile) - { - if(symlinkFile.relative) - { - this->vfs->CreateSymlink(existingFile,symlinkFile.MakeAbsolute(GetWorking())); - } - else - { - this->vfs->CreateSymlink(existingFile, symlinkFile); - } - } - Tesses::Framework::Filesystem::VFSPathEnumerator RelativeFilesystem::EnumeratePaths(Tesses::Framework::Filesystem::VFSPath path) - { - if(path.relative) - { - return this->vfs->EnumeratePaths(path.MakeAbsolute(GetWorking())); - } - else - { - return this->vfs->EnumeratePaths(path); - } - } - void RelativeFilesystem::CreateHardlink(Tesses::Framework::Filesystem::VFSPath existingFile, Tesses::Framework::Filesystem::VFSPath newName) - { - auto working = GetWorking(); - if(existingFile.relative) - { - existingFile = existingFile.MakeAbsolute(working); - existingFile = existingFile.CollapseRelativeParents(); - } - - if(newName.relative) - { - newName = newName.MakeAbsolute(working); - newName = newName.CollapseRelativeParents(); - } - - this->vfs->CreateHardlink(existingFile, newName); - } - - void RelativeFilesystem::MoveFile(Tesses::Framework::Filesystem::VFSPath src, Tesses::Framework::Filesystem::VFSPath dest) - { - auto working = GetWorking(); - if(src.relative) - { - src = src.MakeAbsolute(working); - src = src.CollapseRelativeParents(); - } - - if(dest.relative) - { - dest = dest.MakeAbsolute(working); - dest = dest.CollapseRelativeParents(); - } - - this->vfs->MoveFile(src, dest); - } - - void RelativeFilesystem::MoveDirectory(Tesses::Framework::Filesystem::VFSPath src, Tesses::Framework::Filesystem::VFSPath dest) - { - - auto working = GetWorking(); - if(src.relative) - { - src = src.MakeAbsolute(working); - src = src.CollapseRelativeParents(); - } - - if(dest.relative) - { - dest = dest.MakeAbsolute(working); - dest = dest.CollapseRelativeParents(); - } - - this->vfs->MoveFile(src, dest); - } - Tesses::Framework::Filesystem::VFSPath RelativeFilesystem::ReadLink(Tesses::Framework::Filesystem::VFSPath path) - { - if(path.relative) - { - return this->vfs->ReadLink(path.MakeAbsolute(GetWorking())); - } - else - { - return this->vfs->ReadLink(path); - } - } - std::string RelativeFilesystem::VFSPathToSystem(Tesses::Framework::Filesystem::VFSPath path) - { - return this->vfs->VFSPathToSystem(path); - } - Tesses::Framework::Filesystem::VFSPath RelativeFilesystem::SystemToVFSPath(std::string path) - { - return this->vfs->SystemToVFSPath(path); - } - - void RelativeFilesystem::SetDate(Tesses::Framework::Filesystem::VFSPath path, Tesses::Framework::Date::DateTime lastWrite, Tesses::Framework::Date::DateTime lastAccess) - { - if(path.relative) - { - this->vfs->SetDate(path.MakeAbsolute(GetWorking()), lastWrite,lastAccess); - } - else - { - this->vfs->SetDate(path,lastWrite,lastAccess); - } - } - bool RelativeFilesystem::Stat(Tesses::Framework::Filesystem::VFSPath path, Tesses::Framework::Filesystem::StatData& stat) - { - if(path.relative) - { - return this->vfs->Stat(path.MakeAbsolute(GetWorking()), stat); - } - else - { - return this->vfs->Stat(path,stat); - } - } - bool RelativeFilesystem::StatVFS(Tesses::Framework::Filesystem::VFSPath path, Tesses::Framework::Filesystem::StatVFSData& vfsData) - { - if(path.relative) - { - return this->vfs->StatVFS(path.MakeAbsolute(GetWorking()), vfsData); - } - else - { - return this->vfs->StatVFS(path,vfsData); - } - } - - void RelativeFilesystem::Chmod(Tesses::Framework::Filesystem::VFSPath path, uint32_t mode) - { - if(path.relative) - { - this->vfs->Chmod(path.MakeAbsolute(GetWorking()), mode); - } - else - { - this->vfs->Chmod(path,mode); - } - } - void RelativeFilesystem::Chown(Tesses::Framework::Filesystem::VFSPath path, uint32_t uid, uint32_t gid) - { - if(path.relative) - { - this->vfs->Chown(path.MakeAbsolute(GetWorking()), uid, gid); - } - else - { - this->vfs->Chown(path,uid,gid); - } - } - - void RelativeFilesystem::Lock(Tesses::Framework::Filesystem::VFSPath path) - { - if(path.relative) - { - this->vfs->Lock(path.MakeAbsolute(GetWorking())); - } - else - { - this->vfs->Lock(path); - } - } - void RelativeFilesystem::Unlock(Tesses::Framework::Filesystem::VFSPath path) - { - if(path.relative) - { - this->vfs->Unlock(path.MakeAbsolute(GetWorking())); - } - else - { - this->vfs->Unlock(path); - } - } - - Tesses::Framework::Filesystem::FIFOCreationResult RelativeFilesystem::CreateFIFO(Tesses::Framework::Filesystem::VFSPath path, uint32_t mod) - { - if(path.relative) - { - return this->vfs->CreateFIFO(path.MakeAbsolute(GetWorking()), mod); - } - else - { - return this->vfs->CreateFIFO(path, mod); - } - } - Tesses::Framework::Filesystem::VFSPath RelativeFilesystem::GetWorking() - { - mtx.Lock(); - auto path = this->path; - mtx.Unlock(); - return path; - } - void RelativeFilesystem::SetWorking(Tesses::Framework::Filesystem::VFSPath working) - { - mtx.Lock(); - this->path = working; - mtx.Unlock(); - } - std::shared_ptr RelativeFilesystem::GetVFS() - { - return this->vfs; - } - - - std::shared_ptr RelativeFilesystem::CreateWatcher(std::shared_ptr vfs, Tesses::Framework::Filesystem::VFSPath path) - { - if(path.relative) - { - return Tesses::Framework::Filesystem::FSWatcher::Create(vfs, path.MakeAbsolute(GetWorking())); - } - else - { - return Tesses::Framework::Filesystem::FSWatcher::Create(vfs,path); - } - } - -} \ No newline at end of file diff --git a/src/vm/bc/executemethod2.cpp b/src/vm/bc/executemethod2.cpp index f6061a7..57fdcc7 100644 --- a/src/vm/bc/executemethod2.cpp +++ b/src/vm/bc/executemethod2.cpp @@ -1677,6 +1677,8 @@ namespace Tesses::CrossLang { auto myvfs = std::dynamic_pointer_cast(vfs); auto mountable = std::dynamic_pointer_cast(vfs); + + if(myvfs != nullptr) { TDictionary* dict2; @@ -2284,10 +2286,36 @@ namespace Tesses::CrossLang { } if(key == "RegisterEverything") { - if(myEnv->permissions.canRegisterEverything && myEnv->permissions.localfs) + if(rootEnv->permissions.locked) { - TStd::RegisterStd(gc, rootEnv, std::make_shared(myEnv->permissions.localfs->GetVFS(),myEnv->permissions.localfs->GetWorking())); + cse.back()->Push(gc,nullptr); + return false; + } + + if(myEnv->permissions.canRegisterEverything) + { + std::shared_ptr vfs; + if(GetArgument(args,0, vfs)) + { + + auto rfs = std::dynamic_pointer_cast(vfs); + if(rfs) + { + TStd::RegisterStd(gc,rootEnv, rfs); + cse.back()->Push(gc,nullptr); + return false; + } + } + if(myEnv->permissions.localfs) + TStd::RegisterStd(gc, rootEnv, std::make_shared(myEnv->permissions.localfs->GetVFS(),myEnv->permissions.localfs->GetWorking())); + else + TStd::RegisterStd(gc, rootEnv, nullptr); + + + + cse.back()->Push(gc,nullptr); + return false; } else { @@ -2308,7 +2336,7 @@ namespace Tesses::CrossLang { { if(myEnv->permissions.localfs) { - TStd::RegisterIO(gc, rootEnv, std::make_shared(myEnv->permissions.localfs->GetVFS(),myEnv->permissions.localfs->GetWorking())); + TStd::RegisterIO(gc, rootEnv, std::make_shared(myEnv->permissions.localfs->GetVFS(),myEnv->permissions.localfs->GetWorking())); } else { TStd::RegisterIO(gc, rootEnv, nullptr); @@ -2388,6 +2416,8 @@ namespace Tesses::CrossLang { } if(key == "RegisterIO") { + std::shared_ptr vfs; + bool r; if(GetArgument(args,0,r)) { @@ -2395,13 +2425,21 @@ namespace Tesses::CrossLang { { if(myEnv->permissions.localfs) { - TStd::RegisterIO(gc, rootEnv, std::make_shared(myEnv->permissions.localfs->GetVFS(),myEnv->permissions.localfs->GetWorking())); + TStd::RegisterIO(gc, rootEnv, std::make_shared(myEnv->permissions.localfs->GetVFS(),myEnv->permissions.localfs->GetWorking())); } else { TStd::RegisterIO(gc, rootEnv, nullptr); } } } + if(GetArgument(args,0,vfs)) + { + auto rfs = std::dynamic_pointer_cast(vfs); + if(rfs && (myEnv->permissions.canRegisterEverything || myEnv->permissions.canRegisterIO) && !rootEnv->permissions.locked) + { + TStd::RegisterIO(gc, rootEnv, rfs); + } + } cse.back()->Push(gc,nullptr); return false; diff --git a/src/vm/bc/getfield.cpp b/src/vm/bc/getfield.cpp index c8abf81..02e8cdd 100644 --- a/src/vm/bc/getfield.cpp +++ b/src/vm/bc/getfield.cpp @@ -70,6 +70,29 @@ namespace Tesses::CrossLang { { auto vfs = std::get>(instance); auto tmpFS=std::dynamic_pointer_cast(vfs); + auto relative = std::dynamic_pointer_cast(vfs); + auto myFS = std::dynamic_pointer_cast(vfs); + if(relative) + { + if(key == "Working") + { + cse.back()->Push(gc, relative->GetWorking()); + return false; + } + if(key == "Filesystem") + { + cse.back()->Push(gc, relative->GetVFS()); + return false; + } + } + if(myFS) + { + if(key == "Inner") + { + cse.back()->Push(gc, myFS->obj); + return false; + } + } if(tmpFS) { if(key == "TempDirectoryName") diff --git a/src/vm/bc/setfield.cpp b/src/vm/bc/setfield.cpp index 21061f7..ef2bb21 100644 --- a/src/vm/bc/setfield.cpp +++ b/src/vm/bc/setfield.cpp @@ -116,6 +116,34 @@ namespace Tesses::CrossLang { cse.back()->Push(gc,Undefined()); return false; } + if(std::holds_alternative>(instance)) + { + + auto vfs = std::get>(instance); + auto relative = std::dynamic_pointer_cast(vfs); + + if(relative) + { + if(key == "Working") + { + Tesses::Framework::Filesystem::VFSPath path; + if(GetObjectAsPath(value,path)) + { + if(path.relative) + { + relative->SetWorking(path.MakeAbsolute(relative->GetWorking())); + } + else { + relative->SetWorking(path); + } + } + } + } + + + cse.back()->Push(gc,Undefined()); + return false; + } if(std::holds_alternative>(instance)) { auto svr = std::get>(instance);