Skip to content

Commit 2a3ae10

Browse files
committed
Added new 'jerry_binary_operation' API function
JerryScript-DCO-1.0-Signed-off-by: László Langó [email protected]
1 parent 1ff322b commit 2a3ae10

File tree

4 files changed

+449
-0
lines changed

4 files changed

+449
-0
lines changed

docs/02.API-REFERENCE.md

Lines changed: 76 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 supperted 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,70 @@ 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
1742+
- true, if the two operands are strictly equal
1743+
- false, otherwise
1744+
1745+
**Example**
1746+
1747+
```c
1748+
{
1749+
jerry_value_t value1;
1750+
jerry_value_t value2;
1751+
... // create or acquire value
1752+
jerry_value_t result = jerry_binary_operation (JERRY_BIN_OP_EQUAL, value1, value2)
1753+
1754+
if (!jerry_value_is_error (result))
1755+
{
1756+
if (jerry_get_boolean_value (result))
1757+
{
1758+
// value1 and value2 are equal
1759+
}
1760+
else
1761+
{
1762+
// value1 and value2 are NOT equal
1763+
}
1764+
}
1765+
else
1766+
{
1767+
... // handle error
1768+
}
1769+
1770+
jerry_release_value (value1);
1771+
jerry_release_value (value2);
1772+
jerry_release_value (result);
1773+
}
1774+
```
1775+
1776+
**See also**
1777+
- [jerry_binary_operation_t](#jerry_binary_operation_t)
1778+
1779+
17041780
# Error manipulation functions
17051781

17061782
## jerry_create_abort_from_value

jerry-core/api/jerry.c

Lines changed: 54 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,58 @@ 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 true - if the two operands are strictly equal
910+
* false - otherwise
911+
* error - if argument has an error flag or operation is unsupported
912+
*/
913+
jerry_value_t
914+
jerry_binary_operation (jerry_binary_operation_t op, /**< operation */
915+
const jerry_value_t lhs, /**< first operand */
916+
const jerry_value_t rhs) /**< second operand */
917+
{
918+
jerry_assert_api_available ();
919+
920+
if (ecma_is_value_error_reference (lhs) || ecma_is_value_error_reference (rhs))
921+
{
922+
return jerry_throw (ecma_raise_type_error (ECMA_ERR_MSG (error_value_msg_p)));
923+
}
924+
925+
switch (op)
926+
{
927+
case JERRY_BIN_OP_EQUAL:
928+
{
929+
return ecma_op_abstract_equality_compare (lhs, rhs);
930+
}
931+
case JERRY_BIN_OP_STRICT_EQUAL:
932+
{
933+
return ecma_make_boolean_value (ecma_op_strict_equality_compare (lhs, rhs));
934+
}
935+
case JERRY_BIN_OP_LESS:
936+
{
937+
return opfunc_relation (lhs, rhs, true, false);
938+
}
939+
case JERRY_BIN_OP_LESS_EQUAL:
940+
{
941+
return opfunc_relation (lhs, rhs, false, true);
942+
}
943+
case JERRY_BIN_OP_GREATER:
944+
{
945+
return opfunc_relation (lhs, rhs, false, false);
946+
}
947+
case JERRY_BIN_OP_GREATER_EQUAL:
948+
{
949+
return opfunc_relation (lhs, rhs, true, true);
950+
}
951+
default:
952+
{
953+
return jerry_throw (ecma_raise_type_error (ECMA_ERR_MSG ("Unsupported binary operation")));
954+
}
955+
}
956+
} /* jerry_binary_operation */
957+
904958
/**
905959
* Create abort from an api value.
906960
*

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 supperted binary operation types
313+
*/
314+
typedef enum
315+
{
316+
JERRY_BIN_OP_EQUAL = 0u,
317+
JERRY_BIN_OP_STRICT_EQUAL,
318+
JERRY_BIN_OP_LESS,
319+
JERRY_BIN_OP_LESS_EQUAL,
320+
JERRY_BIN_OP_GREATER,
321+
JERRY_BIN_OP_GREATER_EQUAL
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)