-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
JSRT API to access javascript relational comparison function #3568
Comments
Thanks for the feature request @markt- . This seems generally useful. Any proposal/suggestion on the shape of the API? Given undefined can be a valid result, I imagine returning a JsValueRef rather than a bool, which can be very annoying to deal with. |
Indeed, I would expect a JsValueRef as well. Testing for boolean value true or false would generally involve calling a JsGetValueType() call on the result first to make sure it isn't undefined. The API, I would imagine, would have a function that takes two JsValueRef's, a boolean indicating whether to evaluate the left or right side first, and a pointer to the JsValueRef to put the result in. This, I think, is the cleanest solution. Alternative approaches that just output a bool, or even a 3-way comparison function return -1, 0, or 1 to indicate two values' relatve magnitudes have deficiencies when working with javascript types in that where a is not equal to b and a is not less than b, for instance, does not necessarily translate to a being greater than b, where this point is mitigated by allowing the relational comparison function being able to return undefined. In my view the only right way to deal with this other than to use the aforementioned abstract relational comparison algorithm would be to define a very general API comparison function that takes two values and outputs a small bitvector (stored within a single int), which explicitly indicates which comparative relation operators would succeed for those values, and there would be one bit used for each relational comparison operation, ie, each of ==, !=, <=, >=, <, and >. It's worth noting that for any given relational comparison, multiple bits would often be set in this output (eg, for the values 2 and 3 in that order, the bits corresponding to !=, <, and <= would each be set). The only concern I would have with this being the partial overlap of such a function with the output of the existing JsEquals API call. |
// + spec guru @bterlson and @curtisman for feedback Thanks for the suggestion! I got some more time to look into the spec. Instead of the abstract relational comparison algorithm (ARCA), would an API that mirrors the < operator (alway returns a boolean) be more useful and intuitive? From what I've read in the spec, it looks like
Of course this means |
When I first saw your comment, I was concerned about the possibility of comparing two different non-primitive values, where both <= and >= comparisons could return true, but ==, <, and > would all return false, but it seems to be true in all cases where the comparison will not throw an exception that regardless of what < may evaluate to, >= is always the opposite and regardless of what <= is, > is always the opposite as well. So yes, that would probably be fine. |
@liminzhu your proposed API is good I think. What it loses from the ARC algorithm is the signal that any of the parameters are NaN. This doesn't seem too important to me. |
Shall we tag this as accepting PR? @akroshg |
Merge pull request #3931 from xiaoyinl:JsLessThan This PR implements the API JsLessThan and JsLessThanOrEqual, proposed in issue #3568. Fixes #3568 Todo: - [x] Add more test cases - [x] Update documents (microsoft/ChakraCore-wiki#43)
It would be tremendously convenient if there were a JSRT API that could compare the magnitude of two JsValueRef's, as per the the ecmascript abstract relational comparison algorithm.
The text was updated successfully, but these errors were encountered: