-
Notifications
You must be signed in to change notification settings - Fork 684
Added new 'jerry_value_strict_equal' API function. #2725
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 |
---|---|---|
|
@@ -356,6 +356,11 @@ bool jerry_value_is_promise (const jerry_value_t value); | |
bool jerry_value_is_string (const jerry_value_t value); | ||
bool jerry_value_is_undefined (const jerry_value_t value); | ||
|
||
/* | ||
* Comparison functions | ||
*/ | ||
jerry_value_t jerry_strict_equal (const jerry_value_t lhs, const jerry_value_t rhs); | ||
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. If strict comparison is made part of the public API, shouldn't 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. The same was suggested by @galpeter. IMO, It would be nice to add other comparison features to the API as well. I think one new API function/PR is enough. I have two open PRs about this topic, so I do not want to open new similar PRs until these have not been merged. 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. Hm, I've missed @galpeter 's suggestion. I'm not sure I completely agree with the one function in this PR though. The < and > operators aren't necessarily needed here, but === and == are quite related, so publishing them via API should happen together, IMO. 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. @akosthekiss To have other comparison api functions was suggested in my post above, but I had another "idea" which was not written down yet: As we introduce more and more api functions for comparisons, does it make sense to have a single function for each of them or is it possible to have a single api function which can be configured to perform different kind of comparisons? For example something like this (do not take the naming into account just the concept): enum jerry_binary_operation {
JERRY_OP_STRICT_EQUALS, // lhs === rhs
JERRY_OP_EQUALS, // lhs == rhs
JERRY_OP_NOT_EQUAL, // lhs != rhs
JERRY_OP_LESS, // lhs < rhs
...
JERRY_OP_ADD, // lhs + rhs
};
jerry_value_t
jerry_op (jerry_binary_operation op, jerry_value_t lhs, jerry_value_t rhs); I'm not sure which approach is better: having api functions for each operation or having a single one. (Maybe this discussion needs its own issue.) 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. I like this idea. 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. @galpeter I like your idea, I will do it. |
||
|
||
/** | ||
* JerryScript API value type information. | ||
*/ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* Copyright JS Foundation and other contributors, http://js.foundation | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include "jerryscript.h" | ||
|
||
#include "test-common.h" | ||
|
||
#define T(lhs, rhs, res) \ | ||
{ lhs, rhs, res } | ||
|
||
typedef struct | ||
{ | ||
jerry_value_t lhs; | ||
jerry_value_t rhs; | ||
bool expected; | ||
} test_entry_t; | ||
|
||
int | ||
main (void) | ||
{ | ||
TEST_INIT (); | ||
|
||
jerry_init (JERRY_INIT_EMPTY); | ||
|
||
jerry_value_t obj1 = jerry_eval ((const jerry_char_t *) "o={x:1};o", 9, JERRY_PARSE_NO_OPTS); | ||
jerry_value_t obj2 = jerry_eval ((const jerry_char_t *) "o={x:1};o", 9, JERRY_PARSE_NO_OPTS); | ||
jerry_value_t err1 = jerry_create_error (JERRY_ERROR_SYNTAX, (const jerry_char_t *) "error"); | ||
|
||
test_entry_t tests[] = | ||
{ | ||
T (jerry_create_number (5.0), jerry_create_number (5.0), true), | ||
T (jerry_create_number (3.1), jerry_create_number (10), false), | ||
T (jerry_create_number (3.1), jerry_create_undefined (), false), | ||
T (jerry_create_number (3.1), jerry_create_boolean (true), false), | ||
T (jerry_create_string ((const jerry_char_t *) "example string"), | ||
jerry_create_string ((const jerry_char_t *) "example string"), | ||
true), | ||
T (jerry_create_string ((const jerry_char_t *) "example string"), jerry_create_undefined (), false), | ||
T (jerry_create_string ((const jerry_char_t *) "example string"), jerry_create_null (), false), | ||
T (jerry_create_string ((const jerry_char_t *) "example string"), jerry_create_number (5.0), false), | ||
T (jerry_create_undefined (), jerry_create_undefined (), true), | ||
T (jerry_create_undefined (), jerry_create_null (), false), | ||
T (jerry_create_null (), jerry_create_null (), true), | ||
T (jerry_create_boolean (true), jerry_create_boolean (true), true), | ||
T (jerry_create_boolean (true), jerry_create_boolean (false), false), | ||
T (jerry_create_boolean (false), jerry_create_boolean (true), false), | ||
T (jerry_create_boolean (false), jerry_create_boolean (false), true), | ||
T (jerry_acquire_value (obj1), jerry_acquire_value (obj1), true), | ||
T (jerry_acquire_value (obj1), jerry_acquire_value (obj2), false), | ||
T (jerry_acquire_value (obj2), jerry_acquire_value (obj1), false), | ||
T (jerry_acquire_value (obj1), jerry_create_null (), false), | ||
T (jerry_acquire_value (obj1), jerry_create_undefined (), false), | ||
T (jerry_acquire_value (obj1), jerry_create_boolean (true), false), | ||
T (jerry_acquire_value (obj1), jerry_create_boolean (false), false), | ||
T (jerry_acquire_value (obj1), jerry_create_number (5.0), false), | ||
T (jerry_acquire_value (obj1), jerry_create_string ((const jerry_char_t *) "example string"), false) | ||
}; | ||
|
||
for (uint32_t idx = 0; idx < sizeof (tests) / sizeof (test_entry_t); idx++) | ||
{ | ||
jerry_value_t result = jerry_strict_equal (tests[idx].lhs, tests[idx].rhs); | ||
TEST_ASSERT (!jerry_value_is_error (result)); | ||
TEST_ASSERT (jerry_get_boolean_value (result) == tests[idx].expected); | ||
jerry_release_value (tests[idx].lhs); | ||
jerry_release_value (tests[idx].rhs); | ||
jerry_release_value (result); | ||
} | ||
|
||
test_entry_t error_tests[] = | ||
{ | ||
T (jerry_acquire_value (err1), jerry_acquire_value (err1), true), | ||
T (jerry_acquire_value (err1), jerry_create_undefined (), true), | ||
T (jerry_create_undefined (), jerry_acquire_value (err1), true), | ||
}; | ||
|
||
for (uint32_t idx = 0; idx < sizeof (error_tests) / sizeof (test_entry_t); idx++) | ||
{ | ||
jerry_value_t result = jerry_strict_equal (error_tests[idx].lhs, error_tests[idx].rhs); | ||
TEST_ASSERT (jerry_value_is_error (result) == error_tests[idx].expected); | ||
jerry_release_value (error_tests[idx].lhs); | ||
jerry_release_value (error_tests[idx].rhs); | ||
jerry_release_value (result); | ||
} | ||
|
||
jerry_release_value (obj1); | ||
jerry_release_value (obj2); | ||
jerry_release_value (err1); | ||
|
||
jerry_cleanup (); | ||
|
||
return 0; | ||
} /* main */ |
Uh oh!
There was an error while loading. Please reload this page.