From 670a68071266b83c157a25c75ba1965af328e805 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Mon, 26 Oct 2026 23:43:16 +0000 Subject: [PATCH] Map and Set: @sharedElements; under -mm=own objects go in as Shared - @sharedElements on Map and Set: under -mm=own a key, value or element has to be a value, a string or a Shared handle; a plain object or `any` is a compile error at the user's code that points at Shared. Needs the compiler's @sharedElements check; the other memory models ignore it. - tests.ps1/tests.sh: a test whose first line starts with "// not own" is skipped under -mm=own; the untyped (Map) map_* tests say so. - tests/map_shared.ts: Map and Set of Shared - overwrite, get, entries, values, break, delete, union, intersection; counts given back under rc and own. Co-Authored-By: Claude Opus 5.5 --- src/generics/lib.generics.ts | 4 +++ tests.ps1 | 16 +++++++++- tests.sh | 15 ++++++++- tests/map_any.ts | 1 + tests/map_clear.ts | 1 + tests/map_entries.ts | 1 + tests/map_forEach.ts | 1 + tests/map_iterator.ts | 1 + tests/map_keys.ts | 1 + tests/map_shared.ts | 59 ++++++++++++++++++++++++++++++++++++ tests/map_size.ts | 1 + tests/map_values.ts | 1 + 12 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 tests/map_shared.ts diff --git a/src/generics/lib.generics.ts b/src/generics/lib.generics.ts index fa8eff0..8308366 100644 --- a/src/generics/lib.generics.ts +++ b/src/generics/lib.generics.ts @@ -727,6 +727,8 @@ type MapEntry = value: TValue; // Value of entry }; +// -mm=own: keys and values are values, strings or Shared handles; an object goes in as Shared +@sharedElements class Map { const StartOfFreeList = -3; @@ -1091,6 +1093,8 @@ type SetEntry = value: TValue; // Value of entry }; +// -mm=own: elements are values, strings or Shared handles; an object goes in as Shared +@sharedElements class Set { const StartOfFreeList = -3; diff --git a/tests.ps1 b/tests.ps1 index 9dae9af..bd72857 100644 --- a/tests.ps1 +++ b/tests.ps1 @@ -1,7 +1,8 @@ # Runs every test in .\tests, release and debug, compiled and under the JIT. # .\tests.ps1 the compiler's default memory model (gc) # .\tests.ps1 -Model rc -mm=rc, against the default library built for it; also $Env:TSLANG_MM -# A test whose first line starts with "// gc only" is skipped under any other model. +# A test whose first line starts with "// gc only" is skipped under any other model, and one whose +# first line starts with "// not own" under -mm=own. param([string]$Model = $Env:TSLANG_MM) # The library paths the environment gives when the script starts. One it does not give is worked @@ -132,6 +133,13 @@ function Test-GcOnly([string]$path) return (Get-Content $path -TotalCount 1) -match '^// gc only' } +# A test of what -mm=own refuses by design says so on its first line ("// not own: ..."), e.g. a +# Map of `any`: under own an object goes into a Map or Set as Shared. +function Test-NotOwn([string]$path) +{ + return (Get-Content $path -TotalCount 1) -match '^// not own' +} + function Tests([string]$config, [string]$mode) { Write-Host "Testing..." @@ -154,6 +162,12 @@ function Tests([string]$config, [string]$mode) return } + if ($Model -eq "own" -and (Test-NotOwn $_.FullName)) { + $skipped++ + Write-Host "Skipped (not own)" -ForegroundColor Yellow + return + } + $time = (Measure-Command { $result = Test $config $mode $_.Basename }).TotalSeconds $time = [math]::Round($time, 2).ToString("0.00") diff --git a/tests.sh b/tests.sh index c98cd0e..f7d4e54 100755 --- a/tests.sh +++ b/tests.sh @@ -2,7 +2,8 @@ # Runs every test in ./tests, release and debug, compiled and under the JIT. # ./tests.sh the compiler's default memory model (gc) # ./tests.sh rc -mm=rc, against the default library built for it; also TSLANG_MM=rc -# A test whose first line starts with "// gc only" is skipped under any other model. +# A test whose first line starts with "// gc only" is skipped under any other model, and one whose +# first line starts with "// not own" under -mm=own. MODEL="${1:-$TSLANG_MM}" # The library paths the environment gives when the script starts. One it does not give is worked @@ -94,6 +95,12 @@ function gc_only() { head -n 1 "$1" | grep -q '^// gc only' } +# A test of what -mm=own refuses by design says so on its first line ("// not own: ..."), e.g. a +# Map of `any`: under own an object goes into a Map or Set as Shared. +function not_own() { + head -n 1 "$1" | grep -q '^// not own' +} + function tests() { config="$1" mode="$2" @@ -115,6 +122,12 @@ function tests() { continue fi + if [ "$MODEL" = "own" ] && not_own "$file"; then + skipped=$((skipped + 1)) + printf "\e[33mSkipped\e[0m (not own)\n" + continue + fi + start=$(date +%s.%N) test_script "$config" "$mode" "$(basename "$file" .ts)" result=$? diff --git a/tests/map_any.ts b/tests/map_any.ts index 1a3a46b..cf84aae 100644 --- a/tests/map_any.ts +++ b/tests/map_any.ts @@ -1,3 +1,4 @@ +// not own: an untyped Map is Map, which -mm=own refuses (use typed keys and values) const m = new Map(); m.set(1, "Hello"); diff --git a/tests/map_clear.ts b/tests/map_clear.ts index a50b998..dde4f04 100644 --- a/tests/map_clear.ts +++ b/tests/map_clear.ts @@ -1,3 +1,4 @@ +// not own: an untyped Map is Map, which -mm=own refuses (use typed keys and values) const map1 = new Map(); map1.set('bar', 'baz'); diff --git a/tests/map_entries.ts b/tests/map_entries.ts index 874e9d4..c19daae 100644 --- a/tests/map_entries.ts +++ b/tests/map_entries.ts @@ -1,3 +1,4 @@ +// not own: an untyped Map is Map, which -mm=own refuses (use typed keys and values) const map1 = new Map(); map1.set('0', 'foo'); diff --git a/tests/map_forEach.ts b/tests/map_forEach.ts index d4ee4e2..996d5d8 100644 --- a/tests/map_forEach.ts +++ b/tests/map_forEach.ts @@ -1,3 +1,4 @@ +// not own: an untyped Map is Map, which -mm=own refuses (use typed keys and values) new Map([ ['foo', 3], ['bar', "test"], diff --git a/tests/map_iterator.ts b/tests/map_iterator.ts index 0d7c071..c525e95 100644 --- a/tests/map_iterator.ts +++ b/tests/map_iterator.ts @@ -1,3 +1,4 @@ +// not own: an untyped Map is Map, which -mm=own refuses (use typed keys and values) const map1 = new Map(); map1.set('0', 'foo'); diff --git a/tests/map_keys.ts b/tests/map_keys.ts index b659ac0..ab0670a 100644 --- a/tests/map_keys.ts +++ b/tests/map_keys.ts @@ -1,3 +1,4 @@ +// not own: an untyped Map is Map, which -mm=own refuses (use typed keys and values) const map1 = new Map(); map1.set('0', 'foo'); diff --git a/tests/map_shared.ts b/tests/map_shared.ts new file mode 100644 index 0000000..0f7ad00 --- /dev/null +++ b/tests/map_shared.ts @@ -0,0 +1,59 @@ +// Objects in a Map or a Set go through Shared: under -mm=own a plain object or `any` element is +// a compile error. Under rc and own Shared.count counts: the containers give every count back. +class Node { + constructor(public n: number) {} +} + +function counted() { + return __MEMORY_MODEL__ === "rc" || __MEMORY_MODEL__ === "own"; +} + +function fill(x: Shared, y: Shared) { + const m = new Map>(); + m.set("a", x); + m.set("b", x); + m.set("a", y); + const got = m.get("b")!; + assert(got.value.n == 7); + + let total = 0; + for (const [k, v] of m) total += v.value.n; + assert(total == 15); + + total = 0; + for (const v of m.values()) total += v.value.n; + assert(total == 15); + + for (const v of m.values()) { + if (v.value.n == 8) break; + } + + assert(m.delete("a")); + assert(m.size == 1); +} + +function sets(x: Shared, y: Shared) { + const a = new Set>(); + a.add(x); + a.add(y); + const b = new Set>(); + b.add(x); + const u = a.union(b); + assert(u.size == 2); + assert(a.intersection(b).size == 1); + + let total = 0; + for (const v of u) total += v.value.n; + assert(total == 15); +} + +const x = new Shared(new Node(7)); +const y = new Shared(new Node(8)); + +fill(x, y); +assert(!counted() || (Shared.count(x) == 1 && Shared.count(y) == 1)); + +sets(x, y); +assert(!counted() || (Shared.count(x) == 1 && Shared.count(y) == 1)); + +console.log("ALL DONE"); diff --git a/tests/map_size.ts b/tests/map_size.ts index bcec267..9863a39 100644 --- a/tests/map_size.ts +++ b/tests/map_size.ts @@ -1,3 +1,4 @@ +// not own: an untyped Map is Map, which -mm=own refuses (use typed keys and values) const map1 = new Map(); map1.set('a', 'alpha'); diff --git a/tests/map_values.ts b/tests/map_values.ts index 2e707f8..3751115 100644 --- a/tests/map_values.ts +++ b/tests/map_values.ts @@ -1,3 +1,4 @@ +// not own: an untyped Map is Map, which -mm=own refuses (use typed keys and values) const map1 = new Map(); map1.set('0', 'foo');