6#include <Ultralight/CAPI/CAPI_JSRuntime.h>
7#include <Ultralight/detail/Exceptions.h>
73 ulDestroyJSWeakRef(weak_);
75 other.weak_ =
nullptr;
102 template <
typename F>
104 using Fn = std::decay_t<F>;
105 static_assert(std::is_invocable_v<Fn&> || std::is_invocable_v<Fn&, Context&>,
106 "js::WeakValue::OnCollected takes a callable invocable with nothing or "
107 "with js::Context&");
110 ulJSWeakRefSetCollectedCallback(weak_, &CollectedThunk<Fn>,
111 new Fn(std::forward<F>(on_collected)),
112 &DeleteCollected<Fn>);
121 bool IsEmpty()
const {
return weak_ ==
nullptr; }
143 ULJSWeakRef
raw()
const {
return weak_; }
151 ULJSWeakRef weak = weak_;
157 template <
typename Fn>
158 static void CollectedThunk(
void* user_data,
ULJSContext ctx) {
159 Fn& fn = *
static_cast<Fn*
>(user_data);
161 ultralight::detail::CallCatchingExceptions(
163 if constexpr (std::is_invocable_v<Fn&, Context&>) {
170 [](
const char* what) { detail::LogNativeException(
"js::WeakValue::OnCollected", what); });
173 template <
typename Fn>
174 static void DeleteCollected(
void* user_data) {
175 delete static_cast<Fn*
>(user_data);
178 ULJSWeakRef weak_ =
nullptr;
struct C_JSContext * ULJSContext
Opaque handle to a JavaScript execution context (one page's script world in one View).
Definition View.h:25
JavaScript execution environment for a page.
Definition Context.h:151
static Context FromBorrowed(ULJSContext handle)
Add a reference to a context handle the library owns (eg, the context a C API callback receives) so y...
Definition Context.h:200
A handle to a live JavaScript value.
Definition Value.h:210
static Value Adopt(ULJSValue handle)
Take ownership of a handle from the C API without adding a reference.
Definition Value.h:799
WeakValue(const WeakValue &)=delete
static WeakValue Adopt(ULJSWeakRef handle)
Take ownership of a handle from the C API.
Definition WeakValue.h:132
~WeakValue()
Destructor (releases the weak reference).
Definition WeakValue.h:61
WeakValue & operator=(const WeakValue &)=delete
WeakValue(const Value &target)
Create a weak reference to target.
Definition WeakValue.h:56
void OnCollected(F &&on_collected)
Set a callback to run after the object is garbage collected.
Definition WeakValue.h:103
WeakValue(WeakValue &&other) noexcept
Move constructor (other becomes empty).
Definition WeakValue.h:66
bool IsEmpty() const
Whether or not this WeakValue holds nothing (it was default-constructed, moved from,...
Definition WeakValue.h:121
ULJSWeakRef raw() const
Get the C API handle without transferring ownership.
Definition WeakValue.h:143
ULJSWeakRef LeakRef()
Give up ownership of the C API handle and return it.
Definition WeakValue.h:150
WeakValue()=default
Create an empty WeakValue (Lock() returns an empty Value).
Value Lock() const
Get a Value for the object.
Definition WeakValue.h:88
WeakValue & operator=(WeakValue &&other) noexcept
Move assignment (releases the current weak reference first).
Definition WeakValue.h:71
Type-checked JavaScript bridge between C++ and web pages.
Definition JSInterop.h:141
Root namespace for every public Ultralight type, function, and enumeration.