Skip to content

Commit 4a331b2

Browse files
authored
Add builtin GeneratorFunction support (#3499)
JerryScript-DCO-1.0-Signed-off-by: Robert Fancsik frobert@inf.u-szeged.hu
1 parent e3decff commit 4a331b2

24 files changed

Lines changed: 659 additions & 152 deletions

jerry-core/api/jerry-snapshot.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -990,8 +990,7 @@ jerry_snapshot_result (const uint32_t *snapshot_p, /**< snapshot */
990990
if (as_function)
991991
{
992992
ecma_object_t *lex_env_p = ecma_get_global_environment ();
993-
ecma_object_t *func_obj_p = ecma_op_create_function_object (lex_env_p,
994-
bytecode_p);
993+
ecma_object_t *func_obj_p = ecma_op_create_simple_function_object (lex_env_p, bytecode_p);
995994

996995
if (!(bytecode_p->status_flags & CBC_CODE_FLAGS_STATIC_FUNCTION))
997996
{

jerry-core/api/jerry.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -457,8 +457,7 @@ jerry_parse (const jerry_char_t *resource_name_p, /**< resource name (usually a
457457
ecma_free_value (parse_status);
458458

459459
ecma_object_t *lex_env_p = ecma_get_global_environment ();
460-
ecma_object_t *func_obj_p = ecma_op_create_function_object (lex_env_p,
461-
bytecode_data_p);
460+
ecma_object_t *func_obj_p = ecma_op_create_simple_function_object (lex_env_p, bytecode_data_p);
462461
ecma_bytecode_deref (bytecode_data_p);
463462

464463
return ecma_make_object_value (func_obj_p);
@@ -539,8 +538,7 @@ jerry_parse_function (const jerry_char_t *resource_name_p, /**< resource name (u
539538
ecma_free_value (parse_status);
540539

541540
ecma_object_t *lex_env_p = ecma_get_global_environment ();
542-
ecma_object_t *func_obj_p = ecma_op_create_function_object (lex_env_p,
543-
bytecode_data_p);
541+
ecma_object_t *func_obj_p = ecma_op_create_simple_function_object (lex_env_p, bytecode_data_p);
544542
ecma_bytecode_deref (bytecode_data_p);
545543

546544
return ecma_make_object_value (func_obj_p);

jerry-core/ecma/base/ecma-globals.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ typedef enum
108108
ECMA_PARSE_EVAL = (1u << 6), /**< eval is called */
109109
ECMA_PARSE_MODULE = (1u << 7), /**< module is parsed */
110110
ECMA_PARSE_FUNCTION = (1u << 8), /**< a function body is parsed or the code is inside a function */
111+
ECMA_PARSE_GENERATOR_FUNCTION = (1u << 9), /**< generator function is parsed */
111112
} ecma_parse_opts_t;
112113

113114
/**

jerry-core/ecma/base/ecma-init-finalize.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ ecma_init (void)
5757

5858
#if ENABLED (JERRY_ES2015)
5959
JERRY_CONTEXT (current_new_target) = JERRY_CONTEXT_INVALID_NEW_TARGET;
60+
JERRY_CONTEXT (current_function_obj_p) = NULL;
6061
#endif /* ENABLED (JERRY_ES2015) */
6162
} /* ecma_init */
6263

@@ -68,6 +69,7 @@ ecma_finalize (void)
6869
{
6970
#if ENABLED (JERRY_ES2015)
7071
JERRY_ASSERT (JERRY_CONTEXT (current_new_target) == JERRY_CONTEXT_INVALID_NEW_TARGET);
72+
JERRY_ASSERT (JERRY_CONTEXT (current_function_obj_p) == NULL);
7173
#endif /* ENABLED (JERRY_ES2015) */
7274

7375
ecma_finalize_global_lex_env ();

jerry-core/ecma/builtin-objects/ecma-builtin-function.c

Lines changed: 1 addition & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -59,58 +59,6 @@ ecma_builtin_function_dispatch_call (const ecma_value_t *arguments_list_p, /**<
5959
return ecma_builtin_function_dispatch_construct (arguments_list_p, arguments_list_len);
6060
} /* ecma_builtin_function_dispatch_call */
6161

62-
/**
63-
* Helper method to count and convert the arguments for the Function constructor call.
64-
*
65-
* Performs the operation described in ECMA 262 v5.1 15.3.2.1 steps 5.a-d
66-
*
67-
*
68-
* @return ecma value - concatenated arguments as a string.
69-
* Returned value must be freed with ecma_free_value.
70-
*/
71-
static ecma_value_t
72-
ecma_builtin_function_helper_get_function_arguments (const ecma_value_t *arguments_list_p, /**< arguments list */
73-
ecma_length_t arguments_list_len) /**< number of arguments */
74-
{
75-
JERRY_ASSERT (arguments_list_len == 0 || arguments_list_p != NULL);
76-
77-
if (arguments_list_len <= 1)
78-
{
79-
return ecma_make_magic_string_value (LIT_MAGIC_STRING__EMPTY);
80-
}
81-
82-
ecma_string_t *final_str_p = ecma_op_to_string (arguments_list_p[0]);
83-
84-
if (JERRY_UNLIKELY (final_str_p == NULL))
85-
{
86-
return ECMA_VALUE_ERROR;
87-
}
88-
89-
if (arguments_list_len == 2)
90-
{
91-
return ecma_make_string_value (final_str_p);
92-
}
93-
94-
for (ecma_length_t idx = 1; idx < arguments_list_len - 1; idx++)
95-
{
96-
ecma_string_t *new_str_p = ecma_op_to_string (arguments_list_p[idx]);
97-
98-
if (JERRY_UNLIKELY (new_str_p == NULL))
99-
{
100-
ecma_deref_ecma_string (final_str_p);
101-
return ECMA_VALUE_ERROR;
102-
}
103-
104-
final_str_p = ecma_append_magic_string_to_string (final_str_p,
105-
LIT_MAGIC_STRING_COMMA_CHAR);
106-
107-
final_str_p = ecma_concat_ecma_strings (final_str_p, new_str_p);
108-
ecma_deref_ecma_string (new_str_p);
109-
}
110-
111-
return ecma_make_string_value (final_str_p);
112-
} /* ecma_builtin_function_helper_get_function_arguments */
113-
11462
/**
11563
* Handle calling [[Construct]] of built-in Function object
11664
*
@@ -123,70 +71,7 @@ ecma_value_t
12371
ecma_builtin_function_dispatch_construct (const ecma_value_t *arguments_list_p, /**< arguments list */
12472
ecma_length_t arguments_list_len) /**< number of arguments */
12573
{
126-
JERRY_ASSERT (arguments_list_len == 0 || arguments_list_p != NULL);
127-
128-
ecma_value_t arguments_value = ecma_builtin_function_helper_get_function_arguments (arguments_list_p,
129-
arguments_list_len);
130-
131-
if (ECMA_IS_VALUE_ERROR (arguments_value))
132-
{
133-
return arguments_value;
134-
}
135-
136-
ecma_string_t *function_body_str_p;
137-
138-
if (arguments_list_len > 0)
139-
{
140-
function_body_str_p = ecma_op_to_string (arguments_list_p[arguments_list_len - 1]);
141-
142-
if (JERRY_UNLIKELY (function_body_str_p == NULL))
143-
{
144-
ecma_free_value (arguments_value);
145-
return ECMA_VALUE_ERROR;
146-
}
147-
}
148-
else
149-
{
150-
/* Very unlikely code path, not optimized. */
151-
function_body_str_p = ecma_get_magic_string (LIT_MAGIC_STRING__EMPTY);
152-
}
153-
154-
ecma_string_t *arguments_str_p = ecma_get_string_from_value (arguments_value);
155-
156-
ECMA_STRING_TO_UTF8_STRING (arguments_str_p, arguments_buffer_p, arguments_buffer_size);
157-
ECMA_STRING_TO_UTF8_STRING (function_body_str_p, function_body_buffer_p, function_body_buffer_size);
158-
159-
#if ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ERROR_MESSAGES) || ENABLED (JERRY_ES2015_MODULE_SYSTEM)
160-
JERRY_CONTEXT (resource_name) = ecma_make_magic_string_value (LIT_MAGIC_STRING_RESOURCE_ANON);
161-
#endif /* ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ERROR_MESSAGES) || ENABLED (JERRY_ES2015_MODULE_SYSTEM) */
162-
163-
ecma_compiled_code_t *bytecode_data_p = NULL;
164-
165-
ecma_value_t ret_value = parser_parse_script (arguments_buffer_p,
166-
arguments_buffer_size,
167-
function_body_buffer_p,
168-
function_body_buffer_size,
169-
ECMA_PARSE_NO_OPTS,
170-
&bytecode_data_p);
171-
172-
if (!ECMA_IS_VALUE_ERROR (ret_value))
173-
{
174-
JERRY_ASSERT (ecma_is_value_true (ret_value));
175-
176-
ecma_object_t *func_obj_p = ecma_op_create_function_object (ecma_get_global_environment (),
177-
bytecode_data_p);
178-
179-
ecma_bytecode_deref (bytecode_data_p);
180-
ret_value = ecma_make_object_value (func_obj_p);
181-
}
182-
183-
ECMA_FINALIZE_UTF8_STRING (function_body_buffer_p, function_body_buffer_size);
184-
ECMA_FINALIZE_UTF8_STRING (arguments_buffer_p, arguments_buffer_size);
185-
186-
ecma_deref_ecma_string (arguments_str_p);
187-
ecma_deref_ecma_string (function_body_str_p);
188-
189-
return ret_value;
74+
return ecma_op_create_dynamic_function (arguments_list_p, arguments_list_len, ECMA_PARSE_NO_OPTS);
19075
} /* ecma_builtin_function_dispatch_construct */
19176

19277
/**
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
16+
#include "ecma-globals.h"
17+
18+
#if ENABLED (JERRY_ES2015)
19+
20+
#define ECMA_BUILTINS_INTERNAL
21+
#include "ecma-builtins-internal.h"
22+
#include "ecma-function-object.h"
23+
24+
#define BUILTIN_INC_HEADER_NAME "ecma-builtin-generator-function.inc.h"
25+
#define BUILTIN_UNDERSCORED_ID generator_function
26+
#include "ecma-builtin-internal-routines-template.inc.h"
27+
28+
/** \addtogroup ecma ECMA
29+
* @{
30+
*
31+
* \addtogroup ecmabuiltins
32+
* @{
33+
*
34+
* \addtogroup generator ECMA GeneratorFunction object built-in
35+
* @{
36+
*/
37+
38+
/**
39+
* Handle calling [[Call]] of built-in GeneratorFunction object
40+
*
41+
* @return constructed generator function object - if success
42+
* raised error otherwise
43+
*/
44+
ecma_value_t
45+
ecma_builtin_generator_function_dispatch_call (const ecma_value_t *arguments_list_p, /**< arguments list */
46+
ecma_length_t arguments_list_len) /**< number of arguments */
47+
{
48+
JERRY_ASSERT (arguments_list_len == 0 || arguments_list_p != NULL);
49+
50+
return ecma_op_create_dynamic_function (arguments_list_p, arguments_list_len, ECMA_PARSE_GENERATOR_FUNCTION);
51+
} /* ecma_builtin_generator_function_dispatch_call */
52+
53+
/**
54+
* Handle calling [[Construct]] of built-in GeneratorFunction object
55+
*
56+
* @return constructed generator function object - if success
57+
* raised error otherwise
58+
*/
59+
ecma_value_t
60+
ecma_builtin_generator_function_dispatch_construct (const ecma_value_t *arguments_list_p, /**< arguments list */
61+
ecma_length_t arguments_list_len) /**< number of arguments */
62+
{
63+
return ecma_builtin_generator_function_dispatch_call (arguments_list_p, arguments_list_len);
64+
} /* ecma_builtin_generator_function_dispatch_construct */
65+
66+
/**
67+
* @}
68+
* @}
69+
* @}
70+
*/
71+
72+
#endif /* ENABLED (JERRY_ES2015) */
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
16+
/*
17+
* %GeneratorFunction% built-in description
18+
*/
19+
20+
#include "ecma-builtin-helpers-macro-defines.inc.h"
21+
22+
#if ENABLED (JERRY_ES2015)
23+
24+
/* ECMA-262 v6, 25.2.2 */
25+
STRING_VALUE (LIT_MAGIC_STRING_NAME,
26+
LIT_MAGIC_STRING_GENERATOR_FUNCTION_UL,
27+
ECMA_PROPERTY_FLAG_CONFIGURABLE)
28+
29+
/* ECMA-262 v6, 25.2.2 */
30+
NUMBER_VALUE (LIT_MAGIC_STRING_LENGTH,
31+
1,
32+
ECMA_PROPERTY_FLAG_CONFIGURABLE)
33+
34+
/* ECMA-262 v6, 25.2.2.2 */
35+
OBJECT_VALUE (LIT_MAGIC_STRING_PROTOTYPE,
36+
ECMA_BUILTIN_ID_GENERATOR,
37+
ECMA_PROPERTY_FIXED)
38+
39+
#endif /* ENABLED (JERRY_ES2015) */
40+
41+
#include "ecma-builtin-helpers-macro-undefs.inc.h"

jerry-core/ecma/builtin-objects/ecma-builtin-generator-prototype.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
* \addtogroup ecmabuiltins
3939
* @{
4040
*
41-
* \addtogroup generator ECMA Generator object built-in
41+
* \addtogroup generator ECMA Generator.prototype object built-in
4242
* @{
4343
*/
4444

jerry-core/ecma/builtin-objects/ecma-builtin-generator-prototype.inc.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ STRING_VALUE (LIT_GLOBAL_SYMBOL_TO_STRING_TAG,
2929
LIT_MAGIC_STRING_GENERATOR_UL,
3030
ECMA_PROPERTY_FLAG_CONFIGURABLE)
3131

32+
/* ECMA-262 v6, 25.2.3.1 */
33+
OBJECT_VALUE (LIT_MAGIC_STRING_CONSTRUCTOR,
34+
ECMA_BUILTIN_ID_GENERATOR,
35+
ECMA_PROPERTY_FLAG_CONFIGURABLE)
36+
3237
/* Routine properties:
3338
* (property name, C routine name, arguments number or NON_FIXED, value of the routine's length property) */
3439
ROUTINE (LIT_MAGIC_STRING_NEXT, ecma_builtin_generator_prototype_object_next, 1, 1)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
16+
#include "ecma-globals.h"
17+
18+
#if ENABLED (JERRY_ES2015)
19+
20+
#define ECMA_BUILTINS_INTERNAL
21+
#include "ecma-builtins-internal.h"
22+
23+
#define BUILTIN_INC_HEADER_NAME "ecma-builtin-generator.inc.h"
24+
#define BUILTIN_UNDERSCORED_ID generator
25+
#include "ecma-builtin-internal-routines-template.inc.h"
26+
27+
/** \addtogroup ecma ECMA
28+
* @{
29+
*
30+
* \addtogroup ecmabuiltins
31+
* @{
32+
*
33+
* \addtogroup generator ECMA Generator object built-in
34+
* @{
35+
*/
36+
37+
/**
38+
* @}
39+
* @}
40+
* @}
41+
*/
42+
43+
#endif /* ENABLED (JERRY_ES2015) */

0 commit comments

Comments
 (0)