Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "JReactMarker.h"
#include "NativeArray.h"
#include "NativeMap.h"
#include "TransformHelper.h"
#include "WritableNativeArray.h"
#include "WritableNativeMap.h"

Expand All @@ -28,7 +27,6 @@ extern "C" JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) {
ReadableNativeMap::registerNatives();
WritableNativeArray::registerNatives();
WritableNativeMap::registerNatives();
TransformHelper::registerNatives();
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "InspectorNetworkRequestListener.h"
#include "JavaScriptExecutorHolder.h"
#include "ReactInstanceManagerInspectorTarget.h"
#include "TransformHelper.h"

#ifndef WITH_GLOGINIT
#define WITH_GLOGINIT 1
Expand Down Expand Up @@ -43,6 +44,7 @@ extern "C" JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) {
#endif
ReactInstanceManagerInspectorTarget::registerNatives();
InspectorNetworkRequestListener::registerNatives();
TransformHelper::registerNatives();
});
}

Expand Down
34 changes: 2 additions & 32 deletions packages/react-native/ReactCommon/cxxreact/ErrorUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,6 @@

#pragma once

#include <jsi/jsi.h>
#warning Deprecated: use <jserrorhandler/ErrorUtils.h> instead.

namespace facebook::react {

inline static void handleJSError(jsi::Runtime &runtime, const jsi::JSError &error, bool isFatal)
{
auto errorUtils = runtime.global().getProperty(runtime, "ErrorUtils");
if (errorUtils.isUndefined() || !errorUtils.isObject() ||
!errorUtils.getObject(runtime).hasProperty(runtime, "reportFatalError") ||
!errorUtils.getObject(runtime).hasProperty(runtime, "reportError")) {
// ErrorUtils was not set up. This probably means the bundle didn't
// load properly.
throw jsi::JSError(
runtime,
"ErrorUtils is not set up properly. Something probably went wrong trying to load the JS bundle. Trying to report error " +
error.getMessage(),
error.getStack());
}

// TODO(janzer): Rewrite this function to return the processed error
// instead of just reporting it through the native module
if (isFatal) {
auto func = errorUtils.asObject(runtime).getPropertyAsFunction(runtime, "reportFatalError");

func.call(runtime, error.value());
} else {
auto func = errorUtils.asObject(runtime).getPropertyAsFunction(runtime, "reportError");

func.call(runtime, error.value());
}
}

} // namespace facebook::react
#include <jserrorhandler/ErrorUtils.h>
6 changes: 1 addition & 5 deletions packages/react-native/ReactCommon/cxxreact/Instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,12 @@

#ifndef RCT_REMOVE_LEGACY_ARCH

#include "ErrorUtils.h"
#include <jserrorhandler/ErrorUtils.h>
#include "JSBigString.h"
#include "JSBundleType.h"
#include "JSExecutor.h"
#include "MessageQueueThread.h"
#include "MethodCall.h"
#include "NativeToJsBridge.h"
#include "RAMBundleRegistry.h"
#include "RecoverableError.h"
#include "TraceSection.h"

#include <cxxreact/JSIndexedRAMBundle.h>
Expand All @@ -27,7 +24,6 @@
#include <glog/logging.h>

#include <condition_variable>
#include <exception>
#include <memory>
#include <mutex>
#include <string>
Expand Down
123 changes: 2 additions & 121 deletions packages/react-native/ReactCommon/cxxreact/MoveWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,125 +7,6 @@

#pragma once

#include <memory>
#warning Deprecated. Use <react/utils/MoveWrapper.h> instead.

