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 #include "TurboModule.h"
9
10 using namespace facebook;
11
12 namespace facebook {
13 namespace react {
14
TurboModule(const std::string & name,std::shared_ptr<CallInvoker> jsInvoker)15 TurboModule::TurboModule(
16 const std::string &name,
17 std::shared_ptr<CallInvoker> jsInvoker)
18 : name_(name), jsInvoker_(jsInvoker) {}
19
~TurboModule()20 TurboModule::~TurboModule() {}
21
get(jsi::Runtime & runtime,const jsi::PropNameID & propName)22 jsi::Value TurboModule::get(
23 jsi::Runtime &runtime,
24 const jsi::PropNameID &propName) {
25 std::string propNameUtf8 = propName.utf8(runtime);
26 auto p = methodMap_.find(propNameUtf8);
27 if (p == methodMap_.end()) {
28 // Method was not found, let JS decide what to do.
29 return jsi::Value::undefined();
30 }
31 MethodMetadata meta = p->second;
32 return jsi::Function::createFromHostFunction(
33 runtime,
34 propName,
35 meta.argCount,
36 [this, meta](
37 facebook::jsi::Runtime &rt,
38 const facebook::jsi::Value &thisVal,
39 const facebook::jsi::Value *args,
40 size_t count) { return meta.invoker(rt, *this, args, count); });
41 }
42
43 } // namespace react
44 } // namespace facebook
45