Skip to content

Commit 756b1b3

Browse files
ryansolidClaude via Cursorcursoragent
committed
signals: omit()'s copy path re-homes accessors with the source as receiver
A prop's getter is defined only for a read through its own object. The plain-object merge() path already binds a copied getter to its source; omit()'s no-Proxy path forwarded the descriptor as-is, so a getter that keeps state on the instance (the compiler's server props, #3511) would throw on the first read of the copy. Both copy paths now follow the same rule, the rule is written down on merge(), and isStatic()'s note about server/client parity says what is actually equal: the descriptors. Co-authored-by: Claude via Cursor <[email protected]> Co-authored-by: Cursor <[email protected]>
1 parent ead76f4 commit 756b1b3

3 files changed

Lines changed: 76 additions & 5 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@solidjs/signals": patch
3+
---
4+
5+
`omit()`'s no-Proxy copy path re-homes accessors with the source as receiver instead of forwarding the descriptor, matching `merge()`'s copy path. A prop's getter is defined only for a read through its own object — the compiler's server-side props keep their state on the instance — so a copy that must stay live defines its own getter that reads through the source.

‎packages/signals/src/store/utils.ts‎

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,10 @@ export function hasStaticKeys(o: any): boolean {
248248
* a literal at the call site (`as="button"`) as a data property and an
249249
* expression (`as={isLink() ? "a" : "button"}`) as a getter, so a component
250250
* library reads the caller's own static/dynamic classification of a prop at
251-
* runtime — identically on server and client, the compiled shape being the
252-
* same on both — and can take a no-computation path for the literal:
251+
* runtime — identically on server and client, the compiler emitting the same
252+
* own DESCRIPTORS on both (the object behind them may differ: the server
253+
* builds props as a plain-prototype instance with shared getters) — and can
254+
* take a no-computation path for the literal:
253255
*
254256
* ```tsx
255257
* const Tag = dynamic(() => props.as, { static: isStatic(props, "as") });
@@ -943,6 +945,14 @@ export function mergeSources(o: any): any[] | undefined {
943945
* Use this in component bodies to merge defaults / overrides without losing
944946
* Solid's per-property tracking.
945947
*
948+
* Reading props, here and anywhere: a prop's getter is defined only for a
949+
* read through its own object (`props.x`, `{ ...props }`, `Reflect.get`,
950+
* these views). Forwarding its descriptor onto another object and reading it
951+
* there is not supported — the compiler's server-side props keep their state
952+
* on the instance, so the getter needs its object as receiver. A copy that
953+
* must stay live defines its own getter that reads through the source, as
954+
* the no-Proxy paths of merge() and omit() do.
955+
*
946956
* @example
947957
* ```tsx
948958
* function Button(_props: { label: string; type?: string; disabled?: boolean }) {
@@ -1143,9 +1153,21 @@ export function omit(props: any, ...keys: any[]): any {
11431153
for (const propName of propNames) {
11441154
if (!isHiddenKey(propName)) {
11451155
const desc = Object.getOwnPropertyDescriptor(props, propName)!;
1146-
!desc.get && !desc.set && desc.enumerable && desc.writable && desc.configurable
1147-
? (result[propName] = desc.value)
1148-
: Object.defineProperty(result, propName, desc);
1156+
if (!desc.get && !desc.set && desc.enumerable && desc.writable && desc.configurable) {
1157+
result[propName] = desc.value;
1158+
} else if (desc.get || desc.set) {
1159+
// An accessor is re-homed with its source as receiver, never copied
1160+
// as-is: a props getter is only defined for a read THROUGH its own
1161+
// object (the compiler's server props keep their state on the
1162+
// instance, so a forwarded descriptor read on the copy throws). Same
1163+
// rule as merge()'s copy path above.
1164+
Object.defineProperty(result, propName, {
1165+
enumerable: desc.enumerable,
1166+
configurable: true,
1167+
get: desc.get && desc.get.bind(props),
1168+
set: desc.set && desc.set.bind(props)
1169+
});
1170+
} else Object.defineProperty(result, propName, desc);
11491171
}
11501172
}
11511173
return result as any;

‎packages/signals/tests/store/utilities-no-proxy.test.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,50 @@ describe("merge/omit without Proxy", () => {
148148
expect(typeof Object.getOwnPropertyDescriptor(l4, "label")!.get).toBe("function");
149149
});
150150

151+
// The compiler's server props are built by a per-site constructor whose
152+
// getters are SHARED across instances and read their state off `this`
153+
// (#3511). Such a getter is defined only for a read through its own object;
154+
// a descriptor forwarded onto the copy would throw on the first read. Both
155+
// copy paths must re-home accessors with the source as receiver.
156+
test("copies re-home accessors: a receiver-dependent props getter reads through its source", () => {
157+
const $m = Symbol("m");
158+
const [sig, setSig] = createSignal("a");
159+
const desc = {
160+
get(this: any) {
161+
return this[$m].label();
162+
},
163+
enumerable: true,
164+
configurable: true
165+
};
166+
function Props(this: any, m: any, as: string) {
167+
this[$m] = m;
168+
this.as = as;
169+
Object.defineProperty(this, "label", desc);
170+
}
171+
Props.prototype = Object.prototype;
172+
const props = new (Props as any)({ label: sig }, "a");
173+
// the shape under test: reading the forwarded descriptor elsewhere throws
174+
const forwarded = Object.defineProperty(
175+
{},
176+
"label",
177+
Object.getOwnPropertyDescriptor(props, "label")!
178+
);
179+
expect(() => (forwarded as any).label).toThrow(TypeError);
180+
181+
const rest = omit(props, "as");
182+
expect(Object.keys(rest)).toEqual(["label"]);
183+
expect(rest.label).toBe("a");
184+
const merged = merge({ role: "button" }, props) as any;
185+
expect(merged.label).toBe("a");
186+
setSig("b");
187+
flush();
188+
expect(rest.label).toBe("b");
189+
expect(merged.label).toBe("b");
190+
// still accessors on the copies (isStatic reads the KIND)
191+
expect(typeof Object.getOwnPropertyDescriptor(rest, "label")!.get).toBe("function");
192+
expect(typeof Object.getOwnPropertyDescriptor(merged, "label")!.get).toBe("function");
193+
});
194+
151195
test("LIMITATION: a function source needs Proxy — the copy path does not read through it", () => {
152196
// merge wraps the function in a memo and the copy walks the memo's own
153197
// keys, not the object it returns. Unchanged from before the views; a

0 commit comments

Comments
 (0)