From fcebf654cc4f321d0ccbd03d7f178bee3284bede Mon Sep 17 00:00:00 2001 From: Hanjoung Lee Date: Tue, 13 Oct 2015 20:01:26 +0900 Subject: [PATCH] Fix: parser bug in parse_switch_statement JerryScript-DCO-1.0-Signed-off-by: Hanjoung Lee hanjoung.lee@samsung.com --- jerry-core/parser/js/parser.cpp | 41 ++++++++++++------------ tests/jerry/regression-test-issue-653.js | 22 +++++++++++++ tests/jerry/regression-test-issue-654.js | 19 +++++++++++ 3 files changed, 62 insertions(+), 20 deletions(-) create mode 100644 tests/jerry/regression-test-issue-653.js create mode 100644 tests/jerry/regression-test-issue-654.js diff --git a/jerry-core/parser/js/parser.cpp b/jerry-core/parser/js/parser.cpp index 0e634bb5f..36d73400c 100644 --- a/jerry-core/parser/js/parser.cpp +++ b/jerry-core/parser/js/parser.cpp @@ -2429,6 +2429,9 @@ parse_switch_statement (void) start_dumping_case_clauses (); const locus start_loc = tok.loc; bool was_default = false; + size_t default_body_index = 0; + array_list body_locs = array_list_init (sizeof (locus)); + // First, generate table of jumps skip_newlines (); while (is_keyword (KW_CASE) || is_keyword (KW_DEFAULT)) @@ -2440,6 +2443,7 @@ parse_switch_statement (void) next_token_must_be (TOK_COLON); dump_case_clause_check_for_rewrite (switch_expr, case_expr); skip_newlines (); + array_list_append (body_locs, (void*) &tok.loc); skip_case_clause_body (); } else if (is_keyword (KW_DEFAULT)) @@ -2451,6 +2455,8 @@ parse_switch_statement (void) was_default = true; token_after_newlines_must_be (TOK_COLON); skip_newlines (); + default_body_index = array_list_len (body_locs); + array_list_append (body_locs, (void*) &tok.loc); skip_case_clause_body (); } } @@ -2471,34 +2477,28 @@ parse_switch_statement (void) // Second, parse case clauses' bodies and rewrite jumps skip_newlines (); - while (is_keyword (KW_CASE) || is_keyword (KW_DEFAULT)) + for (size_t i = 0; i < array_list_len (body_locs); i++) { - if (is_keyword (KW_CASE)) + locus loc = * (locus*) array_list_element (body_locs, i); + lexer_seek (loc); + skip_newlines (); + if (was_default && default_body_index == i) { - while (!token_is (TOK_COLON)) - { - skip_newlines (); - } - rewrite_case_clause (); - skip_newlines (); - if (is_keyword (KW_CASE) || is_keyword (KW_DEFAULT)) - { - continue; - } - parse_statement_list (); - } - else if (is_keyword (KW_DEFAULT)) - { - token_after_newlines_must_be (TOK_COLON); - skip_newlines (); rewrite_default_clause (); if (is_keyword (KW_CASE)) { continue; } - parse_statement_list (); - continue; } + else + { + rewrite_case_clause (); + if (is_keyword (KW_CASE) || is_keyword (KW_DEFAULT)) + { + continue; + } + } + parse_statement_list (); skip_newlines (); } current_token_must_be (TOK_CLOSE_BRACE); @@ -2508,6 +2508,7 @@ parse_switch_statement (void) serializer_get_current_instr_counter ()); finish_dumping_case_clauses (); + array_list_free (body_locs); } /* catch_clause diff --git a/tests/jerry/regression-test-issue-653.js b/tests/jerry/regression-test-issue-653.js new file mode 100644 index 000000000..6b2919047 --- /dev/null +++ b/tests/jerry/regression-test-issue-653.js @@ -0,0 +1,22 @@ +// Copyright 2015 Samsung Electronics Co., Ltd. +// Copyright 2015 University of Szeged. +// +// 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 a = "foo", r; +switch(a) { + case true ? "foo" : "bar": + r = "OK"; + break; +} +assert(r === "OK"); diff --git a/tests/jerry/regression-test-issue-654.js b/tests/jerry/regression-test-issue-654.js new file mode 100644 index 000000000..3bb5dac28 --- /dev/null +++ b/tests/jerry/regression-test-issue-654.js @@ -0,0 +1,19 @@ +// Copyright 2015 Samsung Electronics Co., Ltd. +// Copyright 2015 University of Szeged. +// +// 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. + +switch (true) { + case {"foo": "bar"}: + break; +}