Skip to content

Commit 003694d

Browse files
authored
Throw error for generator function class constructor (#3489)
JerryScript-DCO-1.0-Signed-off-by: Robert Fancsik frobert@inf.u-szeged.hu
1 parent 22e52e4 commit 003694d

4 files changed

Lines changed: 40 additions & 4 deletions

File tree

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -569,11 +569,20 @@ parser_parse_class_literal (parser_context_t *context_p) /**< context */
569569
{
570570
is_computed = true;
571571
}
572-
else if ((status_flags & PARSER_CLASS_STATIC_FUNCTION)
573-
&& LEXER_IS_IDENT_OR_STRING (context_p->token.lit_location.type)
574-
&& lexer_compare_literal_to_string (context_p, "prototype", 9))
572+
else if (LEXER_IS_IDENT_OR_STRING (context_p->token.lit_location.type))
575573
{
576-
parser_raise_error (context_p, PARSER_ERR_CLASS_STATIC_PROTOTYPE);
574+
if (status_flags & PARSER_CLASS_STATIC_FUNCTION)
575+
{
576+
if (lexer_compare_literal_to_string (context_p, "prototype", 9))
577+
{
578+
parser_raise_error (context_p, PARSER_ERR_CLASS_STATIC_PROTOTYPE);
579+
}
580+
}
581+
else if ((status_flags & PARSER_IS_GENERATOR_FUNCTION)
582+
&& lexer_compare_literal_to_string (context_p, "constructor", 11))
583+
{
584+
parser_raise_error (context_p, PARSER_ERR_CLASS_CONSTRUCTOR_AS_GENERATOR);
585+
}
577586
}
578587

579588
parse_class_method:

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,6 +1133,10 @@ parser_error_to_string (parser_error_t error) /**< error code */
11331133
{
11341134
return "Class constructor may not be an accessor.";
11351135
}
1136+
case PARSER_ERR_CLASS_CONSTRUCTOR_AS_GENERATOR:
1137+
{
1138+
return "Class constructor may not be a generator.";
1139+
}
11361140
case PARSER_ERR_CLASS_STATIC_PROTOTYPE:
11371141
{
11381142
return "Classes may not have a static property called 'prototype'.";

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

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

131131
PARSER_ERR_MULTIPLE_CLASS_CONSTRUCTORS, /**< multiple class constructor */
132132
PARSER_ERR_CLASS_CONSTRUCTOR_AS_ACCESSOR, /**< class constructor cannot be an accessor */
133+
PARSER_ERR_CLASS_CONSTRUCTOR_AS_GENERATOR, /**< class constructor cannot be a generator */
133134
PARSER_ERR_CLASS_STATIC_PROTOTYPE, /**< static method name 'prototype' is not allowed */
134135
PARSER_ERR_UNEXPECTED_SUPER_REFERENCE, /**< unexpected super keyword */
135136

tests/jerry/es2015/class.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ must_throw("class X {}; var o = {}; Object.defineProperty(o, 'p', { get: X, set:
5656
must_throw("var a = new A; class A {};");
5757
must_throw("class A { g\\u0065t e() {} }");
5858
must_throw('class A { "static" e() {} }');
59+
must_throw('class A { *constructor() {} }');
5960

6061
assert(eval("class A {}") === undefined);
6162
assert(eval("var a = class A {}") === undefined);
@@ -64,6 +65,22 @@ assert(eval("class A { ; ; ; ;;;;;;;;;;;; ; ; ;;;;;;;;;;;;;;;;;;;;;;;;; }") ===
6465
assert(eval('class A {"constructor"() {} }') === undefined);
6566
assert(isNaN (eval('switch(1) { default: (class A{} % 1) }')));
6667

68+
class A1 {
69+
["constructor"]() {
70+
return 5;
71+
}
72+
}
73+
74+
assert ((new A1).constructor() === 5);
75+
76+
class A2 {
77+
*["constructor"]() {
78+
yield 5;
79+
}
80+
}
81+
82+
assert ((new A2).constructor().next().value === 5);
83+
6784
class B {
6885
}
6986

@@ -90,6 +107,10 @@ class C {
90107
return() {
91108
return 43;
92109
}
110+
111+
static *constructor() {
112+
return 44;
113+
}
93114
}
94115

95116
var c = new C;
@@ -99,6 +120,7 @@ assert (c["3"]() === 3);
99120
assert (c.super() === 42);
100121
assert (c.return() === 43);
101122
assert (c.constructor === C);
123+
assert (C.constructor().next().value === 44);
102124

103125
class D {
104126
constructor(d) {

0 commit comments

Comments
 (0)