-
Notifications
You must be signed in to change notification settings - Fork 24.4k
/
TurboModuleBinding.cpp
72 lines (59 loc) · 1.92 KB
/
TurboModuleBinding.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "TurboModuleBinding.h"
#include <string>
#include <cxxreact/SystraceSection.h>
using namespace facebook;
namespace facebook {
namespace react {
/**
* Public API to install the TurboModule system.
*/
TurboModuleBinding::TurboModuleBinding(const TurboModuleProviderFunctionType &moduleProvider)
: moduleProvider_(moduleProvider) {}
void TurboModuleBinding::install(
jsi::Runtime &runtime,
std::shared_ptr<TurboModuleBinding> binding) {
runtime.global().setProperty(
runtime,
"__turboModuleProxy",
jsi::Function::createFromHostFunction(
runtime,
jsi::PropNameID::forAscii(runtime, "__turboModuleProxy"),
1,
[binding](jsi::Runtime& rt, const jsi::Value& thisVal, const jsi::Value* args, size_t count) {
return binding->jsProxy(rt, thisVal, args, count);
}));
}
void TurboModuleBinding::invalidate() const {
// Nothing for now.
}
std::shared_ptr<TurboModule> TurboModuleBinding::getModule(const std::string &name) {
std::shared_ptr<TurboModule> module = nullptr;
{
SystraceSection s("TurboModuleBinding::getModule", "module", name);
module = moduleProvider_(name);
}
return module;
}
jsi::Value TurboModuleBinding::jsProxy(
jsi::Runtime& runtime,
const jsi::Value& thisVal,
const jsi::Value* args,
size_t count) {
if (count != 1) {
throw std::invalid_argument("TurboModuleBinding::jsProxy arg count must be 1");
}
std::string moduleName = args[0].getString(runtime).utf8(runtime);
std::shared_ptr<TurboModule> module = getModule(moduleName);
if (module == nullptr) {
return jsi::Value::null();
}
return jsi::Object::createFromHostObject(runtime, std::move(module));
}
} // namespace react
} // namespace facebook