open Mocha open Test_utils @module("mocha") external testAsync: (string, unit => promise) => unit = "test" /*** After `let {x: _, ...rest} = r`, later reads of `r.x` must see the current value of `r.x`, whatever wrote it after the destructuring. A name the pattern binds keeps the value at the destructuring. */ type mutableXy = {mutable x: int, y: int} type xy = {x: int, y: int} type onlyY = {y: int} let writeAfterRest = (a: mutableXy) => { let b = (a :> xy) let {x: _, ...onlyY as rest} = b a.x = 42 (rest, a) } let readAfterWrite = (a: mutableXy) => { let b = (a :> xy) let {x: _, ...onlyY as rest} = b a.x = 42 (rest, b.x) } type counter = {name: string, bump: unit => unit, other: int} type onlyOther = {other: int} let makeCounter: unit => counter = %raw(`() => ({name: "old", bump() { this.name = "new" }, other: 1})`) let methodCallThenRead = (c: counter) => { let {name: _, bump: _, ...onlyOther as rest} = c c.bump() (rest, c.name) } @inline(never) let bump = (a: mutableXy) => a.x = 42 let calleeThenRead = (a: mutableXy) => { let b = (a :> xy) let {x: _, ...onlyY as rest} = b bump(a) (rest, b.x) } let aliasThenRead = (a: mutableXy, b: xy) => { let {x: _, ...onlyY as rest} = b a.x = 42 (rest, b.x) } let awaitThenRead = async (a: mutableXy, p: promise) => { let b = (a :> xy) let {x: _, ...onlyY as rest} = b await p (rest, b.x) } let closureThenRead = (a: mutableXy, flag) => { let b = (a :> xy) let (get, rest) = if flag { let {x: _, ...onlyY as rest} = b (() => b.x, rest) } else { (() => 0, {y: 0}) } a.x = 42 (rest, get()) } let viewSource: mutableXy = {x: 1, y: 2} let view = (viewSource :> xy) let {x: _, ...onlyY as viewRest} = view let getViewX = () => view.x @set_index external setIndex: (xy, string, int) => unit = "" let computedWriteThenRead = (b: xy) => { let {x: _, ...onlyY as rest} = b setIndex(b, "x", 42) (rest, b.x) } let boundThenAwait = async (c: counter, p: promise) => { let {name, bump: _, ...onlyOther as rest} = c await p (rest, name) } let boundThenCall = (c: counter, f: unit => unit) => { let {name, bump: _, ...onlyOther as rest} = c f() (rest, name, c.name) } let boundThenWrite = (a: mutableXy) => { let {x, ...onlyY as rest} = (a :> xy) a.x = 42 (rest, x, a.x) } let boundWithoutRest = (c: counter, f: unit => unit) => { let {name, bump: _, ...onlyOther as _rest} = c f() name } let boundInCase = (c: option, f: unit => unit) => switch c { | Some({name, bump: _, ...onlyOther as rest}) => f() Some(rest, name) | None => None } let boundAfterTest = (c: counter, f: unit => unit) => switch c { | {name: "none", bump: _, ...onlyOther as rest} => (rest, "nothing") | {name, bump: _, ...onlyOther as rest} => f() (rest, name) } type abc = {a: int, b: int, c: int} type onlyC = {c: int} // A field bound next to a rest is read with the rest, after the test of a // field written after it let boundBeforeTest = (r: abc) => switch r { | {a, b: 0, ...onlyC as rest} => Some((a, rest)) | _ => None } let testedFieldWrittenByGetter: unit => (abc, unit => int) = %raw(`() => { let reads = 0; return [ { get a() { reads = reads + 1; this.b = 0; return 1; }, b: 5, c: 3 }, () => reads, ]; }`) type tagged = {tag: int, a: int, b: int} type onlyB = {b: int} // A case without a rest reads its fields where it uses them let restInOneCase = (r: tagged) => switch r { | {tag: 1, a, b} => a + b | {tag: _, a, ...onlyB as rest} => a - rest.b } let callBound = (c: counter) => { let {name: _, bump, ...onlyOther as rest} = c bump() rest } let callSourceAndBound = ({name, bump, ...onlyOther as rest} as c: counter) => { c.bump() bump() (rest, name, c.name) } type inner = {i: int, j: int} type outer = {inner: inner, z: int} type onlyZ = {z: int} type onlyJ = {j: int} let boundFieldOfField = (o: outer, f: unit => unit) => { let {inner: {i}, ...onlyZ as rest} = o f() (rest, i) } let nestedRests = (o: outer, f: unit => unit) => { let {inner: {i, ...onlyJ as innerRest}, ...onlyZ as rest} = o f() (rest, innerRest, i) } let restsInTuple = ( ({x: a, ...onlyY as rest1}, {x: b, ...onlyY as rest2}): (xy, xy), f: unit => unit, ) => { f() (a, rest1, b, rest2) } let explicitAndBound = (c: counter) => { let n = c.name let {name, bump: _, ...onlyOther as rest} = c c.bump() (n, name, rest, n, name) } /* A field next to a rest that binds no variable is only read where it is tested: it gets no local, and a wildcard no read besides the destructuring's. */ let constantField = (a: xy) => switch a { | {x: 1, ...onlyY as rest} => Some(rest) | _ => None } let wildcardField = ({x: _, ...onlyY as rest}: xy) => rest // `x` and `y` stay reads before the rest, because moving `x` into the // destructuring would read it after `y` let readsBeforeRest = (a: xy) => { let x = a.x let y = a.y let {x: _, ...onlyY as rest} = a (x, y, rest, x, y) } type triple = {first: int, second: int, third: int} type onlyThird = {third: int} // `second` is read before `first`, though the rest lists `first` first let reversedReadsBeforeRest = (t: triple) => { let second = t.second let first = t.first let {first: _, second: _, ...onlyThird as rest} = t (first, second, rest, first, second) } // `second` is read before `first`, which only the rest reads let laterReadBeforeRest = (t: triple) => { let second = t.second let {first: _, second: _, ...onlyThird as rest} = t (second, rest, second) } type optionalName = {maybeName: option, other: int} let constructorField = (o: optionalName) => switch o { | {maybeName: Some(_), ...onlyOther as rest} => ("named", rest) | {maybeName: None, ...onlyOther as rest} => ("unnamed", rest) } // An alias over a tested or destructured field shares the read that was matched type onlyBC = {b: int, c: int} let aliasConstField = (r: abc) => switch r { | {a: 1 as x, ...onlyBC as rest} => Some((x, rest)) | _ => None } let aliasOptionField = (o: optionalName) => switch o { | {maybeName: Some(v) as x, ...onlyOther as rest} => Some((v, x, rest)) | _ => None } let aliasNestedRecord = (o: outer) => switch o { | {inner: {i, ...onlyJ as innerRest} as inner, ...onlyZ as rest} => (i, innerRest, inner, rest) } // A chain of aliases around a variable or wildcard is bound once, with the rest, // after tests of later fields let nestedAliasField = (r: abc) => switch r { | {a: x as y, b: 0, ...onlyC as rest} => Some((x, y, rest)) | _ => None } let nestedWildcardAliasField = (r: abc) => switch r { | {a: (_ as x) as y, b: 0, ...onlyC as rest} => Some((x, y, rest)) | _ => None } let countingAbc: int => (abc, unit => int) = %raw(`(b) => { let reads = 0; return [ { get a() { reads = reads + 1; return reads; }, b, c: 3 }, () => reads, ]; }`) let changingOptionName: unit => optionalName = %raw(`() => { let reads = 0; return { get maybeName() { reads = reads + 1; return reads === 1 ? "a" : undefined; }, other: 1 }; }`) let changingInnerOuter: unit => outer = %raw(`() => { let reads = 0; return { get inner() { reads = reads + 1; return reads === 1 ? { i: 1, j: 2 } : { i: 99, j: 99 }; }, z: 3, }; }`) @module("node:assert") external deepStrictEqual: ('a, 'a) => unit = "deepStrictEqual" // the number of reads of a getter named `name` let countingCounter: unit => (counter, unit => int) = %raw(`() => { let reads = 0; return [ { get name() { reads = reads + 1; return "read" + reads; }, bump() {}, other: 1 }, () => reads, ]; }`) // a getter named `z` that writes `o.inner.j` let innerWrittenByGetter: unit => outer = %raw(`() => { const o = { inner: { i: 1, j: 2 } }; Object.defineProperty(o, "z", { enumerable: true, get() { o.inner.j = 42; return 3; }, }); return o; }`) let nestedGetter: unit => (outer, unit => int) = %raw(`() => { let reads = 0; return [ { get inner() { reads = reads + 1; return { i: reads, j: 2 }; }, z: 3 }, () => reads, ]; }`) // a `bump` that returns its `this` through `seen` let thisCounter: unit => (counter, unit => bool) = %raw(`() => { let seen = "not called"; return [ { name: "old", bump() { seen = this; }, other: 1 }, () => seen === undefined, ]; }`) // the number of reads of a getter named `x` let countingXy: int => (xy, unit => int) = %raw(`(x) => { let reads = 0; return [ { get x() { reads = reads + 1; return x; }, y: 2 }, () => reads, ]; }`) // a getter named `y` that returns the number of its reads let countingY: unit => xy = %raw(`() => { let reads = 0; return { x: 1, get y() { reads = reads + 1; return reads; } }; }`) // getters `first` and `second` that return the number of reads of either let sharedCountingTriple: unit => triple = %raw(`() => { let reads = 0; return { get first() { reads = reads + 1; return reads; }, get second() { reads = reads + 1; return reads; }, third: 3, }; }`) let setInner: (outer, inner) => unit = %raw(`(o, inner) => { o.inner = inner; }`) let setName: (counter, string) => unit = %raw(`(c, name) => { c.name = name; }`) describe(__MODULE__, () => { test("a read of another field before the rest", () => { let (x, y, rest, _, _) = readsBeforeRest(countingY()) deepStrictEqual((x, y, rest), (1, 1, {y: 2})) }) test("reads of fields in another order than the rest's", () => { let (first, second, rest, _, _) = reversedReadsBeforeRest(sharedCountingTriple()) deepStrictEqual((first, second, rest), (2, 1, {third: 3})) let (second, rest, _) = laterReadBeforeRest(sharedCountingTriple()) deepStrictEqual((second, rest), (1, {third: 3})) }) test("a write after the rest", () => { let (rest, a) = writeAfterRest({x: 1, y: 2}) deepStrictEqual((rest, a.x), ({y: 2}, 42)) }) test("a read through the source after a write", () => { let (rest, x) = readAfterWrite({x: 1, y: 2}) deepStrictEqual((rest, x), ({y: 2}, 42)) }) test("a method call that writes the source", () => { let (rest, name) = methodCallThenRead(makeCounter()) deepStrictEqual((rest, name), ({other: 1}, "new")) }) test("a callee that writes the source", () => { let (rest, x) = calleeThenRead({x: 1, y: 2}) deepStrictEqual((rest, x), ({y: 2}, 42)) }) test("a write through an alias of the source", () => { let a: mutableXy = {x: 1, y: 2} let (rest, x) = aliasThenRead(a, a :> xy) deepStrictEqual((rest, x), ({y: 2}, 42)) }) testAsync("a write during an await", async () => { let a: mutableXy = {x: 1, y: 2} let p = Promise.resolve()->Promise.thenResolve(() => a.x = 42) let (rest, x) = await awaitThenRead(a, p) deepStrictEqual((rest, x), ({y: 2}, 42)) }) test("a closure that outlives the rest", () => { let (rest, x) = closureThenRead({x: 1, y: 2}, true) deepStrictEqual((rest, x), ({y: 2}, 42)) }) test("exported top-level state written later", () => { viewSource.x = 42 deepStrictEqual((viewRest, getViewX()), ({y: 2}, 42)) }) test("a computed-member write", () => { let (rest, x) = computedWriteThenRead({x: 1, y: 2}) deepStrictEqual((rest, x), ({y: 2}, 42)) }) testAsync("a bound field after an await", async () => { let c = makeCounter() let p = Promise.resolve()->Promise.thenResolve(() => setName(c, "new")) let (rest, name) = await boundThenAwait(c, p) deepStrictEqual((rest, name), ({other: 1}, "old")) }) test("a bound field after a call", () => { let c = makeCounter() let result = boundThenCall(c, () => setName(c, "new")) deepStrictEqual(result, ({other: 1}, "old", "new")) }) test("a bound field after a write", () => { deepStrictEqual(boundThenWrite({x: 1, y: 2}), ({y: 2}, 1, 42)) }) test("a bound field next to an unused rest", () => { let c = makeCounter() eq(__LOC__, boundWithoutRest(c, () => setName(c, "new")), "old") }) test("a bound field in a switch case", () => { let c = makeCounter() let result = boundInCase(Some(c), () => setName(c, "new")) deepStrictEqual(result, Some({other: 1}, "old")) }) test("a bound field that an earlier case tests", () => { let c = makeCounter() let result = boundAfterTest(c, () => setName(c, "new")) deepStrictEqual(result, ({other: 1}, "old")) }) test("a field bound next to a rest is read after the test of a later field", () => { deepStrictEqual(boundBeforeTest({a: 1, b: 0, c: 3}), Some((1, {c: 3}))) let (r, reads) = testedFieldWrittenByGetter() deepStrictEqual((boundBeforeTest(r), reads()), (None, 0)) }) test("a case without a rest next to one with a rest", () => { deepStrictEqual( (restInOneCase({tag: 1, a: 2, b: 3}), restInOneCase({tag: 0, a: 5, b: 1})), (5, 4), ) }) test("a call of a bound function has no this", () => { let (c, calledWithoutThis) = thisCounter() let rest = callBound(c) deepStrictEqual((rest, calledWithoutThis()), ({other: 1}, true)) }) test("a method call on the source next to a call of a bound function", () => { let (c, calledWithoutThis) = thisCounter() let result = callSourceAndBound(c) deepStrictEqual((result, calledWithoutThis()), (({other: 1}, "old", "old"), true)) }) test("a field of a field", () => { let o = {inner: {i: 1, j: 2}, z: 3} let result = boundFieldOfField(o, () => setInner(o, {i: 42, j: 42})) deepStrictEqual(result, ({z: 3}, 1)) }) test("nested rests", () => { let o = {inner: {i: 1, j: 2}, z: 3} let result = nestedRests(o, () => setInner(o, {i: 42, j: 42})) deepStrictEqual(result, ({z: 3}, {j: 2}, 1)) }) test("rests in a tuple", () => { let a: mutableXy = {x: 1, y: 2} let result = restsInTuple((a :> xy, {x: 3, y: 4}), () => a.x = 42) deepStrictEqual(result, (1, {y: 2}, 3, {y: 4})) }) test("an explicit read next to a bound field", () => { deepStrictEqual(explicitAndBound(makeCounter()), ("old", "old", {other: 1}, "old", "old")) }) test("a bound field is read once", () => { let (c, reads) = countingCounter() let (rest, name) = boundThenCall(c, () => ())->(((rest, name, _)) => (rest, name)) // the second read is the explicit one of the source deepStrictEqual((rest, name, reads()), ({other: 1}, "read1", 2)) // the inner record is copied before the outer one, which reads `inner` again let (o, reads) = nestedGetter() let result = nestedRests(o, () => ()) deepStrictEqual((result, reads()), (({z: 3}, {j: 2}, 1), 2)) }) test("nested rests are copied inner first", () => { let result = nestedRests(innerWrittenByGetter(), () => ()) deepStrictEqual(result, ({z: 3}, {j: 2}, 1)) }) test("a constant next to a rest", () => { deepStrictEqual(constantField({x: 1, y: 2}), Some({y: 2})) deepStrictEqual(constantField({x: 3, y: 2}), None) // a field that fails its test is read for the test only let (a, reads) = countingXy(3) deepStrictEqual((constantField(a), reads()), (None, 1)) }) test("a wildcard next to a rest", () => { // the one read leaves the field out of the rest let (a, reads) = countingXy(1) deepStrictEqual((wildcardField(a), reads()), ({y: 2}, 1)) }) test("a field exported without the rest next to it", () => { deepStrictEqual((Record_rest_hidden_rest.x, Record_rest_hidden_rest.sumTo(3)), (1, 6)) }) test("a constructor without variables next to a rest", () => { deepStrictEqual(constructorField({maybeName: Some("a"), other: 1}), ("named", {other: 1})) deepStrictEqual(constructorField({maybeName: None, other: 1}), ("unnamed", {other: 1})) }) test("an alias over a tested or destructured field next to a rest", () => { let (r, _reads) = countingAbc(2) deepStrictEqual(aliasConstField(r), Some((1, {b: 2, c: 3}))) deepStrictEqual(aliasOptionField(changingOptionName()), Some(("a", Some("a"), {other: 1}))) deepStrictEqual(aliasNestedRecord(changingInnerOuter()), (1, {j: 2}, {i: 1, j: 2}, {z: 3})) }) test("nested aliases on a field next to a rest", () => { let (r, reads) = testedFieldWrittenByGetter() deepStrictEqual((nestedAliasField(r), reads()), (None, 0)) let (r, reads) = countingAbc(0) deepStrictEqual((nestedAliasField(r), reads()), (Some((1, 1, {c: 3})), 1)) let (r, reads) = testedFieldWrittenByGetter() deepStrictEqual((nestedWildcardAliasField(r), reads()), (None, 0)) let (r, reads) = countingAbc(0) deepStrictEqual((nestedWildcardAliasField(r), reads()), (Some((1, 1, {c: 3})), 1)) }) })