Implementing 'Object.prototype.toString' and 'Object.prototype.valueOf' built-ins.
This commit is contained in:
@@ -44,17 +44,6 @@
|
||||
#define ECMA_BUILTIN_OBJECT_PROTOTYPE_OBJECT_OBJECT_VALUES_PROPERTY_LIST(macro) \
|
||||
macro (ECMA_MAGIC_STRING_CONSTRUCTOR, ecma_builtin_get (ECMA_BUILTIN_ID_OBJECT))
|
||||
|
||||
/*
|
||||
Object.prototype.constructor
|
||||
|
||||
Object.prototype.toString
|
||||
Object.prototype.toLocaleString
|
||||
Object.prototype.valueOf
|
||||
Object.prototype.hasOwnProperty
|
||||
Object.prototype.isPrototypeOf
|
||||
Object.prototype.propertyIsEnumerable
|
||||
*/
|
||||
|
||||
/**
|
||||
* List of the Object.prototype object built-in routine properties in format
|
||||
* 'macro (name, C function name, arguments number of the routine, length value of the routine)'.
|
||||
@@ -116,7 +105,76 @@ const ecma_length_t ecma_builtin_object_prototype_property_number = (sizeof (ecm
|
||||
static ecma_completion_value_t
|
||||
ecma_builtin_object_prototype_object_to_string (ecma_value_t this) /**< this argument */
|
||||
{
|
||||
JERRY_UNIMPLEMENTED_REF_UNUSED_VARS (this);
|
||||
ecma_magic_string_id_t type_string;
|
||||
|
||||
if (ecma_is_value_undefined (this))
|
||||
{
|
||||
type_string = ECMA_MAGIC_STRING_UNDEFINED_UL;
|
||||
}
|
||||
else if (ecma_is_value_null (this))
|
||||
{
|
||||
type_string = ECMA_MAGIC_STRING_NULL_UL;
|
||||
}
|
||||
else
|
||||
{
|
||||
ecma_completion_value_t obj_this = ecma_op_to_object (this);
|
||||
|
||||
if (!ecma_is_completion_value_normal (obj_this))
|
||||
{
|
||||
return obj_this;
|
||||
}
|
||||
|
||||
JERRY_ASSERT (obj_this.u.value.value_type == ECMA_TYPE_OBJECT);
|
||||
|
||||
ecma_object_t *obj_p = ECMA_GET_POINTER (obj_this.u.value.value);
|
||||
|
||||
ecma_property_t *class_prop_p = ecma_get_internal_property (obj_p,
|
||||
ECMA_INTERNAL_PROPERTY_CLASS);
|
||||
type_string = (ecma_magic_string_id_t) class_prop_p->u.internal_property.value;
|
||||
|
||||
ecma_free_completion_value (obj_this);
|
||||
}
|
||||
|
||||
/* 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;
|
||||
ecma_char_t str_buffer[buffer_size];
|
||||
|
||||
const ecma_char_t *left_square_zt_str_p = ecma_get_magic_string_zt (ECMA_MAGIC_STRING_LEFT_SQUARE_CHAR);
|
||||
const ecma_char_t *object_zt_str_p = ecma_get_magic_string_zt (ECMA_MAGIC_STRING_OBJECT);
|
||||
const ecma_char_t *space_zt_str_p = ecma_get_magic_string_zt (ECMA_MAGIC_STRING_SPACE_CHAR);
|
||||
const ecma_char_t *type_name_zt_str_p = ecma_get_magic_string_zt (type_string);
|
||||
const ecma_char_t *right_square_zt_str_p = ecma_get_magic_string_zt (ECMA_MAGIC_STRING_RIGHT_SQUARE_CHAR);
|
||||
|
||||
ecma_char_t *buffer_ptr = str_buffer;
|
||||
ssize_t buffer_size_left = buffer_size;
|
||||
buffer_ptr = ecma_copy_zt_string_to_buffer (left_square_zt_str_p,
|
||||
buffer_ptr,
|
||||
buffer_size_left);
|
||||
buffer_size_left = buffer_size - (buffer_ptr - str_buffer) * (ssize_t) sizeof (ecma_char_t);
|
||||
buffer_ptr = ecma_copy_zt_string_to_buffer (object_zt_str_p,
|
||||
buffer_ptr,
|
||||
buffer_size_left);
|
||||
buffer_size_left = buffer_size - (buffer_ptr - str_buffer) * (ssize_t) sizeof (ecma_char_t);
|
||||
buffer_ptr = ecma_copy_zt_string_to_buffer (space_zt_str_p,
|
||||
buffer_ptr,
|
||||
buffer_size_left);
|
||||
buffer_size_left = buffer_size - (buffer_ptr - str_buffer) * (ssize_t) sizeof (ecma_char_t);
|
||||
buffer_ptr = ecma_copy_zt_string_to_buffer (type_name_zt_str_p,
|
||||
buffer_ptr,
|
||||
buffer_size_left);
|
||||
buffer_size_left = buffer_size - (buffer_ptr - str_buffer) * (ssize_t) sizeof (ecma_char_t);
|
||||
buffer_ptr = ecma_copy_zt_string_to_buffer (right_square_zt_str_p,
|
||||
buffer_ptr,
|
||||
buffer_size_left);
|
||||
buffer_size_left = buffer_size - (buffer_ptr - str_buffer) * (ssize_t) sizeof (ecma_char_t);
|
||||
|
||||
JERRY_ASSERT (buffer_size_left >= 0);
|
||||
|
||||
ecma_string_t *ret_string_p = ecma_new_ecma_string (str_buffer);
|
||||
|
||||
return ecma_make_normal_completion_value (ecma_make_string_value (ret_string_p));
|
||||
} /* ecma_builtin_object_prototype_object_to_string */
|
||||
|
||||
/**
|
||||
@@ -131,7 +189,7 @@ ecma_builtin_object_prototype_object_to_string (ecma_value_t this) /**< this arg
|
||||
static ecma_completion_value_t
|
||||
ecma_builtin_object_prototype_object_value_of (ecma_value_t this) /**< this argument */
|
||||
{
|
||||
return ecma_builtin_object_prototype_object_to_string (this);
|
||||
return ecma_op_to_object (this);
|
||||
} /* ecma_builtin_object_prototype_object_value_of */
|
||||
|
||||
/**
|
||||
|
||||
@@ -654,6 +654,8 @@ typedef enum
|
||||
ECMA_MAGIC_STRING_LENGTH, /**< "length" */
|
||||
ECMA_MAGIC_STRING_NAN, /**< "NaN" */
|
||||
ECMA_MAGIC_STRING_INFINITY_UL, /**< "Infinity" */
|
||||
ECMA_MAGIC_STRING_UNDEFINED_UL, /**< "Undefined" */
|
||||
ECMA_MAGIC_STRING_NULL_UL, /**< "Null" */
|
||||
ECMA_MAGIC_STRING_OBJECT_UL, /**< "Object" */
|
||||
ECMA_MAGIC_STRING_FUNCTION_UL, /**< "Function" */
|
||||
ECMA_MAGIC_STRING_ARRAY_UL, /**< "Array" */
|
||||
@@ -812,6 +814,9 @@ typedef enum
|
||||
ECMA_MAGIC_STRING_TEST, /**< "test" */
|
||||
ECMA_MAGIC_STRING_NAME, /**< "name" */
|
||||
ECMA_MAGIC_STRING_MESSAGE, /**< "message" */
|
||||
ECMA_MAGIC_STRING_LEFT_SQUARE_CHAR, /**< "[" */
|
||||
ECMA_MAGIC_STRING_RIGHT_SQUARE_CHAR, /**< "]" */
|
||||
ECMA_MAGIC_STRING_SPACE_CHAR, /**< " " */
|
||||
ECMA_MAGIC_STRING__EMPTY, /**< "" */
|
||||
ECMA_MAGIC_STRING__COUNT /**< number of magic strings */
|
||||
} ecma_magic_string_id_t;
|
||||
|
||||
@@ -1169,9 +1169,9 @@ ecma_compare_zt_strings_relational (const ecma_char_t *string1_p, /**< zero-term
|
||||
* Warning:
|
||||
* the routine requires that buffer size is enough
|
||||
*
|
||||
* @return number of bytes copied
|
||||
* @return pointer to null character of copied string in destination buffer
|
||||
*/
|
||||
ssize_t
|
||||
ecma_char_t*
|
||||
ecma_copy_zt_string_to_buffer (const ecma_char_t *string_p, /**< zero-terminated string */
|
||||
ecma_char_t *buffer_p, /**< destination buffer */
|
||||
ssize_t buffer_size) /**< size of buffer */
|
||||
@@ -1193,7 +1193,7 @@ ecma_copy_zt_string_to_buffer (const ecma_char_t *string_p, /**< zero-terminated
|
||||
|
||||
*buf_iter_p = ECMA_CHAR_NULL;
|
||||
|
||||
return bytes_copied;
|
||||
return buf_iter_p;
|
||||
} /* ecma_copy_zt_string_to_buffer */
|
||||
|
||||
/**
|
||||
@@ -1269,6 +1269,8 @@ ecma_get_magic_string_zt (ecma_magic_string_id_t id) /**< magic string id */
|
||||
case ECMA_MAGIC_STRING_LENGTH: return (ecma_char_t*) "length";
|
||||
case ECMA_MAGIC_STRING_NAN: return (ecma_char_t*) "NaN";
|
||||
case ECMA_MAGIC_STRING_INFINITY_UL: return (ecma_char_t*) "Infinity";
|
||||
case ECMA_MAGIC_STRING_UNDEFINED_UL: return (ecma_char_t*) "Undefined";
|
||||
case ECMA_MAGIC_STRING_NULL_UL: return (ecma_char_t*) "Null";
|
||||
case ECMA_MAGIC_STRING_OBJECT_UL: return (ecma_char_t*) "Object";
|
||||
case ECMA_MAGIC_STRING_FUNCTION_UL: return (ecma_char_t*) "Function";
|
||||
case ECMA_MAGIC_STRING_ARRAY_UL: return (ecma_char_t*) "Array";
|
||||
@@ -1427,6 +1429,9 @@ ecma_get_magic_string_zt (ecma_magic_string_id_t id) /**< magic string id */
|
||||
case ECMA_MAGIC_STRING_TEST: return (ecma_char_t*) "test";
|
||||
case ECMA_MAGIC_STRING_NAME: return (ecma_char_t*) "name";
|
||||
case ECMA_MAGIC_STRING_MESSAGE: return (ecma_char_t*) "message";
|
||||
case ECMA_MAGIC_STRING_LEFT_SQUARE_CHAR: return (ecma_char_t*) "[";
|
||||
case ECMA_MAGIC_STRING_RIGHT_SQUARE_CHAR: return (ecma_char_t*) "]";
|
||||
case ECMA_MAGIC_STRING_SPACE_CHAR: return (ecma_char_t*) " ";
|
||||
case ECMA_MAGIC_STRING__EMPTY: return (ecma_char_t*) "";
|
||||
case ECMA_MAGIC_STRING__COUNT: break;
|
||||
}
|
||||
|
||||
@@ -116,7 +116,10 @@ extern int32_t ecma_string_get_length (const ecma_string_t *string_p);
|
||||
extern ecma_char_t ecma_string_get_char_at_pos (const ecma_string_t *string_p, uint32_t index);
|
||||
extern bool ecma_compare_zt_strings (const ecma_char_t *string1_p, const ecma_char_t *string2_p);
|
||||
extern bool ecma_compare_zt_strings_relational (const ecma_char_t *string1_p, const ecma_char_t *string2_p);
|
||||
extern ssize_t ecma_copy_zt_string_to_buffer (const ecma_char_t *string_p, ecma_char_t *buffer_p, ssize_t buffer_size);
|
||||
extern ecma_char_t*
|
||||
ecma_copy_zt_string_to_buffer (const ecma_char_t *string_p,
|
||||
ecma_char_t *buffer_p,
|
||||
ssize_t buffer_size);
|
||||
extern ecma_length_t ecma_zt_string_length (const ecma_char_t *string_p);
|
||||
|
||||
extern void ecma_strings_init (void);
|
||||
|
||||
Reference in New Issue
Block a user