Skip to content

Commit

Permalink
Implement Android Cxx TurboModule support
Browse files Browse the repository at this point in the history
Summary:
## Summary
This diff does a bunch of things:
1. The TurboModule resolution algorithm on Android now supports C++ TurboModules.
2. `SampleTurboCxxModule` is moved from `ReactCommon/turbomodule/samples/platform/ios/` to `ReactCommon/turbomodule/samples` so that both iOS and Android can share it.
3. `CatalystTurboModuleManagerDelegate::getTurboModule(std::string, JSCallInvoker)` now understands and returns `SampleTurboCxxModule`.

Reviewed By: mdvacca

Differential Revision: D15253477

fbshipit-source-id: 3def91911b091f8cf93be17decd245a0499ed718
  • Loading branch information
RSNara authored and facebook-github-bot committed May 22, 2019
1 parent 08d87cd commit e1102b4
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 99 deletions.
2 changes: 1 addition & 1 deletion RNTester/RNTester/RNTesterTurboModuleProvider.mm
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#import "RNTesterTurboModuleProvider.h"

#import <jsireact/RCTSampleTurboCxxModule.h>
#import <jsireact/RCTSampleTurboModule.h>
#import <jsireact/SampleTurboModule.h>

// NOTE: This entire file should be codegen'ed.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ void TurboModuleManager::installJSIBindings() {
}
TurboModuleBinding::install(*runtime_, std::make_shared<TurboModuleBinding>(
[this](const std::string &name) {
auto cxxModule = turboModuleManagerDelegate_->cthis()->getTurboModule(name, jsCallInvoker_);
if (cxxModule) {
return cxxModule;
}

const auto moduleInstance = getJavaModule(name);
return turboModuleManagerDelegate_->cthis()->getTurboModule(name, moduleInstance, jsCallInvoker_);
})
Expand Down
94 changes: 94 additions & 0 deletions ReactCommon/turbomodule/samples/SampleTurboCxxModule.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/**
* 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 "SampleTurboCxxModule.h"

#import <jsireact/TurboModuleUtils.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
44 changes: 44 additions & 0 deletions ReactCommon/turbomodule/samples/SampleTurboCxxModule.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* 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.
*/

#pragma once

#import <memory>

#import "NativeSampleTurboCxxModuleSpecJSI.h"

namespace facebook {
namespace react {

/**
* A sample implementation of the C++ spec. In practice, this class can just
* extend jsi::HostObject directly, but using the spec provides build-time
* type-safety.
*/
class SampleTurboCxxModule : public NativeSampleTurboCxxModuleSpecJSI {
public:
SampleTurboCxxModule(std::shared_ptr<JSCallInvoker> jsInvoker);

void voidFunc(jsi::Runtime &rt) override;
bool getBool(jsi::Runtime &rt, bool arg) override;
double getNumber(jsi::Runtime &rt, double arg) override;
jsi::String getString(jsi::Runtime &rt, const jsi::String &arg) override;
jsi::Array getArray(jsi::Runtime &rt, const jsi::Array &arg) override;
jsi::Object getObject(jsi::Runtime &rt, const jsi::Object &arg) override;
jsi::Object getValue(
jsi::Runtime &rt,
double x,
const jsi::String &y,
const jsi::Object &z) override;
void getValueWithCallback(jsi::Runtime &rt, const jsi::Function &callback)
override;
jsi::Value getValueWithPromise(jsi::Runtime &rt, bool error) override;
jsi::Object getConstants(jsi::Runtime &rt) override;
};

} // namespace react
} // namespace facebook
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,9 @@

#pragma once

#import <memory>

#import <React/RCTCxxModule.h>
#import <jsireact/RCTTurboModule.h>

#import "NativeSampleTurboCxxModuleSpecJSI.h"

namespace facebook {
namespace react {

/**
* A sample implementation of the C++ spec. In practice, this class can just extend jsi::HostObject
* directly, but using the spec provides build-time type-safety.
*/
class SampleTurboCxxModule : public NativeSampleTurboCxxModuleSpecJSI {
public:
SampleTurboCxxModule(std::shared_ptr<JSCallInvoker> jsInvoker);

void voidFunc(jsi::Runtime &rt) override;
bool getBool(jsi::Runtime &rt, bool arg) override;
double getNumber(jsi::Runtime &rt, double arg) override;
jsi::String getString(jsi::Runtime &rt, const jsi::String &arg) override;
jsi::Array getArray(jsi::Runtime &rt, const jsi::Array &arg) override;
jsi::Object getObject(jsi::Runtime &rt, const jsi::Object &arg) override;
jsi::Object getValue(jsi::Runtime &rt, double x, const jsi::String &y, const jsi::Object &z) override;
void getValueWithCallback(jsi::Runtime &rt, const jsi::Function &callback) override;
jsi::Value getValueWithPromise(jsi::Runtime &rt, bool error) override;
jsi::Object getConstants(jsi::Runtime &rt) override;
};

} // namespace react
} // namespace facebook

/**
* Sample backward-compatible RCTCxxModule-based module.
* With jsi::HostObject, this class is no longer necessary, but the system supports it for
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,78 +8,11 @@
#import "RCTSampleTurboCxxModule.h"

#import <cxxreact/CxxModule.h>
#import <jsireact/TurboModuleUtils.h>

#import <jsireact/SampleTurboCxxModule.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

Expand Down

0 comments on commit e1102b4

Please sign in to comment.