-
Notifications
You must be signed in to change notification settings - Fork 24.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move C++ code to OSS folders (#36789)
Summary: Pull Request resolved: #36789 Move > xplat/ReactNative/venice to > xplat/js/react-native-github/packages/react-native/ReactCommon/react/bridgeless Changelog: [General][Changed] - Move Bridgeless C++ code to OSS folders Reviewed By: fkgozali Differential Revision: D44626153 fbshipit-source-id: ec8340db92b805d07d3c5f8e86cb35335637ccd6
- Loading branch information
1 parent
94356e1
commit e2e59c4
Showing
10 changed files
with
2,087 additions
and
0 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
packages/react-native/ReactCommon/react/bridgeless/.clang-tidy
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,17 @@ | ||
--- | ||
InheritParentConfig: true | ||
Checks: ' | ||
-cert-err60-cpp, | ||
-cppcoreguidelines-pro-bounds-pointer-arithmetic, | ||
-cppcoreguidelines-special-member-functions, | ||
-fuchsia-default-arguments-calls, | ||
-google-readability-casting, | ||
-google-runtime-references, | ||
-hicpp-special-member-functions, | ||
-llvm-header-guard, | ||
-misc-non-private-member-variables-in-classes, | ||
-misc-unused-parameters, | ||
-modernize-use-trailing-return-type, | ||
-performance-unnecessary-value-param | ||
' | ||
... |
57 changes: 57 additions & 0 deletions
57
packages/react-native/ReactCommon/react/bridgeless/BufferedRuntimeExecutor.cpp
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,57 @@ | ||
// (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary. | ||
|
||
#include "BufferedRuntimeExecutor.h" | ||
#include <cxxreact/MessageQueueThread.h> | ||
#include <algorithm> | ||
|
||
namespace facebook { | ||
namespace react { | ||
|
||
BufferedRuntimeExecutor::BufferedRuntimeExecutor( | ||
RuntimeExecutor runtimeExecutor) | ||
: runtimeExecutor_(runtimeExecutor), | ||
isBufferingEnabled_(true), | ||
lastIndex_(0) {} | ||
|
||
void BufferedRuntimeExecutor::execute(Work &&callback) { | ||
if (!isBufferingEnabled_) { | ||
// Fast path: Schedule directly to RuntimeExecutor, without locking | ||
runtimeExecutor_(std::move(callback)); | ||
return; | ||
} | ||
|
||
/** | ||
* Note: std::mutex doesn't have a FIFO ordering. | ||
* To preserve the order of the buffered work, use a priority queue and | ||
* track the last known work index. | ||
*/ | ||
uint64_t newIndex = lastIndex_++; | ||
std::lock_guard<std::mutex> guard(lock_); | ||
if (isBufferingEnabled_) { | ||
queue_.push({.index_ = newIndex, .work_ = std::move(callback)}); | ||
return; | ||
} | ||
|
||
// Force flush the queue to maintain the execution order. | ||
unsafeFlush(); | ||
|
||
runtimeExecutor_(std::move(callback)); | ||
} | ||
|
||
void BufferedRuntimeExecutor::flush() { | ||
std::lock_guard<std::mutex> guard(lock_); | ||
unsafeFlush(); | ||
isBufferingEnabled_ = false; | ||
} | ||
|
||
void BufferedRuntimeExecutor::unsafeFlush() { | ||
while (queue_.size() > 0) { | ||
BufferedWork const &bufferedWork = queue_.top(); | ||
Work work = std::move(bufferedWork.work_); | ||
runtimeExecutor_(std::move(work)); | ||
queue_.pop(); | ||
} | ||
} | ||
|
||
} // namespace react | ||
} // namespace facebook |
46 changes: 46 additions & 0 deletions
46
packages/react-native/ReactCommon/react/bridgeless/BufferedRuntimeExecutor.h
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,46 @@ | ||
// (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary. | ||
|
||
#include <ReactCommon/RuntimeExecutor.h> | ||
#include <jsi/jsi.h> | ||
#include <react/bridgeless/TimerManager.h> | ||
#include <atomic> | ||
#include <queue> | ||
|
||
namespace facebook { | ||
namespace react { | ||
|
||
class BufferedRuntimeExecutor { | ||
public: | ||
using Work = std::function<void(jsi::Runtime &runtime)>; | ||
|
||
// A utility structure to track pending work in the order of when they arrive. | ||
struct BufferedWork { | ||
uint64_t index_; | ||
Work work_; | ||
bool operator<(const BufferedWork &rhs) const { | ||
// Higher index has lower priority, so this inverted comparison puts | ||
// the smaller index on top of the queue. | ||
return index_ > rhs.index_; | ||
} | ||
}; | ||
|
||
BufferedRuntimeExecutor(RuntimeExecutor runtimeExecutor); | ||
|
||
void execute(Work &&callback); | ||
|
||
// Flush buffered JS calls and then diable JS buffering | ||
void flush(); | ||
|
||
private: | ||
// Perform flushing without locking mechanism | ||
void unsafeFlush(); | ||
|
||
RuntimeExecutor runtimeExecutor_; | ||
bool isBufferingEnabled_; | ||
std::mutex lock_; | ||
std::atomic<uint64_t> lastIndex_; | ||
std::priority_queue<BufferedWork> queue_; | ||
}; | ||
|
||
} // namespace react | ||
} // namespace facebook |
22 changes: 22 additions & 0 deletions
22
packages/react-native/ReactCommon/react/bridgeless/JSEngineInstance.h
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,22 @@ | ||
// (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary. | ||
|
||
#pragma once | ||
|
||
#include <ReactCommon/RuntimeExecutor.h> | ||
#include <jsi/jsi.h> | ||
|
||
namespace facebook { | ||
namespace react { | ||
|
||
/** | ||
* Interface for a class that creates and owns an instance of a JS VM | ||
*/ | ||
class JSEngineInstance { | ||
public: | ||
virtual std::unique_ptr<jsi::Runtime> createJSRuntime() noexcept = 0; | ||
|
||
virtual ~JSEngineInstance() = default; | ||
}; | ||
|
||
} // namespace react | ||
} // namespace facebook |
29 changes: 29 additions & 0 deletions
29
packages/react-native/ReactCommon/react/bridgeless/PlatformTimerRegistry.h
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,29 @@ | ||
// (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary. | ||
|
||
#pragma once | ||
|
||
#include <cstdint> | ||
|
||
namespace facebook { | ||
namespace react { | ||
|
||
/** | ||
* This interface is implemented by each platform. | ||
* Responsibility: Call into some platform API to register/schedule, or delete | ||
* registered/scheduled timers. | ||
*/ | ||
class PlatformTimerRegistry { | ||
public: | ||
virtual void createTimer(uint32_t timerID, double delayMS) = 0; | ||
|
||
virtual void deleteTimer(uint32_t timerID) = 0; | ||
|
||
virtual void createRecurringTimer(uint32_t timerID, double delayMS) = 0; | ||
|
||
virtual ~PlatformTimerRegistry() noexcept = default; | ||
}; | ||
|
||
using TimerManagerDelegate = PlatformTimerRegistry; | ||
|
||
} // namespace react | ||
} // namespace facebook |
Oops, something went wrong.