first commit

This commit is contained in:
2026-03-10 16:18:05 +00:00
commit 11f9c069b5
31635 changed files with 3187747 additions and 0 deletions

View File

@@ -0,0 +1,63 @@
# 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.
require "json"
package = JSON.parse(File.read(File.join(__dir__, "..", "..", "..", "..", "package.json")))
version = package['version']
source = { :git => 'https://github.com/facebook/react-native.git' }
if version == '1000.0.0'
# This is an unpublished version, use the latest commit hash of the react-native repo, which were presumably in.
source[:commit] = `git rev-parse HEAD`.strip if system("git rev-parse --git-dir > /dev/null 2>&1")
else
source[:tag] = "v#{version}"
end
header_search_paths = [
"\"$(PODS_ROOT)/Headers/Private/React-Core\"",
]
create_header_search_path_for_frameworks("ReactCommon-Samples").each { |search_path| header_search_paths << "\"#{search_path}\""}
Pod::Spec.new do |s|
s.name = "ReactCommon-Samples"
s.module_name = "ReactCommon_Samples"
s.header_dir = "ReactCommon"
s.version = version
s.summary = "-" # TODO
s.homepage = "https://reactnative.dev/"
s.license = package["license"]
s.author = "Meta Platforms, Inc. and its affiliates"
s.platforms = min_supported_versions
s.source = source
s.pod_target_xcconfig = { "HEADER_SEARCH_PATHS" => header_search_paths,
"USE_HEADERMAP" => "YES",
"CLANG_CXX_LANGUAGE_STANDARD" => rct_cxx_language_standard(),
"GCC_WARN_PEDANTIC" => "YES" }
s.framework = "UIKit"
if ENV['USE_FRAMEWORKS']
# Do not use resolve_use_frameworks here - since we're including source files.
# Then it is not needed.
s.header_mappings_dir = './'
end
s.source_files = "ReactCommon/**/*.{cpp,h}",
"platform/ios/**/*.{mm,cpp,h}"
s.dependency "React-Core"
s.dependency "React-cxxreact"
s.dependency "React-jsi"
add_dependency(s, "React-RCTFBReactNativeSpec")
add_dependency(s, "ReactCommon", :subspec => "turbomodule/core", :additional_framework_paths => ["react/nativemodule/core"])
add_dependency(s, "React-NativeModulesApple")
depend_on_js_engine(s)
add_rn_third_party_dependencies(s)
add_rncore_dependency(s)
end

View File

@@ -0,0 +1,2 @@
Refer to packages/rn-tester/NativeCxxModuleExample/NativeCxxModuleExample.h for a concrete example.
See: https://reactnative.dev/docs/the-new-architecture/pure-cxx-modules

View File

@@ -0,0 +1,23 @@
# 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.
cmake_minimum_required(VERSION 3.13)
set(CMAKE_VERBOSE_MAKEFILE on)
include(${REACT_COMMON_DIR}/cmake-utils/react-native-flags.cmake)
file(GLOB sampleturbomodule_SRC CONFIGURE_DEPENDS ReactCommon/*.cpp)
add_library(sampleturbomodule STATIC ${sampleturbomodule_SRC})
target_include_directories(sampleturbomodule PUBLIC .)
target_link_libraries(sampleturbomodule
fbjni
jsi
reactnative
)
target_compile_reactnative_options(sampleturbomodule PRIVATE)
target_compile_options(sampleturbomodule PRIVATE -Wpedantic)

View File

@@ -0,0 +1,147 @@
/*
* 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.
*/
// NOTE: This entire file should be codegen'ed.
package com.facebook.fbreact.specs;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.common.build.ReactBuildConfig;
import com.facebook.react.turbomodule.core.interfaces.TurboModule;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
public abstract class NativeSampleTurboModuleSpec extends ReactContextBaseJavaModule
implements TurboModule {
public static final String NAME = "SampleTurboModule";
public NativeSampleTurboModuleSpec(ReactApplicationContext reactContext) {
super(reactContext);
}
@Override
public @Nonnull String getName() {
return NAME;
}
protected final void emitOnPress() {
mEventEmitterCallback.invoke("onPress");
}
protected final void emitOnClick(String value) {
mEventEmitterCallback.invoke("onClick", value);
}
protected final void emitOnChange(ReadableMap value) {
mEventEmitterCallback.invoke("onChange", value);
}
protected void emitOnSubmit(ReadableArray value) {
mEventEmitterCallback.invoke("onSubmit", value);
}
protected abstract Map<String, Object> getTypedExportedConstants();
@Override
public final @Nullable Map<String, Object> getConstants() {
Map<String, Object> constants = getTypedExportedConstants();
if (ReactBuildConfig.DEBUG || ReactBuildConfig.IS_INTERNAL_BUILD) {
Set<String> obligatoryFlowConstants =
new HashSet<>(Arrays.asList("const1", "const2", "const3"));
Set<String> optionalFlowConstants = new HashSet<>();
Set<String> undeclaredConstants = new HashSet<>(constants.keySet());
undeclaredConstants.removeAll(obligatoryFlowConstants);
undeclaredConstants.removeAll(optionalFlowConstants);
if (!undeclaredConstants.isEmpty()) {
throw new IllegalStateException(
"Native Module Flow doesn't declare constants: " + undeclaredConstants);
}
undeclaredConstants = obligatoryFlowConstants;
undeclaredConstants.removeAll(constants.keySet());
if (!undeclaredConstants.isEmpty()) {
throw new IllegalStateException(
"Native Module doesn't fill in constants: " + undeclaredConstants);
}
}
return constants;
}
@ReactMethod
public abstract void voidFunc();
@ReactMethod(isBlockingSynchronousMethod = true)
public abstract boolean getBool(boolean arg);
@ReactMethod(isBlockingSynchronousMethod = true)
public double getEnum(double arg) {
return 0;
}
@ReactMethod(isBlockingSynchronousMethod = true)
public abstract double getNumber(double arg);
@ReactMethod(isBlockingSynchronousMethod = true)
public abstract String getString(String arg);
@ReactMethod(isBlockingSynchronousMethod = true)
public abstract WritableArray getArray(ReadableArray arg);
@ReactMethod(isBlockingSynchronousMethod = true)
public abstract WritableMap getObject(ReadableMap arg);
@ReactMethod(isBlockingSynchronousMethod = true)
public abstract WritableMap getUnsafeObject(ReadableMap arg);
@ReactMethod(isBlockingSynchronousMethod = true)
public abstract double getRootTag(double arg);
@ReactMethod(isBlockingSynchronousMethod = true)
public abstract WritableMap getValue(double x, String y, ReadableMap z);
@ReactMethod
public abstract void getValueWithCallback(Callback callback);
@ReactMethod
public abstract void getValueWithPromise(boolean error, Promise promise);
@ReactMethod
public void voidFuncThrows() {}
@ReactMethod(isBlockingSynchronousMethod = true)
public WritableMap getObjectThrows(ReadableMap arg) {
return null;
}
@ReactMethod
public void promiseThrows(Promise promise) {}
@ReactMethod
public void voidFuncAssert() {}
@ReactMethod(isBlockingSynchronousMethod = true)
public WritableMap getObjectAssert(ReadableMap arg) {
return null;
}
@ReactMethod
public void promiseAssert(Promise promise) {}
@ReactMethod
public void getImageUrl(Promise promise) {}
}

View File

@@ -0,0 +1,32 @@
/*
* 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 <ReactCommon/SampleTurboModuleJSIBindings.h>
namespace facebook::react {
// static
void SampleTurboModuleJSIBindings::registerNatives() {
javaClassLocal()->registerNatives({
makeNativeMethod(
"getBindingsInstaller",
SampleTurboModuleJSIBindings::getBindingsInstaller),
});
}
// static
jni::local_ref<BindingsInstallerHolder::javaobject>
SampleTurboModuleJSIBindings::getBindingsInstaller(
jni::alias_ref<SampleTurboModuleJSIBindings> /*jobj*/) {
return BindingsInstallerHolder::newObjectCxxArgs(
[](jsi::Runtime& runtime, const std::shared_ptr<CallInvoker>&) {
runtime.global().setProperty(
runtime, "__SampleTurboModuleJSIBindings", "Hello JSI!");
});
}
} // namespace facebook::react

View File

@@ -0,0 +1,30 @@
/*
* 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 <ReactCommon/BindingsInstallerHolder.h>
#include <fbjni/fbjni.h>
#include <jsi/jsi.h>
namespace facebook::react {
class SampleTurboModuleJSIBindings : public jni::JavaClass<SampleTurboModuleJSIBindings> {
public:
static constexpr const char *kJavaDescriptor = "Lcom/facebook/fbreact/specs/SampleTurboModule;";
SampleTurboModuleJSIBindings() = default;
static void registerNatives();
private:
// Using static function as a simple demonstration
static jni::local_ref<BindingsInstallerHolder::javaobject> getBindingsInstaller(
jni::alias_ref<SampleTurboModuleJSIBindings> jobj);
};
} // namespace facebook::react

View File

@@ -0,0 +1,416 @@
/*
* 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.
*/
// NOTE: This entire file should be codegen'ed.
#include <ReactCommon/SampleTurboModuleSpec.h>
namespace facebook::react {
static facebook::jsi::Value
__hostFunction_NativeSampleTurboModuleSpecJSI_getConstants(
facebook::jsi::Runtime& rt,
TurboModule& turboModule,
const facebook::jsi::Value* args,
size_t count) {
static jmethodID cachedMethodId = nullptr;
return static_cast<JavaTurboModule&>(turboModule)
.invokeJavaMethod(
rt,
ObjectKind,
"getConstants",
"()Ljava/util/Map;",
args,
count,
cachedMethodId);
}
static facebook::jsi::Value
__hostFunction_NativeSampleTurboModuleSpecJSI_voidFunc(
facebook::jsi::Runtime& rt,
TurboModule& turboModule,
const facebook::jsi::Value* args,
size_t count) {
static jmethodID cachedMethodId = nullptr;
return static_cast<JavaTurboModule&>(turboModule)
.invokeJavaMethod(
rt, VoidKind, "voidFunc", "()V", args, count, cachedMethodId);
}
static facebook::jsi::Value
__hostFunction_NativeSampleTurboModuleSpecJSI_getBool(
facebook::jsi::Runtime& rt,
TurboModule& turboModule,
const facebook::jsi::Value* args,
size_t count) {
static jmethodID cachedMethodId = nullptr;
return static_cast<JavaTurboModule&>(turboModule)
.invokeJavaMethod(
rt, BooleanKind, "getBool", "(Z)Z", args, count, cachedMethodId);
}
static facebook::jsi::Value
__hostFunction_NativeSampleTurboModuleSpecJSI_getEnum(
facebook::jsi::Runtime& rt,
TurboModule& turboModule,
const facebook::jsi::Value* args,
size_t count) {
static jmethodID cachedMethodId = nullptr;
return static_cast<JavaTurboModule&>(turboModule)
.invokeJavaMethod(
rt, NumberKind, "getEnum", "(D)D", args, count, cachedMethodId);
}
static facebook::jsi::Value
__hostFunction_NativeSampleTurboModuleSpecJSI_getNumber(
facebook::jsi::Runtime& rt,
TurboModule& turboModule,
const facebook::jsi::Value* args,
size_t count) {
static jmethodID cachedMethodId = nullptr;
return static_cast<JavaTurboModule&>(turboModule)
.invokeJavaMethod(
rt, NumberKind, "getNumber", "(D)D", args, count, cachedMethodId);
}
static facebook::jsi::Value
__hostFunction_NativeSampleTurboModuleSpecJSI_getString(
facebook::jsi::Runtime& rt,
TurboModule& turboModule,
const facebook::jsi::Value* args,
size_t count) {
static jmethodID cachedMethodId = nullptr;
return static_cast<JavaTurboModule&>(turboModule)
.invokeJavaMethod(
rt,
StringKind,
"getString",
"(Ljava/lang/String;)Ljava/lang/String;",
args,
count,
cachedMethodId);
}
static facebook::jsi::Value
__hostFunction_NativeSampleTurboModuleSpecJSI_getArray(
facebook::jsi::Runtime& rt,
TurboModule& turboModule,
const facebook::jsi::Value* args,
size_t count) {
static jmethodID cachedMethodId = nullptr;
return static_cast<JavaTurboModule&>(turboModule)
.invokeJavaMethod(
rt,
ArrayKind,
"getArray",
"(Lcom/facebook/react/bridge/ReadableArray;)Lcom/facebook/react/bridge/WritableArray;",
args,
count,
cachedMethodId);
}
static facebook::jsi::Value
__hostFunction_NativeSampleTurboModuleSpecJSI_getObject(
facebook::jsi::Runtime& rt,
TurboModule& turboModule,
const facebook::jsi::Value* args,
size_t count) {
static jmethodID cachedMethodId = nullptr;
return static_cast<JavaTurboModule&>(turboModule)
.invokeJavaMethod(
rt,
ObjectKind,
"getObject",
"(Lcom/facebook/react/bridge/ReadableMap;)Lcom/facebook/react/bridge/WritableMap;",
args,
count,
cachedMethodId);
}
static facebook::jsi::Value
__hostFunction_NativeSampleTurboModuleSpecJSI_getUnsafeObject(
facebook::jsi::Runtime& rt,
TurboModule& turboModule,
const facebook::jsi::Value* args,
size_t count) {
static jmethodID cachedMethodId = nullptr;
return static_cast<JavaTurboModule&>(turboModule)
.invokeJavaMethod(
rt,
ObjectKind,
"getUnsafeObject",
"(Lcom/facebook/react/bridge/ReadableMap;)Lcom/facebook/react/bridge/WritableMap;",
args,
count,
cachedMethodId);
}
static facebook::jsi::Value
__hostFunction_NativeSampleTurboModuleSpecJSI_getRootTag(
facebook::jsi::Runtime& rt,
TurboModule& turboModule,
const facebook::jsi::Value* args,
size_t count) {
static jmethodID cachedMethodId = nullptr;
return static_cast<JavaTurboModule&>(turboModule)
.invokeJavaMethod(
rt, NumberKind, "getRootTag", "(D)D", args, count, cachedMethodId);
}
static facebook::jsi::Value
__hostFunction_NativeSampleTurboModuleSpecJSI_getValue(
facebook::jsi::Runtime& rt,
TurboModule& turboModule,
const facebook::jsi::Value* args,
size_t count) {
static jmethodID cachedMethodId = nullptr;
return static_cast<JavaTurboModule&>(turboModule)
.invokeJavaMethod(
rt,
ObjectKind,
"getValue",
"(DLjava/lang/String;Lcom/facebook/react/bridge/ReadableMap;)Lcom/facebook/react/bridge/WritableMap;",
args,
count,
cachedMethodId);
}
static facebook::jsi::Value
__hostFunction_NativeSampleTurboModuleSpecJSI_getValueWithCallback(
facebook::jsi::Runtime& rt,
TurboModule& turboModule,
const facebook::jsi::Value* args,
size_t count) {
static jmethodID cachedMethodId = nullptr;
return static_cast<JavaTurboModule&>(turboModule)
.invokeJavaMethod(
rt,
VoidKind,
"getValueWithCallback",
"(Lcom/facebook/react/bridge/Callback;)V",
args,
count,
cachedMethodId);
}
static facebook::jsi::Value
__hostFunction_NativeSampleTurboModuleSpecJSI_getValueWithPromise(
facebook::jsi::Runtime& rt,
TurboModule& turboModule,
const facebook::jsi::Value* args,
size_t count) {
static jmethodID cachedMethodId = nullptr;
return static_cast<JavaTurboModule&>(turboModule)
.invokeJavaMethod(
rt,
PromiseKind,
"getValueWithPromise",
"(ZLcom/facebook/react/bridge/Promise;)V",
args,
count,
cachedMethodId);
}
static facebook::jsi::Value
__hostFunction_NativeSampleTurboModuleSpecJSI_voidFuncThrows(
facebook::jsi::Runtime& rt,
TurboModule& turboModule,
const facebook::jsi::Value* args,
size_t count) {
static jmethodID cachedMethodId = nullptr;
return static_cast<JavaTurboModule&>(turboModule)
.invokeJavaMethod(
rt, VoidKind, "voidFuncThrows", "()V", args, count, cachedMethodId);
}
static facebook::jsi::Value
__hostFunction_NativeSampleTurboModuleSpecJSI_getObjectThrows(
facebook::jsi::Runtime& rt,
TurboModule& turboModule,
const facebook::jsi::Value* args,
size_t count) {
static jmethodID cachedMethodId = nullptr;
return static_cast<JavaTurboModule&>(turboModule)
.invokeJavaMethod(
rt,
ObjectKind,
"getObjectThrows",
"(Lcom/facebook/react/bridge/ReadableMap;)Lcom/facebook/react/bridge/WritableMap;",
args,
count,
cachedMethodId);
}
static facebook::jsi::Value
__hostFunction_NativeSampleTurboModuleSpecJSI_promiseThrows(
facebook::jsi::Runtime& rt,
TurboModule& turboModule,
const facebook::jsi::Value* args,
size_t count) {
static jmethodID cachedMethodId = nullptr;
return static_cast<JavaTurboModule&>(turboModule)
.invokeJavaMethod(
rt,
PromiseKind,
"promiseThrows",
"(Lcom/facebook/react/bridge/Promise;)V",
args,
count,
cachedMethodId);
}
static facebook::jsi::Value
__hostFunction_NativeSampleTurboModuleSpecJSI_voidFuncAssert(
facebook::jsi::Runtime& rt,
TurboModule& turboModule,
const facebook::jsi::Value* args,
size_t count) {
static jmethodID cachedMethodId = nullptr;
return static_cast<JavaTurboModule&>(turboModule)
.invokeJavaMethod(
rt, VoidKind, "voidFuncAssert", "()V", args, count, cachedMethodId);
}
static facebook::jsi::Value
__hostFunction_NativeSampleTurboModuleSpecJSI_getObjectAssert(
facebook::jsi::Runtime& rt,
TurboModule& turboModule,
const facebook::jsi::Value* args,
size_t count) {
static jmethodID cachedMethodId = nullptr;
return static_cast<JavaTurboModule&>(turboModule)
.invokeJavaMethod(
rt,
ObjectKind,
"getObjectAssert",
"(Lcom/facebook/react/bridge/ReadableMap;)Lcom/facebook/react/bridge/WritableMap;",
args,
count,
cachedMethodId);
}
static facebook::jsi::Value
__hostFunction_NativeSampleTurboModuleSpecJSI_promiseAssert(
facebook::jsi::Runtime& rt,
TurboModule& turboModule,
const facebook::jsi::Value* args,
size_t count) {
static jmethodID cachedMethodId = nullptr;
return static_cast<JavaTurboModule&>(turboModule)
.invokeJavaMethod(
rt,
PromiseKind,
"promiseAssert",
"(Lcom/facebook/react/bridge/Promise;)V",
args,
count,
cachedMethodId);
}
static facebook::jsi::Value
__hostFunction_NativeSampleTurboModuleSpecJSI_getImageUrl(
facebook::jsi::Runtime& rt,
TurboModule& turboModule,
const facebook::jsi::Value* args,
size_t count) {
static jmethodID cachedMethodId = nullptr;
return static_cast<JavaTurboModule&>(turboModule)
.invokeJavaMethod(
rt,
PromiseKind,
"getImageUrl",
"(Lcom/facebook/react/bridge/Promise;)V",
args,
count,
cachedMethodId);
}
NativeSampleTurboModuleSpecJSI::NativeSampleTurboModuleSpecJSI(
const JavaTurboModule::InitParams& params)
: JavaTurboModule(params) {
methodMap_["getConstants"] = MethodMetadata{
.argCount = 0,
.invoker = __hostFunction_NativeSampleTurboModuleSpecJSI_getConstants};
methodMap_["voidFunc"] = MethodMetadata{
.argCount = 0,
.invoker = __hostFunction_NativeSampleTurboModuleSpecJSI_voidFunc};
methodMap_["getBool"] = MethodMetadata{
.argCount = 1,
.invoker = __hostFunction_NativeSampleTurboModuleSpecJSI_getBool};
methodMap_["getEnum"] = MethodMetadata{
.argCount = 1,
.invoker = __hostFunction_NativeSampleTurboModuleSpecJSI_getEnum};
methodMap_["getNumber"] = MethodMetadata{
.argCount = 1,
.invoker = __hostFunction_NativeSampleTurboModuleSpecJSI_getNumber};
methodMap_["getString"] = MethodMetadata{
.argCount = 1,
.invoker = __hostFunction_NativeSampleTurboModuleSpecJSI_getString};
methodMap_["getArray"] = MethodMetadata{
.argCount = 1,
.invoker = __hostFunction_NativeSampleTurboModuleSpecJSI_getArray};
methodMap_["getObject"] = MethodMetadata{
.argCount = 1,
.invoker = __hostFunction_NativeSampleTurboModuleSpecJSI_getObject};
methodMap_["getUnsafeObject"] = MethodMetadata{
.argCount = 1,
.invoker = __hostFunction_NativeSampleTurboModuleSpecJSI_getUnsafeObject};
methodMap_["getRootTag"] = MethodMetadata{
.argCount = 1,
.invoker = __hostFunction_NativeSampleTurboModuleSpecJSI_getRootTag};
methodMap_["getValue"] = MethodMetadata{
.argCount = 3,
.invoker = __hostFunction_NativeSampleTurboModuleSpecJSI_getValue};
methodMap_["getValueWithCallback"] = MethodMetadata{
.argCount = 1,
.invoker =
__hostFunction_NativeSampleTurboModuleSpecJSI_getValueWithCallback};
methodMap_["getValueWithPromise"] = MethodMetadata{
.argCount = 1,
.invoker =
__hostFunction_NativeSampleTurboModuleSpecJSI_getValueWithPromise};
methodMap_["voidFuncThrows"] = MethodMetadata{
.argCount = 0,
.invoker = __hostFunction_NativeSampleTurboModuleSpecJSI_voidFuncThrows};
methodMap_["getObjectThrows"] = MethodMetadata{
.argCount = 1,
.invoker = __hostFunction_NativeSampleTurboModuleSpecJSI_getObjectThrows};
methodMap_["promiseThrows"] = MethodMetadata{
.argCount = 0,
.invoker = __hostFunction_NativeSampleTurboModuleSpecJSI_promiseThrows};
methodMap_["voidFuncAssert"] = MethodMetadata{
.argCount = 0,
.invoker = __hostFunction_NativeSampleTurboModuleSpecJSI_voidFuncAssert};
methodMap_["getObjectAssert"] = MethodMetadata{
.argCount = 1,
.invoker = __hostFunction_NativeSampleTurboModuleSpecJSI_getObjectAssert};
methodMap_["promiseAssert"] = MethodMetadata{
.argCount = 0,
.invoker = __hostFunction_NativeSampleTurboModuleSpecJSI_promiseAssert};
methodMap_["getImageUrl"] = MethodMetadata{
.argCount = 0,
.invoker = __hostFunction_NativeSampleTurboModuleSpecJSI_getImageUrl};
eventEmitterMap_["onPress"] =
std::make_shared<AsyncEventEmitter<folly::dynamic>>();
eventEmitterMap_["onClick"] =
std::make_shared<AsyncEventEmitter<folly::dynamic>>();
eventEmitterMap_["onChange"] =
std::make_shared<AsyncEventEmitter<folly::dynamic>>();
eventEmitterMap_["onSubmit"] =
std::make_shared<AsyncEventEmitter<folly::dynamic>>();
configureEventEmitterCallback();
}
std::shared_ptr<TurboModule> SampleTurboModuleSpec_ModuleProvider(
const std::string& moduleName,
const JavaTurboModule::InitParams& params) {
if (moduleName == "SampleTurboModule") {
return std::make_shared<NativeSampleTurboModuleSpecJSI>(params);
}
return nullptr;
}
} // namespace facebook::react

