Format code in ReactCommon/turbomodule/core

Summary: Just ran `arc f ReactCommon/turbomodule/core/**/*`.

Reviewed By: ejanzer

Differential Revision: D16691807

fbshipit-source-id: 3f499ffeffaae47bda550c0071c93cd7f48e2a23
This commit is contained in:
Ramanpreet Nara 2019-08-08 10:47:10 -07:00 коммит произвёл Facebook Github Bot
Родитель 9b1465b74f
Коммит c237794236
19 изменённых файлов: 423 добавлений и 242 удалений

Просмотреть файл

@ -11,7 +11,7 @@ namespace facebook {
namespace react { namespace react {
// LongLivedObjectCollection // LongLivedObjectCollection
LongLivedObjectCollection& LongLivedObjectCollection::get() { LongLivedObjectCollection &LongLivedObjectCollection::get() {
static LongLivedObjectCollection instance; static LongLivedObjectCollection instance;
return instance; return instance;
} }

Просмотреть файл

@ -14,18 +14,20 @@ namespace facebook {
namespace react { namespace react {
/** /**
* A simple wrapper class that can be registered to a collection that keep it alive for extended period of time. * A simple wrapper class that can be registered to a collection that keep it
* This object can be removed from the collection when needed. * alive for extended period of time. This object can be removed from the
* collection when needed.
* *
* The subclass of this class must be created using std::make_shared<T>(). * The subclass of this class must be created using std::make_shared<T>().
* After creation, add it to the `LongLivedObjectCollection`. * After creation, add it to the `LongLivedObjectCollection`.
* When done with the object, call `allowRelease()` to allow the OS to release it. * When done with the object, call `allowRelease()` to allow the OS to release
* it.
*/ */
class LongLivedObject { class LongLivedObject {
public: public:
void allowRelease(); void allowRelease();
protected: protected:
LongLivedObject(); LongLivedObject();
}; };
@ -33,17 +35,17 @@ protected:
* A singleton collection for the `LongLivedObject`s. * A singleton collection for the `LongLivedObject`s.
*/ */
class LongLivedObjectCollection { class LongLivedObjectCollection {
public: public:
static LongLivedObjectCollection& get(); static LongLivedObjectCollection &get();
LongLivedObjectCollection(LongLivedObjectCollection const&) = delete; LongLivedObjectCollection(LongLivedObjectCollection const &) = delete;
void operator=(LongLivedObjectCollection const&) = delete; void operator=(LongLivedObjectCollection const &) = delete;
void add(std::shared_ptr<LongLivedObject> o); void add(std::shared_ptr<LongLivedObject> o);
void remove(const LongLivedObject *o); void remove(const LongLivedObject *o);
void clear(); void clear();
private: private:
LongLivedObjectCollection(); LongLivedObjectCollection();
std::unordered_set<std::shared_ptr<LongLivedObject>> collection_; std::unordered_set<std::shared_ptr<LongLivedObject>> collection_;
}; };

Просмотреть файл

@ -18,15 +18,20 @@ namespace facebook {
namespace react { namespace react {
/** /**
* A helper class to convert the legacy CxxModule instance to a TurboModule instance. * A helper class to convert the legacy CxxModule instance to a TurboModule
* This should be used only for migration purpose (to TurboModule), since it's not very performant * instance. This should be used only for migration purpose (to TurboModule),
* due to a lot of back-and-forth value conversions between folly::dynamic and jsi::Value. * since it's not very performant due to a lot of back-and-forth value
* conversions between folly::dynamic and jsi::Value.
*/ */
class JSI_EXPORT TurboCxxModule : public TurboModule { class JSI_EXPORT TurboCxxModule : public TurboModule {
public: public:
TurboCxxModule(std::unique_ptr<facebook::xplat::module::CxxModule> cxxModule, std::shared_ptr<JSCallInvoker> jsInvoker); TurboCxxModule(
std::unique_ptr<facebook::xplat::module::CxxModule> cxxModule,
std::shared_ptr<JSCallInvoker> jsInvoker);
virtual facebook::jsi::Value get(facebook::jsi::Runtime& runtime, const facebook::jsi::PropNameID& propName) override; virtual facebook::jsi::Value get(
facebook::jsi::Runtime &runtime,
const facebook::jsi::PropNameID &propName) override;
jsi::Value invokeMethod( jsi::Value invokeMethod(
jsi::Runtime &runtime, jsi::Runtime &runtime,
@ -35,7 +40,7 @@ public:
const jsi::Value *args, const jsi::Value *args,
size_t count); size_t count);
private: private:
std::vector<facebook::xplat::module::CxxModule::Method> cxxMethods_; std::vector<facebook::xplat::module::CxxModule::Method> cxxMethods_;
std::unique_ptr<facebook::xplat::module::CxxModule> cxxModule_; std::unique_ptr<facebook::xplat::module::CxxModule> cxxModule_;
}; };

Просмотреть файл

@ -12,13 +12,16 @@ using namespace facebook;
namespace facebook { namespace facebook {
namespace react { namespace react {
TurboModule::TurboModule(const std::string &name, std::shared_ptr<JSCallInvoker> jsInvoker) TurboModule::TurboModule(
: name_(name), const std::string &name,
jsInvoker_(jsInvoker) {} std::shared_ptr<JSCallInvoker> jsInvoker)
: name_(name), jsInvoker_(jsInvoker) {}
TurboModule::~TurboModule() {} TurboModule::~TurboModule() {}
jsi::Value TurboModule::get(jsi::Runtime& runtime, const jsi::PropNameID& propName) { jsi::Value TurboModule::get(
jsi::Runtime &runtime,
const jsi::PropNameID &propName) {
std::string propNameUtf8 = propName.utf8(runtime); std::string propNameUtf8 = propName.utf8(runtime);
auto p = methodMap_.find(propNameUtf8); auto p = methodMap_.find(propNameUtf8);
if (p == methodMap_.end()) { if (p == methodMap_.end()) {
@ -27,12 +30,14 @@ jsi::Value TurboModule::get(jsi::Runtime& runtime, const jsi::PropNameID& propNa
} }
MethodMetadata meta = p->second; MethodMetadata meta = p->second;
return jsi::Function::createFromHostFunction( return jsi::Function::createFromHostFunction(
runtime, runtime,
propName, propName,
meta.argCount, meta.argCount,
[this, meta](facebook::jsi::Runtime &rt, const facebook::jsi::Value &thisVal, const facebook::jsi::Value *args, size_t count) { [this, meta](
return meta.invoker(rt, *this, args, count); facebook::jsi::Runtime &rt,
}); const facebook::jsi::Value &thisVal,
const facebook::jsi::Value *args,
size_t count) { return meta.invoker(rt, *this, args, count); });
} }
} // namespace react } // namespace react

Просмотреть файл

@ -36,22 +36,26 @@ enum TurboModuleMethodValueKind {
* Base HostObject class for every module to be exposed to JS * Base HostObject class for every module to be exposed to JS
*/ */
class JSI_EXPORT TurboModule : public facebook::jsi::HostObject { class JSI_EXPORT TurboModule : public facebook::jsi::HostObject {
public: public:
TurboModule(const std::string &name, std::shared_ptr<JSCallInvoker> jsInvoker); TurboModule(
const std::string &name,
std::shared_ptr<JSCallInvoker> jsInvoker);
virtual ~TurboModule(); virtual ~TurboModule();
virtual facebook::jsi::Value get(facebook::jsi::Runtime& runtime, const facebook::jsi::PropNameID& propName) override; virtual facebook::jsi::Value get(
facebook::jsi::Runtime &runtime,
const facebook::jsi::PropNameID &propName) override;
const std::string name_; const std::string name_;
std::shared_ptr<JSCallInvoker> jsInvoker_; std::shared_ptr<JSCallInvoker> jsInvoker_;
protected: protected:
struct MethodMetadata { struct MethodMetadata {
size_t argCount; size_t argCount;
facebook::jsi::Value (*invoker)( facebook::jsi::Value (*invoker)(
facebook::jsi::Runtime& rt, facebook::jsi::Runtime &rt,
TurboModule &turboModule, TurboModule &turboModule,
const facebook::jsi::Value* args, const facebook::jsi::Value *args,
size_t count); size_t count);
}; };
@ -59,10 +63,11 @@ protected:
}; };
/** /**
* An app/platform-specific provider function to get an instance of a module given a name. * An app/platform-specific provider function to get an instance of a module
* given a name.
*/ */
using TurboModuleProviderFunctionType = std::function<std::shared_ptr<TurboModule>( using TurboModuleProviderFunctionType =
const std::string &name)>; std::function<std::shared_ptr<TurboModule>(const std::string &name)>;
} // namespace react } // namespace react
} // namespace facebook } // namespace facebook

Просмотреть файл

@ -20,8 +20,9 @@ namespace react {
/** /**
* Public API to install the TurboModule system. * Public API to install the TurboModule system.
*/ */
TurboModuleBinding::TurboModuleBinding(const TurboModuleProviderFunctionType &moduleProvider) TurboModuleBinding::TurboModuleBinding(
: moduleProvider_(moduleProvider) {} const TurboModuleProviderFunctionType &moduleProvider)
: moduleProvider_(moduleProvider) {}
void TurboModuleBinding::install( void TurboModuleBinding::install(
jsi::Runtime &runtime, jsi::Runtime &runtime,
@ -33,7 +34,11 @@ void TurboModuleBinding::install(
runtime, runtime,
jsi::PropNameID::forAscii(runtime, "__turboModuleProxy"), jsi::PropNameID::forAscii(runtime, "__turboModuleProxy"),
1, 1,
[binding](jsi::Runtime& rt, const jsi::Value& thisVal, const jsi::Value* args, size_t count) { [binding](
jsi::Runtime &rt,
const jsi::Value &thisVal,
const jsi::Value *args,
size_t count) {
return binding->jsProxy(rt, thisVal, args, count); return binding->jsProxy(rt, thisVal, args, count);
})); }));
} }
@ -48,7 +53,8 @@ void TurboModuleBinding::invalidate() const {
// LongLivedObjectCollection::get().clear(); // LongLivedObjectCollection::get().clear();
} }
std::shared_ptr<TurboModule> TurboModuleBinding::getModule(const std::string &name) { std::shared_ptr<TurboModule> TurboModuleBinding::getModule(
const std::string &name) {
std::shared_ptr<TurboModule> module = nullptr; std::shared_ptr<TurboModule> module = nullptr;
{ {
SystraceSection s("TurboModuleBinding::getModule", "module", name); SystraceSection s("TurboModuleBinding::getModule", "module", name);
@ -58,12 +64,13 @@ std::shared_ptr<TurboModule> TurboModuleBinding::getModule(const std::string &na
} }
jsi::Value TurboModuleBinding::jsProxy( jsi::Value TurboModuleBinding::jsProxy(
jsi::Runtime& runtime, jsi::Runtime &runtime,
const jsi::Value& thisVal, const jsi::Value &thisVal,
const jsi::Value* args, const jsi::Value *args,
size_t count) { size_t count) {
if (count != 1) { if (count != 1) {
throw std::invalid_argument("TurboModuleBinding::jsProxy arg count must be 1"); throw std::invalid_argument(
"TurboModuleBinding::jsProxy arg count must be 1");
} }
std::string moduleName = args[0].getString(runtime).utf8(runtime); std::string moduleName = args[0].getString(runtime).utf8(runtime);
std::shared_ptr<TurboModule> module = getModule(moduleName); std::shared_ptr<TurboModule> module = getModule(moduleName);

Просмотреть файл

@ -21,7 +21,7 @@ class JSCallInvoker;
* Represents the JavaScript binding for the TurboModule system. * Represents the JavaScript binding for the TurboModule system.
*/ */
class TurboModuleBinding { class TurboModuleBinding {
public: public:
/* /*
* Installs TurboModuleBinding into JavaScript runtime. * Installs TurboModuleBinding into JavaScript runtime.
* Thread synchronization must be enforced externally. * Thread synchronization must be enforced externally.
@ -43,15 +43,15 @@ public:
*/ */
std::shared_ptr<TurboModule> getModule(const std::string &name); std::shared_ptr<TurboModule> getModule(const std::string &name);
private: private:
/** /**
* A lookup function exposed to JS to get an instance of a TurboModule * A lookup function exposed to JS to get an instance of a TurboModule
* for the given name. * for the given name.
*/ */
jsi::Value jsProxy( jsi::Value jsProxy(
jsi::Runtime& runtime, jsi::Runtime &runtime,
const jsi::Value& thisVal, const jsi::Value &thisVal,
const jsi::Value* args, const jsi::Value *args,
size_t count); size_t count);
TurboModuleProviderFunctionType moduleProvider_; TurboModuleProviderFunctionType moduleProvider_;

Просмотреть файл

@ -57,15 +57,14 @@ jsi::Array deepCopyJSIArray(jsi::Runtime &rt, const jsi::Array &arr) {
size_t size = arr.size(rt); size_t size = arr.size(rt);
jsi::Array copy(rt, size); jsi::Array copy(rt, size);
for (size_t i = 0; i < size; i++) { for (size_t i = 0; i < size; i++) {
copy.setValueAtIndex(rt, i, deepCopyJSIValue(rt, arr.getValueAtIndex(rt, i))); copy.setValueAtIndex(
rt, i, deepCopyJSIValue(rt, arr.getValueAtIndex(rt, i)));
} }
return copy; return copy;
} }
Promise::Promise(jsi::Runtime &rt, jsi::Function resolve, jsi::Function reject) Promise::Promise(jsi::Runtime &rt, jsi::Function resolve, jsi::Function reject)
: runtime_(rt), : runtime_(rt), resolve_(std::move(resolve)), reject_(std::move(reject)) {}
resolve_(std::move(resolve)),
reject_(std::move(reject)) {}
void Promise::resolve(const jsi::Value &result) { void Promise::resolve(const jsi::Value &result) {
resolve_.call(runtime_, result); resolve_.call(runtime_, result);
@ -73,20 +72,28 @@ void Promise::resolve(const jsi::Value &result) {
void Promise::reject(const std::string &message) { void Promise::reject(const std::string &message) {
jsi::Object error(runtime_); jsi::Object error(runtime_);
error.setProperty(runtime_, "message", jsi::String::createFromUtf8(runtime_, message)); error.setProperty(
runtime_, "message", jsi::String::createFromUtf8(runtime_, message));
reject_.call(runtime_, error); reject_.call(runtime_, error);
} }
jsi::Value createPromiseAsJSIValue(jsi::Runtime &rt, const PromiseSetupFunctionType func) { jsi::Value createPromiseAsJSIValue(
jsi::Runtime &rt,
const PromiseSetupFunctionType func) {
jsi::Function JSPromise = rt.global().getPropertyAsFunction(rt, "Promise"); jsi::Function JSPromise = rt.global().getPropertyAsFunction(rt, "Promise");
jsi::Function fn = jsi::Function::createFromHostFunction( jsi::Function fn = jsi::Function::createFromHostFunction(
rt, rt,
jsi::PropNameID::forAscii(rt, "fn"), jsi::PropNameID::forAscii(rt, "fn"),
2, 2,
[func](jsi::Runtime &rt2, const jsi::Value &thisVal, const jsi::Value *args, size_t count) { [func](
jsi::Runtime &rt2,
const jsi::Value &thisVal,
const jsi::Value *args,
size_t count) {
jsi::Function resolve = args[0].getObject(rt2).getFunction(rt2); jsi::Function resolve = args[0].getObject(rt2).getFunction(rt2);
jsi::Function reject = args[1].getObject(rt2).getFunction(rt2); jsi::Function reject = args[1].getObject(rt2).getFunction(rt2);
auto wrapper = std::make_shared<Promise>(rt2, std::move(resolve), std::move(reject)); auto wrapper = std::make_shared<Promise>(
rt2, std::move(resolve), std::move(reject));
func(rt2, wrapper); func(rt2, wrapper);
return jsi::Value::undefined(); return jsi::Value::undefined();
}); });

Просмотреть файл

@ -18,7 +18,8 @@
#import <string> #import <string>
#import <unordered_map> #import <unordered_map>
#define RCT_IS_TURBO_MODULE_CLASS(klass) ((RCTTurboModuleEnabled() && [(klass) conformsToProtocol:@protocol(RCTTurboModule)])) #define RCT_IS_TURBO_MODULE_CLASS(klass) \
((RCTTurboModuleEnabled() && [(klass) conformsToProtocol:@protocol(RCTTurboModule)]))
#define RCT_IS_TURBO_MODULE_INSTANCE(module) RCT_IS_TURBO_MODULE_CLASS([(module) class]) #define RCT_IS_TURBO_MODULE_INSTANCE(module) RCT_IS_TURBO_MODULE_CLASS([(module) class])
namespace facebook { namespace facebook {
@ -30,7 +31,7 @@ class Instance;
* ObjC++ specific TurboModule base class. * ObjC++ specific TurboModule base class.
*/ */
class JSI_EXPORT ObjCTurboModule : public TurboModule { class JSI_EXPORT ObjCTurboModule : public TurboModule {
public: public:
ObjCTurboModule(const std::string &name, id<RCTTurboModule> instance, std::shared_ptr<JSCallInvoker> jsInvoker); ObjCTurboModule(const std::string &name, id<RCTTurboModule> instance, std::shared_ptr<JSCallInvoker> jsInvoker);
jsi::Value invokeObjCMethod( jsi::Value invokeObjCMethod(
@ -42,27 +43,29 @@ public:
size_t count); size_t count);
id<RCTTurboModule> instance_; id<RCTTurboModule> instance_;
protected:
protected:
void setMethodArgConversionSelector(NSString *methodName, int argIndex, NSString *fnName); void setMethodArgConversionSelector(NSString *methodName, int argIndex, NSString *fnName);
private:
private:
/** /**
* TODO(ramanpreet): * TODO(ramanpreet):
* Investigate an optimization that'll let us get rid of this NSMutableDictionary. * Investigate an optimization that'll let us get rid of this NSMutableDictionary.
*/ */
NSMutableDictionary<NSString *, NSMutableArray *> *methodArgConversionSelectors_; NSMutableDictionary<NSString *, NSMutableArray *> *methodArgConversionSelectors_;
NSDictionary<NSString *, NSArray<NSString *> *> *methodArgumentTypeNames_; NSDictionary<NSString *, NSArray<NSString *> *> *methodArgumentTypeNames_;
NSString* getArgumentTypeName(NSString* methodName, int argIndex); NSString *getArgumentTypeName(NSString *methodName, int argIndex);
NSInvocation *getMethodInvocation( NSInvocation *getMethodInvocation(
jsi::Runtime &runtime, jsi::Runtime &runtime,
TurboModuleMethodValueKind valueKind, TurboModuleMethodValueKind valueKind,
const id<RCTTurboModule> module, const id<RCTTurboModule> module,
std::shared_ptr<JSCallInvoker> jsInvoker, std::shared_ptr<JSCallInvoker> jsInvoker,
const std::string& methodName, const std::string &methodName,
SEL selector, SEL selector,
const jsi::Value *args, const jsi::Value *args,
size_t count, size_t count,
NSMutableArray *retainedObjectsForInvocation); NSMutableArray *retainedObjectsForInvocation);
BOOL hasMethodArgConversionSelector(NSString *methodName, int argIndex); BOOL hasMethodArgConversionSelector(NSString *methodName, int argIndex);
SEL getMethodArgConversionSelector(NSString *methodName, int argIndex); SEL getMethodArgConversionSelector(NSString *methodName, int argIndex);
@ -84,7 +87,8 @@ private:
@optional @optional
// This should be required, after migration is done. // This should be required, after migration is done.
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModuleWithJsInvoker:(std::shared_ptr<facebook::react::JSCallInvoker>)jsInvoker; - (std::shared_ptr<facebook::react::TurboModule>)getTurboModuleWithJsInvoker:
(std::shared_ptr<facebook::react::JSCallInvoker>)jsInvoker;
@end @end

Просмотреть файл

@ -12,7 +12,8 @@
// TODO: Move to xplat codegen. // TODO: Move to xplat codegen.
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:(const std::string &)name - (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:(const std::string &)name
instance:(id<RCTTurboModule>)instance instance:(id<RCTTurboModule>)instance
jsInvoker:(std::shared_ptr<facebook::react::JSCallInvoker>)jsInvoker; jsInvoker:
(std::shared_ptr<facebook::react::JSCallInvoker>)jsInvoker;
@optional @optional
@ -30,11 +31,12 @@
* Create an instance of a TurboModule without relying on any ObjC++ module instance. * Create an instance of a TurboModule without relying on any ObjC++ module instance.
*/ */
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:(const std::string &)name - (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:(const std::string &)name
jsInvoker:(std::shared_ptr<facebook::react::JSCallInvoker>)jsInvoker; jsInvoker:
(std::shared_ptr<facebook::react::JSCallInvoker>)jsInvoker;
@end @end
@interface RCTTurboModuleManager : NSObject<RCTTurboModuleLookupDelegate> @interface RCTTurboModuleManager : NSObject <RCTTurboModuleLookupDelegate>
- (instancetype)initWithBridge:(RCTBridge *)bridge delegate:(id<RCTTurboModuleManagerDelegate>)delegate; - (instancetype)initWithBridge:(RCTBridge *)bridge delegate:(id<RCTTurboModuleManagerDelegate>)delegate;

Просмотреть файл

@ -12,62 +12,129 @@
namespace facebook { namespace facebook {
namespace react { namespace react {
static jsi::Value __hostFunction_NativeSampleTurboCxxModuleSpecJSI_voidFunc(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { static jsi::Value __hostFunction_NativeSampleTurboCxxModuleSpecJSI_voidFunc(
jsi::Runtime &rt,
TurboModule &turboModule,
const jsi::Value *args,
size_t count) {
static_cast<NativeSampleTurboCxxModuleSpecJSI *>(&turboModule)->voidFunc(rt); static_cast<NativeSampleTurboCxxModuleSpecJSI *>(&turboModule)->voidFunc(rt);
return jsi::Value::undefined(); return jsi::Value::undefined();
} }
static jsi::Value __hostFunction_NativeSampleTurboCxxModuleSpecJSI_getBool(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { static jsi::Value __hostFunction_NativeSampleTurboCxxModuleSpecJSI_getBool(
return jsi::Value(static_cast<NativeSampleTurboCxxModuleSpecJSI *>(&turboModule)->getBool(rt, args[0].getBool())); jsi::Runtime &rt,
TurboModule &turboModule,
const jsi::Value *args,
size_t count) {
return jsi::Value(
static_cast<NativeSampleTurboCxxModuleSpecJSI *>(&turboModule)
->getBool(rt, args[0].getBool()));
} }
static jsi::Value __hostFunction_NativeSampleTurboCxxModuleSpecJSI_getNumber(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { static jsi::Value __hostFunction_NativeSampleTurboCxxModuleSpecJSI_getNumber(
return jsi::Value(static_cast<NativeSampleTurboCxxModuleSpecJSI *>(&turboModule)->getNumber(rt, args[0].getNumber())); jsi::Runtime &rt,
TurboModule &turboModule,
const jsi::Value *args,
size_t count) {
return jsi::Value(
static_cast<NativeSampleTurboCxxModuleSpecJSI *>(&turboModule)
->getNumber(rt, args[0].getNumber()));
} }
static jsi::Value __hostFunction_NativeSampleTurboCxxModuleSpecJSI_getString(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { static jsi::Value __hostFunction_NativeSampleTurboCxxModuleSpecJSI_getString(
return static_cast<NativeSampleTurboCxxModuleSpecJSI *>(&turboModule)->getString(rt, args[0].getString(rt)); jsi::Runtime &rt,
TurboModule &turboModule,
const jsi::Value *args,
size_t count) {
return static_cast<NativeSampleTurboCxxModuleSpecJSI *>(&turboModule)
->getString(rt, args[0].getString(rt));
} }
static jsi::Value __hostFunction_NativeSampleTurboCxxModuleSpecJSI_getArray(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { static jsi::Value __hostFunction_NativeSampleTurboCxxModuleSpecJSI_getArray(
return static_cast<NativeSampleTurboCxxModuleSpecJSI *>(&turboModule)->getArray(rt, args[0].getObject(rt).getArray(rt)); jsi::Runtime &rt,
TurboModule &turboModule,
const jsi::Value *args,
size_t count) {
return static_cast<NativeSampleTurboCxxModuleSpecJSI *>(&turboModule)
->getArray(rt, args[0].getObject(rt).getArray(rt));
} }
static jsi::Value __hostFunction_NativeSampleTurboCxxModuleSpecJSI_getObject(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { static jsi::Value __hostFunction_NativeSampleTurboCxxModuleSpecJSI_getObject(
return static_cast<NativeSampleTurboCxxModuleSpecJSI *>(&turboModule)->getObject(rt, args[0].getObject(rt)); jsi::Runtime &rt,
TurboModule &turboModule,
const jsi::Value *args,
size_t count) {
return static_cast<NativeSampleTurboCxxModuleSpecJSI *>(&turboModule)
->getObject(rt, args[0].getObject(rt));
} }
static jsi::Value __hostFunction_NativeSampleTurboCxxModuleSpecJSI_getValue(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { static jsi::Value __hostFunction_NativeSampleTurboCxxModuleSpecJSI_getValue(
return static_cast<NativeSampleTurboCxxModuleSpecJSI *>(&turboModule)->getValue(rt, args[0].getNumber(), args[1].getString(rt), args[2].getObject(rt)); jsi::Runtime &rt,
TurboModule &turboModule,
const jsi::Value *args,
size_t count) {
return static_cast<NativeSampleTurboCxxModuleSpecJSI *>(&turboModule)
->getValue(
rt,
args[0].getNumber(),
args[1].getString(rt),
args[2].getObject(rt));
} }
static jsi::Value __hostFunction_NativeSampleTurboCxxModuleSpecJSI_getValueWithCallback(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { static jsi::Value
static_cast<NativeSampleTurboCxxModuleSpecJSI *>(&turboModule)->getValueWithCallback(rt, std::move(args[0].getObject(rt).getFunction(rt))); __hostFunction_NativeSampleTurboCxxModuleSpecJSI_getValueWithCallback(
jsi::Runtime &rt,
TurboModule &turboModule,
const jsi::Value *args,
size_t count) {
static_cast<NativeSampleTurboCxxModuleSpecJSI *>(&turboModule)
->getValueWithCallback(
rt, std::move(args[0].getObject(rt).getFunction(rt)));
return jsi::Value::undefined(); return jsi::Value::undefined();
} }
static jsi::Value __hostFunction_NativeSampleTurboCxxModuleSpecJSI_getValueWithPromise(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { static jsi::Value
return static_cast<NativeSampleTurboCxxModuleSpecJSI *>(&turboModule)->getValueWithPromise(rt, args[0].getBool()); __hostFunction_NativeSampleTurboCxxModuleSpecJSI_getValueWithPromise(
jsi::Runtime &rt,
TurboModule &turboModule,
const jsi::Value *args,
size_t count) {
return static_cast<NativeSampleTurboCxxModuleSpecJSI *>(&turboModule)
->getValueWithPromise(rt, args[0].getBool());
} }
static jsi::Value __hostFunction_NativeSampleTurboCxxModuleSpecJSI_getConstants(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { static jsi::Value __hostFunction_NativeSampleTurboCxxModuleSpecJSI_getConstants(
return static_cast<NativeSampleTurboCxxModuleSpecJSI *>(&turboModule)->getConstants(rt); jsi::Runtime &rt,
TurboModule &turboModule,
const jsi::Value *args,
size_t count) {
return static_cast<NativeSampleTurboCxxModuleSpecJSI *>(&turboModule)
->getConstants(rt);
} }
NativeSampleTurboCxxModuleSpecJSI::NativeSampleTurboCxxModuleSpecJSI(std::shared_ptr<JSCallInvoker> jsInvoker) NativeSampleTurboCxxModuleSpecJSI::NativeSampleTurboCxxModuleSpecJSI(
: TurboModule("SampleTurboCxxModule", jsInvoker) { std::shared_ptr<JSCallInvoker> jsInvoker)
: TurboModule("SampleTurboCxxModule", jsInvoker) {
methodMap_["voidFunc"] = MethodMetadata {0, __hostFunction_NativeSampleTurboCxxModuleSpecJSI_voidFunc}; methodMap_["voidFunc"] = MethodMetadata{
methodMap_["getBool"] = MethodMetadata {1, __hostFunction_NativeSampleTurboCxxModuleSpecJSI_getBool}; 0, __hostFunction_NativeSampleTurboCxxModuleSpecJSI_voidFunc};
methodMap_["getNumber"] = MethodMetadata {1, __hostFunction_NativeSampleTurboCxxModuleSpecJSI_getNumber}; methodMap_["getBool"] = MethodMetadata{
methodMap_["getString"] = MethodMetadata {1, __hostFunction_NativeSampleTurboCxxModuleSpecJSI_getString}; 1, __hostFunction_NativeSampleTurboCxxModuleSpecJSI_getBool};
methodMap_["getArray"] = MethodMetadata {1, __hostFunction_NativeSampleTurboCxxModuleSpecJSI_getArray}; methodMap_["getNumber"] = MethodMetadata{
methodMap_["getObject"] = MethodMetadata {1, __hostFunction_NativeSampleTurboCxxModuleSpecJSI_getObject}; 1, __hostFunction_NativeSampleTurboCxxModuleSpecJSI_getNumber};
methodMap_["getValue"] = MethodMetadata {3, __hostFunction_NativeSampleTurboCxxModuleSpecJSI_getValue}; methodMap_["getString"] = MethodMetadata{
methodMap_["getValueWithCallback"] = MethodMetadata {1, __hostFunction_NativeSampleTurboCxxModuleSpecJSI_getValueWithCallback}; 1, __hostFunction_NativeSampleTurboCxxModuleSpecJSI_getString};
methodMap_["getValueWithPromise"] = MethodMetadata {1, __hostFunction_NativeSampleTurboCxxModuleSpecJSI_getValueWithPromise}; methodMap_["getArray"] = MethodMetadata{
methodMap_["getConstants"] = MethodMetadata {0, __hostFunction_NativeSampleTurboCxxModuleSpecJSI_getConstants}; 1, __hostFunction_NativeSampleTurboCxxModuleSpecJSI_getArray};
methodMap_["getObject"] = MethodMetadata{
1, __hostFunction_NativeSampleTurboCxxModuleSpecJSI_getObject};
methodMap_["getValue"] = MethodMetadata{
3, __hostFunction_NativeSampleTurboCxxModuleSpecJSI_getValue};
methodMap_["getValueWithCallback"] = MethodMetadata{
1, __hostFunction_NativeSampleTurboCxxModuleSpecJSI_getValueWithCallback};
methodMap_["getValueWithPromise"] = MethodMetadata{
1, __hostFunction_NativeSampleTurboCxxModuleSpecJSI_getValueWithPromise};
methodMap_["getConstants"] = MethodMetadata{
0, __hostFunction_NativeSampleTurboCxxModuleSpecJSI_getConstants};
} }
} // namespace react } // namespace react

Просмотреть файл

@ -17,21 +17,26 @@ namespace react {
// TODO: This definition should be codegen'ed for type-safety purpose. // TODO: This definition should be codegen'ed for type-safety purpose.
class JSI_EXPORT NativeSampleTurboCxxModuleSpecJSI : public TurboModule { class JSI_EXPORT NativeSampleTurboCxxModuleSpecJSI : public TurboModule {
protected: protected:
NativeSampleTurboCxxModuleSpecJSI(std::shared_ptr<JSCallInvoker> jsInvoker); NativeSampleTurboCxxModuleSpecJSI(std::shared_ptr<JSCallInvoker> jsInvoker);
public: public:
virtual void voidFunc(jsi::Runtime &rt) = 0; virtual void voidFunc(jsi::Runtime &rt) = 0;
virtual bool getBool(jsi::Runtime &rt, bool arg) = 0; virtual bool getBool(jsi::Runtime &rt, bool arg) = 0;
virtual double getNumber(jsi::Runtime &rt, double arg) = 0; virtual double getNumber(jsi::Runtime &rt, double arg) = 0;
virtual jsi::String getString(jsi::Runtime &rt, const jsi::String &arg) = 0; virtual jsi::String getString(jsi::Runtime &rt, const jsi::String &arg) = 0;
virtual jsi::Array getArray(jsi::Runtime &rt, const jsi::Array &arg) = 0; virtual jsi::Array getArray(jsi::Runtime &rt, const jsi::Array &arg) = 0;
virtual jsi::Object getObject(jsi::Runtime &rt, const jsi::Object &arg) = 0; virtual jsi::Object getObject(jsi::Runtime &rt, const jsi::Object &arg) = 0;
virtual jsi::Object getValue(jsi::Runtime &rt, double x, const jsi::String &y, const jsi::Object &z) = 0; virtual jsi::Object getValue(
virtual void getValueWithCallback(jsi::Runtime &rt, const jsi::Function &callback) = 0; jsi::Runtime &rt,
double x,
const jsi::String &y,
const jsi::Object &z) = 0;
virtual void getValueWithCallback(
jsi::Runtime &rt,
const jsi::Function &callback) = 0;
virtual jsi::Value getValueWithPromise(jsi::Runtime &rt, bool error) = 0; virtual jsi::Value getValueWithPromise(jsi::Runtime &rt, bool error) = 0;
virtual jsi::Object getConstants(jsi::Runtime &rt) = 0; virtual jsi::Object getConstants(jsi::Runtime &rt) = 0;
}; };
} // namespace react } // namespace react

Просмотреть файл

@ -26,13 +26,9 @@
- (NSString *)getString:(NSString *)arg; - (NSString *)getString:(NSString *)arg;
- (NSArray<id<NSObject>> *)getArray:(NSArray *)arg; - (NSArray<id<NSObject>> *)getArray:(NSArray *)arg;
- (NSDictionary *)getObject:(NSDictionary *)arg; - (NSDictionary *)getObject:(NSDictionary *)arg;
- (NSDictionary *)getValue:(double)x - (NSDictionary *)getValue:(double)x y:(NSString *)y z:(NSDictionary *)z;
y:(NSString *)y
z:(NSDictionary *)z;
- (void)getValueWithCallback:(RCTResponseSenderBlock)callback; - (void)getValueWithCallback:(RCTResponseSenderBlock)callback;
- (void)getValueWithPromise:(BOOL)error - (void)getValueWithPromise:(BOOL)error resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject;
resolve:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject;
- (NSDictionary *)constantsToExport; - (NSDictionary *)constantsToExport;
- (NSDictionary *)getConstants; - (NSDictionary *)getConstants;
@ -45,7 +41,7 @@ namespace react {
* The iOS TurboModule impl specific to SampleTurboModule. * The iOS TurboModule impl specific to SampleTurboModule.
*/ */
class JSI_EXPORT NativeSampleTurboModuleSpecJSI : public ObjCTurboModule { class JSI_EXPORT NativeSampleTurboModuleSpecJSI : public ObjCTurboModule {
public: public:
NativeSampleTurboModuleSpecJSI(id<RCTTurboModule> instance, std::shared_ptr<JSCallInvoker> jsInvoker); NativeSampleTurboModuleSpecJSI(id<RCTTurboModule> instance, std::shared_ptr<JSCallInvoker> jsInvoker);
}; };

Просмотреть файл

@ -10,69 +10,124 @@
namespace facebook { namespace facebook {
namespace react { namespace react {
static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_voidFunc(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) { static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_voidFunc(
facebook::jsi::Runtime &rt,
TurboModule &turboModule,
const facebook::jsi::Value *args,
size_t count)
{
return static_cast<ObjCTurboModule &>(turboModule) return static_cast<ObjCTurboModule &>(turboModule)
.invokeObjCMethod(rt, VoidKind, "voidFunc", @selector(voidFunc), args, count); .invokeObjCMethod(rt, VoidKind, "voidFunc", @selector(voidFunc), args, count);
} }
static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getBool(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) { static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getBool(
facebook::jsi::Runtime &rt,
TurboModule &turboModule,
const facebook::jsi::Value *args,
size_t count)
{
return static_cast<ObjCTurboModule &>(turboModule) return static_cast<ObjCTurboModule &>(turboModule)
.invokeObjCMethod(rt, BooleanKind, "getBool", @selector(getBool:), args, count); .invokeObjCMethod(rt, BooleanKind, "getBool", @selector(getBool:), args, count);
} }
static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getNumber(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) { static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getNumber(
facebook::jsi::Runtime &rt,
TurboModule &turboModule,
const facebook::jsi::Value *args,
size_t count)
{
return static_cast<ObjCTurboModule &>(turboModule) return static_cast<ObjCTurboModule &>(turboModule)
.invokeObjCMethod(rt, NumberKind, "getNumber", @selector(getNumber:), args, count); .invokeObjCMethod(rt, NumberKind, "getNumber", @selector(getNumber:), args, count);
} }
static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getString(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) { static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getString(
facebook::jsi::Runtime &rt,
TurboModule &turboModule,
const facebook::jsi::Value *args,
size_t count)
{
return static_cast<ObjCTurboModule &>(turboModule) return static_cast<ObjCTurboModule &>(turboModule)
.invokeObjCMethod(rt, StringKind, "getString", @selector(getString:), args, count); .invokeObjCMethod(rt, StringKind, "getString", @selector(getString:), args, count);
} }
static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getArray(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) { static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getArray(
facebook::jsi::Runtime &rt,
TurboModule &turboModule,
const facebook::jsi::Value *args,
size_t count)
{
return static_cast<ObjCTurboModule &>(turboModule) return static_cast<ObjCTurboModule &>(turboModule)
.invokeObjCMethod(rt, ArrayKind, "getArray", @selector(getArray:), args, count); .invokeObjCMethod(rt, ArrayKind, "getArray", @selector(getArray:), args, count);
} }
static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getObject(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) { static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getObject(
facebook::jsi::Runtime &rt,
TurboModule &turboModule,
const facebook::jsi::Value *args,
size_t count)
{
return static_cast<ObjCTurboModule &>(turboModule) return static_cast<ObjCTurboModule &>(turboModule)
.invokeObjCMethod(rt, ObjectKind, "getObject", @selector(getObject:), args, count); .invokeObjCMethod(rt, ObjectKind, "getObject", @selector(getObject:), args, count);
} }
static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getValue(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) { static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getValue(
facebook::jsi::Runtime &rt,
TurboModule &turboModule,
const facebook::jsi::Value *args,
size_t count)
{
return static_cast<ObjCTurboModule &>(turboModule) return static_cast<ObjCTurboModule &>(turboModule)
.invokeObjCMethod(rt, ObjectKind, "getValue", @selector(getValue:y:z:), args, count); .invokeObjCMethod(rt, ObjectKind, "getValue", @selector(getValue:y:z:), args, count);
} }
static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getValueWithCallback(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) { static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getValueWithCallback(
facebook::jsi::Runtime &rt,
TurboModule &turboModule,
const facebook::jsi::Value *args,
size_t count)
{
return static_cast<ObjCTurboModule &>(turboModule) return static_cast<ObjCTurboModule &>(turboModule)
.invokeObjCMethod(rt, VoidKind, "getValueWithCallback", @selector(getValueWithCallback:), args, count); .invokeObjCMethod(rt, VoidKind, "getValueWithCallback", @selector(getValueWithCallback:), args, count);
} }
static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getValueWithPromise(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) { static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getValueWithPromise(
facebook::jsi::Runtime &rt,
TurboModule &turboModule,
const facebook::jsi::Value *args,
size_t count)
{
return static_cast<ObjCTurboModule &>(turboModule) return static_cast<ObjCTurboModule &>(turboModule)
.invokeObjCMethod( .invokeObjCMethod(
rt, PromiseKind, "getValueWithPromise", @selector(getValueWithPromise:resolve:reject:), args, count); rt, PromiseKind, "getValueWithPromise", @selector(getValueWithPromise:resolve:reject:), args, count);
} }
static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getConstants(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) { static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getConstants(
facebook::jsi::Runtime &rt,
TurboModule &turboModule,
const facebook::jsi::Value *args,
size_t count)
{
return static_cast<ObjCTurboModule &>(turboModule) return static_cast<ObjCTurboModule &>(turboModule)
.invokeObjCMethod(rt, ObjectKind, "getConstants", @selector(getConstants), args, count); .invokeObjCMethod(rt, ObjectKind, "getConstants", @selector(getConstants), args, count);
} }
NativeSampleTurboModuleSpecJSI::NativeSampleTurboModuleSpecJSI(id<RCTTurboModule> instance, std::shared_ptr<JSCallInvoker> jsInvoker) NativeSampleTurboModuleSpecJSI::NativeSampleTurboModuleSpecJSI(
: ObjCTurboModule("SampleTurboModule", instance, jsInvoker) { id<RCTTurboModule> instance,
methodMap_["voidFunc"] = MethodMetadata {0, __hostFunction_NativeSampleTurboModuleSpecJSI_voidFunc}; std::shared_ptr<JSCallInvoker> jsInvoker)
methodMap_["getBool"] = MethodMetadata {1, __hostFunction_NativeSampleTurboModuleSpecJSI_getBool}; : ObjCTurboModule("SampleTurboModule", instance, jsInvoker)
methodMap_["getNumber"] = MethodMetadata {1, __hostFunction_NativeSampleTurboModuleSpecJSI_getNumber}; {
methodMap_["getString"] = MethodMetadata {1, __hostFunction_NativeSampleTurboModuleSpecJSI_getString}; methodMap_["voidFunc"] = MethodMetadata{0, __hostFunction_NativeSampleTurboModuleSpecJSI_voidFunc};
methodMap_["getArray"] = MethodMetadata {1, __hostFunction_NativeSampleTurboModuleSpecJSI_getArray}; methodMap_["getBool"] = MethodMetadata{1, __hostFunction_NativeSampleTurboModuleSpecJSI_getBool};
methodMap_["getObject"] = MethodMetadata {1, __hostFunction_NativeSampleTurboModuleSpecJSI_getObject}; methodMap_["getNumber"] = MethodMetadata{1, __hostFunction_NativeSampleTurboModuleSpecJSI_getNumber};
methodMap_["getValue"] = MethodMetadata {3, __hostFunction_NativeSampleTurboModuleSpecJSI_getValue}; methodMap_["getString"] = MethodMetadata{1, __hostFunction_NativeSampleTurboModuleSpecJSI_getString};
methodMap_["getValueWithCallback"] = MethodMetadata {1, __hostFunction_NativeSampleTurboModuleSpecJSI_getValueWithCallback}; methodMap_["getArray"] = MethodMetadata{1, __hostFunction_NativeSampleTurboModuleSpecJSI_getArray};
methodMap_["getValueWithPromise"] = MethodMetadata {1, __hostFunction_NativeSampleTurboModuleSpecJSI_getValueWithPromise}; methodMap_["getObject"] = MethodMetadata{1, __hostFunction_NativeSampleTurboModuleSpecJSI_getObject};
methodMap_["getConstants"] = MethodMetadata {0, __hostFunction_NativeSampleTurboModuleSpecJSI_getConstants}; methodMap_["getValue"] = MethodMetadata{3, __hostFunction_NativeSampleTurboModuleSpecJSI_getValue};
methodMap_["getValueWithCallback"] =
MethodMetadata{1, __hostFunction_NativeSampleTurboModuleSpecJSI_getValueWithCallback};
methodMap_["getValueWithPromise"] =
MethodMetadata{1, __hostFunction_NativeSampleTurboModuleSpecJSI_getValueWithPromise};
methodMap_["getConstants"] = MethodMetadata{0, __hostFunction_NativeSampleTurboModuleSpecJSI_getConstants};
} }
} // namespace react } // namespace react

Просмотреть файл

@ -15,13 +15,13 @@
* With jsi::HostObject, this class is no longer necessary, but the system supports it for * With jsi::HostObject, this class is no longer necessary, but the system supports it for
* backward compatibility. * backward compatibility.
*/ */
@interface RCTSampleTurboCxxModule_v1 : RCTCxxModule<RCTTurboModule> @interface RCTSampleTurboCxxModule_v1 : RCTCxxModule <RCTTurboModule>
@end @end
/** /**
* Second variant of a sample backward-compatible RCTCxxModule-based module. * Second variant of a sample backward-compatible RCTCxxModule-based module.
*/ */
@interface RCTSampleTurboCxxModule_v2 : RCTCxxModule<RCTTurboModule> @interface RCTSampleTurboCxxModule_v2 : RCTCxxModule <RCTTurboModule>
@end @end

Просмотреть файл

@ -13,6 +13,6 @@
* Sample iOS-specific impl of a TurboModule, conforming to the spec protocol. * Sample iOS-specific impl of a TurboModule, conforming to the spec protocol.
* This class is also 100% compatible with the NativeModule system. * This class is also 100% compatible with the NativeModule system.
*/ */
@interface RCTSampleTurboModule : NSObject<NativeSampleTurboModuleSpec> @interface RCTSampleTurboModule : NSObject <NativeSampleTurboModuleSpec>
@end @end

Просмотреть файл

@ -30,7 +30,8 @@ RCT_EXPORT_MODULE()
return dispatch_get_main_queue(); return dispatch_get_main_queue();
} }
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModuleWithJsInvoker:(std::shared_ptr<facebook::react::JSCallInvoker>)jsInvoker - (std::shared_ptr<facebook::react::TurboModule>)getTurboModuleWithJsInvoker:
(std::shared_ptr<facebook::react::JSCallInvoker>)jsInvoker
{ {
return std::make_shared<NativeSampleTurboModuleSpecJSI>(self, jsInvoker); return std::make_shared<NativeSampleTurboModuleSpecJSI>(self, jsInvoker);
} }
@ -48,9 +49,9 @@ RCT_EXPORT_MODULE()
CGSize screenSize = mainScreen.bounds.size; CGSize screenSize = mainScreen.bounds.size;
return @{ return @{
@"const1": @YES, @"const1" : @YES,
@"const2": @(screenSize.width), @"const2" : @(screenSize.width),
@"const3": @"something", @"const3" : @"something",
}; };
} }
@ -65,56 +66,62 @@ RCT_EXPORT_METHOD(voidFunc)
// Nothing to do // Nothing to do
} }
RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSNumber *, getBool:(BOOL)arg) RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSNumber *, getBool : (BOOL)arg)
{ {
return @(arg); return @(arg);
} }
RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSNumber *, getNumber:(double)arg) RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSNumber *, getNumber : (double)arg)
{ {
return @(arg); return @(arg);
} }
RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSString *, getString:(NSString *)arg) RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSString *, getString : (NSString *)arg)
{ {
return arg; return arg;
} }
RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSArray<id<NSObject>> *, getArray:(NSArray *)arg) RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSArray<id<NSObject>> *, getArray : (NSArray *)arg)
{ {
return arg; return arg;
} }
RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSDictionary *, getObject:(NSDictionary *)arg) RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSDictionary *, getObject : (NSDictionary *)arg)
{ {
return arg; return arg;
} }
RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSDictionary *, getValue:(double)x y:(NSString *)y z:(NSDictionary *)z) RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSDictionary *, getValue : (double)x y : (NSString *)y z : (NSDictionary *)z)
{ {
return @{ return @{
@"x": @(x), @"x" : @(x),
@"y": y ?: [NSNull null], @"y" : y ?: [NSNull null],
@"z": z ?: [NSNull null], @"z" : z ?: [NSNull null],
}; };
} }
RCT_EXPORT_METHOD(getValueWithCallback:(RCTResponseSenderBlock)callback) RCT_EXPORT_METHOD(getValueWithCallback : (RCTResponseSenderBlock)callback)
{ {
if (!callback) { if (!callback) {
return; return;
} }
callback(@[@"value from callback!"]); callback(@[ @"value from callback!" ]);
} }
RCT_EXPORT_METHOD(getValueWithPromise:(BOOL)error resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject) RCT_EXPORT_METHOD(getValueWithPromise
: (BOOL)error resolve
: (RCTPromiseResolveBlock)resolve reject
: (RCTPromiseRejectBlock)reject)
{ {
if (!resolve || !reject) { if (!resolve || !reject) {
return; return;
} }
if (error) { if (error) {
reject(@"code_1", @"intentional promise rejection", [NSError errorWithDomain:@"RCTSampleTurboModule" code:1 userInfo:nil]); reject(
@"code_1",
@"intentional promise rejection",
[NSError errorWithDomain:@"RCTSampleTurboModule" code:1 userInfo:nil]);
} else { } else {
resolve(@"result!"); resolve(@"result!");
} }

Просмотреть файл

@ -20,76 +20,77 @@ std::string SampleTurboCxxModuleLegacyImpl::getName() {
return "SampleTurboCxxModule_v2"; return "SampleTurboCxxModule_v2";
} }
std::map<std::string, folly::dynamic> SampleTurboCxxModuleLegacyImpl::getConstants() { std::map<std::string, folly::dynamic>
SampleTurboCxxModuleLegacyImpl::getConstants() {
return { return {
{"const1", true}, {"const1", true},
{"const2", 375}, {"const2", 375},
{"const3", "something"}, {"const3", "something"},
}; };
}; };
std::vector<CxxModule::Method> SampleTurboCxxModuleLegacyImpl::getMethods() { std::vector<CxxModule::Method> SampleTurboCxxModuleLegacyImpl::getMethods() {
return { return {
CxxModule::Method( CxxModule::Method(
"voidFunc", "voidFunc", [this](folly::dynamic args) { voidFunc(); }),
[this](folly::dynamic args) { CxxModule::Method(
voidFunc(); "getBool",
}), [this](folly::dynamic args) {
CxxModule::Method( return getBool(xplat::jsArgAsBool(args, 0));
"getBool", },
[this](folly::dynamic args) { CxxModule::SyncTag),
return getBool(xplat::jsArgAsBool(args, 0)); CxxModule::Method(
}, "getNumber",
CxxModule::SyncTag), [this](folly::dynamic args) {
CxxModule::Method( return getNumber(xplat::jsArgAsDouble(args, 0));
"getNumber", },
[this](folly::dynamic args) { CxxModule::SyncTag),
return getNumber(xplat::jsArgAsDouble(args, 0)); CxxModule::Method(
}, "getString",
CxxModule::SyncTag), [this](folly::dynamic args) {
CxxModule::Method( return getString(xplat::jsArgAsString(args, 0));
"getString", },
[this](folly::dynamic args) { CxxModule::SyncTag),
return getString(xplat::jsArgAsString(args, 0)); CxxModule::Method(
}, "getString",
CxxModule::SyncTag), [this](folly::dynamic args) {
CxxModule::Method( return getString(xplat::jsArgAsString(args, 0));
"getString", },
[this](folly::dynamic args) { CxxModule::SyncTag),
return getString(xplat::jsArgAsString(args, 0)); CxxModule::Method(
}, "getArray",
CxxModule::SyncTag), [this](folly::dynamic args) {
CxxModule::Method( return getArray(xplat::jsArgAsArray(args, 0));
"getArray", },
[this](folly::dynamic args) { CxxModule::SyncTag),
return getArray(xplat::jsArgAsArray(args, 0)); CxxModule::Method(
}, "getObject",
CxxModule::SyncTag), [this](folly::dynamic args) {
CxxModule::Method( return getObject(xplat::jsArgAsObject(args, 0));
"getObject", },
[this](folly::dynamic args) { CxxModule::SyncTag),
return getObject(xplat::jsArgAsObject(args, 0)); CxxModule::Method(
}, "getValue",
CxxModule::SyncTag), [this](folly::dynamic args) {
CxxModule::Method( return getValue(
"getValue", xplat::jsArgAsDouble(args, 0),
[this](folly::dynamic args) { xplat::jsArgAsString(args, 1),
return getValue( xplat::jsArgAsObject(args, 2));
xplat::jsArgAsDouble(args, 0), },
xplat::jsArgAsString(args, 1), CxxModule::SyncTag),
xplat::jsArgAsObject(args, 2)); CxxModule::Method(
}, "getValueWithCallback",
CxxModule::SyncTag), [this](folly::dynamic args, CxxModule::Callback callback) {
CxxModule::Method( getValueWithCallback(callback);
"getValueWithCallback", }),
[this](folly::dynamic args, CxxModule::Callback callback) { CxxModule::Method(
getValueWithCallback(callback); "getValueWithPromise",
}), [this](
CxxModule::Method( folly::dynamic args,
"getValueWithPromise", CxxModule::Callback resolve,
[this](folly::dynamic args, CxxModule::Callback resolve, CxxModule::Callback reject) { CxxModule::Callback reject) {
getValueWithPromise(xplat::jsArgAsBool(args, 0), resolve, reject); getValueWithPromise(xplat::jsArgAsBool(args, 0), resolve, reject);
}), }),
}; };
}; };
@ -109,30 +110,37 @@ std::string SampleTurboCxxModuleLegacyImpl::getString(const std::string &arg) {
return arg; return arg;
} }
folly::dynamic SampleTurboCxxModuleLegacyImpl::getArray(const folly::dynamic &arg) { folly::dynamic SampleTurboCxxModuleLegacyImpl::getArray(
const folly::dynamic &arg) {
return arg; return arg;
} }
folly::dynamic SampleTurboCxxModuleLegacyImpl::getObject(const folly::dynamic &arg) { folly::dynamic SampleTurboCxxModuleLegacyImpl::getObject(
const folly::dynamic &arg) {
return arg; return arg;
} }
folly::dynamic SampleTurboCxxModuleLegacyImpl::getValue(double x, const std::string &y, const folly::dynamic &z) { folly::dynamic SampleTurboCxxModuleLegacyImpl::getValue(
return folly::dynamic::object double x,
("x", x) const std::string &y,
("y", y) const folly::dynamic &z) {
("z", z); return folly::dynamic::object("x", x)("y", y)("z", z);
} }
void SampleTurboCxxModuleLegacyImpl::getValueWithCallback(const CxxModule::Callback &callback) { void SampleTurboCxxModuleLegacyImpl::getValueWithCallback(
const CxxModule::Callback &callback) {
callback({"value from callback!"}); callback({"value from callback!"});
} }
void SampleTurboCxxModuleLegacyImpl::getValueWithPromise(bool error, const CxxModule::Callback &resolve, const CxxModule::Callback &reject) { void SampleTurboCxxModuleLegacyImpl::getValueWithPromise(
bool error,
const CxxModule::Callback &resolve,
const CxxModule::Callback &reject) {
if (!error) { if (!error) {
resolve({"result!"}); resolve({"result!"});
} else { } else {
reject({folly::dynamic::object("message", "intentional promise rejection")}); reject(
{folly::dynamic::object("message", "intentional promise rejection")});
} }
} }

Просмотреть файл

@ -15,8 +15,9 @@ namespace react {
/** /**
* A sample CxxModule (legacy system) implementation. * A sample CxxModule (legacy system) implementation.
*/ */
class SampleTurboCxxModuleLegacyImpl : public facebook::xplat::module::CxxModule { class SampleTurboCxxModuleLegacyImpl
public: : public facebook::xplat::module::CxxModule {
public:
SampleTurboCxxModuleLegacyImpl(); SampleTurboCxxModuleLegacyImpl();
std::string getName() override; std::string getName() override;
@ -30,9 +31,14 @@ public:
std::string getString(const std::string &arg); std::string getString(const std::string &arg);
folly::dynamic getArray(const folly::dynamic &arg); folly::dynamic getArray(const folly::dynamic &arg);
folly::dynamic getObject(const folly::dynamic &arg); folly::dynamic getObject(const folly::dynamic &arg);
folly::dynamic getValue(double x, const std::string &y, const folly::dynamic &z); folly::dynamic
void getValueWithCallback(const facebook::xplat::module::CxxModule::Callback &callback); getValue(double x, const std::string &y, const folly::dynamic &z);
void getValueWithPromise(bool error, const facebook::xplat::module::CxxModule::Callback &resolve, const facebook::xplat::module::CxxModule::Callback &reject); void getValueWithCallback(
const facebook::xplat::module::CxxModule::Callback &callback);
void getValueWithPromise(
bool error,
const facebook::xplat::module::CxxModule::Callback &resolve,
const facebook::xplat::module::CxxModule::Callback &reject);
}; };
} // namespace react } // namespace react