Repository navigation
expect: stop toMatchObject and snapshot property matchers from mutating the received object #35452
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
a64e0b4
0c2c5b3
de76a02
bc40454
c04c920
965d7df
809865b
d45cf16
359ecd6
e73970a
4aded9e
4574ec1
f070c03
66db71b
4f1a622
e8602e3
234405d
fe611a9
876c19a
95a49de
3720452
b7544f3
b499bc5
870f2f6
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 |
|---|---|---|
|
|
@@ -272,7 +272,7 @@ template bool Bun__deepMatch<true>( | |
| JSGlobalObject* globalObject, | ||
| ThrowScope& throwScope, | ||
| MarkedArgumentBuffer* gcBuffer, | ||
| bool replacePropsWithAsymmetricMatchers, | ||
| Bun::DeferredMatcherWrites* matcherWrites, | ||
| bool isMatchingObjectContaining); | ||
|
|
||
| template bool Bun__deepMatch<false>( | ||
|
|
@@ -283,7 +283,7 @@ template bool Bun__deepMatch<false>( | |
| JSGlobalObject* globalObject, | ||
| ThrowScope& throwScope, | ||
| MarkedArgumentBuffer* gcBuffer, | ||
| bool replacePropsWithAsymmetricMatchers, | ||
| Bun::DeferredMatcherWrites* matcherWrites, | ||
| bool isMatchingObjectContaining); | ||
|
|
||
| extern "C" bool Expect_readFlagsAndProcessPromise(JSC::EncodedJSValue instanceValue, JSC::JSGlobalObject* globalObject, ExpectFlags* flags, JSC::EncodedJSValue* value, AsymmetricMatcherConstructorType* constructorType); | ||
|
|
@@ -577,7 +577,7 @@ AsymmetricMatcherResult matchAsymmetricMatcherAndGetFlags(JSGlobalObject* global | |
| // SAFETY: visited property sets are not required when | ||
| // `enableAsymmetricMatchers` and `isMatchingObjectContaining` | ||
| // are both true | ||
| bool match = Bun__deepMatch<true>(otherProp, nullptr, patternObject, nullptr, globalObject, throwScope, nullptr, false, true); | ||
| bool match = Bun__deepMatch<true>(otherProp, nullptr, patternObject, nullptr, globalObject, throwScope, nullptr, nullptr, true); | ||
| RETURN_IF_EXCEPTION(throwScope, AsymmetricMatcherResult::FAIL); | ||
| if (match) { | ||
| return AsymmetricMatcherResult::PASS; | ||
|
|
@@ -2222,6 +2222,34 @@ std::optional<bool> specialObjectsDequal(JSC::JSGlobalObject* globalObject, Mark | |
| // only reached from `Bun.deepEquals(a, b, true, true)` in BunObject.cpp. | ||
| template bool Bun__deepEquals<true, false, false, true>(JSC::JSGlobalObject*, JSValue, JSValue, MarkedArgumentBuffer&, Vector<std::pair<JSC::JSValue, JSC::JSValue>, 16>&, ThrowScope&, bool); | ||
|
|
||
| // The properties `Bun__deepMatch` matched with an asymmetric matcher, which the snapshot and diff formatters print as that matcher. | ||
| class Bun::DeferredMatcherWrites { | ||
| WTF_FORBID_HEAP_ALLOCATION; | ||
|
|
||
| public: | ||
| void append(JSObject* target, const Identifier& key, JSValue matcher) | ||
| { | ||
| m_values.append(target); | ||
| m_values.append(matcher); | ||
| m_writes.append({ key }); | ||
| } | ||
|
|
||
| // Puts each matcher over the property it matched, where the formatters read it. | ||
| void apply(JSGlobalObject*, ThrowScope&); | ||
| // Puts back what `apply` replaced. | ||
| void undo(JSGlobalObject*, ThrowScope&); | ||
|
|
||
| private: | ||
| struct Write { | ||
| Identifier key; | ||
| unsigned previousAttributes { 0 }; | ||
| bool written { false }; | ||
| }; | ||
| Vector<Write, 4> m_writes; | ||
| MarkedArgumentBuffer m_values; // the (target, matcher) of each write | ||
| MarkedArgumentBuffer m_previousValues; // what each write replaced, empty if the target had no such property | ||
| }; | ||
|
|
||
| /** | ||
| * @brief `Bun.deepMatch(a, b)` | ||
| * | ||
|
|
@@ -2243,7 +2271,7 @@ template bool Bun__deepEquals<true, false, false, true>(JSC::JSGlobalObject*, JS | |
| * @param globalObject | ||
| * @param throwScope | ||
| * @param gcBuffer | ||
| * @param replacePropsWithAsymmetricMatchers | ||
| * @param matcherWrites | ||
| * @param isMatchingObjectContaining | ||
| * | ||
| * @return true | ||
|
|
@@ -2258,7 +2286,7 @@ bool Bun__deepMatch( | |
| JSGlobalObject* globalObject, | ||
| ThrowScope& throwScope, | ||
| MarkedArgumentBuffer* gcBuffer, | ||
| bool replacePropsWithAsymmetricMatchers, | ||
| Bun::DeferredMatcherWrites* matcherWrites, | ||
| bool isMatchingObjectContaining) | ||
| { | ||
|
|
||
|
|
@@ -2321,10 +2349,8 @@ bool Bun__deepMatch( | |
| case AsymmetricMatcherResult::FAIL: | ||
| return false; | ||
| case AsymmetricMatcherResult::PASS: | ||
| if (replacePropsWithAsymmetricMatchers) { | ||
| obj->putDirectMayBeIndex(globalObject, property, subsetProp); | ||
| RETURN_IF_EXCEPTION(throwScope, false); | ||
| } | ||
| RETURN_IF_EXCEPTION(throwScope, false); | ||
| if (matcherWrites) matcherWrites->append(obj, property, subsetProp); | ||
| // continue to next subset prop | ||
| continue; | ||
| case AsymmetricMatcherResult::NOT_MATCHER: | ||
|
|
@@ -2335,10 +2361,8 @@ bool Bun__deepMatch( | |
| case AsymmetricMatcherResult::FAIL: | ||
| return false; | ||
| case AsymmetricMatcherResult::PASS: | ||
| if (replacePropsWithAsymmetricMatchers) { | ||
| subsetObj->putDirectMayBeIndex(globalObject, property, prop); | ||
| RETURN_IF_EXCEPTION(throwScope, false); | ||
| } | ||
| RETURN_IF_EXCEPTION(throwScope, false); | ||
| if (matcherWrites) matcherWrites->append(subsetObj, property, prop); | ||
| // continue to next subset prop | ||
| continue; | ||
| case AsymmetricMatcherResult::NOT_MATCHER: | ||
|
|
@@ -2367,7 +2391,7 @@ bool Bun__deepMatch( | |
| gcBuffer->append(subsetProp); | ||
| // property cycle detected | ||
| if (!didInsertProp.second || !didInsertSubset.second) continue; | ||
| bool matched = Bun__deepMatch<enableAsymmetricMatchers>(prop, seenObjProperties, subsetProp, seenSubsetProperties, globalObject, throwScope, gcBuffer, replacePropsWithAsymmetricMatchers, isMatchingObjectContaining); | ||
| bool matched = Bun__deepMatch<enableAsymmetricMatchers>(prop, seenObjProperties, subsetProp, seenSubsetProperties, globalObject, throwScope, gcBuffer, matcherWrites, isMatchingObjectContaining); | ||
| RETURN_IF_EXCEPTION(throwScope, false); | ||
| if (!matched) return false; | ||
| } | ||
|
|
@@ -2381,6 +2405,73 @@ bool Bun__deepMatch( | |
| return true; | ||
| } | ||
|
|
||
| // Reads the own property of `target` as it is stored. False if a direct put over it could not be undone. | ||
| static bool readReplaceableProperty(JSGlobalObject* globalObject, ThrowScope& scope, JSObject* target, const Identifier& key, JSValue& value, unsigned& attributes) | ||
| { | ||
| VM& vm = globalObject->vm(); | ||
| if (std::optional<uint32_t> index = parseIndex(key)) { | ||
| JSC::JSType type = target->type(); | ||
| // These keep their elements elsewhere, but take a put and take the element back. A BigInt array throws on a matcher. | ||
| bool asksClass = type == ProxyObjectType || (isTypedArrayType(type) && type != BigInt64ArrayType && type != BigUint64ArrayType); | ||
|
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, not blocking: nit, pre-existing: a Proxy whose target is a BigInt64Array or BigUint64Array still makes a passing index matcher throw a SyntaxError when the diff or snapshot is printed, as on the base branch. The BigInt exclusion in Why this was flaggedTrigger: Verification: pre-existing (nit).
Member
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. Stays as is: same throw by the same path on main, and a Proxy around a BigInt typed array with a matcher on one element is not what this PR is about. Generated by Claude Code |
||
| PropertySlot slot(target, asksClass ? PropertySlot::InternalMethodType::GetOwnProperty : PropertySlot::InternalMethodType::VMInquiry, asksClass ? nullptr : &vm); | ||
| bool hasOwnProperty = asksClass ? target->methodTable()->getOwnPropertySlotByIndex(target, globalObject, *index, slot) : JSObject::getOwnPropertySlotByIndex(target, globalObject, *index, slot); | ||
| RETURN_IF_EXCEPTION(scope, false); | ||
| // A new element can grow an array. | ||
| if (!hasOwnProperty) return false; | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| value = slot.isAccessor() ? JSValue(slot.getterSetter()) : slot.getValue(globalObject, *index); | ||
| attributes = slot.attributes(); | ||
| return true; | ||
| } | ||
| value = target->getDirect(vm, key, attributes); | ||
| // A property the class implements natively, stored or still in its static table, cannot be put over another one. | ||
| return value ? !value.isCustomGetterSetter() : !target->hasNonReifiedStaticProperties() || !target->findPropertyHashEntry(key); | ||
| } | ||
|
|
||
| void Bun::DeferredMatcherWrites::apply(JSGlobalObject* globalObject, ThrowScope& scope) | ||
| { | ||
| for (size_t i = 0; i < m_writes.size(); i++) { | ||
| Write& write = m_writes[i]; | ||
| JSObject* target = asObject(m_values.at(i * 2)); | ||
| JSValue previous; | ||
| bool replaceable = readReplaceableProperty(globalObject, scope, target, write.key, previous, write.previousAttributes); | ||
| RETURN_IF_EXCEPTION(scope, ); | ||
| m_previousValues.append(previous); | ||
| if (!replaceable) continue; | ||
| write.written = target->putDirectMayBeIndex(globalObject, write.key, m_values.at(i * 2 + 1)); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| RETURN_IF_EXCEPTION(scope, ); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| } | ||
| } | ||
|
|
||
| void Bun::DeferredMatcherWrites::undo(JSGlobalObject* globalObject, ThrowScope& scope) | ||
| { | ||
| JSC::Exception* failure = nullptr; | ||
| // Last first: two matchers can be written over one property. | ||
| for (size_t i = m_writes.size(); i--;) { | ||
| const Write& write = m_writes[i]; | ||
| if (!write.written) continue; | ||
| JSObject* target = asObject(m_values.at(i * 2)); | ||
| JSValue previous = m_previousValues.at(i); | ||
|
|
||
| if (!previous) { | ||
| DeletePropertySlot slot; | ||
| JSObject::deleteProperty(target, globalObject, write.key, slot); | ||
| } else if (previous.isGetterSetter()) | ||
| target->putDirectAccessor(globalObject, write.key, uncheckedDowncast<GetterSetter>(previous), write.previousAttributes); | ||
| else if (std::optional<uint32_t> index = parseIndex(write.key)) | ||
| target->putDirectIndex(globalObject, *index, previous, write.previousAttributes, PutDirectIndexLikePutDirect); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| else | ||
| target->putDirect(globalObject->vm(), write.key, previous, write.previousAttributes); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| // A Proxy trap can throw. The other properties are still put back. | ||
| if (JSC::Exception* exception = scope.exception()) [[unlikely]] { | ||
| if (!scope.tryClearException()) return; | ||
| if (!failure) failure = exception; | ||
| } | ||
| } | ||
| if (failure) [[unlikely]] | ||
| scope.throwException(globalObject, failure); | ||
| } | ||
|
|
||
| // anonymous namespace to avoid name collision | ||
| namespace { | ||
| template<bool isStrict, bool enableAsymmetricMatchers, bool checkPrototypes, bool skipPrototypeIdentity = false> | ||
|
|
@@ -3341,7 +3432,7 @@ bool Bun__deepEqualsNodeStrictSkipProto(JSC::EncodedJSValue JSValue0, JSC::Encod | |
|
|
||
| #undef IMPL_DEEP_EQUALS_WRAPPER | ||
|
|
||
| bool JSC__JSValue__jestDeepMatch(JSC::EncodedJSValue JSValue0, JSC::EncodedJSValue JSValue1, JSC::JSGlobalObject* globalObject, bool replacePropsWithAsymmetricMatchers) | ||
| bool JSC__JSValue__jestDeepMatch(JSC::EncodedJSValue JSValue0, JSC::EncodedJSValue JSValue1, JSC::JSGlobalObject* globalObject, bool formatIfMatched, bool formatIfNotMatched, void* ctx, void (*format)(void* ctx, bool matched)) | ||
| { | ||
| JSValue obj = JSValue::decode(JSValue0); | ||
| JSValue subset = JSValue::decode(JSValue1); | ||
|
|
@@ -3351,7 +3442,27 @@ bool JSC__JSValue__jestDeepMatch(JSC::EncodedJSValue JSValue0, JSC::EncodedJSVal | |
| std::set<EncodedJSValue> objVisited; | ||
| std::set<EncodedJSValue> subsetVisited; | ||
| MarkedArgumentBuffer gcBuffer; | ||
| RELEASE_AND_RETURN(scope, Bun__deepMatch<true>(obj, &objVisited, subset, &subsetVisited, globalObject, scope, &gcBuffer, replacePropsWithAsymmetricMatchers, false)); | ||
| Bun::DeferredMatcherWrites matcherWrites; | ||
| bool matched = Bun__deepMatch<true>(obj, &objVisited, subset, &subsetVisited, globalObject, scope, &gcBuffer, &matcherWrites, false); | ||
| RETURN_IF_EXCEPTION(scope, false); | ||
| if (!(matched ? formatIfMatched : formatIfNotMatched)) return matched; | ||
|
|
||
| matcherWrites.apply(globalObject, scope); | ||
| if (!scope.exception()) [[likely]] | ||
| format(ctx, matched); | ||
|
|
||
| { | ||
| // A termination cannot be cleared, and after a `node:vm` timeout the caller goes on. This holds it until the end of the block. | ||
| DeferTermination deferTermination(scope.vm()); | ||
| // `undo` calls into JSC, which returns early while an exception is pending. | ||
| JSC::Exception* exception = scope.exception(); | ||
| if (exception && !scope.tryClearException()) [[unlikely]] | ||
| exception = nullptr; | ||
| matcherWrites.undo(globalObject, scope); | ||
| if (exception && !scope.exception()) [[unlikely]] | ||
| scope.throwException(globalObject, exception); | ||
|
Comment on lines
+3455
to
+3463
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 (optional): a Proxy whose Why this was flaggedTrigger: expect(obj).toMatchObject({ list: { 0: expect.any(Number) }, missing: 1 }) or toMatchSnapshot where obj.list is a Proxy over an array with a defineProperty trap, run under vm.runInNewContext(..., { timeout }) (deadline armed at src/jsc/bindings/vm/NodeVMRunTermination.cpp:29) or inside a Worker that the parent terminates. apply writes the matcher through the trap at bindings.cpp:2392 (first define returns normally). After format, JSC__JSValue__jestDeepMatch constructs DeferTermination at bindings.cpp:3333 and calls undo; for the Proxy write undo calls target->putDirectIndex at bindings.cpp:2413, which goes through ProxyObject::defineOwnProperty and runs the user's defineProperty trap a second time. Because termination is deferred, a trap that loops (while (flag);) or blocks is never interrupted by the deadline: the request is only honoured when the block at 3332-3340 ends, which it never does. The base branch has no restore step, so the trap is never entered a second time and the deadline ends the script. Verification: nit.
Member
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. Stays as is. The path needs a Generated by Claude Code
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| } | ||
| return matched; | ||
| } | ||
|
|
||
| extern "C" bool Bun__JSValue__isAsyncContextFrame(JSC::EncodedJSValue value) | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.