module scheme import arena enum GoopsWeakStorage { tracing arena collector } // Heap targets use standard collector weak handles. Arena targets retain their // original lifetime check; resetting/freeing an arena still requires host synchronization. struct GoopsWeakValue { value Value lifetime arena.LifetimeToken collector GCWeakReference storage GoopsWeakStorage } // Callers supply validated metaobjects or callable applicability hooks. In // particular, arbitrary scalar Value bits must never reach the raw GC API. fn goops_weak(value Value) GoopsWeakValue { if lifetime := arena.lifetime_token(value.pointer()) { return GoopsWeakValue{ value: value, lifetime: lifetime, storage: .arena } } $if gcboehm_leak ? { // Leak-finding deliberately keeps ordinary tracing semantics. The weak // service is unavailable in this mode; never pretend this is collector weakness. return GoopsWeakValue{ value: value } } $else { collector := unsafe { gc_weak_reference(value.pointer()) } or { // A missing registry edge could bypass ownership or redefinition checks. // Allocation/registration failure cannot silently publish a partial registry. panic('cannot create GOOPS collector weak reference') } return GoopsWeakValue{ value: Value{ ...value, bits: ValueBits{ payload: unsafe { nil } } } collector: collector storage: .collector } } } @[inline] fn (reference &GoopsWeakValue) get() ?Value { match reference.storage { .tracing { return reference.value } .arena { if !reference.lifetime.matches(reference.value.pointer()) { return none } return reference.value } .collector { // Strengthen once under the collector lock and hold the resulting // ordinary Value through use. Stored metadata contains no target pointer. target := reference.collector.get() if isnil(target) { return none } return Value{ ...reference.value, bits: ValueBits{ payload: target } } } } } // Recycle expired ordinary-heap registry nodes too. Arena-owned targets in the // VM's own region cannot expire independently, so they keep the existing fast path. @[inline] fn (mut vm VM) goops_note_weak_reference(reference &GoopsWeakValue) { if v_arena_current() == vm.allocation_owner && reference.storage != .collector { return } vm.goops.temporary_references++ if vm.goops.temporary_references >= 256 { vm.goops_prune_references() } }