/* * 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 #if __has_include() // CocoaPod headers on Apple #include #elif __has_include("AppSpecsJSI.h") // Cmake headers on Android #include "AppSpecsJSI.h" #else // BUCK headers #include #endif #include #include #include #include #include namespace facebook::react { #pragma mark - Structs using ConstantsStruct = NativeCxxModuleExampleConstantsStruct; template <> struct Bridging : NativeCxxModuleExampleConstantsStructBridging {}; using ObjectStruct = NativeCxxModuleExampleObjectStruct< int32_t, std::string, std::optional>; template <> struct Bridging : NativeCxxModuleExampleObjectStructBridging {}; using ValueStruct = NativeCxxModuleExampleValueStruct; template <> struct Bridging : NativeCxxModuleExampleValueStructBridging {}; #pragma mark - enums enum class CustomEnumInt : int32_t { A = 23, B = 42 }; template <> struct Bridging { static CustomEnumInt fromJs(jsi::Runtime& rt, jsi::Value rawValue) { auto value = static_cast(rawValue.asNumber()); if (value == 23) { return CustomEnumInt::A; } else if (value == 42) { return CustomEnumInt::B; } else { throw jsi::JSError(rt, "Invalid enum value"); } } static int32_t toJs(jsi::Runtime& rt, CustomEnumInt value) { return bridging::toJs(rt, static_cast(value)); } }; #pragma mark - jsi::HostObjects template class HostObjectWrapper : public jsi::HostObject { public: HostObjectWrapper(std::shared_ptr value) : value_(std::move(value)) {} std::shared_ptr getValue() const { return value_; } ~HostObjectWrapper() override = default; private: std::shared_ptr value_; }; struct CustomHostObjectRef { CustomHostObjectRef(std::string a, int32_t b) : a_(a), b_(b) {} std::string a_; int32_t b_; }; using CustomHostObject = HostObjectWrapper; #pragma mark - recursive objects using BinaryTreeNode = NativeCxxModuleExampleBinaryTreeNode; template <> struct Bridging : NativeCxxModuleExampleBinaryTreeNodeBridging {}; using GraphNode = NativeCxxModuleExampleGraphNode; template <> struct Bridging : NativeCxxModuleExampleGraphNodeBridging {}; #pragma mark - functional object properties using MenuItem = NativeCxxModuleExampleMenuItem< std::string, AsyncCallback, std::optional>; template <> struct Bridging : NativeCxxModuleExampleMenuItemBridging {}; #pragma mark - RCTDeviceEventEmitter events using CustomDeviceEvent = NativeCxxModuleExampleCustomDeviceEvent< std::string, int32_t, std::optional>; template <> struct Bridging : NativeCxxModuleExampleCustomDeviceEventBridging {}; #pragma mark - implementation class NativeCxxModuleExample : public NativeCxxModuleExampleCxxSpec { public: NativeCxxModuleExample(std::shared_ptr jsInvoker); void getValueWithCallback( jsi::Runtime& rt, AsyncCallback callback); std::function setValueCallbackWithSubscription( jsi::Runtime& rt, AsyncCallback callback); std::vector> getArray( jsi::Runtime& rt, std::vector> arg); bool getBool(jsi::Runtime& rt, bool arg); ConstantsStruct getConstants(jsi::Runtime& rt); CustomEnumInt getCustomEnum(jsi::Runtime& rt, CustomEnumInt arg); std::shared_ptr getCustomHostObject(jsi::Runtime& rt); std::string consumeCustomHostObject( jsi::Runtime& rt, std::shared_ptr arg); BinaryTreeNode getBinaryTreeNode(jsi::Runtime& rt, BinaryTreeNode arg); GraphNode getGraphNode(jsi::Runtime& rt, GraphNode arg); NativeCxxModuleExampleEnumInt getNumEnum( jsi::Runtime& rt, NativeCxxModuleExampleEnumInt arg); NativeCxxModuleExampleEnumStr getStrEnum( jsi::Runtime& rt, NativeCxxModuleExampleEnumNone arg); std::map> getMap( jsi::Runtime& rt, std::map> arg); double getNumber(jsi::Runtime& rt, double arg); ObjectStruct getObject(jsi::Runtime& rt, ObjectStruct arg); std::set getSet(jsi::Runtime& rt, std::set arg); std::string getString(jsi::Runtime& rt, std::string arg); std::string getUnion(jsi::Runtime& rt, float x, std::string y, jsi::Object z); ValueStruct getValue(jsi::Runtime& rt, double x, std::string y, ObjectStruct z); AsyncPromise getValueWithPromise(jsi::Runtime& rt, bool error); std::optional getWithWithOptionalArgs( jsi::Runtime& rt, std::optional optionalArg); void voidFunc(jsi::Runtime& rt); void setMenu(jsi::Runtime& rt, MenuItem menuItem); void emitCustomDeviceEvent(jsi::Runtime& rt, const std::string& eventName); void voidFuncThrows(jsi::Runtime& rt); ObjectStruct getObjectThrows(jsi::Runtime& rt, ObjectStruct arg); AsyncPromise promiseThrows(jsi::Runtime& rt); void voidFuncAssert(jsi::Runtime& rt); ObjectStruct getObjectAssert(jsi::Runtime& rt, ObjectStruct arg); AsyncPromise promiseAssert(jsi::Runtime& rt); private: std::optional> valueCallback_; }; } // namespace facebook::react