Do not create temporary object during [[Get]] operation with primitive base (#3278)

JerryScript-DCO-1.0-Signed-off-by: Robert Fancsik frobert@inf.u-szeged.hu
This commit is contained in:
Robert Fancsik 2019-11-05 19:37:20 +01:00 committed by Dániel Bátyai
parent f2a67876eb
commit 06135da642
3 changed files with 88 additions and 44 deletions

View File

@ -120,59 +120,62 @@ ecma_value_t
ecma_op_get_value_object_base (ecma_value_t base_value, /**< base value */
ecma_string_t *property_name_p) /**< property name */
{
if (ecma_is_value_object (base_value))
{
ecma_object_t *obj_p = ecma_get_object_from_value (base_value);
JERRY_ASSERT (obj_p != NULL
&& !ecma_is_lexical_environment (obj_p));
ecma_object_t *obj_p;
return ecma_op_object_get (obj_p, property_name_p);
if (JERRY_UNLIKELY (ecma_is_value_object (base_value)))
{
obj_p = ecma_get_object_from_value (base_value);
}
JERRY_ASSERT (ecma_is_value_boolean (base_value)
|| ecma_is_value_number (base_value)
|| ECMA_ASSERT_VALUE_IS_SYMBOL (base_value)
|| ecma_is_value_string (base_value));
/* Fast path for the strings length property. The length of the string
can be obtained directly from the ecma-string. */
if (ecma_is_value_string (base_value) && ecma_string_is_length (property_name_p))
else
{
ecma_string_t *string_p = ecma_get_string_from_value (base_value);
ecma_builtin_id_t id = ECMA_BUILTIN_ID_OBJECT_PROTOTYPE;
return ecma_make_uint32_value (ecma_string_get_length (string_p));
}
ecma_value_t object_base = ecma_op_to_object (base_value);
JERRY_ASSERT (!ECMA_IS_VALUE_ERROR (object_base));
ecma_object_t *object_p = ecma_get_object_from_value (object_base);
JERRY_ASSERT (object_p != NULL
&& !ecma_is_lexical_environment (object_p));
ecma_value_t ret_value = ECMA_VALUE_UNDEFINED;
while (true)
{
ecma_value_t value = ecma_op_object_find_own (base_value, object_p, property_name_p);
if (ecma_is_value_found (value))
if (JERRY_LIKELY (ecma_is_value_string (base_value)))
{
ret_value = value;
break;
ecma_string_t *string_p = ecma_get_string_from_value (base_value);
if (ecma_string_is_length (property_name_p))
{
return ecma_make_uint32_value (ecma_string_get_length (string_p));
}
uint32_t index = ecma_string_get_array_index (property_name_p);
if (index != ECMA_STRING_NOT_ARRAY_INDEX
&& index < ecma_string_get_length (string_p))
{
ecma_char_t char_at_idx = ecma_string_get_char_at_pos (string_p, index);
return ecma_make_string_value (ecma_new_ecma_string_from_code_unit (char_at_idx));
}
#if ENABLED (JERRY_BUILTIN_STRING)
id = ECMA_BUILTIN_ID_STRING_PROTOTYPE;
#endif /* ENABLED (JERRY_BUILTIN_STRING) */
}
else if (ecma_is_value_number (base_value))
{
#if ENABLED (JERRY_BUILTIN_NUMBER)
id = ECMA_BUILTIN_ID_NUMBER_PROTOTYPE;
#endif /* ENABLED (JERRY_BUILTIN_NUMBER) */
}
#if ENABLED (JERRY_ES2015)
else if (ecma_is_value_symbol (base_value))
{
id = ECMA_BUILTIN_ID_SYMBOL_PROTOTYPE;
}
#endif /* ENABLED (JERRY_ES2015) */
else
{
JERRY_ASSERT (ecma_is_value_boolean (base_value));
#if ENABLED (JERRY_BUILTIN_BOOLEAN)
id = ECMA_BUILTIN_ID_BOOLEAN_PROTOTYPE;
#endif /* ENABLED (JERRY_BUILTIN_BOOLEAN) */
}
if (object_p->u2.prototype_cp == JMEM_CP_NULL)
{
break;
}
object_p = ECMA_GET_NON_NULL_POINTER (ecma_object_t, object_p->u2.prototype_cp);
obj_p = ecma_builtin_get (id);
}
ecma_free_value (object_base);
return ret_value;
return ecma_op_object_get_with_receiver (obj_p, property_name_p, base_value);
} /* ecma_op_get_value_object_base */
/**

View File

@ -825,6 +825,45 @@ ecma_op_object_get (ecma_object_t *object_p, /**< the object */
return ECMA_VALUE_UNDEFINED;
} /* ecma_op_object_get */
/**
* [[Get]] operation of ecma object with the specified receiver
*
* This function returns the value of a named property, or undefined
* if the property is not found in the prototype chain. If the property
* is an accessor, it calls the "get" callback function and returns
* with its result (including error throws).
*
* See also:
* ECMA-262 v5, 8.6.2; ECMA-262 v5, Table 8
*
* @return ecma value
* Returned value must be freed with ecma_free_value
*/
ecma_value_t
ecma_op_object_get_with_receiver (ecma_object_t *object_p, /**< the object */
ecma_string_t *property_name_p, /**< property name */
ecma_value_t receiver) /**< receiver to invoke getter function */
{
while (true)
{
ecma_value_t value = ecma_op_object_find_own (receiver, object_p, property_name_p);
if (ecma_is_value_found (value))
{
return value;
}
if (object_p->u2.prototype_cp == JMEM_CP_NULL)
{
break;
}
object_p = ECMA_GET_NON_NULL_POINTER (ecma_object_t, object_p->u2.prototype_cp);
}
return ECMA_VALUE_UNDEFINED;
} /* ecma_op_object_get_with_receiver */
/**
* [[Get]] operation of ecma object specified for uint32_t property index
*

View File

@ -36,6 +36,8 @@ ecma_value_t ecma_op_object_find_by_uint32_index (ecma_object_t *object_p, uint3
ecma_value_t ecma_op_object_find_by_number_index (ecma_object_t *object_p, ecma_number_t index);
ecma_value_t ecma_op_object_get_own_data_prop (ecma_object_t *object_p, ecma_string_t *property_name_p);
ecma_value_t ecma_op_object_get (ecma_object_t *object_p, ecma_string_t *property_name_p);
ecma_value_t ecma_op_object_get_with_receiver (ecma_object_t *object_p, ecma_string_t *property_name_p,
ecma_value_t receiver);
ecma_value_t ecma_op_object_get_length (ecma_object_t *object_p, uint32_t *length_p);
ecma_value_t ecma_op_object_get_by_uint32_index (ecma_object_t *object_p, uint32_t index);
ecma_value_t ecma_op_object_get_by_magic_id (ecma_object_t *object_p, lit_magic_string_id_t property_id);