ecma-builtin-helpers.c 27.2 KB
Newer Older
1
/* Copyright JS Foundation and other contributors, http://js.foundation
P
Peter Gal 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "ecma-builtin-helpers.h"

18
#include "ecma-alloc.h"
19
#include "ecma-array-object.h"
P
Peter Gal 已提交
20 21
#include "ecma-builtins.h"
#include "ecma-conversion.h"
22
#include "ecma-function-object.h"
P
Peter Gal 已提交
23
#include "ecma-exceptions.h"
24
#include "ecma-gc.h"
P
Peter Gal 已提交
25
#include "ecma-helpers.h"
26
#include "jmem.h"
P
Peter Gal 已提交
27
#include "ecma-objects.h"
28
#include "ecma-try-catch-macro.h"
A
Andrey Shitov 已提交
29
#include "lit-magic-strings.h"
P
Peter Gal 已提交
30 31 32 33 34 35 36 37

/** \addtogroup ecma ECMA
 * @{
 *
 * \addtogroup ecmabuiltinhelpers ECMA builtin helper operations
 * @{
 */

38
#if ENABLED (JERRY_ES2015_BUILTIN_SYMBOL)
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
/**
 * Helper function for Object.prototype.toString routine when
 * the @@toStringTag property is present
 *
 * See also:
 *          ECMA-262 v6, 19.1.3.6
 *
 * @return ecma value
 *         Returned value must be freed with ecma_free_value.
 */
static ecma_value_t
ecma_builtin_helper_object_to_string_tag_helper (ecma_value_t tag_value) /**< string tag */
{
  JERRY_ASSERT (ecma_is_value_string (tag_value));

  ecma_string_t *tag_str_p = ecma_get_string_from_value (tag_value);
  ecma_string_t *ret_string_p;

  /* Building string "[object #@@toStringTag#]"
     The string size will be size("[object ") + size(#@@toStringTag#) + size ("]"). */
  const lit_utf8_size_t buffer_size = 9 + ecma_string_get_size (tag_str_p);
  JMEM_DEFINE_LOCAL_ARRAY (str_buffer, buffer_size, lit_utf8_byte_t);

  lit_utf8_byte_t *buffer_ptr = str_buffer;

  const lit_magic_string_id_t magic_string_ids[] =
  {
    LIT_MAGIC_STRING_LEFT_SQUARE_CHAR,
    LIT_MAGIC_STRING_OBJECT,
    LIT_MAGIC_STRING_SPACE_CHAR,
  };

  /* Copy to buffer the "[object " string */
  for (uint32_t i = 0; i < sizeof (magic_string_ids) / sizeof (lit_magic_string_id_t); ++i)
  {
    buffer_ptr = lit_copy_magic_string_to_buffer (magic_string_ids[i], buffer_ptr,
                                                  (lit_utf8_size_t) ((str_buffer + buffer_size) - buffer_ptr));

    JERRY_ASSERT (buffer_ptr <= str_buffer + buffer_size);
  }

  /* Copy to buffer the #@@toStringTag# string */
81 82
  buffer_ptr += ecma_string_copy_to_cesu8_buffer (tag_str_p, buffer_ptr,
                                                  (lit_utf8_size_t) ((str_buffer + buffer_size) - buffer_ptr));
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98

  JERRY_ASSERT (buffer_ptr <= str_buffer + buffer_size);

  /* Copy to buffer the "]" string */
  buffer_ptr = lit_copy_magic_string_to_buffer (LIT_MAGIC_STRING_RIGHT_SQUARE_CHAR, buffer_ptr,
                                                (lit_utf8_size_t) ((str_buffer + buffer_size) - buffer_ptr));

  JERRY_ASSERT (buffer_ptr <= str_buffer + buffer_size);

  ret_string_p = ecma_new_ecma_string_from_utf8 (str_buffer, (lit_utf8_size_t) (buffer_ptr - str_buffer));

  JMEM_FINALIZE_LOCAL_ARRAY (str_buffer);
  ecma_deref_ecma_string (tag_str_p);

  return ecma_make_string_value (ret_string_p);
} /* ecma_builtin_helper_object_to_string_tag_helper */
99
#endif /* ENABLED (JERRY_ES2015_BUILTIN_SYMBOL) */
100

P
Peter Gal 已提交
101 102 103 104 105 106 107 108 109 110
/**
 * Common implementation of the Object.prototype.toString routine
 *
 * See also:
 *          ECMA-262 v5, 15.2.4.2
 *
 * Used by:
 *         - The Object.prototype.toString routine.
 *         - The Array.prototype.toString routine as fallback.
 *
111 112
 * @return ecma value
 *         Returned value must be freed with ecma_free_value.
P
Peter Gal 已提交
113 114
 */

115
ecma_value_t
P
Peter Gal 已提交
116 117
ecma_builtin_helper_object_to_string (const ecma_value_t this_arg) /**< this argument */
{
118
  lit_magic_string_id_t type_string;
P
Peter Gal 已提交
119 120 121

  if (ecma_is_value_undefined (this_arg))
  {
122
    type_string = LIT_MAGIC_STRING_UNDEFINED_UL;
P
Peter Gal 已提交
123 124 125
  }
  else if (ecma_is_value_null (this_arg))
  {
126
    type_string = LIT_MAGIC_STRING_NULL_UL;
P
Peter Gal 已提交
127 128 129
  }
  else
  {
130
    ecma_value_t obj_this = ecma_op_to_object (this_arg);
P
Peter Gal 已提交
131

132
    if (ECMA_IS_VALUE_ERROR (obj_this))
P
Peter Gal 已提交
133 134 135 136
    {
      return obj_this;
    }

137
    JERRY_ASSERT (ecma_is_value_object (obj_this));
P
Peter Gal 已提交
138

139
    ecma_object_t *obj_p = ecma_get_object_from_value (obj_this);
P
Peter Gal 已提交
140

141
    type_string = ecma_object_get_class_name (obj_p);
P
Peter Gal 已提交
142

143
#if ENABLED (JERRY_ES2015_BUILTIN_SYMBOL)
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
    ecma_value_t tag_value = ecma_op_object_get_by_symbol_id (obj_p, LIT_MAGIC_STRING_TO_STRING_TAG);

    if (ECMA_IS_VALUE_ERROR (tag_value))
    {
      ecma_deref_object (obj_p);
      return tag_value;
    }

    if (ecma_is_value_string (tag_value))
    {
      ecma_deref_object (obj_p);
      return ecma_builtin_helper_object_to_string_tag_helper (tag_value);
    }

    ecma_free_value (tag_value);
159
#endif /* ENABLED (JERRY_ES2015_BUILTIN_SYMBOL) */
160 161

    ecma_deref_object (obj_p);
P
Peter Gal 已提交
162 163 164 165 166 167
  }

  ecma_string_t *ret_string_p;

  /* Building string "[object #type#]" where type is 'Undefined',
     'Null' or one of possible object's classes.
168 169
     The string with null character is maximum 27 characters long. */
  const lit_utf8_size_t buffer_size = 27;
Y
Yonggang Luo 已提交
170
  JERRY_VLA (lit_utf8_byte_t, str_buffer, buffer_size);
P
Peter Gal 已提交
171

A
Andrey Shitov 已提交
172 173 174 175 176 177 178 179 180 181 182 183 184
  lit_utf8_byte_t *buffer_ptr = str_buffer;

  const lit_magic_string_id_t magic_string_ids[] =
  {
    LIT_MAGIC_STRING_LEFT_SQUARE_CHAR,
    LIT_MAGIC_STRING_OBJECT,
    LIT_MAGIC_STRING_SPACE_CHAR,
    type_string,
    LIT_MAGIC_STRING_RIGHT_SQUARE_CHAR
  };

  for (uint32_t i = 0; i < sizeof (magic_string_ids) / sizeof (lit_magic_string_id_t); ++i)
  {
185 186 187
    buffer_ptr = lit_copy_magic_string_to_buffer (magic_string_ids[i], buffer_ptr,
                                                  (lit_utf8_size_t) ((str_buffer + buffer_size) - buffer_ptr));
    JERRY_ASSERT (buffer_ptr <= str_buffer + buffer_size);
A
Andrey Shitov 已提交
188
  }
P
Peter Gal 已提交
189

190
  ret_string_p = ecma_new_ecma_string_from_utf8 (str_buffer, (lit_utf8_size_t) (buffer_ptr - str_buffer));
P
Peter Gal 已提交
191

192
  return ecma_make_string_value (ret_string_p);
P
Peter Gal 已提交
193 194
} /* ecma_builtin_helper_object_to_string */

