JSExecutor.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. #pragma once
  8. #include <memory>
  9. #include <string>
  10. #include <cxxreact/NativeModule.h>
  11. #include <folly/dynamic.h>
  12. #ifndef RN_EXPORT
  13. #define RN_EXPORT __attribute__((visibility("default")))
  14. #endif
  15. namespace facebook {
  16. namespace react {
  17. class JSBigString;
  18. class JSExecutor;
  19. class JSModulesUnbundle;
  20. class MessageQueueThread;
  21. class ModuleRegistry;
  22. class RAMBundleRegistry;
  23. // This interface describes the delegate interface required by
  24. // Executor implementations to call from JS into native code.
  25. class ExecutorDelegate {
  26. public:
  27. virtual ~ExecutorDelegate() {}
  28. virtual std::shared_ptr<ModuleRegistry> getModuleRegistry() = 0;
  29. virtual void callNativeModules(
  30. JSExecutor &executor,
  31. folly::dynamic &&calls,
  32. bool isEndOfBatch) = 0;
  33. virtual MethodCallResult callSerializableNativeHook(
  34. JSExecutor &executor,
  35. unsigned int moduleId,
  36. unsigned int methodId,
  37. folly::dynamic &&args) = 0;
  38. };
  39. using NativeExtensionsProvider =
  40. std::function<folly::dynamic(const std::string &)>;
  41. class JSExecutorFactory {
  42. public:
  43. virtual std::unique_ptr<JSExecutor> createJSExecutor(
  44. std::shared_ptr<ExecutorDelegate> delegate,
  45. std::shared_ptr<MessageQueueThread> jsQueue) = 0;
  46. virtual ~JSExecutorFactory() {}
  47. };
  48. class RN_EXPORT JSExecutor {
  49. public:
  50. /**
  51. * Prepares the JS runtime for React Native by installing global variables.
  52. * Called once before any JS is evaluated.
  53. */
  54. virtual void initializeRuntime() = 0;
  55. /**
  56. * Execute an application script bundle in the JS context.
  57. */
  58. virtual void loadBundle(
  59. std::unique_ptr<const JSBigString> script,
  60. std::string sourceURL) = 0;
  61. /**
  62. * Add an application "RAM" bundle registry
  63. */
  64. virtual void setBundleRegistry(
  65. std::unique_ptr<RAMBundleRegistry> bundleRegistry) = 0;
  66. /**
  67. * Register a file path for an additional "RAM" bundle
  68. */
  69. virtual void registerBundle(
  70. uint32_t bundleId,
  71. const std::string &bundlePath) = 0;
  72. /**
  73. * Executes BatchedBridge.callFunctionReturnFlushedQueue with the module ID,
  74. * method ID and optional additional arguments in JS. The executor is
  75. * responsible for using Bridge->callNativeModules to invoke any necessary
  76. * native modules methods.
  77. */
  78. virtual void callFunction(
  79. const std::string &moduleId,
  80. const std::string &methodId,
  81. const folly::dynamic &arguments) = 0;
  82. /**
  83. * Executes BatchedBridge.invokeCallbackAndReturnFlushedQueue with the cbID,
  84. * and optional additional arguments in JS and returns the next queue. The
  85. * executor is responsible for using Bridge->callNativeModules to invoke any
  86. * necessary native modules methods.
  87. */
  88. virtual void invokeCallback(
  89. const double callbackId,
  90. const folly::dynamic &arguments) = 0;
  91. virtual void setGlobalVariable(
  92. std::string propName,
  93. std::unique_ptr<const JSBigString> jsonValue) = 0;
  94. virtual void *getJavaScriptContext() {
  95. return nullptr;
  96. }
  97. /**
  98. * Returns whether or not the underlying executor supports debugging via the
  99. * Chrome remote debugging protocol.
  100. */
  101. virtual bool isInspectable() {
  102. return false;
  103. }
  104. /**
  105. * The description is displayed in the dev menu, if there is one in
  106. * this build. There is a default, but if this method returns a
  107. * non-empty string, it will be used instead.
  108. */
  109. virtual std::string getDescription() = 0;
  110. virtual void handleMemoryPressure(__unused int pressureLevel) {}
  111. virtual void destroy() {}
  112. virtual ~JSExecutor() {}
  113. virtual void flush() {}
  114. static std::string getSyntheticBundlePath(
  115. uint32_t bundleId,
  116. const std::string &bundlePath);
  117. };
  118. } // namespace react
  119. } // namespace facebook