SampleTurboCxxModule.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (c) Facebook, Inc. and its 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. #import "SampleTurboCxxModule.h"
  8. #import <ReactCommon/TurboModuleUtils.h>
  9. using namespace facebook;
  10. namespace facebook {
  11. namespace react {
  12. SampleTurboCxxModule::SampleTurboCxxModule(
  13. std::shared_ptr<CallInvoker> jsInvoker)
  14. : NativeSampleTurboCxxModuleSpecJSI(jsInvoker) {}
  15. void SampleTurboCxxModule::voidFunc(jsi::Runtime &rt) {
  16. // Nothing to do
  17. }
  18. bool SampleTurboCxxModule::getBool(jsi::Runtime &rt, bool arg) {
  19. return arg;
  20. }
  21. double SampleTurboCxxModule::getNumber(jsi::Runtime &rt, double arg) {
  22. return arg;
  23. }
  24. jsi::String SampleTurboCxxModule::getString(
  25. jsi::Runtime &rt,
  26. const jsi::String &arg) {
  27. return jsi::String::createFromUtf8(rt, arg.utf8(rt));
  28. }
  29. jsi::Array SampleTurboCxxModule::getArray(
  30. jsi::Runtime &rt,
  31. const jsi::Array &arg) {
  32. return deepCopyJSIArray(rt, arg);
  33. }
  34. jsi::Object SampleTurboCxxModule::getObject(
  35. jsi::Runtime &rt,
  36. const jsi::Object &arg) {
  37. return deepCopyJSIObject(rt, arg);
  38. }
  39. jsi::Object SampleTurboCxxModule::getValue(
  40. jsi::Runtime &rt,
  41. double x,
  42. const jsi::String &y,
  43. const jsi::Object &z) {
  44. // Note: return type isn't type-safe.
  45. jsi::Object result(rt);
  46. result.setProperty(rt, "x", jsi::Value(x));
  47. result.setProperty(rt, "y", jsi::String::createFromUtf8(rt, y.utf8(rt)));
  48. result.setProperty(rt, "z", deepCopyJSIObject(rt, z));
  49. return result;
  50. }
  51. void SampleTurboCxxModule::getValueWithCallback(
  52. jsi::Runtime &rt,
  53. const jsi::Function &callback) {
  54. callback.call(rt, jsi::String::createFromUtf8(rt, "value from callback!"));
  55. }
  56. jsi::Value SampleTurboCxxModule::getValueWithPromise(
  57. jsi::Runtime &rt,
  58. bool error) {
  59. return createPromiseAsJSIValue(
  60. rt, [error](jsi::Runtime &rt2, std::shared_ptr<Promise> promise) {
  61. if (error) {
  62. promise->reject("intentional promise rejection");
  63. } else {
  64. promise->resolve(jsi::String::createFromUtf8(rt2, "result!"));
  65. }
  66. });
  67. }
  68. jsi::Object SampleTurboCxxModule::getConstants(jsi::Runtime &rt) {
  69. // Note: return type isn't type-safe.
  70. jsi::Object result(rt);
  71. result.setProperty(rt, "const1", jsi::Value(true));
  72. result.setProperty(rt, "const2", jsi::Value(375));
  73. result.setProperty(
  74. rt, "const3", jsi::String::createFromUtf8(rt, "something"));
  75. return result;
  76. }
  77. } // namespace react
  78. } // namespace facebook