/** * List names of a Function object's lazy instantiated properties, * adding them to corresponding string collections * * See also: * ecma_op_function_try_lazy_instantiate_property */ void ecma_op_function_list_lazy_property_names (bool separate_enumerable, /**< true - list enumerable properties into * main collection and non-enumerable * to collection of 'skipped * non-enumerable' properties, * false - list all properties into main * collection. */ ecma_collection_header_t *main_collection_p, /**< 'main' collection */ ecma_collection_header_t *non_enum_collection_p) /**< skipped * 'non-enumerable' * collection */ { ecma_collection_header_t *for_enumerable_p = main_collection_p; JERRY_UNUSED (for_enumerable_p); ecma_collection_header_t *for_non_enumerable_p = separate_enumerable ? non_enum_collection_p : main_collection_p; ecma_string_t *name_p; /* 'length' property is non-enumerable (ECMA-262 v5, 13.2.5) */ name_p = ecma_new_ecma_length_string (); ecma_append_to_values_collection (for_non_enumerable_p, ecma_make_string_value (name_p), true); ecma_deref_ecma_string (name_p); /* 'prototype' property is non-enumerable (ECMA-262 v5, 13.2.18) */ name_p = ecma_get_magic_string (LIT_MAGIC_STRING_PROTOTYPE); ecma_append_to_values_collection (for_non_enumerable_p, ecma_make_string_value (name_p), true); ecma_deref_ecma_string (name_p); } /* ecma_op_function_list_lazy_property_names */
/** * The String object's 'fromCharCode' routine * * See also: * ECMA-262 v5, 15.5.3.2 * * @return completion value * Returned value must be freed with ecma_free_completion_value. */ static ecma_completion_value_t ecma_builtin_string_object_from_char_code (ecma_value_t this_arg __attr_unused___, /**< 'this' argument */ const ecma_value_t args[], /**< arguments list */ ecma_length_t args_number) /**< number of arguments */ { ecma_completion_value_t ret_value = ecma_make_empty_completion_value (); if (args_number == 0) { ecma_string_t *ret_str_p = ecma_new_ecma_string_from_utf8 (NULL, 0); return ecma_make_normal_completion_value (ecma_make_string_value (ret_str_p)); } lit_utf8_size_t utf8_buf_size = args_number * LIT_UTF8_MAX_BYTES_IN_CODE_UNIT; ecma_string_t *ret_str_p; MEM_DEFINE_LOCAL_ARRAY (utf8_buf_p, utf8_buf_size, lit_utf8_byte_t); lit_utf8_size_t utf8_buf_used = 0; FIXME ("Support surrogate pairs"); for (ecma_length_t arg_index = 0; arg_index < args_number; arg_index++) { ECMA_OP_TO_NUMBER_TRY_CATCH (arg_num, args[arg_index], ret_value); uint32_t uint32_char_code = ecma_number_to_uint32 (arg_num); ecma_char_t code_unit = (uint16_t) uint32_char_code; JERRY_ASSERT (utf8_buf_used <= utf8_buf_size - LIT_UTF8_MAX_BYTES_IN_CODE_UNIT); utf8_buf_used += lit_code_unit_to_utf8 (code_unit, utf8_buf_p + utf8_buf_used); JERRY_ASSERT (utf8_buf_used <= utf8_buf_size); ECMA_OP_TO_NUMBER_FINALIZE (arg_num); if (ecma_is_completion_value_throw (ret_value)) { mem_heap_free_block (utf8_buf_p); return ret_value; } JERRY_ASSERT (ecma_is_completion_value_empty (ret_value)); } ret_str_p = ecma_new_ecma_string_from_utf8 (utf8_buf_p, utf8_buf_used); MEM_FINALIZE_LOCAL_ARRAY (utf8_buf_p); return ecma_make_normal_completion_value (ecma_make_string_value (ret_str_p)); } /* ecma_builtin_string_object_from_char_code */
/** * The String.prototype object's 'toString' routine * * See also: * ECMA-262 v5, 15.5.4.2 * * @return completion value * Returned value must be freed with ecma_free_completion_value. */ static ecma_completion_value_t ecma_builtin_string_prototype_object_to_string (ecma_value_t this_arg) /**< this argument */ { if (ecma_is_value_string (this_arg)) { return ecma_make_normal_completion_value (ecma_copy_value (this_arg, true)); } else if (ecma_is_value_object (this_arg)) { ecma_object_t *obj_p = ecma_get_object_from_value (this_arg); if (ecma_object_get_class_name (obj_p) == LIT_MAGIC_STRING_STRING_UL) { ecma_property_t *prim_value_prop_p = ecma_get_internal_property (obj_p, ECMA_INTERNAL_PROPERTY_PRIMITIVE_STRING_VALUE); ecma_string_t *prim_value_str_p = ECMA_GET_NON_NULL_POINTER (ecma_string_t, prim_value_prop_p->u.internal_property.value); prim_value_str_p = ecma_copy_or_ref_ecma_string (prim_value_str_p); return ecma_make_normal_completion_value (ecma_make_string_value (prim_value_str_p)); } } return ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_TYPE)); } /* ecma_builtin_string_prototype_object_to_string */
/** * The Date.prototype object's 'toString' routine * * See also: * ECMA-262 v5, 15.9.5.2 * * @return ecma value * Returned value must be freed with ecma_free_value. */ static ecma_value_t ecma_builtin_date_prototype_to_string (ecma_value_t this_arg) /**< this argument */ { ecma_value_t ret_value = ecma_make_simple_value (ECMA_SIMPLE_VALUE_EMPTY); ECMA_TRY_CATCH (prim_value, ecma_date_get_primitive_value (this_arg), ret_value); ecma_number_t *prim_num_p = ecma_get_number_from_value (prim_value); if (ecma_number_is_nan (*prim_num_p)) { ecma_string_t *magic_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_INVALID_DATE_UL); ret_value = ecma_make_string_value (magic_str_p); } else { ret_value = ecma_date_value_to_string (*prim_num_p); } ECMA_FINALIZE (prim_value); return ret_value; } /* ecma_builtin_date_prototype_to_string */
/** * Common function to create a non-formatted JSON string. * * See also: * ECMA-262 v5, 15.12.3 * * Used by: * - ecma_builtin_json_object step 10.a * - ecma_builtin_json_array step 10.a * * @return ecma value * Returned value must be freed with ecma_free_value. */ ecma_value_t ecma_builtin_helper_json_create_non_formatted_json (ecma_string_t *left_bracket_p, /**< left bracket*/ ecma_string_t *right_bracket_p, /**< right bracket*/ ecma_collection_header_t *partial_p) /**< key-value pairs*/ { /* 10.a */ ecma_string_t *comma_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_COMMA_CHAR); ecma_string_t *properties_str_p; ecma_string_t *tmp_str_p; /* 10.a.i */ properties_str_p = ecma_builtin_helper_json_create_separated_properties (partial_p, comma_str_p); ecma_deref_ecma_string (comma_str_p); /* 10.a.ii */ tmp_str_p = ecma_concat_ecma_strings (left_bracket_p, properties_str_p); ecma_deref_ecma_string (properties_str_p); properties_str_p = tmp_str_p; tmp_str_p = ecma_concat_ecma_strings (properties_str_p, right_bracket_p); ecma_deref_ecma_string (properties_str_p); properties_str_p = tmp_str_p; return ecma_make_string_value (properties_str_p); } /* ecma_builtin_helper_json_create_non_formatted_json */
/** * The Date.prototype object's 'getYear' routine * * See also: * ECMA-262 v5, AnnexB.B.2.4 * * @return ecma value * Returned value must be freed with ecma_free_value. */ static ecma_value_t ecma_builtin_date_prototype_get_year (ecma_value_t this_arg) /**< this argument */ { ecma_value_t ret_value = ecma_make_simple_value (ECMA_SIMPLE_VALUE_EMPTY); /* 1. */ ECMA_TRY_CATCH (value, ecma_builtin_date_prototype_get_time (this_arg), ret_value); ecma_number_t *this_num_p = ecma_get_number_from_value (value); /* 2. */ if (ecma_number_is_nan (*this_num_p)) { ecma_string_t *nan_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_NAN); ret_value = ecma_make_string_value (nan_str_p); } else { /* 3. */ ecma_number_t *ret_num_p = ecma_alloc_number (); *ret_num_p = ecma_date_year_from_time (ecma_date_local_time (*this_num_p)) - 1900; ret_value = ecma_make_number_value (ret_num_p); } ECMA_FINALIZE (value); return ret_value; } /* ecma_builtin_date_prototype_get_year */
/** * The Boolean.prototype object's 'toString' routine * * See also: * ECMA-262 v5, 15.6.4.2 * * @return ecma value * Returned value must be freed with ecma_free_value. */ static ecma_value_t ecma_builtin_boolean_prototype_object_to_string (ecma_value_t this_arg) /**< this argument */ { ecma_value_t ret_value = ecma_make_simple_value (ECMA_SIMPLE_VALUE_EMPTY); ECMA_TRY_CATCH (value_of_ret, ecma_builtin_boolean_prototype_object_value_of (this_arg), ret_value); ecma_string_t *ret_str_p; if (ecma_is_value_true (value_of_ret)) { ret_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_TRUE); } else { JERRY_ASSERT (ecma_is_value_boolean (value_of_ret)); ret_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_FALSE); } ret_value = ecma_make_string_value (ret_str_p); ECMA_FINALIZE (value_of_ret); return ret_value; } /* ecma_builtin_boolean_prototype_object_to_string */
/** * Allocate a collection of ecma-strings. * * @return pointer to the collection's header */ ecma_collection_header_t * ecma_new_strings_collection (ecma_string_t *string_ptrs_buffer[], /**< pointers to ecma-strings */ ecma_length_t strings_number) /**< number of ecma-strings */ { JERRY_ASSERT (string_ptrs_buffer != NULL); JERRY_ASSERT (strings_number > 0); ecma_collection_header_t *new_collection_p; MEM_DEFINE_LOCAL_ARRAY (values_buffer, strings_number, ecma_value_t); for (ecma_length_t string_index = 0; string_index < strings_number; string_index++) { values_buffer[string_index] = ecma_make_string_value (string_ptrs_buffer[string_index]); } new_collection_p = ecma_new_values_collection (values_buffer, strings_number, false); MEM_FINALIZE_LOCAL_ARRAY (values_buffer); return new_collection_p; } /* ecma_new_strings_collection */
/** * 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 * * @return ecma value * Returned value must be freed with ecma_free_value. */ ecma_value_t ecma_builtin_helper_get_to_locale_string_at_index (ecma_object_t *obj_p, /**< this object */ uint32_t index) /**< array index */ { ecma_value_t ret_value = ecma_make_simple_value (ECMA_SIMPLE_VALUE_EMPTY); 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)) { ecma_string_t *return_string_p = ecma_get_magic_string (LIT_MAGIC_STRING__EMPTY); ret_value = ecma_make_string_value (return_string_p); } 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_string_t *locale_string_magic_string_p = ecma_get_magic_string (LIT_MAGIC_STRING_TO_LOCALE_STRING_UL); ECMA_TRY_CATCH (to_locale_value, ecma_op_object_get (index_obj_p, locale_string_magic_string_p), 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); ECMA_TRY_CATCH (call_value, ecma_op_function_call (locale_func_obj_p, ecma_make_object_value (index_obj_p), NULL, 0), ret_value); ret_value = ecma_op_to_string (call_value); ECMA_FINALIZE (call_value); } else { ret_value = ecma_raise_type_error (ECMA_ERR_MSG ("")); } ECMA_FINALIZE (to_locale_value); ecma_deref_ecma_string (locale_string_magic_string_p); 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 */
/** * The String.prototype object's 'charAt' routine * * See also: * ECMA-262 v5, 15.5.4.4 * * @return completion value * Returned value must be freed with ecma_free_completion_value. */ static ecma_completion_value_t ecma_builtin_string_prototype_object_char_at (ecma_value_t this_arg, /**< this argument */ ecma_value_t arg) /**< routine's argument */ { ecma_completion_value_t ret_value = ecma_make_empty_completion_value (); /* 1 */ ECMA_TRY_CATCH (check_coercible_val, ecma_op_check_object_coercible (this_arg), ret_value); /* 2 */ ECMA_TRY_CATCH (to_string_val, ecma_op_to_string (this_arg), ret_value); /* 3 */ ECMA_OP_TO_NUMBER_TRY_CATCH (index_num, arg, ret_value); /* 4 */ ecma_string_t *original_string_p = ecma_get_string_from_value (to_string_val); const ecma_length_t len = ecma_string_get_length (original_string_p); /* 5 */ if (index_num < 0 || index_num >= len || !len) { ret_value = ecma_make_normal_completion_value (ecma_make_string_value ( ecma_get_magic_string (LIT_MAGIC_STRING__EMPTY))); } else { /* 6 */ ecma_char_t new_ecma_char = ecma_string_get_char_at_pos (original_string_p, ecma_number_to_uint32 (index_num)); ret_value = ecma_make_normal_completion_value (ecma_make_string_value ( ecma_new_ecma_string_from_code_unit (new_ecma_char))); } ECMA_OP_TO_NUMBER_FINALIZE (index_num); ECMA_FINALIZE (to_string_val); ECMA_FINALIZE (check_coercible_val); return ret_value; } /* ecma_builtin_string_prototype_object_char_at */
/** * The String.prototype object's 'concat' routine * * See also: * ECMA-262 v5, 15.5.4.6 * * @return completion value * Returned value must be freed with ecma_free_completion_value. */ static ecma_completion_value_t ecma_builtin_string_prototype_object_concat (ecma_value_t this_arg, /**< this argument */ const ecma_value_t* argument_list_p, /**< arguments list */ ecma_length_t arguments_number) /**< number of arguments */ { ecma_completion_value_t ret_value = ecma_make_empty_completion_value (); /* 1 */ ECMA_TRY_CATCH (check_coercible_val, ecma_op_check_object_coercible (this_arg), ret_value); /* 2 */ ECMA_TRY_CATCH (to_string_val, ecma_op_to_string (this_arg), ret_value); /* 3 */ // No copy performed /* 4 */ ecma_string_t *string_to_return = ecma_copy_or_ref_ecma_string (ecma_get_string_from_value (to_string_val)); /* 5 */ for (uint32_t arg_index = 0; arg_index < arguments_number && ecma_is_completion_value_empty (ret_value); ++arg_index) { /* 5a */ /* 5b */ ecma_string_t *string_temp = string_to_return; ECMA_TRY_CATCH (get_arg_string, ecma_op_to_string (argument_list_p[arg_index]), ret_value); string_to_return = ecma_concat_ecma_strings (string_to_return, ecma_get_string_from_value (get_arg_string)); ecma_deref_ecma_string (string_temp); ECMA_FINALIZE (get_arg_string); } /* 6 */ if (ecma_is_completion_value_empty (ret_value)) { ret_value = ecma_make_normal_completion_value (ecma_make_string_value (string_to_return)); } else { ecma_deref_ecma_string (string_to_return); } ECMA_FINALIZE (to_string_val); ECMA_FINALIZE (check_coercible_val); return ret_value; } /* ecma_builtin_string_prototype_object_concat */
/** * ToString operation. * * See also: * ECMA-262 v5, 9.8 * * @return completion value * Returned value must be freed with ecma_free_completion_value */ ecma_completion_value_t ecma_op_to_string (ecma_value_t value) /**< ecma-value */ { ecma_check_value_type_is_spec_defined (value); if (unlikely (ecma_is_value_object (value))) { ecma_completion_value_t ret_value = ecma_make_empty_completion_value (); ECMA_TRY_CATCH (prim_value, ecma_op_to_primitive (value, ECMA_PREFERRED_TYPE_STRING), ret_value); ret_value = ecma_op_to_string (prim_value); ECMA_FINALIZE (prim_value); return ret_value; } else { ecma_string_t *res_p = NULL; if (ecma_is_value_string (value)) { res_p = ecma_get_string_from_value (value); res_p = ecma_copy_or_ref_ecma_string (res_p); } else if (ecma_is_value_number (value)) { ecma_number_t *num_p = ecma_get_number_from_value (value); res_p = ecma_new_ecma_string_from_number (*num_p); } else if (ecma_is_value_undefined (value)) { res_p = ecma_get_magic_string (LIT_MAGIC_STRING_UNDEFINED); } else if (ecma_is_value_null (value)) { res_p = ecma_get_magic_string (LIT_MAGIC_STRING_NULL); } else { JERRY_ASSERT (ecma_is_value_boolean (value)); if (ecma_is_value_true (value)) { res_p = ecma_get_magic_string (LIT_MAGIC_STRING_TRUE); } else { res_p = ecma_get_magic_string (LIT_MAGIC_STRING_FALSE); } } return ecma_make_normal_completion_value (ecma_make_string_value (res_p)); } } /* ecma_op_to_string */
/** * Common function to create a formatted JSON string. * * See also: * ECMA-262 v5, 15.12.3 * * Used by: * - ecma_builtin_json_object step 10.b * - ecma_builtin_json_array step 10.b * * @return ecma value * Returned value must be freed with ecma_free_value. */ ecma_value_t ecma_builtin_helper_json_create_formatted_json (ecma_string_t *left_bracket_p, /**< left bracket*/ ecma_string_t *right_bracket_p, /**< right bracket*/ ecma_string_t *stepback_p, /**< stepback*/ ecma_collection_header_t *partial_p, /**< key-value pairs*/ ecma_json_stringify_context_t *context_p) /**< context*/ { /* 10.b */ ecma_string_t *comma_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_COMMA_CHAR); ecma_string_t *line_feed_p = ecma_get_magic_string (LIT_MAGIC_STRING_NEW_LINE_CHAR); ecma_string_t *properties_str_p; ecma_string_t *separator_p; /* 10.b.i */ ecma_string_t *tmp_str_p = ecma_concat_ecma_strings (comma_str_p, line_feed_p); ecma_deref_ecma_string (comma_str_p); separator_p = tmp_str_p; tmp_str_p = ecma_concat_ecma_strings (separator_p, context_p->indent_str_p); ecma_deref_ecma_string (separator_p); separator_p = tmp_str_p; /* 10.b.ii */ properties_str_p = ecma_builtin_helper_json_create_separated_properties (partial_p, separator_p); ecma_deref_ecma_string (separator_p); /* 10.b.iii */ ecma_string_t *final_str_p; tmp_str_p = ecma_concat_ecma_strings (left_bracket_p, line_feed_p); final_str_p = tmp_str_p; tmp_str_p = ecma_concat_ecma_strings (final_str_p, context_p->indent_str_p); ecma_deref_ecma_string (final_str_p); final_str_p = tmp_str_p; tmp_str_p = ecma_concat_ecma_strings (final_str_p, properties_str_p); ecma_deref_ecma_string (final_str_p); ecma_deref_ecma_string (properties_str_p); final_str_p = tmp_str_p; tmp_str_p = ecma_concat_ecma_strings (final_str_p, line_feed_p); ecma_deref_ecma_string (line_feed_p); ecma_deref_ecma_string (final_str_p); final_str_p = tmp_str_p; tmp_str_p = ecma_concat_ecma_strings (final_str_p, stepback_p); ecma_deref_ecma_string (final_str_p); final_str_p = tmp_str_p; tmp_str_p = ecma_concat_ecma_strings (final_str_p, right_bracket_p); ecma_deref_ecma_string (final_str_p); final_str_p = tmp_str_p; return ecma_make_string_value (final_str_p); } /* ecma_builtin_helper_json_create_formatted_json */
/** * The String.prototype object's 'trim' routine * * See also: * ECMA-262 v5, 15.5.4.20 * * @return completion value * Returned value must be freed with ecma_free_completion_value. */ static ecma_completion_value_t ecma_builtin_string_prototype_object_trim (ecma_value_t this_arg) /**< this argument */ { ecma_completion_value_t ret_value = ecma_make_empty_completion_value (); /* 1 */ ECMA_TRY_CATCH (check_coercible_val, ecma_op_check_object_coercible (this_arg), ret_value); /* 2 */ ECMA_TRY_CATCH (to_string_val, ecma_op_to_string (this_arg), ret_value); ecma_string_t *original_string_p = ecma_get_string_from_value (to_string_val); /* 3 */ const lit_utf8_size_t size = ecma_string_get_size (original_string_p); const ecma_length_t length = ecma_string_get_size (original_string_p); /* Workaround: avoid repeated call of ecma_string_get_char_at_pos() because its overhead */ lit_utf8_byte_t *original_utf8_str_p = (lit_utf8_byte_t *) mem_heap_alloc_block (size + 1, MEM_HEAP_ALLOC_SHORT_TERM); ecma_string_to_utf8_string (original_string_p, original_utf8_str_p, (ssize_t) size); uint32_t prefix = 0, postfix = 0; uint32_t new_len = 0; while (prefix < length && isspace (lit_utf8_string_code_unit_at (original_utf8_str_p, size, prefix))) { prefix++; } while (postfix < length - prefix && isspace (lit_utf8_string_code_unit_at (original_utf8_str_p, size, length - postfix - 1))) { postfix++; } new_len = prefix < size ? size - prefix - postfix : 0; ecma_string_t *new_str_p = ecma_string_substr (original_string_p, prefix, prefix + new_len); /* 4 */ ret_value = ecma_make_normal_completion_value (ecma_make_string_value (new_str_p)); mem_heap_free_block (original_utf8_str_p); ECMA_FINALIZE (to_string_val); ECMA_FINALIZE (check_coercible_val); return ret_value; } /* ecma_builtin_string_prototype_object_trim */
/** * Copy ecma value. * * Note: * Operation algorithm. * switch (valuetype) * case simple: * simply return the value as it was passed; * case number: * copy the number * and return new ecma value * pointing to copy of the number; * case string: * increase reference counter of the string * and return the value as it was passed. * case object; * increase reference counter of the object if do_ref_if_object is true * and return the value as it was passed. * * @return See note. */ ecma_value_t ecma_copy_value (ecma_value_t value, /**< ecma value */ bool do_ref_if_object) /**< if the value is object value, increment reference counter of the object */ { ecma_value_t value_copy = 0; switch (ecma_get_value_type_field (value)) { case ECMA_TYPE_SIMPLE: { value_copy = value; break; } case ECMA_TYPE_NUMBER: { ecma_number_t *num_p = ecma_get_number_from_value (value); ecma_number_t *number_copy_p = ecma_alloc_number (); *number_copy_p = *num_p; value_copy = ecma_make_number_value (number_copy_p); break; } case ECMA_TYPE_STRING: { ecma_string_t *string_p = ecma_get_string_from_value (value); string_p = ecma_copy_or_ref_ecma_string (string_p); value_copy = ecma_make_string_value (string_p); break; } case ECMA_TYPE_OBJECT: { ecma_object_t *obj_p = ecma_get_object_from_value (value); if (do_ref_if_object) { ecma_ref_object (obj_p); } value_copy = value; break; } } return value_copy; } /* ecma_copy_value */
/** * The RegExp.prototype object's 'exec' routine * * See also: * ECMA-262 v5, 15.10.6.2 * * @return array object containing the results - if the matched * null - otherwise * * May raise error, so returned value must be freed with ecma_free_value. */ static ecma_value_t ecma_builtin_regexp_prototype_exec (ecma_value_t this_arg, /**< this argument */ ecma_value_t arg) /**< routine's argument */ { ecma_value_t ret_value = ecma_make_simple_value (ECMA_SIMPLE_VALUE_EMPTY); if (!ecma_is_value_object (this_arg) || ecma_object_get_class_name (ecma_get_object_from_value (this_arg)) != LIT_MAGIC_STRING_REGEXP_UL) { ret_value = ecma_raise_type_error (ECMA_ERR_MSG ("Incomplete RegExp type")); } else { ECMA_TRY_CATCH (obj_this, ecma_op_to_object (this_arg), ret_value); ECMA_TRY_CATCH (input_str_value, ecma_op_to_string (arg), ret_value); ecma_property_t *bytecode_prop_p; ecma_object_t *obj_p = ecma_get_object_from_value (obj_this); bytecode_prop_p = ecma_get_internal_property (obj_p, ECMA_INTERNAL_PROPERTY_REGEXP_BYTECODE); void *bytecode_p = ECMA_GET_INTERNAL_VALUE_POINTER (void, ecma_get_internal_property_value (bytecode_prop_p)); if (bytecode_p == NULL) { /* Missing bytecode means empty RegExp: '/(?:)/', so always return empty string. */ ecma_string_t *capture_str_p = ecma_get_magic_string (LIT_MAGIC_STRING__EMPTY); ecma_value_t arguments_list[1]; arguments_list[0] = ecma_make_string_value (capture_str_p); ret_value = ecma_op_create_array_object (arguments_list, 1, false); ecma_deref_ecma_string (capture_str_p); re_set_result_array_properties (ecma_get_object_from_value (ret_value), ecma_get_string_from_value (input_str_value), 1, 0); } else { ret_value = ecma_regexp_exec_helper (obj_this, input_str_value, false); } ECMA_FINALIZE (input_str_value); ECMA_FINALIZE (obj_this); } return ret_value; } /* ecma_builtin_regexp_prototype_exec */
/** * The String object's 'fromCharCode' routine * * See also: * ECMA-262 v5, 15.5.3.2 * * @return ecma value * Returned value must be freed with ecma_free_value. */ static ecma_value_t ecma_builtin_string_object_from_char_code (ecma_value_t this_arg, /**< 'this' argument */ const ecma_value_t args[], /**< arguments list */ ecma_length_t args_number) /**< number of arguments */ { JERRY_UNUSED (this_arg); if (args_number == 0) { return ecma_make_magic_string_value (LIT_MAGIC_STRING__EMPTY); } ecma_value_t ret_value = ECMA_VALUE_EMPTY; ecma_string_t *ret_string_p = NULL; lit_utf8_size_t utf8_buf_size = args_number * LIT_CESU8_MAX_BYTES_IN_CODE_UNIT; JMEM_DEFINE_LOCAL_ARRAY (utf8_buf_p, utf8_buf_size, lit_utf8_byte_t); lit_utf8_size_t utf8_buf_used = 0; for (ecma_length_t arg_index = 0; arg_index < args_number && ecma_is_value_empty (ret_value); arg_index++) { ECMA_OP_TO_NUMBER_TRY_CATCH (arg_num, args[arg_index], ret_value); uint32_t uint32_char_code = ecma_number_to_uint32 (arg_num); ecma_char_t code_unit = (uint16_t) uint32_char_code; JERRY_ASSERT (utf8_buf_used <= utf8_buf_size - LIT_UTF8_MAX_BYTES_IN_CODE_UNIT); utf8_buf_used += lit_code_unit_to_utf8 (code_unit, utf8_buf_p + utf8_buf_used); JERRY_ASSERT (utf8_buf_used <= utf8_buf_size); ECMA_OP_TO_NUMBER_FINALIZE (arg_num); } if (ecma_is_value_empty (ret_value)) { ret_string_p = ecma_new_ecma_string_from_utf8 (utf8_buf_p, utf8_buf_used); } JMEM_FINALIZE_LOCAL_ARRAY (utf8_buf_p); if (ecma_is_value_empty (ret_value)) { ret_value = ecma_make_string_value (ret_string_p); } return ret_value; } /* ecma_builtin_string_object_from_char_code */
/** * The Function.prototype object's 'toString' routine * * See also: * ECMA-262 v5, 15.3.4.2 * * @return completion value * Returned value must be freed with ecma_free_completion_value. */ static ecma_completion_value_t ecma_builtin_function_prototype_object_to_string (ecma_value_t this_arg) /**< this argument */ { ecma_completion_value_t ret_value = ecma_make_empty_completion_value (); if (!ecma_op_is_callable (this_arg)) { ret_value = ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_TYPE)); } else { ecma_string_t *function_to_string_p = ecma_get_magic_string (LIT_MAGIC_STRING__FUNCTION_TO_STRING); ret_value = ecma_make_normal_completion_value (ecma_make_string_value (function_to_string_p)); } return ret_value; } /* ecma_builtin_function_prototype_object_to_string */
/** * The Function.prototype object's 'toString' routine * * See also: * ECMA-262 v5, 15.3.4.2 * * @return ecma value * Returned value must be freed with ecma_free_value. */ static ecma_value_t ecma_builtin_function_prototype_object_to_string (ecma_value_t this_arg) /**< this argument */ { ecma_value_t ret_value = ecma_make_simple_value (ECMA_SIMPLE_VALUE_EMPTY); if (!ecma_op_is_callable (this_arg)) { ret_value = ecma_raise_type_error (ECMA_ERR_MSG ("")); } else { ecma_string_t *function_to_string_p = ecma_get_magic_string (LIT_MAGIC_STRING__FUNCTION_TO_STRING); ret_value = ecma_make_string_value (function_to_string_p); } return ret_value; } /* ecma_builtin_function_prototype_object_to_string */
/** * 'typeof' opcode handler. * * See also: ECMA-262 v5, 11.4.3 * * @return ecma value * Returned value must be freed with ecma_free_value */ ecma_value_t opfunc_typeof (ecma_value_t left_value) /**< left value */ { ecma_value_t ret_value = ecma_make_simple_value (ECMA_SIMPLE_VALUE_EMPTY); ecma_string_t *type_str_p = NULL; if (ecma_is_value_undefined (left_value)) { type_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_UNDEFINED); } else if (ecma_is_value_null (left_value)) { type_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_OBJECT); } else if (ecma_is_value_boolean (left_value)) { type_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_BOOLEAN); } else if (ecma_is_value_number (left_value)) { type_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_NUMBER); } else if (ecma_is_value_string (left_value)) { type_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_STRING); } else { JERRY_ASSERT (ecma_is_value_object (left_value)); if (ecma_op_is_callable (left_value)) { type_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_FUNCTION); } else { type_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_OBJECT); } } ret_value = ecma_make_string_value (type_str_p); return ret_value; } /* opfunc_typeof */
/** * Standard ecma-error object constructor. * * @return pointer to ecma-object representing specified error * with reference counter set to one. */ ecma_object_t * ecma_new_standard_error_with_message (ecma_standard_error_t error_type, /**< native error type */ ecma_string_t *message_string_p) /**< message string */ { ecma_object_t *new_error_obj_p = ecma_new_standard_error (error_type); ecma_string_t *message_magic_string_p = ecma_get_magic_string (LIT_MAGIC_STRING_MESSAGE); ecma_property_t *prop_p = ecma_create_named_data_property (new_error_obj_p, message_magic_string_p, ECMA_PROPERTY_CONFIGURABLE_WRITABLE); ecma_ref_ecma_string (message_string_p); ecma_set_named_data_property_value (prop_p, ecma_make_string_value (message_string_p)); ecma_deref_ecma_string (message_magic_string_p); return new_error_obj_p; } /* ecma_new_standard_error_with_message */
/** * List names of a String object's lazy instantiated properties * * @return string values collection */ void ecma_op_string_list_lazy_property_names (ecma_object_t *obj_p, /**< a String object */ bool separate_enumerable, /**< true - list enumerable properties * into main collection, * and non-enumerable to collection of * 'skipped non-enumerable' properties, * false - list all properties into main * collection. */ ecma_collection_header_t *main_collection_p, /**< 'main' * collection */ ecma_collection_header_t *non_enum_collection_p) /**< skipped * 'non-enumerable' * collection */ { JERRY_ASSERT (ecma_get_object_type (obj_p) == ECMA_OBJECT_TYPE_CLASS); ecma_collection_header_t *for_enumerable_p = main_collection_p; ecma_collection_header_t *for_non_enumerable_p = separate_enumerable ? non_enum_collection_p : main_collection_p; ecma_extended_object_t *ext_object_p = (ecma_extended_object_t *) obj_p; JERRY_ASSERT (ext_object_p->u.class_prop.class_id == LIT_MAGIC_STRING_STRING_UL); ecma_string_t *prim_value_str_p = ecma_get_string_from_value (ext_object_p->u.class_prop.u.value); ecma_length_t length = ecma_string_get_length (prim_value_str_p); for (ecma_length_t i = 0; i < length; i++) { ecma_string_t *name_p = ecma_new_ecma_string_from_uint32 (i); /* the properties are enumerable (ECMA-262 v5, 15.5.5.2.9) */ ecma_append_to_values_collection (for_enumerable_p, ecma_make_string_value (name_p), 0); ecma_deref_ecma_string (name_p); } ecma_append_to_values_collection (for_non_enumerable_p, ecma_make_magic_string_value (LIT_MAGIC_STRING_LENGTH), 0); } /* ecma_op_string_list_lazy_property_names */
/** * Allocate a collection of ecma-strings. * * @return pointer to the collection's header */ ecma_collection_header_t * ecma_new_strings_collection (ecma_string_t *string_ptrs_buffer[], /**< pointers to ecma-strings */ ecma_length_t strings_number) /**< number of ecma-strings */ { JERRY_ASSERT (string_ptrs_buffer != NULL || strings_number == 0); ecma_collection_header_t *new_collection_p; new_collection_p = ecma_new_values_collection (NULL, 0, false); for (ecma_length_t string_index = 0; string_index < strings_number; string_index++) { ecma_append_to_values_collection (new_collection_p, ecma_make_string_value (string_ptrs_buffer[string_index]), false); } return new_collection_p; } /* ecma_new_strings_collection */
/** * List names of a String object's lazy instantiated properties * * See also: * ecma_op_string_object_get_own_property * * @return string values collection */ void ecma_op_string_list_lazy_property_names (ecma_object_t *obj_p, /**< a String object */ bool separate_enumerable, /**< true - list enumerable properties * into main collection, * and non-enumerable to collection of * 'skipped non-enumerable' properties, * false - list all properties into main * collection. */ ecma_collection_header_t *main_collection_p, /**< 'main' * collection */ ecma_collection_header_t *non_enum_collection_p) /**< skipped * 'non-enumerable' * collection */ { JERRY_ASSERT (ecma_get_object_type (obj_p) == ECMA_OBJECT_TYPE_STRING); ecma_collection_header_t *for_enumerable_p = main_collection_p; ecma_collection_header_t *for_non_enumerable_p = separate_enumerable ? main_collection_p : non_enum_collection_p; JERRY_UNUSED (for_non_enumerable_p); ecma_property_t *prim_value_prop_p = ecma_get_internal_property (obj_p, ECMA_INTERNAL_PROPERTY_ECMA_VALUE); ecma_string_t *prim_value_str_p; prim_value_str_p = ecma_get_string_from_value (ecma_get_internal_property_value (prim_value_prop_p)); ecma_length_t length = ecma_string_get_length (prim_value_str_p); for (ecma_length_t i = 0; i < length; i++) { ecma_string_t *name_p = ecma_new_ecma_string_from_uint32 (i); /* the properties are enumerable (ECMA-262 v5, 15.5.5.2.9) */ ecma_append_to_values_collection (for_enumerable_p, ecma_make_string_value (name_p), true); ecma_deref_ecma_string (name_p); } } /* ecma_op_string_list_lazy_property_names */
/** * Handle calling [[Call]] of built-in String object * * @return completion-value */ ecma_completion_value_t ecma_builtin_string_dispatch_call (const ecma_value_t *arguments_list_p, /**< arguments list */ ecma_length_t arguments_list_len) /**< number of arguments */ { JERRY_ASSERT (arguments_list_len == 0 || arguments_list_p != NULL); ecma_completion_value_t ret_value = ecma_make_empty_completion_value (); if (arguments_list_len == 0) { ecma_string_t *str_p = ecma_new_ecma_string_from_magic_string_id (LIT_MAGIC_STRING__EMPTY); ecma_value_t str_value = ecma_make_string_value (str_p); ret_value = ecma_make_normal_completion_value (str_value); } else { ret_value = ecma_op_to_string (arguments_list_p[0]); } return ret_value; } /* ecma_builtin_string_dispatch_call */
/** * The Date.prototype object's 'toDateString' routine * * See also: * ECMA-262 v5, 15.9.5.3 * * @return ecma value * Returned value must be freed with ecma_free_value. */ static ecma_value_t ecma_builtin_date_prototype_to_date_string (ecma_value_t this_arg) /**< this argument */ { ecma_value_t ret_value = ecma_make_simple_value (ECMA_SIMPLE_VALUE_EMPTY); if (!ecma_is_value_object (this_arg) || ecma_object_get_class_name (ecma_get_object_from_value (this_arg)) != LIT_MAGIC_STRING_DATE_UL) { ret_value = ecma_raise_type_error (ECMA_ERR_MSG ("Incompatible type")); } else { ECMA_TRY_CATCH (obj_this, ecma_op_to_object (this_arg), ret_value); ecma_object_t *obj_p = ecma_get_object_from_value (obj_this); ecma_property_t *prim_prop_p = ecma_get_internal_property (obj_p, ECMA_INTERNAL_PROPERTY_PRIMITIVE_NUMBER_VALUE); ecma_number_t *prim_value_num_p = ECMA_GET_INTERNAL_VALUE_POINTER (ecma_number_t, ecma_get_internal_property_value (prim_prop_p)); if (ecma_number_is_nan (*prim_value_num_p)) { ecma_string_t *magic_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_INVALID_DATE_UL); ret_value = ecma_make_string_value (magic_str_p); } else { ret_value = ecma_date_value_to_date_string (*prim_value_num_p); } ECMA_FINALIZE (obj_this); } return ret_value; } /* ecma_builtin_date_prototype_to_date_string */
ecma_completion_value_t ecma_builtin_helper_object_to_string (const ecma_value_t this_arg) /**< this argument */ { lit_magic_string_id_t type_string; if (ecma_is_value_undefined (this_arg)) { type_string = LIT_MAGIC_STRING_UNDEFINED_UL; } else if (ecma_is_value_null (this_arg)) { type_string = LIT_MAGIC_STRING_NULL_UL; } else { ecma_completion_value_t obj_this = ecma_op_to_object (this_arg); if (!ecma_is_completion_value_normal (obj_this)) { return obj_this; } JERRY_ASSERT (ecma_is_value_object (ecma_get_completion_value_value (obj_this))); ecma_object_t *obj_p = ecma_get_object_from_completion_value (obj_this); type_string = ecma_object_get_class_name (obj_p); ecma_free_completion_value (obj_this); } ecma_string_t *ret_string_p; /* Building string "[object #type#]" where type is 'Undefined', 'Null' or one of possible object's classes. The string with null character is maximum 19 characters long. */ const ssize_t buffer_size = 19; MEM_DEFINE_LOCAL_ARRAY (str_buffer, buffer_size, lit_utf8_byte_t); lit_utf8_byte_t *buffer_ptr = str_buffer; ssize_t buffer_size_left = buffer_size; 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) { buffer_ptr = lit_copy_magic_string_to_buffer (magic_string_ids[i], buffer_ptr, buffer_size_left); buffer_size_left = buffer_size - (buffer_ptr - str_buffer); } JERRY_ASSERT (buffer_size_left >= 0); ret_string_p = ecma_new_ecma_string_from_utf8 (str_buffer, (lit_utf8_size_t) (buffer_size - buffer_size_left)); MEM_FINALIZE_LOCAL_ARRAY (str_buffer); return ecma_make_normal_completion_value (ecma_make_string_value (ret_string_p)); } /* ecma_builtin_helper_object_to_string */
/** * The Object.keys and Object.getOwnPropertyName routine's common part. * * See also: * ECMA-262 v5, 15.2.3.4 steps 2-5 * ECMA-262 v5, 15.2.3.14 steps 3-6 * * @return completion value - Array of property names. * Returned value must be freed with ecma_free_completion_value. */ ecma_completion_value_t ecma_builtin_helper_object_get_properties (ecma_object_t *obj_p, /** < object */ bool only_enumerable_properties) /** < list enumerable properties? */ { JERRY_ASSERT (obj_p != NULL); ecma_completion_value_t new_array = ecma_op_create_array_object (NULL, 0, false); JERRY_ASSERT (ecma_is_completion_value_normal (new_array)); ecma_object_t *new_array_p = ecma_get_object_from_completion_value (new_array); uint32_t index = 0; for (ecma_property_t *property_p = ecma_get_property_list (obj_p); property_p != NULL; property_p = ECMA_GET_POINTER (ecma_property_t, property_p->next_property_p)) { ecma_string_t *property_name_p; if (property_p->type == ECMA_PROPERTY_NAMEDDATA) { property_name_p = ECMA_GET_NON_NULL_POINTER (ecma_string_t, property_p->u.named_data_property.name_p); } else if (property_p->type == ECMA_PROPERTY_NAMEDACCESSOR) { property_name_p = ECMA_GET_NON_NULL_POINTER (ecma_string_t, property_p->u.named_accessor_property.name_p); } else { continue; } if (only_enumerable_properties && !ecma_is_property_enumerable (property_p)) { continue; } JERRY_ASSERT (property_name_p != NULL); ecma_string_t *index_string_p = ecma_new_ecma_string_from_uint32 (index); ecma_property_descriptor_t item_prop_desc = ecma_make_empty_property_descriptor (); { item_prop_desc.is_value_defined = true; item_prop_desc.value = ecma_make_string_value (property_name_p); item_prop_desc.is_writable_defined = true; item_prop_desc.is_writable = true; item_prop_desc.is_enumerable_defined = true; item_prop_desc.is_enumerable = true; item_prop_desc.is_configurable_defined = true; item_prop_desc.is_configurable = true; } ecma_completion_value_t completion = ecma_op_object_define_own_property (new_array_p, index_string_p, &item_prop_desc, false); JERRY_ASSERT (ecma_is_completion_value_normal_true (completion)); ecma_free_completion_value (completion); ecma_deref_ecma_string (index_string_p); index++; } return new_array; } /* ecma_builtin_helper_object_get_properties */
/** * RegExp helper function to start the recursive matching algorithm * and create the result Array object * * @return completion value * Returned value must be freed with ecma_free_completion_value */ ecma_completion_value_t ecma_regexp_exec_helper (ecma_value_t regexp_value, /**< RegExp object */ ecma_value_t input_string, /**< input string */ bool ignore_global) /**< ignore global flag */ { ecma_completion_value_t ret_value = ecma_make_empty_completion_value (); JERRY_ASSERT (ecma_is_value_object (regexp_value)); JERRY_ASSERT (ecma_is_value_string (input_string)); ecma_object_t *regexp_object_p = ecma_get_object_from_value (regexp_value); JERRY_ASSERT (ecma_object_get_class_name (regexp_object_p) == LIT_MAGIC_STRING_REGEXP_UL); ecma_property_t *bytecode_prop_p = ecma_get_internal_property (regexp_object_p, ECMA_INTERNAL_PROPERTY_REGEXP_BYTECODE); re_bytecode_t *bc_p = ECMA_GET_POINTER (re_bytecode_t, bytecode_prop_p->u.internal_property.value); ecma_string_t *input_string_p = ecma_get_string_from_value (input_string); lit_utf8_size_t input_string_size = ecma_string_get_size (input_string_p); MEM_DEFINE_LOCAL_ARRAY (input_utf8_buffer_p, input_string_size, lit_utf8_byte_t); ecma_string_to_utf8_string (input_string_p, input_utf8_buffer_p, (ssize_t) input_string_size); lit_utf8_iterator_t iterator = lit_utf8_iterator_create (input_utf8_buffer_p, input_string_size); re_matcher_ctx_t re_ctx; re_ctx.input_start_p = iterator.buf_p; re_ctx.input_end_p = iterator.buf_p + iterator.buf_size; /* 1. Read bytecode header and init regexp matcher context. */ re_ctx.flags = (uint8_t) re_get_value (&bc_p); if (ignore_global) { re_ctx.flags &= (uint8_t) ~RE_FLAG_GLOBAL; } JERRY_DDLOG ("Exec with flags [global: %d, ignoreCase: %d, multiline: %d]\n", re_ctx.flags & RE_FLAG_GLOBAL, re_ctx.flags & RE_FLAG_IGNORE_CASE, re_ctx.flags & RE_FLAG_MULTILINE); re_ctx.num_of_captures = re_get_value (&bc_p); JERRY_ASSERT (re_ctx.num_of_captures % 2 == 0); re_ctx.num_of_non_captures = re_get_value (&bc_p); /* We create an invalid iterator, that will be used to identify unused result values. */ lit_utf8_iterator_t unused_iter = lit_utf8_iterator_create (NULL, 0); unused_iter.buf_p = (lit_utf8_byte_t *) 1; MEM_DEFINE_LOCAL_ARRAY (saved_p, re_ctx.num_of_captures + re_ctx.num_of_non_captures, lit_utf8_iterator_t); for (uint32_t i = 0; i < re_ctx.num_of_captures + re_ctx.num_of_non_captures; i++) { saved_p[i] = unused_iter; } re_ctx.saved_p = saved_p; uint32_t num_of_iter_length = (re_ctx.num_of_captures / 2) + (re_ctx.num_of_non_captures - 1); MEM_DEFINE_LOCAL_ARRAY (num_of_iter_p, num_of_iter_length, uint32_t); for (uint32_t i = 0; i < num_of_iter_length; i++) { num_of_iter_p[i] = 0u; } bool is_match = false; re_ctx.num_of_iterations_p = num_of_iter_p; int32_t index = 0; ecma_length_t input_str_len = lit_utf8_string_length (iterator.buf_p, iterator.buf_size); if (iterator.buf_p && (re_ctx.flags & RE_FLAG_GLOBAL)) { ecma_string_t *magic_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_LASTINDEX_UL); ecma_property_t *lastindex_prop_p = ecma_op_object_get_property (regexp_object_p, magic_str_p); ECMA_OP_TO_NUMBER_TRY_CATCH (lastindex_num, lastindex_prop_p->u.named_data_property.value, ret_value) index = ecma_number_to_int32 (lastindex_num); JERRY_ASSERT (iterator.buf_pos.offset == 0 && !iterator.buf_pos.is_non_bmp_middle); if (!lit_utf8_iterator_is_eos (&iterator) && index <= (int32_t) input_str_len && index > 0) { lit_utf8_iterator_advance (&iterator, (ecma_length_t) index); } ECMA_OP_TO_NUMBER_FINALIZE (lastindex_num); ecma_deref_ecma_string (magic_str_p); } /* 2. Try to match */ lit_utf8_iterator_t sub_iter = lit_utf8_iterator_create (NULL, 0); while (ecma_is_completion_value_empty (ret_value)) { if (index < 0 || index > (int32_t) input_str_len) { if (re_ctx.flags & RE_FLAG_GLOBAL) { ecma_string_t *magic_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_LASTINDEX_UL); ecma_number_t *lastindex_num_p = ecma_alloc_number (); *lastindex_num_p = ECMA_NUMBER_ZERO; ecma_op_object_put (regexp_object_p, magic_str_p, ecma_make_number_value (lastindex_num_p), true); ecma_dealloc_number (lastindex_num_p); ecma_deref_ecma_string (magic_str_p); } is_match = false; break; } else { ECMA_TRY_CATCH (match_value, re_match_regexp (&re_ctx, bc_p, iterator, &sub_iter), ret_value); if (ecma_is_value_true (match_value)) { is_match = true; break; } if (!lit_utf8_iterator_is_eos (&iterator)) { lit_utf8_iterator_advance (&iterator, 1); } index++; ECMA_FINALIZE (match_value); } } if (iterator.buf_p && (re_ctx.flags & RE_FLAG_GLOBAL)) { ecma_string_t *magic_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_LASTINDEX_UL); ecma_number_t *lastindex_num_p = ecma_alloc_number (); *lastindex_num_p = sub_iter.buf_pos.offset; ecma_op_object_put (regexp_object_p, magic_str_p, ecma_make_number_value (lastindex_num_p), true); ecma_dealloc_number (lastindex_num_p); ecma_deref_ecma_string (magic_str_p); } /* 3. Fill the result array or return with 'undefiend' */ if (ecma_is_completion_value_empty (ret_value)) { if (is_match) { ecma_completion_value_t result_array = ecma_op_create_array_object (0, 0, false); ecma_object_t *result_array_obj_p = ecma_get_object_from_completion_value (result_array); ecma_string_t *input_str_p = ecma_new_ecma_string_from_utf8 (iterator.buf_p, iterator.buf_size); re_set_result_array_properties (result_array_obj_p, input_str_p, re_ctx.num_of_captures / 2, index); ecma_deref_ecma_string (input_str_p); for (uint32_t i = 0; i < re_ctx.num_of_captures; i += 2) { ecma_string_t *index_str_p = ecma_new_ecma_string_from_uint32 (i / 2); /* Note: 'iter_p->buf_p == NULL' means the input is empty string */ if ((re_ctx.saved_p[i].buf_p != unused_iter.buf_p && re_ctx.saved_p[i + 1].buf_p != unused_iter.buf_p) && re_ctx.saved_p[i + 1].buf_pos.offset >= re_ctx.saved_p[i].buf_pos.offset) { ecma_length_t capture_str_len; capture_str_len = (ecma_length_t) re_ctx.saved_p[i + 1].buf_pos.offset - re_ctx.saved_p[i].buf_pos.offset; ecma_string_t *capture_str_p; if (capture_str_len > 0) { const lit_utf8_byte_t *utf8_str_p = re_ctx.saved_p[i].buf_p + re_ctx.saved_p[i].buf_pos.offset; capture_str_p = ecma_new_ecma_string_from_utf8 (utf8_str_p, capture_str_len); } else { capture_str_p = ecma_get_magic_string (LIT_MAGIC_STRING__EMPTY); } ecma_op_object_put (result_array_obj_p, index_str_p, ecma_make_string_value (capture_str_p), true); ecma_deref_ecma_string (capture_str_p); } else { ecma_op_object_put (result_array_obj_p, index_str_p, ecma_make_simple_value (ECMA_SIMPLE_VALUE_UNDEFINED), true); } ecma_deref_ecma_string (index_str_p); } ret_value = result_array; } else { ret_value = ecma_make_normal_completion_value (ecma_make_simple_value (ECMA_SIMPLE_VALUE_NULL)); } } MEM_FINALIZE_LOCAL_ARRAY (num_of_iter_p); MEM_FINALIZE_LOCAL_ARRAY (saved_p); MEM_FINALIZE_LOCAL_ARRAY (input_utf8_buffer_p); return ret_value; } /* ecma_regexp_exec_helper */
/* * Initializes the source, global, ignoreCase, multiline, and lastIndex properties of RegExp instance. */ void re_initialize_props (ecma_object_t *re_obj_p, /**< RegExp obejct */ ecma_string_t *source_p, /**< source string */ uint8_t flags) /**< flags */ { /* Set source property. ECMA-262 v5, 15.10.7.1 */ ecma_string_t *magic_string_p = ecma_get_magic_string (LIT_MAGIC_STRING_SOURCE); ecma_property_t *prop_p = ecma_find_named_property (re_obj_p, magic_string_p); if (prop_p == NULL) { prop_p = ecma_create_named_data_property (re_obj_p, magic_string_p, false, false, false); } ecma_deref_ecma_string (magic_string_p); JERRY_ASSERT (prop_p->type == ECMA_PROPERTY_NAMEDDATA); ecma_named_data_property_assign_value (re_obj_p, prop_p, ecma_make_string_value (source_p)); ecma_simple_value_t prop_value; /* Set global property. ECMA-262 v5, 15.10.7.2 */ magic_string_p = ecma_get_magic_string (LIT_MAGIC_STRING_GLOBAL); prop_p = ecma_find_named_property (re_obj_p, magic_string_p); if (prop_p == NULL) { prop_p = ecma_create_named_data_property (re_obj_p, magic_string_p, false, false, false); } ecma_deref_ecma_string (magic_string_p); prop_value = flags & RE_FLAG_GLOBAL ? ECMA_SIMPLE_VALUE_TRUE : ECMA_SIMPLE_VALUE_FALSE; JERRY_ASSERT (prop_p->type == ECMA_PROPERTY_NAMEDDATA); ecma_set_named_data_property_value (prop_p, ecma_make_simple_value (prop_value)); /* Set ignoreCase property. ECMA-262 v5, 15.10.7.3 */ magic_string_p = ecma_get_magic_string (LIT_MAGIC_STRING_IGNORECASE_UL); prop_p = ecma_find_named_property (re_obj_p, magic_string_p); if (prop_p == NULL) { prop_p = ecma_create_named_data_property (re_obj_p, magic_string_p, false, false, false); } ecma_deref_ecma_string (magic_string_p); prop_value = flags & RE_FLAG_IGNORE_CASE ? ECMA_SIMPLE_VALUE_TRUE : ECMA_SIMPLE_VALUE_FALSE; JERRY_ASSERT (prop_p->type == ECMA_PROPERTY_NAMEDDATA); ecma_set_named_data_property_value (prop_p, ecma_make_simple_value (prop_value)); /* Set multiline property. ECMA-262 v5, 15.10.7.4 */ magic_string_p = ecma_get_magic_string (LIT_MAGIC_STRING_MULTILINE); prop_p = ecma_find_named_property (re_obj_p, magic_string_p); if (prop_p == NULL) { prop_p = ecma_create_named_data_property (re_obj_p, magic_string_p, false, false, false); } ecma_deref_ecma_string (magic_string_p); prop_value = flags & RE_FLAG_MULTILINE ? ECMA_SIMPLE_VALUE_TRUE : ECMA_SIMPLE_VALUE_FALSE; JERRY_ASSERT (prop_p->type == ECMA_PROPERTY_NAMEDDATA); ecma_set_named_data_property_value (prop_p, ecma_make_simple_value (prop_value)); /* Set lastIndex property. ECMA-262 v5, 15.10.7.5 */ magic_string_p = ecma_get_magic_string (LIT_MAGIC_STRING_LASTINDEX_UL); prop_p = ecma_find_named_property (re_obj_p, magic_string_p); if (prop_p == NULL) { prop_p = ecma_create_named_data_property (re_obj_p, magic_string_p, true, false, false); } ecma_deref_ecma_string (magic_string_p); ecma_number_t *lastindex_num_p = ecma_alloc_number (); *lastindex_num_p = ECMA_NUMBER_ZERO; JERRY_ASSERT (prop_p->type == ECMA_PROPERTY_NAMEDDATA); ecma_named_data_property_assign_value (re_obj_p, prop_p, ecma_make_number_value (lastindex_num_p)); ecma_dealloc_number (lastindex_num_p); } /* re_initialize_props */