Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,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<K, V>`: `get`, `set`, `has`, `delete`. `WeakSet<K>`: `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.
Expand Down
6 changes: 2 additions & 4 deletions src/lib.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -721,16 +721,14 @@ declare class JsonValue {
get length(): int;
@lends("this")
at(position: int): JsonValue;
@lends("this")
@keeps("value")
push(value: JsonValue): JsonValue;
push(value: JsonValue): this;
[key: number | string]: JsonValue;
has(key: string): boolean;
@lends("this")
get(key: number | string): JsonValue;
@lends("this")
@keeps("value")
set(key: number | string, value: JsonValue): JsonValue;
set(key: number | string, value: JsonValue): this;
clone(): JsonValue;
keys(): string[];
stringify(space?: string): string;
Expand Down
8 changes: 4 additions & 4 deletions src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -2383,9 +2383,9 @@ export class JsonValue {
// An object's member (a string key) or an array's element (a number). A key already there keeps
// its place and takes the new value, and an element past an array's end leaves JSON nulls before
// it, as in JavaScript.
@lends("this")
// `this`: the object itself, so a chain from a new value is the caller's (own spec 78)
@keeps("value")
public set(key: number | string, value: JsonValue): JsonValue {
public set(key: number | string, value: JsonValue): this {
let position = -1;
if (typeof key == "number") {
this.expect(JSON_ARRAY, "array");
Expand Down
33 changes: 16 additions & 17 deletions tests/json.ts
Original file line number Diff line number Diff line change
@@ -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) {
}
Expand Down Expand Up @@ -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() {
Expand Down
Loading