namespace facebook::react {

/*
NOTE: we keep this internal copy of folly/MoveWrapper.h to unblock
the the workstream of dropping the dependency on folly in RN!
For a technical explanation on why we still need this we defer
to the doc in folly/Function.h:
"There are some limitations in std::function that folly::Function tries to
avoid. std::function is copy-constructible and requires that the callable that
it wraps is copy-constructible as well, which is a constraint that is often
inconvenient. In most cases when using a std::function you don't make use of
its copy-constructibility, so you might sometimes feel like you get back very
little in return for a noticeable restriction. This restriction becomes
apparent when trying to use a lambda capturing a unique_ptr (or any
non-copyable type) as a callback for a folly::Future.
std::unique_ptr<Foo> foo_ptr = new Foo;
some_future.then(
[foo_ptr = std::move(foo_ptr)] mutable
(int x)
{ foo_ptr->setX(x); }
);
This piece of code did not compile before folly::Future started using
folly::Function instead of std::function to store the callback. Because the
lambda captures something non-copyable (the unique_ptr), it is not copyable
itself. And std::function can only store copyable callables.
The implementation of folly::Future did not make use of the
copy-constructibility of std::function at any point. There was no benefit from
the fact that the std::function is copy-constructible, but the fact that it can
only wrap copy-constructible callables posed a restriction.
A workaround was available: folly::MoveWrapper, which wraps an object that may
be non-copyable and implements copy operations by moving the embedded object.
Using a folly::MoveWrapper, you can capture non-copyable objects in a lambda,
and the lambda itself is still copyable and may be wrapped in a std::function.
It is a pragmatic solution for the above problem, but you have to be a little
careful. The problem is that you can’t use a MoveWrapper anywhere where copy
operations are assumed to behave like actual copy operations. Also, a
folly::MoveWrapper<std::unique_ptr<T>> essentially behaves like auto_ptr<T>. Ask
yourself whether you’d want to use lots of auto_ptrs in your codebase. And the
original question still persists: we very often don’t benefit from
copy-constructibility of std::function, so why do we have to live with this
restriction? I.e. why do we have to use MoveWrapper?"
*/

/** C++11 closures don't support move-in capture. Nor does std::bind.
facepalm.
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3610.html
"[...] a work-around that should make people's stomach crawl:
write a wrapper that performs move-on-copy, much like the deprecated
auto_ptr"
Unlike auto_ptr, this doesn't require a heap allocation.
*/
template <class T>
class MoveWrapper {
public:
/** If value can be default-constructed, why not?
Then we don't have to move it in */
MoveWrapper() = default;

/// Move a value in.
explicit MoveWrapper(T &&t) : value(std::move(t)) {}

/// copy is move
MoveWrapper(const MoveWrapper &other) : value(std::move(other.value)) {}

/// move is also move
MoveWrapper(MoveWrapper &&other) noexcept : value(std::move(other.value)) {}

const T &operator*() const
{
return value;
}
T &operator*()
{
return value;
}

const T *operator->() const
{
return &value;
}
T *operator->()
{
return &value;
}

/// move the value out (sugar for std::move(*moveWrapper))
T &&move()
{
return std::move(value);
}

// If you want these you're probably doing it wrong, though they'd be
// easy enough to implement
MoveWrapper &operator=(const MoveWrapper &) = delete;
MoveWrapper &operator=(MoveWrapper &&) = delete;

private:
mutable T value;
};

/// Make a MoveWrapper from the argument. Because the name "makeMoveWrapper"
/// is already quite transparent in its intent, this will work for lvalues as
/// if you had wrapped them in std::move.
template <class T, class T0 = typename std::remove_reference<T>::type>
MoveWrapper<T0> makeMoveWrapper(T &&t)
{
return MoveWrapper<T0>(std::forward<T0>(t));
}

} // namespace facebook::react
#include <react/utils/MoveWrapper.h>
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@
#include <ReactCommon/CallInvoker.h>
#include <folly/json.h>
#include <glog/logging.h>
#include <jserrorhandler/JsErrorHandler.h>
#include <jsi/jsi.h>
#include <react/utils/MoveWrapper.h>
#include <reactperflogger/BridgeNativeModulePerfLogger.h>

#include "ErrorUtils.h"
#include "Instance.h"
#include "JSBigString.h"
#include "MessageQueueThread.h"
#include "MethodCall.h"
#include "ModuleRegistry.h"
#include "MoveWrapper.h"
#include "RAMBundleRegistry.h"
#include "TraceSection.h"