View File

@@ -0,0 +1,31 @@
/*
* 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.
*/
// NOTE: This entire file should be codegen'ed.
#pragma once
#include <ReactCommon/JavaTurboModule.h>
#include <ReactCommon/TurboModule.h>
#include <fbjni/fbjni.h>
namespace facebook::react {
/**
* JNI C++ class for module 'NativeSampleTurboModule'
*/
class JSI_EXPORT NativeSampleTurboModuleSpecJSI : public JavaTurboModule {
public:
NativeSampleTurboModuleSpecJSI(const JavaTurboModule::InitParams &params);
};
JSI_EXPORT
std::shared_ptr<TurboModule> SampleTurboModuleSpec_ModuleProvider(
const std::string &moduleName,
const JavaTurboModule::InitParams &params);
} // namespace facebook::react

View File

@@ -0,0 +1,281 @@
/*
* 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.fbreact.specs
import android.widget.Toast
import com.facebook.proguard.annotations.DoNotStrip
import com.facebook.react.bridge.Arguments
import com.facebook.react.bridge.Callback
import com.facebook.react.bridge.Dynamic
import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactContextBaseJavaModule
import com.facebook.react.bridge.ReactMethod
import com.facebook.react.bridge.ReadableArray
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.bridge.ReadableType
import com.facebook.react.bridge.WritableArray
import com.facebook.react.bridge.WritableMap
import com.facebook.react.bridge.WritableNativeArray
import com.facebook.react.bridge.WritableNativeMap
import com.facebook.react.module.annotations.ReactModule
@ReactModule(name = SampleLegacyModule.NAME)
public class SampleLegacyModule(private val context: ReactApplicationContext) :
ReactContextBaseJavaModule(context) {
private var toast: Toast? = null
@DoNotStrip
@Suppress("unused")
@ReactMethod(isBlockingSynchronousMethod = true)
public fun getBool(arg: Boolean?): Boolean? {
log("getBool", arg, arg)
return arg
}
@DoNotStrip
@Suppress("unused")
@ReactMethod(isBlockingSynchronousMethod = true)
public fun getEnum(arg: Double?): Double? {
log("getEnum", arg, arg)
return arg
}
@DoNotStrip
@Suppress("unused")
@ReactMethod(isBlockingSynchronousMethod = true)
public fun getDouble(arg: Double?): Double? {
log("getDouble", arg, arg)
return arg
}
@DoNotStrip
@Suppress("unused")
@ReactMethod(isBlockingSynchronousMethod = true)
public fun getInt(arg: Int?): Int? {
log("getInt", arg, arg)
return arg
}
@DoNotStrip
@Suppress("unused")
@ReactMethod(isBlockingSynchronousMethod = true)
public fun getFloat(arg: Float?): Float? {
log("getFloat", arg, arg)
return arg
}
@DoNotStrip
@Suppress("unused")
@ReactMethod(isBlockingSynchronousMethod = true)
public fun getObjectDouble(arg: Double?): Double? {
log("getObjectDouble", arg, arg)
return arg
}
@DoNotStrip
@Suppress("unused")
@ReactMethod(isBlockingSynchronousMethod = true)
public fun getObjectInteger(arg: Int?): Int? {
log("getObjectInteger", arg, arg)
return arg
}
@DoNotStrip
@Suppress("unused")
@ReactMethod(isBlockingSynchronousMethod = true)
public fun getObjectFloat(arg: Float?): Float? {
log("getObjectFloat", arg, arg)
return arg
}
@DoNotStrip
@Suppress("unused")
@ReactMethod(isBlockingSynchronousMethod = true)
public fun getString(arg: String?): String? {
log("getString", arg, arg)
return arg
}
@DoNotStrip
@Suppress("unused")
@ReactMethod(isBlockingSynchronousMethod = true)
public fun getRootTag(arg: Double?): Double? {
log("getRootTag", arg, arg)
return arg
}
@DoNotStrip
@Suppress("unused")
@ReactMethod
public fun voidFunc() {
log("voidFunc", "<void>", "<void>")
return
}
// This function returns {@link WritableMap} instead of {@link Map} for backward compat with
// existing native modules that use this Writable* as return types or in events. {@link
// WritableMap} is modified in the Java side, and read (or consumed) on the C++ side.
// In the future, all native modules should ideally return an immutable Map
@DoNotStrip
@Suppress("unused")
@ReactMethod(isBlockingSynchronousMethod = true)
public fun getObject(arg: ReadableMap?): WritableMap {
val map = WritableNativeMap()
arg?.let { map.merge(it) }
log("getObject", arg, map)
return map
}
@DoNotStrip
@Suppress("unused")
@ReactMethod(isBlockingSynchronousMethod = true)
public fun getUnsafeObject(arg: ReadableMap?): WritableMap {
val map = WritableNativeMap()
arg?.let { map.merge(it) }
log("getUnsafeObject", arg, map)
return map
}
@Suppress("unused")
@ReactMethod(isBlockingSynchronousMethod = true)
public fun getDynamic(dynamic: Dynamic?): WritableMap {
val resultMap = WritableNativeMap()
when (dynamic?.type) {
ReadableType.Null -> {
log("getDynamic as Null", dynamic, dynamic)
resultMap.putString("type", "Null")
resultMap.putNull("value")
}
ReadableType.Boolean -> {
val result = dynamic.asBoolean()
log("getDynamic as Boolean", dynamic, result)
resultMap.putString("type", "Boolean")
resultMap.putBoolean("value", result)
}
ReadableType.Number -> {
val result = dynamic.asInt()
log("getDynamic as Number", dynamic, result)
resultMap.putString("type", "Number")
resultMap.putInt("value", result)
}
ReadableType.String -> {
val result = dynamic.asString()
log("getDynamic as String", dynamic, result)
resultMap.putString("type", "String")
resultMap.putString("value", result)
}
ReadableType.Array -> {
val result = dynamic.asArray()
log("getDynamic as Array", dynamic, result)
resultMap.putString("type", "Array")
resultMap.putArray("value", result)
}
ReadableType.Map -> {
val result = dynamic.asMap()
log("getDynamic as Map", dynamic, result)
resultMap.putString("type", "Map")
resultMap.putMap("value", result)
}
else -> error("Unsupported dynamic type")
}
return resultMap
}
@DoNotStrip
@Suppress("unused")
@ReactMethod(isBlockingSynchronousMethod = true)
public fun getValue(numberArg: Double?, stringArg: String?, mapArg: ReadableMap?): WritableMap {
val map: WritableMap =
WritableNativeMap().apply {
putDouble("x", numberArg ?: 0.0)
putString("y", stringArg)
}
val zMap: WritableMap = WritableNativeMap()
mapArg?.let { zMap.merge(it) }
map.putMap("z", zMap)
log(
"getValue",
mapOf("1-numberArg" to numberArg, "2-stringArg" to stringArg, "3-mapArg" to mapArg),
map,
)
return map
}
@DoNotStrip
@Suppress("unused")
@ReactMethod
public fun getValueWithCallback(callback: Callback?) {
val result = "Value From Callback"
log("Callback", "Return Time", result)
callback?.invoke(result)
}
@DoNotStrip
@Suppress("unused")
@ReactMethod(isBlockingSynchronousMethod = true)
public fun getArray(arg: ReadableArray?): WritableArray {
if (arg == null || Arguments.toList(arg) == null) {
// Returning an empty array, since the super class always returns non-null
return WritableNativeArray()
}
val result: WritableArray = Arguments.makeNativeArray(Arguments.toList(arg))
log("getArray", arg, result)
return result
}
@DoNotStrip
@Suppress("unused")
@ReactMethod
public fun getValueWithPromise(error: Boolean, promise: Promise?) {
if (error) {
promise?.reject(
"code 1",
"intentional promise rejection",
Throwable("promise intentionally rejected"),
)
} else {
promise?.resolve("result")
}
}
override fun getConstants(): Map<String, Any> {
val result: MutableMap<String, Any> = mutableMapOf()
val activity = context.currentActivity
if (activity != null) {
result["const2"] = 390
}
result["const1"] = true
result["const3"] = "something"
log("constantsToExport", "", result)
return result
}
private fun log(method: String, input: Any?, output: Any?) {
toast?.cancel()
val message = StringBuilder("Method :")
message
.append(method)
.append("\nInputs: ")
.append(input.toString())
.append("\nOutputs: ")
.append(output.toString())
toast = Toast.makeText(context, message.toString(), Toast.LENGTH_LONG)
toast?.show()
}
override fun invalidate(): Unit = Unit
override fun getName(): String {
return NAME
}
public companion object {
public const val NAME: String = "SampleLegacyModule"
}
}

View File

@@ -0,0 +1,276 @@
/*
* 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.fbreact.specs
import android.net.Uri
import android.os.Build
import android.util.DisplayMetrics
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.result.contract.ActivityResultContracts
import com.facebook.proguard.annotations.DoNotStrip
import com.facebook.react.bridge.Arguments
import com.facebook.react.bridge.Callback
import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReadableArray
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.bridge.WritableArray
import com.facebook.react.bridge.WritableMap
import com.facebook.react.bridge.WritableNativeArray
import com.facebook.react.bridge.WritableNativeMap
import com.facebook.react.module.annotations.ReactModule
import com.facebook.react.turbomodule.core.interfaces.BindingsInstallerHolder
import com.facebook.react.turbomodule.core.interfaces.TurboModuleWithJSIBindings
import java.util.UUID
@DoNotStrip
@ReactModule(name = SampleTurboModule.NAME)
public class SampleTurboModule(private val context: ReactApplicationContext) :
NativeSampleTurboModuleSpec(context), TurboModuleWithJSIBindings {
private var toast: Toast? = null
@DoNotStrip
override fun getBool(arg: Boolean): Boolean {
log("getBool", arg, arg)
return arg
}
@DoNotStrip
override fun getEnum(arg: Double): Double {
log("getEnum", arg, arg)
return arg
}
override fun getTypedExportedConstants(): MutableMap<String, Any> {
val result: MutableMap<String, Any> = mutableMapOf()
val activity = context.currentActivity
if (activity != null) {
@Suppress("DEPRECATION")
val widthPixels =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
activity.windowManager.currentWindowMetrics.bounds.width()
} else {
val displayMetrics = DisplayMetrics()
activity.windowManager.defaultDisplay.getMetrics(displayMetrics)
displayMetrics.widthPixels
}
result["const2"] = widthPixels
}
result["const1"] = true
result["const3"] = "something"
log("constantsToExport", "", result)
return result
}
@DoNotStrip
override fun getNumber(arg: Double): Double {
log("getNumber", arg, arg)
return arg
}
@DoNotStrip
override fun getString(arg: String?): String? {
log("getString", arg, arg)
return arg
}
@DoNotStrip
@Suppress("unused")
override fun getRootTag(arg: Double): Double {
log("getRootTag", arg, arg)
return arg
}
@DoNotStrip
override fun voidFunc() {
log("voidFunc", "<void>", "<void>")
emitOnPress()
emitOnClick("click")
run {
val map =
WritableNativeMap().apply {
putInt("a", 1)
putString("b", "two")
}
emitOnChange(map)
}
run {
val array = WritableNativeArray()
val map1 =
WritableNativeMap().apply {
putInt("a", 1)
putString("b", "two")
}
val map2 =
WritableNativeMap().apply {
putInt("a", 3)
putString("b", "four")
}
array.pushMap(map1)
array.pushMap(map2)
emitOnSubmit(array)
}
}
// This function returns {@link WritableMap} instead of {@link Map} for backward compat with
// existing native modules that use this Writable* as return types or in events. {@link
// WritableMap} is modified in the Java side, and read (or consumed) on the C++ side.
// In the future, all native modules should ideally return an immutable Map
@DoNotStrip
@Suppress("unused")
override fun getObject(arg: ReadableMap?): WritableMap {
val map = WritableNativeMap()
arg?.let { map.merge(it) }
log("getObject", arg, map)
return map
}
@DoNotStrip
@Suppress("unused")
override fun getUnsafeObject(arg: ReadableMap?): WritableMap {
val map = WritableNativeMap()
arg?.let { map.merge(it) }
log("getUnsafeObject", arg, map)
return map
}
@DoNotStrip
@Suppress("unused")
override fun getValue(x: Double, y: String?, z: ReadableMap?): WritableMap {
val map: WritableMap = WritableNativeMap()
map.putDouble("x", x)
map.putString("y", y)
val zMap: WritableMap = WritableNativeMap()
z?.let { zMap.merge(it) }
map.putMap("z", zMap)
log("getValue", mapOf("1-numberArg" to x, "2-stringArg" to y, "3-mapArg" to z), map)
return map
}
@DoNotStrip
@Suppress("unused")
override fun getValueWithCallback(callback: Callback?) {
val result = "Value From Callback"
log("Callback", "Return Time", result)
callback?.invoke(result)
}
@DoNotStrip
@Suppress("unused")
override fun getArray(arg: ReadableArray?): WritableArray {
if (arg == null || Arguments.toList(arg) == null) {
// Returning an empty array, since the super class always returns non-null
return WritableNativeArray()
}
val result: WritableArray = Arguments.makeNativeArray(Arguments.toList(arg))
log("getArray", arg, result)
return result
}
@DoNotStrip
@Suppress("unused")
override fun getValueWithPromise(error: Boolean, promise: Promise) {
if (error) {
promise?.reject(
"code 1",
"intentional promise rejection",
Throwable("promise intentionally rejected"),
)
} else {
promise?.resolve("result")
}
}
@DoNotStrip
@Suppress("unused")
override fun voidFuncThrows() {
error("Intentional exception from JVM voidFuncThrows")
}
@DoNotStrip
@Suppress("unused")
override fun getObjectThrows(arg: ReadableMap): WritableMap {
error("Intentional exception from JVM getObjectThrows with $arg")
}
@DoNotStrip
@Suppress("unused")
override fun promiseThrows(promise: Promise) {
error("Intentional exception from JVM promiseThrows")
}
@DoNotStrip
@Suppress("unused")
override fun voidFuncAssert() {
assert(false) { "Intentional assert from JVM voidFuncAssert" }
}
@DoNotStrip
@Suppress("unused")
override fun getObjectAssert(arg: ReadableMap): WritableMap? {
assert(false) { "Intentional assert from JVM getObjectAssert with $arg" }
return null
}
@DoNotStrip
@Suppress("unused")
override fun promiseAssert(promise: Promise) {
assert(false) { "Intentional assert from JVM promiseAssert" }
}
@DoNotStrip
@Suppress("unused")
override fun getImageUrl(promise: Promise) {
val activity = context.getCurrentActivity() as? ComponentActivity
if (activity != null) {
val key = UUID.randomUUID().toString()
activity.activityResultRegistry
.register(
key,
ActivityResultContracts.GetContent(),
{ uri: Uri? ->
if (uri != null) {
promise.resolve(uri.toString())
} else {
promise.resolve(null)
}
},
)
.launch("image/*")
} else {
promise.reject("error", "Unable to obtain an image uri without current activity")
}
}
private fun log(method: String, input: Any?, output: Any?) {
toast?.cancel()
val message = StringBuilder("Method :")
message
.append(method)
.append("\nInputs: ")
.append(input.toString())
.append("\nOutputs: ")
.append(output.toString())
toast = Toast.makeText(context, message.toString(), Toast.LENGTH_LONG)
toast?.show()
}
override fun invalidate(): Unit = Unit
override fun getName(): String {
return NAME
}
@DoNotStrip external override fun getBindingsInstaller(): BindingsInstallerHolder
public companion object {
public const val NAME: String = "SampleTurboModule"
}
}

View File

@@ -0,0 +1,71 @@
/*
* 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.
*/
// NOTE: This entire file should be codegen'ed.
#import <vector>
#import <Foundation/Foundation.h>
#import <React/RCTBridgeModule.h>
#import <ReactCommon/RCTTurboModule.h>
NS_ASSUME_NONNULL_BEGIN
/**
* The ObjC protocol based on the JS Flow type for SampleTurboModule.
*/
@protocol NativeSampleTurboModuleSpec <RCTBridgeModule, RCTTurboModule>
- (void)voidFunc;
- (NSNumber *)getBool:(BOOL)arg;
- (NSNumber *)getEnum:(double)arg;
- (NSNumber *)getNumber:(double)arg;
- (NSString *)getString:(NSString *)arg;
- (NSArray<id<NSObject>> *)getArray:(NSArray *)arg;
- (NSDictionary *)getObject:(NSDictionary *)arg;
- (NSDictionary *)getUnsafeObject:(NSDictionary *)arg;
- (NSNumber *)getRootTag:(double)arg;
- (NSDictionary *)getValue:(double)x y:(NSString *)y z:(NSDictionary *)z;
- (void)getValueWithCallback:(RCTResponseSenderBlock)callback;
- (void)getValueWithPromise:(BOOL)error resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject;
- (void)voidFuncThrows;
- (NSDictionary *)getObjectThrows:(NSDictionary *)arg;
- (void)promiseThrows:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject;
- (void)voidFuncAssert;
- (NSDictionary *)getObjectAssert:(NSDictionary *)arg;
- (void)promiseAssert:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject;
- (NSDictionary *)constantsToExport;
- (NSDictionary *)getConstants;
@end
@interface NativeSampleTurboModuleSpecBase : NSObject {
@protected
facebook::react::EventEmitterCallback _eventEmitterCallback;
}
- (void)setEventEmitterCallback:(EventEmitterCallbackWrapper *_Nonnull)eventEmitterCallbackWrapper;
- (void)emitOnPress;
- (void)emitOnClick:(NSString *)value;
- (void)emitOnChange:(NSDictionary *)value;
- (void)emitOnSubmit:(NSArray *)value;
@end
namespace facebook::react {
/**
* The iOS TurboModule impl specific to SampleTurboModule.
*/
class JSI_EXPORT NativeSampleTurboModuleSpecJSI : public ObjCTurboModule {
public:
NativeSampleTurboModuleSpecJSI(const ObjCTurboModule::InitParams &params);
};
} // namespace facebook::react
NS_ASSUME_NONNULL_END

