Skip to content

Commit

Permalink
Scripting layer improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Segfaultd committed Apr 24, 2023
1 parent 004e4d4 commit f56f596
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
22 changes: 9 additions & 13 deletions code/framework/src/scripting/engines/node/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,8 @@ namespace Framework::Scripting::Engines::Node {
v8::V8::InitializePlatform(_platform.get());
v8::V8::Initialize();

// Allocate the isolate
_isolate = v8::Isolate::Allocate();

// Register the isolate to the platform
_platform->RegisterIsolate(_isolate, uv_default_loop());

// Initialize the isolate
v8::Isolate::CreateParams params;
params.array_buffer_allocator = node::CreateArrayBufferAllocator();
v8::Isolate::Initialize(_isolate, params);

// Register the IsWorker data slot
_isolate->SetData(v8::Isolate::GetNumberOfDataSlots() - 1, new bool(false));
// Allocate and register the isolate
_isolate = node::NewIsolate(node::CreateArrayBufferAllocator(), uv_default_loop(), _platform.get());

// Allocate our scopes
v8::Locker locker(_isolate);
Expand All @@ -65,6 +54,7 @@ namespace Framework::Scripting::Engines::Node {

// Ye
Logging::GetLogger(FRAMEWORK_INNER_SCRIPTING)->debug("Node.JS engine initialized!");
_isShuttingDown = false;
return EngineError::ENGINE_NONE;
}

Expand All @@ -77,6 +67,8 @@ namespace Framework::Scripting::Engines::Node {
return EngineError::ENGINE_ISOLATE_NULL;
}

_isShuttingDown = true;

v8::SealHandleScope seal(_isolate);
// Drain the remaining tasks while there are available ones
do {
Expand Down Expand Up @@ -107,6 +99,10 @@ namespace Framework::Scripting::Engines::Node {
return;
}

if(_isShuttingDown) {
return;
}

// Update the scripting layer
v8::Locker locker(_isolate);
v8::Isolate::Scope isolateScope(_isolate);
Expand Down
3 changes: 3 additions & 0 deletions code/framework/src/scripting/engines/node/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#pragma once

#include <atomic>

#include <node.h>
#include <v8.h>

Expand All @@ -24,6 +26,7 @@ namespace Framework::Scripting::Engines::Node {
v8::Persistent<v8::ObjectTemplate> _globalObjectTemplate;
std::unique_ptr<node::MultiIsolatePlatform> _platform;
std::string _modName;
std::atomic<bool> _isShuttingDown = false;

public:
EngineError Init(SDKRegisterCallback) override;
Expand Down
5 changes: 5 additions & 0 deletions code/framework/src/scripting/engines/node/resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,15 @@ namespace Framework::Scripting::Engines::Node {
return false;
}

// Scope the resources
v8::Locker locker(_isolate);
v8::Isolate::Scope isolateScope(_isolate);
v8::HandleScope handleScope(_isolate);

// Scope the context and emit destroy
v8::Context::Scope scope(GetContext());
node::EmitAsyncDestroy(_isolate, _asyncContext);

// Exit node environment
node::EmitProcessBeforeExit(_env);
node::EmitProcessExit(_env);
Expand Down

0 comments on commit f56f596

Please sign in to comment.