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#ifndef ECMA_BUILTIN_HELPERS_H
17#define ECMA_BUILTIN_HELPERS_H
18
19#include "ecma-globals.h"
20#include "ecma-exceptions.h"
21#include "ecma-helpers.h"
22#include "ecma-regexp-object.h"
23
24/** \addtogroup ecma ECMA
25 * @{
26 *
27 * \addtogroup ecmabuiltinhelpers ECMA builtin helper operations
28 * @{
29 */
30
31/**
32 * Mode of string index routine.
33 */
34typedef enum
35{
36  /** These routines must be in this order */
37  ECMA_STRING_LAST_INDEX_OF, /**< String.lastIndexOf: ECMA-262 v5, 15.5.4.8 */
38  ECMA_STRING_INDEX_OF, /**< String.indexOf: ECMA-262 v5, 15.5.4.7 */
39  ECMA_STRING_STARTS_WITH, /**< String.startsWith: ECMA-262 v6, 21.1.3.18 */
40  ECMA_STRING_INCLUDES, /**< String.includes: ECMA-262 v6, 21.1.3.7 */
41  ECMA_STRING_ENDS_WITH /**< String.includes: ECMA-262 v6, 21.1.3.6 */
42} ecma_string_index_of_mode_t;
43
44ecma_value_t
45ecma_builtin_helper_object_to_string (const ecma_value_t this_arg);
46ecma_string_t *
47ecma_builtin_helper_get_to_locale_string_at_index (ecma_object_t *obj_p, uint32_t index);
48ecma_value_t
49ecma_builtin_helper_object_get_properties (ecma_object_t *obj_p, uint32_t opts);
50ecma_value_t
51ecma_builtin_helper_array_concat_value (ecma_object_t *obj_p, uint32_t *length_p, ecma_value_t value);
52uint32_t
53ecma_builtin_helper_array_index_normalize (ecma_value_t arg, uint32_t length, uint32_t *number_p);
54uint32_t
55ecma_builtin_helper_string_index_normalize (ecma_number_t index, uint32_t length, bool nan_to_zero);
56ecma_value_t
57ecma_builtin_helper_string_prototype_object_index_of (ecma_string_t *original_str_p, ecma_value_t arg1,
58                                                      ecma_value_t arg2, ecma_string_index_of_mode_t mode);
59bool
60ecma_builtin_helper_string_find_index (ecma_string_t *original_str_p, ecma_string_t *search_str_p, bool first_index,
61                                       ecma_length_t start_pos, ecma_length_t *ret_index_p);
62ecma_value_t
63ecma_builtin_helper_def_prop (ecma_object_t *obj_p, ecma_string_t *name_p, ecma_value_t value, uint32_t opts);
64
65ecma_value_t
66ecma_builtin_helper_def_prop_by_index (ecma_object_t *obj_p, uint32_t index, ecma_value_t value, uint32_t opts);
67
68/**
69 * Context for replace substitutions
70 */
71typedef struct
72{
73  ecma_stringbuilder_t builder;      /**< result string builder */
74  const lit_utf8_byte_t *string_p;   /**< source string */
75  lit_utf8_size_t string_size;       /**< source string size */
76  const lit_utf8_byte_t *matched_p;  /**< matched string */
77  lit_utf8_size_t matched_size;      /**< matcehd string size */
78  lit_utf8_size_t match_byte_pos;    /**< byte position of the match in the source string */
79  ecma_length_t index;               /**< current match index */
80
81  /**
82   * Capture results
83   */
84  union
85  {
86#if ENABLED (JERRY_BUILTIN_REGEXP)
87    const ecma_regexp_capture_t *captures_p; /**< array of regexp capturing groups */
88#endif /* ENABLED (JERRY_BUILTIN_REGEXP) */
89    const ecma_collection_t *collection_p;   /**< collection of captured substrings */
90  } u;
91
92  uint32_t capture_count;            /**< number of captures in the capturing group array */
93  ecma_string_t *replace_str_p;      /**< replacement string */
94} ecma_replace_context_t;
95
96void
97ecma_builtin_replace_substitute (ecma_replace_context_t *ctx_p);
98
99#if ENABLED (JERRY_ES2015)
100bool
101ecma_builtin_is_regexp_exec (ecma_extended_object_t *obj_p);
102#endif /* ENABLED (JERRY_ES2015) */
103
104#if ENABLED (JERRY_BUILTIN_DATE)
105
106/**
107 * Time range defines for helper functions.
108 *
109 * See also:
110 *          ECMA-262 v5, 15.9.1.1, 15.9.1.10
111 */
112
113/** Hours in a day. */
114#define ECMA_DATE_HOURS_PER_DAY         ((ecma_number_t) 24)
115
116/** Minutes in an hour. */
117#define ECMA_DATE_MINUTES_PER_HOUR      ((ecma_number_t) 60)
118
119/** Seconds in a minute. */
120#define ECMA_DATE_SECONDS_PER_MINUTE    ((ecma_number_t) 60)
121
122/** Milliseconds in a second. */
123#define ECMA_DATE_MS_PER_SECOND         ((ecma_number_t) 1000)
124
125/** ECMA_DATE_MS_PER_MINUTE == 60000 */
126#define ECMA_DATE_MS_PER_MINUTE         (ECMA_DATE_MS_PER_SECOND * ECMA_DATE_SECONDS_PER_MINUTE)
127
128/** ECMA_DATE_MS_PER_HOUR == 3600000 */
129#define ECMA_DATE_MS_PER_HOUR           (ECMA_DATE_MS_PER_MINUTE * ECMA_DATE_MINUTES_PER_HOUR)
130
131/** ECMA_DATE_MS_PER_DAY == 86400000 */
132#define ECMA_DATE_MS_PER_DAY            (ECMA_DATE_MS_PER_HOUR * ECMA_DATE_HOURS_PER_DAY)
133
134/**
135 * This gives a range of 8,640,000,000,000,000 milliseconds
136 * to either side of 01 January, 1970 UTC.
137 */
138#define ECMA_DATE_MAX_VALUE             8.64e15
139
140/**
141 * Timezone type.
142 */
143typedef enum
144{
145  ECMA_DATE_UTC, /**< date vaule is in UTC */
146  ECMA_DATE_LOCAL /**< date vaule is in local time */
147} ecma_date_timezone_t;
148
149/* ecma-builtin-helpers-date.c */
150extern const char day_names_p[7][3];
151extern const char month_names_p[12][3];
152
153ecma_number_t ecma_date_day (ecma_number_t time);
154ecma_number_t ecma_date_time_within_day (ecma_number_t time);
155ecma_number_t ecma_date_year_from_time (ecma_number_t time);
156ecma_number_t ecma_date_month_from_time (ecma_number_t time);
157ecma_number_t ecma_date_date_from_time (ecma_number_t time);
158ecma_number_t ecma_date_week_day (ecma_number_t time);
159ecma_number_t ecma_date_local_time_zone_adjustment (ecma_number_t time);
160ecma_number_t ecma_date_utc (ecma_number_t time);
161ecma_number_t ecma_date_hour_from_time (ecma_number_t time);
162ecma_number_t ecma_date_min_from_time (ecma_number_t time);
163ecma_number_t ecma_date_sec_from_time (ecma_number_t time);
164ecma_number_t ecma_date_ms_from_time (ecma_number_t time);
165ecma_number_t ecma_date_make_time (ecma_number_t hour, ecma_number_t min, ecma_number_t sec, ecma_number_t ms);
166ecma_number_t ecma_date_make_day (ecma_number_t year, ecma_number_t month, ecma_number_t date);
167ecma_number_t ecma_date_make_date (ecma_number_t day, ecma_number_t time);
168ecma_number_t ecma_date_time_clip (ecma_number_t time);
169ecma_number_t ecma_date_timezone_offset (ecma_number_t time);
170
171ecma_value_t ecma_date_value_to_string (ecma_number_t datetime_number);
172ecma_value_t ecma_date_value_to_utc_string (ecma_number_t datetime_number);
173ecma_value_t ecma_date_value_to_iso_string (ecma_number_t datetime_number);
174ecma_value_t ecma_date_value_to_date_string (ecma_number_t datetime_number);
175ecma_value_t ecma_date_value_to_time_string (ecma_number_t datetime_number);
176
177#endif /* ENABLED (JERRY_BUILTIN_DATE) */
178
179#if ENABLED (JERRY_BUILTIN_JSON)
180/* ecma-builtin-helper-json.c */
181
182/**
183 * Occurence stack item of JSON.stringify()
184 */
185typedef struct struct_ecma_json_occurence_stack_item_t
186{
187  struct struct_ecma_json_occurence_stack_item_t *next_p; /**< next stack item */
188  ecma_object_t *object_p; /**< current object */
189} ecma_json_occurence_stack_item_t;
190
191/**
192 * Context for JSON.stringify()
193 */
194typedef struct
195{
196  /** Collection for property keys. */
197  ecma_collection_t *property_list_p;
198
199  /** Collection for traversing objects. */
200  ecma_json_occurence_stack_item_t *occurence_stack_last_p;
201
202  /** The actual indentation text. */
203  ecma_stringbuilder_t indent_builder;
204
205  /** The indentation text. */
206  ecma_string_t *gap_str_p;
207
208  /** The replacer function. */
209  ecma_object_t *replacer_function_p;
210
211  /** Result string builder. */
212  ecma_stringbuilder_t result_builder;
213} ecma_json_stringify_context_t;
214
215ecma_value_t ecma_builtin_json_parse_buffer (const lit_utf8_byte_t * str_start_p,
216                                             lit_utf8_size_t string_size);
217ecma_value_t ecma_builtin_json_string_from_object (const ecma_value_t arg1);
218bool ecma_json_has_object_in_stack (ecma_json_occurence_stack_item_t *stack_p, ecma_object_t *object_p);
219bool ecma_has_string_value_in_collection (ecma_collection_t *collection_p, ecma_string_t *string_p);
220
221ecma_value_t
222ecma_builtin_helper_json_create_non_formatted_json (lit_utf8_byte_t left_bracket, lit_utf8_byte_t right_bracket,
223                                                    ecma_collection_t *partial_p);
224#endif /* ENABLED (JERRY_BUILTIN_JSON) */
225
226/* ecma-builtin-helper-error.c */
227
228ecma_value_t
229ecma_builtin_helper_error_dispatch_call (ecma_standard_error_t error_type, const ecma_value_t *arguments_list_p,
230                                         ecma_length_t arguments_list_len);
231
232/* ecma-builtin-helpers-sort.c */
233
234/**
235 * Comparison callback function header for sorting helper routines.
236 */
237typedef ecma_value_t (*ecma_builtin_helper_sort_compare_fn_t) (ecma_value_t lhs, /**< left value */
238                                                               ecma_value_t rhs, /**< right value */
239                                                               ecma_value_t compare_func); /**< compare function */
240
241ecma_value_t ecma_builtin_helper_array_heap_sort_helper (ecma_value_t *array_p,
242                                                         uint32_t right,
243                                                         ecma_value_t compare_func,
244                                                         const ecma_builtin_helper_sort_compare_fn_t sort_cb);
245
246/**
247 * @}
248 * @}
249 */
250
251#endif /* !ECMA_BUILTIN_HELPERS_H */
252