From 4042d99ca03353f6ee9dabd1e6b059027dccca93 Mon Sep 17 00:00:00 2001 From: xLuxy <67131061+xLuxy@users.noreply.github.com> Date: Thu, 6 Jun 2024 10:51:13 +0200 Subject: [PATCH] fix: Fix jsv2b runtime --- module/src/main.cpp | 5 ++- module/src/runtime.cpp | 2 +- module/src/runtimev2.cpp | 96 ++++++++++++++++++++++++++++++++++++++++ module/src/runtimev2.h | 36 +++++++++++++++ 4 files changed, 137 insertions(+), 2 deletions(-) create mode 100644 module/src/runtimev2.cpp create mode 100644 module/src/runtimev2.h diff --git a/module/src/main.cpp b/module/src/main.cpp index 07ef7a3..13faa6e 100644 --- a/module/src/main.cpp +++ b/module/src/main.cpp @@ -2,6 +2,7 @@ #include "version/version.h" #include "Log.h" #include "runtime.h" +#include "runtimev2.h" static void CommandHandler(const std::vector& args) { @@ -35,7 +36,9 @@ EXPORT bool altMain(alt::ICore* core) auto& runtime = JSBytecodeRuntime::Instance(); core->RegisterScriptRuntime("jsb", &runtime); - core->RegisterScriptRuntime("jsv2b", &runtime); + + auto& runtimeV2 = JSBytecodeRuntimeV2::Instance(); + core->RegisterScriptRuntime("jsv2b", &runtimeV2); core->SubscribeCommand("jsb-module", &CommandHandler); diff --git a/module/src/runtime.cpp b/module/src/runtime.cpp index f481a0e..8113453 100644 --- a/module/src/runtime.cpp +++ b/module/src/runtime.cpp @@ -29,7 +29,7 @@ void JSBytecodeRuntime::ProcessClientFile(alt::IResource* resource, alt::IPackag Config::Value::ValuePtr config = resource->GetConfig(); // Get ignored files - std::vector ignoredModules = { "alt", "alt-client", "natives", "alt-worker", "alt-shared", "@altv/client", "@altv/server", "@altv/shared", "@altv/natives" }; + std::vector ignoredModules = { "alt", "alt-client", "natives", "alt-worker", "alt-shared" }; Config::Value::ValuePtr ignoredFiles = config->Get("ignored-files"); if(ignoredFiles->IsList()) { diff --git a/module/src/runtimev2.cpp b/module/src/runtimev2.cpp new file mode 100644 index 0000000..03874b7 --- /dev/null +++ b/module/src/runtimev2.cpp @@ -0,0 +1,96 @@ +#include "runtimev2.h" +#include "Log.h" +#include "compiler.h" +#include "package.h" +#include "logger.h" + +JSBytecodeRuntimeV2::JSBytecodeRuntimeV2() +{ + v8::V8::SetFlagsFromString("--harmony-import-assertions --short-builtin-calls --no-lazy --no-flush-bytecode --no-enable-lazy-source-positions"); + platform = v8::platform::NewDefaultPlatform(); + v8::V8::InitializePlatform(platform.get()); + v8::V8::Initialize(); + + create_params.array_buffer_allocator = v8::ArrayBuffer::Allocator::NewDefaultAllocator(); + + isolate = v8::Isolate::New(create_params); +} + +void JSBytecodeRuntimeV2::ProcessClientFile(alt::IResource* resource, alt::IPackage* package) +{ + v8::Isolate::Scope isolateScope(isolate); + v8::HandleScope handleScope(isolate); + + // Set up compiler + alt::IPackage* resourcePackage = resource->GetPackage(); + Package compilerPackage(package, resourcePackage, resource); + Logger compilerLogger; + BytecodeCompiler::Compiler compiler(isolate, &compilerPackage, &compilerLogger); + + Config::Value::ValuePtr config = resource->GetConfig(); + // Get ignored files + std::vector ignoredModules = { "alt", "alt-client", "natives", "alt-worker", "alt-shared", "@altv/client", "@altv/server", "@altv/shared", "@altv/natives" }; + Config::Value::ValuePtr ignoredFiles = config->Get("ignored-files"); + if(ignoredFiles->IsList()) + { + Config::Value::List list = ignoredFiles->As(); + ignoredModules.reserve(ignoredModules.size() + list.size()); + for(auto& item : list) + { + if(item->IsString()) ignoredModules.push_back(item->As()); + } + } + compiler.SetIgnoredModules(ignoredModules); + + // Compile client main file + bool result = compiler.CompileModule(resource->GetClientMain()); + if(!result) return; + + // Compile the extra files + Config::Value::ValuePtr extraCompileFiles = config->Get("extra-compile-files"); + if(extraCompileFiles->IsList()) + { + Config::Value::List list = extraCompileFiles->As(); + std::vector extraFilePatterns; + extraFilePatterns.reserve(list.size()); + for(auto& item : list) + { + if(item->IsString()) extraFilePatterns.push_back(item->As()); + } + + std::set files = resource->GetMatchedFiles(extraFilePatterns); + for(const std::string& file : files) + { + bool result = compiler.CompileModule(file, false); + if(!result) return; + } + } + + // Write all other files normally + const std::vector& clientFiles = resource->GetClientFiles(); + const std::vector& compiledFiles = compiler.GetCompiledFiles(); + for(const std::string& clientFile : clientFiles) + { + // Check if the file is compiled, then we don't want to overwrite it + if(std::find(compiledFiles.begin(), compiledFiles.end(), clientFile) != compiledFiles.end()) continue; + + // Open the file from the resource package and read the content + alt::IPackage::File* file = resourcePackage->OpenFile(clientFile); + size_t fileSize = resourcePackage->GetFileSize(file); + std::string buffer; + buffer.resize(fileSize); + resourcePackage->ReadFile(file, buffer.data(), buffer.size()); + resourcePackage->CloseFile(file); + + // Write the file content into the client package + alt::IPackage::File* clientPkgFile = package->OpenFile(clientFile); + package->WriteFile(clientPkgFile, buffer.data(), buffer.size()); + package->CloseFile(clientPkgFile); + } +} + +bool JSBytecodeRuntimeV2::GetProcessClientType(std::string& clientType) +{ + clientType = "jsv2b"; + return true; +} diff --git a/module/src/runtimev2.h b/module/src/runtimev2.h new file mode 100644 index 0000000..0e4c4a0 --- /dev/null +++ b/module/src/runtimev2.h @@ -0,0 +1,36 @@ +#pragma once + +#include "SDK.h" +#include "v8.h" +#include "libplatform/libplatform.h" + +class JSBytecodeRuntimeV2 : public alt::IScriptRuntime +{ + v8::Isolate* isolate; + v8::Isolate::CreateParams create_params; + std::unique_ptr platform; + +public: + JSBytecodeRuntimeV2(); + + bool GetProcessClientType(std::string& clientType) override; + void ProcessClientFile(alt::IResource* resource, alt::IPackage* clientPackage) override; + + v8::Isolate* GetIsolate() + { + return isolate; + } + + alt::IResource::Impl* CreateImpl(alt::IResource* resource) override + { + return nullptr; + } + + void DestroyImpl(alt::IResource::Impl* impl) override {} + + static JSBytecodeRuntimeV2& Instance() + { + static JSBytecodeRuntimeV2 runtime; + return runtime; + } +};