Skip to content

Commit 81ccd6a

Browse files
authored
Added new 'jerry_binary_operation' API function (#2746)
JerryScript-DCO-1.0-Signed-off-by: László Langó llango.u-szeged@partner.samsung.com
1 parent 5e846c9 commit 81ccd6a

File tree

5 files changed

+451
-0
lines changed

5 files changed

+451
-0
lines changed

docs/02.API-REFERENCE.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,18 @@ An opaque declaration of the JerryScript context structure.
276276
typedef struct jerry_context_t jerry_context_t;
277277
```
278278

279+
280+
## jerry_binary_operation_t
281+
282+
Enum that contains the supported binary operation types
283+
- JERRY_BIN_OP_EQUAL - equal comparison (==)
284+
- JERRY_BIN_OP_STRICT_EQUAL - strict equal comparison (===)
285+
- JERRY_BIN_OP_LESS - less relation (<)
286+
- JERRY_BIN_OP_LESS_EQUAL - less or equal relation (<=)
287+
- JERRY_BIN_OP_GREATER - greater relation (>)
288+
- JERRY_BIN_OP_GREATER_EQUAL - greater or equal relation (>=)
289+
290+
279291
## jerry_property_descriptor_t
280292

281293
**Summary**
@@ -1701,6 +1713,69 @@ jerry_is_feature_enabled (const jerry_feature_t feature);
17011713
}
17021714
```
17031715

1716+
**See also**
1717+
- [jerry_feature_t](#jerry_feature_t)
1718+
1719+
1720+
# Binary operations
1721+
1722+
## jerry_binary_operation
1723+
1724+
**Summary**
1725+
1726+
Perform binary operation on the given operands (==, ===, <, >, etc.).
1727+
1728+
**Prototype**
1729+
1730+
```c
1731+
jerry_value_t
1732+
jerry_binary_operation (jerry_binary_operation_t op,
1733+
const jerry_value_t lhs,
1734+
const jerry_value_t rhs);
1735+
```
1736+
1737+
- `op` - binary operation
1738+
- `lhs` - left-hand side operand
1739+
- `rhs` - right-hand side operand
1740+
- return value
1741+
- error, if argument has an error flag or operation is unsuccessful or unsupported
1742+
- true/false, the result of the binary operation on the given operands otherwise
1743+
1744+
**Example**
1745+
1746+
```c
1747+
{
1748+
jerry_value_t value1;
1749+
jerry_value_t value2;
1750+
... // create or acquire value
1751+
jerry_value_t result = jerry_binary_operation (JERRY_BIN_OP_EQUAL, value1, value2)
1752+
1753+
if (!jerry_value_is_error (result))
1754+
{
1755+
if (jerry_get_boolean_value (result))
1756+
{
1757+
// value1 and value2 are equal
1758+
}
1759+
else
1760+
{
1761+
// value1 and value2 are NOT equal
1762+
}
1763+
}
1764+
else
1765+
{
1766+
... // handle error
1767+
}
1768+
1769+
jerry_release_value (value1);
1770+
jerry_release_value (value2);
1771+
jerry_release_value (result);
1772+
}
1773+
```
1774+
1775+
**See also**
1776+
- [jerry_binary_operation_t](#jerry_binary_operation_t)
1777+
1778+
17041779
# Error manipulation functions
17051780

17061781
## jerry_create_abort_from_value

jerry-core/api/jerry.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "ecma-arraybuffer-object.h"
2222
#include "ecma-builtin-helpers.h"
2323
#include "ecma-builtins.h"
24+
#include "ecma-comparison.h"
2425
#include "ecma-exceptions.h"
2526
#include "ecma-eval.h"
2627
#include "ecma-function-object.h"
@@ -34,6 +35,7 @@
3435
#include "ecma-regexp-object.h"
3536
#include "ecma-promise-object.h"
3637
#include "ecma-typedarray-object.h"
38+
#include "opcodes.h"
3739
#include "jcontext.h"
3840
#include "jerryscript.h"
3941
#include "jerryscript-debugger-transport.h"
@@ -901,6 +903,57 @@ jerry_is_feature_enabled (const jerry_feature_t feature) /**< feature to check *
901903
);
902904
} /* jerry_is_feature_enabled */
903905

906+
/**
907+
* Perform binary operation on the given operands (==, ===, <, >, etc.).
908+
*
909+
* @return error - if argument has an error flag or operation is unsuccessful or unsupported
910+
* true/false - the result of the binary operation on the given operands otherwise
911+
*/
912+
jerry_value_t
913+
jerry_binary_operation (jerry_binary_operation_t op, /**< operation */
914+
const jerry_value_t lhs, /**< first operand */
915+
const jerry_value_t rhs) /**< second operand */
916+
{
917+
jerry_assert_api_available ();
918+
919+
if (ecma_is_value_error_reference (lhs) || ecma_is_value_error_reference (rhs))
920+
{
921+
return jerry_throw (ecma_raise_type_error (ECMA_ERR_MSG (error_value_msg_p)));
922+
}
923+
924+
switch (op)
925+
{
926+
case JERRY_BIN_OP_EQUAL:
927+
{
928+
return jerry_return (ecma_op_abstract_equality_compare (lhs, rhs));
929+
}
930+
case JERRY_BIN_OP_STRICT_EQUAL:
931+
{
932+
return ecma_make_boolean_value (ecma_op_strict_equality_compare (lhs, rhs));
933+
}
934+
case JERRY_BIN_OP_LESS:
935+
{
936+
return jerry_return (opfunc_relation (lhs, rhs, true, false));
937+
}
938+
case JERRY_BIN_OP_LESS_EQUAL:
939+
{
940+
return jerry_return (opfunc_relation (lhs, rhs, false, true));
941+
}
942+
case JERRY_BIN_OP_GREATER:
943+
{
944+
return jerry_return (opfunc_relation (lhs, rhs, false, false));
945+
}
946+
case JERRY_BIN_OP_GREATER_EQUAL:
947+
{
948+
return jerry_return (opfunc_relation (lhs, rhs, true, true));
949+
}
950+
default:
951+
{
952+
return jerry_throw (ecma_raise_type_error (ECMA_ERR_MSG ("Unsupported binary operation")));
953+
}
954+
}
955+
} /* jerry_binary_operation */
956+
904957
/**
905958
* Create abort from an api value.
906959
*

jerry-core/ecma/operations/ecma-comparison.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,13 @@
3131
*
3232
* See also: ECMA-262 v5, 11.9.3
3333
*
34+
* Note:
35+
* This function might raise an exception, so the
36+
* returned value must be freed with ecma_free_value.
37+
*
3438
* @return true - if values are equal,
3539
* false - otherwise
40+
* error - in case of any problems
3641
*/
3742
ecma_value_t
3843
ecma_op_abstract_equality_compare (ecma_value_t x, /**< first operand */

jerry-core/include/jerryscript-core.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,19 @@ typedef struct
308308
*/
309309
typedef struct jerry_context_t jerry_context_t;
310310

311+
/**
312+
* Enum that contains the supported binary operation types
313+
*/
314+
typedef enum
315+
{
316+
JERRY_BIN_OP_EQUAL = 0u, /**< equal comparison (==) */
317+
JERRY_BIN_OP_STRICT_EQUAL, /**< strict equal comparison (===) */
318+
JERRY_BIN_OP_LESS, /**< less relation (<) */
319+
JERRY_BIN_OP_LESS_EQUAL, /**< less or equal relation (<=) */
320+
JERRY_BIN_OP_GREATER, /**< greater relation (>) */
321+
JERRY_BIN_OP_GREATER_EQUAL /**< greater or equal relation (>=)*/
322+
} jerry_binary_operation_t;
323+
311324
/**
312325
* General engine functions.
313326
*/
@@ -379,6 +392,13 @@ jerry_type_t jerry_value_get_type (const jerry_value_t value);
379392
*/
380393
bool jerry_is_feature_enabled (const jerry_feature_t feature);
381394

395+
/**
396+
* Binary operations
397+
*/
398+
jerry_value_t jerry_binary_operation (jerry_binary_operation_t op,
399+
const jerry_value_t lhs,
400+
const jerry_value_t rhs);
401+
382402
/**
383403
* Error manipulation functions.
384404
*/

0 commit comments

Comments
 (0)