|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +#include "NativeCxxModuleExample.h" |
| 9 | + |
| 10 | +namespace facebook::react { |
| 11 | + |
| 12 | +NativeCxxModuleExample::NativeCxxModuleExample( |
| 13 | + std::shared_ptr<CallInvoker> jsInvoker) |
| 14 | + : NativeCxxModuleExampleCxxSpec(std::move(jsInvoker)) {} |
| 15 | + |
| 16 | +void NativeCxxModuleExample::getValueWithCallback( |
| 17 | + jsi::Runtime &rt, |
| 18 | + AsyncCallback<std::string> callback) { |
| 19 | + callback({"value from callback!"}); |
| 20 | +} |
| 21 | + |
| 22 | +std::vector<std::optional<ObjectStruct>> NativeCxxModuleExample::getArray( |
| 23 | + jsi::Runtime &rt, |
| 24 | + std::vector<std::optional<ObjectStruct>> arg) { |
| 25 | + return arg; |
| 26 | +} |
| 27 | + |
| 28 | +bool NativeCxxModuleExample::getBool(jsi::Runtime &rt, bool arg) { |
| 29 | + return arg; |
| 30 | +} |
| 31 | + |
| 32 | +ConstantsStruct NativeCxxModuleExample::getConstants(jsi::Runtime &rt) { |
| 33 | + return ConstantsStruct{true, 69, "react-native"}; |
| 34 | +} |
| 35 | + |
| 36 | +int32_t NativeCxxModuleExample::getEnum(jsi::Runtime &rt, int32_t arg) { |
| 37 | + return arg; |
| 38 | +} |
| 39 | + |
| 40 | +std::map<std::string, std::optional<int32_t>> NativeCxxModuleExample::getMap( |
| 41 | + jsi::Runtime &rt, |
| 42 | + std::map<std::string, std::optional<int32_t>> arg) { |
| 43 | + return arg; |
| 44 | +} |
| 45 | + |
| 46 | +double NativeCxxModuleExample::getNumber(jsi::Runtime &rt, double arg) { |
| 47 | + return arg; |
| 48 | +} |
| 49 | + |
| 50 | +ObjectStruct NativeCxxModuleExample::getObject( |
| 51 | + jsi::Runtime &rt, |
| 52 | + ObjectStruct arg) { |
| 53 | + return arg; |
| 54 | +} |
| 55 | + |
| 56 | +std::set<float> NativeCxxModuleExample::getSet( |
| 57 | + jsi::Runtime &rt, |
| 58 | + std::set<float> arg) { |
| 59 | + return arg; |
| 60 | +} |
| 61 | + |
| 62 | +std::string NativeCxxModuleExample::getString( |
| 63 | + jsi::Runtime &rt, |
| 64 | + std::string arg) { |
| 65 | + return arg; |
| 66 | +} |
| 67 | + |
| 68 | +std::string NativeCxxModuleExample::getUnion( |
| 69 | + jsi::Runtime &rt, |
| 70 | + float x, |
| 71 | + std::string y, |
| 72 | + jsi::Object z) { |
| 73 | + std::string result = "x: " + std::to_string(x) + ", y: " + y + ", z: { "; |
| 74 | + if (z.hasProperty(rt, "value")) { |
| 75 | + result += "value: "; |
| 76 | + result += std::to_string(z.getProperty(rt, "value").getNumber()); |
| 77 | + } else if (z.hasProperty(rt, "low")) { |
| 78 | + result += "low: "; |
| 79 | + result += z.getProperty(rt, "low").getString(rt).utf8(rt); |
| 80 | + } |
| 81 | + result += " }"; |
| 82 | + return result; |
| 83 | +} |
| 84 | + |
| 85 | +ValueStruct NativeCxxModuleExample::getValue( |
| 86 | + jsi::Runtime &rt, |
| 87 | + double x, |
| 88 | + std::string y, |
| 89 | + ObjectStruct z) { |
| 90 | + ValueStruct result{x, y, z}; |
| 91 | + return result; |
| 92 | +} |
| 93 | + |
| 94 | +AsyncPromise<std::string> NativeCxxModuleExample::getValueWithPromise( |
| 95 | + jsi::Runtime &rt, |
| 96 | + bool error) { |
| 97 | + auto promise = AsyncPromise<std::string>(rt, jsInvoker_); |
| 98 | + if (error) { |
| 99 | + promise.reject("intentional promise rejection"); |
| 100 | + } else { |
| 101 | + promise.resolve("result!"); |
| 102 | + } |
| 103 | + return promise; |
| 104 | +} |
| 105 | + |
| 106 | +void NativeCxxModuleExample::voidFunc(jsi::Runtime &rt) { |
| 107 | + // Nothing to do |
| 108 | +} |
| 109 | + |
| 110 | +} // namespace facebook::react |
0 commit comments