-
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 all 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 | |||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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,26 @@ | ||||||||||||||||
| 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; | |||||||||||||||||
|
|
|||||||||||||||||
| JSObject* newTarget = asObject(callframe->newTarget()); | |||||||||||||||||
| JSGlobalObject* functionGlobalObject = getFunctionRealm(lexicalGlobalObject, newTarget); | |||||||||||||||||
| RETURN_IF_EXCEPTION(scope, {}); | |||||||||||||||||
| Structure* structure = InternalFunction::createSubclassStructure(lexicalGlobalObject, newTarget, functionGlobalObject->objectStructureForObjectConstructor()); | |||||||||||||||||
| RETURN_IF_EXCEPTION(scope, {}); | |||||||||||||||||
| RELEASE_AND_RETURN(scope, JSValue::encode(JSC::constructEmptyObject(vm, structure))); | |||||||||||||||||
| } | |||||||||||||||||
|
Check warning on line 1003 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); | |||||||||||||||||
|
|
|||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is that createSubclassStructure correct? shouldn't it be based on the mock functions structure or no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I believe so — the third argument to
createSubclassStructureis the base structure for the instances this constructor produces, not the structure of the callee. It's used as:newTarget.prototypeisn't an objectnewTarget.prototypeis an object (the result is a structure with that prototype but the base's cell type)A mock function acting as a constructor is emulating an ordinary user-defined function constructor, so its instances should be plain
JSFinalObjects — there's no "mock instance" cell type.objectStructureForObjectConstructor()is the plain-object structure, which matches:ObjectConstructor.cpp—new Object()itselfNapiClass.cpp:54-55— same pattern for N-API classes with a foreignnewTargetJSMockFunctionalso doesn't expose a.prototypeproperty (jest.fn().prototype === undefined), so whennewTargetis the mock itself,createSubclassStructurefalls back to the base structure → plain{}withObject.prototype, which is the OrdinaryCreateFromConstructor default. WhennewTargetis something else (Reflect.construct(mock, [], SomeClass)), it correctly picks upSomeClass.prototype.