/** * Construct property descriptor from specified property * * @return property descriptor, corresponding to type and content of the specified property, i.e.: * - for named data properties: { [Value], [Writable], [Enumerable], [Configurable] }; * - for named accessor properties: { [Get] - if defined, * [Set] - if defined, * [Enumerable], [Configurable] * }. */ ecma_property_descriptor_t ecma_get_property_descriptor_from_property (ecma_property_t *prop_p) /**< property */ { ecma_property_descriptor_t prop_desc = ecma_make_empty_property_descriptor (); prop_desc.is_enumerable = ecma_is_property_enumerable (prop_p); prop_desc.is_enumerable_defined = true; prop_desc.is_configurable = ecma_is_property_configurable (prop_p); prop_desc.is_configurable_defined = true; if (ECMA_PROPERTY_GET_TYPE (prop_p) == ECMA_PROPERTY_TYPE_NAMEDDATA) { prop_desc.value = ecma_copy_value (ecma_get_named_data_property_value (prop_p)); prop_desc.is_value_defined = true; prop_desc.is_writable = ecma_is_property_writable (prop_p); prop_desc.is_writable_defined = true; } else { JERRY_ASSERT (ECMA_PROPERTY_GET_TYPE (prop_p) == ECMA_PROPERTY_TYPE_NAMEDACCESSOR); prop_desc.get_p = ecma_get_named_accessor_property_getter (prop_p); prop_desc.is_get_defined = true; if (prop_desc.get_p != NULL) { ecma_ref_object (prop_desc.get_p); } prop_desc.set_p = ecma_get_named_accessor_property_setter (prop_p); prop_desc.is_set_defined = true; if (prop_desc.set_p != NULL) { ecma_ref_object (prop_desc.set_p); } } return prop_desc; } /* ecma_get_property_descriptor_from_property */
/** * 'for-in' opcode handler * * See also: * ECMA-262 v5, 12.6.4 * * @return completion value * Returned value must be freed with ecma_free_value */ ecma_collection_header_t * opfunc_for_in (ecma_value_t left_value, /**< left value */ ecma_value_t *result_obj_p) /**< expression object */ { ecma_value_t compl_val = ecma_make_simple_value (ECMA_SIMPLE_VALUE_EMPTY); ecma_collection_header_t *prop_names_p = NULL; /* 3. */ if (!ecma_is_value_undefined (left_value) && !ecma_is_value_null (left_value)) { /* 4. */ ECMA_TRY_CATCH (obj_expr_value, ecma_op_to_object (left_value), compl_val); ecma_object_t *obj_p = ecma_get_object_from_value (obj_expr_value); prop_names_p = ecma_op_object_get_property_names (obj_p, false, true, true); if (prop_names_p->unit_number != 0) { ecma_ref_object (obj_p); *result_obj_p = ecma_make_object_value (obj_p); } else { ecma_dealloc_collection_header (prop_names_p); prop_names_p = NULL; } ECMA_FINALIZE (obj_expr_value); } JERRY_ASSERT (ecma_is_value_empty (compl_val)); return prop_names_p; } /* opfunc_for_in */
/** * Get size of ecma-string * * @return number of bytes in the buffer needed to represent the string */ lit_utf8_size_t ecma_string_get_size (const ecma_string_t *string_p) /**< ecma-string */ { switch (ECMA_STRING_GET_CONTAINER (string_p)) { case ECMA_STRING_CONTAINER_HEAP_UTF8_STRING: { return (lit_utf8_size_t) string_p->u.utf8_string.size; } case ECMA_STRING_CONTAINER_UINT32_IN_DESC: { return (lit_utf8_size_t) ecma_string_get_number_in_desc_size (string_p->u.uint32_number); } case ECMA_STRING_CONTAINER_MAGIC_STRING: { return lit_get_magic_string_size (string_p->u.magic_string_id); } default: { JERRY_ASSERT (ECMA_STRING_GET_CONTAINER (string_p) == ECMA_STRING_CONTAINER_MAGIC_STRING_EX); return lit_get_magic_string_ex_size (string_p->u.magic_string_ex_id); } } } /* ecma_string_get_size */
/** * Try to free some memory (depending on severity). */ void ecma_free_unused_memory (jmem_free_unused_memory_severity_t severity) /**< severity of the request */ { if (severity == JMEM_FREE_UNUSED_MEMORY_SEVERITY_LOW) { /* * If there is enough newly allocated objects since last GC, probably it is worthwhile to start GC now. * Otherwise, probability to free sufficient space is considered to be low. */ size_t new_objects_share = CONFIG_ECMA_GC_NEW_OBJECTS_SHARE_TO_START_GC; if (JERRY_CONTEXT (ecma_gc_new_objects) * new_objects_share > JERRY_CONTEXT (ecma_gc_objects_number)) { ecma_gc_run (severity); } } else { JERRY_ASSERT (severity == JMEM_FREE_UNUSED_MEMORY_SEVERITY_HIGH); /* Freeing as much memory as we currently can */ ecma_gc_run (severity); } } /* ecma_free_unused_memory */
/** * Check the object value existance in the collection. * * Used by: * - ecma_builtin_json_object step 1 * - ecma_builtin_json_array step 1 * * @return true, if the object is already in the collection. */ bool ecma_has_object_value_in_collection (ecma_collection_header_t *collection_p, /**< collection */ ecma_value_t object_value) /**< object value */ { JERRY_ASSERT (ecma_is_value_object (object_value)); ecma_object_t *obj_p = ecma_get_object_from_value (object_value); ecma_collection_iterator_t iterator; ecma_collection_iterator_init (&iterator, collection_p); while (ecma_collection_iterator_next (&iterator)) { ecma_value_t value = *iterator.current_value_p; ecma_object_t *current_p = ecma_get_object_from_value (value); if (current_p == obj_p) { return true; } } return false; } /* ecma_has_object_value_in_collection */
/** * Check the string value existance in the collection. * * Used by: * - ecma_builtin_json_stringify step 4.b.ii.5 * * @return true, if the string is already in the collection. */ bool ecma_has_string_value_in_collection (ecma_collection_header_t *collection_p, /**< collection */ ecma_value_t string_value) /**< string value */ { JERRY_ASSERT (ecma_is_value_string (string_value)); ecma_string_t *string_p = ecma_get_string_from_value (string_value); ecma_collection_iterator_t iterator; ecma_collection_iterator_init (&iterator, collection_p); while (ecma_collection_iterator_next (&iterator)) { ecma_value_t value = *iterator.current_value_p; ecma_string_t *current_p = ecma_get_string_from_value (value); if (ecma_compare_ecma_strings (current_p, string_p)) { return true; } } return false; } /* ecma_has_string_value_in_collection*/
/** * The Number.prototype object's 'valueOf' routine * * See also: * ECMA-262 v5, 15.7.4.4 * * @return ecma value * Returned value must be freed with ecma_free_value. */ static ecma_value_t ecma_builtin_number_prototype_object_value_of (ecma_value_t this_arg) /**< this argument */ { if (ecma_is_value_number (this_arg)) { return ecma_copy_value (this_arg); } 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_NUMBER_UL) { ecma_property_t *prim_value_prop_p = ecma_get_internal_property (obj_p, ECMA_INTERNAL_PROPERTY_ECMA_VALUE); JERRY_ASSERT (ecma_is_value_number (ecma_get_internal_property_value (prim_value_prop_p))); return ecma_copy_value (ecma_get_internal_property_value (prim_value_prop_p)); } } return ecma_raise_type_error (ECMA_ERR_MSG ("")); } /* ecma_builtin_number_prototype_object_value_of */
/** * Helper function to decide if time value is in a leap-year. * * See also: * ECMA-262 v5, 15.9.1.3 * * @return 1 if time within a leap year * 0 otherwise */ static int ecma_date_in_leap_year (ecma_number_t year) /**< time value */ { int mod_400 = (int) fmod (floor (year), 400); JERRY_ASSERT (mod_400 >= -399 && mod_400 <= 399); if ((mod_400 % 4) != 0) { return 0; } if ((mod_400 % 100) != 0) { return 1; } if (mod_400 != 0) { return 0; } return 1; } /* ecma_date_in_leap_year */
/** * Allocation of memory region, running 'try to give memory back' callbacks, if there is not enough memory. * * Note: * if after running the callbacks, there is still not enough memory, engine is terminated with ERR_OUT_OF_MEMORY. * * Note: * To reduce heap fragmentation there are two allocation modes - short-term and long-term. * * If allocation is short-term then the beginning of the heap is preferred, else - the end of the heap. * * It is supposed, that all short-term allocation is used during relatively short discrete sessions. * After end of the session all short-term allocated regions are supposed to be freed. * * @return pointer to allocated memory block */ static void* mem_heap_alloc_block_try_give_memory_back (size_t size_in_bytes, /**< size of region to allocate in bytes */ mem_block_length_type_t length_type, /**< length type of the block * (one-chunked or general) */ mem_heap_alloc_term_t alloc_term) /**< expected allocation term */ { if (mem_heap.allocated_bytes + size_in_bytes >= mem_heap.limit) { mem_run_try_to_give_memory_back_callbacks (MEM_TRY_GIVE_MEMORY_BACK_SEVERITY_LOW); } void *data_space_p = mem_heap_alloc_block_internal (size_in_bytes, length_type, alloc_term); if (likely (data_space_p != NULL)) { return data_space_p; } for (mem_try_give_memory_back_severity_t severity = MEM_TRY_GIVE_MEMORY_BACK_SEVERITY_LOW; severity <= MEM_TRY_GIVE_MEMORY_BACK_SEVERITY_CRITICAL; severity = (mem_try_give_memory_back_severity_t) (severity + 1)) { mem_run_try_to_give_memory_back_callbacks (severity); data_space_p = mem_heap_alloc_block_internal (size_in_bytes, length_type, alloc_term); if (data_space_p != NULL) { return data_space_p; } } JERRY_ASSERT (data_space_p == NULL); jerry_fatal (ERR_OUT_OF_MEMORY); } /* mem_heap_alloc_block_try_give_memory_back */
/** * 'Object' object creation operation with one argument. * * See also: ECMA-262 v5, 15.2.2.1 * * @return pointer to newly created 'Object' object */ ecma_completion_value_t ecma_op_create_object_object_arg (ecma_value_t value) /**< argument of constructor */ { ecma_check_value_type_is_spec_defined (value); if (ecma_is_value_object (value) || ecma_is_value_number (value) || ecma_is_value_string (value) || ecma_is_value_boolean (value)) { // 1.b, 1.c, 1.d return ecma_op_to_object (value); } else { // 2. JERRY_ASSERT (ecma_is_value_undefined (value) || ecma_is_value_null (value)); ecma_object_t *obj_p = ecma_op_create_object_object_noarg (); return ecma_make_normal_completion_value (ecma_make_object_value (obj_p)); } } /* ecma_op_create_object_object_arg */
/** * Construct a Function object for specified built-in routine * * See also: ECMA-262 v5, 15 * * @return pointer to constructed Function object */ static ecma_object_t * ecma_builtin_make_function_object_for_routine (ecma_builtin_id_t builtin_id, /**< identifier of built-in object */ uint16_t routine_id, /**< builtin-wide identifier of the built-in * object's routine property */ uint8_t length_prop_value) /**< value of 'length' property */ { ecma_object_t *prototype_obj_p = ecma_builtin_get (ECMA_BUILTIN_ID_FUNCTION_PROTOTYPE); ecma_object_t *func_obj_p = ecma_create_object (prototype_obj_p, true, true, ECMA_OBJECT_TYPE_FUNCTION); ecma_deref_object (prototype_obj_p); ecma_set_object_is_builtin (func_obj_p); JERRY_ASSERT (routine_id >= ECMA_BUILTIN_ID__COUNT); ecma_extended_object_t *ext_func_obj_p = (ecma_extended_object_t *) func_obj_p; ext_func_obj_p->u.built_in.id = builtin_id; ext_func_obj_p->u.built_in.length = length_prop_value; ext_func_obj_p->u.built_in.routine_id = routine_id; ext_func_obj_p->u.built_in.instantiated_bitset = 0; return func_obj_p; } /* ecma_builtin_make_function_object_for_routine */
/** * Startup initialization of heap */ void jmem_heap_init (void) { JERRY_STATIC_ASSERT ((1u << JMEM_HEAP_OFFSET_LOG) >= JMEM_HEAP_SIZE, two_pow_mem_heap_offset_should_not_be_less_than_mem_heap_size); JERRY_ASSERT ((uintptr_t) JERRY_HEAP_CONTEXT (area) % JMEM_ALIGNMENT == 0); JERRY_CONTEXT (jmem_heap_limit) = CONFIG_MEM_HEAP_DESIRED_LIMIT; jmem_heap_free_t *const region_p = (jmem_heap_free_t *) JERRY_HEAP_CONTEXT (area); region_p->size = JMEM_HEAP_AREA_SIZE; region_p->next_offset = JMEM_HEAP_GET_OFFSET_FROM_ADDR (JMEM_HEAP_END_OF_LIST); JERRY_HEAP_CONTEXT (first).size = 0; JERRY_HEAP_CONTEXT (first).next_offset = JMEM_HEAP_GET_OFFSET_FROM_ADDR (region_p); JERRY_CONTEXT (jmem_heap_list_skip_p) = &JERRY_HEAP_CONTEXT (first); VALGRIND_NOACCESS_SPACE (JERRY_HEAP_CONTEXT (area), JMEM_HEAP_AREA_SIZE); JMEM_HEAP_STAT_INIT (); } /* jmem_heap_init */
/** * Assign value to named data property * * Note: * value previously stored in the property is freed */ void ecma_named_data_property_assign_value (ecma_object_t *obj_p, /**< object */ ecma_property_t *prop_p, /**< property */ ecma_value_t value) /**< value to assign */ { JERRY_ASSERT (ECMA_PROPERTY_GET_TYPE (prop_p) == ECMA_PROPERTY_TYPE_NAMEDDATA); ecma_assert_object_contains_the_property (obj_p, prop_p); if (ecma_is_value_number (value) && ecma_is_value_number (ecma_get_named_data_property_value (prop_p))) { const ecma_number_t *num_src_p = ecma_get_number_from_value (value); ecma_number_t *num_dst_p = ecma_get_number_from_value (ecma_get_named_data_property_value (prop_p)); *num_dst_p = *num_src_p; } else { ecma_value_t v = ecma_get_named_data_property_value (prop_p); ecma_free_value_if_not_object (v); ecma_set_named_data_property_value (prop_p, ecma_copy_value_if_not_object (value)); } } /* ecma_named_data_property_assign_value */
/** * Compress pointer * * @return packed pointer */ inline jmem_cpointer_t __attr_always_inline___ jmem_compress_pointer (const void *pointer_p) /**< pointer to compress */ { JERRY_ASSERT (pointer_p != NULL); JERRY_ASSERT (jmem_is_heap_pointer (pointer_p)); uintptr_t uint_ptr = (uintptr_t) pointer_p; JERRY_ASSERT (uint_ptr % JMEM_ALIGNMENT == 0); #ifdef JERRY_CPOINTER_32_BIT JERRY_ASSERT (((jmem_cpointer_t) uint_ptr) == uint_ptr); #else /* !JERRY_CPOINTER_32_BIT */ const uintptr_t heap_start = (uintptr_t) &JERRY_HEAP_CONTEXT (first); uint_ptr -= heap_start; uint_ptr >>= JMEM_ALIGNMENT_LOG; JERRY_ASSERT (uint_ptr <= UINT16_MAX); JERRY_ASSERT (uint_ptr != JMEM_CP_NULL); #endif /* JERRY_CPOINTER_32_BIT */ return (jmem_cpointer_t) uint_ptr; } /* jmem_compress_pointer */
/** * Decrease reference counter of an object */ void ecma_deref_object (ecma_object_t *object_p) /**< object */ { JERRY_ASSERT (ecma_gc_get_object_refs (object_p) > 0); ecma_gc_set_object_refs (object_p, ecma_gc_get_object_refs (object_p) - 1); } /* ecma_deref_object */
/** * 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. * * @return completion value * Returned value must be freed with ecma_free_completion_value. */ ecma_completion_value_t ecma_builtin_helper_array_concat_value (ecma_object_t *obj_p, /**< array */ uint32_t *length_p, /**< in-out: array's length */ ecma_value_t value) /**< value to concat */ { ecma_completion_value_t ret_value = ecma_make_empty_completion_value (); /* 5.b */ if (ecma_is_value_object (value) && (ecma_object_get_class_name (ecma_get_object_from_value (value)) == LIT_MAGIC_STRING_ARRAY_UL)) { ecma_string_t *magic_string_length_p = ecma_get_magic_string (LIT_MAGIC_STRING_LENGTH); /* 5.b.ii */ ECMA_TRY_CATCH (arg_len_value, ecma_op_object_get (ecma_get_object_from_value (value), magic_string_length_p), 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; array_index < arg_len && ecma_is_completion_value_empty (ret_value); array_index++) { ecma_string_t *array_index_string_p = ecma_new_ecma_string_from_uint32 (array_index); /* 5.b.iii.2 */ if (ecma_op_object_get_property (ecma_get_object_from_value (value), array_index_string_p) != NULL) { ecma_string_t *new_array_index_string_p = ecma_new_ecma_string_from_uint32 (*length_p + array_index); /* 5.b.iii.3.a */ ECMA_TRY_CATCH (get_value, ecma_op_object_get (ecma_get_object_from_value (value), array_index_string_p), ret_value); ecma_property_descriptor_t prop_desc = ecma_make_empty_property_descriptor (); { prop_desc.is_value_defined = true; prop_desc.value = get_value; prop_desc.is_writable_defined = true; prop_desc.is_writable = true; prop_desc.is_enumerable_defined = true; prop_desc.is_enumerable = true; prop_desc.is_configurable_defined = true; prop_desc.is_configurable = true; } /* 5.b.iii.3.b */ /* This will always be a simple value since 'is_throw' is false, so no need to free. */ ecma_completion_value_t put_comp = ecma_op_object_define_own_property (obj_p, new_array_index_string_p, &prop_desc, false); JERRY_ASSERT (ecma_is_completion_value_normal_true (put_comp)); ECMA_FINALIZE (get_value); ecma_deref_ecma_string (new_array_index_string_p); } 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); ecma_deref_ecma_string (magic_string_length_p); } else { ecma_string_t *new_array_index_string_p = ecma_new_ecma_string_from_uint32 ((*length_p)++); ecma_property_descriptor_t prop_desc = ecma_make_empty_property_descriptor (); { prop_desc.is_value_defined = true; prop_desc.value = value; prop_desc.is_writable_defined = true; prop_desc.is_writable = true; prop_desc.is_enumerable_defined = true; prop_desc.is_enumerable = true; prop_desc.is_configurable_defined = true; prop_desc.is_configurable = true; } /* 5.c.i */ /* This will always be a simple value since 'is_throw' is false, so no need to free. */ ecma_completion_value_t put_comp = ecma_op_object_define_own_property (obj_p, new_array_index_string_p, &prop_desc, false); JERRY_ASSERT (ecma_is_completion_value_normal_true (put_comp)); ecma_deref_ecma_string (new_array_index_string_p); } if (ecma_is_completion_value_empty (ret_value)) { ret_value = ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_TRUE); } return ret_value; } /* ecma_builtin_helper_array_concat_value */
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 */
/** * Find a literal in literal storage. * Only charset and magic string records are checked during search. * * @return pointer to a literal or NULL if no corresponding literal exists */ lit_literal_t lit_find_literal_by_utf8_string (const lit_utf8_byte_t *str_p, /**< a string to search for */ lit_utf8_size_t str_size) /**< length of the string */ { JERRY_ASSERT (str_p || !str_size); lit_string_hash_t str_hash = lit_utf8_string_calc_hash (str_p, str_size); lit_literal_t lit; for (lit = lit_storage; lit != NULL; lit = lit_cpointer_decompress (lit->next)) { const lit_record_type_t type = (lit_record_type_t) lit->type; switch (type) { case LIT_RECORD_TYPE_CHARSET: { const lit_charset_record_t *const rec_p = (const lit_charset_record_t *) lit; if (rec_p->hash != str_hash) { continue; } if (rec_p->size != str_size) { continue; } if (!strncmp ((const char *) (rec_p + 1), (const char *) str_p, str_size)) { return lit; } break; } case LIT_RECORD_TYPE_MAGIC_STR: { lit_magic_string_id_t magic_id = (lit_magic_string_id_t) ((lit_magic_record_t *) lit)->magic_id; const lit_utf8_byte_t *magic_str_p = lit_get_magic_string_utf8 (magic_id); if (lit_get_magic_string_size (magic_id) != str_size) { continue; } if (!strncmp ((const char *) magic_str_p, (const char *) str_p, str_size)) { return lit; } break; } case LIT_RECORD_TYPE_MAGIC_STR_EX: { lit_magic_string_ex_id_t magic_id = ((lit_magic_record_t *) lit)->magic_id; const lit_utf8_byte_t *magic_str_p = lit_get_magic_string_ex_utf8 (magic_id); if (lit_get_magic_string_ex_size (magic_id) != str_size) { continue; } if (!strncmp ((const char *) magic_str_p, (const char *) str_p, str_size)) { return lit; } break; } default: { JERRY_ASSERT (type == LIT_RECORD_TYPE_NUMBER); break; } } } return NULL; } /* lit_find_literal_by_utf8_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 */
/** * The Function.prototype object's 'bind' routine * * See also: * ECMA-262 v5, 15.3.4.5 * * @return ecma value * Returned value must be freed with ecma_free_value. */ static ecma_value_t ecma_builtin_function_prototype_object_bind (ecma_value_t this_arg, /**< this argument */ const ecma_value_t *arguments_list_p, /**< list of arguments */ ecma_length_t arguments_number) /**< number of arguments */ { ecma_value_t ret_value = ecma_make_simple_value (ECMA_SIMPLE_VALUE_EMPTY); /* 2. */ if (!ecma_op_is_callable (this_arg)) { ret_value = ecma_raise_type_error (ECMA_ERR_MSG ("")); } else { /* 4. 11. 18. */ ecma_object_t *prototype_obj_p = ecma_builtin_get (ECMA_BUILTIN_ID_FUNCTION_PROTOTYPE); ecma_object_t *function_p = ecma_create_object (prototype_obj_p, false, true, ECMA_OBJECT_TYPE_BOUND_FUNCTION); ecma_deref_object (prototype_obj_p); /* 7. */ ecma_value_t *target_function_prop_p; target_function_prop_p = ecma_create_internal_property (function_p, ECMA_INTERNAL_PROPERTY_BOUND_FUNCTION_TARGET_FUNCTION); ecma_object_t *this_arg_obj_p = ecma_get_object_from_value (this_arg); ECMA_SET_INTERNAL_VALUE_POINTER (*target_function_prop_p, this_arg_obj_p); /* 8. */ ecma_value_t *bound_this_prop_p; bound_this_prop_p = ecma_create_internal_property (function_p, ECMA_INTERNAL_PROPERTY_BOUND_FUNCTION_BOUND_THIS); const ecma_length_t arg_count = arguments_number; if (arg_count > 0) { *bound_this_prop_p = ecma_copy_value_if_not_object (arguments_list_p[0]); } else { *bound_this_prop_p = ecma_make_simple_value (ECMA_SIMPLE_VALUE_UNDEFINED); } if (arg_count > 1) { ecma_collection_header_t *bound_args_collection_p; bound_args_collection_p = ecma_new_values_collection (&arguments_list_p[1], arg_count - 1, false); ecma_value_t *bound_args_prop_p; bound_args_prop_p = ecma_create_internal_property (function_p, ECMA_INTERNAL_PROPERTY_BOUND_FUNCTION_BOUND_ARGS); ECMA_SET_INTERNAL_VALUE_POINTER (*bound_args_prop_p, bound_args_collection_p); } /* * [[Class]] property is not stored explicitly for objects of ECMA_OBJECT_TYPE_FUNCTION type. * * See also: ecma_object_get_class_name */ /* 16. */ ecma_number_t length = ECMA_NUMBER_ZERO; ecma_string_t *magic_string_length_p = ecma_new_ecma_length_string (); /* 15. */ if (ecma_object_get_class_name (this_arg_obj_p) == LIT_MAGIC_STRING_FUNCTION_UL) { ecma_value_t get_len_value = ecma_op_object_get (this_arg_obj_p, magic_string_length_p); JERRY_ASSERT (!ECMA_IS_VALUE_ERROR (get_len_value)); JERRY_ASSERT (ecma_is_value_number (get_len_value)); const ecma_length_t bound_arg_count = arg_count > 1 ? arg_count - 1 : 0; /* 15.a */ length = ecma_get_number_from_value (get_len_value) - ((ecma_number_t) bound_arg_count); ecma_free_value (get_len_value); /* 15.b */ if (ecma_number_is_negative (length)) { length = ECMA_NUMBER_ZERO; } } /* 17. */ ecma_value_t completion = ecma_builtin_helper_def_prop (function_p, magic_string_length_p, ecma_make_number_value (length), false, /* Writable */ false, /* Enumerable */ false, /* Configurable */ false); /* Failure handling */ JERRY_ASSERT (ecma_is_value_boolean (completion)); ecma_deref_ecma_string (magic_string_length_p); /* 19-21. */ ecma_object_t *thrower_p = ecma_builtin_get (ECMA_BUILTIN_ID_TYPE_ERROR_THROWER); ecma_property_descriptor_t prop_desc = ecma_make_empty_property_descriptor (); { prop_desc.is_enumerable_defined = true; prop_desc.is_enumerable = false; prop_desc.is_configurable_defined = true; prop_desc.is_configurable = false; prop_desc.is_get_defined = true; prop_desc.get_p = thrower_p; prop_desc.is_set_defined = true; prop_desc.set_p = thrower_p; } ecma_string_t *magic_string_caller_p = ecma_get_magic_string (LIT_MAGIC_STRING_CALLER); completion = ecma_op_object_define_own_property (function_p, magic_string_caller_p, &prop_desc, false); JERRY_ASSERT (ecma_is_value_boolean (completion)); ecma_deref_ecma_string (magic_string_caller_p); ecma_string_t *magic_string_arguments_p = ecma_get_magic_string (LIT_MAGIC_STRING_ARGUMENTS); completion = ecma_op_object_define_own_property (function_p, magic_string_arguments_p, &prop_desc, false); JERRY_ASSERT (ecma_is_value_boolean (completion)); ecma_deref_ecma_string (magic_string_arguments_p); ecma_deref_object (thrower_p); /* 22. */ ret_value = ecma_make_object_value (function_p); } return ret_value; } /* ecma_builtin_function_prototype_object_bind */
/** * The Function.prototype object's 'apply' routine * * See also: * ECMA-262 v5, 15.3.4.3 * * @return ecma value * Returned value must be freed with ecma_free_value. */ static ecma_value_t ecma_builtin_function_prototype_object_apply (ecma_value_t this_arg, /**< this argument */ ecma_value_t arg1, /**< first argument */ ecma_value_t arg2) /**< second argument */ { ecma_value_t ret_value = ecma_make_simple_value (ECMA_SIMPLE_VALUE_EMPTY); /* 1. */ if (!ecma_op_is_callable (this_arg)) { ret_value = ecma_raise_type_error (ECMA_ERR_MSG ("")); } else { ecma_object_t *func_obj_p = ecma_get_object_from_value (this_arg); /* 2. */ if (ecma_is_value_null (arg2) || ecma_is_value_undefined (arg2)) { ret_value = ecma_op_function_call (func_obj_p, arg1, NULL, 0); } else { /* 3. */ if (!ecma_is_value_object (arg2)) { ret_value = ecma_raise_type_error (ECMA_ERR_MSG ("")); } else { ecma_object_t *obj_p = ecma_get_object_from_value (arg2); ecma_string_t *length_magic_string_p = ecma_new_ecma_length_string (); /* 4. */ ECMA_TRY_CATCH (length_value, ecma_op_object_get (obj_p, length_magic_string_p), ret_value); ECMA_OP_TO_NUMBER_TRY_CATCH (length_number, length_value, ret_value); /* 5. */ const uint32_t length = ecma_number_to_uint32 (length_number); /* 6. */ JMEM_DEFINE_LOCAL_ARRAY (arguments_list_p, length, ecma_value_t); uint32_t last_index = 0; /* 7. */ for (uint32_t index = 0; index < length && ecma_is_value_empty (ret_value); index++) { ecma_string_t *curr_idx_str_p = ecma_new_ecma_string_from_uint32 (index); ECMA_TRY_CATCH (get_value, ecma_op_object_get (obj_p, curr_idx_str_p), ret_value); arguments_list_p[index] = ecma_copy_value (get_value); last_index = index + 1; ECMA_FINALIZE (get_value); ecma_deref_ecma_string (curr_idx_str_p); } if (ecma_is_value_empty (ret_value)) { JERRY_ASSERT (last_index == length); ret_value = ecma_op_function_call (func_obj_p, arg1, arguments_list_p, length); } for (uint32_t index = 0; index < last_index; index++) { ecma_free_value (arguments_list_p[index]); } JMEM_FINALIZE_LOCAL_ARRAY (arguments_list_p); ECMA_OP_TO_NUMBER_FINALIZE (length_number); ECMA_FINALIZE (length_value); ecma_deref_ecma_string (length_magic_string_p); } } } return ret_value; } /* ecma_builtin_function_prototype_object_apply */
/** * 'Try' opcode handler. * * See also: ECMA-262 v5, 12.14 * * @return completion value * Returned value must be freed with ecma_free_completion_value */ ecma_completion_value_t opfunc_try_block (opcode_t opdata, /**< operation data */ vm_frame_ctx_t *frame_ctx_p) /**< interpreter context */ { const idx_t block_end_oc_idx_1 = opdata.data.try_block.oc_idx_1; const idx_t block_end_oc_idx_2 = opdata.data.try_block.oc_idx_2; const opcode_counter_t try_end_oc = (opcode_counter_t) ( calc_opcode_counter_from_idx_idx (block_end_oc_idx_1, block_end_oc_idx_2) + frame_ctx_p->pos); frame_ctx_p->pos++; vm_run_scope_t run_scope_try = { frame_ctx_p->pos, try_end_oc }; ecma_completion_value_t try_completion = vm_loop (frame_ctx_p, &run_scope_try); JERRY_ASSERT ((!ecma_is_completion_value_empty (try_completion) && frame_ctx_p->pos <= try_end_oc) || (ecma_is_completion_value_empty (try_completion) && frame_ctx_p->pos == try_end_oc)); frame_ctx_p->pos = try_end_oc; opcode_t next_opcode = vm_get_opcode (frame_ctx_p->opcodes_p, frame_ctx_p->pos); JERRY_ASSERT (next_opcode.op_idx == __op__idx_meta); if (next_opcode.data.meta.type == OPCODE_META_TYPE_CATCH) { const opcode_counter_t catch_end_oc = (opcode_counter_t) ( read_meta_opcode_counter (OPCODE_META_TYPE_CATCH, frame_ctx_p) + frame_ctx_p->pos); frame_ctx_p->pos++; if (ecma_is_completion_value_throw (try_completion)) { next_opcode = vm_get_opcode (frame_ctx_p->opcodes_p, frame_ctx_p->pos); JERRY_ASSERT (next_opcode.op_idx == __op__idx_meta); JERRY_ASSERT (next_opcode.data.meta.type == OPCODE_META_TYPE_CATCH_EXCEPTION_IDENTIFIER); lit_cpointer_t catch_exc_val_var_name_lit_cp = serializer_get_literal_cp_by_uid (next_opcode.data.meta.data_1, frame_ctx_p->opcodes_p, frame_ctx_p->pos); frame_ctx_p->pos++; ecma_string_t *catch_exc_var_name_str_p = ecma_new_ecma_string_from_lit_cp (catch_exc_val_var_name_lit_cp); ecma_object_t *old_env_p = frame_ctx_p->lex_env_p; ecma_object_t *catch_env_p = ecma_create_decl_lex_env (old_env_p); ecma_completion_value_t completion = ecma_op_create_mutable_binding (catch_env_p, catch_exc_var_name_str_p, false); JERRY_ASSERT (ecma_is_completion_value_empty (completion)); completion = ecma_op_set_mutable_binding (catch_env_p, catch_exc_var_name_str_p, ecma_get_completion_value_value (try_completion), false); JERRY_ASSERT (ecma_is_completion_value_empty (completion)); ecma_deref_ecma_string (catch_exc_var_name_str_p); frame_ctx_p->lex_env_p = catch_env_p; ecma_free_completion_value (try_completion); vm_run_scope_t run_scope_catch = { frame_ctx_p->pos, catch_end_oc }; try_completion = vm_loop (frame_ctx_p, &run_scope_catch); frame_ctx_p->lex_env_p = old_env_p; ecma_deref_object (catch_env_p); JERRY_ASSERT ((!ecma_is_completion_value_empty (try_completion) && frame_ctx_p->pos <= catch_end_oc) || (ecma_is_completion_value_empty (try_completion) && frame_ctx_p->pos == catch_end_oc)); } frame_ctx_p->pos = catch_end_oc; } next_opcode = vm_get_opcode (frame_ctx_p->opcodes_p, frame_ctx_p->pos); JERRY_ASSERT (next_opcode.op_idx == __op__idx_meta); if (next_opcode.data.meta.type == OPCODE_META_TYPE_FINALLY) { const opcode_counter_t finally_end_oc = (opcode_counter_t) ( read_meta_opcode_counter (OPCODE_META_TYPE_FINALLY, frame_ctx_p) + frame_ctx_p->pos); frame_ctx_p->pos++; vm_run_scope_t run_scope_finally = { frame_ctx_p->pos, finally_end_oc }; ecma_completion_value_t finally_completion = vm_loop (frame_ctx_p, &run_scope_finally); JERRY_ASSERT ((!ecma_is_completion_value_empty (finally_completion) && frame_ctx_p->pos <= finally_end_oc) || (ecma_is_completion_value_empty (finally_completion) && frame_ctx_p->pos == finally_end_oc)); frame_ctx_p->pos = finally_end_oc; if (!ecma_is_completion_value_empty (finally_completion)) { ecma_free_completion_value (try_completion); try_completion = finally_completion; } } next_opcode = vm_get_opcode (frame_ctx_p->opcodes_p, frame_ctx_p->pos++); JERRY_ASSERT (next_opcode.op_idx == __op__idx_meta); JERRY_ASSERT (next_opcode.data.meta.type == OPCODE_META_TYPE_END_TRY_CATCH_FINALLY); return try_completion; } /* opfunc_try_block */
/** * Long path for mem_pools_alloc */ static void __attr_noinline___ mem_pools_alloc_longpath (void) { mem_check_pools (); JERRY_ASSERT (mem_free_chunk_p == NULL); JERRY_ASSERT (MEM_POOL_SIZE <= mem_heap_get_chunked_block_data_size ()); JERRY_ASSERT (MEM_POOL_CHUNKS_NUMBER >= 1); MEM_HEAP_VALGRIND_FREYA_MEMPOOL_REQUEST (); mem_pool_chunk_t *pool_start_p = (mem_pool_chunk_t*) mem_heap_alloc_chunked_block (MEM_HEAP_ALLOC_LONG_TERM); if (mem_free_chunk_p != NULL) { /* some chunks were freed due to GC invoked by heap allocator */ MEM_HEAP_VALGRIND_FREYA_MEMPOOL_REQUEST (); mem_heap_free_block (pool_start_p); return; } #ifndef JERRY_NDEBUG mem_free_chunks_number += MEM_POOL_CHUNKS_NUMBER; #endif /* !JERRY_NDEBUG */ JERRY_STATIC_ASSERT (MEM_POOL_CHUNK_SIZE % MEM_ALIGNMENT == 0); JERRY_STATIC_ASSERT (sizeof (mem_pool_chunk_t) == MEM_POOL_CHUNK_SIZE); JERRY_STATIC_ASSERT (sizeof (mem_pool_chunk_index_t) <= MEM_POOL_CHUNK_SIZE); JERRY_ASSERT ((mem_pool_chunk_index_t) MEM_POOL_CHUNKS_NUMBER == MEM_POOL_CHUNKS_NUMBER); JERRY_ASSERT (MEM_POOL_SIZE == MEM_POOL_CHUNKS_NUMBER * MEM_POOL_CHUNK_SIZE); JERRY_ASSERT (((uintptr_t) pool_start_p) % MEM_ALIGNMENT == 0); mem_pool_chunk_t *prev_free_chunk_p = NULL; for (mem_pool_chunk_index_t chunk_index = 0; chunk_index < MEM_POOL_CHUNKS_NUMBER; chunk_index++) { mem_pool_chunk_t *chunk_p = pool_start_p + chunk_index; if (prev_free_chunk_p != NULL) { prev_free_chunk_p->u.free.next_p = chunk_p; } prev_free_chunk_p = chunk_p; } prev_free_chunk_p->u.free.next_p = NULL; #ifdef JERRY_VALGRIND for (mem_pool_chunk_index_t chunk_index = 0; chunk_index < MEM_POOL_CHUNKS_NUMBER; chunk_index++) { mem_pool_chunk_t *chunk_p = pool_start_p + chunk_index; VALGRIND_NOACCESS_SPACE (chunk_p, MEM_POOL_CHUNK_SIZE); } #endif /* JERRY_VALGRIND */ mem_free_chunk_p = pool_start_p; MEM_POOLS_STAT_ALLOC_POOL (); mem_check_pools (); } /* mem_pools_alloc_longpath */
/** * Collect chunks from empty pools and free the pools */ void mem_pools_collect_empty (void) { /* * Hint magic number in header of pools with free pool-first chunks */ const uint16_t hint_magic_num_value = 0x7e89; /* * Collection-time chunk lists */ mem_pool_chunk_t *first_chunks_list_p = NULL; mem_pool_chunk_t *non_first_chunks_list_p = NULL; /* * At first stage collect free pool-first chunks to separate collection-time lists * and change their layout from mem_pool_chunk_t::u::free to mem_pool_chunk_t::u::pool_gc */ { mem_pool_chunk_t tmp_header; tmp_header.u.free.next_p = mem_free_chunk_p; for (mem_pool_chunk_t *free_chunk_iter_p = tmp_header.u.free.next_p, *prev_free_chunk_p = &tmp_header, *next_free_chunk_p; free_chunk_iter_p != NULL; free_chunk_iter_p = next_free_chunk_p) { mem_pool_chunk_t *pool_start_p = (mem_pool_chunk_t *) mem_heap_get_chunked_block_start (free_chunk_iter_p); VALGRIND_DEFINED_SPACE (free_chunk_iter_p, MEM_POOL_CHUNK_SIZE); next_free_chunk_p = free_chunk_iter_p->u.free.next_p; if (pool_start_p == free_chunk_iter_p) { /* * The chunk is first at its pool * * Remove the chunk from common list of free chunks */ prev_free_chunk_p->u.free.next_p = next_free_chunk_p; /* * Initialize pool-first chunk as pool header and it insert into list of free pool-first chunks */ free_chunk_iter_p->u.pool_gc.free_list_cp = MEM_CP_NULL; free_chunk_iter_p->u.pool_gc.free_chunks_num = 1; /* the first chunk */ free_chunk_iter_p->u.pool_gc.hint_magic_num = hint_magic_num_value; free_chunk_iter_p->u.pool_gc.traversal_check_flag = false; MEM_CP_SET_POINTER (free_chunk_iter_p->u.pool_gc.next_first_cp, first_chunks_list_p); first_chunks_list_p = free_chunk_iter_p; } else { prev_free_chunk_p = free_chunk_iter_p; } } mem_free_chunk_p = tmp_header.u.free.next_p; } if (first_chunks_list_p == NULL) { /* there are no empty pools */ return; } /* * At second stage we collect all free non-pool-first chunks, for which corresponding pool-first chunks are free, * and link them into the corresponding mem_pool_chunk_t::u::pool_gc::free_list_cp list, while also maintaining * the corresponding mem_pool_chunk_t::u::pool_gc::free_chunks_num: * - at first, for each non-pool-first free chunk we check whether traversal check flag is cleared in corresponding * first chunk in the same pool, and move those chunks, for which the condition is true, * to separate temporary list. * * - then, we flip the traversal check flags for each of free pool-first chunks. * * - at last, we perform almost the same as at first step, but check only non-pool-first chunks from the temporary * list, and send the chunks, for which the corresponding traversal check flag is cleared, back to the common list * of free chunks, and the rest chunks from the temporary list are linked to corresponding pool-first chunks. * Also, counter of the linked free chunks is maintained in every free pool-first chunk. */ { { mem_pool_chunk_t tmp_header; tmp_header.u.free.next_p = mem_free_chunk_p; for (mem_pool_chunk_t *free_chunk_iter_p = tmp_header.u.free.next_p, *prev_free_chunk_p = &tmp_header, *next_free_chunk_p; free_chunk_iter_p != NULL; free_chunk_iter_p = next_free_chunk_p) { mem_pool_chunk_t *pool_start_p = (mem_pool_chunk_t *) mem_heap_get_chunked_block_start (free_chunk_iter_p); next_free_chunk_p = free_chunk_iter_p->u.free.next_p; /* * The magic number doesn't guarantee that the chunk is actually a free pool-first chunk, * so we test the traversal check flag after flipping values of the flags in every * free pool-first chunk. */ uint16_t magic_num_field; bool traversal_check_flag; mem_pools_collect_read_magic_num_and_flag (pool_start_p, &magic_num_field, &traversal_check_flag); /* * During this traversal the flag in the free header chunks is in cleared state */ if (!traversal_check_flag && magic_num_field == hint_magic_num_value) { free_chunk_iter_p->u.free.next_p = non_first_chunks_list_p; non_first_chunks_list_p = free_chunk_iter_p; prev_free_chunk_p->u.free.next_p = next_free_chunk_p; } else { prev_free_chunk_p = free_chunk_iter_p; } } mem_free_chunk_p = tmp_header.u.free.next_p; } { /* * Now, flip the traversal check flag in free pool-first chunks */ for (mem_pool_chunk_t *first_chunks_iter_p = first_chunks_list_p; first_chunks_iter_p != NULL; first_chunks_iter_p = MEM_CP_GET_POINTER (mem_pool_chunk_t, first_chunks_iter_p->u.pool_gc.next_first_cp)) { JERRY_ASSERT (!first_chunks_iter_p->u.pool_gc.traversal_check_flag); first_chunks_iter_p->u.pool_gc.traversal_check_flag = true; } } { for (mem_pool_chunk_t *non_first_chunks_iter_p = non_first_chunks_list_p, *next_p; non_first_chunks_iter_p != NULL; non_first_chunks_iter_p = next_p) { next_p = non_first_chunks_iter_p->u.free.next_p; mem_pool_chunk_t *pool_start_p; pool_start_p = (mem_pool_chunk_t *) mem_heap_get_chunked_block_start (non_first_chunks_iter_p); uint16_t magic_num_field; bool traversal_check_flag; mem_pools_collect_read_magic_num_and_flag (pool_start_p, &magic_num_field, &traversal_check_flag); JERRY_ASSERT (magic_num_field == hint_magic_num_value); #ifndef JERRY_DISABLE_HEAVY_DEBUG bool is_occured = false; for (mem_pool_chunk_t *first_chunks_iter_p = first_chunks_list_p; first_chunks_iter_p != NULL; first_chunks_iter_p = MEM_CP_GET_POINTER (mem_pool_chunk_t, first_chunks_iter_p->u.pool_gc.next_first_cp)) { if (pool_start_p == first_chunks_iter_p) { is_occured = true; break; } } JERRY_ASSERT (is_occured == traversal_check_flag); #endif /* !JERRY_DISABLE_HEAVY_DEBUG */ /* * During this traversal the flag in the free header chunks is in set state * * If the flag is set, it is guaranteed that the pool-first chunk, * from the same pool, as the current non-pool-first chunk, is free * and is placed in the corresponding list of free pool-first chunks. */ if (traversal_check_flag) { pool_start_p->u.pool_gc.free_chunks_num++; non_first_chunks_iter_p->u.free.next_p = MEM_CP_GET_POINTER (mem_pool_chunk_t, pool_start_p->u.pool_gc.free_list_cp); MEM_CP_SET_NON_NULL_POINTER (pool_start_p->u.pool_gc.free_list_cp, non_first_chunks_iter_p); } else { non_first_chunks_iter_p->u.free.next_p = mem_free_chunk_p; mem_free_chunk_p = non_first_chunks_iter_p; } } } non_first_chunks_list_p = NULL; } /* * At third stage we check each free pool-first chunk in collection-time list for counted * number of free chunks in the pool, containing the chunk. * * If the number is equal to number of chunks in the pool - then the pool is empty, and so is freed, * otherwise - free chunks of the pool are returned to the common list of free chunks. */ for (mem_pool_chunk_t *first_chunks_iter_p = first_chunks_list_p, *next_p; first_chunks_iter_p != NULL; first_chunks_iter_p = next_p) { next_p = MEM_CP_GET_POINTER (mem_pool_chunk_t, first_chunks_iter_p->u.pool_gc.next_first_cp); JERRY_ASSERT (first_chunks_iter_p->u.pool_gc.hint_magic_num == hint_magic_num_value); JERRY_ASSERT (first_chunks_iter_p->u.pool_gc.traversal_check_flag); JERRY_ASSERT (first_chunks_iter_p->u.pool_gc.free_chunks_num <= MEM_POOL_CHUNKS_NUMBER); if (first_chunks_iter_p->u.pool_gc.free_chunks_num == MEM_POOL_CHUNKS_NUMBER) { #ifndef JERRY_NDEBUG mem_free_chunks_number -= MEM_POOL_CHUNKS_NUMBER; #endif /* !JERRY_NDEBUG */ MEM_HEAP_VALGRIND_FREYA_MEMPOOL_REQUEST (); mem_heap_free_block (first_chunks_iter_p); MEM_POOLS_STAT_FREE_POOL (); } else { mem_pool_chunk_t *first_chunk_p = first_chunks_iter_p; /* * Convert layout of first chunk from collection-time pool-first chunk's layout to the common free chunk layout */ first_chunk_p->u.free.next_p = MEM_CP_GET_POINTER (mem_pool_chunk_t, first_chunks_iter_p->u.pool_gc.free_list_cp); /* * Link local pool's list of free chunks into the common list of free chunks */ for (mem_pool_chunk_t *pool_chunks_iter_p = first_chunk_p; ; pool_chunks_iter_p = pool_chunks_iter_p->u.free.next_p) { JERRY_ASSERT (pool_chunks_iter_p != NULL); if (pool_chunks_iter_p->u.free.next_p == NULL) { pool_chunks_iter_p->u.free.next_p = mem_free_chunk_p; break; } } mem_free_chunk_p = first_chunk_p; } } #ifdef JERRY_VALGRIND /* * Valgrind-mode specific pass that marks all free chunks inaccessible */ for (mem_pool_chunk_t *free_chunk_iter_p = mem_free_chunk_p, *next_free_chunk_p; free_chunk_iter_p != NULL; free_chunk_iter_p = next_free_chunk_p) { next_free_chunk_p = free_chunk_iter_p->u.free.next_p; VALGRIND_NOACCESS_SPACE (free_chunk_iter_p, MEM_POOL_CHUNK_SIZE); } #endif /* JERRY_VALGRIND */ } /* mem_pools_collect_empty */
/** * Mark objects as visited starting from specified object as root */ void ecma_gc_mark (ecma_object_t *object_p) /**< object to mark from */ { JERRY_ASSERT (object_p != NULL); JERRY_ASSERT (ecma_gc_is_object_visited (object_p)); bool traverse_properties = true; if (ecma_is_lexical_environment (object_p)) { ecma_object_t *lex_env_p = ecma_get_lex_env_outer_reference (object_p); if (lex_env_p != NULL) { ecma_gc_set_object_visited (lex_env_p, true); } if (ecma_get_lex_env_type (object_p) == ECMA_LEXICAL_ENVIRONMENT_OBJECTBOUND) { ecma_object_t *binding_object_p = ecma_get_lex_env_binding_object (object_p); ecma_gc_set_object_visited (binding_object_p, true); traverse_properties = false; } } else { ecma_object_t *proto_p = ecma_get_object_prototype (object_p); if (proto_p != NULL) { ecma_gc_set_object_visited (proto_p, true); } } if (traverse_properties) { for (ecma_property_t *property_p = ecma_get_property_list (object_p), *next_property_p; property_p != NULL; property_p = next_property_p) { next_property_p = ECMA_GET_POINTER (ecma_property_t, property_p->next_property_p); switch ((ecma_property_type_t) property_p->type) { case ECMA_PROPERTY_NAMEDDATA: { ecma_value_t value = ecma_get_named_data_property_value (property_p); if (ecma_is_value_object (value)) { ecma_object_t *value_obj_p = ecma_get_object_from_value (value); ecma_gc_set_object_visited (value_obj_p, true); } break; } case ECMA_PROPERTY_NAMEDACCESSOR: { ecma_object_t *getter_obj_p = ecma_get_named_accessor_property_getter (property_p); ecma_object_t *setter_obj_p = ecma_get_named_accessor_property_setter (property_p); if (getter_obj_p != NULL) { ecma_gc_set_object_visited (getter_obj_p, true); } if (setter_obj_p != NULL) { ecma_gc_set_object_visited (setter_obj_p, true); } break; } case ECMA_PROPERTY_INTERNAL: { ecma_internal_property_id_t property_id = (ecma_internal_property_id_t) property_p->u.internal_property.type; uint32_t property_value = property_p->u.internal_property.value; switch (property_id) { case ECMA_INTERNAL_PROPERTY_NUMBER_INDEXED_ARRAY_VALUES: /* a collection of ecma-values */ case ECMA_INTERNAL_PROPERTY_STRING_INDEXED_ARRAY_VALUES: /* a collection of ecma-values */ { JERRY_UNIMPLEMENTED ("Indexed array storage is not implemented yet."); } case ECMA_INTERNAL_PROPERTY_PROTOTYPE: /* the property's value is located in ecma_object_t (see above in the routine) */ case ECMA_INTERNAL_PROPERTY_EXTENSIBLE: /* the property's value is located in ecma_object_t (see above in the routine) */ case ECMA_INTERNAL_PROPERTY__COUNT: /* not a real internal property type, * but number of the real internal property types */ { JERRY_UNREACHABLE (); } case ECMA_INTERNAL_PROPERTY_FORMAL_PARAMETERS: /* a collection of strings */ case ECMA_INTERNAL_PROPERTY_PRIMITIVE_STRING_VALUE: /* compressed pointer to a ecma_string_t */ case ECMA_INTERNAL_PROPERTY_PRIMITIVE_NUMBER_VALUE: /* compressed pointer to a ecma_number_t */ case ECMA_INTERNAL_PROPERTY_PRIMITIVE_BOOLEAN_VALUE: /* a simple boolean value */ case ECMA_INTERNAL_PROPERTY_CLASS: /* an enum */ case ECMA_INTERNAL_PROPERTY_CODE_BYTECODE: /* compressed pointer to a bytecode array */ case ECMA_INTERNAL_PROPERTY_CODE_FLAGS_AND_OFFSET: /* an integer */ case ECMA_INTERNAL_PROPERTY_NATIVE_CODE: /* an external pointer */ case ECMA_INTERNAL_PROPERTY_NATIVE_HANDLE: /* an external pointer */ case ECMA_INTERNAL_PROPERTY_FREE_CALLBACK: /* an object's native free callback */ case ECMA_INTERNAL_PROPERTY_BUILT_IN_ID: /* an integer */ case ECMA_INTERNAL_PROPERTY_BUILT_IN_ROUTINE_DESC: /* an integer */ case ECMA_INTERNAL_PROPERTY_EXTENSION_ID: /* an integer */ case ECMA_INTERNAL_PROPERTY_NON_INSTANTIATED_BUILT_IN_MASK_0_31: /* an integer (bit-mask) */ case ECMA_INTERNAL_PROPERTY_NON_INSTANTIATED_BUILT_IN_MASK_32_63: /* an integer (bit-mask) */ case ECMA_INTERNAL_PROPERTY_REGEXP_BYTECODE: case ECMA_INTERNAL_PROPERTY_BOUND_FUNCTION_TARGET_FUNCTION: case ECMA_INTERNAL_PROPERTY_BOUND_FUNCTION_BOUND_THIS: case ECMA_INTERNAL_PROPERTY_BOUND_FUNCTION_BOUND_ARGS: { break; } case ECMA_INTERNAL_PROPERTY_SCOPE: /* a lexical environment */ case ECMA_INTERNAL_PROPERTY_PARAMETERS_MAP: /* an object */ { ecma_object_t *obj_p = ECMA_GET_NON_NULL_POINTER (ecma_object_t, property_value); ecma_gc_set_object_visited (obj_p, true); break; } } break; } } } } } /* ecma_gc_mark */
/** * [[DefineOwnProperty]] ecma array object's operation * * See also: * ECMA-262 v5, 8.6.2; ECMA-262 v5, Table 8 * ECMA-262 v5, 15.4.5.1 * * @return completion value * Returned value must be freed with ecma_free_completion_value */ ecma_completion_value_t ecma_op_array_object_define_own_property (ecma_object_t *obj_p, /**< the array object */ ecma_string_t *property_name_p, /**< property name */ const ecma_property_descriptor_t *property_desc_p, /**< property descriptor */ bool is_throw) /**< flag that controls failure handling */ { JERRY_ASSERT (ecma_get_object_type (obj_p) == ECMA_OBJECT_TYPE_ARRAY); // 1. ecma_string_t *magic_string_length_p = ecma_get_magic_string (LIT_MAGIC_STRING_LENGTH); ecma_property_t *len_prop_p = ecma_op_object_get_own_property (obj_p, magic_string_length_p); JERRY_ASSERT (len_prop_p != NULL && len_prop_p->type == ECMA_PROPERTY_NAMEDDATA); // 2. ecma_value_t old_len_value = ecma_get_named_data_property_value (len_prop_p); ecma_number_t *num_p = ecma_get_number_from_value (old_len_value); uint32_t old_len_uint32 = ecma_number_to_uint32 (*num_p); // 3. bool is_property_name_equal_length = ecma_compare_ecma_strings (property_name_p, magic_string_length_p); ecma_deref_ecma_string (magic_string_length_p); if (is_property_name_equal_length) { // a. if (!property_desc_p->is_value_defined) { // i. return ecma_op_general_object_define_own_property (obj_p, property_name_p, property_desc_p, is_throw); } ecma_number_t new_len_num; // c. ecma_completion_value_t completion = ecma_op_to_number (property_desc_p->value); if (ecma_is_completion_value_throw (completion)) { return completion; } JERRY_ASSERT (ecma_is_completion_value_normal (completion) && ecma_is_value_number (ecma_get_completion_value_value (completion))); new_len_num = *ecma_get_number_from_completion_value (completion); ecma_free_completion_value (completion); uint32_t new_len_uint32 = ecma_number_to_uint32 (new_len_num); // d. if (ecma_uint32_to_number (new_len_uint32) != new_len_num) { return ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_RANGE)); } else { // b., e. ecma_number_t *new_len_num_p = ecma_alloc_number (); *new_len_num_p = new_len_num; ecma_property_descriptor_t new_len_property_desc = *property_desc_p; new_len_property_desc.value = ecma_make_number_value (new_len_num_p); ecma_completion_value_t ret_value = ecma_make_empty_completion_value (); // f. if (new_len_uint32 >= old_len_uint32) { // i. magic_string_length_p = ecma_get_magic_string (LIT_MAGIC_STRING_LENGTH); ret_value = ecma_op_general_object_define_own_property (obj_p, magic_string_length_p, &new_len_property_desc, is_throw); ecma_deref_ecma_string (magic_string_length_p); } else { // g. if (!ecma_is_property_writable (len_prop_p)) { ret_value = ecma_reject (is_throw); } else { // h. bool new_writable; if (!new_len_property_desc.is_writable_defined || new_len_property_desc.is_writable) { new_writable = true; } else { // ii. new_writable = false; // iii. new_len_property_desc.is_writable_defined = true; new_len_property_desc.is_writable = true; } // j. magic_string_length_p = ecma_get_magic_string (LIT_MAGIC_STRING_LENGTH); ecma_completion_value_t succeeded = ecma_op_general_object_define_own_property (obj_p, magic_string_length_p, &new_len_property_desc, is_throw); ecma_deref_ecma_string (magic_string_length_p); /* Handling normal false and throw values */ if (!ecma_is_completion_value_normal_true (succeeded)) { JERRY_ASSERT (ecma_is_completion_value_normal_false (succeeded) || ecma_is_completion_value_throw (succeeded)); // k. ret_value = succeeded; } else { // l JERRY_ASSERT (new_len_uint32 < old_len_uint32); /* * Item i. is replaced with faster iteration: only indices that actually exist in the array, are iterated */ bool is_reduce_succeeded = true; ecma_collection_header_t *array_index_props_p = ecma_op_object_get_property_names (obj_p, true, false, false); ecma_length_t array_index_props_num = array_index_props_p->unit_number; MEM_DEFINE_LOCAL_ARRAY (array_index_values_p, array_index_props_num, uint32_t); ecma_collection_iterator_t iter; ecma_collection_iterator_init (&iter, array_index_props_p); uint32_t array_index_values_pos = 0; while (ecma_collection_iterator_next (&iter)) { ecma_string_t *property_name_p = ecma_get_string_from_value (*iter.current_value_p); uint32_t index; bool is_index = ecma_string_get_array_index (property_name_p, &index); JERRY_ASSERT (is_index); JERRY_ASSERT (index < old_len_uint32); array_index_values_p[array_index_values_pos++] = index; } JERRY_ASSERT (array_index_values_pos == array_index_props_num); while (array_index_values_pos != 0 && array_index_values_p[--array_index_values_pos] >= new_len_uint32) { uint32_t index = array_index_values_p[array_index_values_pos]; // ii. ecma_string_t *index_string_p = ecma_new_ecma_string_from_uint32 (index); ecma_completion_value_t delete_succeeded = ecma_op_object_delete (obj_p, index_string_p, false); ecma_deref_ecma_string (index_string_p); if (ecma_is_completion_value_normal_false (delete_succeeded)) { // iii. new_len_uint32 = (index + 1u); ecma_number_t *new_len_num_p = ecma_get_number_from_value (new_len_property_desc.value); // 1. *new_len_num_p = ecma_uint32_to_number (index + 1u); // 2. if (!new_writable) { new_len_property_desc.is_writable_defined = true; new_len_property_desc.is_writable = false; } // 3. ecma_string_t *magic_string_length_p = ecma_get_magic_string (LIT_MAGIC_STRING_LENGTH); ecma_completion_value_t completion = ecma_op_general_object_define_own_property (obj_p, magic_string_length_p, &new_len_property_desc, false); ecma_deref_ecma_string (magic_string_length_p); JERRY_ASSERT (ecma_is_completion_value_normal_true (completion) || ecma_is_completion_value_normal_false (completion)); is_reduce_succeeded = false; break; } } MEM_FINALIZE_LOCAL_ARRAY (array_index_values_p); ecma_free_values_collection (array_index_props_p, true); if (!is_reduce_succeeded) { ret_value = ecma_reject (is_throw); } else { // m. if (!new_writable) { ecma_property_descriptor_t prop_desc_not_writable = ecma_make_empty_property_descriptor (); prop_desc_not_writable.is_writable_defined = true; prop_desc_not_writable.is_writable = false; ecma_completion_value_t completion_set_not_writable; magic_string_length_p = ecma_get_magic_string (LIT_MAGIC_STRING_LENGTH); completion_set_not_writable = ecma_op_general_object_define_own_property (obj_p, magic_string_length_p, &prop_desc_not_writable, false); ecma_deref_ecma_string (magic_string_length_p); JERRY_ASSERT (ecma_is_completion_value_normal_true (completion_set_not_writable)); } ret_value = ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_TRUE); } } } } ecma_dealloc_number (new_len_num_p); return ret_value; } JERRY_UNREACHABLE (); } else { // 4.a. uint32_t index; if (!ecma_string_get_array_index (property_name_p, &index)) { // 5. return ecma_op_general_object_define_own_property (obj_p, property_name_p, property_desc_p, is_throw); } // 4. // b. if (index >= old_len_uint32 && !ecma_is_property_writable (len_prop_p)) { return ecma_reject (is_throw); } // c. ecma_completion_value_t succeeded = ecma_op_general_object_define_own_property (obj_p, property_name_p, property_desc_p, false); // d. JERRY_ASSERT (ecma_is_completion_value_normal_true (succeeded) || ecma_is_completion_value_normal_false (succeeded)); if (ecma_is_completion_value_normal_false (succeeded)) { return ecma_reject (is_throw); } // e. if (index >= old_len_uint32) { // i., ii. ecma_number_t *num_p = ecma_alloc_number (); *num_p = ecma_number_add (ecma_uint32_to_number (index), ECMA_NUMBER_ONE); ecma_named_data_property_assign_value (obj_p, len_prop_p, ecma_make_number_value (num_p)); ecma_dealloc_number (num_p); } // f. return ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_TRUE); } JERRY_UNREACHABLE (); } /* ecma_op_array_object_define_own_property */
/** * Run garbage collecting */ void ecma_gc_run (void) { ecma_gc_new_objects_since_last_gc = 0; JERRY_ASSERT (ecma_gc_objects_lists[ECMA_GC_COLOR_BLACK] == NULL); /* if some object is referenced from stack or globals (i.e. it is root), mark it */ for (ecma_object_t *obj_iter_p = ecma_gc_objects_lists[ECMA_GC_COLOR_WHITE_GRAY]; obj_iter_p != NULL; obj_iter_p = ecma_gc_get_object_next (obj_iter_p)) { JERRY_ASSERT (!ecma_gc_is_object_visited (obj_iter_p)); if (ecma_gc_get_object_refs (obj_iter_p) > 0) { ecma_gc_set_object_visited (obj_iter_p, true); } } /* if some object is referenced from a register variable (i.e. it is root), * start recursive marking traverse from the object */ for (vm_stack_frame_t *frame_iter_p = vm_stack_get_top_frame (); frame_iter_p != NULL; frame_iter_p = frame_iter_p->prev_frame_p) { for (int32_t reg_index = 0; reg_index < frame_iter_p->regs_number; reg_index++) { ecma_value_t reg_value = vm_stack_frame_get_reg_value (frame_iter_p, reg_index); if (ecma_is_value_object (reg_value)) { ecma_object_t *obj_p = ecma_get_object_from_value (reg_value); ecma_gc_set_object_visited (obj_p, true); } } } bool marked_anything_during_current_iteration = false; do { marked_anything_during_current_iteration = false; for (ecma_object_t *obj_iter_p = ecma_gc_objects_lists[ECMA_GC_COLOR_WHITE_GRAY], *obj_prev_p = NULL, *obj_next_p; obj_iter_p != NULL; obj_iter_p = obj_next_p) { obj_next_p = ecma_gc_get_object_next (obj_iter_p); if (ecma_gc_is_object_visited (obj_iter_p)) { /* Moving the object to list of marked objects */ ecma_gc_set_object_next (obj_iter_p, ecma_gc_objects_lists[ECMA_GC_COLOR_BLACK]); ecma_gc_objects_lists[ECMA_GC_COLOR_BLACK] = obj_iter_p; if (likely (obj_prev_p != NULL)) { JERRY_ASSERT (ecma_gc_get_object_next (obj_prev_p) == obj_iter_p); ecma_gc_set_object_next (obj_prev_p, obj_next_p); } else { ecma_gc_objects_lists[ECMA_GC_COLOR_WHITE_GRAY] = obj_next_p; } ecma_gc_mark (obj_iter_p); marked_anything_during_current_iteration = true; } else { obj_prev_p = obj_iter_p; } } } while (marked_anything_during_current_iteration); /* Sweeping objects that are currently unmarked */ for (ecma_object_t *obj_iter_p = ecma_gc_objects_lists[ECMA_GC_COLOR_WHITE_GRAY], *obj_next_p; obj_iter_p != NULL; obj_iter_p = obj_next_p) { obj_next_p = ecma_gc_get_object_next (obj_iter_p); JERRY_ASSERT (!ecma_gc_is_object_visited (obj_iter_p)); ecma_gc_sweep (obj_iter_p); } /* Unmarking all objects */ ecma_gc_objects_lists[ECMA_GC_COLOR_WHITE_GRAY] = ecma_gc_objects_lists[ECMA_GC_COLOR_BLACK]; ecma_gc_objects_lists[ECMA_GC_COLOR_BLACK] = NULL; ecma_gc_visited_flip_flag = !ecma_gc_visited_flip_flag; } /* ecma_gc_run */
/** * Append new value to ecma-values collection */ void ecma_append_to_values_collection (ecma_collection_header_t *header_p, /**< collection's header */ ecma_value_t v, /**< ecma-value to append */ bool do_ref_if_object) /**< if the value is object value, increase reference counter of the object */ { const size_t values_in_chunk = sizeof (ecma_collection_chunk_t::data) / sizeof (ecma_value_t); size_t values_number = header_p->unit_number; size_t pos_of_new_value_in_chunk = values_number % values_in_chunk; values_number++; if ((ecma_length_t) values_number == values_number) { header_p->unit_number = (ecma_length_t) values_number; } else { jerry_fatal (ERR_OUT_OF_MEMORY); } ecma_collection_chunk_t *chunk_p = ECMA_GET_POINTER (ecma_collection_chunk_t, header_p->last_chunk_cp); if (pos_of_new_value_in_chunk == 0) { /* all chunks are currently filled with values */ chunk_p = ecma_alloc_collection_chunk (); chunk_p->next_chunk_cp = ECMA_NULL_POINTER; if (header_p->last_chunk_cp == ECMA_NULL_POINTER) { JERRY_ASSERT (header_p->first_chunk_cp == ECMA_NULL_POINTER); ECMA_SET_NON_NULL_POINTER (header_p->first_chunk_cp, chunk_p); } else { ecma_collection_chunk_t *last_chunk_p = ECMA_GET_NON_NULL_POINTER (ecma_collection_chunk_t, header_p->last_chunk_cp); JERRY_ASSERT (last_chunk_p->next_chunk_cp == ECMA_NULL_POINTER); ECMA_SET_NON_NULL_POINTER (last_chunk_p->next_chunk_cp, chunk_p); } ECMA_SET_NON_NULL_POINTER (header_p->last_chunk_cp, chunk_p); } else { /* last chunk can be appended with the new value */ JERRY_ASSERT (chunk_p != NULL); } ecma_value_t *values_p = (ecma_value_t *) chunk_p->data; JERRY_ASSERT ((uint8_t *) (values_p + pos_of_new_value_in_chunk + 1) <= (uint8_t *) (chunk_p + 1)); values_p[pos_of_new_value_in_chunk] = ecma_copy_value (v, do_ref_if_object); } /* ecma_append_to_values_collection */
/** * Array object creation operation. * * See also: ECMA-262 v5, 15.4.2.1 * ECMA-262 v5, 15.4.2.2 * * @return completion value * Returned value must be freed with ecma_free_completion_value */ ecma_completion_value_t ecma_op_create_array_object (const ecma_value_t *arguments_list_p, /**< list of arguments that are passed to Array constructor */ ecma_length_t arguments_list_len, /**< length of the arguments' list */ bool is_treat_single_arg_as_length) /**< if the value is true, arguments_list_len is 1 and single argument is Number, then treat the single argument as new Array's length rather than as single item of the Array */ { JERRY_ASSERT (arguments_list_len == 0 || arguments_list_p != NULL); uint32_t length; const ecma_value_t *array_items_p; ecma_length_t array_items_count; if (is_treat_single_arg_as_length && arguments_list_len == 1 && ecma_is_value_number (arguments_list_p[0])) { ecma_number_t *num_p = ecma_get_number_from_value (arguments_list_p[0]); uint32_t num_uint32 = ecma_number_to_uint32 (*num_p); if (*num_p != ecma_uint32_to_number (num_uint32)) { return ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_RANGE)); } else { length = num_uint32; array_items_p = NULL; array_items_count = 0; } } else { length = arguments_list_len; array_items_p = arguments_list_p; array_items_count = arguments_list_len; } #ifndef CONFIG_ECMA_COMPACT_PROFILE_DISABLE_ARRAY_BUILTIN ecma_object_t *array_prototype_obj_p = ecma_builtin_get (ECMA_BUILTIN_ID_ARRAY_PROTOTYPE); #else /* !CONFIG_ECMA_COMPACT_PROFILE_DISABLE_ARRAY_BUILTIN */ ecma_object_t *array_prototype_obj_p = ecma_builtin_get (ECMA_BUILTIN_ID_OBJECT_PROTOTYPE); #endif /* CONFIG_ECMA_COMPACT_PROFILE_DISABLE_ARRAY_BUILTIN */ ecma_object_t *obj_p = ecma_create_object (array_prototype_obj_p, true, ECMA_OBJECT_TYPE_ARRAY); ecma_deref_object (array_prototype_obj_p); /* * [[Class]] property is not stored explicitly for objects of ECMA_OBJECT_TYPE_ARRAY type. * * See also: ecma_object_get_class_name */ ecma_string_t *length_magic_string_p = ecma_get_magic_string (LIT_MAGIC_STRING_LENGTH); ecma_number_t *length_num_p = ecma_alloc_number (); *length_num_p = ecma_uint32_to_number (length); ecma_property_t *length_prop_p = ecma_create_named_data_property (obj_p, length_magic_string_p, true, false, false); ecma_set_named_data_property_value (length_prop_p, ecma_make_number_value (length_num_p)); ecma_deref_ecma_string (length_magic_string_p); for (uint32_t index = 0; index < array_items_count; index++) { if (ecma_is_value_array_hole (array_items_p[index])) { continue; } ecma_string_t *item_name_string_p = ecma_new_ecma_string_from_uint32 (index); ecma_builtin_helper_def_prop (obj_p, item_name_string_p, array_items_p[index], true, /* Writable */ true, /* Enumerable */ true, /* Configurable */ false); /* Failure handling */ ecma_deref_ecma_string (item_name_string_p); } return ecma_make_normal_completion_value (ecma_make_object_value (obj_p)); } /* ecma_op_create_array_object */
/** * Unit test's main function. */ int main (int __attr_unused___ argc, char __attr_unused___ **argv) { /* int ecma_date_day (time)*/ JERRY_ASSERT (ecma_date_day (0) == 0); JERRY_ASSERT (ecma_date_day (MS_PER_DAY) == 1); /* ecma_number_t ecma_date_time_within_day (time) */ JERRY_ASSERT (ecma_date_time_within_day (0) == 0); JERRY_ASSERT (ecma_date_time_within_day (42) == 42); JERRY_ASSERT (ecma_date_time_within_day (42.51) == 42.51); JERRY_ASSERT (ecma_date_time_within_day (MS_PER_DAY + 42) == 42); /* int ecma_date_days_in_year (year) */ JERRY_ASSERT (ecma_date_days_in_year (0) == 366); JERRY_ASSERT (ecma_date_days_in_year (1600) == 366); JERRY_ASSERT (ecma_date_days_in_year (1603) == 365); JERRY_ASSERT (ecma_date_days_in_year (1900) == 365); JERRY_ASSERT (ecma_date_days_in_year (1970) == 365); JERRY_ASSERT (ecma_date_days_in_year (2000) == 366); JERRY_ASSERT (ecma_date_days_in_year (2000.45) == 366); JERRY_ASSERT (ecma_date_days_in_year (2012) == 366); JERRY_ASSERT (ecma_date_days_in_year (2015) == 365); JERRY_ASSERT (ecma_date_days_in_year (285616 + 1970) == 365); JERRY_ASSERT (ecma_date_days_in_year (-1970) == 365); /* int ecma_date_day_from_year (year) */ JERRY_ASSERT (ecma_date_day_from_year (1969) == -365); JERRY_ASSERT (ecma_date_day_from_year (1970) == 0); JERRY_ASSERT (ecma_date_day_from_year (1971) == 365); JERRY_ASSERT (ecma_date_day_from_year (2000) == 10957); /* int ecma_date_year_from_time (time) */ JERRY_ASSERT (ecma_date_year_from_time (0) == 1970); JERRY_ASSERT (ecma_date_year_from_time (MS_PER_DAY) == 1970); JERRY_ASSERT (ecma_date_year_from_time ((MS_PER_DAY) * (ecma_number_t) 365 - 1) == 1970); JERRY_ASSERT (ecma_date_year_from_time (MS_PER_DAY * (ecma_number_t) 365) == 1971); JERRY_ASSERT (ecma_date_year_from_time (MS_PER_DAY * (ecma_number_t) (365 * (2015 - 1970))) == 2014); JERRY_ASSERT (ecma_date_year_from_time (MS_PER_DAY * (ecma_number_t) (365.25 * (2015 - 1970))) == 2015); /* int ecma_date_day_within_year (time) */ /* FIXME: Implement */ /* int ecma_date_month_from_time (time) */ /* FIXME: Implement */ /* int ecma_date_date_from_time (time) */ /* FIXME: Implement */ /* int ecma_date_week_day (ecma_number_t time) */ /* FIXME: Implement */ /* ecma_number_t ecma_date_local_tza () */ /* FIXME: Implement */ /* ecma_number_t ecma_date_daylight_saving_ta (time) */ /* FIXME: Implement */ /* ecma_number_t ecma_date_local_time (time) */ /* FIXME: Implement */ /* ecma_number_t ecma_date_utc (time) */ /* FIXME: Implement */ /* ecma_number_t ecma_date_hour_from_time (time) */ /* FIXME: Implement */ /* ecma_number_t ecma_date_min_from_time (time) */ /* FIXME: Implement */ /* ecma_number_t ecma_date_sec_from_time (time) */ /* FIXME: Implement */ /* ecma_number_t ecma_date_ms_from_time (time) */ /* FIXME: Implement */ /* ecma_number_t ecma_date_make_time (hour, min, sec, ms) */ /* FIXME: Implement */ /* ecma_number_t ecma_date_make_day (year, month, date) */ JERRY_ASSERT (ecma_date_make_day (1970, 0, 1) == 0); JERRY_ASSERT (ecma_date_make_day (1970, 0, 2.5) == 1); JERRY_ASSERT (ecma_date_make_day (1970, 1, 35) == 65); JERRY_ASSERT (ecma_date_make_day (1970, 13, 35) == 430); JERRY_ASSERT (ecma_date_make_day (2016, 2, 1) == 16861); /* ecma_number_t ecma_date_make_date (day, time) */ /* FIXME: Implement */ /* ecma_number_t ecma_date_time_clip (year) */ /* FIXME: Implement */ return 0; } /* main */