-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[0.71] Switch between V8 JSI and V8 Node-API (#12211)
* Switch between V8 JSI and V8 Node-API * Change files * Format code
- Loading branch information
Showing
9 changed files
with
192 additions
and
3 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
change/react-native-windows-521af409-3887-4e28-a380-e8689c109448.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"type": "patch", | ||
"comment": "Switch between V8 JSI and V8 Node-API", | ||
"packageName": "react-native-windows", | ||
"email": "vmorozov@microsoft.com", | ||
"dependentChangeType": "patch" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
#include "pch.h" | ||
|
||
#include <V8JsiRuntime.h> | ||
#include "V8JSIRuntimeHolder.h" | ||
|
||
#include <atomic> | ||
#include <queue> | ||
|
||
using namespace facebook; | ||
using namespace facebook::react; | ||
|
||
namespace facebook { | ||
namespace react { | ||
|
||
class TaskRunnerAdapter : public v8runtime::JSITaskRunner { | ||
public: | ||
TaskRunnerAdapter(std::shared_ptr<facebook::react::MessageQueueThread> jsQueue) : jsQueue_(std::move(jsQueue)) {} | ||
|
||
void postTask(std::unique_ptr<v8runtime::JSITask> task) override { | ||
std::shared_ptr<v8runtime::JSITask> shared_task(task.release()); | ||
jsQueue_->runOnQueue([shared_task2 = std::move(shared_task)]() { shared_task2->run(); }); | ||
} | ||
|
||
private: | ||
TaskRunnerAdapter(const TaskRunnerAdapter &) = delete; | ||
TaskRunnerAdapter &operator=(const TaskRunnerAdapter &) = delete; | ||
|
||
std::shared_ptr<facebook::react::MessageQueueThread> jsQueue_; | ||
}; | ||
|
||
facebook::react::JSIEngineOverride V8JSIRuntimeHolder::getRuntimeType() noexcept { | ||
return facebook::react::JSIEngineOverride::V8; | ||
} | ||
|
||
std::shared_ptr<facebook::jsi::Runtime> V8JSIRuntimeHolder::getRuntime() noexcept { | ||
std::call_once(once_flag_, [this]() { initRuntime(); }); | ||
|
||
if (!runtime_) | ||
std::terminate(); | ||
|
||
// V8JsiRuntime is not thread safe as of now. | ||
if (own_thread_id_ != std::this_thread::get_id()) | ||
std::terminate(); | ||
|
||
return runtime_; | ||
} | ||
|
||
void V8JSIRuntimeHolder::initRuntime() noexcept { | ||
v8runtime::V8RuntimeArgs args{}; | ||
|
||
if (debuggerPort_ > 0) | ||
args.inspectorPort = debuggerPort_; | ||
|
||
args.flags.enableInspector = useDirectDebugger_; | ||
args.flags.waitForDebugger = debuggerBreakOnNextLine_; | ||
args.debuggerRuntimeName = debuggerRuntimeName_; | ||
|
||
args.foreground_task_runner = std::make_shared<TaskRunnerAdapter>(jsQueue_); | ||
args.preparedScriptStore = std::move(preparedScriptStore_); | ||
args.flags.enableMultiThread = enableMultiThreadingSupport_; | ||
|
||
runtime_ = v8runtime::makeV8Runtime(std::move(args)); | ||
|
||
own_thread_id_ = std::this_thread::get_id(); | ||
} | ||
|
||
} // namespace react | ||
} // namespace facebook |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
#pragma once | ||
|
||
#include <DevSettings.h> | ||
|
||
#include <JSI/RuntimeHolder.h> | ||
#include <JSI/ScriptStore.h> | ||
|
||
#include <Logging.h> | ||
|
||
namespace facebook { | ||
namespace react { | ||
|
||
class V8JSIRuntimeHolder : public Microsoft::JSI::RuntimeHolderLazyInit { | ||
public: | ||
std::shared_ptr<facebook::jsi::Runtime> getRuntime() noexcept override; | ||
facebook::react::JSIEngineOverride getRuntimeType() noexcept override; | ||
|
||
V8JSIRuntimeHolder( | ||
std::shared_ptr<facebook::react::DevSettings> devSettings, | ||
std::shared_ptr<facebook::react::MessageQueueThread> jsQueue, | ||
std::unique_ptr<facebook::jsi::ScriptStore> &&scriptStore, | ||
std::shared_ptr<facebook::jsi::PreparedScriptStore> &&preparedScriptStore, | ||
bool enableMultiThreadingSupport) noexcept | ||
: useDirectDebugger_(devSettings->useDirectDebugger), | ||
debuggerBreakOnNextLine_(devSettings->debuggerBreakOnNextLine), | ||
debuggerPort_(devSettings->debuggerPort), | ||
debuggerRuntimeName_(devSettings->debuggerRuntimeName), | ||
jsQueue_(std::move(jsQueue)), | ||
scriptStore_(std::move(scriptStore)), | ||
preparedScriptStore_(std::move(preparedScriptStore)), | ||
enableMultiThreadingSupport_(enableMultiThreadingSupport) {} | ||
|
||
private: | ||
void initRuntime() noexcept; | ||
|
||
std::shared_ptr<facebook::jsi::Runtime> runtime_; | ||
std::shared_ptr<facebook::react::MessageQueueThread> jsQueue_; | ||
|
||
std::unique_ptr<facebook::jsi::ScriptStore> scriptStore_; | ||
std::shared_ptr<facebook::jsi::PreparedScriptStore> preparedScriptStore_; | ||
|
||
std::once_flag once_flag_; | ||
std::thread::id own_thread_id_; | ||
|
||
uint16_t debuggerPort_; | ||
bool useDirectDebugger_; | ||
bool debuggerBreakOnNextLine_; | ||
std::string debuggerRuntimeName_; | ||
bool enableMultiThreadingSupport_; | ||
}; | ||
|
||
} // namespace react | ||
} // namespace facebook |