195 196 197 198 199 200
/**
 * The Array.prototype's 'toLocaleString' single element operation routine
 *
 * See also:
 *          ECMA-262 v5, 15.4.4.3 steps 6-8 and 10.b-d
 *
201 202
 * @return ecma value
 *         Returned value must be freed with ecma_free_value.
203
 */
204
ecma_value_t
205
ecma_builtin_helper_get_to_locale_string_at_index (ecma_object_t *obj_p, /**< this object */
A
Akos Kiss 已提交
206
                                                   uint32_t index) /**< array index */
207
{
208
  ecma_value_t ret_value = ECMA_VALUE_EMPTY;
209 210 211 212 213 214 215 216
  ecma_string_t *index_string_p = ecma_new_ecma_string_from_uint32 (index);

  ECMA_TRY_CATCH (index_value,
                  ecma_op_object_get (obj_p, index_string_p),
                  ret_value);

  if (ecma_is_value_undefined (index_value) || ecma_is_value_null (index_value))
  {
217
    ret_value = ecma_make_magic_string_value (LIT_MAGIC_STRING__EMPTY);
218 219 220 221 222 223 224 225 226 227
  }
  else
  {
    ECMA_TRY_CATCH (index_obj_value,
                    ecma_op_to_object (index_value),
                    ret_value);

    ecma_object_t *index_obj_p = ecma_get_object_from_value (index_obj_value);

    ECMA_TRY_CATCH (to_locale_value,
228
                    ecma_op_object_get_by_magic_id (index_obj_p, LIT_MAGIC_STRING_TO_LOCALE_STRING_UL),
229 230 231 232 233
                    ret_value);

    if (ecma_op_is_callable (to_locale_value))
    {
      ecma_object_t *locale_func_obj_p = ecma_get_object_from_value (to_locale_value);
234 235 236
      ECMA_TRY_CATCH (call_value,
                      ecma_op_function_call (locale_func_obj_p,
                                             ecma_make_object_value (index_obj_p),
237 238
                                             NULL,
                                             0),
239 240 241 242
                      ret_value);
      ret_value = ecma_op_to_string (call_value);
      ECMA_FINALIZE (call_value);

243 244 245
    }
    else
    {
246
      ret_value = ecma_raise_type_error (ECMA_ERR_MSG ("'toLocaleString' is missing or not a function."));
247 248 249 250 251 252 253 254 255 256 257 258 259
    }

    ECMA_FINALIZE (to_locale_value);
    ECMA_FINALIZE (index_obj_value);
  }

  ECMA_FINALIZE (index_value);

  ecma_deref_ecma_string (index_string_p);

  return ret_value;
} /* ecma_builtin_helper_get_to_locale_string_at_index */

260 261

/**
262
 * The Object.keys and Object.getOwnPropertyNames routine's common part.
263 264 265 266 267
 *
 * See also:
 *          ECMA-262 v5, 15.2.3.4 steps 2-5
 *          ECMA-262 v5, 15.2.3.14 steps 3-6
 *
268 269
 * @return ecma value - Array of property names.
 *         Returned value must be freed with ecma_free_value.
270
 */
271
ecma_value_t
272
ecma_builtin_helper_object_get_properties (ecma_object_t *obj_p, /**< object */
273
                                           uint32_t opts) /**< any combination of ecma_list_properties_options_t */
274 275 276
{
  JERRY_ASSERT (obj_p != NULL);

277
  ecma_value_t new_array = ecma_op_create_array_object (NULL, 0, false);
278
  JERRY_ASSERT (!ECMA_IS_VALUE_ERROR (new_array));
279
  ecma_object_t *new_array_p = ecma_get_object_from_value (new_array);
280

R
Robert Fancsik 已提交
281
  ecma_collection_t *props_p = ecma_op_object_get_property_names (obj_p, opts);
282

283
  if (props_p->item_count == 0)
284
  {
R
Robert Fancsik 已提交
285
    ecma_collection_destroy (props_p);
286 287
    return new_array;
  }
288

289
  JERRY_ASSERT (((ecma_extended_object_t *) new_array_p)->u.array.is_fast_mode);
290

R
Robert Fancsik 已提交
291
  ecma_value_t *buffer_p = props_p->buffer_p;
292
  ecma_value_t *values_p = ecma_fast_array_extend (new_array_p, props_p->item_count);
293

R
Robert Fancsik 已提交
294
  memcpy (values_p, buffer_p, props_p->item_count * sizeof (ecma_value_t));
295

R
Robert Fancsik 已提交
296
  ecma_collection_free_objects (props_p);
297

298 299 300
  return new_array;
} /* ecma_builtin_helper_object_get_properties */

301 302 303 304 305 306 307
/**
 * Helper function to normalizing an array index
 *
 * This function clamps the given index to the [0, length] range.
 * If the index is negative, it is used as the offset from the end of the array,
 * to compute normalized index.
 * If the index is greater than the length of the array, the normalized index will be the length of the array.
308
 * If is_last_index_of is true, then we use the method in ECMA-262 v6, 22.2.3.16 to compute the normalized index.
309 310 311 312 313 314 315 316 317 318 319
 * See also:
 *          ECMA-262 v5, 15.4.4.10 steps 5-6, 7 (part 2) and 8
 *          ECMA-262 v5, 15.4.4.12 steps 5-6
 *          ECMA-262 v5, 15.4.4.14 steps 5
 *          ECMA-262 v5, 15.5.4.13 steps 4, 5 (part 2) and 6-7
 *
 * Used by:
 *         - The Array.prototype.slice routine.
 *         - The Array.prototype.splice routine.
 *         - The Array.prototype.indexOf routine.
 *         - The String.prototype.slice routine.
320 321
 *         - The TypedArray.prototype.indexOf routine.
 *         - The TypedArray.prototype.lastIndexOf routine
322 323 324 325 326
 *
 * @return uint32_t - the normalized value of the index
 */
uint32_t
ecma_builtin_helper_array_index_normalize (ecma_number_t index, /**< index */
327 328
                                           uint32_t length, /**< array's length */
                                           bool is_last_index_of) /**< true - normalize for lastIndexOf method*/
329 330 331 332 333 334
{
  uint32_t norm_index;

  if (!ecma_number_is_nan (index))
  {

335 336 337 338 339
    if (ecma_number_is_zero (index))
    {
      norm_index = 0;
    }
    else if (ecma_number_is_infinity (index))
340
    {
341 342 343 344 345 346 347 348
      if (is_last_index_of)
      {
        norm_index = ecma_number_is_negative (index) ? (uint32_t) -1 : length - 1;
      }
      else
      {
        norm_index = ecma_number_is_negative (index) ? 0 : length;
      }
349 350 351
    }
    else
    {
352
      if (ecma_number_is_negative (index))
353
      {
354
        ecma_number_t index_neg = -index;
355

356
        if (is_last_index_of)
357
        {
358
          norm_index = length - ecma_number_to_uint32 (index_neg);
359 360 361
        }
        else
        {
362 363 364 365 366 367 368 369
          if (index_neg > length)
          {
            norm_index = 0;
          }
          else
          {
            norm_index = length - ecma_number_to_uint32 (index_neg);
          }
370
        }
371 372 373
      }
      else
      {
374
        if (index > length)
375
        {
376
          norm_index = is_last_index_of ? length - 1 : length;
377
        }
378 379 380 381
        else
        {
          norm_index = ecma_number_to_uint32 (index);
        }
382 383 384 385 386 387 388 389 390 391 392
      }
    }
  }
  else
  {
    norm_index = 0;
  }

  return norm_index;
} /* ecma_builtin_helper_array_index_normalize */

393 394 395 396 397 398 399 400 401
/**
 * Helper function for concatenating an ecma_value_t to an Array.
 *
 * See also:
 *          ECMA-262 v5, 15.4.4.4 steps 5.b - 5.c
 *
 * Used by:
 *         - The Array.prototype.concat routine.
 *
402 403
 * @return ecma value
 *         Returned value must be freed with ecma_free_value.
404
 */
405
ecma_value_t
406
ecma_builtin_helper_array_concat_value (ecma_object_t *obj_p, /**< array */
407
                                        uint32_t *length_p, /**< [in,out] array's length */
408 409
                                        ecma_value_t value) /**< value to concat */
{
410
  ecma_value_t ret_value = ECMA_VALUE_EMPTY;
411 412 413 414 415 416 417

  /* 5.b */
  if (ecma_is_value_object (value)
      && (ecma_object_get_class_name (ecma_get_object_from_value (value)) == LIT_MAGIC_STRING_ARRAY_UL))
  {
    /* 5.b.ii */
    ECMA_TRY_CATCH (arg_len_value,
418 419
                    ecma_op_object_get_by_magic_id (ecma_get_object_from_value (value),
                                                    LIT_MAGIC_STRING_LENGTH),
420 421 422 423 424 425 426
                    ret_value);
    ECMA_OP_TO_NUMBER_TRY_CATCH (arg_len_number, arg_len_value, ret_value);

    uint32_t arg_len = ecma_number_to_uint32 (arg_len_number);

    /* 5.b.iii */
    for (uint32_t array_index = 0;
427
         array_index < arg_len && ecma_is_value_empty (ret_value);
428 429 430 431 432
         array_index++)
    {
      ecma_string_t *array_index_string_p = ecma_new_ecma_string_from_uint32 (array_index);

      /* 5.b.iii.2 */
433 434 435 436
      ECMA_TRY_CATCH (get_value,
                      ecma_op_object_find (ecma_get_object_from_value (value),
                                           array_index_string_p),
                      ret_value);
437

438 439
      if (ecma_is_value_found (get_value))
      {
440
        /* 5.b.iii.3.a */
441
        ecma_string_t *new_array_index_string_p = ecma_new_ecma_string_from_uint32 (*length_p + array_index);
442 443 444

        /* 5.b.iii.3.b */
        /* This will always be a simple value since 'is_throw' is false, so no need to free. */
445 446 447
        ecma_value_t put_comp = ecma_builtin_helper_def_prop (obj_p,
                                                              new_array_index_string_p,
                                                              get_value,
448
                                                              ECMA_PROPERTY_CONFIGURABLE_ENUMERABLE_WRITABLE);
449

450
        JERRY_ASSERT (ecma_is_value_true (put_comp));
451 452 453
        ecma_deref_ecma_string (new_array_index_string_p);
      }

454 455
      ECMA_FINALIZE (get_value);

456 457 458 459 460 461 462 463 464 465 466 467 468 469
      ecma_deref_ecma_string (array_index_string_p);
    }

    *length_p += arg_len;

    ECMA_OP_TO_NUMBER_FINALIZE (arg_len_number);
    ECMA_FINALIZE (arg_len_value);
  }
  else
  {
    ecma_string_t *new_array_index_string_p = ecma_new_ecma_string_from_uint32 ((*length_p)++);

    /* 5.c.i */
    /* This will always be a simple value since 'is_throw' is false, so no need to free. */
470 471 472
    ecma_value_t put_comp = ecma_builtin_helper_def_prop (obj_p,
                                                          new_array_index_string_p,
                                                          value,
473
                                                          ECMA_PROPERTY_CONFIGURABLE_ENUMERABLE_WRITABLE);
474
    JERRY_ASSERT (ecma_is_value_true (put_comp));
475 476 477 478 479 480 481

    ecma_deref_ecma_string (new_array_index_string_p);
  }

  return ret_value;
} /* ecma_builtin_helper_array_concat_value */

482 483 484 485 486 487
/**
 * Helper function to normalizing a string index
 *
 * This function clamps the given index to the [0, length] range.
 * If the index is negative, 0 value is used.
 * If the index is greater than the length of the string, the normalized index will be the length of the string.
488
 * NaN is mapped to zero or length depending on the nan_to_zero parameter.
489 490 491 492 493 494
 *
 * See also:
 *          ECMA-262 v5, 15.5.4.15
 *
 * Used by:
 *         - The String.prototype.substring routine.
495
 *         - The ecma_builtin_helper_string_prototype_object_index_of helper routine.
496 497 498 499 500
 *
 * @return uint32_t - the normalized value of the index
 */
uint32_t
ecma_builtin_helper_string_index_normalize (ecma_number_t index, /**< index */
501 502
                                            uint32_t length, /**< string's length */
                                            bool nan_to_zero) /**< whether NaN is mapped to zero (t) or length (f) */
503 504 505
{
  uint32_t norm_index = 0;

506 507 508 509 510 511 512 513
  if (ecma_number_is_nan (index))
  {
    if (!nan_to_zero)
    {
      norm_index = length;
    }
  }
  else if (!ecma_number_is_negative (index))
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
  {
    if (ecma_number_is_infinity (index))
    {
      norm_index = length;
    }
    else
    {
      norm_index = ecma_number_to_uint32 (index);

      if (norm_index > length)
      {
        norm_index = length;
      }
    }
  }

  return norm_index;
} /* ecma_builtin_helper_string_index_normalize */

533
/**
534
 * Helper function for string indexOf, lastIndexOf, startsWith, includes, endsWith functions
535 536 537 538
 *
 * See also:
 *          ECMA-262 v5, 15.5.4.7
 *          ECMA-262 v5, 15.5.4.8
539 540 541
 *          ECMA-262 v6, 21.1.3.6
 *          ECMA-262 v6, 21.1.3.7
 *          ECMA-262 v6, 21.1.3.18
542 543 544 545
 *
 * Used by:
 *         - The String.prototype.indexOf routine.
 *         - The String.prototype.lastIndexOf routine.
546 547 548
 *         - The String.prototype.startsWith routine.
 *         - The String.prototype.includes routine.
 *         - The String.prototype.endsWith routine.
549
 *
550 551
 * @return ecma_value_t - Returns index (last index) or a
 *                        boolean value
552
 */
553
ecma_value_t
554
ecma_builtin_helper_string_prototype_object_index_of (ecma_string_t *original_str_p, /**< this argument */
555 556
                                                      ecma_value_t arg1, /**< routine's first argument */
                                                      ecma_value_t arg2, /**< routine's second argument */
557
                                                      ecma_string_index_of_mode_t mode) /**< routine's mode */
558
{
559
  /* 5 (indexOf) -- 6 (lastIndexOf) */
560 561
  const ecma_length_t original_len = ecma_string_get_length (original_str_p);

562 563 564 565 566 567 568 569 570 571
#if ENABLED (JERRY_ES2015_BUILTIN)
  /* 4, 6 (startsWith, includes, endsWith) */
  if (mode >= ECMA_STRING_STARTS_WITH
      && (ecma_is_value_object (arg1)
      && ecma_object_class_is (ecma_get_object_from_value (arg1), LIT_MAGIC_STRING_REGEXP_UL)))
  {
    JERRY_ASSERT (ECMA_STRING_LAST_INDEX_OF < mode && mode <= ECMA_STRING_ENDS_WITH);
    return ecma_raise_type_error (ECMA_ERR_MSG ("Search string can't be of type: RegExp"));
  }
#endif /* ENABLED (JERRY_ES2015_BUILTIN) */
572

573 574 575 576 577 578 579 580 581
  /* 3 */
  ecma_value_t search_str_val = ecma_op_to_string (arg1);

  if (ECMA_IS_VALUE_ERROR (search_str_val))
  {
    return search_str_val;
  }

  /* 7, 8 */
582 583
  ecma_string_t *search_str_p = ecma_get_string_from_value (search_str_val);

584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
  /* 4 (indexOf, lastIndexOf), 9 (startsWith, includes), 10 (endsWith) */
  ecma_number_t pos_num;
  ecma_value_t ret_value  = ecma_get_number (arg2, &pos_num);

  /* 10 (startsWith, includes), 11 (endsWith) */
  if (ECMA_IS_VALUE_ERROR (ret_value))
  {
    ecma_deref_ecma_string (search_str_p);
    return ret_value;
  }

  bool use_first_index = mode != ECMA_STRING_LAST_INDEX_OF;

  /* 4b, 6 (indexOf) - 4b, 5, 7 (lastIndexOf) */
  ecma_length_t start = ecma_builtin_helper_string_index_normalize (pos_num, original_len, use_first_index);

600
  ecma_number_t ret_num = ECMA_NUMBER_MINUS_ONE;
601

602
  ecma_length_t index_of = 0;
603 604 605 606

  ret_value = ECMA_VALUE_FALSE;

  switch (mode)
607
  {
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
#if ENABLED (JERRY_ES2015_BUILTIN)
    case ECMA_STRING_STARTS_WITH:
    {
      if (pos_num + start > original_len)
      {
        break;
      }

      if (ecma_builtin_helper_string_find_index (original_str_p, search_str_p, true, start, &index_of))
      {
        /* 15, 16 (startsWith) */
        ret_value = ecma_make_boolean_value (index_of == start);
      }
      break;
    }
    case ECMA_STRING_INCLUDES:
    {
      if (ecma_builtin_helper_string_find_index (original_str_p, search_str_p, true, start, &index_of))
      {
        ret_value = ECMA_VALUE_TRUE;
      }
      break;
    }
    case ECMA_STRING_ENDS_WITH:
    {
      if (start == 0)
      {
        start = original_len;
      }

      ecma_length_t search_str_len = ecma_string_get_length (search_str_p);
639

640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
      if (search_str_len == 0)
      {
        ret_value = ECMA_VALUE_TRUE;
        break;
      }

      int32_t start_ends_with = (int32_t) (start - search_str_len);

      if (start_ends_with < 0)
      {
        break;
      }
      if (ecma_builtin_helper_string_find_index (original_str_p, search_str_p, true,
                                                 (ecma_length_t) start_ends_with, &index_of))
      {
        ret_value = ecma_make_boolean_value (index_of == (ecma_length_t) start_ends_with);
      }
      break;
    }
#endif /* ENABLED (JERRY_ES2015_BUILTIN) */

    case ECMA_STRING_INDEX_OF:
    case ECMA_STRING_LAST_INDEX_OF:
    default:
    {
      /* 8 (indexOf) -- 9 (lastIndexOf) */
      if (ecma_builtin_helper_string_find_index (original_str_p, search_str_p, use_first_index, start, &index_of))
      {
        ret_num = ((ecma_number_t) index_of);
      }
      ret_value = ecma_make_number_value (ret_num);
      break;
    }
  }
674

675
  ecma_deref_ecma_string (search_str_p);
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694

  return ret_value;
} /* ecma_builtin_helper_string_prototype_object_index_of */

/**
 * Helper function for finding index of a search string
 *
 * This function clamps the given index to the [0, length] range.
 * If the index is negative, 0 value is used.
 * If the index is greater than the length of the string, the normalized index will be the length of the string.
 * NaN is mapped to zero or length depending on the nan_to_zero parameter.
 *
 * See also:
 *          ECMA-262 v5, 15.5.4.7,8,11
 *
 * Used by:
 *         - The ecma_builtin_helper_string_prototype_object_index_of helper routine.
 *         - The ecma_builtin_string_prototype_object_replace_match helper routine.
 *
695
 * @return bool - whether there is a match for the search string
696 697 698 699 700 701
 */
bool
ecma_builtin_helper_string_find_index (ecma_string_t *original_str_p, /**< index */
                                       ecma_string_t *search_str_p, /**< string's length */
                                       bool first_index, /**< whether search for first (t) or last (f) index */
                                       ecma_length_t start_pos, /**< start position */
702
                                       ecma_length_t *ret_index_p) /**< [out] position found in original string */
703 704 705 706 707
{
  bool match_found = false;
  const ecma_length_t original_len = ecma_string_get_length (original_str_p);
  const ecma_length_t search_len = ecma_string_get_length (search_str_p);

708 709 710 711
  if (search_len <= original_len)
  {
    if (!search_len)
    {
712 713
      match_found = true;
      *ret_index_p = first_index ? 0 : original_len;
714 715 716 717
    }
    else
    {
      /* create utf8 string from original string and advance to position */
718
      ECMA_STRING_TO_UTF8_STRING (original_str_p, original_str_utf8_p, original_str_size);
719

720
      ecma_length_t index = start_pos;
721

722
      const lit_utf8_byte_t *original_str_curr_p = original_str_utf8_p;
723 724 725 726
      for (ecma_length_t idx = 0; idx < index; idx++)
      {
        lit_utf8_incr (&original_str_curr_p);
      }
727 728

      /* create utf8 string from search string */
729
      ECMA_STRING_TO_UTF8_STRING (search_str_p, search_str_utf8_p, search_str_size);
730

731
      const lit_utf8_byte_t *search_str_curr_p = search_str_utf8_p;
732 733 734

      /* iterate original string and try to match at each position */
      bool searching = true;
735
      ecma_char_t first_char = lit_utf8_read_next (&search_str_curr_p);
736 737 738 739
      while (searching)
      {
        /* match as long as possible */
        ecma_length_t match_len = 0;
740
        const lit_utf8_byte_t *stored_original_str_curr_p = original_str_curr_p;
741

742 743 744
        if (match_len < search_len &&
            index + match_len < original_len &&
            lit_utf8_read_next (&original_str_curr_p) == first_char)
745
        {
746
          const lit_utf8_byte_t *nested_search_str_curr_p = search_str_curr_p;
747
          match_len++;
748 749 750 751 752 753 754

          while (match_len < search_len &&
                 index + match_len < original_len &&
                 lit_utf8_read_next (&original_str_curr_p) == lit_utf8_read_next (&nested_search_str_curr_p))
          {
            match_len++;
          }
755 756 757 758 759
        }

        /* check for match */
        if (match_len == search_len)
        {
760 761 762
          match_found = true;
          *ret_index_p = index;

763 764 765 766 767
          break;
        }
        else
        {
          /* inc/dec index and update iterators and search condition */
768
          original_str_curr_p = stored_original_str_curr_p;
769

770
          if (first_index)
771 772 773
          {
            if ((searching = (index <= original_len - search_len)))
            {
774
              lit_utf8_incr (&original_str_curr_p);
775 776 777 778 779 780 781
              index++;
            }
          }
          else
          {
            if ((searching = (index > 0)))
            {
782
              lit_utf8_decr (&original_str_curr_p);
783 784 785 786 787 788
              index--;
            }
          }
        }
      }

789 790
      ECMA_FINALIZE_UTF8_STRING (search_str_utf8_p, search_str_size);
      ECMA_FINALIZE_UTF8_STRING (original_str_utf8_p, original_str_size);
791 792 793
    }
  }

794 795
  return match_found;
} /* ecma_builtin_helper_string_find_index */
796

797 798 799 800 801 802 803 804 805 806 807 808
/**
 * Helper function for using [[DefineOwnProperty]] specialized for indexed property names
 *
 * Note: this method falls back to the general ecma_builtin_helper_def_prop
 *
 * @return ecma value
 *         Returned value must be freed with ecma_free_value.
 */
ecma_value_t
ecma_builtin_helper_def_prop_by_index (ecma_object_t *obj_p, /**< object */
                                       uint32_t index, /**< property index */
                                       ecma_value_t value, /**< value */
809
                                       uint32_t opts) /**< any combination of ecma_property_flag_t bits */
810 811 812 813 814 815
{
  if (JERRY_LIKELY (index <= ECMA_DIRECT_STRING_MAX_IMM))
  {
    return ecma_builtin_helper_def_prop (obj_p,
                                         ECMA_CREATE_DIRECT_UINT32_STRING (index),
                                         value,
816
                                         opts);
817 818 819 820 821 822
  }

  ecma_string_t *index_str_p = ecma_new_non_direct_string_from_uint32 (index);
  ecma_value_t ret_value = ecma_builtin_helper_def_prop (obj_p,
                                                         index_str_p,
                                                         value,
823
                                                         opts);
824 825 826 827 828
  ecma_deref_ecma_string (index_str_p);

  return ret_value;
} /* ecma_builtin_helper_def_prop_by_index */

829 830 831 832 833 834 835
/**
 * Helper function for using [[DefineOwnProperty]].
 *
 * See also:
 *          ECMA-262 v5, 8.12.9
 *          ECMA-262 v5, 15.4.5.1
 *
836 837
 * @return ecma value
 *         Returned value must be freed with ecma_free_value.
838
 */
839
ecma_value_t
840 841 842
ecma_builtin_helper_def_prop (ecma_object_t *obj_p, /**< object */
                              ecma_string_t *index_p, /**< index string */
                              ecma_value_t value, /**< value */
843 844
                              uint32_t opts) /**< any combination of ecma_property_flag_t bits
                                              *   with the optional ECMA_IS_THROW flag */
845
{
846
  ecma_property_descriptor_t prop_desc;
847

848
  prop_desc.flags = (uint16_t) (ECMA_NAME_DATA_PROPERTY_DESCRIPTOR_BITS | opts);
849

850
  prop_desc.value = value;
851 852 853

  return ecma_op_object_define_own_property (obj_p,
                                             index_p,
854
                                             &prop_desc);
855 856
} /* ecma_builtin_helper_def_prop */

P
Peter Gal 已提交
857 858 859 860
/**
 * @}
 * @}
 */