Skip to content

Commit

Permalink
Merge branch 'dev' into rc
Browse files Browse the repository at this point in the history
  • Loading branch information
C0kkie committed Jul 13, 2024
2 parents ceb119f + 18a2bb5 commit e01fedc
Show file tree
Hide file tree
Showing 9 changed files with 170 additions and 32 deletions.
8 changes: 6 additions & 2 deletions compiler/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

using namespace BytecodeCompiler;

bool Compiler::CompileModule(const std::string& fileName, bool compileDependencies)
bool Compiler::CompileModule(const std::string& fileName, bool compileDependencies, bool verbose)
{
// Read the file
if(!package->FileExists(fileName))
Expand Down Expand Up @@ -61,7 +61,11 @@ bool Compiler::CompileModule(const std::string& fileName, bool compileDependenci
cache->buffer_policy = v8::ScriptCompiler::CachedData::BufferPolicy::BufferOwned;
delete cache;

logger->Log("Converted file to bytecode: " + logger->GetHighlightColor() + fileName);
if (verbose)
{
logger->Log("Converted file to bytecode: " + logger->GetHighlightColor() + fileName);
}

compiledFiles.push_back(fileName);

// Compile all dependencies
Expand Down
2 changes: 1 addition & 1 deletion compiler/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ namespace BytecodeCompiler
return magicBytes;
}

bool CompileModule(const std::string& fileName, bool compileDependencies = true);
bool CompileModule(const std::string& fileName, bool compileDependencies = true, bool verbose = false);

bool IsBytecodeFile(void* buffer, size_t size);

Expand Down
2 changes: 1 addition & 1 deletion deps/cpp-sdk
Submodule cpp-sdk updated 3 files
+3 −0 ICore.h
+32 −20 types/MValue.h
+20 −0 types/Metric.h
34 changes: 34 additions & 0 deletions module/src/CScriptRuntimeInfo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#pragma once

#include "v8.h"
#include <libplatform/libplatform.h>

class CScriptRuntimeInfo
{
private:
v8::Isolate* isolate;
std::unique_ptr<v8::Platform> platform;

public:
v8::Isolate* GetIsolate() { return isolate; }

void Instanciate()
{
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();

auto createParams = v8::Isolate::CreateParams{};
createParams.array_buffer_allocator = v8::ArrayBuffer::Allocator::NewDefaultAllocator();

isolate = v8::Isolate::New(createParams);
}

static CScriptRuntimeInfo& Instance()
{
static CScriptRuntimeInfo runtimeInfo;
return runtimeInfo;
}
};
8 changes: 6 additions & 2 deletions module/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include "CScriptRuntimeInfo.h"
#include "SDK.h"
#include "version/version.h"
#include "Log.h"
#include "runtime.h"
#include "runtimev2.h"

static void CommandHandler(const std::vector<std::string>& args)
{
Expand Down Expand Up @@ -33,8 +35,10 @@ EXPORT bool altMain(alt::ICore* core)
{
alt::ICore::SetInstance(core);

auto& runtime = JSBytecodeRuntime::Instance();
core->RegisterScriptRuntime("jsb", &runtime);
CScriptRuntimeInfo::Instance().Instanciate();

core->RegisterScriptRuntime("jsb", &JSBytecodeRuntime::Instance());
core->RegisterScriptRuntime("jsv2b", &JSBytecodeRuntimeV2::Instance());

core->SubscribeCommand("jsb-module", &CommandHandler);

Expand Down
22 changes: 8 additions & 14 deletions module/src/runtime.cpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
#include "runtime.h"
#include "Log.h"
#include "compiler.h"
#include "CScriptRuntimeInfo.h"
#include "package.h"
#include "logger.h"

JSBytecodeRuntime::JSBytecodeRuntime()
{
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 JSBytecodeRuntime::ProcessClientFile(alt::IResource* resource, alt::IPackage* package)
{
v8::Isolate* isolate = CScriptRuntimeInfo::Instance().GetIsolate();
v8::Isolate::Scope isolateScope(isolate);
v8::HandleScope handleScope(isolate);

Expand All @@ -31,6 +21,8 @@ void JSBytecodeRuntime::ProcessClientFile(alt::IResource* resource, alt::IPackag
// Get ignored files
std::vector<std::string> ignoredModules = { "alt", "alt-client", "natives", "alt-worker", "alt-shared" };
Config::Value::ValuePtr ignoredFiles = config->Get("ignored-files");
Config::Value::Bool verboseLogging = config->Get("verbose")->AsBool(false);

if(ignoredFiles->IsList())
{
Config::Value::List list = ignoredFiles->As<Config::Value::List>();
Expand All @@ -43,7 +35,7 @@ void JSBytecodeRuntime::ProcessClientFile(alt::IResource* resource, alt::IPackag
compiler.SetIgnoredModules(ignoredModules);

// Compile client main file
bool result = compiler.CompileModule(resource->GetClientMain());
bool result = compiler.CompileModule(resource->GetClientMain(), true, verboseLogging);
if(!result) return;

// Compile the extra files
Expand All @@ -61,7 +53,7 @@ void JSBytecodeRuntime::ProcessClientFile(alt::IResource* resource, alt::IPackag
std::set<std::string> files = resource->GetMatchedFiles(extraFilePatterns);
for(const std::string& file : files)
{
bool result = compiler.CompileModule(file, false);
bool result = compiler.CompileModule(file, false, verboseLogging);
if(!result) return;
}
}
Expand All @@ -87,6 +79,8 @@ void JSBytecodeRuntime::ProcessClientFile(alt::IResource* resource, alt::IPackag
package->WriteFile(clientPkgFile, buffer.data(), buffer.size());
package->CloseFile(clientPkgFile);
}

compilerLogger.Log("Converted " + std::to_string(compiledFiles.size()) + " script files to bytecode");
}

bool JSBytecodeRuntime::GetProcessClientType(std::string& clientType)
Expand Down
12 changes: 0 additions & 12 deletions module/src/runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,13 @@

#include "SDK.h"
#include "v8.h"
#include "libplatform/libplatform.h"

class JSBytecodeRuntime : public alt::IScriptRuntime
{
v8::Isolate* isolate;
v8::Isolate::CreateParams create_params;
std::unique_ptr<v8::Platform> platform;

public:
JSBytecodeRuntime();

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;
Expand Down
90 changes: 90 additions & 0 deletions module/src/runtimev2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#include "runtimev2.h"
#include "Log.h"
#include "compiler.h"
#include "CScriptRuntimeInfo.h"
#include "package.h"
#include "logger.h"

void JSBytecodeRuntimeV2::ProcessClientFile(alt::IResource* resource, alt::IPackage* package)
{
v8::Isolate* isolate = CScriptRuntimeInfo::Instance().GetIsolate();
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<std::string> 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");
Config::Value::Bool verboseLogging = config->Get("verbose")->AsBool(false);

if(ignoredFiles->IsList())
{
Config::Value::List list = ignoredFiles->As<Config::Value::List>();
ignoredModules.reserve(ignoredModules.size() + list.size());
for(auto& item : list)
{
if(item->IsString()) ignoredModules.push_back(item->As<std::string>());
}
}
compiler.SetIgnoredModules(ignoredModules);

// Compile client main file
bool result = compiler.CompileModule(resource->GetClientMain(), true, verboseLogging);
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<Config::Value::List>();
std::vector<std::string> extraFilePatterns;
extraFilePatterns.reserve(list.size());
for(auto& item : list)
{
if(item->IsString()) extraFilePatterns.push_back(item->As<std::string>());
}

std::set<std::string> files = resource->GetMatchedFiles(extraFilePatterns);
for(const std::string& file : files)
{
bool result = compiler.CompileModule(file, false, verboseLogging);
if(!result) return;
}
}

// Write all other files normally
const std::vector<std::string>& clientFiles = resource->GetClientFiles();
const std::vector<std::string>& 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);
}

compilerLogger.Log("Converted " + std::to_string(compiledFiles.size()) + " script files to bytecode");
}

bool JSBytecodeRuntimeV2::GetProcessClientType(std::string& clientType)
{
clientType = "jsv2b";
return true;
}
24 changes: 24 additions & 0 deletions module/src/runtimev2.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include "SDK.h"
#include "v8.h"

class JSBytecodeRuntimeV2 : public alt::IScriptRuntime
{
public:
bool GetProcessClientType(std::string& clientType) override;
void ProcessClientFile(alt::IResource* resource, alt::IPackage* clientPackage) override;

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;
}
};

0 comments on commit e01fedc

Please sign in to comment.