View File

@@ -0,0 +1,280 @@
/*
* 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.
*/
#import "RCTNativeSampleTurboModuleSpec.h"
namespace facebook::react {
static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_voidFunc(
facebook::jsi::Runtime &rt,
TurboModule &turboModule,
const facebook::jsi::Value *args,
size_t count)
{
return static_cast<ObjCTurboModule &>(turboModule)
.invokeObjCMethod(rt, VoidKind, "voidFunc", @selector(voidFunc), args, count);
}
static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getBool(
facebook::jsi::Runtime &rt,
TurboModule &turboModule,
const facebook::jsi::Value *args,
size_t count)
{
return static_cast<ObjCTurboModule &>(turboModule)
.invokeObjCMethod(rt, BooleanKind, "getBool", @selector(getBool:), args, count);
}
static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getEnum(
facebook::jsi::Runtime &rt,
TurboModule &turboModule,
const facebook::jsi::Value *args,
size_t count)
{
return static_cast<ObjCTurboModule &>(turboModule)
.invokeObjCMethod(rt, NumberKind, "getEnum", @selector(getEnum:), args, count);
}
static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getNumber(
facebook::jsi::Runtime &rt,
TurboModule &turboModule,
const facebook::jsi::Value *args,
size_t count)
{
return static_cast<ObjCTurboModule &>(turboModule)
.invokeObjCMethod(rt, NumberKind, "getNumber", @selector(getNumber:), args, count);
}
static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getString(
facebook::jsi::Runtime &rt,
TurboModule &turboModule,
const facebook::jsi::Value *args,
size_t count)
{
return static_cast<ObjCTurboModule &>(turboModule)
.invokeObjCMethod(rt, StringKind, "getString", @selector(getString:), args, count);
}
static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getArray(
facebook::jsi::Runtime &rt,
TurboModule &turboModule,
const facebook::jsi::Value *args,
size_t count)
{
return static_cast<ObjCTurboModule &>(turboModule)
.invokeObjCMethod(rt, ArrayKind, "getArray", @selector(getArray:), args, count);
}
static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getObject(
facebook::jsi::Runtime &rt,
TurboModule &turboModule,
const facebook::jsi::Value *args,
size_t count)
{
return static_cast<ObjCTurboModule &>(turboModule)
.invokeObjCMethod(rt, ObjectKind, "getObject", @selector(getObject:), args, count);
}
static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getUnsafeObject(
facebook::jsi::Runtime &rt,
TurboModule &turboModule,
const facebook::jsi::Value *args,
size_t count)
{
return static_cast<ObjCTurboModule &>(turboModule)
.invokeObjCMethod(rt, ObjectKind, "getUnsafeObject", @selector(getUnsafeObject:), args, count);
}
static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getRootTag(
facebook::jsi::Runtime &rt,
TurboModule &turboModule,
const facebook::jsi::Value *args,
size_t count)
{
return static_cast<ObjCTurboModule &>(turboModule)
.invokeObjCMethod(rt, NumberKind, "getRootTag", @selector(getRootTag:), args, count);
}
static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getValue(
facebook::jsi::Runtime &rt,
TurboModule &turboModule,
const facebook::jsi::Value *args,
size_t count)
{
return static_cast<ObjCTurboModule &>(turboModule)
.invokeObjCMethod(rt, ObjectKind, "getValue", @selector(getValue:y:z:), args, count);
}
static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getValueWithCallback(
facebook::jsi::Runtime &rt,
TurboModule &turboModule,
const facebook::jsi::Value *args,
size_t count)
{
return static_cast<ObjCTurboModule &>(turboModule)
.invokeObjCMethod(rt, VoidKind, "getValueWithCallback", @selector(getValueWithCallback:), args, count);
}
static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getValueWithPromise(
facebook::jsi::Runtime &rt,
TurboModule &turboModule,
const facebook::jsi::Value *args,
size_t count)
{
return static_cast<ObjCTurboModule &>(turboModule)
.invokeObjCMethod(
rt, PromiseKind, "getValueWithPromise", @selector(getValueWithPromise:resolve:reject:), args, count);
}
static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_voidFuncThrows(
facebook::jsi::Runtime &rt,
TurboModule &turboModule,
const facebook::jsi::Value *args,
size_t count)
{
return static_cast<ObjCTurboModule &>(turboModule)
.invokeObjCMethod(rt, VoidKind, "voidFuncThrows", @selector(voidFuncThrows), args, count);
}
static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getObjectThrows(
facebook::jsi::Runtime &rt,
TurboModule &turboModule,
const facebook::jsi::Value *args,
size_t count)
{
return static_cast<ObjCTurboModule &>(turboModule)
.invokeObjCMethod(rt, ObjectKind, "getObjectThrows", @selector(getObjectThrows:), args, count);
}
static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_promiseThrows(
facebook::jsi::Runtime &rt,
TurboModule &turboModule,
const facebook::jsi::Value *args,
size_t count)
{
return static_cast<ObjCTurboModule &>(turboModule)
.invokeObjCMethod(rt, PromiseKind, "promiseThrows", @selector(promiseThrows:reject:), args, count);
}
static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_voidFuncAssert(
facebook::jsi::Runtime &rt,
TurboModule &turboModule,
const facebook::jsi::Value *args,
size_t count)
{
return static_cast<ObjCTurboModule &>(turboModule)
.invokeObjCMethod(rt, VoidKind, "voidFuncAssert", @selector(voidFuncAssert), args, count);
}
static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getObjectAssert(
facebook::jsi::Runtime &rt,
TurboModule &turboModule,
const facebook::jsi::Value *args,
size_t count)
{
return static_cast<ObjCTurboModule &>(turboModule)
.invokeObjCMethod(rt, ObjectKind, "getObjectAssert", @selector(getObjectAssert:), args, count);
}
static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_promiseAssert(
facebook::jsi::Runtime &rt,
TurboModule &turboModule,
const facebook::jsi::Value *args,
size_t count)
{
return static_cast<ObjCTurboModule &>(turboModule)
.invokeObjCMethod(rt, PromiseKind, "promiseAssert", @selector(promiseAssert:reject:), args, count);
}
static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getConstants(
facebook::jsi::Runtime &rt,
TurboModule &turboModule,
const facebook::jsi::Value *args,
size_t count)
{
return static_cast<ObjCTurboModule &>(turboModule)
.invokeObjCMethod(rt, ObjectKind, "getConstants", @selector(getConstants), args, count);
}
NativeSampleTurboModuleSpecJSI::NativeSampleTurboModuleSpecJSI(const ObjCTurboModule::InitParams &params)
: ObjCTurboModule(params)
{
methodMap_["voidFunc"] =
MethodMetadata{.argCount = 0, .invoker = __hostFunction_NativeSampleTurboModuleSpecJSI_voidFunc};
methodMap_["getBool"] =
MethodMetadata{.argCount = 1, .invoker = __hostFunction_NativeSampleTurboModuleSpecJSI_getBool};
methodMap_["getEnum"] =
MethodMetadata{.argCount = 1, .invoker = __hostFunction_NativeSampleTurboModuleSpecJSI_getEnum};
methodMap_["getNumber"] =
MethodMetadata{.argCount = 1, .invoker = __hostFunction_NativeSampleTurboModuleSpecJSI_getNumber};
methodMap_["getString"] =
MethodMetadata{.argCount = 1, .invoker = __hostFunction_NativeSampleTurboModuleSpecJSI_getString};
methodMap_["getArray"] =
MethodMetadata{.argCount = 1, .invoker = __hostFunction_NativeSampleTurboModuleSpecJSI_getArray};
methodMap_["getObject"] =
MethodMetadata{.argCount = 1, .invoker = __hostFunction_NativeSampleTurboModuleSpecJSI_getObject};
methodMap_["getUnsafeObject"] =
MethodMetadata{.argCount = 1, .invoker = __hostFunction_NativeSampleTurboModuleSpecJSI_getUnsafeObject};
methodMap_["getRootTag"] =
MethodMetadata{.argCount = 1, .invoker = __hostFunction_NativeSampleTurboModuleSpecJSI_getRootTag};
methodMap_["getValue"] =
MethodMetadata{.argCount = 3, .invoker = __hostFunction_NativeSampleTurboModuleSpecJSI_getValue};
methodMap_["getValueWithCallback"] =
MethodMetadata{.argCount = 1, .invoker = __hostFunction_NativeSampleTurboModuleSpecJSI_getValueWithCallback};
methodMap_["getValueWithPromise"] =
MethodMetadata{.argCount = 1, .invoker = __hostFunction_NativeSampleTurboModuleSpecJSI_getValueWithPromise};
methodMap_["voidFuncThrows"] =
MethodMetadata{.argCount = 0, .invoker = __hostFunction_NativeSampleTurboModuleSpecJSI_voidFuncThrows};
methodMap_["getObjectThrows"] =
MethodMetadata{.argCount = 1, .invoker = __hostFunction_NativeSampleTurboModuleSpecJSI_getObjectThrows};
methodMap_["promiseThrows"] =
MethodMetadata{.argCount = 0, .invoker = __hostFunction_NativeSampleTurboModuleSpecJSI_promiseThrows};
methodMap_["voidFuncAssert"] =
MethodMetadata{.argCount = 0, .invoker = __hostFunction_NativeSampleTurboModuleSpecJSI_voidFuncAssert};
methodMap_["getObjectAssert"] =
MethodMetadata{.argCount = 1, .invoker = __hostFunction_NativeSampleTurboModuleSpecJSI_getObjectAssert};
methodMap_["promiseAssert"] =
MethodMetadata{.argCount = 0, .invoker = __hostFunction_NativeSampleTurboModuleSpecJSI_promiseAssert};
methodMap_["getConstants"] =
MethodMetadata{.argCount = 0, .invoker = __hostFunction_NativeSampleTurboModuleSpecJSI_getConstants};
eventEmitterMap_["onPress"] = std::make_shared<AsyncEventEmitter<id>>();
eventEmitterMap_["onClick"] = std::make_shared<AsyncEventEmitter<id>>();
eventEmitterMap_["onChange"] = std::make_shared<AsyncEventEmitter<id>>();
eventEmitterMap_["onSubmit"] = std::make_shared<AsyncEventEmitter<id>>();
setEventEmitterCallback([&](const std::string &name, id value) {
static_cast<AsyncEventEmitter<id> &>(*eventEmitterMap_[name]).emit(value);
});
}
} // namespace facebook::react
@implementation NativeSampleTurboModuleSpecBase
- (void)setEventEmitterCallback:(EventEmitterCallbackWrapper *_Nonnull)eventEmitterCallbackWrapper
{
_eventEmitterCallback = std::move(eventEmitterCallbackWrapper->_eventEmitterCallback);
}
- (void)emitOnPress
{
_eventEmitterCallback("onPress", nil);
}
- (void)emitOnClick:(NSString *)value
{
_eventEmitterCallback("onClick", value);
}
- (void)emitOnChange:(NSDictionary *)value
{
_eventEmitterCallback("onChange", value);
}
- (void)emitOnSubmit:(NSArray *)value
{
_eventEmitterCallback("onSubmit", value);
}
@end

