-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Fix crash when using Reflect.construct on a mock function #29790
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 1 commit
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 | |||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -86,6 +86,7 @@ | ||||||||||||||||
| } | |||||||||||||||||
|
|
|||||||||||||||||
| JSC_DECLARE_HOST_FUNCTION(jsMockFunctionCall); | |||||||||||||||||
| JSC_DECLARE_HOST_FUNCTION(jsMockFunctionConstruct); | |||||||||||||||||
| JSC_DECLARE_CUSTOM_GETTER(jsMockFunctionGetter_protoImpl); | |||||||||||||||||
| JSC_DECLARE_CUSTOM_GETTER(jsMockFunctionGetter_mock); | |||||||||||||||||
| JSC_DECLARE_HOST_FUNCTION(jsMockFunctionGetter_mockGetLastCall); | |||||||||||||||||
|
|
@@ -462,7 +463,7 @@ | ||||||||||||||||
| } | |||||||||||||||||
|
|
|||||||||||||||||
| JSMockFunction(JSC::VM& vm, JSC::Structure* structure, CallbackKind wrapKind) | |||||||||||||||||
| : Base(vm, structure, jsMockFunctionCall, jsMockFunctionCall) | |||||||||||||||||
| : Base(vm, structure, jsMockFunctionCall, jsMockFunctionConstruct) | |||||||||||||||||
| { | |||||||||||||||||
| initMock(); | |||||||||||||||||
| } | |||||||||||||||||
|
|
@@ -981,6 +982,21 @@ | ||||||||||||||||
| return JSValue::encode(jsUndefined()); | |||||||||||||||||
| } | |||||||||||||||||
|
|
|||||||||||||||||
| JSC_DEFINE_HOST_FUNCTION(jsMockFunctionConstruct, (JSGlobalObject * lexicalGlobalObject, CallFrame* callframe)) | |||||||||||||||||
| { | |||||||||||||||||
| auto& vm = JSC::getVM(lexicalGlobalObject); | |||||||||||||||||
| auto scope = DECLARE_THROW_SCOPE(vm); | |||||||||||||||||
|
|
|||||||||||||||||
| EncodedJSValue encodedResult = jsMockFunctionCall(lexicalGlobalObject, callframe); | |||||||||||||||||
| RETURN_IF_EXCEPTION(scope, {}); | |||||||||||||||||
|
|
|||||||||||||||||
| JSValue result = JSValue::decode(encodedResult); | |||||||||||||||||
| if (result.isObject()) [[likely]] | |||||||||||||||||
| return encodedResult; | |||||||||||||||||
|
|
|||||||||||||||||
| RELEASE_AND_RETURN(scope, JSValue::encode(JSC::constructEmptyObject(lexicalGlobalObject))); | |||||||||||||||||
|
Check warning on line 997 in src/bun.js/bindings/JSMockFunction.cpp
|
|||||||||||||||||
| } | |||||||||||||||||
|
coderabbitai[bot] marked this conversation as resolved.
Comment on lines
+985
to
+1003
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. 🟡 Nit: the fallback object is allocated after the implementation runs (which is invoked as a plain Extended reasoning...What the bug is
JSValue returnValue = Bun::call(globalObject, result, callData, thisValue, args);
// where thisValue = callframe->thisValue()i.e. a plain In ordinary ECMAScript Step‑by‑step proofconst m = jest.fn(function () { this.x = 42; });
const inst = new m();
A secondary consequence of the same ordering: Why nothing prevents it
ImpactLow. Before this PR the same path crashed ( How to fix (follow‑up)Allocate the receiver up front and thread it through the implementation call, roughly: JSObject* newTarget = asObject(callframe->newTarget());
auto* fnGlobal = getFunctionRealm(lexicalGlobalObject, newTarget);
RETURN_IF_EXCEPTION(scope, {});
Structure* structure = InternalFunction::createSubclassStructure(
lexicalGlobalObject, newTarget, fnGlobal->objectStructureForObjectConstructor());
RETURN_IF_EXCEPTION(scope, {});
JSObject* receiver = constructEmptyObject(vm, structure);
// invoke the mock with `receiver` as thisValue (requires plumbing into jsMockFunctionCall
// or factoring out a helper that takes an explicit thisValue)
return result.isObject() ? encodedResult : JSValue::encode(receiver);That's a larger refactor than this crash fix targets, so flagging as a known limitation / follow‑up rather than a blocker. |
|||||||||||||||||
|
|
|||||||||||||||||
| void JSMockFunctionPrototype::finishCreation(JSC::VM& vm, JSC::JSGlobalObject* globalObject) | |||||||||||||||||
| { | |||||||||||||||||
| Base::finishCreation(vm); | |||||||||||||||||
|
|
|||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.