Skip to content

Commit 507411f

Browse files
Implement parse of for-in statement.
JerryScript-DCO-1.0-Signed-off-by: Evgeny Gavrin e.gavrin@samsung.com JerryScript-DCO-1.0-Signed-off-by: Ruben Ayrapetyan r.ayrapetyan@samsung.com
1 parent b988fe2 commit 507411f

File tree

6 files changed

+539
-8
lines changed

6 files changed

+539
-8
lines changed

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

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,23 @@ eval_ret_operand (void)
708708
return ret;
709709
} /* eval_ret_operand */
710710

711+
/**
712+
* Creates operand for taking iterator value (next property name)
713+
* from for-in opcode handler.
714+
*
715+
* @return constructed operand
716+
*/
717+
operand
718+
jsp_create_operand_for_in_special_reg (void)
719+
{
720+
operand ret;
721+
722+
ret.type = OPERAND_TMP;
723+
ret.data.uid = OPCODE_REG_SPECIAL_FOR_IN_PROPERTY_NAME;
724+
725+
return ret;
726+
} /* jsp_create_operand_for_in_special_reg */
727+
711728
bool
712729
operand_is_empty (operand op)
713730
{
@@ -2362,6 +2379,62 @@ dump_with_end (void)
23622379
serializer_dump_op_meta (create_op_meta_000 (opcode));
23632380
} /* dump_with_end */
23642381

2382+
/**
2383+
* Dump template of 'for_in' instruction.
2384+
*
2385+
* Note:
2386+
* the instruction's flags field is written later (see also: rewrite_for_in).
2387+
*
2388+
* @return position of dumped instruction
2389+
*/
2390+
opcode_counter_t
2391+
dump_for_in_for_rewrite (operand op) /**< operand - result of evaluating Expression
2392+
* in for-in statement */
2393+
{
2394+
opcode_counter_t oc = serializer_get_current_opcode_counter ();
2395+
2396+
if (op.type == OPERAND_LITERAL)
2397+
{
2398+
const opcode_t opcode = getop_for_in (LITERAL_TO_REWRITE, INVALID_VALUE, INVALID_VALUE);
2399+
serializer_dump_op_meta (create_op_meta_100 (opcode, op.data.lit_id));
2400+
}
2401+
else
2402+
{
2403+
JERRY_ASSERT (op.type == OPERAND_TMP);
2404+
2405+
const opcode_t opcode = getop_for_in (op.data.uid, INVALID_VALUE, INVALID_VALUE);
2406+
serializer_dump_op_meta (create_op_meta_000 (opcode));
2407+
}
2408+
2409+
return oc;
2410+
} /* dump_for_in_for_rewrite */
2411+
2412+
/**
2413+
* Write position of 'for_in' block's end to specified 'for_in' instruction template,
2414+
* dumped earlier (see also: dump_for_in_for_rewrite).
2415+
*/
2416+
void
2417+
rewrite_for_in (opcode_counter_t oc) /**< opcode counter of the instruction template */
2418+
{
2419+
op_meta for_in_op_meta = serializer_get_op_meta (oc);
2420+
2421+
idx_t id1, id2;
2422+
split_opcode_counter (get_diff_from (oc), &id1, &id2);
2423+
for_in_op_meta.op.data.for_in.oc_idx_1 = id1;
2424+
for_in_op_meta.op.data.for_in.oc_idx_2 = id2;
2425+
serializer_rewrite_op_meta (oc, for_in_op_meta);
2426+
} /* rewrite_for_in */
2427+
2428+
/**
2429+
* Dump 'meta' instruction of 'end for_in' type
2430+
*/
2431+
void
2432+
dump_for_in_end (void)
2433+
{
2434+
const opcode_t opcode = getop_meta (OPCODE_META_TYPE_END_FOR_IN, INVALID_VALUE, INVALID_VALUE);
2435+
serializer_dump_op_meta (create_op_meta_000 (opcode));
2436+
} /* dump_for_in_end */
2437+
23652438
void
23662439
dump_try_for_rewrite (void)
23672440
{

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ typedef enum __attr_packed___
4949
operand empty_operand (void);
5050
operand literal_operand (lit_cpointer_t);
5151
operand eval_ret_operand (void);
52+
operand jsp_create_operand_for_in_special_reg (void);
5253
bool operand_is_empty (operand);
5354

5455
void dumper_init (void);
@@ -211,6 +212,10 @@ opcode_counter_t dump_with_for_rewrite (operand);
211212
void rewrite_with (opcode_counter_t);
212213
void dump_with_end (void);
213214

215+
opcode_counter_t dump_for_in_for_rewrite (operand);
216+
void rewrite_for_in (opcode_counter_t);
217+
void dump_for_in_end (void);
218+
214219
void dump_try_for_rewrite (void);
215220
void rewrite_try (void);
216221
void dump_catch_for_rewrite (operand);

jerry-core/parser/js/parser.cpp

Lines changed: 171 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1768,7 +1768,7 @@ parse_expression (bool in_allowed, /**< flag indicating if 'in' is allowed insid
17681768
initialiser
17691769
: '=' LT!* assignment_expression
17701770
; */
1771-
static void
1771+
static operand
17721772
parse_variable_declaration (void)
17731773
{
17741774
current_token_must_be (TOK_NAME);
@@ -1785,6 +1785,8 @@ parse_variable_declaration (void)
17851785
{
17861786
lexer_save_token (tok);
17871787
}
1788+
1789+
return name;
17881790
}
17891791

17901792
/* variable_declaration_list
@@ -1933,15 +1935,176 @@ jsp_parse_for_statement (jsp_label_t *outermost_stmt_label_p, /**< outermost (fi
19331935
}
19341936
} /* jsp_parse_for_statement */
19351937

1938+
/**
1939+
* Parse VariableDeclarationNoIn / LeftHandSideExpression (iterator part) of for-in statement
1940+
*
1941+
* See also:
1942+
* jsp_parse_for_in_statement
1943+
*
1944+
* @return true - if iterator consists of base and property name,
1945+
* false - otherwise, iterator consists of an identifier name (without base).
1946+
*/
1947+
static bool
1948+
jsp_parse_for_in_statement_iterator (operand *base_p, /**< out: base value of member expression, if any,
1949+
* empty operand - otherwise */
1950+
operand *identifier_p) /**< out: property name (if base value is not empty),
1951+
* identifier - otherwise */
1952+
{
1953+
JERRY_ASSERT (base_p != NULL);
1954+
JERRY_ASSERT (identifier_p != NULL);
1955+
1956+
if (is_keyword (KW_VAR))
1957+
{
1958+
skip_newlines ();
1959+
1960+
*base_p = empty_operand ();
1961+
*identifier_p = parse_variable_declaration ();
1962+
1963+
return false;
1964+
}
1965+
else
1966+
{
1967+
operand base, identifier;
1968+
1969+
/*
1970+
* FIXME:
1971+
* Remove evaluation of last part of identifier chain
1972+
*/
1973+
operand i = parse_left_hand_side_expression (&base, &identifier);
1974+
1975+
if (operand_is_empty (base))
1976+
{
1977+
*base_p = empty_operand ();
1978+
*identifier_p = i;
1979+
1980+
return false;
1981+
}
1982+
else
1983+
{
1984+
*base_p = base;
1985+
*identifier_p = identifier;
1986+
1987+
return true;
1988+
}
1989+
}
1990+
} /* jsp_parse_for_in_statement_iterator */
1991+
1992+
/**
1993+
* Parse for-in statement
1994+
*
1995+
* See also:
1996+
* ECMA-262 v5, 12.6.4
1997+
*
1998+
* Note:
1999+
* Syntax:
2000+
* Iterator Collection Body LoopEnd
2001+
* - for ( LeftHandSideExpression in Expression) Statement
2002+
* - for (var VariableDeclarationNoIn in Expression) Statement
2003+
*
2004+
* Note:
2005+
* Layout of generate byte-code is the following:
2006+
* tmp <- Collection (Expression)
2007+
* for_in instruction (tmp, opcode counter of for-in end mark)
2008+
* {
2009+
* Assignment of OPCODE_REG_SPECIAL_FOR_IN_PROPERTY_NAME to
2010+
* Iterator (VariableDeclarationNoIn / LeftHandSideExpression)
2011+
* }
2012+
* Body (Statement)
2013+
* ContinueTarget:
2014+
* meta (OPCODE_META_TYPE_END_FOR_IN)
2015+
*/
19362016
static void
1937-
parse_for_in (jsp_label_t *outermost_stmt_label_p) /**< outermost (first) label, corresponding to
1938-
* the statement (or NULL, if there are no named
1939-
* labels associated with the statement) */
2017+
jsp_parse_for_in_statement (jsp_label_t *outermost_stmt_label_p, /**< outermost (first) label,
2018+
* corresponding to the statement
2019+
* (or NULL, if there are no name
2020+
* labels associated with the statement) */
2021+
locus for_body_statement_loc) /**< locus of loop body statement */
19402022
{
1941-
(void) outermost_stmt_label_p;
2023+
jsp_label_raise_nested_jumpable_border ();
19422024

1943-
EMIT_SORRY ("'for in' loops are not supported yet");
1944-
}
2025+
current_token_must_be (TOK_OPEN_PAREN);
2026+
skip_newlines ();
2027+
2028+
// Save Iterator location
2029+
locus iterator_loc = tok.loc;
2030+
2031+
while (tok.loc < for_body_statement_loc)
2032+
{
2033+
if (jsp_find_next_token_before_the_locus (TOK_KEYWORD,
2034+
for_body_statement_loc,
2035+
true))
2036+
{
2037+
if (is_keyword (KW_IN))
2038+
{
2039+
break;
2040+
}
2041+
else
2042+
{
2043+
skip_token ();
2044+
}
2045+
}
2046+
else
2047+
{
2048+
EMIT_ERROR ("Invalid for statement");
2049+
}
2050+
}
2051+
2052+
JERRY_ASSERT (is_keyword (KW_IN));
2053+
skip_newlines ();
2054+
2055+
// Collection
2056+
operand collection = parse_expression (true, JSP_EVAL_RET_STORE_NOT_DUMP);
2057+
current_token_must_be (TOK_CLOSE_PAREN);
2058+
skip_token ();
2059+
2060+
// Dump for-in instruction
2061+
opcode_counter_t for_in_oc = dump_for_in_for_rewrite (collection);
2062+
2063+
// Dump assignment VariableDeclarationNoIn / LeftHandSideExpression <- OPCODE_REG_SPECIAL_FOR_IN_PROPERTY_NAME
2064+
lexer_seek (iterator_loc);
2065+
tok = lexer_next_token ();
2066+
2067+
operand iterator_base, iterator_identifier, for_in_special_reg;
2068+
for_in_special_reg = jsp_create_operand_for_in_special_reg ();
2069+
2070+
if (jsp_parse_for_in_statement_iterator (&iterator_base, &iterator_identifier))
2071+
{
2072+
dump_prop_setter (iterator_base, iterator_identifier, for_in_special_reg);
2073+
}
2074+
else
2075+
{
2076+
JERRY_ASSERT (operand_is_empty (iterator_base));
2077+
dump_variable_assignment (iterator_identifier, for_in_special_reg);
2078+
}
2079+
2080+
// Body
2081+
lexer_seek (for_body_statement_loc);
2082+
tok = lexer_next_token ();
2083+
2084+
parse_statement (NULL);
2085+
2086+
// Save LoopEnd locus
2087+
const locus loop_end_loc = tok.loc;
2088+
2089+
// Setup ContinueTarget
2090+
jsp_label_setup_continue_target (outermost_stmt_label_p,
2091+
serializer_get_current_opcode_counter ());
2092+
2093+
// Write position of for-in end to for_in instruction
2094+
rewrite_for_in (for_in_oc);
2095+
2096+
// Dump meta (OPCODE_META_TYPE_END_FOR_IN)
2097+
dump_for_in_end ();
2098+
2099+
lexer_seek (loop_end_loc);
2100+
tok = lexer_next_token ();
2101+
if (tok.type != TOK_CLOSE_BRACE)
2102+
{
2103+
lexer_save_token (tok);
2104+
}
2105+
2106+
jsp_label_remove_nested_jumpable_border ();
2107+
} /* jsp_parse_for_in_statement */
19452108

19462109
/**
19472110
* Parse for/for-in statements
@@ -1982,7 +2145,7 @@ jsp_parse_for_or_for_in_statement (jsp_label_t *outermost_stmt_label_p) /**< out
19822145
}
19832146
else
19842147
{
1985-
parse_for_in (outermost_stmt_label_p);
2148+
jsp_parse_for_in_statement (outermost_stmt_label_p, for_body_statement_loc);
19862149
}
19872150
} /* jsp_parse_for_or_for_in_statement */
19882151

jerry-core/parser/js/scopes-tree.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ generate_opcode (scopes_tree tree, opcode_counter_t opc_index, lit_id_hash_table
306306
case OPCODE (obj_decl):
307307
case OPCODE (this_binding):
308308
case OPCODE (with):
309+
case OPCODE (for_in):
309310
case OPCODE (throw_value):
310311
case OPCODE (is_true_jmp_up):
311312
case OPCODE (is_true_jmp_down):

jerry-core/vm/pretty-printer.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ pp_op_meta (const opcode_t *opcodes_p,
232232
PP_OP (delete_prop, "%s = delete %s.%s;");
233233
PP_OP (typeof, "%s = typeof %s;");
234234
PP_OP (with, "with (%s);");
235+
PP_OP (for_in, "for_in (%s);");
235236
case NAME_TO_ID (is_true_jmp_up): printf ("if (%s) goto %d;", VAR (1), oc - OC (2, 3)); break;
236237
case NAME_TO_ID (is_false_jmp_up): printf ("if (%s == false) goto %d;", VAR (1), oc - OC (2, 3)); break;
237238
case NAME_TO_ID (is_true_jmp_down): printf ("if (%s) goto %d;", VAR (1), oc + OC (2, 3)); break;
@@ -554,6 +555,11 @@ pp_op_meta (const opcode_t *opcodes_p,
554555
printf ("end with;");
555556
break;
556557
}
558+
case OPCODE_META_TYPE_END_FOR_IN:
559+
{
560+
printf ("end for-in;");
561+
break;
562+
}
557563
case OPCODE_META_TYPE_FUNCTION_END:
558564
{
559565
printf ("function end: %d;", oc + OC (2, 3));

0 commit comments

Comments
 (0)