View File

@@ -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.
*/
#import <UIKit/UIKit.h>
#import <React/RCTAssert.h>
#import <React/RCTBridgeModule.h>
#import <React/RCTInvalidating.h>
#import <React/RCTUtils.h>
@interface RCTSampleLegacyModule : NSObject <RCTBridgeModule, RCTInvalidating>
@end

View File

@@ -0,0 +1,211 @@
/*
* 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.
*/
#import "RCTSampleLegacyModule.h"
@implementation RCTSampleLegacyModule {
RCTBridge *_bridge;
}
// Backward-compatible export
RCT_EXPORT_MODULE()
// Backward-compatible queue configuration
+ (BOOL)requiresMainQueueSetup
{
return YES;
}
- (void)setBridge:(RCTBridge *)bridge
{
_bridge = bridge;
}
- (dispatch_queue_t)methodQueue
{
return dispatch_get_main_queue();
}
// Backward compatible invalidation
- (void)invalidate
{
// Actually do nothing here.
NSLog(@"Invalidating RCTSampleTurboModule...");
}
- (NSDictionary *)getConstants
{
return @{
@"const1" : @YES,
@"const2" : @(390),
@"const3" : @"something",
};
}
// TODO: Remove once fully migrated to TurboModule.
- (NSDictionary *)constantsToExport
{
return [self getConstants];
}
RCT_EXPORT_METHOD(voidFunc)
{
// Nothing to do
}
RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSNumber *, getBool : (BOOL)arg)
{
return @(arg);
}
RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSNumber *, getEnum : (double)arg)
{
return @(arg);
}
RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSNumber *, getNumber : (double)arg)
{
return @(arg);
}
RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSNumber *, getFloat : (float)arg)
{
return @(arg);
}
RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSNumber *, getInt : (int)arg)
{
return @(arg);
}
RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSNumber *, getLongLong : (int64_t)arg)
{
return @(arg);
}
RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSNumber *, getUnsignedLongLong : (uint64_t)arg)
{
return @(arg);
}
RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSNumber *, getNSInteger : (NSInteger)arg)
{
return @(arg);
}
RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSNumber *, getNSUInteger : (NSUInteger)arg)
{
return @(arg);
}
RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSArray<id<NSObject>> *, getArray : (NSArray *)arg)
{
return arg;
}
RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSDictionary *, getObject : (NSDictionary *)arg)
{
return arg;
}
RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSString *, getString : (NSString *)arg)
{
return arg;
}
RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSNumber *, getNSNumber : (nonnull NSNumber *)arg)
{
return arg;
}
RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSDictionary *, getUnsafeObject : (NSDictionary *)arg)
{
return arg;
}
RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSNumber *, getRootTag : (double)arg)
{
return @(arg);
}
RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSDictionary *, getValue : (double)x y : (NSString *)y z : (NSDictionary *)z)
{
return @{
@"x" : @(x),
@"y" : (y != nullptr) ? y : [NSNull null],
@"z" : (z != nullptr) ? z : [NSNull null],
};
}
RCT_EXPORT_METHOD(getValueWithCallback : (RCTResponseSenderBlock)callback)
{
if (callback == nullptr) {
return;
}
callback(@[ @"value from callback!" ]);
}
RCT_EXPORT_METHOD(
getValueWithPromise : (BOOL)error resolve : (RCTPromiseResolveBlock)resolve reject : (RCTPromiseRejectBlock)reject)
{
if ((resolve == nullptr) || (reject == nullptr)) {
return;
}
if (error) {
reject(
@"code_1",
@"intentional promise rejection",
[NSError errorWithDomain:@"RCTSampleTurboModule" code:1 userInfo:nil]);
} else {
resolve(@"result!");
}
}
RCT_EXPORT_METHOD(voidFuncThrows)
{
NSException *myException = [NSException exceptionWithName:@"Excepption"
reason:@"Intentional exception from ObjC voidFuncThrows"
userInfo:nil];
@throw myException;
}
RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSDictionary *, getObjectThrows : (NSDictionary *)arg)
{
NSException *myException = [NSException exceptionWithName:@"Excepption"
reason:@"Intentional exception from ObjC getObjectThrows"
userInfo:nil];
@throw myException;
}
RCT_EXPORT_METHOD(
promiseThrows : (BOOL)error resolve : (RCTPromiseResolveBlock)resolve reject : (RCTPromiseRejectBlock)reject)
{
NSException *myException = [NSException exceptionWithName:@"Excepption"
reason:@"Intentional exception from ObjC promiseThrows"
userInfo:nil];
@throw myException;
}
RCT_EXPORT_METHOD(voidFuncAssert)
{
RCTAssert(false, @"Intentional assert from ObjC voidFuncAssert");
}
RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSDictionary *, getObjectAssert : (NSDictionary *)arg)
{
RCTAssert(false, @"Intentional assert from ObjC getObjectAssert");
return arg;
}
RCT_EXPORT_METHOD(
promiseAssert : (BOOL)error resolve : (RCTPromiseResolveBlock)resolve reject : (RCTPromiseRejectBlock)reject)
{
RCTAssert(false, @"Intentional assert from ObjC promiseAssert");
}
@end

