dymokomi/luce-browser-foundation
Source of release 0.6.0
#==============================================================================================
#
# weak - WeakImpl and Weak<T>: references that do not keep a cell alive
#
# DESCRIPTION:
# Port of Libraries/LibGC/Weak.h (lines 1-186) and WeakInlines.h (lines 1-113). A Weak<T>
# holds a WeakImpl from the heap's weak blocks; when the cell dies, WeakBlock::sweep clears
# the impl's pointer. A null Weak<T> points at the_null_weak_impl in C++; in the port its
# impl is none, which reads the same.
#
# C++ reference counts the impl: every copy of a Weak refs it, every destructor unrefs it,
# and sweep_weak_blocks frees the impls no Weak holds. The port has no destructors and
# copies a Weak as plain bytes, wherever it lives (a cell, a blob, the stack, the C heap, a
# module global). So a cell has one impl, which every Weak to it shares
# (heap_create_weak_impl), and the impl goes back to its block when its cell dies. Each
# impl counts how often it went back (its generation) and each Weak remembers the
# generation it was made with: a Weak whose impl went back reads none, as the C++ Weak to
# a dead cell does. A Weak is therefore valid for as long as anything holds it, and no
# memory has to be scanned to find it (DESIGN.md ยง3.3).
#
#==============================================================================================
# mark: WeakImpl ================================================================================
## Ported from Libraries/LibGC/Weak.h:24 GC::WeakImpl::WeakImpl.
pub func weak_impl_construct(this: WeakImpl*, ptr: void*?):
this.m_generation = 0
this.m_state = WeakImplState.allocated
this.m_ptr = ptr
## Ported from Libraries/LibGC/Weak.h:29 GC::WeakImpl::ptr.
pub func weak_impl_ptr(this: const WeakImpl*) -> void*?:
return this.m_ptr
## Ported from Libraries/LibGC/Weak.h:30 GC::WeakImpl::set_ptr.
pub func weak_impl_set_ptr(this: WeakImpl*, ptr: void*?):
this.m_ptr = ptr
## Ported from Libraries/LibGC/Weak.h:32 GC::WeakImpl::operator==.
pub func weak_impl_eq(this: const WeakImpl*, other: WeakImpl) -> bool:
return this.m_ptr == other.m_ptr
## Ported from Libraries/LibGC/Weak.h:33 GC::WeakImpl::operator!=.
pub func weak_impl_ne(this: const WeakImpl*, other: WeakImpl) -> bool:
return this.m_ptr != other.m_ptr
## Ported from Libraries/LibGC/Weak.h:49 GC::WeakImpl::set_state.
pub func weak_impl_set_state(this: WeakImpl*, state: WeakImplState):
this.m_state = state
## Ported from Libraries/LibGC/Weak.h:50 GC::WeakImpl::state.
pub func weak_impl_state(this: const WeakImpl*) -> WeakImplState:
return this.m_state
# mark: Weak ====================================================================================
## Ported from Libraries/LibGC/WeakInlines.h:15 GC::Weak::Weak (T const*): a weak reference to
## `ptr`, or a null one.
pub func weak_construct[T: IsCell](this: Weak[T]*, ptr: T*?):
if let cell = ptr:
let impl = heap_create_weak_impl(cell_heap(cell.cell()), cell.cell())
this.m_impl = impl
this.m_generation = impl.m_generation
else:
this.m_impl = none
this.m_generation = 0
## Ported from Libraries/LibGC/WeakInlines.h:15 GC::Weak::Weak (T const*), as a value.
pub func make_weak[T: IsCell](ptr: T*?) -> Weak[T]:
var weak: Weak[T]
weak_construct(&weak, ptr)
return weak
## Ported from Libraries/LibGC/WeakInlines.h:96 GC::Weak::operator= (T const*).
pub func weak_assign[T: IsCell](this: Weak[T]*, other: T*?):
if (void*?)weak_ptr(this) != (void*?)other:
weak_construct(this, other)
## Ported from Libraries/LibGC/Weak.h:118 GC::Weak::ptr: the cell, or none once it died.
pub func weak_ptr[T](this: const Weak[T]*) -> T*?:
let impl = weak_impl(this) else return none
return (T*?)weak_impl_ptr(impl)
## Ported from Libraries/LibGC/Weak.h:131 GC::Weak::impl: none for a null Weak, and for one
## whose impl went back to its block with its dead cell (a later generation of the impl may serve
## another cell).
pub func weak_impl[T](this: const Weak[T]*) -> WeakImpl*?:
let impl = (WeakImpl*?)this.m_impl else return none
if impl.m_generation != this.m_generation:
return none
return impl
## Ported from Libraries/LibGC/Weak.h:154 GC::weak_callback: a callback that runs `callback` on
## `obj` while `obj` lives. (C++ forwards any further arguments; the port's has none.)
# NOTE: Unlike GC::Function, captures in this callback are not visited by the garbage collector.
# Do not capture GC-managed objects without ensuring they are kept alive through other means.
pub func weak_callback[T: IsCell](obj: T*, callback: ak.Function1[T*, unit]) -> WeakCallback[T]:
return WeakCallback[T](weak = make_weak[T](obj), cb = callback)
## The call of the lambda weak_callback returns: `if (weak) cb(*weak)`.
pub func weak_callback_call[T](this: const WeakCallback[T]*):
if let object = weak_ptr(&this.weak):
ak.function1_call(&this.cb, object)
## Ported from Libraries/LibGC/Weak.h:163 GC::operator== (Weak<T>, Ptr<U>).
pub func eq_weak_u[T, U](a: Weak[T], b: U*?) -> bool:
return (void*?)weak_ptr(&a) == (void*?)b
## Ported from Libraries/LibGC/Weak.h:169 GC::operator== (Weak<T>, Ref<U>).
pub func eq_weak_u_2[T, U](a: Weak[T], b: U*) -> bool:
return (void*?)weak_ptr(&a) == (void*?)b