Skip to content

Commit

Permalink
fix: Fix v8 initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
xLuxy committed Jun 6, 2024
1 parent 4042d99 commit f08aafd
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 52 deletions.
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: 4 additions & 4 deletions module/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "CScriptRuntimeInfo.h"
#include "SDK.h"
#include "version/version.h"
#include "Log.h"
Expand Down Expand Up @@ -34,11 +35,10 @@ EXPORT bool altMain(alt::ICore* core)
{
alt::ICore::SetInstance(core);

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

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

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

Expand Down
14 changes: 2 additions & 12 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 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
14 changes: 2 additions & 12 deletions module/src/runtimev2.cpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
#include "runtimev2.h"
#include "Log.h"
#include "compiler.h"
#include "CScriptRuntimeInfo.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* isolate = CScriptRuntimeInfo::Instance().GetIsolate();
v8::Isolate::Scope isolateScope(isolate);
v8::HandleScope handleScope(isolate);

Expand Down
12 changes: 0 additions & 12 deletions module/src/runtimev2.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 JSBytecodeRuntimeV2 : public alt::IScriptRuntime
{
v8::Isolate* isolate;
v8::Isolate::CreateParams create_params;
std::unique_ptr<v8::Platform> 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;
Expand Down

0 comments on commit f08aafd

Please sign in to comment.