-
Notifications
You must be signed in to change notification settings - Fork 24.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Android Cxx TurboModule support
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
1 parent
08d87cd
commit e1102b4
Showing
8 changed files
with
145 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters