Add ES2015 feature: ArrayBuffer (#1467)

This patch implements ArrayBuffer and ArrayBuffer.prototype built-in objects.

JerryScript-DCO-1.0-Signed-off-by: Zidong Jiang zidong.jiang@intel.com
This commit is contained in:
Zidong Jiang
2016-12-07 21:04:01 +08:00
committed by Zoltan Herczeg
parent eccfc1849e
commit 29d058cec4
67 changed files with 845 additions and 59 deletions
@@ -0,0 +1,123 @@
/* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "ecma-arraybuffer-object.h"
#include "ecma-conversion.h"
#include "ecma-objects.h"
#include "ecma-builtins.h"
#include "ecma-exceptions.h"
#include "ecma-gc.h"
#include "ecma-globals.h"
#include "ecma-helpers.h"
#ifndef CONFIG_DISABLE_ARRAYBUFFER_BUILTIN
/** \addtogroup ecma ECMA
* @{
*
* \addtogroup ecmaarraybufferobject ECMA ArrayBuffer object related routines
* @{
*/
/**
* ArrayBuffer object creation operation.
*
* See also: ES2015 24.1.1.1
*
* @return ecma value
* Returned value must be freed with ecma_free_value
*/
ecma_value_t
ecma_op_create_arraybuffer_object (const ecma_value_t *arguments_list_p, /**< list of arguments that
* are passed to String constructor */
ecma_length_t arguments_list_len) /**< length of the arguments' list */
{
JERRY_ASSERT (arguments_list_len == 0 || arguments_list_p != NULL);
uint32_t length = 0;
if (arguments_list_len > 0)
{
ecma_number_t num = ecma_get_number_from_value (ecma_op_to_number (arguments_list_p[0]));
length = ecma_number_to_uint32 (num);
if (num != ((ecma_number_t) length))
{
return ecma_raise_range_error (ECMA_ERR_MSG ("Invalid ArrayBuffer length."));
}
}
ecma_object_t *object_p = ecma_arraybuffer_new_object (length);
return ecma_make_object_value (object_p);
} /* ecma_op_create_arraybuffer_object */
/**
* Helper function: create arraybuffer object based on the array length
*
* The struct of arraybuffer object:
* ecma_object_t
* extend_part
* data buffer
*
* @return ecma value
* Returned value must be freed with ecma_free_value
*/
ecma_object_t *
ecma_arraybuffer_new_object (ecma_length_t length) /**< length of the arraybuffer */
{
ecma_object_t *prototype_obj_p = ecma_builtin_get (ECMA_BUILTIN_ID_ARRAYBUFFER_PROTOTYPE);
ecma_object_t *object_p = ecma_create_object (prototype_obj_p,
sizeof (ecma_extended_object_t) + length,
ECMA_OBJECT_TYPE_CLASS);
ecma_deref_object (prototype_obj_p);
ecma_extended_object_t *ext_object_p = (ecma_extended_object_t *) object_p;
ext_object_p->u.class_prop.class_id = LIT_MAGIC_STRING_ARRAY_BUFFER_UL;
ext_object_p->u.class_prop.u.length = length;
return object_p;
} /* ecma_arraybuffer_new_object */
/**
* Helper function: return the length of the buffer inside the arraybuffer object
*
* @return ecma_length_t, the length of the arraybuffer
*/
inline ecma_length_t __attr_pure___ __attr_always_inline___
ecma_arraybuffer_get_length (ecma_object_t *object_p) /**< pointer to the ArrayBuffer object */
{
JERRY_ASSERT (ecma_object_class_is (object_p, LIT_MAGIC_STRING_ARRAY_BUFFER_UL));
ecma_extended_object_t *ext_object_p = (ecma_extended_object_t *) object_p;
return ext_object_p->u.class_prop.u.length;
} /* ecma_arraybuffer_get_length */
/**
* Helper function: return the pointer to the data buffer inside the arraybuffer object
*
* @return pointer to the data buffer
*/
inline lit_utf8_byte_t * __attr_pure___ __attr_always_inline___
ecma_arraybuffer_get_buffer (ecma_object_t *object_p) /**< pointer to the ArrayBuffer object */
{
JERRY_ASSERT (ecma_object_class_is (object_p, LIT_MAGIC_STRING_ARRAY_BUFFER_UL));
ecma_extended_object_t *ext_object_p = (ecma_extended_object_t *) object_p;
return (lit_utf8_byte_t *) (ext_object_p + 1);
} /* ecma_arraybuffer_get_buffer */
/**
* @}
* @}
*/
#endif /* !CONFIG_DISABLE_ARRAYBUFFER_BUILTIN */
@@ -0,0 +1,46 @@
/* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef ECMA_ARRAYBUFFER_OBJECT_H
#define ECMA_ARRAYBUFFER_OBJECT_H
#include "ecma-globals.h"
/** \addtogroup ecma ECMA
* @{
*
* \addtogroup ecmaarraybufferobject ECMA Arraybuffer object related routines
* @{
*/
extern ecma_value_t
ecma_op_create_arraybuffer_object (const ecma_value_t *, ecma_length_t);
/**
* Helper functions for arraybuffer.
*/
extern ecma_object_t *
ecma_arraybuffer_new_object (ecma_length_t);
extern lit_utf8_byte_t *
ecma_arraybuffer_get_buffer (ecma_object_t *) __attr_pure___ __attr_always_inline___;
extern ecma_length_t
ecma_arraybuffer_get_length (ecma_object_t *) __attr_pure___ __attr_always_inline___;
/**
* @}
* @}
*/
#endif /* !ECMA_ARRAYBUFFER_OBJECT_H */
@@ -57,7 +57,7 @@ ecma_op_create_boolean_object (ecma_value_t arg) /**< argument passed to the Boo
ecma_extended_object_t *ext_object_p = (ecma_extended_object_t *) object_p;
ext_object_p->u.class_prop.class_id = LIT_MAGIC_STRING_BOOLEAN_UL;
ext_object_p->u.class_prop.value = ecma_make_boolean_value (boolean_value);
ext_object_p->u.class_prop.u.value = ecma_make_boolean_value (boolean_value);
return ecma_make_object_value (object_p);
} /* ecma_op_create_boolean_object */
@@ -64,7 +64,7 @@ ecma_op_create_number_object (ecma_value_t arg) /**< argument passed to the Numb
ext_object_p->u.class_prop.class_id = LIT_MAGIC_STRING_NUMBER_UL;
/* Pass reference (no need to free conv_to_num_completion). */
ext_object_p->u.class_prop.value = conv_to_num_completion;
ext_object_p->u.class_prop.u.value = conv_to_num_completion;
return ecma_make_object_value (object_p);
} /* ecma_op_create_number_object */
@@ -384,7 +384,8 @@ ecma_op_general_object_define_own_property (ecma_object_t *object_p, /**< the ob
property_name_p,
property_desc_p->get_p,
property_desc_p->set_p,
prop_attributes);
prop_attributes,
NULL);
}
return ecma_make_simple_value (ECMA_SIMPLE_VALUE_TRUE);
+5 -5
View File
@@ -92,7 +92,7 @@ ecma_op_object_get_own_property (ecma_object_t *object_p, /**< the object */
{
if (options & ECMA_PROPERTY_GET_VALUE)
{
ecma_value_t prim_value_p = ext_object_p->u.class_prop.value;
ecma_value_t prim_value_p = ext_object_p->u.class_prop.u.value;
ecma_string_t *prim_value_str_p = ecma_get_string_from_value (prim_value_p);
ecma_length_t length = ecma_string_get_length (prim_value_str_p);
@@ -106,7 +106,7 @@ ecma_op_object_get_own_property (ecma_object_t *object_p, /**< the object */
if (index != ECMA_STRING_NOT_ARRAY_INDEX)
{
ecma_value_t prim_value_p = ext_object_p->u.class_prop.value;
ecma_value_t prim_value_p = ext_object_p->u.class_prop.u.value;
ecma_string_t *prim_value_str_p = ecma_get_string_from_value (prim_value_p);
if (index < ecma_string_get_length (prim_value_str_p))
@@ -347,7 +347,7 @@ ecma_op_object_find_own (ecma_value_t base_value, /**< base value */
{
if (ecma_string_is_length (property_name_p))
{
ecma_value_t prim_value_p = ext_object_p->u.class_prop.value;
ecma_value_t prim_value_p = ext_object_p->u.class_prop.u.value;
ecma_string_t *prim_value_str_p = ecma_get_string_from_value (prim_value_p);
ecma_length_t length = ecma_string_get_length (prim_value_str_p);
@@ -359,7 +359,7 @@ ecma_op_object_find_own (ecma_value_t base_value, /**< base value */
if (index != ECMA_STRING_NOT_ARRAY_INDEX)
{
ecma_value_t prim_value_p = ext_object_p->u.class_prop.value;
ecma_value_t prim_value_p = ext_object_p->u.class_prop.u.value;
ecma_string_t *prim_value_str_p = ecma_get_string_from_value (prim_value_p);
@@ -684,7 +684,7 @@ ecma_op_object_put (ecma_object_t *object_p, /**< the object */
if (index != ECMA_STRING_NOT_ARRAY_INDEX)
{
ecma_value_t prim_value_p = ext_object_p->u.class_prop.value;
ecma_value_t prim_value_p = ext_object_p->u.class_prop.u.value;
ecma_string_t *prim_value_str_p = ecma_get_string_from_value (prim_value_p);
if (index < ecma_string_get_length (prim_value_str_p))
@@ -235,7 +235,7 @@ ecma_op_create_regexp_object_from_bytecode (re_compiled_code_t *bytecode_p) /**<
ext_object_p->u.class_prop.class_id = LIT_MAGIC_STRING_REGEXP_UL;
/* Set bytecode internal property. */
ECMA_SET_INTERNAL_VALUE_POINTER (ext_object_p->u.class_prop.value, bytecode_p);
ECMA_SET_INTERNAL_VALUE_POINTER (ext_object_p->u.class_prop.u.value, bytecode_p);
ecma_bytecode_ref ((ecma_compiled_code_t *) bytecode_p);
/* Initialize RegExp object properties */
@@ -294,7 +294,7 @@ ecma_op_create_regexp_object (ecma_string_t *pattern_p, /**< input pattern */
/* Set [[Class]] and bytecode internal properties. */
ext_object_p->u.class_prop.class_id = LIT_MAGIC_STRING_REGEXP_UL;
ECMA_SET_INTERNAL_VALUE_POINTER (ext_object_p->u.class_prop.value, bc_p);
ECMA_SET_INTERNAL_VALUE_POINTER (ext_object_p->u.class_prop.u.value, bc_p);
ret_value = ecma_make_object_value (object_p);
@@ -1243,7 +1243,7 @@ ecma_regexp_exec_helper (ecma_value_t regexp_value, /**< RegExp object */
ecma_extended_object_t *ext_object_p = (ecma_extended_object_t *) regexp_object_p;
re_compiled_code_t *bc_p = ECMA_GET_INTERNAL_VALUE_POINTER (re_compiled_code_t,
ext_object_p->u.class_prop.value);
ext_object_p->u.class_prop.u.value);
if (bc_p == NULL)
{
@@ -84,7 +84,7 @@ ecma_op_create_string_object (const ecma_value_t *arguments_list_p, /**< list of
ecma_extended_object_t *ext_object_p = (ecma_extended_object_t *) object_p;
ext_object_p->u.class_prop.class_id = LIT_MAGIC_STRING_STRING_UL;
ext_object_p->u.class_prop.value = ecma_make_string_value (prim_prop_str_value_p);
ext_object_p->u.class_prop.u.value = ecma_make_string_value (prim_prop_str_value_p);
return ecma_make_object_value (object_p);
} /* ecma_op_create_string_object */
@@ -118,7 +118,7 @@ ecma_op_string_list_lazy_property_names (ecma_object_t *obj_p, /**< a String obj
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.value);
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);