Files
react-native/ReactCommon/turbomodule/samples/platform/ios/RCTSampleTurboCxxModule.mm
Kevin Gozali a2aba45067 TM iOS: Move SampleTurboModule to OSS
Summary:
This provides various versions of SampleTurboModule, that are:
* compatible with existing NativeModule
* TurboModule compliant

Variants:
* RCTSampleTurboModule (traditional objc module)
* RCTSampleTurboCxxModule (objc++ module using CxxModule)
* SampleTurboModule (pure C++ impl of a TurboModule, no ObjC)

As noted in some files, they need to be codegen'ed based on the `NativeSampleTurboModule.js` (Flow type). The codegen script is not yet usable in OSS (we'll work on it some time in H2 2019). For now, these files need to be manually synced with Flow type.

Reviewed By: cpojer

Differential Revision: D14932539

fbshipit-source-id: fb887192384e5e6e4dff4cac68b4e037a4783cd9
2019-04-15 12:25:47 -07:00

110 lines
3.0 KiB
Plaintext

/**
* 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.
*/
#import "RCTSampleTurboCxxModule.h"
#import <cxxreact/CxxModule.h>
#import <jsireact/TurboModuleUtils.h>
#import "SampleTurboCxxModuleLegacyImpl.h"
using namespace facebook;
namespace facebook {
namespace react {
SampleTurboCxxModule::SampleTurboCxxModule(std::shared_ptr<JSCallInvoker> jsInvoker)
: NativeSampleTurboCxxModuleSpecJSI(jsInvoker) {}
void SampleTurboCxxModule::voidFunc(jsi::Runtime &rt) {
// Nothing to do
}
bool SampleTurboCxxModule::getBool(jsi::Runtime &rt, bool arg) {
return arg;
}
double SampleTurboCxxModule::getNumber(jsi::Runtime &rt, double arg) {
return arg;
}
jsi::String SampleTurboCxxModule::getString(jsi::Runtime &rt, const jsi::String &arg) {
return jsi::String::createFromUtf8(rt, arg.utf8(rt));
}
jsi::Array SampleTurboCxxModule::getArray(jsi::Runtime &rt, const jsi::Array &arg) {
return deepCopyJSIArray(rt, arg);
}
jsi::Object SampleTurboCxxModule::getObject(jsi::Runtime &rt, const jsi::Object &arg) {
return deepCopyJSIObject(rt, arg);
}
jsi::Object SampleTurboCxxModule::getValue(jsi::Runtime &rt, double x, const jsi::String &y, const jsi::Object &z) {
// Note: return type isn't type-safe.
jsi::Object result(rt);
result.setProperty(rt, "x", jsi::Value(x));
result.setProperty(rt, "y", jsi::String::createFromUtf8(rt, y.utf8(rt)));
result.setProperty(rt, "z", deepCopyJSIObject(rt, z));
return result;
}
void SampleTurboCxxModule::getValueWithCallback(jsi::Runtime &rt, const jsi::Function &callback) {
callback.call(rt, jsi::String::createFromUtf8(rt, "value from callback!"));
}
jsi::Value SampleTurboCxxModule::getValueWithPromise(jsi::Runtime &rt, bool error) {
return createPromiseAsJSIValue(rt, [error](jsi::Runtime &rt2, std::shared_ptr<Promise> promise) {
if (error) {
promise->reject("intentional promise rejection");
} else {
promise->resolve(jsi::String::createFromUtf8(rt2, "result!"));
}
});
}
jsi::Object SampleTurboCxxModule::getConstants(jsi::Runtime &rt) {
// Note: return type isn't type-safe.
jsi::Object result(rt);
result.setProperty(rt, "const1", jsi::Value(true));
result.setProperty(rt, "const2", jsi::Value(375));
result.setProperty(rt, "const3", jsi::String::createFromUtf8(rt, "something"));
return result;
}
} // namespace react
} // namespace facebook
// ObjC++ wrapper.
@implementation RCTSampleTurboCxxModule_v1
RCT_EXPORT_MODULE();
- (std::shared_ptr<react::TurboModule>)getTurboModuleWithJsInvoker:(std::shared_ptr<react::JSCallInvoker>)jsInvoker
{
return std::make_shared<react::SampleTurboCxxModule>(jsInvoker);
}
- (std::unique_ptr<xplat::module::CxxModule>)createModule
{
return nullptr;
}
@end
@implementation RCTSampleTurboCxxModule_v2
RCT_EXPORT_MODULE();
- (std::unique_ptr<xplat::module::CxxModule>)createModule
{
return std::make_unique<react::SampleTurboCxxModuleLegacyImpl>();
}
@end