-
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.
[0.75] Add support for handling
com.facebook.react.bridge.Dynamic
a…
…s parameter for TurboModules (#46066)
- Loading branch information
Showing
8 changed files
with
216 additions
and
6 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/DynamicNative.kt
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,51 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
package com.facebook.react.bridge | ||
|
||
import com.facebook.jni.HybridData | ||
import com.facebook.proguard.annotations.DoNotStrip | ||
import com.facebook.proguard.annotations.DoNotStripAny | ||
|
||
/** | ||
* An implementation of [Dynamic] that has a C++ implementation. | ||
* | ||
* This is used to support Legacy Native Modules that have not been migrated to the new architecture | ||
* and are using [Dynamic] as a parameter type. | ||
*/ | ||
@DoNotStripAny | ||
private class DynamicNative( | ||
@Suppress("NoHungarianNotation") @field:DoNotStrip private val mHybridData: HybridData? | ||
) : Dynamic { | ||
|
||
override val type: ReadableType | ||
get() = getTypeNative() | ||
|
||
override val isNull: Boolean | ||
get() = isNullNative() | ||
|
||
private external fun getTypeNative(): ReadableType | ||
|
||
private external fun isNullNative(): Boolean | ||
|
||
external override fun asBoolean(): Boolean | ||
|
||
// The native representation is holding the value as Double. We do the Int conversion here. | ||
override fun asInt(): Int = asDouble().toInt() | ||
|
||
external override fun asDouble(): Double | ||
|
||
external override fun asString(): String | ||
|
||
external override fun asArray(): ReadableArray | ||
|
||
external override fun asMap(): ReadableMap | ||
|
||
override fun recycle() { | ||
// Noop - nothing to recycle since there is no pooling | ||
} | ||
} |
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
46 changes: 46 additions & 0 deletions
46
packages/react-native/ReactAndroid/src/main/jni/react/jni/JDynamicNative.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,46 @@ | ||
/* | ||
* 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 "JDynamicNative.h" | ||
#include "ReadableNativeArray.h" | ||
#include "ReadableNativeMap.h" | ||
|
||
using namespace facebook::jni; | ||
|
||
namespace facebook::react { | ||
|
||
jboolean JDynamicNative::isNullNative() { | ||
return payload_.isNull(); | ||
} | ||
|
||
jni::local_ref<ReadableType> JDynamicNative::getTypeNative() { | ||
return ReadableType::getType(payload_.type()); | ||
} | ||
|
||
jni::local_ref<jstring> JDynamicNative::asString() { | ||
return jni::make_jstring(payload_.asString()); | ||
} | ||
|
||
jboolean JDynamicNative::asBoolean() { | ||
return payload_.asBool(); | ||
} | ||
|
||
jdouble JDynamicNative::asDouble() { | ||
return payload_.asDouble(); | ||
} | ||
|
||
jni::local_ref<ReadableArray> JDynamicNative::asArray() { | ||
return jni::adopt_local(reinterpret_cast<ReadableArray::javaobject>( | ||
ReadableNativeArray::newObjectCxxArgs(payload_).release())); | ||
} | ||
|
||
jni::local_ref<ReadableMap> JDynamicNative::asMap() { | ||
return jni::adopt_local(reinterpret_cast<ReadableMap::javaobject>( | ||
ReadableNativeMap::createWithContents(std::move(payload_)).release())); | ||
} | ||
|
||
} // namespace facebook::react |
56 changes: 56 additions & 0 deletions
56
packages/react-native/ReactAndroid/src/main/jni/react/jni/JDynamicNative.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,56 @@ | ||
/* | ||
* 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 "NativeCommon.h" | ||
#include "ReadableNativeArray.h" | ||
#include "ReadableNativeMap.h" | ||
|
||
#include <fbjni/fbjni.h> | ||
#include <folly/dynamic.h> | ||
#include <folly/json.h> | ||
|
||
namespace facebook::react { | ||
|
||
struct JDynamic : public jni::JavaClass<JDynamic> { | ||
constexpr static auto kJavaDescriptor = "Lcom/facebook/react/bridge/Dynamic;"; | ||
}; | ||
|
||
class JDynamicNative : public jni::HybridClass<JDynamicNative, JDynamic> { | ||
public: | ||
constexpr static auto kJavaDescriptor = | ||
"Lcom/facebook/react/bridge/DynamicNative;"; | ||
|
||
JDynamicNative(folly::dynamic payload) : payload_(std::move(payload)) {} | ||
|
||
static void registerNatives() { | ||
javaClassStatic()->registerNatives( | ||
{makeNativeMethod("isNullNative", JDynamicNative::isNullNative), | ||
makeNativeMethod("getTypeNative", JDynamicNative::getTypeNative), | ||
makeNativeMethod("asDouble", JDynamicNative::asDouble), | ||
makeNativeMethod("asBoolean", JDynamicNative::asBoolean), | ||
makeNativeMethod("asString", JDynamicNative::asString), | ||
makeNativeMethod("asArray", JDynamicNative::asArray), | ||
makeNativeMethod("asMap", JDynamicNative::asMap)}); | ||
} | ||
|
||
private: | ||
friend HybridBase; | ||
|
||
jni::local_ref<ReadableType> getTypeNative(); | ||
jni::local_ref<jstring> asString(); | ||
jboolean asBoolean(); | ||
jdouble asDouble(); | ||
jboolean isNullNative(); | ||
jni::local_ref<ReadableArray> asArray(); | ||
jni::local_ref<ReadableMap> asMap(); | ||
|
||
folly::dynamic payload_; | ||
}; | ||
|
||
} // namespace facebook::react |
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