mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
1f87729697
Summary: Not having this disallows including turbo module and extending in places where RTTI is enabled. There is no additional includes or implementation changes - it merely allows for things to nicely link with other libraries. Changelog: [General][Fixed] - Allow including TurboModule.h in mixed rtti/no-rtti environment, even if TurboModule.h/cpp is compiled without RTTI. Reviewed By: appden Differential Revision: D34637168 fbshipit-source-id: 2e5d9e546bdc5652f06436fec3b12f1aa9daab05
88 lines
2.2 KiB
C++
88 lines
2.2 KiB
C++
/*
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <string>
|
|
#include <unordered_map>
|
|
|
|
#include <jsi/jsi.h>
|
|
|
|
#include <ReactCommon/CallInvoker.h>
|
|
|
|
namespace facebook {
|
|
namespace react {
|
|
|
|
/**
|
|
* For now, support the same set of return types as existing impl.
|
|
* This can be improved to support richer typed objects.
|
|
*/
|
|
enum TurboModuleMethodValueKind {
|
|
VoidKind,
|
|
BooleanKind,
|
|
NumberKind,
|
|
StringKind,
|
|
ObjectKind,
|
|
ArrayKind,
|
|
FunctionKind,
|
|
PromiseKind,
|
|
};
|
|
|
|
/**
|
|
* Base HostObject class for every module to be exposed to JS
|
|
*/
|
|
class JSI_EXPORT TurboModule : public facebook::jsi::HostObject {
|
|
public:
|
|
TurboModule(const std::string &name, std::shared_ptr<CallInvoker> jsInvoker);
|
|
|
|
virtual facebook::jsi::Value get(
|
|
facebook::jsi::Runtime &runtime,
|
|
const facebook::jsi::PropNameID &propName) override {
|
|
std::string propNameUtf8 = propName.utf8(runtime);
|
|
auto p = methodMap_.find(propNameUtf8);
|
|
if (p == methodMap_.end()) {
|
|
// Method was not found, let JS decide what to do.
|
|
return jsi::Value::undefined();
|
|
}
|
|
MethodMetadata meta = p->second;
|
|
return jsi::Function::createFromHostFunction(
|
|
runtime,
|
|
propName,
|
|
static_cast<unsigned int>(meta.argCount),
|
|
[this, meta](
|
|
facebook::jsi::Runtime &rt,
|
|
const facebook::jsi::Value &thisVal,
|
|
const facebook::jsi::Value *args,
|
|
size_t count) { return meta.invoker(rt, *this, args, count); });
|
|
}
|
|
|
|
const std::string name_;
|
|
std::shared_ptr<CallInvoker> jsInvoker_;
|
|
|
|
protected:
|
|
struct MethodMetadata {
|
|
size_t argCount;
|
|
facebook::jsi::Value (*invoker)(
|
|
facebook::jsi::Runtime &rt,
|
|
TurboModule &turboModule,
|
|
const facebook::jsi::Value *args,
|
|
size_t count);
|
|
};
|
|
|
|
std::unordered_map<std::string, MethodMetadata> methodMap_;
|
|
};
|
|
|
|
/**
|
|
* An app/platform-specific provider function to get an instance of a module
|
|
* given a name.
|
|
*/
|
|
using TurboModuleProviderFunctionType =
|
|
std::function<std::shared_ptr<TurboModule>(const std::string &name)>;
|
|
|
|
} // namespace react
|
|
} // namespace facebook
|