Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions backend/V8/V8Helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,32 @@ struct v8_interop {
static ArgumentsData extractArguments(const Arguments& args) {
return ArgumentsData{args.callbackInfo_.first, args.callbackInfo_.second};
}

struct Critical {
/**
* DANGEROUS OPERATION!!!
* By default the v8 platform is a process-level singleton, which will be destroyed during
* process exit. In some C++ guide, it's recommended not to relay on static variable
* destruction.
*
* By call this method, it is guaranteed not to destroy the platform instance (actually by
* making the singleton leak).
*/
static void neverDestroyPlatform();

/**
* DANGEROUS OPERATION!!!
* By default the v8 platform is a process-level singleton, which will be destroyed during
* process exit. In some case, if you are sure not to use V8 in the process again (never
* re-create a instance), you can call this method to destroy the platform immediately. Once
* destroyed, V8 usually shuts down thread pool, etc to release resources.
*
* In face, if there is still running V8Engine instance, the platform will be destroy
* afterwords.
*
*/
static void immediatelyDestroyPlatform();
};
};

} // namespace script
17 changes: 17 additions & 0 deletions backend/V8/V8Platform.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@

#include "V8Platform.h"
#include <libplatform/libplatform.h>
#include <type_traits>
#include "../../src/Utils.h"
#include "V8Engine.h"
#include "V8Helper.hpp"

namespace script::v8_backend {

Expand Down Expand Up @@ -198,3 +200,18 @@ bool V8Platform::OnCriticalMemoryPressure(size_t length) {
#endif

} // namespace script::v8_backend

void script::v8_interop::Critical::neverDestroyPlatform() {
using script::v8_backend::V8Platform;
auto platform = V8Platform::getPlatform();

// make the shared_ptr leak
std::aligned_storage_t<sizeof(std::shared_ptr<V8Platform>)> buffer;
new (&buffer) std::shared_ptr<V8Platform>(platform);
}

void script::v8_interop::Critical::immediatelyDestroyPlatform() {
using script::v8_backend::V8Platform;
std::lock_guard<std::mutex> lock(V8Platform::lock_);
V8Platform::singletonInstance_ = nullptr;
}
2 changes: 2 additions & 0 deletions backend/V8/V8Platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ class V8Platform : public v8::Platform {
void addEngineInstance(v8::Isolate* isolate, V8Engine* engine);

void removeEngineInstance(v8::Isolate* isolate);

friend struct script::v8_interop;
};

} // namespace script::v8_backend