Skip to content

Commit 6179233

Browse files
kodafbfacebook-github-bot
authored andcommitted
Add API for setting/getting native state
Summary: Add API for setting/getting native state. When present, the internal NativeState property of an object always stores a NativeState with a pointer to a heap-allocated shared_ptr + a finalizer that simply `delete`s it. Changelog: [Internal][Added] - JSI API for setting/getting native state on a JS object Reviewed By: jpporto Differential Revision: D36499239 fbshipit-source-id: a1ff1905811db1aac99ece3f928b81d0abfb342b
1 parent 639daf8 commit 6179233

File tree

5 files changed

+84
-0
lines changed

5 files changed

+84
-0
lines changed

ReactCommon/jsi/JSCRuntime.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,12 @@ class JSCRuntime : public jsi::Runtime {
168168
const jsi::Object &) override;
169169
jsi::HostFunctionType &getHostFunction(const jsi::Function &) override;
170170

171+
bool hasNativeState(const jsi::Object &) override;
172+
std::shared_ptr<jsi::NativeState> getNativeState(
173+
const jsi::Object &) override;
174+
void setNativeState(const jsi::Object &, std::shared_ptr<jsi::NativeState>)
175+
override;
176+
171177
jsi::Value getProperty(const jsi::Object &, const jsi::String &name) override;
172178
jsi::Value getProperty(const jsi::Object &, const jsi::PropNameID &name)
173179
override;
@@ -862,6 +868,21 @@ std::shared_ptr<jsi::HostObject> JSCRuntime::getHostObject(
862868
return metadata->hostObject;
863869
}
864870

871+
bool JSCRuntime::hasNativeState(const jsi::Object &) {
872+
throw std::logic_error("Not implemented");
873+
}
874+
875+
std::shared_ptr<jsi::NativeState> JSCRuntime::getNativeState(
876+
const jsi::Object &) {
877+
throw std::logic_error("Not implemented");
878+
}
879+
880+
void JSCRuntime::setNativeState(
881+
const jsi::Object &,
882+
std::shared_ptr<jsi::NativeState>) {
883+
throw std::logic_error("Not implemented");
884+
}
885+
865886
jsi::Value JSCRuntime::getProperty(
866887
const jsi::Object &obj,
867888
const jsi::String &name) {

ReactCommon/jsi/jsi/decorator.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,17 @@ class RuntimeDecorator : public Base, private jsi::Instrumentation {
222222
return dhf.target<DecoratedHostFunction>()->plainHF_;
223223
};
224224

225+
bool hasNativeState(const Object& o) override {
226+
return plain_.hasNativeState(o);
227+
}
228+
std::shared_ptr<NativeState> getNativeState(const Object& o) override {
229+
return plain_.getNativeState(o);
230+
}
231+
void setNativeState(const Object& o, std::shared_ptr<NativeState> state)
232+
override {
233+
plain_.setNativeState(o, state);
234+
}
235+
225236
Value getProperty(const Object& o, const PropNameID& name) override {
226237
return plain_.getProperty(o, name);
227238
};

ReactCommon/jsi/jsi/jsi-inl.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,24 @@ inline std::shared_ptr<HostObject> Object::getHostObject<HostObject>(
202202
return runtime.getHostObject(*this);
203203
}
204204

205+
template <typename T>
206+
inline bool Object::hasNativeState(Runtime& runtime) const {
207+
return runtime.hasNativeState(*this) &&
208+
std::dynamic_pointer_cast<T>(runtime.getNativeState(*this));
209+
}
210+
211+
template <typename T>
212+
inline std::shared_ptr<T> Object::getNativeState(Runtime& runtime) const {
213+
assert(hasNativeState<T>(runtime));
214+
return std::static_pointer_cast<T>(runtime.getNativeState(*this));
215+
}
216+
217+
inline void Object::setNativeState(
218+
Runtime& runtime,
219+
std::shared_ptr<NativeState> state) const {
220+
runtime.setNativeState(*this, state);
221+
}
222+
205223
inline Array Object::getPropertyNames(Runtime& runtime) const {
206224
return runtime.getPropertyNames(*this);
207225
}

ReactCommon/jsi/jsi/jsi.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ void HostObject::set(Runtime& rt, const PropNameID& name, const Value&) {
8181

8282
HostObject::~HostObject() {}
8383

84+
NativeState::~NativeState() {}
85+
8486
Runtime::~Runtime() {}
8587

8688
Instrumentation& Runtime::instrumentation() {

ReactCommon/jsi/jsi/jsi.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,13 @@ class JSI_EXPORT HostObject {
128128
virtual std::vector<PropNameID> getPropertyNames(Runtime& rt);
129129
};
130130

131+
/// Native state (and destructor) that can be attached to any JS object
132+
/// using setNativeState.
133+
class JSI_EXPORT NativeState {
134+
public:
135+
virtual ~NativeState();
136+
};
137+
131138
/// Represents a JS runtime. Movable, but not copyable. Note that
132139
/// this object may not be thread-aware, but cannot be used safely from
133140
/// multiple threads at once. The application is responsible for
@@ -296,6 +303,12 @@ class JSI_EXPORT Runtime {
296303
virtual std::shared_ptr<HostObject> getHostObject(const jsi::Object&) = 0;
297304
virtual HostFunctionType& getHostFunction(const jsi::Function&) = 0;
298305

306+
virtual bool hasNativeState(const jsi::Object&) = 0;
307+
virtual std::shared_ptr<NativeState> getNativeState(const jsi::Object&) = 0;
308+
virtual void setNativeState(
309+
const jsi::Object&,
310+
std::shared_ptr<NativeState> state) = 0;
311+
299312
virtual Value getProperty(const Object&, const PropNameID& name) = 0;
300313
virtual Value getProperty(const Object&, const String& name) = 0;
301314
virtual bool hasProperty(const Object&, const PropNameID& name) = 0;
@@ -711,6 +724,25 @@ class JSI_EXPORT Object : public Pointer {
711724
template <typename T = HostObject>
712725
std::shared_ptr<T> asHostObject(Runtime& runtime) const;
713726

727+
/// \return whether this object has native state of type T previously set by
728+
/// \c setNativeState.
729+
template <typename T = NativeState>
730+
bool hasNativeState(Runtime& runtime) const;
731+
732+
/// \return a shared_ptr to the state previously set by \c setNativeState.
733+
/// If \c hasNativeState<T> is false, this will assert. Note that this does a
734+
/// type check and will assert if the native state isn't of type \c T
735+
template <typename T = NativeState>
736+
std::shared_ptr<T> getNativeState(Runtime& runtime) const;
737+
738+
/// Set the internal native state property of this object, overwriting any old
739+
/// value. Creates a new shared_ptr to the object managed by \p state, which
740+
/// will live until the value at this property becomes unreachable.
741+
///
742+
/// Throws a type error if this object is a proxy or host object.
743+
void setNativeState(Runtime& runtime, std::shared_ptr<NativeState> state)
744+
const;
745+
714746
/// \return same as \c getProperty(name).asObject(), except with
715747
/// a better exception message.
716748
Object getPropertyAsObject(Runtime& runtime, const char* name) const;

0 commit comments

Comments
 (0)