Skip to content

Commit 5884d5e

Browse files
committed
Ensure that class extends value does not contain super reference
This patch fixes #2657. JerryScript-DCO-1.0-Signed-off-by: Robert Fancsik [email protected]
1 parent aae50c9 commit 5884d5e

File tree

4 files changed

+60
-30
lines changed

4 files changed

+60
-30
lines changed

jerry-core/parser/js/js-parser-expr.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,6 +1403,11 @@ parser_parse_unary_expression (parser_context_t *context_p, /**< context */
14031403
}
14041404
case LEXER_KEYW_SUPER:
14051405
{
1406+
if (context_p->status_flags & PARSER_CLASS_EXTENDS_EXPR)
1407+
{
1408+
parser_raise_error (context_p, PARSER_ERR_UNEXPECTED_SUPER_REFERENCE);
1409+
}
1410+
14061411
if ((lexer_check_next_character (context_p, LIT_CHAR_DOT)
14071412
|| lexer_check_next_character (context_p, LIT_CHAR_LEFT_SQUARE))
14081413
&& context_p->status_flags & (PARSER_CLASS_HAS_SUPER | PARSER_IS_ARROW_FUNCTION))

jerry-core/parser/js/js-parser-internal.h

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -39,40 +39,41 @@
3939
*/
4040
typedef enum
4141
{
42-
PARSER_IS_STRICT = (1u << 0), /**< strict mode code */
43-
PARSER_IS_FUNCTION = (1u << 1), /**< function body is parsed */
44-
PARSER_IS_CLOSURE = (1u << 2), /**< function body is encapsulated in {} block */
45-
PARSER_IS_FUNC_EXPRESSION = (1u << 3), /**< a function expression is parsed */
46-
PARSER_IS_PROPERTY_GETTER = (1u << 4), /**< a property getter function is parsed */
47-
PARSER_IS_PROPERTY_SETTER = (1u << 5), /**< a property setter function is parsed */
48-
PARSER_HAS_NON_STRICT_ARG = (1u << 7), /**< the function has arguments which
49-
* are not supported in strict mode */
50-
PARSER_ARGUMENTS_NEEDED = (1u << 8), /**< arguments object must be created */
51-
PARSER_ARGUMENTS_NOT_NEEDED = (1u << 9), /**< arguments object must NOT be created */
52-
PARSER_LEXICAL_ENV_NEEDED = (1u << 10), /**< lexical environment object must be created */
53-
PARSER_NO_REG_STORE = (1u << 11), /**< all local variables must be stored
54-
* in the lexical environment object */
55-
PARSER_INSIDE_WITH = (1u << 12), /**< code block is inside a with statement */
56-
PARSER_RESOLVE_BASE_FOR_CALLS = (1u << 13), /**< the this object must be resolved when
57-
* a function without a base object is called */
58-
PARSER_HAS_INITIALIZED_VARS = (1u << 14), /**< a CBC_INITIALIZE_VARS instruction must be emitted */
59-
PARSER_HAS_LATE_LIT_INIT = (1u << 15), /**< allocate memory for this string after
60-
* the local parser data is freed */
61-
PARSER_NO_END_LABEL = (1u << 16), /**< return instruction must be inserted
62-
* after the last byte code */
63-
PARSER_DEBUGGER_BREAKPOINT_APPENDED = (1u << 17), /**< pending (unsent) breakpoint
64-
* info is available */
42+
PARSER_IS_STRICT = (1u << 0), /**< strict mode code */
43+
PARSER_IS_FUNCTION = (1u << 1), /**< function body is parsed */
44+
PARSER_IS_CLOSURE = (1u << 2), /**< function body is encapsulated in {} block */
45+
PARSER_IS_FUNC_EXPRESSION = (1u << 3), /**< a function expression is parsed */
46+
PARSER_IS_PROPERTY_GETTER = (1u << 4), /**< a property getter function is parsed */
47+
PARSER_IS_PROPERTY_SETTER = (1u << 5), /**< a property setter function is parsed */
48+
PARSER_HAS_NON_STRICT_ARG = (1u << 7), /**< the function has arguments which
49+
* are not supported in strict mode */
50+
PARSER_ARGUMENTS_NEEDED = (1u << 8), /**< arguments object must be created */
51+
PARSER_ARGUMENTS_NOT_NEEDED = (1u << 9), /**< arguments object must NOT be created */
52+
PARSER_LEXICAL_ENV_NEEDED = (1u << 10), /**< lexical environment object must be created */
53+
PARSER_NO_REG_STORE = (1u << 11), /**< all local variables must be stored
54+
* in the lexical environment object */
55+
PARSER_INSIDE_WITH = (1u << 12), /**< code block is inside a with statement */
56+
PARSER_RESOLVE_BASE_FOR_CALLS = (1u << 13), /**< the this object must be resolved when
57+
* a function without a base object is called */
58+
PARSER_HAS_INITIALIZED_VARS = (1u << 14), /**< a CBC_INITIALIZE_VARS instruction must be emitted */
59+
PARSER_HAS_LATE_LIT_INIT = (1u << 15), /**< allocate memory for this string after
60+
* the local parser data is freed */
61+
PARSER_NO_END_LABEL = (1u << 16), /**< return instruction must be inserted
62+
* after the last byte code */
63+
PARSER_DEBUGGER_BREAKPOINT_APPENDED = (1u << 17), /**< pending (unsent) breakpoint
64+
* info is available */
6565
#ifndef CONFIG_DISABLE_ES2015_ARROW_FUNCTION
66-
PARSER_IS_ARROW_FUNCTION = (1u << 18), /**< an arrow function is parsed */
67-
PARSER_ARROW_PARSE_ARGS = (1u << 19), /**< parse the argument list of an arrow function */
66+
PARSER_IS_ARROW_FUNCTION = (1u << 18), /**< an arrow function is parsed */
67+
PARSER_ARROW_PARSE_ARGS = (1u << 19), /**< parse the argument list of an arrow function */
6868
#endif /* !CONFIG_DISABLE_ES2015_ARROW_FUNCTION */
6969
#ifndef CONFIG_DISABLE_ES2015_CLASS
7070
/* These three status flags must be in this order. See PARSER_CLASS_PARSE_OPTS_OFFSET. */
71-
PARSER_CLASS_CONSTRUCTOR = (1u << 20), /**< a class constructor is parsed (this value must be kept in
72-
* in sync with ECMA_PARSE_CLASS_CONSTRUCTOR) */
73-
PARSER_CLASS_HAS_SUPER = (1u << 21), /**< class has super reference */
74-
PARSER_CLASS_STATIC_FUNCTION = (1u << 22), /**< this function is a static class method */
75-
PARSER_CLASS_SUPER_PROP_REFERENCE = (1u << 23), /**< super property call or assignment */
71+
PARSER_CLASS_CONSTRUCTOR = (1u << 20), /**< a class constructor is parsed (this value must be kept in
72+
* in sync with ECMA_PARSE_CLASS_CONSTRUCTOR) */
73+
PARSER_CLASS_HAS_SUPER = (1u << 21), /**< class has super reference */
74+
PARSER_CLASS_STATIC_FUNCTION = (1u << 22), /**< this function is a static class method */
75+
PARSER_CLASS_SUPER_PROP_REFERENCE = (1u << 23), /**< super property call or assignment */
76+
PARSER_CLASS_EXTENDS_EXPR = PARSER_IS_FUNC_EXPRESSION /**< class extends expression is parsed */
7677
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
7778
} parser_general_flags_t;
7879

jerry-core/parser/js/js-parser-statm.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,11 +612,15 @@ parser_parse_super_class_context_start (parser_context_t *context_p) /**< contex
612612

613613
lexer_next_token (context_p);
614614

615+
context_p->status_flags |= PARSER_CLASS_EXTENDS_EXPR;
616+
615617
/* NOTE: Currently there is no proper way to check whether the currently parsed expression
616618
is a valid lefthand-side expression or not, so we do not throw syntax error and parse
617619
the class extending value as an expression. */
618620
parser_parse_expression (context_p, PARSE_EXPR | PARSE_EXPR_NO_COMMA);
619621

622+
context_p->status_flags &= (uint32_t) ~PARSER_CLASS_EXTENDS_EXPR;
623+
620624
#ifndef JERRY_NDEBUG
621625
PARSER_PLUS_EQUAL_U16 (context_p->context_stack_depth, PARSER_SUPER_CLASS_CONTEXT_STACK_ALLOCATION);
622626
#endif /* !JERRY_NDEBUG */
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright JS Foundation and other contributors, http://js.foundation
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
try {
16+
eval ("var Mixin1 = (superclass) => class extends super.lass {};");
17+
assert (false);
18+
} catch (e) {
19+
assert (e instanceof SyntaxError)
20+
}

0 commit comments

Comments
 (0)