Files
react-native/ReactCommon/react/renderer/core/EventTarget.cpp
T
Joshua Gross b3930f935f Convert most Fabric Cxx code to use react_native_assert instead of assert
Summary:
See react_native_assert.{h,cpp}. Because of the BUCK+Android issue where NDEBUG is always defined, we use react_native_assert instead of assert to enable xplat asserts in debug/dev mode.

This migrates most of the codebase, but probably not 100%. The goal is to increase assertion coverage on Android, not to get to 100% (yet).

Changelog: [Internal]

Reviewed By: RSNara

Differential Revision: D26562866

fbshipit-source-id: a7bf2055b973e1d3650ed8d68a6d02d556604af9
2021-02-19 20:52:52 -08:00

69 lines
2.0 KiB
C++

/*
* 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.
*/
#include "EventTarget.h"
#include <react/debug/react_native_assert.h>
namespace facebook {
namespace react {
using Tag = EventTarget::Tag;
EventTarget::EventTarget(
jsi::Runtime &runtime,
jsi::Value const &instanceHandle,
Tag tag)
: weakInstanceHandle_(
jsi::WeakObject(runtime, instanceHandle.asObject(runtime))),
strongInstanceHandle_(jsi::Value::null()),
tag_(tag) {}
void EventTarget::setEnabled(bool enabled) const {
enabled_ = enabled;
}
void EventTarget::retain(jsi::Runtime &runtime) const {
if (!enabled_) {
return;
}
strongInstanceHandle_ = weakInstanceHandle_.lock(runtime);
// Having a `null` or `undefined` object here indicates that
// `weakInstanceHandle_` was already deallocated. This should *not* happen by
// design, and if it happens it's a severe problem. This basically means that
// particular implementation of JSI was able to detect this inconsistency and
// dealt with it, but some JSI implementation may not support this feature and
// that case will lead to a crash in those environments.
react_native_assert(!strongInstanceHandle_.isNull());
react_native_assert(!strongInstanceHandle_.isUndefined());
}
void EventTarget::release(jsi::Runtime &runtime) const {
// The method does not use `jsi::Runtime` reference.
// It takes it only to ensure thread-safety (if the caller has the reference,
// we are on a proper thread).
strongInstanceHandle_ = jsi::Value::null();
}
jsi::Value EventTarget::getInstanceHandle(jsi::Runtime &runtime) const {
if (strongInstanceHandle_.isNull()) {
// The `instanceHandle` is not retained.
return jsi::Value::null();
}
return jsi::Value(runtime, strongInstanceHandle_);
}
Tag EventTarget::getTag() const {
return tag_;
}
} // namespace react
} // namespace facebook