未验证 提交 81ccd6a7 编写于 作者: L László Langó 提交者: GitHub

Added new 'jerry_binary_operation' API function (#2746)

JerryScript-DCO-1.0-Signed-off-by: László Langó llango.u-szeged@partner.samsung.com
上级 5e846c9e
......@@ -276,6 +276,18 @@ An opaque declaration of the JerryScript context structure.
typedef struct jerry_context_t jerry_context_t;
```
## jerry_binary_operation_t
Enum that contains the supported binary operation types
- JERRY_BIN_OP_EQUAL - equal comparison (==)
- JERRY_BIN_OP_STRICT_EQUAL - strict equal comparison (===)
- JERRY_BIN_OP_LESS - less relation (<)
- JERRY_BIN_OP_LESS_EQUAL - less or equal relation (<=)
- JERRY_BIN_OP_GREATER - greater relation (>)
- JERRY_BIN_OP_GREATER_EQUAL - greater or equal relation (>=)
## jerry_property_descriptor_t
**Summary**
......@@ -1701,6 +1713,69 @@ jerry_is_feature_enabled (const jerry_feature_t feature);
}
```
**See also**
- [jerry_feature_t](#jerry_feature_t)
# Binary operations
## jerry_binary_operation
**Summary**
Perform binary operation on the given operands (==, ===, <, >, etc.).
**Prototype**
```c
jerry_value_t
jerry_binary_operation (jerry_binary_operation_t op,
const jerry_value_t lhs,
const jerry_value_t rhs);
```
- `op` - binary operation
- `lhs` - left-hand side operand
- `rhs` - right-hand side operand
- return value
- error, if argument has an error flag or operation is unsuccessful or unsupported
- true/false, the result of the binary operation on the given operands otherwise
**Example**
```c
{
jerry_value_t value1;
jerry_value_t value2;
... // create or acquire value
jerry_value_t result = jerry_binary_operation (JERRY_BIN_OP_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_binary_operation_t](#jerry_binary_operation_t)
# Error manipulation functions
## jerry_create_abort_from_value
......
......@@ -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"
......@@ -34,6 +35,7 @@
#include "ecma-regexp-object.h"
#include "ecma-promise-object.h"
#include "ecma-typedarray-object.h"
#include "opcodes.h"
#include "jcontext.h"
#include "jerryscript.h"
#include "jerryscript-debugger-transport.h"
......@@ -901,6 +903,57 @@ jerry_is_feature_enabled (const jerry_feature_t feature) /**< feature to check *
);
} /* jerry_is_feature_enabled */
/**
* Perform binary operation on the given operands (==, ===, <, >, etc.).
*
* @return error - if argument has an error flag or operation is unsuccessful or unsupported
* true/false - the result of the binary operation on the given operands otherwise
*/
jerry_value_t
jerry_binary_operation (jerry_binary_operation_t op, /**< operation */
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)));
}
switch (op)
{
case JERRY_BIN_OP_EQUAL:
{
return jerry_return (ecma_op_abstract_equality_compare (lhs, rhs));
}
case JERRY_BIN_OP_STRICT_EQUAL:
{
return ecma_make_boolean_value (ecma_op_strict_equality_compare (lhs, rhs));
}
case JERRY_BIN_OP_LESS:
{
return jerry_return (opfunc_relation (lhs, rhs, true, false));
}
case JERRY_BIN_OP_LESS_EQUAL:
{
return jerry_return (opfunc_relation (lhs, rhs, false, true));
}
case JERRY_BIN_OP_GREATER:
{
return jerry_return (opfunc_relation (lhs, rhs, false, false));
}
case JERRY_BIN_OP_GREATER_EQUAL:
{
return jerry_return (opfunc_relation (lhs, rhs, true, true));
}
default:
{
return jerry_throw (ecma_raise_type_error (ECMA_ERR_MSG ("Unsupported binary operation")));
}
}
} /* jerry_binary_operation */
/**
* Create abort from an api value.
*
......
......@@ -31,8 +31,13 @@
*
* See also: ECMA-262 v5, 11.9.3
*
* Note:
* This function might raise an exception, so the
* returned value must be freed with ecma_free_value.
*
* @return true - if values are equal,
* false - otherwise
* error - in case of any problems
*/
ecma_value_t
ecma_op_abstract_equality_compare (ecma_value_t x, /**< first operand */
......
......@@ -308,6 +308,19 @@ typedef struct
*/
typedef struct jerry_context_t jerry_context_t;
/**
* Enum that contains the supported binary operation types
*/
typedef enum
{
JERRY_BIN_OP_EQUAL = 0u, /**< equal comparison (==) */
JERRY_BIN_OP_STRICT_EQUAL, /**< strict equal comparison (===) */
JERRY_BIN_OP_LESS, /**< less relation (<) */
JERRY_BIN_OP_LESS_EQUAL, /**< less or equal relation (<=) */
JERRY_BIN_OP_GREATER, /**< greater relation (>) */
JERRY_BIN_OP_GREATER_EQUAL /**< greater or equal relation (>=)*/
} jerry_binary_operation_t;
/**
* General engine functions.
*/
......@@ -379,6 +392,13 @@ jerry_type_t jerry_value_get_type (const jerry_value_t value);
*/
bool jerry_is_feature_enabled (const jerry_feature_t feature);
/**
* Binary operations
*/
jerry_value_t jerry_binary_operation (jerry_binary_operation_t op,
const jerry_value_t lhs,
const jerry_value_t rhs);
/**
* Error manipulation functions.
*/
......
/* 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(op, lhs, rhs, res) \
{ op, lhs, rhs, res }
typedef struct
{
jerry_binary_operation_t op;
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[] =
{
/* Testing strict equal comparison */
T (JERRY_BIN_OP_STRICT_EQUAL, jerry_create_number (5.0), jerry_create_number (5.0), true),
T (JERRY_BIN_OP_STRICT_EQUAL, jerry_create_number (3.1), jerry_create_number (10), false),
T (JERRY_BIN_OP_STRICT_EQUAL, jerry_create_number (3.1), jerry_create_undefined (), false),
T (JERRY_BIN_OP_STRICT_EQUAL, jerry_create_number (3.1), jerry_create_boolean (true), false),
T (JERRY_BIN_OP_STRICT_EQUAL,
jerry_create_string ((const jerry_char_t *) "example string"),
jerry_create_string ((const jerry_char_t *) "example string"),
true),
T (JERRY_BIN_OP_STRICT_EQUAL,
jerry_create_string ((const jerry_char_t *) "example string"),
jerry_create_undefined (),
false),
T (JERRY_BIN_OP_STRICT_EQUAL,
jerry_create_string ((const jerry_char_t *) "example string"),
jerry_create_null (),
false),
T (JERRY_BIN_OP_STRICT_EQUAL,
jerry_create_string ((const jerry_char_t *) "example string"),
jerry_create_number (5.0),
false),
T (JERRY_BIN_OP_STRICT_EQUAL, jerry_create_undefined (), jerry_create_undefined (), true),
T (JERRY_BIN_OP_STRICT_EQUAL, jerry_create_undefined (), jerry_create_null (), false),
T (JERRY_BIN_OP_STRICT_EQUAL, jerry_create_null (), jerry_create_null (), true),
T (JERRY_BIN_OP_STRICT_EQUAL, jerry_create_boolean (true), jerry_create_boolean (true), true),
T (JERRY_BIN_OP_STRICT_EQUAL, jerry_create_boolean (true), jerry_create_boolean (false), false),
T (JERRY_BIN_OP_STRICT_EQUAL, jerry_create_boolean (false), jerry_create_boolean (true), false),
T (JERRY_BIN_OP_STRICT_EQUAL, jerry_create_boolean (false), jerry_create_boolean (false), true),
T (JERRY_BIN_OP_STRICT_EQUAL, jerry_acquire_value (obj1), jerry_acquire_value (obj1), true),
T (JERRY_BIN_OP_STRICT_EQUAL, jerry_acquire_value (obj1), jerry_acquire_value (obj2), false),
T (JERRY_BIN_OP_STRICT_EQUAL, jerry_acquire_value (obj2), jerry_acquire_value (obj1), false),
T (JERRY_BIN_OP_STRICT_EQUAL, jerry_acquire_value (obj1), jerry_create_null (), false),
T (JERRY_BIN_OP_STRICT_EQUAL, jerry_acquire_value (obj1), jerry_create_undefined (), false),
T (JERRY_BIN_OP_STRICT_EQUAL, jerry_acquire_value (obj1), jerry_create_boolean (true), false),
T (JERRY_BIN_OP_STRICT_EQUAL, jerry_acquire_value (obj1), jerry_create_boolean (false), false),
T (JERRY_BIN_OP_STRICT_EQUAL, jerry_acquire_value (obj1), jerry_create_number (5.0), false),
T (JERRY_BIN_OP_STRICT_EQUAL,
jerry_acquire_value (obj1),
jerry_create_string ((const jerry_char_t *) "example string"),
false),
/* Testing equal comparison */
T (JERRY_BIN_OP_EQUAL, jerry_create_number (5.0), jerry_create_number (5.0), true),
T (JERRY_BIN_OP_EQUAL, jerry_create_number (3.1), jerry_create_number (10), false),
T (JERRY_BIN_OP_EQUAL, jerry_create_number (3.1), jerry_create_undefined (), false),
T (JERRY_BIN_OP_EQUAL, jerry_create_number (3.1), jerry_create_boolean (true), false),
T (JERRY_BIN_OP_EQUAL,
jerry_create_string ((const jerry_char_t *) "example string"),
jerry_create_string ((const jerry_char_t *) "example string"),
true),
T (JERRY_BIN_OP_EQUAL,
jerry_create_string ((const jerry_char_t *) "example string"),
jerry_create_undefined (),
false),
T (JERRY_BIN_OP_EQUAL,
jerry_create_string ((const jerry_char_t *) "example string"),
jerry_create_null (),
false),
T (JERRY_BIN_OP_EQUAL,
jerry_create_string ((const jerry_char_t *) "example string"),
jerry_create_number (5.0),
false),
T (JERRY_BIN_OP_EQUAL, jerry_create_undefined (), jerry_create_undefined (), true),
T (JERRY_BIN_OP_EQUAL, jerry_create_undefined (), jerry_create_null (), true),
T (JERRY_BIN_OP_EQUAL, jerry_create_null (), jerry_create_null (), true),
T (JERRY_BIN_OP_EQUAL, jerry_create_boolean (true), jerry_create_boolean (true), true),
T (JERRY_BIN_OP_EQUAL, jerry_create_boolean (true), jerry_create_boolean (false), false),
T (JERRY_BIN_OP_EQUAL, jerry_create_boolean (false), jerry_create_boolean (true), false),
T (JERRY_BIN_OP_EQUAL, jerry_create_boolean (false), jerry_create_boolean (false), true),
T (JERRY_BIN_OP_EQUAL, jerry_acquire_value (obj1), jerry_acquire_value (obj1), true),
T (JERRY_BIN_OP_EQUAL, jerry_acquire_value (obj1), jerry_acquire_value (obj2), false),
T (JERRY_BIN_OP_EQUAL, jerry_acquire_value (obj2), jerry_acquire_value (obj1), false),
T (JERRY_BIN_OP_EQUAL, jerry_acquire_value (obj1), jerry_create_null (), false),
T (JERRY_BIN_OP_EQUAL, jerry_acquire_value (obj1), jerry_create_undefined (), false),
T (JERRY_BIN_OP_EQUAL, jerry_acquire_value (obj1), jerry_create_boolean (true), false),
T (JERRY_BIN_OP_EQUAL, jerry_acquire_value (obj1), jerry_create_boolean (false), false),
T (JERRY_BIN_OP_EQUAL, jerry_acquire_value (obj1), jerry_create_number (5.0), false),
T (JERRY_BIN_OP_EQUAL,
jerry_acquire_value (obj1),
jerry_create_string ((const jerry_char_t *) "example string"),
false),
/* Testing less comparison */
T (JERRY_BIN_OP_LESS, jerry_create_number (5.0), jerry_create_number (5.0), false),
T (JERRY_BIN_OP_LESS, jerry_create_number (3.1), jerry_create_number (10), true),
T (JERRY_BIN_OP_LESS, jerry_create_number (3.1), jerry_create_undefined (), false),
T (JERRY_BIN_OP_LESS, jerry_create_number (3.1), jerry_create_boolean (true), false),
T (JERRY_BIN_OP_LESS,
jerry_create_string ((const jerry_char_t *) "1"),
jerry_create_string ((const jerry_char_t *) "2"),
true),
T (JERRY_BIN_OP_LESS,
jerry_create_string ((const jerry_char_t *) "1"),
jerry_create_undefined (),
false),
T (JERRY_BIN_OP_LESS,
jerry_create_string ((const jerry_char_t *) "1"),
jerry_create_null (),
false),
T (JERRY_BIN_OP_LESS,
jerry_create_string ((const jerry_char_t *) "1"),
jerry_create_number (5.0),
true),
T (JERRY_BIN_OP_LESS, jerry_create_undefined (), jerry_create_undefined (), false),
T (JERRY_BIN_OP_LESS, jerry_create_undefined (), jerry_create_null (), false),
T (JERRY_BIN_OP_LESS, jerry_create_null (), jerry_create_null (), false),
T (JERRY_BIN_OP_LESS, jerry_create_boolean (true), jerry_create_boolean (true), false),
T (JERRY_BIN_OP_LESS, jerry_create_boolean (true), jerry_create_boolean (false), false),
T (JERRY_BIN_OP_LESS, jerry_create_boolean (false), jerry_create_boolean (true), true),
T (JERRY_BIN_OP_LESS, jerry_create_boolean (false), jerry_create_boolean (false), false),
/* Testing less or equal comparison */
T (JERRY_BIN_OP_LESS_EQUAL, jerry_create_number (5.0), jerry_create_number (5.0), true),
T (JERRY_BIN_OP_LESS_EQUAL, jerry_create_number (5.1), jerry_create_number (5.0), false),
T (JERRY_BIN_OP_LESS_EQUAL, jerry_create_number (3.1), jerry_create_number (10), true),
T (JERRY_BIN_OP_LESS_EQUAL, jerry_create_number (3.1), jerry_create_undefined (), false),
T (JERRY_BIN_OP_LESS_EQUAL, jerry_create_number (3.1), jerry_create_boolean (true), false),
T (JERRY_BIN_OP_LESS_EQUAL,
jerry_create_string ((const jerry_char_t *) "1"),
jerry_create_string ((const jerry_char_t *) "2"),
true),
T (JERRY_BIN_OP_LESS_EQUAL,
jerry_create_string ((const jerry_char_t *) "1"),
jerry_create_string ((const jerry_char_t *) "1"),
true),
T (JERRY_BIN_OP_LESS_EQUAL,
jerry_create_string ((const jerry_char_t *) "1"),
jerry_create_undefined (),
false),
T (JERRY_BIN_OP_LESS_EQUAL,
jerry_create_string ((const jerry_char_t *) "1"),
jerry_create_null (),
false),
T (JERRY_BIN_OP_LESS_EQUAL,
jerry_create_string ((const jerry_char_t *) "1"),
jerry_create_number (5.0),
true),
T (JERRY_BIN_OP_LESS_EQUAL,
jerry_create_string ((const jerry_char_t *) "5.0"),
jerry_create_number (5.0),
true),
T (JERRY_BIN_OP_LESS_EQUAL, jerry_create_undefined (), jerry_create_undefined (), false),
T (JERRY_BIN_OP_LESS_EQUAL, jerry_create_undefined (), jerry_create_null (), false),
T (JERRY_BIN_OP_LESS_EQUAL, jerry_create_null (), jerry_create_null (), true),
T (JERRY_BIN_OP_LESS_EQUAL, jerry_create_boolean (true), jerry_create_boolean (true), true),
T (JERRY_BIN_OP_LESS_EQUAL, jerry_create_boolean (true), jerry_create_boolean (false), false),
T (JERRY_BIN_OP_LESS_EQUAL, jerry_create_boolean (false), jerry_create_boolean (true), true),
T (JERRY_BIN_OP_LESS_EQUAL, jerry_create_boolean (false), jerry_create_boolean (false), true),
/* Testing greater comparison */
T (JERRY_BIN_OP_GREATER, jerry_create_number (5.0), jerry_create_number (5.0), false),
T (JERRY_BIN_OP_GREATER, jerry_create_number (10), jerry_create_number (3.1), true),
T (JERRY_BIN_OP_GREATER, jerry_create_number (3.1), jerry_create_undefined (), false),
T (JERRY_BIN_OP_GREATER, jerry_create_number (3.1), jerry_create_boolean (true), true),
T (JERRY_BIN_OP_GREATER,
jerry_create_string ((const jerry_char_t *) "2"),
jerry_create_string ((const jerry_char_t *) "1"),
true),
T (JERRY_BIN_OP_GREATER,
jerry_create_string ((const jerry_char_t *) "1"),
jerry_create_string ((const jerry_char_t *) "2"),
false),
T (JERRY_BIN_OP_GREATER,
jerry_create_string ((const jerry_char_t *) "1"),
jerry_create_undefined (),
false),
T (JERRY_BIN_OP_GREATER,
jerry_create_string ((const jerry_char_t *) "1"),
jerry_create_null (),
true),
T (JERRY_BIN_OP_GREATER,
jerry_create_number (5.0),
jerry_create_string ((const jerry_char_t *) "1"),
true),
T (JERRY_BIN_OP_GREATER, jerry_create_undefined (), jerry_create_undefined (), false),
T (JERRY_BIN_OP_GREATER, jerry_create_undefined (), jerry_create_null (), false),
T (JERRY_BIN_OP_GREATER, jerry_create_null (), jerry_create_null (), false),
T (JERRY_BIN_OP_GREATER, jerry_create_boolean (true), jerry_create_boolean (true), false),
T (JERRY_BIN_OP_GREATER, jerry_create_boolean (true), jerry_create_boolean (false), true),
T (JERRY_BIN_OP_GREATER, jerry_create_boolean (false), jerry_create_boolean (true), false),
T (JERRY_BIN_OP_GREATER, jerry_create_boolean (false), jerry_create_boolean (false), false),
/* Testing greater or equal comparison */
T (JERRY_BIN_OP_GREATER_EQUAL, jerry_create_number (5.0), jerry_create_number (5.0), true),
T (JERRY_BIN_OP_GREATER_EQUAL, jerry_create_number (5.0), jerry_create_number (5.1), false),
T (JERRY_BIN_OP_GREATER_EQUAL, jerry_create_number (10), jerry_create_number (3.1), true),
T (JERRY_BIN_OP_GREATER_EQUAL, jerry_create_number (3.1), jerry_create_undefined (), false),
T (JERRY_BIN_OP_GREATER_EQUAL, jerry_create_number (3.1), jerry_create_boolean (true), true),
T (JERRY_BIN_OP_GREATER_EQUAL,
jerry_create_string ((const jerry_char_t *) "2"),
jerry_create_string ((const jerry_char_t *) "1"),
true),
T (JERRY_BIN_OP_GREATER_EQUAL,
jerry_create_string ((const jerry_char_t *) "1"),
jerry_create_string ((const jerry_char_t *) "1"),
true),
T (JERRY_BIN_OP_GREATER_EQUAL,
jerry_create_string ((const jerry_char_t *) "1"),
jerry_create_undefined (),
false),
T (JERRY_BIN_OP_GREATER_EQUAL,
jerry_create_string ((const jerry_char_t *) "1"),
jerry_create_null (),
true),
T (JERRY_BIN_OP_GREATER_EQUAL,
jerry_create_number (5.0),
jerry_create_string ((const jerry_char_t *) "1"),
true),
T (JERRY_BIN_OP_GREATER_EQUAL,
jerry_create_string ((const jerry_char_t *) "5.0"),
jerry_create_number (5.0),
true),
T (JERRY_BIN_OP_GREATER_EQUAL, jerry_create_undefined (), jerry_create_undefined (), false),
T (JERRY_BIN_OP_GREATER_EQUAL, jerry_create_undefined (), jerry_create_null (), false),
T (JERRY_BIN_OP_GREATER_EQUAL, jerry_create_null (), jerry_create_null (), true),
T (JERRY_BIN_OP_GREATER_EQUAL, jerry_create_boolean (true), jerry_create_boolean (true), true),
T (JERRY_BIN_OP_GREATER_EQUAL, jerry_create_boolean (true), jerry_create_boolean (false), true),
T (JERRY_BIN_OP_GREATER_EQUAL, jerry_create_boolean (false), jerry_create_boolean (true), false),
T (JERRY_BIN_OP_GREATER_EQUAL, jerry_create_boolean (false), jerry_create_boolean (false), true),
};
for (uint32_t idx = 0; idx < sizeof (tests) / sizeof (test_entry_t); idx++)
{
jerry_value_t result = jerry_binary_operation (tests[idx].op, 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_BIN_OP_STRICT_EQUAL, jerry_acquire_value (err1), jerry_acquire_value (err1), true),
T (JERRY_BIN_OP_STRICT_EQUAL, jerry_acquire_value (err1), jerry_create_undefined (), true),
T (JERRY_BIN_OP_STRICT_EQUAL, 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_binary_operation (tests[idx].op, 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 */
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册