Skip to content

Commit 3577261

Browse files
sand1kRuben Ayrapetyan
authored andcommitted
Add support for eval in parser.
JerryScript-DCO-1.0-Signed-off-by: Andrey Shitov [email protected]
1 parent 57c6c37 commit 3577261

20 files changed

+322
-92
lines changed

jerry-core/ecma/operations/ecma-eval.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include "ecma-globals.h"
2121
#include "ecma-helpers.h"
2222
#include "ecma-lex-env.h"
23+
#include "parser.h"
24+
#include "serializer.h"
2325
#include "vm.h"
2426

2527
/** \addtogroup ecma ECMA
@@ -54,15 +56,15 @@ ecma_op_eval (ecma_string_t *code_p, /**< code string */
5456
buf_size);
5557
JERRY_ASSERT (buffer_size_req == buf_size);
5658

57-
// FIXME: Get parser feedback about syntax correctness
58-
bool is_syntax_correct = true;
59-
bool is_strict_prologue;
60-
61-
// FIXME: Call parser
62-
JERRY_UNIMPLEMENTED ("eval operation is not implemented");
59+
parser_init ();
60+
bool is_syntax_correct = parser_parse_eval ((const char *) code_p, (size_t) buf_size);
61+
const opcode_t* opcodes_p = (const opcode_t*) serializer_get_bytecode ();
62+
serializer_print_opcodes ();
63+
parser_free ();
6364

6465
// FIXME:
65-
is_strict_prologue = false;
66+
bool is_strict_prologue = false;
67+
(void) is_strict_prologue;
6668

6769
if (!is_syntax_correct)
6870
{
@@ -95,6 +97,7 @@ ecma_op_eval (ecma_string_t *code_p, /**< code string */
9597
}
9698

9799
// FIXME: Call interpreter
100+
(void) opcodes_p;
98101
completion = ecma_make_return_completion_value (ecma_make_simple_value (ECMA_SIMPLE_VALUE_UNDEFINED));
99102
JERRY_UNIMPLEMENTED ("eval operation is not implemented");
100103

jerry-core/jerry.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,8 +1215,9 @@ jerry_parse (const char* source_p, /**< script source */
12151215

12161216
bool is_show_opcodes = ((jerry_flags & JERRY_FLAG_SHOW_OPCODES) != 0);
12171217

1218-
parser_init (source_p, source_size, is_show_opcodes);
1219-
parser_parse_program ();
1218+
parser_set_show_opcodes (is_show_opcodes);
1219+
parser_init ();
1220+
parser_parse_script (source_p, source_size);
12201221

12211222
const opcode_t* opcodes = (const opcode_t*) serializer_get_bytecode ();
12221223

jerry-core/parser/js/bytecode-data.h

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,30 @@
3434
*/
3535
#define BLOCK_SIZE 64
3636

37+
/**
38+
* Pointer to lit_id_hash_table precedes every independent bytecode region
39+
*/
40+
typedef struct __attribute__ ((aligned (MEM_ALIGNMENT)))
41+
{
42+
lit_id_hash_table *lit_id_hash;
43+
} opcodes_header_t;
44+
3745
typedef struct
3846
{
3947
const ecma_char_t *strings_buffer;
4048
const opcode_t *opcodes;
41-
lit_id_hash_table *lit_id_hash;
4249
opcode_counter_t opcodes_count;
4350
} bytecode_data_t;
4451

52+
/**
53+
* Macros to get a hash table corresponding to a bytecode region
54+
*/
55+
#define GET_HASH_TABLE_FOR_BYTECODE(opcodes) (((opcodes_header_t *) (((uint8_t *) (opcodes)) - \
56+
sizeof (opcodes_header_t)))->lit_id_hash)
57+
58+
/**
59+
* Macros to get a pointer to bytecode header by pointer to opcodes start
60+
*/
61+
#define GET_BYTECODE_HEADER(opcodes) ((opcodes_header_t *) (((uint8_t *) (opcodes)) - sizeof (opcodes_header_t)))
62+
4563
#endif // BYTECODE_DATA_H

jerry-core/parser/js/lexer.cpp

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,26 +1466,37 @@ lexer_are_tokens_with_same_identifier (token id1, /**< identifier token (TOK_NAM
14661466
return (id1.uid == id2.uid);
14671467
} /* lexer_are_tokens_with_same_identifier */
14681468

1469+
/**
1470+
* Initialize lexer to start parsing of a new source
1471+
*/
14691472
void
1470-
lexer_init (const char *source, size_t source_size, bool show_opcodes)
1473+
lexer_init_source (const char *source, /**< script source */
1474+
size_t source_size) /**< script source size in bytes */
1475+
{
1476+
saved_token = prev_token = sent_token = empty_token;
1477+
1478+
buffer_size = source_size;
1479+
lexer_set_source (source);
1480+
lexer_set_strict_mode (false);
1481+
} /* lexer_init_source */
1482+
1483+
/**
1484+
* Intitialize lexer
1485+
*/
1486+
void
1487+
lexer_init (bool show_opcodes) /**< flag indicating if to dump opcodes */
14711488
{
14721489
empty_token.type = TOK_EMPTY;
14731490
empty_token.uid = 0;
14741491
empty_token.loc = 0;
14751492

1476-
saved_token = prev_token = sent_token = empty_token;
1477-
14781493
#ifndef JERRY_NDEBUG
14791494
allow_dump_lines = show_opcodes;
14801495
#else /* JERRY_NDEBUG */
14811496
(void) show_opcodes;
14821497
allow_dump_lines = false;
14831498
#endif /* JERRY_NDEBUG */
1484-
1485-
buffer_size = source_size;
1486-
lexer_set_source (source);
1487-
lexer_set_strict_mode (false);
1488-
}
1499+
} /* lexer_init */
14891500

14901501
void
14911502
lexer_free (void)

jerry-core/parser/js/lexer.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "lit-literal.h"
2020

2121
#define INVALID_VALUE 255
22+
#define EVAL_RET_VALUE 128
2223
#define INVALID_LITERAL (rcs_cpointer_t::null_cp ())
2324

2425
/* Keywords. */
@@ -168,7 +169,8 @@ typedef struct
168169
*/
169170
#define TOKEN_EMPTY_INITIALIZER {0, TOK_EMPTY, 0}
170171

171-
void lexer_init (const char *, size_t, bool);
172+
void lexer_init (bool);
173+
void lexer_init_source (const char *, size_t);
172174
void lexer_free (void);
173175

174176
token lexer_next_token (void);

jerry-core/parser/js/opcodes-dumper.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,22 @@ literal_operand (lit_cpointer_t lit_cp)
695695
return ret;
696696
}
697697

698+
/**
699+
* Creates operand for eval's return value
700+
*
701+
* @return constructed operand
702+
*/
703+
operand
704+
eval_ret_operand (void)
705+
{
706+
operand ret;
707+
708+
ret.type = OPERAND_TMP;
709+
ret.data.uid = EVAL_RET_VALUE;
710+
711+
return ret;
712+
} /* eval_ret_operand */
713+
698714
bool
699715
operand_is_empty (operand op)
700716
{

jerry-core/parser/js/opcodes-dumper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ typedef enum __attr_packed___
4848

4949
operand empty_operand (void);
5050
operand literal_operand (lit_cpointer_t);
51+
operand eval_ret_operand (void);
5152
bool operand_is_empty (operand);
5253

5354
void dumper_init (void);

0 commit comments

Comments
 (0)