Implement exponentiation operation. (#3692)
JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg zherczeg.u-szeged@partner.samsung.com
This commit is contained in:
@@ -13,6 +13,8 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
#include "ecma-conversion.h"
|
#include "ecma-conversion.h"
|
||||||
#include "lit-char-helpers.h"
|
#include "lit-char-helpers.h"
|
||||||
|
|
||||||
@@ -656,6 +658,31 @@ ecma_number_calc_remainder (ecma_number_t left_num, /**< left operand */
|
|||||||
return r;
|
return r;
|
||||||
} /* ecma_number_calc_remainder */
|
} /* ecma_number_calc_remainder */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compute power operation according to the ES standard.
|
||||||
|
*
|
||||||
|
* @return x ** y
|
||||||
|
*/
|
||||||
|
ecma_number_t
|
||||||
|
ecma_number_pow (ecma_number_t x, /**< left operand */
|
||||||
|
ecma_number_t y) /**< right operand */
|
||||||
|
{
|
||||||
|
if (ecma_number_is_nan (y) ||
|
||||||
|
(ecma_number_is_infinity (y) && (x == 1.0 || x == -1.0)))
|
||||||
|
{
|
||||||
|
/* Handle differences between ES5.1 and ISO C standards for pow. */
|
||||||
|
return ecma_number_make_nan ();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ecma_number_is_zero (y))
|
||||||
|
{
|
||||||
|
/* Handle differences between ES5.1 and ISO C standards for pow. */
|
||||||
|
return (ecma_number_t) 1.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return DOUBLE_TO_ECMA_NUMBER_T (pow (x, y));
|
||||||
|
} /* ecma_number_pow */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ECMA-integer number multiplication.
|
* ECMA-integer number multiplication.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -400,6 +400,7 @@ ecma_number_t ecma_number_get_prev (ecma_number_t num);
|
|||||||
ecma_number_t ecma_number_get_next (ecma_number_t num);
|
ecma_number_t ecma_number_get_next (ecma_number_t num);
|
||||||
ecma_number_t ecma_number_trunc (ecma_number_t num);
|
ecma_number_t ecma_number_trunc (ecma_number_t num);
|
||||||
ecma_number_t ecma_number_calc_remainder (ecma_number_t left_num, ecma_number_t right_num);
|
ecma_number_t ecma_number_calc_remainder (ecma_number_t left_num, ecma_number_t right_num);
|
||||||
|
ecma_number_t ecma_number_pow (ecma_number_t x, ecma_number_t y);
|
||||||
ecma_value_t ecma_number_parse_int (const lit_utf8_byte_t *string_buff,
|
ecma_value_t ecma_number_parse_int (const lit_utf8_byte_t *string_buff,
|
||||||
lit_utf8_size_t string_buff_size,
|
lit_utf8_size_t string_buff_size,
|
||||||
ecma_value_t radix);
|
ecma_value_t radix);
|
||||||
|
|||||||
@@ -505,21 +505,7 @@ ecma_builtin_math_dispatch_routine (uint16_t builtin_routine_id, /**< built-in w
|
|||||||
}
|
}
|
||||||
case ECMA_MATH_OBJECT_POW:
|
case ECMA_MATH_OBJECT_POW:
|
||||||
{
|
{
|
||||||
if (ecma_number_is_nan (y) ||
|
x = ecma_number_pow (x, y);
|
||||||
(ecma_number_is_infinity (y) && (x == 1.0 || x == -1.0)))
|
|
||||||
{
|
|
||||||
/* Handle differences between ES5.1 and ISO C standards for pow. */
|
|
||||||
x = ecma_number_make_nan ();
|
|
||||||
}
|
|
||||||
else if (ecma_number_is_zero (y))
|
|
||||||
{
|
|
||||||
/* Handle differences between ES5.1 and ISO C standards for pow. */
|
|
||||||
x = (ecma_number_t) 1.0;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
x = DOUBLE_TO_ECMA_NUMBER_T (pow (x, y));
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
#if ENABLED (JERRY_ES2015)
|
#if ENABLED (JERRY_ES2015)
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ extern "C"
|
|||||||
/**
|
/**
|
||||||
* Jerry snapshot format version.
|
* Jerry snapshot format version.
|
||||||
*/
|
*/
|
||||||
#define JERRY_SNAPSHOT_VERSION (40u)
|
#define JERRY_SNAPSHOT_VERSION (41u)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Flags for jerry_generate_snapshot and jerry_generate_function_snapshot.
|
* Flags for jerry_generate_snapshot and jerry_generate_function_snapshot.
|
||||||
|
|||||||
@@ -396,6 +396,8 @@
|
|||||||
DIV) \
|
DIV) \
|
||||||
CBC_BINARY_OPERATION (CBC_MODULO, \
|
CBC_BINARY_OPERATION (CBC_MODULO, \
|
||||||
MOD) \
|
MOD) \
|
||||||
|
CBC_BINARY_OPERATION (CBC_EXPONENTIATION, \
|
||||||
|
EXP) \
|
||||||
\
|
\
|
||||||
/* Unary lvalue opcodes. */ \
|
/* Unary lvalue opcodes. */ \
|
||||||
CBC_OPCODE (CBC_DELETE_PUSH_RESULT, CBC_NO_FLAG, -1, \
|
CBC_OPCODE (CBC_DELETE_PUSH_RESULT, CBC_NO_FLAG, -1, \
|
||||||
|
|||||||
@@ -1401,7 +1401,7 @@ lexer_parse_number (parser_context_t *context_p) /**< context */
|
|||||||
* @param type1 type
|
* @param type1 type
|
||||||
*/
|
*/
|
||||||
#define LEXER_TYPE_A_TOKEN(char1, type1) \
|
#define LEXER_TYPE_A_TOKEN(char1, type1) \
|
||||||
case (uint8_t) (char1) : \
|
case (uint8_t) (char1): \
|
||||||
{ \
|
{ \
|
||||||
context_p->token.type = (type1); \
|
context_p->token.type = (type1); \
|
||||||
length = 1; \
|
length = 1; \
|
||||||
@@ -1417,7 +1417,7 @@ lexer_parse_number (parser_context_t *context_p) /**< context */
|
|||||||
* @param type2 type of the second character
|
* @param type2 type of the second character
|
||||||
*/
|
*/
|
||||||
#define LEXER_TYPE_B_TOKEN(char1, type1, char2, type2) \
|
#define LEXER_TYPE_B_TOKEN(char1, type1, char2, type2) \
|
||||||
case (uint8_t) (char1) : \
|
case (uint8_t) (char1): \
|
||||||
{ \
|
{ \
|
||||||
if (length >= 2 && context_p->source_p[1] == (uint8_t) (char2)) \
|
if (length >= 2 && context_p->source_p[1] == (uint8_t) (char2)) \
|
||||||
{ \
|
{ \
|
||||||
@@ -1442,7 +1442,7 @@ lexer_parse_number (parser_context_t *context_p) /**< context */
|
|||||||
* @param type3 type of the third character
|
* @param type3 type of the third character
|
||||||
*/
|
*/
|
||||||
#define LEXER_TYPE_C_TOKEN(char1, type1, char2, type2, char3, type3) \
|
#define LEXER_TYPE_C_TOKEN(char1, type1, char2, type2, char3, type3) \
|
||||||
case (uint8_t) (char1) : \
|
case (uint8_t) (char1): \
|
||||||
{ \
|
{ \
|
||||||
if (length >= 2) \
|
if (length >= 2) \
|
||||||
{ \
|
{ \
|
||||||
@@ -1671,8 +1671,39 @@ lexer_next_token (parser_context_t *context_p) /**< context */
|
|||||||
LEXER_TYPE_C_TOKEN (LIT_CHAR_MINUS, LEXER_SUBTRACT, LIT_CHAR_EQUALS,
|
LEXER_TYPE_C_TOKEN (LIT_CHAR_MINUS, LEXER_SUBTRACT, LIT_CHAR_EQUALS,
|
||||||
LEXER_ASSIGN_SUBTRACT, LIT_CHAR_MINUS, LEXER_DECREASE)
|
LEXER_ASSIGN_SUBTRACT, LIT_CHAR_MINUS, LEXER_DECREASE)
|
||||||
|
|
||||||
LEXER_TYPE_B_TOKEN (LIT_CHAR_ASTERISK, LEXER_MULTIPLY, LIT_CHAR_EQUALS,
|
case (uint8_t) LIT_CHAR_ASTERISK:
|
||||||
LEXER_ASSIGN_MULTIPLY)
|
{
|
||||||
|
if (length >= 2)
|
||||||
|
{
|
||||||
|
if (context_p->source_p[1] == (uint8_t) LIT_CHAR_EQUALS)
|
||||||
|
{
|
||||||
|
context_p->token.type = LEXER_ASSIGN_MULTIPLY;
|
||||||
|
length = 2;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
#if ENABLED (JERRY_ES2015)
|
||||||
|
if (context_p->source_p[1] == (uint8_t) LIT_CHAR_ASTERISK)
|
||||||
|
{
|
||||||
|
if (length >= 3 && context_p->source_p[2] == (uint8_t) LIT_CHAR_EQUALS)
|
||||||
|
{
|
||||||
|
context_p->token.type = LEXER_ASSIGN_EXPONENTIATION;
|
||||||
|
length = 3;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
context_p->token.type = LEXER_EXPONENTIATION;
|
||||||
|
length = 2;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
#endif /* ENABLED (JERRY_ES2015) */
|
||||||
|
}
|
||||||
|
|
||||||
|
context_p->token.type = LEXER_MULTIPLY;
|
||||||
|
length = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
LEXER_TYPE_B_TOKEN (LIT_CHAR_SLASH, LEXER_DIVIDE, LIT_CHAR_EQUALS,
|
LEXER_TYPE_B_TOKEN (LIT_CHAR_SLASH, LEXER_DIVIDE, LIT_CHAR_EQUALS,
|
||||||
LEXER_ASSIGN_DIVIDE)
|
LEXER_ASSIGN_DIVIDE)
|
||||||
LEXER_TYPE_B_TOKEN (LIT_CHAR_PERCENT, LEXER_MODULO, LIT_CHAR_EQUALS,
|
LEXER_TYPE_B_TOKEN (LIT_CHAR_PERCENT, LEXER_MODULO, LIT_CHAR_EQUALS,
|
||||||
@@ -1763,7 +1794,7 @@ lexer_check_next_characters (parser_context_t *context_p, /**< context */
|
|||||||
* @return consumed character
|
* @return consumed character
|
||||||
*/
|
*/
|
||||||
inline uint8_t JERRY_ATTR_ALWAYS_INLINE
|
inline uint8_t JERRY_ATTR_ALWAYS_INLINE
|
||||||
lexer_consume_next_character (parser_context_t *context_p)
|
lexer_consume_next_character (parser_context_t *context_p) /**< context */
|
||||||
{
|
{
|
||||||
JERRY_ASSERT (context_p->source_p < context_p->source_end_p);
|
JERRY_ASSERT (context_p->source_p < context_p->source_end_p);
|
||||||
|
|
||||||
@@ -1773,6 +1804,59 @@ lexer_consume_next_character (parser_context_t *context_p)
|
|||||||
return *context_p->source_p++;
|
return *context_p->source_p++;
|
||||||
} /* lexer_consume_next_character */
|
} /* lexer_consume_next_character */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks whether the next character can be the start of a post primary expression
|
||||||
|
*
|
||||||
|
* Note:
|
||||||
|
* the result is not precise, but this inprecise result
|
||||||
|
* has no side effects for negating number literals
|
||||||
|
*
|
||||||
|
* @return true if the next character can be the start of a post primary expression
|
||||||
|
*/
|
||||||
|
bool
|
||||||
|
lexer_check_post_primary_exp (parser_context_t *context_p) /**< context */
|
||||||
|
{
|
||||||
|
if (!(context_p->token.flags & LEXER_NO_SKIP_SPACES))
|
||||||
|
{
|
||||||
|
lexer_skip_spaces (context_p);
|
||||||
|
context_p->token.flags = (uint8_t) (context_p->token.flags | LEXER_NO_SKIP_SPACES);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (context_p->source_p >= context_p->source_end_p)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (context_p->source_p[0])
|
||||||
|
{
|
||||||
|
case LIT_CHAR_DOT:
|
||||||
|
case LIT_CHAR_LEFT_PAREN:
|
||||||
|
case LIT_CHAR_LEFT_SQUARE:
|
||||||
|
#if ENABLED (JERRY_ES2015)
|
||||||
|
case LIT_CHAR_GRAVE_ACCENT:
|
||||||
|
#endif /* ENABLED (JERRY_ES2015) */
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
case LIT_CHAR_PLUS:
|
||||||
|
case LIT_CHAR_MINUS:
|
||||||
|
{
|
||||||
|
return (!(context_p->token.flags & LEXER_WAS_NEWLINE)
|
||||||
|
&& context_p->source_p + 1 < context_p->source_end_p
|
||||||
|
&& context_p->source_p[1] == context_p->source_p[0]);
|
||||||
|
}
|
||||||
|
#if ENABLED (JERRY_ES2015)
|
||||||
|
case LIT_CHAR_ASTERISK:
|
||||||
|
{
|
||||||
|
return (context_p->source_p + 1 < context_p->source_end_p
|
||||||
|
&& context_p->source_p[1] == (uint8_t) LIT_CHAR_ASTERISK);
|
||||||
|
}
|
||||||
|
#endif /* ENABLED (JERRY_ES2015) */
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
} /* lexer_check_post_primary_exp */
|
||||||
|
|
||||||
#if ENABLED (JERRY_ES2015)
|
#if ENABLED (JERRY_ES2015)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -3295,10 +3379,17 @@ lexer_convert_binary_lvalue_token_to_binary (uint8_t token) /**< binary lvalue t
|
|||||||
JERRY_ASSERT (LEXER_IS_BINARY_LVALUE_TOKEN (token));
|
JERRY_ASSERT (LEXER_IS_BINARY_LVALUE_TOKEN (token));
|
||||||
JERRY_ASSERT (token != LEXER_ASSIGN);
|
JERRY_ASSERT (token != LEXER_ASSIGN);
|
||||||
|
|
||||||
|
#if ENABLED (JERRY_ES2015)
|
||||||
|
if (token <= LEXER_ASSIGN_EXPONENTIATION)
|
||||||
|
{
|
||||||
|
return (uint8_t) (LEXER_ADD + (token - LEXER_ASSIGN_ADD));
|
||||||
|
}
|
||||||
|
#else /* !ENABLED (JERRY_ES2015) */
|
||||||
if (token <= LEXER_ASSIGN_MODULO)
|
if (token <= LEXER_ASSIGN_MODULO)
|
||||||
{
|
{
|
||||||
return (uint8_t) (LEXER_ADD + (token - LEXER_ASSIGN_ADD));
|
return (uint8_t) (LEXER_ADD + (token - LEXER_ASSIGN_ADD));
|
||||||
}
|
}
|
||||||
|
#endif /* ENABLED (JERRY_ES2015) */
|
||||||
|
|
||||||
if (token <= LEXER_ASSIGN_UNS_RIGHT_SHIFT)
|
if (token <= LEXER_ASSIGN_UNS_RIGHT_SHIFT)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -66,10 +66,17 @@ typedef enum
|
|||||||
* IMPORTANT: update CBC_BINARY_OP_TOKEN_TO_OPCODE,
|
* IMPORTANT: update CBC_BINARY_OP_TOKEN_TO_OPCODE,
|
||||||
* CBC_BINARY_LVALUE_OP_TOKEN_TO_OPCODE and
|
* CBC_BINARY_LVALUE_OP_TOKEN_TO_OPCODE and
|
||||||
* parser_binary_precedence_table after changes. */
|
* parser_binary_precedence_table after changes. */
|
||||||
|
#if ENABLED (JERRY_ES2015)
|
||||||
|
#define LEXER_IS_BINARY_OP_TOKEN(token_type) \
|
||||||
|
((token_type) >= LEXER_ASSIGN && (token_type) <= LEXER_EXPONENTIATION)
|
||||||
|
#else /* !ENABLED (JERRY_ES2015) */
|
||||||
#define LEXER_IS_BINARY_OP_TOKEN(token_type) \
|
#define LEXER_IS_BINARY_OP_TOKEN(token_type) \
|
||||||
((token_type) >= LEXER_ASSIGN && (token_type) <= LEXER_MODULO)
|
((token_type) >= LEXER_ASSIGN && (token_type) <= LEXER_MODULO)
|
||||||
|
#endif /* ENABLED (JERRY_ES2015) */
|
||||||
|
|
||||||
#define LEXER_IS_BINARY_LVALUE_TOKEN(token_type) \
|
#define LEXER_IS_BINARY_LVALUE_TOKEN(token_type) \
|
||||||
((token_type) >= LEXER_ASSIGN && (token_type) <= LEXER_ASSIGN_BIT_XOR)
|
((token_type) >= LEXER_ASSIGN && (token_type) <= LEXER_ASSIGN_BIT_XOR)
|
||||||
|
|
||||||
#define LEXER_FIRST_BINARY_OP LEXER_ASSIGN
|
#define LEXER_FIRST_BINARY_OP LEXER_ASSIGN
|
||||||
|
|
||||||
LEXER_ASSIGN, /**< "=" (prec: 3) */
|
LEXER_ASSIGN, /**< "=" (prec: 3) */
|
||||||
@@ -78,6 +85,9 @@ typedef enum
|
|||||||
LEXER_ASSIGN_MULTIPLY, /**< "*=" (prec: 3) */
|
LEXER_ASSIGN_MULTIPLY, /**< "*=" (prec: 3) */
|
||||||
LEXER_ASSIGN_DIVIDE, /**< "/=" (prec: 3) */
|
LEXER_ASSIGN_DIVIDE, /**< "/=" (prec: 3) */
|
||||||
LEXER_ASSIGN_MODULO, /**< "%=" (prec: 3) */
|
LEXER_ASSIGN_MODULO, /**< "%=" (prec: 3) */
|
||||||
|
#if ENABLED (JERRY_ES2015)
|
||||||
|
LEXER_ASSIGN_EXPONENTIATION, /**< "**=" (prec: 3) */
|
||||||
|
#endif /* ENABLED (JERRY_ES2015) */
|
||||||
LEXER_ASSIGN_LEFT_SHIFT, /**< "<<=" (prec: 3) */
|
LEXER_ASSIGN_LEFT_SHIFT, /**< "<<=" (prec: 3) */
|
||||||
LEXER_ASSIGN_RIGHT_SHIFT, /**< ">>=" (prec: 3) */
|
LEXER_ASSIGN_RIGHT_SHIFT, /**< ">>=" (prec: 3) */
|
||||||
LEXER_ASSIGN_UNS_RIGHT_SHIFT, /**< ">>>=" (prec: 3) */
|
LEXER_ASSIGN_UNS_RIGHT_SHIFT, /**< ">>>=" (prec: 3) */
|
||||||
@@ -108,6 +118,9 @@ typedef enum
|
|||||||
LEXER_MULTIPLY, /**< "*" (prec: 14) */
|
LEXER_MULTIPLY, /**< "*" (prec: 14) */
|
||||||
LEXER_DIVIDE, /**< "/" (prec: 14) */
|
LEXER_DIVIDE, /**< "/" (prec: 14) */
|
||||||
LEXER_MODULO, /**< "%" (prec: 14) */
|
LEXER_MODULO, /**< "%" (prec: 14) */
|
||||||
|
#if ENABLED (JERRY_ES2015)
|
||||||
|
LEXER_EXPONENTIATION, /**< "**" (prec: 15) */
|
||||||
|
#endif /* ENABLED (JERRY_ES2015) */
|
||||||
|
|
||||||
LEXER_LEFT_BRACE, /**< "{" */
|
LEXER_LEFT_BRACE, /**< "{" */
|
||||||
LEXER_LEFT_PAREN, /**< "(" */
|
LEXER_LEFT_PAREN, /**< "(" */
|
||||||
|
|||||||
@@ -38,10 +38,15 @@
|
|||||||
#define PARSER_RIGHT_TO_LEFT_ORDER_MAX_PRECEDENCE 6
|
#define PARSER_RIGHT_TO_LEFT_ORDER_MAX_PRECEDENCE 6
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Precende for ternary operation.
|
* Precedence for ternary operation.
|
||||||
*/
|
*/
|
||||||
#define PARSER_RIGHT_TO_LEFT_ORDER_TERNARY_PRECEDENCE 4
|
#define PARSER_RIGHT_TO_LEFT_ORDER_TERNARY_PRECEDENCE 4
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Precedence for exponentiation operation.
|
||||||
|
*/
|
||||||
|
#define PARSER_RIGHT_TO_LEFT_ORDER_EXPONENTIATION 15
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Value of grouping level increase and decrease.
|
* Value of grouping level increase and decrease.
|
||||||
*/
|
*/
|
||||||
@@ -53,14 +58,28 @@
|
|||||||
* See also:
|
* See also:
|
||||||
* lexer_token_type_t
|
* lexer_token_type_t
|
||||||
*/
|
*/
|
||||||
static const uint8_t parser_binary_precedence_table[36] =
|
static const uint8_t parser_binary_precedence_table[] =
|
||||||
{
|
{
|
||||||
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
|
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
|
||||||
|
#if ENABLED (JERRY_ES2015)
|
||||||
|
3,
|
||||||
|
#endif /* ENABLED (JERRY_ES2015) */
|
||||||
4, 5, 6, 7, 8, 9, 10, 10, 10, 10,
|
4, 5, 6, 7, 8, 9, 10, 10, 10, 10,
|
||||||
11, 11, 11, 11, 11, 11, 12, 12, 12,
|
11, 11, 11, 11, 11, 11, 12, 12, 12,
|
||||||
13, 13, 14, 14, 14
|
13, 13, 14, 14, 14,
|
||||||
|
#if ENABLED (JERRY_ES2015)
|
||||||
|
15,
|
||||||
|
#endif /* ENABLED (JERRY_ES2015) */
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#if ENABLED (JERRY_ES2015)
|
||||||
|
JERRY_STATIC_ASSERT (sizeof (parser_binary_precedence_table) == 38,
|
||||||
|
parser_binary_precedence_table_should_have_38_values_in_es2015);
|
||||||
|
#else /* !ENABLED (JERRY_ES2015) */
|
||||||
|
JERRY_STATIC_ASSERT (sizeof (parser_binary_precedence_table) == 36,
|
||||||
|
parser_binary_precedence_table_should_have_36_values_in_es51);
|
||||||
|
#endif /* ENABLED (JERRY_ES2015) */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate byte code for operators with lvalue.
|
* Generate byte code for operators with lvalue.
|
||||||
*/
|
*/
|
||||||
@@ -1562,14 +1581,19 @@ parser_parse_unary_expression (parser_context_t *context_p, /**< context */
|
|||||||
{
|
{
|
||||||
bool is_negative_number = false;
|
bool is_negative_number = false;
|
||||||
|
|
||||||
while (context_p->stack_top_uint8 == LEXER_PLUS
|
if ((context_p->stack_top_uint8 == LEXER_PLUS || context_p->stack_top_uint8 == LEXER_NEGATE)
|
||||||
|| context_p->stack_top_uint8 == LEXER_NEGATE)
|
&& !lexer_check_post_primary_exp (context_p))
|
||||||
{
|
{
|
||||||
if (context_p->stack_top_uint8 == LEXER_NEGATE)
|
do
|
||||||
{
|
{
|
||||||
is_negative_number = !is_negative_number;
|
if (context_p->stack_top_uint8 == LEXER_NEGATE)
|
||||||
|
{
|
||||||
|
is_negative_number = !is_negative_number;
|
||||||
|
}
|
||||||
|
parser_stack_pop_uint8 (context_p);
|
||||||
}
|
}
|
||||||
parser_stack_pop_uint8 (context_p);
|
while (context_p->stack_top_uint8 == LEXER_PLUS
|
||||||
|
|| context_p->stack_top_uint8 == LEXER_NEGATE);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (lexer_construct_number_object (context_p, true, is_negative_number))
|
if (lexer_construct_number_object (context_p, true, is_negative_number))
|
||||||
@@ -2123,15 +2147,31 @@ parser_process_unary_expression (parser_context_t *context_p, /**< context */
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if ENABLED (JERRY_ES2015)
|
||||||
|
uint8_t last_unary_token = LEXER_INCREASE;
|
||||||
|
#endif /* ENABLED (JERRY_ES2015) */
|
||||||
|
|
||||||
/* Generate byte code for the unary operators. */
|
/* Generate byte code for the unary operators. */
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
uint8_t token = context_p->stack_top_uint8;
|
uint8_t token = context_p->stack_top_uint8;
|
||||||
if (!LEXER_IS_UNARY_OP_TOKEN (token))
|
if (!LEXER_IS_UNARY_OP_TOKEN (token))
|
||||||
{
|
{
|
||||||
|
#if ENABLED (JERRY_ES2015)
|
||||||
|
if (context_p->token.type == LEXER_EXPONENTIATION
|
||||||
|
&& last_unary_token != LEXER_INCREASE
|
||||||
|
&& last_unary_token != LEXER_DECREASE)
|
||||||
|
{
|
||||||
|
parser_raise_error (context_p, PARSER_ERR_INVALID_EXPONENTIATION);
|
||||||
|
}
|
||||||
|
#endif /* ENABLED (JERRY_ES2015) */
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if ENABLED (JERRY_ES2015)
|
||||||
|
last_unary_token = token;
|
||||||
|
#endif /* ENABLED (JERRY_ES2015) */
|
||||||
|
|
||||||
parser_push_result (context_p);
|
parser_push_result (context_p);
|
||||||
parser_stack_pop_uint8 (context_p);
|
parser_stack_pop_uint8 (context_p);
|
||||||
|
|
||||||
@@ -3142,6 +3182,16 @@ process_unary_expression:
|
|||||||
{
|
{
|
||||||
min_prec_treshold = parser_binary_precedence_table[context_p->token.type - LEXER_FIRST_BINARY_OP];
|
min_prec_treshold = parser_binary_precedence_table[context_p->token.type - LEXER_FIRST_BINARY_OP];
|
||||||
|
|
||||||
|
#if ENABLED (JERRY_ES2015)
|
||||||
|
/* Check for BINARY_LVALUE tokens + LEXER_LOGICAL_OR + LEXER_LOGICAL_AND + LEXER_EXPONENTIATION */
|
||||||
|
if ((min_prec_treshold == PARSER_RIGHT_TO_LEFT_ORDER_EXPONENTIATION)
|
||||||
|
|| (min_prec_treshold <= PARSER_RIGHT_TO_LEFT_ORDER_MAX_PRECEDENCE
|
||||||
|
&& min_prec_treshold != PARSER_RIGHT_TO_LEFT_ORDER_TERNARY_PRECEDENCE))
|
||||||
|
{
|
||||||
|
/* Right-to-left evaluation order. */
|
||||||
|
min_prec_treshold++;
|
||||||
|
}
|
||||||
|
#else /* !ENABLED (JERRY_ES2015) */
|
||||||
/* Check for BINARY_LVALUE tokens + LEXER_LOGICAL_OR + LEXER_LOGICAL_AND */
|
/* Check for BINARY_LVALUE tokens + LEXER_LOGICAL_OR + LEXER_LOGICAL_AND */
|
||||||
if (min_prec_treshold <= PARSER_RIGHT_TO_LEFT_ORDER_MAX_PRECEDENCE
|
if (min_prec_treshold <= PARSER_RIGHT_TO_LEFT_ORDER_MAX_PRECEDENCE
|
||||||
&& min_prec_treshold != PARSER_RIGHT_TO_LEFT_ORDER_TERNARY_PRECEDENCE)
|
&& min_prec_treshold != PARSER_RIGHT_TO_LEFT_ORDER_TERNARY_PRECEDENCE)
|
||||||
@@ -3149,6 +3199,7 @@ process_unary_expression:
|
|||||||
/* Right-to-left evaluation order. */
|
/* Right-to-left evaluation order. */
|
||||||
min_prec_treshold++;
|
min_prec_treshold++;
|
||||||
}
|
}
|
||||||
|
#endif /* ENABLED (JERRY_ES2015) */
|
||||||
}
|
}
|
||||||
|
|
||||||
parser_process_binary_opcodes (context_p, min_prec_treshold);
|
parser_process_binary_opcodes (context_p, min_prec_treshold);
|
||||||
|
|||||||
@@ -667,6 +667,7 @@ bool lexer_check_next_character (parser_context_t *context_p, lit_utf8_byte_t ch
|
|||||||
bool lexer_check_next_characters (parser_context_t *context_p, lit_utf8_byte_t character1,
|
bool lexer_check_next_characters (parser_context_t *context_p, lit_utf8_byte_t character1,
|
||||||
lit_utf8_byte_t character2);
|
lit_utf8_byte_t character2);
|
||||||
uint8_t lexer_consume_next_character (parser_context_t *context_p);
|
uint8_t lexer_consume_next_character (parser_context_t *context_p);
|
||||||
|
bool lexer_check_post_primary_exp (parser_context_t *context_p);
|
||||||
#if ENABLED (JERRY_ES2015)
|
#if ENABLED (JERRY_ES2015)
|
||||||
void lexer_skip_empty_statements (parser_context_t *context_p);
|
void lexer_skip_empty_statements (parser_context_t *context_p);
|
||||||
bool lexer_check_arrow (parser_context_t *context_p);
|
bool lexer_check_arrow (parser_context_t *context_p);
|
||||||
|
|||||||
@@ -1179,6 +1179,10 @@ parser_error_to_string (parser_error_t error) /**< error code */
|
|||||||
{
|
{
|
||||||
return "Illegal property in declaration context.";
|
return "Illegal property in declaration context.";
|
||||||
}
|
}
|
||||||
|
case PARSER_ERR_INVALID_EXPONENTIATION:
|
||||||
|
{
|
||||||
|
return "Left operand of ** operator cannot be unary expression.";
|
||||||
|
}
|
||||||
case PARSER_ERR_FORMAL_PARAM_AFTER_REST_PARAMETER:
|
case PARSER_ERR_FORMAL_PARAM_AFTER_REST_PARAMETER:
|
||||||
{
|
{
|
||||||
return "Rest parameter must be the last formal parameter.";
|
return "Rest parameter must be the last formal parameter.";
|
||||||
|
|||||||
@@ -146,6 +146,7 @@ typedef enum
|
|||||||
PARSER_ERR_DUPLICATED_ARGUMENT_NAMES, /**< duplicated argument names */
|
PARSER_ERR_DUPLICATED_ARGUMENT_NAMES, /**< duplicated argument names */
|
||||||
PARSER_ERR_INVALID_DESTRUCTURING_PATTERN, /**< invalid destructuring pattern */
|
PARSER_ERR_INVALID_DESTRUCTURING_PATTERN, /**< invalid destructuring pattern */
|
||||||
PARSER_ERR_ILLEGAL_PROPERTY_IN_DECLARATION, /**< illegal property in declaration context */
|
PARSER_ERR_ILLEGAL_PROPERTY_IN_DECLARATION, /**< illegal property in declaration context */
|
||||||
|
PARSER_ERR_INVALID_EXPONENTIATION, /**< left operand of ** operator cannot be unary expression */
|
||||||
PARSER_ERR_NEW_TARGET_EXPECTED, /**< expected new.target expression */
|
PARSER_ERR_NEW_TARGET_EXPECTED, /**< expected new.target expression */
|
||||||
PARSER_ERR_NEW_TARGET_NOT_ALLOWED, /**< new.target is not allowed in the given context */
|
PARSER_ERR_NEW_TARGET_NOT_ALLOWED, /**< new.target is not allowed in the given context */
|
||||||
#endif /* ENABLED (JERRY_ES2015) */
|
#endif /* ENABLED (JERRY_ES2015) */
|
||||||
|
|||||||
@@ -74,6 +74,13 @@ do_number_arithmetic (number_arithmetic_op op, /**< number arithmetic operation
|
|||||||
result = ecma_op_number_remainder (num_left, num_right);
|
result = ecma_op_number_remainder (num_left, num_right);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
#if ENABLED (JERRY_ES2015)
|
||||||
|
case NUMBER_ARITHMETIC_EXPONENTIATION:
|
||||||
|
{
|
||||||
|
result = ecma_number_pow (num_left, num_right);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
#endif /* ENABLED (JERRY_ES2015) */
|
||||||
}
|
}
|
||||||
|
|
||||||
ret_value = ecma_make_number_value (result);
|
ret_value = ecma_make_number_value (result);
|
||||||
|
|||||||
@@ -35,6 +35,9 @@ typedef enum
|
|||||||
NUMBER_ARITHMETIC_MULTIPLICATION, /**< multiplication */
|
NUMBER_ARITHMETIC_MULTIPLICATION, /**< multiplication */
|
||||||
NUMBER_ARITHMETIC_DIVISION, /**< division */
|
NUMBER_ARITHMETIC_DIVISION, /**< division */
|
||||||
NUMBER_ARITHMETIC_REMAINDER, /**< remainder calculation */
|
NUMBER_ARITHMETIC_REMAINDER, /**< remainder calculation */
|
||||||
|
#if ENABLED (JERRY_ES2015)
|
||||||
|
NUMBER_ARITHMETIC_EXPONENTIATION, /**< exponentiation */
|
||||||
|
#endif /* ENABLED (JERRY_ES2015) */
|
||||||
} number_arithmetic_op;
|
} number_arithmetic_op;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -2815,6 +2815,22 @@ vm_loop (vm_frame_ctx_t *frame_ctx_p) /**< frame context */
|
|||||||
*stack_top_p++ = result;
|
*stack_top_p++ = result;
|
||||||
goto free_both_values;
|
goto free_both_values;
|
||||||
}
|
}
|
||||||
|
#if ENABLED (JERRY_ES2015)
|
||||||
|
case VM_OC_EXP:
|
||||||
|
{
|
||||||
|
result = do_number_arithmetic (NUMBER_ARITHMETIC_EXPONENTIATION,
|
||||||
|
left_value,
|
||||||
|
right_value);
|
||||||
|
|
||||||
|
if (ECMA_IS_VALUE_ERROR (result))
|
||||||
|
{
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
|
|
||||||
|
*stack_top_p++ = result;
|
||||||
|
goto free_both_values;
|
||||||
|
}
|
||||||
|
#endif /* ENABLED (JERRY_ES2015) */
|
||||||
case VM_OC_EQUAL:
|
case VM_OC_EQUAL:
|
||||||
{
|
{
|
||||||
result = opfunc_equality (left_value, right_value);
|
result = opfunc_equality (left_value, right_value);
|
||||||
|
|||||||
@@ -182,6 +182,9 @@ typedef enum
|
|||||||
VM_OC_MUL, /**< mul */
|
VM_OC_MUL, /**< mul */
|
||||||
VM_OC_DIV, /**< div */
|
VM_OC_DIV, /**< div */
|
||||||
VM_OC_MOD, /**< mod */
|
VM_OC_MOD, /**< mod */
|
||||||
|
#if ENABLED (JERRY_ES2015)
|
||||||
|
VM_OC_EXP, /**< exponentiation */
|
||||||
|
#endif /* ENABLED (JERRY_ES2015) */
|
||||||
|
|
||||||
VM_OC_EQUAL, /**< equal */
|
VM_OC_EQUAL, /**< equal */
|
||||||
VM_OC_NOT_EQUAL, /**< not equal */
|
VM_OC_NOT_EQUAL, /**< not equal */
|
||||||
@@ -276,6 +279,9 @@ typedef enum
|
|||||||
*/
|
*/
|
||||||
typedef enum
|
typedef enum
|
||||||
{
|
{
|
||||||
|
#if !ENABLED (JERRY_ES2015)
|
||||||
|
VM_OC_EXP = VM_OC_NONE, /**< exponentiation */
|
||||||
|
#endif /* !ENABLED (JERRY_ES2015) */
|
||||||
#if !ENABLED (JERRY_DEBUGGER)
|
#if !ENABLED (JERRY_DEBUGGER)
|
||||||
VM_OC_BREAKPOINT_ENABLED = VM_OC_NONE, /**< enabled breakpoint for debugger is unused */
|
VM_OC_BREAKPOINT_ENABLED = VM_OC_NONE, /**< enabled breakpoint for debugger is unused */
|
||||||
VM_OC_BREAKPOINT_DISABLED = VM_OC_NONE, /**< disabled breakpoint for debugger is unused */
|
VM_OC_BREAKPOINT_DISABLED = VM_OC_NONE, /**< disabled breakpoint for debugger is unused */
|
||||||
|
|||||||
@@ -45,6 +45,11 @@ parse ("c = a-");
|
|||||||
parse("a++\n()");
|
parse("a++\n()");
|
||||||
parse("a--\n.b");
|
parse("a--\n.b");
|
||||||
|
|
||||||
|
assert((-2 .toString()) === -2);
|
||||||
|
|
||||||
|
Number.prototype[0] = 123;
|
||||||
|
assert(-2[0] === -123);
|
||||||
|
|
||||||
function f() {
|
function f() {
|
||||||
var a = 0;
|
var a = 0;
|
||||||
function g() {}
|
function g() {}
|
||||||
|
|||||||
@@ -0,0 +1,73 @@
|
|||||||
|
/* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
var two = 2
|
||||||
|
var three = 3
|
||||||
|
|
||||||
|
// Precedence (right-to-left) tests
|
||||||
|
assert(2 ** 3 ** 2 === 512)
|
||||||
|
assert((2 ** 3) ** 2 === 64)
|
||||||
|
assert(2 ** (3 ** 2) === 512)
|
||||||
|
assert(two ** three ** two === 512)
|
||||||
|
assert((two ** three) ** two === 64)
|
||||||
|
assert(two ** (three ** two) === 512)
|
||||||
|
|
||||||
|
assert(2 ** 2 * 3 ** 3 === 4 * 27)
|
||||||
|
assert(two ** two * three ** three === 4 * 27)
|
||||||
|
|
||||||
|
var a = 3
|
||||||
|
assert((a **= 3) === 27)
|
||||||
|
assert(a === 27)
|
||||||
|
|
||||||
|
a = 2
|
||||||
|
assert((a **= a **= 2) === 16)
|
||||||
|
assert(a === 16)
|
||||||
|
|
||||||
|
function assertThrows(src) {
|
||||||
|
try {
|
||||||
|
eval(src)
|
||||||
|
assert(false)
|
||||||
|
} catch (e) {
|
||||||
|
assert(e instanceof SyntaxError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assertThrows("-2 ** 2")
|
||||||
|
assertThrows("+a ** 2")
|
||||||
|
assertThrows("!a ** 2")
|
||||||
|
assertThrows("~a ** 2")
|
||||||
|
assertThrows("void a ** 2")
|
||||||
|
assertThrows("typeof a ** 2")
|
||||||
|
assertThrows("delete a ** 2")
|
||||||
|
assertThrows("!(-a) ** 2")
|
||||||
|
|
||||||
|
assert((-2) ** 2 === 4)
|
||||||
|
|
||||||
|
a = 3
|
||||||
|
assert((-+-a) ** 3 === 27)
|
||||||
|
|
||||||
|
a = 0
|
||||||
|
assert((!a) ** 2 === 1)
|
||||||
|
|
||||||
|
a = 0
|
||||||
|
assert(isNaN((void a) ** 3))
|
||||||
|
|
||||||
|
a = -4
|
||||||
|
assert(++a ** 2 === 9)
|
||||||
|
assert(a === -3)
|
||||||
|
|
||||||
|
a = -2
|
||||||
|
assert(a-- ** 2 === 4)
|
||||||
|
assert(a === -3)
|
||||||
@@ -223,12 +223,12 @@ main (void)
|
|||||||
/* Check the snapshot data. Unused bytes should be filled with zeroes */
|
/* Check the snapshot data. Unused bytes should be filled with zeroes */
|
||||||
const uint8_t expected_data[] =
|
const uint8_t expected_data[] =
|
||||||
{
|
{
|
||||||
0x4A, 0x52, 0x52, 0x59, 0x28, 0x00, 0x00, 0x00,
|
0x4A, 0x52, 0x52, 0x59, 0x29, 0x00, 0x00, 0x00,
|
||||||
0x00, 0x00, 0x00, 0x00, 0x48, 0x00, 0x00, 0x00,
|
0x00, 0x00, 0x00, 0x00, 0x48, 0x00, 0x00, 0x00,
|
||||||
0x01, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00,
|
0x01, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00,
|
||||||
0x03, 0x00, 0x01, 0x00, 0x41, 0x00, 0x01, 0x00,
|
0x03, 0x00, 0x01, 0x00, 0x41, 0x00, 0x01, 0x00,
|
||||||
0x00, 0x00, 0x00, 0x01, 0x18, 0x00, 0x00, 0x00,
|
0x00, 0x00, 0x00, 0x01, 0x18, 0x00, 0x00, 0x00,
|
||||||
0x2C, 0x00, 0xC3, 0x50, 0x00, 0x00, 0x00, 0x00,
|
0x2C, 0x00, 0xC6, 0x50, 0x00, 0x00, 0x00, 0x00,
|
||||||
0x03, 0x00, 0x01, 0x00, 0x41, 0x00, 0x00, 0x00,
|
0x03, 0x00, 0x01, 0x00, 0x41, 0x00, 0x00, 0x00,
|
||||||
0x00, 0x00, 0x01, 0x01, 0x07, 0x00, 0x00, 0x00,
|
0x00, 0x00, 0x01, 0x01, 0x07, 0x00, 0x00, 0x00,
|
||||||
0x51, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
0x51, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
|||||||
Reference in New Issue
Block a user