-
Notifications
You must be signed in to change notification settings - Fork 58
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
Mapping between .Net objects and JSReference should not allow to match objects of different types. #156
Conversation
@microsoft-github-policy-service agree |
@dmitriyse Thanks for the PR! I understand the issue this is trying to fix, and it makes sense for reference types. However I'm not sure about value types (structs). There is a test failure when checking JS equality of
You can run just that test case with the command:
.NET structs are boxed when wrapped by JS, or when stored or compared in that dictionary. So every time a value is boxed it gets a new object instance, meaning reference equality doesn't work. Arguably since More broadly, I'm curious: what is your interest in this project, and how do you plan to use it? |
Hi @jasongin, I am working on the bridge for the testing framework that propagates FlaUI API to Node.JS I can implement a custom comparison that falls back to the structural Equality for:
I think it's hard to find a solution for object mapping that doesn't have any side effects. Once we discover some unwanted side effects, we will try to improve the solution. I will prepare the update of this PR in the next couple of days. |
Cool! Feel free to open issues about any challenges you encounter in doing that. And I would love to see a demo when you have something ready to share.
That sounds like it should work well. |
…tion of .Net reference types to JS object.
47b57c8
to
cec73a2
Compare
@jasongin, can you please review the updated implementation of this PR? |
|
||
// We should deliberately make different types not equal to avoid type definition mismatch. | ||
// Object of type B that matched through A.Equal(B) can be accessed in JS through description of type A, which is wrong. | ||
if (x.GetType() != y.GetType()) |
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.
This doesn't seem right, and is different from what was discussed. It will return the wrong result for two (equal) instances of the same class, as in the original problem scenario you described.
If x
is a reference type then we want to use reference equality, as in the original iteration of this PR. If x
is a (boxed) value-type then we want to use value equality. (Value equality should fail if the values are different types.)
I think this should work, but it might need some testing:
return x is ValueType ? x.Equals(y) : Object.ReferenceEquals(x, y);
Superseded by PR #247 The implementation in the newer PR is basically the same as the first iteration of this one, though I added tests. I apologize for my confusion in the discussion above. I had been thinking |
The problem can be reproduced as follows:
Object.Equals
that matches different instancesThis fix implements object comparison that doesn't match objects of different types, even if the Equals(object) method matches another type.
Plus, it allows the replacement of the comparer if, for some reason, the default comparer implementation does not work well with an Equal(object) implementation of some type.