Skip to content

Commit 1d31794

Browse files
committed
🐛 Retain reused component answers once
1 parent 6da61ba commit 1d31794

2 files changed

Lines changed: 65 additions & 7 deletions

File tree

‎packages/core/src/components/component-resolution.ts‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -589,12 +589,17 @@ export class CanonicalImports {
589589
// add: one import is one implementation, and this is that one again.
590590
return answer;
591591
}
592-
// Still the definition the identity was stated about. A claimed object the
593-
// provider went on to edit is a different implementation, and taking a
594-
// fresh copy of it here would record the edited one under the revision
595-
// that described the original. Asked before anything is recorded and
596-
// before the window is spent, so a refusal leaves both untouched.
597-
if (held.baseline === undefined || !stillDescribes(held.baseline, answer)) {
592+
// Retained once before it is compared: the answer may run user-controlled
593+
// machinery when read, so comparing it and then retaining it would leave
594+
// a gap in which those two reads could describe different definitions.
595+
// Asked before anything is recorded and before the window is spent, so a
596+
// refusal leaves both untouched.
597+
const copy = retain(answer);
598+
if (
599+
held.baseline === undefined ||
600+
copy === undefined ||
601+
!stillDescribes(held.baseline, copy)
602+
) {
598603
throw new AnswerIdentityError(
599604
"this answer no longer describes the implementation its identity was stated about, so a " +
600605
"further import cannot be answered with it. One implementation states what it is " +
@@ -607,7 +612,7 @@ export class CanonicalImports {
607612
if (this.#spent.get(installation)?.has(open) === true) {
608613
throw new AnswerIdentityError(SPENT_OPPORTUNITY);
609614
}
610-
held.copies.set(open, retain(answer));
615+
held.copies.set(open, copy);
611616
this.#spend(installation, open);
612617
return answer;
613618
}

‎packages/core/tests/answer-identity.test.ts‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,6 +1135,59 @@ describe("Tier PA10c — one definition answering more than one import", () => {
11351135
expect(imports.identify(first, shared)?.identity).toEqual(IDENTITY);
11361136
});
11371137

1138+
it("PA10c: reuse cannot substitute between comparison and retention", function* () {
1139+
const imports = owner();
1140+
const provider = imports.provider(ORIGIN);
1141+
const here = yield* useScope();
1142+
const reads: string[] = [];
1143+
const shared = new Proxy(answer(), {
1144+
get(target, key, receiver) {
1145+
if (key !== "name") {
1146+
return Reflect.get(target, key, receiver);
1147+
}
1148+
reads.push(key);
1149+
return reads.length === 1 ? "Open" : "Substituted";
1150+
},
1151+
getOwnPropertyDescriptor(target, key) {
1152+
const descriptor = Reflect.getOwnPropertyDescriptor(target, key);
1153+
if (key !== "name" || descriptor === undefined || !("value" in descriptor)) {
1154+
return descriptor;
1155+
}
1156+
return { ...descriptor, value: reads.length < 2 ? "Open" : "Substituted" };
1157+
},
1158+
});
1159+
1160+
const first = resolving(
1161+
imports,
1162+
"Open",
1163+
(resolution) => {
1164+
asking(provider, "Open", (request) => request.claim(shared, OPEN), here);
1165+
return resolution;
1166+
},
1167+
here,
1168+
);
1169+
expect(imports.identify(first, shared)?.definition.name).toBe("Open");
1170+
resolving(
1171+
imports,
1172+
"Open",
1173+
(resolution) => {
1174+
asking(
1175+
provider,
1176+
"Open",
1177+
(request) => {
1178+
expect(refusalOf(() => request.claim(shared, OPEN))).toBeInstanceOf(
1179+
AnswerIdentityError,
1180+
);
1181+
return undefined;
1182+
},
1183+
here,
1184+
);
1185+
expect(imports.identify(resolution, shared)).toBe(undefined);
1186+
},
1187+
here,
1188+
);
1189+
});
1190+
11381191
it("PA10c: an edited definition cannot be claimed again, and identifies nowhere", function* () {
11391192
const imports = owner();
11401193
const provider = imports.provider(ORIGIN);

0 commit comments

Comments
 (0)