Expand Down
42 changes: 42 additions & 0 deletions packages/react-native/ReactCommon/jserrorhandler/ErrorUtils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#include "ErrorUtils.h"

namespace facebook::react {

void handleJSError(
jsi::Runtime& runtime,
const jsi::JSError& error,
bool isFatal) {
auto errorUtils = runtime.global().getProperty(runtime, "ErrorUtils");
if (errorUtils.isUndefined() || !errorUtils.isObject() ||
!errorUtils.getObject(runtime).hasProperty(runtime, "reportFatalError") ||
!errorUtils.getObject(runtime).hasProperty(runtime, "reportError")) {
// ErrorUtils was not set up. This probably means the bundle didn't
// load properly.
throw jsi::JSError(
runtime,
"ErrorUtils is not set up properly. Something probably went wrong trying to load the JS bundle. Trying to report error " +
error.getMessage(),
error.getStack());
}

// TODO(janzer): Rewrite this function to return the processed error
// instead of just reporting it through the native module
if (isFatal) {
auto func = errorUtils.asObject(runtime).getPropertyAsFunction(
runtime, "reportFatalError");
func.call(runtime, error.value());
} else {
auto func = errorUtils.asObject(runtime).getPropertyAsFunction(
runtime, "reportError");
func.call(runtime, error.value());
}
}

} // namespace facebook::react
16 changes: 16 additions & 0 deletions packages/react-native/ReactCommon/jserrorhandler/ErrorUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#pragma once

#include <jsi/jsi.h>

namespace facebook::react {

void handleJSError(jsi::Runtime &runtime, const jsi::JSError &error, bool isFatal);

} // namespace facebook::react
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
*/

#include "JsErrorHandler.h"
#include <cxxreact/ErrorUtils.h>

#include <glog/logging.h>
#include <react/bridging/Bridging.h>
#include <react/featureflags/ReactNativeFeatureFlags.h>
#include <string>

#include "ErrorUtils.h"
#include "StackTraceParser.h"

using namespace facebook;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@

#include "jsireact/JSIExecutor.h"

#include <cxxreact/ErrorUtils.h>
#include <cxxreact/JSBigString.h>
#include <cxxreact/ModuleRegistry.h>
#include <cxxreact/ReactMarker.h>
#include <cxxreact/TraceSection.h>
#include <folly/json.h>
#include <glog/logging.h>
#include <jserrorhandler/ErrorUtils.h>
#include <jsi/JSIDynamic.h>
#include <jsi/instrumentation.h>
#include <reactperflogger/BridgeNativeModulePerfLogger.h>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
#include <memory>
#include <string>

#include <cxxreact/MoveWrapper.h>
#include <cxxreact/TraceSection.h>
#include <fbjni/fbjni.h>
#include <glog/logging.h>
#include <jsi/jsi.h>
#include <react/utils/MoveWrapper.h>

#include <ReactCommon/TurboModule.h>
#include <ReactCommon/TurboModulePerfLogger.h>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
#include "RuntimeScheduler_Legacy.h"
#include "RuntimeScheduler_Modern.h"

#include <cxxreact/ErrorUtils.h>
#include <cxxreact/TraceSection.h>
#include <jserrorhandler/ErrorUtils.h>
#include <react/featureflags/ReactNativeFeatureFlags.h>
#include <utility>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@
#include "ReactInstance.h"

#include <ReactCommon/RuntimeExecutor.h>
#include <cxxreact/ErrorUtils.h>
#include <cxxreact/JSBigString.h>
#include <cxxreact/JSExecutor.h>
#include <cxxreact/ReactMarker.h>
#include <cxxreact/TraceSection.h>
#include <glog/logging.h>
#include <jserrorhandler/ErrorUtils.h>
#include <jsi/JSIDynamic.h>
#include <jsi/hermes.h>
#include <jsi/instrumentation.h>
Expand Down
Loading
Loading