Add arithmetic operations support for the API (#3249)

This patch extends the jerry_binary_operation_t list with arithmetic operations.

JerryScript-DCO-1.0-Signed-off-by: Robert Fancsik frobert@inf.u-szeged.hu
This commit is contained in:
Robert Fancsik
2019-10-28 15:46:35 +01:00
committed by GitHub
parent 448c239f08
commit 87f60da14f
7 changed files with 327 additions and 3 deletions
+22
View File
@@ -79,6 +79,17 @@ JERRY_STATIC_ASSERT (((ECMA_PROMISE_STATE_PENDING + 1) == JERRY_PROMISE_STATE_PE
promise_internal_state_matches_external);
#endif /* ENABLED (JERRY_ES2015_BUILTIN_PROMISE) */
/**
* Offset between internal and external arithmetic operator types
*/
#define ECMA_NUMBER_ARITHMETIC_OP_API_OFFSET (JERRY_BIN_OP_SUB - NUMBER_ARITHMETIC_SUBTRACTION)
JERRY_STATIC_ASSERT (((NUMBER_ARITHMETIC_SUBTRACTION + ECMA_NUMBER_ARITHMETIC_OP_API_OFFSET) == JERRY_BIN_OP_SUB)
&& ((NUMBER_ARITHMETIC_MULTIPLICATION + ECMA_NUMBER_ARITHMETIC_OP_API_OFFSET) == JERRY_BIN_OP_MUL)
&& ((NUMBER_ARITHMETIC_DIVISION + ECMA_NUMBER_ARITHMETIC_OP_API_OFFSET) == JERRY_BIN_OP_DIV)
&& ((NUMBER_ARITHMETIC_REMAINDER + ECMA_NUMBER_ARITHMETIC_OP_API_OFFSET) == JERRY_BIN_OP_REM),
number_arithmetics_operation_type_matches_external);
#if !ENABLED (JERRY_PARSER) && !ENABLED (JERRY_SNAPSHOT_EXEC)
#error "JERRY_SNAPSHOT_EXEC must be enabled if JERRY_PARSER is disabled!"
#endif /* !ENABLED (JERRY_PARSER) && !ENABLED (JERRY_SNAPSHOT_EXEC) */
@@ -1027,6 +1038,17 @@ jerry_binary_operation (jerry_binary_operation_t op, /**< operation */
ecma_object_t *proto_obj_p = ecma_get_object_from_value (rhs);
return jerry_return (ecma_op_object_has_instance (proto_obj_p, lhs));
}
case JERRY_BIN_OP_ADD:
{
return jerry_return (opfunc_addition (lhs, rhs));
}
case JERRY_BIN_OP_SUB:
case JERRY_BIN_OP_MUL:
case JERRY_BIN_OP_DIV:
case JERRY_BIN_OP_REM:
{
return jerry_return (do_number_arithmetic (op - ECMA_NUMBER_ARITHMETIC_OP_API_OFFSET, lhs, rhs));
}
default:
{
return jerry_throw (ecma_raise_type_error (ECMA_ERR_MSG ("Unsupported binary operation")));
+5
View File
@@ -327,6 +327,11 @@ typedef enum
JERRY_BIN_OP_GREATER, /**< greater relation (>) */
JERRY_BIN_OP_GREATER_EQUAL, /**< greater or equal relation (>=)*/
JERRY_BIN_OP_INSTANCEOF, /**< instanceof operation */
JERRY_BIN_OP_ADD, /**< addition operator (+) */
JERRY_BIN_OP_SUB, /**< subtraction operator (-) */
JERRY_BIN_OP_MUL, /**< multiplication operator (*) */
JERRY_BIN_OP_DIV, /**< division operator (/) */
JERRY_BIN_OP_REM, /**< remainder operator (%) */
} jerry_binary_operation_t;
/**
+1 -1
View File
@@ -54,7 +54,7 @@ do_number_arithmetic (number_arithmetic_op op, /**< number arithmetic operation
switch (op)
{
case NUMBER_ARITHMETIC_SUBSTRACTION:
case NUMBER_ARITHMETIC_SUBTRACTION:
{
result = num_left - num_right;
break;
+1 -1
View File
@@ -31,7 +31,7 @@
*/
typedef enum
{
NUMBER_ARITHMETIC_SUBSTRACTION, /**< substraction */
NUMBER_ARITHMETIC_SUBTRACTION, /**< subtraction */
NUMBER_ARITHMETIC_MULTIPLICATION, /**< multiplication */
NUMBER_ARITHMETIC_DIVISION, /**< division */
NUMBER_ARITHMETIC_REMAINDER, /**< remainder calculation */
+1 -1
View File
@@ -2393,7 +2393,7 @@ vm_loop (vm_frame_ctx_t *frame_ctx_p) /**< frame context */
continue;
}
result = do_number_arithmetic (NUMBER_ARITHMETIC_SUBSTRACTION,
result = do_number_arithmetic (NUMBER_ARITHMETIC_SUBTRACTION,
left_value,
right_value);