Skip to content

Add haxe.ds.WeakRef and expand WeakMap target coverage#12765

Open
jdonaldson wants to merge 3 commits intoHaxeFoundation:developmentfrom
jdonaldson:weakref-weakmap
Open

Add haxe.ds.WeakRef and expand WeakMap target coverage#12765
jdonaldson wants to merge 3 commits intoHaxeFoundation:developmentfrom
jdonaldson:weakref-weakmap

Conversation

@jdonaldson
Copy link
Copy Markdown
Member

Summary

  • Add haxe.ds.WeakRef<T> — a cross-platform weak reference type with implementations for 6 targets
  • Add haxe.ds.WeakMap<K,V> implementations for Lua, Python, JS, and PHP (C++, JVM, Flash already existed)
  • Unsupported targets (HL, Neko, Eval) fall through to the default stub which throws NotImplementedException

WeakRef target coverage

Target Mechanism
Lua setmetatable({[1]=target}, {__mode='v'})
Python weakref.ref()
JS js.lib.WeakRef
C++ __hxcpp_weak_ref_create/get
JVM java.lang.ref.WeakReference
PHP \WeakReference::create() (PHP 7.4+)

New WeakMap implementations

Target Mechanism
Lua setmetatable({}, {__mode='k'}) — full IMap support
Python weakref.WeakKeyDictionary — full IMap support
JS js.lib.WeakMap — enumeration methods throw NotImplementedException (spec limitation)
PHP \WeakMap (PHP 8.0+) — full IMap support

Test plan

  • Lua unit tests pass (11726 assertions, 0 failures)
  • CI validates other targets
  • Tests cover: WeakRef.get(), identity preservation, WeakMap set/get/exists/remove/overwrite/clear, iteration (#if !js), JS enumeration throws (#if js)

Add WeakRef<T> for holding weak references across 6 targets:
- Lua: setmetatable with __mode='v'
- Python: weakref.ref()
- JS: js.lib.WeakRef
- C++: __hxcpp_weak_ref_create/get
- JVM: java.lang.ref.WeakReference
- PHP: \WeakReference (PHP 7.4+)

Add WeakMap<K,V> for Lua (__mode='k'), Python (WeakKeyDictionary),
JS (js.lib.WeakMap, enumeration throws), and PHP (\WeakMap, PHP 8.0+).
C++, JVM, and Flash implementations already existed.

Unsupported targets fall through to the default stub which throws
NotImplementedException.
Python WeakRef: use python.Syntax.code instead of @:selfCall which
generates broken .call() on Python target.

JVM WeakMap: fix NPE by initializing hash table arrays in constructor,
use explicit java.lang.Object casts for hashCode()/equals() instead of
untyped which fails JVM field resolution on inherited methods.

Tests: add target-conditional gates for platforms with/without WeakRef
and WeakMap support, add strong reference assertions to prevent compiler
from inlining away weak reference targets on refcount-GC platforms.
@jdonaldson
Copy link
Copy Markdown
Member Author

Rebased onto current development (picks up GcFinalizer merge, native bit ops, etc.) and added a couple of fixes:

  • Fixed cppia test gap: The noAssert() fallback guard used cpp which excluded cppia entirely, meaning zero test functions would run on cppia (empty class = CI warning). Changed to (cpp && !cppia) to match the other guards.
  • Added CHANGES.txt entries for WeakRef and WeakMap.

CI is green, all targets pass. Now that GcFinalizer (#12766) has landed, this is a natural companion — together they cover the core GC-aware primitives (WeakRef, WeakMap, GcFinalizer). Would be great to get some eyes on this.

noAssert() fallback guard used bare `cpp` which excluded cppia entirely,
meaning zero test functions would run on cppia (empty class = CI warning).
Changed to (cpp && !cppia) to match the other guards in the file.
@jdonaldson
Copy link
Copy Markdown
Member Author

Removed the changes to Changes.txt. Bad Claude!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant