Skip to content

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions docs/02.API-REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -1630,6 +1630,63 @@ jerry_value_is_undefined (const jerry_value_t value)

- [jerry_release_value](#jerry_release_value)


## jerry_strict_equal

**Summary**

Perform strict equality comparison on the given operands.

**Prototype**

```c
jerry_value_t jerry_strict_equal (const jerry_value_t lhs, const jerry_value_t rhs);
```

- `lhs` - left-hand side operand
- `rhs` - right-hand side operand
- return value
- error, if argument has an error flag
- true, if the two operands are strictly equal
- false, otherwise

**Example**

```c
{
jerry_value_t value1;
jerry_value_t value2;
... // create or acquire value

jerry_value_t result = jerry_strict_equal (value1, value2)

if (!jerry_value_is_error (result))
{
if (jerry_get_boolean_value (result))
{
// value1 and value2 are equal
}
else
{
// value1 and value2 are NOT equal
}
}
else
{
... // handle error
}

jerry_release_value (value1);
jerry_release_value (value2);
jerry_release_value (result);
}
```

**See also**

- [jerry_value_t](#jerry_value_t)


## jerry_value_get_type

**Summary**
Expand Down
24 changes: 24 additions & 0 deletions jerry-core/api/jerry.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "ecma-arraybuffer-object.h"
#include "ecma-builtin-helpers.h"
#include "ecma-builtins.h"
#include "ecma-comparison.h"
#include "ecma-exceptions.h"
#include "ecma-eval.h"
#include "ecma-function-object.h"
Expand Down Expand Up @@ -782,6 +783,29 @@ jerry_value_is_undefined (const jerry_value_t value) /**< api value */
return ecma_is_value_undefined (value);
} /* jerry_value_is_undefined */

/**
* Perform strict equality comparison on the given operands.
*
* See also ECMA-262 v5.1, 11.9.6
*
* @return true - if the two operands are strictly equal
* false - otherwise
* error - if argument has an error flag
*/
jerry_value_t
jerry_strict_equal (const jerry_value_t lhs, /**< first operand */
const jerry_value_t rhs) /**< second operand */
{
jerry_assert_api_available ();

if (ecma_is_value_error_reference (lhs) || ecma_is_value_error_reference (rhs))
{
return jerry_throw (ecma_raise_type_error (ECMA_ERR_MSG (error_value_msg_p)));
}

return ecma_make_boolean_value (ecma_op_strict_equality_compare (lhs, rhs));
} /* jerry_strict_equal */

/**
* Perform the base type of the JavaScript value.
*
Expand Down
5 changes: 5 additions & 0 deletions jerry-core/include/jerryscript-core.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If strict comparison is made part of the public API, shouldn't ecma_op_abstract_equality_compare have its API counterpart, too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The 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.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this idea.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.
*/
Expand Down
104 changes: 104 additions & 0 deletions tests/unit-core/test-api-strict-equal.c
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 */