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 
8 #pragma once
9 
10 #include <ReactCommon/CallInvoker.h>
11 #include <jsi/jsi.h>
12 #include <memory>
13 #include <string>
14 #include <unordered_map>
15 
16 namespace facebook {
17 namespace react {
18 
19 /**
20  * For now, support the same set of return types as existing impl.
21  * This can be improved to support richer typed objects.
22  */
23 enum TurboModuleMethodValueKind {
24   VoidKind,
25   BooleanKind,
26   NumberKind,
27   StringKind,
28   ObjectKind,
29   ArrayKind,
30   FunctionKind,
31   PromiseKind,
32 };
33 
34 /**
35  * Base HostObject class for every module to be exposed to JS
36  */
37 class JSI_EXPORT TurboModule : public facebook::jsi::HostObject {
38  public:
39   TurboModule(const std::string &name, std::shared_ptr<CallInvoker> jsInvoker);
40   virtual ~TurboModule();
41 
42   facebook::jsi::Value get(
43       facebook::jsi::Runtime &runtime,
44       const facebook::jsi::PropNameID &propName) override;
45 
46   const std::string name_;
47   std::shared_ptr<CallInvoker> jsInvoker_;
48 
49  protected:
50   struct MethodMetadata {
51     size_t argCount;
52     facebook::jsi::Value (*invoker)(
53         facebook::jsi::Runtime &rt,
54         TurboModule &turboModule,
55         const facebook::jsi::Value *args,
56         size_t count);
57   };
58 
59   std::unordered_map<std::string, MethodMetadata> methodMap_;
60 };
61 
62 /**
63  * An app/platform-specific provider function to get an instance of a module
64  * given a name.
65  */
66 using TurboModuleProviderFunctionType =
67     std::function<std::shared_ptr<TurboModule>(const std::string &name)>;
68 
69 } // namespace react
70 } // namespace facebook
71