mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
Add castInterface API in JSI (#49604)
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/49604 Add JSI castInterface API. This provides a structured way to provide optional APIs that may or may not be supported in all engines. Every interface will have its own set of APIs. For instance, Hermes will provide an interface (`IHermes`) containing Hermes-specific APIs. In the future, an interface may be created to expose optional APIs. A runtime will inherit the interfaces of which the APIs are supported by the runtime. Users who want to access a specific API may call castInterface with the UUID of interface containing the API. For backwards compatibility, the current `jsi::Runtime` will remain as an object, and its functionalities will not be described by an interface. However, this may be revisited later. Changelog: [Internal] Reviewed By: tmikov Differential Revision: D68964360 fbshipit-source-id: 3efe61c5bc1a0fa113a192f4abad6d0f135b4301
This commit is contained in:
1 parent
e275da45ea
commit
f7f60dc794
4 files changed
+80
-1
No files matched your search
@@ -112,6 +112,10 @@ class RuntimeDecorator : public Base, private jsi::Instrumentation {
|
||||
return plain_;
|
||||
}
|
||||
|
||||
ICast* castInterface(const UUID& interfaceUUID) override {
|
||||
return plain().castInterface(interfaceUUID);
|
||||
}
|
||||
|
||||
Value evaluateJavaScript(
|
||||
const std::shared_ptr<const Buffer>& buffer,
|
||||
const std::string& sourceURL) override {
|
||||
@@ -587,6 +591,11 @@ class WithRuntimeDecorator : public RuntimeDecorator<Plain, Base> {
|
||||
// the derived class.
|
||||
WithRuntimeDecorator(Plain& plain, With& with) : RD(plain), with_(with) {}
|
||||
|
||||
ICast* castInterface(const UUID& interfaceUUID) override {
|
||||
Around around{with_};
|
||||
return RD::castInterface(interfaceUUID);
|
||||
}
|
||||
|
||||
Value evaluateJavaScript(
|
||||
const std::shared_ptr<const Buffer>& buffer,
|
||||
const std::string& sourceURL) override {
|
||||
|
||||
@@ -286,6 +286,10 @@ NativeState::~NativeState() {}
|
||||
|
||||
Runtime::~Runtime() {}
|
||||
|
||||
ICast* Runtime::castInterface(const UUID& /*interfaceUUID*/) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Instrumentation& Runtime::instrumentation() {
|
||||
class NoInstrumentation : public Instrumentation {
|
||||
std::string getRecordedGCStats() override {
|
||||
|
||||
@@ -113,6 +113,25 @@ class JSI_EXPORT UUID {
|
||||
uint64_t low;
|
||||
};
|
||||
|
||||
/// Base interface that all JSI interfaces inherit from. Users should not try to
|
||||
/// manipulate this base type directly, and should use castInterface to get the
|
||||
/// appropriate subtype.
|
||||
struct JSI_EXPORT ICast {
|
||||
/// If the current object can be cast into the interface specified by \p
|
||||
/// interfaceUUID, return a pointer to the object. Otherwise, return a null
|
||||
/// pointer.
|
||||
/// The returned interface has the same lifetime as the underlying object. It
|
||||
/// does not need to be released when not needed.
|
||||
virtual ICast* castInterface(const UUID& interfaceUUID) = 0;
|
||||
|
||||
protected:
|
||||
/// Interfaces are not destructible, thus the destructor is intentionally
|
||||
/// protected to prevent delete calls on the interface.
|
||||
/// Additionally, the destructor is non-virtual to reduce the vtable
|
||||
/// complexity from inheritance.
|
||||
~ICast() = default;
|
||||
};
|
||||
|
||||
/// Base class for buffers of data or bytecode that need to be passed to the
|
||||
/// runtime. The buffer is expected to be fully immutable, so the result of
|
||||
/// size(), data(), and the contents of the pointer returned by data() must not
|
||||
@@ -251,10 +270,12 @@ class JSI_EXPORT NativeState {
|
||||
/// in a non-Runtime-managed object, and not clean it up before the Runtime
|
||||
/// is shut down. If your lifecycle is such that avoiding this is hard,
|
||||
/// you will probably need to do use your own locks.
|
||||
class JSI_EXPORT Runtime {
|
||||
class JSI_EXPORT Runtime : public ICast {
|
||||
public:
|
||||
virtual ~Runtime();
|
||||
|
||||
ICast* castInterface(const UUID& interfaceUUID) override;
|
||||
|
||||
/// Evaluates the given JavaScript \c buffer. \c sourceURL is used
|
||||
/// to annotate the stack trace if there is an exception. The
|
||||
/// contents may be utf8-encoded JS source code, or binary bytecode
|
||||
@@ -1763,6 +1784,32 @@ class JSI_EXPORT JSError : public JSIException {
|
||||
std::string stack_;
|
||||
};
|
||||
|
||||
/// Helper function to cast the object pointed to by \p ptr into an interface
|
||||
/// specified by \c U. If cast is successful, return a pointer to the object
|
||||
/// as a raw pointer of \c U. Otherwise, return nullptr.
|
||||
/// The returned interface same lifetime as the object referenced by \p ptr.
|
||||
template <typename U, typename T>
|
||||
U* castInterface(T* ptr) {
|
||||
if (ptr) {
|
||||
return static_cast<U*>(ptr->castInterface(U::uuid));
|
||||
}
|
||||
return nullptr;
|
||||
};
|
||||
|
||||
/// Helper function to cast the object managed by the shared_ptr \p ptr into an
|
||||
/// interface specified by \c U. If the cast is successful, return a shared_ptr
|
||||
/// of type \c U to the object. Otherwise, return an empty pointer.
|
||||
/// The returned shared_ptr shares ownership of the object with \p ptr.
|
||||
template <typename U, typename T>
|
||||
std::shared_ptr<U> dynamicInterfaceCast(T&& ptr) {
|
||||
auto* p = ptr->castInterface(U::uuid);
|
||||
U* res = static_cast<U*>(p);
|
||||
if (res) {
|
||||
return std::shared_ptr<U>(std::forward<T>(ptr), res);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
} // namespace jsi
|
||||
} // namespace facebook
|
||||
|
||||
|
||||
@@ -1861,6 +1861,25 @@ TEST_P(JSITest, SetRuntimeData) {
|
||||
EXPECT_EQ(weakObj3.use_count(), 0);
|
||||
}
|
||||
|
||||
TEST_P(JSITest, CastInterface) {
|
||||
// This Runtime Decorator is used to test the default implementation of
|
||||
// jsi::Runtime::castInterface
|
||||
class RD : public RuntimeDecorator<Runtime, Runtime> {
|
||||
public:
|
||||
explicit RD(Runtime& rt) : RuntimeDecorator(rt) {}
|
||||
|
||||
ICast* castInterface(const UUID& interfaceUuid) override {
|
||||
return Runtime::castInterface(interfaceUuid);
|
||||
}
|
||||
};
|
||||
|
||||
RD rd = RD(rt);
|
||||
auto randomUuid = UUID{0xf2cd96cf, 0x455e, 0x42d9, 0x850a, 0x13e2cde59b8b};
|
||||
auto ptr = rd.castInterface(randomUuid);
|
||||
|
||||
EXPECT_EQ(ptr, nullptr);
|
||||
}
|
||||
|
||||
INSTANTIATE_TEST_CASE_P(
|
||||
Runtimes,
|
||||
JSITest,
|
||||
|
||||
Reference in new issue
Block a user