From 77d240a7a20aec527352f29b959b8faa6cf899e3 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Mon, 9 Nov 2026 19:13:03 +0000 Subject: [PATCH] JsonValue.set and push return this: chains from a new value under -mm=own Co-Authored-By: Claude Opus 5.5 --- README.md | 6 +++--- src/lib.d.ts | 6 ++---- src/lib.ts | 7 +++---- tests/json.ts | 33 ++++++++++++++++----------------- 4 files changed, 24 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 3d72dc5..948d020 100644 --- a/README.md +++ b/README.md @@ -241,9 +241,9 @@ Generics (`src/generics/lib.generics.ts`): fields. An object literal or a tuple is a `TypeError`, a `BigInt` too. `space` is a number or a string; a replacer is not supported, only `null`. It takes all of these under every memory model; under `-mm=own` it writes out a copy of a `JsonValue` - it is given (`clone()`), since the value is only lent to it. Under `-mm=own` a `JsonValue` is built - one step at a time (`const o = JsonValue.newObject(); o.set("a", ...);`), not with chained `set`s or - `push`es: their result borrows the value they were called on. + it is given (`clone()`), since the value is only lent to it. `set` and `push` return `this`, so a + chain from a new value (`JsonValue.newObject().set("a", ...)`) can be returned or kept under every + model. - `WeakMap`: `get`, `set`, `has`, `delete`. `WeakSet`: `add`, `has`, `delete`. Keys are objects, compared by identity. `-mm=gc` only, as `WeakRef` is. A value is held strongly, so a value that refers to its own key keeps that key alive. diff --git a/src/lib.d.ts b/src/lib.d.ts index 8fe5702..810dc11 100644 --- a/src/lib.d.ts +++ b/src/lib.d.ts @@ -721,15 +721,13 @@ declare class JsonValue { get length(): int; @lends("this") at(position: int): JsonValue; - @lends("this") @keeps("value") - push(value: JsonValue): JsonValue; + push(value: JsonValue): this; has(key: string): boolean; @lends("this") get(key: string): JsonValue; - @lends("this") @keeps("value") - set(key: string, value: JsonValue): JsonValue; + set(key: string, value: JsonValue): this; clone(): JsonValue; keys(): string[]; stringify(space?: string): string; diff --git a/src/lib.ts b/src/lib.ts index 829424c..f86fc88 100644 --- a/src/lib.ts +++ b/src/lib.ts @@ -2341,9 +2341,9 @@ export class JsonValue { return this.items[position]; } - @lends("this") + // `this`: the object itself, so a chain from a new value is the caller's (own spec 78) @keeps("value") - public push(value: JsonValue): JsonValue { + public push(value: JsonValue): this { this.expect(JSON_ARRAY, "array"); this.items.push(value); return this; @@ -2367,9 +2367,8 @@ export class JsonValue { } // A key already there keeps its place and takes the new value, as in JavaScript. - @lends("this") @keeps("value") - public set(key: string, value: JsonValue): JsonValue { + public set(key: string, value: JsonValue): this { this.expect(JSON_OBJECT, "object"); let position = this.indexOf(key); if (position < 0) { diff --git a/tests/json.ts b/tests/json.ts index 6f1d616..e527b43 100644 --- a/tests/json.ts +++ b/tests/json.ts @@ -1,7 +1,6 @@ // JSON.parse gives a JsonValue, read through its accessors; JSON.stringify takes numbers, strings, // booleans, null, arrays of those, JsonValues and classes with toJSON(). Values are built one step at -// a time, which works under every memory model; chained set() and push() are tested where they -// compile, every model but -mm=own (see chained()). +// a time, which works under every memory model; chained set() and push() too (see chained()). class Point { constructor(public x: number) { } @@ -138,22 +137,22 @@ function writing() { assert(JSON.stringify([1], null, 0) == "[1]", "indented by nothing"); } -// Under -mm=own the result of set() and push() borrows the value they were called on, so a chain that -// starts from a new value (JsonValue.newObject().set(...)) can't be kept: the compiler has no way yet -// to say that a method gives its receiver back. Only the branch for the model built is compiled. +// set() and push() return `this`, the object itself: a chain from a new value is the caller's under +// every model, returned or given to a parameter that keeps it. +function person(): JsonValue { + return JsonValue.newObject().set("name", JsonValue.ofString("Ann")).set("age", JsonValue.ofNumber(30)); +} + function chained() { - if (__MEMORY_MODEL__ != "own") { - const person = JsonValue.newObject().set("name", JsonValue.ofString("Ann")).set("age", JsonValue.ofNumber(30)); - assert(JSON.stringify(person) == `{"name":"Ann","age":30}`, "a chain of set()"); - - const built = JsonValue.newObject() - .set("n", JsonValue.ofNumber(1)) - .set("list", JsonValue.newArray().push(JsonValue.ofString("x")).push(new JsonValue())) - .set("empty", JsonValue.newObject()); - assert(JSON.stringify(built) == `{"n":1,"list":["x",null],"empty":{}}`, "a chained build"); - built.set("n", JsonValue.ofBoolean(true)).set("added", JsonValue.ofString("y")); - assert(JSON.stringify(built) == `{"n":true,"list":["x",null],"empty":{},"added":"y"}`, "set again, chained"); - } + assert(JSON.stringify(person()) == `{"name":"Ann","age":30}`, "a chain of set() returned"); + + const built = JsonValue.newObject() + .set("n", JsonValue.ofNumber(1)) + .set("list", JsonValue.newArray().push(JsonValue.ofString("x")).push(new JsonValue())) + .set("empty", JsonValue.newObject()); + assert(JSON.stringify(built) == `{"n":1,"list":["x",null],"empty":{}}`, "a chained build"); + built.set("n", JsonValue.ofBoolean(true)).set("added", JsonValue.ofString("y")); + assert(JSON.stringify(built) == `{"n":true,"list":["x",null],"empty":{},"added":"y"}`, "set again, chained"); } function roundTrip() {