View File

@@ -0,0 +1,18 @@
/*
* 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.
*/
#import <Foundation/Foundation.h>
#import "RCTNativeSampleTurboModuleSpec.h"
/**
* Sample iOS-specific impl of a TurboModule, conforming to the spec protocol.
* This class is also 100% compatible with the NativeModule system.
*/
@interface RCTSampleTurboModule : NativeSampleTurboModuleSpecBase <NativeSampleTurboModuleSpec>
@end

View File

@@ -0,0 +1,214 @@
/*
* 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.
*/
#import "RCTSampleTurboModule.h"
#import "RCTSampleTurboModulePlugin.h"
#import <React/RCTAssert.h>
#import <React/RCTInitializing.h>
#import <React/RCTUtils.h>
#import <ReactCommon/RCTTurboModuleWithJSIBindings.h>
#import <UIKit/UIKit.h>
using namespace facebook::react;
@interface RCTSampleTurboModule () <RCTTurboModuleWithJSIBindings, RCTInitializing>
@end
@implementation RCTSampleTurboModule {
NSDictionary *_constants;
}
// Backward-compatible export
RCT_EXPORT_MODULE()
// Backward-compatible queue configuration
+ (BOOL)requiresMainQueueSetup
{
return YES;
}
- (void)initialize
{
UIScreen *mainScreen = UIScreen.mainScreen;
CGSize screenSize = mainScreen.bounds.size;
_constants = @{
@"const1" : @YES,
@"const2" : @(screenSize.width),
@"const3" : @"something",
};
}
- (dispatch_queue_t)methodQueue
{
return dispatch_get_main_queue();
}
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
(const facebook::react::ObjCTurboModule::InitParams &)params
{
return std::make_shared<NativeSampleTurboModuleSpecJSI>(params);
}
// Backward compatible invalidation
- (void)invalidate
{
// Actually do nothing here.
NSLog(@"Invalidating RCTSampleTurboModule...");
}
- (NSDictionary *)getConstants
{
return _constants;
}
// TODO: Remove once fully migrated to TurboModule.
- (NSDictionary *)constantsToExport
{
return [self getConstants];
}
#pragma mark - RCTTurboModuleWithJSIBindings
- (void)installJSIBindingsWithRuntime:(facebook::jsi::Runtime &)runtime
callInvoker:(const std::shared_ptr<CallInvoker> &)callinvoker
{
runtime.global().setProperty(runtime, "__SampleTurboModuleJSIBindings", "Hello JSI!");
}
#pragma mark - Spec Methods
RCT_EXPORT_METHOD(voidFunc)
{
// Nothing to do
[self emitOnPress];
[self emitOnClick:@"click"];
[self emitOnChange:@{@"a" : @1, @"b" : @"two"}];
[self emitOnSubmit:@[ @{@"a" : @1, @"b" : @"two"}, @{@"a" : @3, @"b" : @"four"} ]];
}
RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSNumber *, getBool : (BOOL)arg)
{
return @(arg);
}
RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSNumber *, getEnum : (double)arg)
{
return @(arg);
}
RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSNumber *, getNumber : (double)arg)
{
return @(arg);
}
RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSString *, getString : (NSString *)arg)
{
return arg;
}
RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSArray<id<NSObject>> *, getArray : (NSArray *)arg)
{
return arg;
}
RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSDictionary *, getObject : (NSDictionary *)arg)
{
return arg;
}
RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSDictionary *, getUnsafeObject : (NSDictionary *)arg)
{
return arg;
}
RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSNumber *, getRootTag : (double)arg)
{
return @(arg);
}
RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSDictionary *, getValue : (double)x y : (NSString *)y z : (NSDictionary *)z)
{
return @{
@"x" : @(x),
@"y" : (y != nullptr) ? y : [NSNull null],
@"z" : (z != nullptr) ? z : [NSNull null],
};
}
RCT_EXPORT_METHOD(getValueWithCallback : (RCTResponseSenderBlock)callback)
{
if (callback == nullptr) {
return;
}
callback(@[ @"value from callback!" ]);
}
RCT_EXPORT_METHOD(
getValueWithPromise : (BOOL)error resolve : (RCTPromiseResolveBlock)resolve reject : (RCTPromiseRejectBlock)reject)
{
if ((resolve == nullptr) || (reject == nullptr)) {
return;
}
if (error) {
reject(
@"code_1",
@"intentional promise rejection",
[NSError errorWithDomain:@"RCTSampleTurboModule" code:1 userInfo:nil]);
} else {
resolve(@"result!");
}
}
RCT_EXPORT_METHOD(voidFuncThrows)
{
NSException *myException = [NSException exceptionWithName:@"Excepption"
reason:@"Intentional exception from ObjC voidFuncThrows"
userInfo:nil];
@throw myException;
}
RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSDictionary *, getObjectThrows : (NSDictionary *)arg)
{
NSException *myException = [NSException exceptionWithName:@"Excepption"
reason:@"Intentional exception from ObjC getObjectThrows"
userInfo:nil];
@throw myException;
}
RCT_EXPORT_METHOD(promiseThrows : (RCTPromiseResolveBlock)resolve reject : (RCTPromiseRejectBlock)reject)
{
NSException *myException = [NSException exceptionWithName:@"Excepption"
reason:@"Intentional exception from ObjC promiseThrows"
userInfo:nil];
@throw myException;
}
RCT_EXPORT_METHOD(voidFuncAssert)
{
RCTAssert(false, @"Intentional assert from ObjC voidFuncAssert");
}
RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSDictionary *, getObjectAssert : (NSDictionary *)arg)
{
RCTAssert(false, @"Intentional assert from ObjC getObjectAssert");
return arg;
}
RCT_EXPORT_METHOD(promiseAssert : (RCTPromiseResolveBlock)resolve reject : (RCTPromiseRejectBlock)reject)
{
RCTAssert(false, @"Intentional assert from ObjC promiseAssert");
}
@end
Class _Nonnull RCTSampleTurboModuleCls(void)
{
return RCTSampleTurboModule.class;
}

View File

@@ -0,0 +1,37 @@
/**
* 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.
*
* @generated by an internal plugin build system
*/
#ifdef RN_DISABLE_OSS_PLUGIN_HEADER
// FB Internal: Plugins.h is autogenerated by the build system.
#import "Plugins.h"
#else
// OSS-compatibility layer
#import <Foundation/Foundation.h>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wreturn-type-c-linkage"
#ifdef __cplusplus
extern "C" {
#endif
// RCTTurboModuleManagerDelegate should call this to resolve module classes.
Class _Nonnull RCTSampleTurboModuleCls(void);
#ifdef __cplusplus
}
#endif
#pragma GCC diagnostic pop
#endif // RN_DISABLE_OSS_PLUGIN_HEADER