You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
That wouldn't cover weak references, just finalization.
In order to correctly implement weak references in go-lua, the Lua GC (or some variation of it) would have to be implemented. Right now, go-lua relies on the Go GC, which does not support weak references, to manage memory.
Going back to finalizers, they're something that could be supported if the GC were implemented (via a metatable with a gc metamethod on userdata, same as Lua 5.2). In any case, it wouldn't make sense to use Go GC finalizers (and using them should be mostly discouraged).
func NewWeakRef(v interface{}) WeakRef {
i := ([2]uintptr)(unsafe.Pointer(&v))
w := &WeakRef{^i[0], ^i[1]}
runtime.SetFinalizer((*uintptr)(unsafe.Pointer(i[1])), func(_ *uintptr) {
atomic.StoreUintptr(&w.d, 0)
w.t = 0
})
return w
}
func (w WeakRef) Get() (v interface{}) {
t := w.t
d := atomic.LoadUintptr(&w.d)
if d != 0 {
i := ([2]uintptr)(unsafe.Pointer(&v))
i[0] = ^t
i[1] = ^d
}
return
}
`
convert Go pointer to uintptr which not prevent Go object from being collected by GC. Here runtime.SetFinalizer I think only provide a chance to notify us "oh, it has been collected".
I heard that can use runtime.SetFinalizer function implement WEAK REF feature.
The text was updated successfully, but these errors were encountered: