-
Notifications
You must be signed in to change notification settings - Fork 5k
Fix crash when Reflect.construct is used on mock functions returning non-objects #28532
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -837,6 +837,7 @@ JSC_DEFINE_HOST_FUNCTION(jsMockFunctionCall, (JSGlobalObject * lexicalGlobalObje | |
| return {}; | ||
| } | ||
|
|
||
| const bool isConstruct = !callframe->newTarget().isUndefined(); | ||
| JSC::ArgList args = JSC::ArgList(callframe); | ||
| JSValue thisValue = callframe->thisValue(); | ||
| JSC::JSArray* argumentsArray = nullptr; | ||
|
|
@@ -954,15 +955,24 @@ JSC_DEFINE_HOST_FUNCTION(jsMockFunctionCall, (JSGlobalObject * lexicalGlobalObje | |
| fn->returnValues.set(vm, fn, returnValuesArray); | ||
| } | ||
|
|
||
| if (isConstruct && !returnValue.isObject()) { | ||
| return JSValue::encode(thisValue.isObject() ? thisValue : JSC::constructEmptyObject(globalObject)); | ||
| } | ||
| return JSValue::encode(returnValue); | ||
| } | ||
| case JSMockImplementation::Kind::ReturnValue: { | ||
| JSValue returnValue = impl->underlyingValue.get(); | ||
| setReturnValue(createMockResult(vm, globalObject, "return"_s, returnValue)); | ||
| if (isConstruct && !returnValue.isObject()) { | ||
| return JSValue::encode(thisValue.isObject() ? thisValue : JSC::constructEmptyObject(globalObject)); | ||
| } | ||
| return JSValue::encode(returnValue); | ||
| } | ||
| case JSMockImplementation::Kind::ReturnThis: { | ||
| setReturnValue(createMockResult(vm, globalObject, "return"_s, thisValue)); | ||
| if (isConstruct && !thisValue.isObject()) { | ||
| return JSValue::encode(JSC::constructEmptyObject(globalObject)); | ||
| } | ||
| return JSValue::encode(thisValue); | ||
| } | ||
| case JSMockImplementation::Kind::RejectedValue: { | ||
|
|
@@ -978,6 +988,9 @@ JSC_DEFINE_HOST_FUNCTION(jsMockFunctionCall, (JSGlobalObject * lexicalGlobalObje | |
| } | ||
|
|
||
| setReturnValue(createMockResult(vm, globalObject, "return"_s, jsUndefined())); | ||
| if (isConstruct) { | ||
| return JSValue::encode(thisValue.isObject() ? thisValue : JSC::constructEmptyObject(globalObject)); | ||
| } | ||
| return JSValue::encode(jsUndefined()); | ||
| } | ||
|
|
||
|
Comment on lines
988
to
996
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟣 Pre-existing issue: Extended reasoning...What the bug is
The specific code pathIn Why existing code does not prevent thisSearching the entire file confirms ImpactAny code that inspects const m = jest.fn();
const obj = new m();
console.log(m.mock.instances[0] === obj); // Jest: true, Bun: false (empty array)How to fixWhen JSC::JSArray* instances = fn->instances.get();
if (isConstruct) {
JSValue instanceValue = thisValue.isObject() ? thisValue : /* fallback constructed object */;
if (instances) {
instances->push(globalObject, instanceValue);
} else {
// initialize array with one entry
fn->instances.set(vm, fn, ...);
}
}Step-by-step proof
This is pre-existing behavior unrelated to this PR, but the PR directly interacts with the affected code area and increases the surface for this issue by making
Comment on lines
988
to
996
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟣 Pre-existing bug: in Extended reasoning...What the bug isIn auto lastImpl = thisObject->implementation.get(); // head of the once-queue
auto lastTail = thisObject->tail.get(); // tail of the once-queue
auto lastFallback = thisObject->fallbackImplmentation.get();
// ...
// synchronous restore:
thisObject->implementation.set(vm, thisObject, lastImpl); // correct
thisObject->tail.set(vm, thisObject, lastImpl); // BUG: should be lastTail
thisObject->fallbackImplmentation.set(vm, thisObject, lastFallback); // correct
The specific code pathThe async cleanup handler Why existing code does not prevent thisThe ImpactWhen a user has queued multiple once-implementations and then calls const m = jest.fn();
m.mockImplementationOnce(() => 1);
m.mockImplementationOnce(() => 2); // tail -> once(2), impl -> once(1)
m.withImplementation(() => 99, () => {});
// After sync restore: tail = once(1) (WRONG, should be once(2))
m.mockImplementationOnce(() => 3);
// pushImplOnce chains once(3) off once(1), but once(2) is now orphaned
// queue should be: once(1)->once(2)->once(3), but is: once(1)->once(3)Step-by-step proof
FixChange the synchronous restore to use thisObject->tail.set(vm, thisObject, lastTail); // was: lastImpl |
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import { expect, jest, test } from "bun:test"; | ||
|
|
||
| test("Reflect.construct on mock with non-object return value does not crash", () => { | ||
| const m = jest.fn(() => 42); | ||
| const result = Reflect.construct(m, []); | ||
| expect(result).toBeObject(); | ||
| expect(m.mock.results[0].value).toBe(42); | ||
| }); | ||
|
|
||
| test("Reflect.construct on mock with mockReturnValue does not crash", () => { | ||
| const m = jest.fn(); | ||
| m.mockReturnValue(123); | ||
| const result = Reflect.construct(m, []); | ||
| expect(result).toBeObject(); | ||
| expect(m.mock.results[0].value).toBe(123); | ||
| }); | ||
|
Comment on lines
+3
to
+16
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 Nitpick | 🔵 Trivial Assert receiver/prototype semantics in the primitive-return cases.
🧪 Suggested coverage additions test("Reflect.construct on mock with non-object return value does not crash", () => {
const m = jest.fn(() => 42);
const result = Reflect.construct(m, []);
expect(result).toBeObject();
+ expect(Object.getPrototypeOf(result)).toBe(m.prototype);
expect(m.mock.results[0].value).toBe(42);
});
@@
test("new on mock with non-object return value still works", () => {
const m = jest.fn(() => 42);
const result = new m();
expect(result).toBeObject();
+ expect(Object.getPrototypeOf(result)).toBe(m.prototype);
expect(m.mock.results[0].value).toBe(42);
});
+
+test("Reflect.construct preserves the supplied newTarget prototype", () => {
+ const m = jest.fn(() => 42);
+ function NewTarget() {}
+
+ const result = Reflect.construct(m, [], NewTarget);
+ expect(Object.getPrototypeOf(result)).toBe(NewTarget.prototype);
+});
+
+test("mockReturnThis under construction returns the receiver", () => {
+ const m = jest.fn().mockReturnThis();
+ const result = new m();
+
+ expect(Object.getPrototypeOf(result)).toBe(m.prototype);
+ expect(m.mock.results[0].value).toBe(result);
+});Also applies to: 25-30 🤖 Prompt for AI Agents |
||
|
|
||
| test("Reflect.construct on mock with object return value returns that object", () => { | ||
| const obj = { a: 1 }; | ||
| const m = jest.fn(() => obj); | ||
| const result = Reflect.construct(m, []); | ||
| expect(result).toBe(obj); | ||
| }); | ||
|
|
||
| test("new on mock with non-object return value still works", () => { | ||
| const m = jest.fn(() => 42); | ||
| const result = new m(); | ||
| expect(result).toBeObject(); | ||
| expect(m.mock.results[0].value).toBe(42); | ||
| }); | ||
|
claude[bot] marked this conversation as resolved.
Outdated
|
||
Uh oh!
There was an error while loading. Please reload this page.