Repository navigation
WeakRef: the target is cleared once it has gone; weakref_basic asserts it - #17
Merged
Merged
Conversation
…s it WeakRef kept its target in a plain pointer field. The collector finds that pointer when it scans the WeakRef, so the target stayed reachable and the disappearing link was never cleared: after the last reference went and GC_gcollect() ran, deref() still gave the object. gc.h asks for a disguised pointer in the link location; the field now holds the target's address complemented (GC_HIDE_POINTER), which the collector sets to 0 when the target goes. weakref_basic only printed its results and checked for "ALL DONE", so it passed with the link never cleared. It now asserts: the object before a collection, undefined after it, and that a held object survives. The deref of a live object is in a function of its own, so no pointer to it is left in main's frame for the conservative stack scan. WeakRef and this test are gc only - under rc, none and own the compiler now rejects the GC_* calls (TypeScriptCompiler: "A call to the collector's API outside -mm=gc is a compile error"). Both say so in a comment. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Under gc,
WeakRefnever let go of its target. After the last reference was gone andGC_gcollect()had run,deref()still returned the object.The target sat in a plain pointer field. The collector finds that pointer when it scans the
WeakRef, so the target stayed reachable and the weak link was never cleared. gc.h says the link location should hold "a disguised pointer to obj". The field now holds the target's address complemented, which is whatGC_HIDE_POINTERdoes. The collector sets it to 0 when the target goes.Tests
weakref_basiconly printed its results and checked for "ALL DONE", so it passed even though the link was never cleared. It now asserts three things:deref()returns the object before a collection;undefinedafter the collection;The
deref()of a live object runs in a function of its own, so no pointer to it stays inmain's frame for the collector's conservative stack scan to find.It passed every run under gc: the JIT at -O0 and -O3, and 10 of 10 compiled runs at each level.
gc only
WeakRefand this test are gc-only for now, and both say so in a comment. Under rc, none and own the compiler now rejects theGC_*calls with a clear error (ASDAlexander77/TypeScriptCompiler#420). Real rc weak references remain later work.Results
weakref_basicskipped as gc-only🤖 Generated with Claude Code