1425bb815Sopenharmony_ci/* Copyright JS Foundation and other contributors, http://js.foundation
2425bb815Sopenharmony_ci *
3425bb815Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4425bb815Sopenharmony_ci * you may not use this file except in compliance with the License.
5425bb815Sopenharmony_ci * You may obtain a copy of the License at
6425bb815Sopenharmony_ci *
7425bb815Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8425bb815Sopenharmony_ci *
9425bb815Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10425bb815Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS
11425bb815Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12425bb815Sopenharmony_ci * See the License for the specific language governing permissions and
13425bb815Sopenharmony_ci * limitations under the License.
14425bb815Sopenharmony_ci */
15425bb815Sopenharmony_ci
16425bb815Sopenharmony_ci#ifndef ECMA_BUILTIN_HELPERS_H
17425bb815Sopenharmony_ci#define ECMA_BUILTIN_HELPERS_H
18425bb815Sopenharmony_ci
19425bb815Sopenharmony_ci#include "ecma-globals.h"
20425bb815Sopenharmony_ci#include "ecma-exceptions.h"
21425bb815Sopenharmony_ci#include "ecma-helpers.h"
22425bb815Sopenharmony_ci#include "ecma-regexp-object.h"
23425bb815Sopenharmony_ci
24425bb815Sopenharmony_ci/** \addtogroup ecma ECMA
25425bb815Sopenharmony_ci * @{
26425bb815Sopenharmony_ci *
27425bb815Sopenharmony_ci * \addtogroup ecmabuiltinhelpers ECMA builtin helper operations
28425bb815Sopenharmony_ci * @{
29425bb815Sopenharmony_ci */
30425bb815Sopenharmony_ci
31425bb815Sopenharmony_ci/**
32425bb815Sopenharmony_ci * Mode of string index routine.
33425bb815Sopenharmony_ci */
34425bb815Sopenharmony_citypedef enum
35425bb815Sopenharmony_ci{
36425bb815Sopenharmony_ci  /** These routines must be in this order */
37425bb815Sopenharmony_ci  ECMA_STRING_LAST_INDEX_OF, /**< String.lastIndexOf: ECMA-262 v5, 15.5.4.8 */
38425bb815Sopenharmony_ci  ECMA_STRING_INDEX_OF, /**< String.indexOf: ECMA-262 v5, 15.5.4.7 */
39425bb815Sopenharmony_ci  ECMA_STRING_STARTS_WITH, /**< String.startsWith: ECMA-262 v6, 21.1.3.18 */
40425bb815Sopenharmony_ci  ECMA_STRING_INCLUDES, /**< String.includes: ECMA-262 v6, 21.1.3.7 */
41425bb815Sopenharmony_ci  ECMA_STRING_ENDS_WITH /**< String.includes: ECMA-262 v6, 21.1.3.6 */
42425bb815Sopenharmony_ci} ecma_string_index_of_mode_t;
43425bb815Sopenharmony_ci
44425bb815Sopenharmony_ciecma_value_t
45425bb815Sopenharmony_ciecma_builtin_helper_object_to_string (const ecma_value_t this_arg);
46425bb815Sopenharmony_ciecma_string_t *
47425bb815Sopenharmony_ciecma_builtin_helper_get_to_locale_string_at_index (ecma_object_t *obj_p, uint32_t index);
48425bb815Sopenharmony_ciecma_value_t
49425bb815Sopenharmony_ciecma_builtin_helper_object_get_properties (ecma_object_t *obj_p, uint32_t opts);
50425bb815Sopenharmony_ciecma_value_t
51425bb815Sopenharmony_ciecma_builtin_helper_array_concat_value (ecma_object_t *obj_p, uint32_t *length_p, ecma_value_t value);
52425bb815Sopenharmony_ciuint32_t
53425bb815Sopenharmony_ciecma_builtin_helper_array_index_normalize (ecma_value_t arg, uint32_t length, uint32_t *number_p);
54425bb815Sopenharmony_ciuint32_t
55425bb815Sopenharmony_ciecma_builtin_helper_string_index_normalize (ecma_number_t index, uint32_t length, bool nan_to_zero);
56425bb815Sopenharmony_ciecma_value_t
57425bb815Sopenharmony_ciecma_builtin_helper_string_prototype_object_index_of (ecma_string_t *original_str_p, ecma_value_t arg1,
58425bb815Sopenharmony_ci                                                      ecma_value_t arg2, ecma_string_index_of_mode_t mode);
59425bb815Sopenharmony_cibool
60425bb815Sopenharmony_ciecma_builtin_helper_string_find_index (ecma_string_t *original_str_p, ecma_string_t *search_str_p, bool first_index,
61425bb815Sopenharmony_ci                                       ecma_length_t start_pos, ecma_length_t *ret_index_p);
62425bb815Sopenharmony_ciecma_value_t
63425bb815Sopenharmony_ciecma_builtin_helper_def_prop (ecma_object_t *obj_p, ecma_string_t *name_p, ecma_value_t value, uint32_t opts);
64425bb815Sopenharmony_ci
65425bb815Sopenharmony_ciecma_value_t
66425bb815Sopenharmony_ciecma_builtin_helper_def_prop_by_index (ecma_object_t *obj_p, uint32_t index, ecma_value_t value, uint32_t opts);
67425bb815Sopenharmony_ci
68425bb815Sopenharmony_ci/**
69425bb815Sopenharmony_ci * Context for replace substitutions
70425bb815Sopenharmony_ci */
71425bb815Sopenharmony_citypedef struct
72425bb815Sopenharmony_ci{
73425bb815Sopenharmony_ci  ecma_stringbuilder_t builder;      /**< result string builder */
74425bb815Sopenharmony_ci  const lit_utf8_byte_t *string_p;   /**< source string */
75425bb815Sopenharmony_ci  lit_utf8_size_t string_size;       /**< source string size */
76425bb815Sopenharmony_ci  const lit_utf8_byte_t *matched_p;  /**< matched string */
77425bb815Sopenharmony_ci  lit_utf8_size_t matched_size;      /**< matcehd string size */
78425bb815Sopenharmony_ci  lit_utf8_size_t match_byte_pos;    /**< byte position of the match in the source string */
79425bb815Sopenharmony_ci  ecma_length_t index;               /**< current match index */
80425bb815Sopenharmony_ci
81425bb815Sopenharmony_ci  /**
82425bb815Sopenharmony_ci   * Capture results
83425bb815Sopenharmony_ci   */
84425bb815Sopenharmony_ci  union
85425bb815Sopenharmony_ci  {
86425bb815Sopenharmony_ci#if ENABLED (JERRY_BUILTIN_REGEXP)
87425bb815Sopenharmony_ci    const ecma_regexp_capture_t *captures_p; /**< array of regexp capturing groups */
88425bb815Sopenharmony_ci#endif /* ENABLED (JERRY_BUILTIN_REGEXP) */
89425bb815Sopenharmony_ci    const ecma_collection_t *collection_p;   /**< collection of captured substrings */
90425bb815Sopenharmony_ci  } u;
91425bb815Sopenharmony_ci
92425bb815Sopenharmony_ci  uint32_t capture_count;            /**< number of captures in the capturing group array */
93425bb815Sopenharmony_ci  ecma_string_t *replace_str_p;      /**< replacement string */
94425bb815Sopenharmony_ci} ecma_replace_context_t;
95425bb815Sopenharmony_ci
96425bb815Sopenharmony_civoid
97425bb815Sopenharmony_ciecma_builtin_replace_substitute (ecma_replace_context_t *ctx_p);
98425bb815Sopenharmony_ci
99425bb815Sopenharmony_ci#if ENABLED (JERRY_ES2015)
100425bb815Sopenharmony_cibool
101425bb815Sopenharmony_ciecma_builtin_is_regexp_exec (ecma_extended_object_t *obj_p);
102425bb815Sopenharmony_ci#endif /* ENABLED (JERRY_ES2015) */
103425bb815Sopenharmony_ci
104425bb815Sopenharmony_ci#if ENABLED (JERRY_BUILTIN_DATE)
105425bb815Sopenharmony_ci
106425bb815Sopenharmony_ci/**
107425bb815Sopenharmony_ci * Time range defines for helper functions.
108425bb815Sopenharmony_ci *
109425bb815Sopenharmony_ci * See also:
110425bb815Sopenharmony_ci *          ECMA-262 v5, 15.9.1.1, 15.9.1.10
111425bb815Sopenharmony_ci */
112425bb815Sopenharmony_ci
113425bb815Sopenharmony_ci/** Hours in a day. */
114425bb815Sopenharmony_ci#define ECMA_DATE_HOURS_PER_DAY         ((ecma_number_t) 24)
115425bb815Sopenharmony_ci
116425bb815Sopenharmony_ci/** Minutes in an hour. */
117425bb815Sopenharmony_ci#define ECMA_DATE_MINUTES_PER_HOUR      ((ecma_number_t) 60)
118425bb815Sopenharmony_ci
119425bb815Sopenharmony_ci/** Seconds in a minute. */
120425bb815Sopenharmony_ci#define ECMA_DATE_SECONDS_PER_MINUTE    ((ecma_number_t) 60)
121425bb815Sopenharmony_ci
122425bb815Sopenharmony_ci/** Milliseconds in a second. */
123425bb815Sopenharmony_ci#define ECMA_DATE_MS_PER_SECOND         ((ecma_number_t) 1000)
124425bb815Sopenharmony_ci
125425bb815Sopenharmony_ci/** ECMA_DATE_MS_PER_MINUTE == 60000 */
126425bb815Sopenharmony_ci#define ECMA_DATE_MS_PER_MINUTE         (ECMA_DATE_MS_PER_SECOND * ECMA_DATE_SECONDS_PER_MINUTE)
127425bb815Sopenharmony_ci
128425bb815Sopenharmony_ci/** ECMA_DATE_MS_PER_HOUR == 3600000 */
129425bb815Sopenharmony_ci#define ECMA_DATE_MS_PER_HOUR           (ECMA_DATE_MS_PER_MINUTE * ECMA_DATE_MINUTES_PER_HOUR)
130425bb815Sopenharmony_ci
131425bb815Sopenharmony_ci/** ECMA_DATE_MS_PER_DAY == 86400000 */
132425bb815Sopenharmony_ci#define ECMA_DATE_MS_PER_DAY            (ECMA_DATE_MS_PER_HOUR * ECMA_DATE_HOURS_PER_DAY)
133425bb815Sopenharmony_ci
134425bb815Sopenharmony_ci/**
135425bb815Sopenharmony_ci * This gives a range of 8,640,000,000,000,000 milliseconds
136425bb815Sopenharmony_ci * to either side of 01 January, 1970 UTC.
137425bb815Sopenharmony_ci */
138425bb815Sopenharmony_ci#define ECMA_DATE_MAX_VALUE             8.64e15
139425bb815Sopenharmony_ci
140425bb815Sopenharmony_ci/**
141425bb815Sopenharmony_ci * Timezone type.
142425bb815Sopenharmony_ci */
143425bb815Sopenharmony_citypedef enum
144425bb815Sopenharmony_ci{
145425bb815Sopenharmony_ci  ECMA_DATE_UTC, /**< date vaule is in UTC */
146425bb815Sopenharmony_ci  ECMA_DATE_LOCAL /**< date vaule is in local time */
147425bb815Sopenharmony_ci} ecma_date_timezone_t;
148425bb815Sopenharmony_ci
149425bb815Sopenharmony_ci/* ecma-builtin-helpers-date.c */
150425bb815Sopenharmony_ciextern const char day_names_p[7][3];
151425bb815Sopenharmony_ciextern const char month_names_p[12][3];
152425bb815Sopenharmony_ci
153425bb815Sopenharmony_ciecma_number_t ecma_date_day (ecma_number_t time);
154425bb815Sopenharmony_ciecma_number_t ecma_date_time_within_day (ecma_number_t time);
155425bb815Sopenharmony_ciecma_number_t ecma_date_year_from_time (ecma_number_t time);
156425bb815Sopenharmony_ciecma_number_t ecma_date_month_from_time (ecma_number_t time);
157425bb815Sopenharmony_ciecma_number_t ecma_date_date_from_time (ecma_number_t time);
158425bb815Sopenharmony_ciecma_number_t ecma_date_week_day (ecma_number_t time);
159425bb815Sopenharmony_ciecma_number_t ecma_date_local_time_zone_adjustment (ecma_number_t time);
160425bb815Sopenharmony_ciecma_number_t ecma_date_utc (ecma_number_t time);
161425bb815Sopenharmony_ciecma_number_t ecma_date_hour_from_time (ecma_number_t time);
162425bb815Sopenharmony_ciecma_number_t ecma_date_min_from_time (ecma_number_t time);
163425bb815Sopenharmony_ciecma_number_t ecma_date_sec_from_time (ecma_number_t time);
164425bb815Sopenharmony_ciecma_number_t ecma_date_ms_from_time (ecma_number_t time);
165425bb815Sopenharmony_ciecma_number_t ecma_date_make_time (ecma_number_t hour, ecma_number_t min, ecma_number_t sec, ecma_number_t ms);
166425bb815Sopenharmony_ciecma_number_t ecma_date_make_day (ecma_number_t year, ecma_number_t month, ecma_number_t date);
167425bb815Sopenharmony_ciecma_number_t ecma_date_make_date (ecma_number_t day, ecma_number_t time);
168425bb815Sopenharmony_ciecma_number_t ecma_date_time_clip (ecma_number_t time);
169425bb815Sopenharmony_ciecma_number_t ecma_date_timezone_offset (ecma_number_t time);
170425bb815Sopenharmony_ci
171425bb815Sopenharmony_ciecma_value_t ecma_date_value_to_string (ecma_number_t datetime_number);
172425bb815Sopenharmony_ciecma_value_t ecma_date_value_to_utc_string (ecma_number_t datetime_number);
173425bb815Sopenharmony_ciecma_value_t ecma_date_value_to_iso_string (ecma_number_t datetime_number);
174425bb815Sopenharmony_ciecma_value_t ecma_date_value_to_date_string (ecma_number_t datetime_number);
175425bb815Sopenharmony_ciecma_value_t ecma_date_value_to_time_string (ecma_number_t datetime_number);
176425bb815Sopenharmony_ci
177425bb815Sopenharmony_ci#endif /* ENABLED (JERRY_BUILTIN_DATE) */
178425bb815Sopenharmony_ci
179425bb815Sopenharmony_ci#if ENABLED (JERRY_BUILTIN_JSON)
180425bb815Sopenharmony_ci/* ecma-builtin-helper-json.c */
181425bb815Sopenharmony_ci
182425bb815Sopenharmony_ci/**
183425bb815Sopenharmony_ci * Occurence stack item of JSON.stringify()
184425bb815Sopenharmony_ci */
185425bb815Sopenharmony_citypedef struct struct_ecma_json_occurence_stack_item_t
186425bb815Sopenharmony_ci{
187425bb815Sopenharmony_ci  struct struct_ecma_json_occurence_stack_item_t *next_p; /**< next stack item */
188425bb815Sopenharmony_ci  ecma_object_t *object_p; /**< current object */
189425bb815Sopenharmony_ci} ecma_json_occurence_stack_item_t;
190425bb815Sopenharmony_ci
191425bb815Sopenharmony_ci/**
192425bb815Sopenharmony_ci * Context for JSON.stringify()
193425bb815Sopenharmony_ci */
194425bb815Sopenharmony_citypedef struct
195425bb815Sopenharmony_ci{
196425bb815Sopenharmony_ci  /** Collection for property keys. */
197425bb815Sopenharmony_ci  ecma_collection_t *property_list_p;
198425bb815Sopenharmony_ci
199425bb815Sopenharmony_ci  /** Collection for traversing objects. */
200425bb815Sopenharmony_ci  ecma_json_occurence_stack_item_t *occurence_stack_last_p;
201425bb815Sopenharmony_ci
202425bb815Sopenharmony_ci  /** The actual indentation text. */
203425bb815Sopenharmony_ci  ecma_stringbuilder_t indent_builder;
204425bb815Sopenharmony_ci
205425bb815Sopenharmony_ci  /** The indentation text. */
206425bb815Sopenharmony_ci  ecma_string_t *gap_str_p;
207425bb815Sopenharmony_ci
208425bb815Sopenharmony_ci  /** The replacer function. */
209425bb815Sopenharmony_ci  ecma_object_t *replacer_function_p;
210425bb815Sopenharmony_ci
211425bb815Sopenharmony_ci  /** Result string builder. */
212425bb815Sopenharmony_ci  ecma_stringbuilder_t result_builder;
213425bb815Sopenharmony_ci} ecma_json_stringify_context_t;
214425bb815Sopenharmony_ci
215425bb815Sopenharmony_ciecma_value_t ecma_builtin_json_parse_buffer (const lit_utf8_byte_t * str_start_p,
216425bb815Sopenharmony_ci                                             lit_utf8_size_t string_size);
217425bb815Sopenharmony_ciecma_value_t ecma_builtin_json_string_from_object (const ecma_value_t arg1);
218425bb815Sopenharmony_cibool ecma_json_has_object_in_stack (ecma_json_occurence_stack_item_t *stack_p, ecma_object_t *object_p);
219425bb815Sopenharmony_cibool ecma_has_string_value_in_collection (ecma_collection_t *collection_p, ecma_string_t *string_p);
220425bb815Sopenharmony_ci
221425bb815Sopenharmony_ciecma_value_t
222425bb815Sopenharmony_ciecma_builtin_helper_json_create_non_formatted_json (lit_utf8_byte_t left_bracket, lit_utf8_byte_t right_bracket,
223425bb815Sopenharmony_ci                                                    ecma_collection_t *partial_p);
224425bb815Sopenharmony_ci#endif /* ENABLED (JERRY_BUILTIN_JSON) */
225425bb815Sopenharmony_ci
226425bb815Sopenharmony_ci/* ecma-builtin-helper-error.c */
227425bb815Sopenharmony_ci
228425bb815Sopenharmony_ciecma_value_t
229425bb815Sopenharmony_ciecma_builtin_helper_error_dispatch_call (ecma_standard_error_t error_type, const ecma_value_t *arguments_list_p,
230425bb815Sopenharmony_ci                                         ecma_length_t arguments_list_len);
231425bb815Sopenharmony_ci
232425bb815Sopenharmony_ci/* ecma-builtin-helpers-sort.c */
233425bb815Sopenharmony_ci
234425bb815Sopenharmony_ci/**
235425bb815Sopenharmony_ci * Comparison callback function header for sorting helper routines.
236425bb815Sopenharmony_ci */
237425bb815Sopenharmony_citypedef ecma_value_t (*ecma_builtin_helper_sort_compare_fn_t) (ecma_value_t lhs, /**< left value */
238425bb815Sopenharmony_ci                                                               ecma_value_t rhs, /**< right value */
239425bb815Sopenharmony_ci                                                               ecma_value_t compare_func); /**< compare function */
240425bb815Sopenharmony_ci
241425bb815Sopenharmony_ciecma_value_t ecma_builtin_helper_array_heap_sort_helper (ecma_value_t *array_p,
242425bb815Sopenharmony_ci                                                         uint32_t right,
243425bb815Sopenharmony_ci                                                         ecma_value_t compare_func,
244425bb815Sopenharmony_ci                                                         const ecma_builtin_helper_sort_compare_fn_t sort_cb);
245425bb815Sopenharmony_ci
246425bb815Sopenharmony_ci/**
247425bb815Sopenharmony_ci * @}
248425bb815Sopenharmony_ci * @}
249425bb815Sopenharmony_ci */
250425bb815Sopenharmony_ci
251425bb815Sopenharmony_ci#endif /* !ECMA_BUILTIN_HELPERS_H */
252