Implement %TypedArray%.from and fix the issue #1670 (#1679)

JerryScript-DCO-1.0-Signed-off-by: Zidong Jiang zidong.jiang@intel.com
This commit is contained in:
Zidong Jiang
2017-03-24 17:37:22 +08:00
committed by Robert Sipka
parent f50193111b
commit 8571ebfae5
6 changed files with 271 additions and 7 deletions
@@ -20,6 +20,7 @@
#include "ecma-helpers.h"
#include "ecma-typedarray-object.h"
#include "ecma-try-catch-macro.h"
#include "ecma-function-object.h"
#include "jrt.h"
#ifndef CONFIG_DISABLE_ES2015_TYPEDARRAY_BUILTIN
@@ -55,13 +56,128 @@ ecma_builtin_typedarray_from (ecma_value_t this_arg, /**< 'this' argument */
const ecma_value_t *arguments_list_p, /**< arguments list */
ecma_length_t arguments_list_len) /**< number of arguments */
{
JERRY_UNUSED (this_arg);
JERRY_UNUSED (arguments_list_p);
JERRY_UNUSED (arguments_list_len);
JERRY_ASSERT (arguments_list_len == 0 || arguments_list_p != NULL);
/* TODO: inplement 'from' */
if (!ecma_is_constructor (this_arg))
{
return ecma_raise_type_error (ECMA_ERR_MSG ("'this' is not a constructor."));
}
ecma_value_t source;
ecma_value_t map_fn = ecma_make_simple_value (ECMA_SIMPLE_VALUE_UNDEFINED);
ecma_value_t this_in_fn = ecma_make_simple_value (ECMA_SIMPLE_VALUE_UNDEFINED);
if (arguments_list_len == 0)
{
return ecma_raise_type_error (ECMA_ERR_MSG ("no source argument"));
}
source = arguments_list_p[0];
if (arguments_list_len > 1)
{
map_fn = arguments_list_p[1];
if (!ecma_op_is_callable (map_fn))
{
return ecma_raise_type_error (ECMA_ERR_MSG ("mapfn argument is not callable"));
}
if (arguments_list_len > 2)
{
this_in_fn = arguments_list_p[2];
}
}
ecma_object_t *obj_p = ecma_get_object_from_value (this_arg);
uint8_t builtin_id = ecma_get_object_builtin_id (obj_p);
ecma_object_t *proto_p;
uint8_t element_size_shift;
lit_magic_string_id_t class_id;
switch (builtin_id)
{
case ECMA_BUILTIN_ID_INT8ARRAY:
{
proto_p = ecma_builtin_get (ECMA_BUILTIN_ID_INT8ARRAY_PROTOTYPE);
element_size_shift = 0;
class_id = LIT_MAGIC_STRING_INT8_ARRAY_UL;
break;
}
case ECMA_BUILTIN_ID_UINT8ARRAY:
{
proto_p = ecma_builtin_get (ECMA_BUILTIN_ID_UINT8ARRAY_PROTOTYPE);
element_size_shift = 0;
class_id = LIT_MAGIC_STRING_UINT8_ARRAY_UL;
break;
}
case ECMA_BUILTIN_ID_UINT8CLAMPEDARRAY:
{
proto_p = ecma_builtin_get (ECMA_BUILTIN_ID_UINT8CLAMPEDARRAY_PROTOTYPE);
element_size_shift = 0;
class_id = LIT_MAGIC_STRING_UINT8_CLAMPED_ARRAY_UL;
break;
}
case ECMA_BUILTIN_ID_INT16ARRAY:
{
proto_p = ecma_builtin_get (ECMA_BUILTIN_ID_INT16ARRAY_PROTOTYPE);
element_size_shift = 1;
class_id = LIT_MAGIC_STRING_INT16_ARRAY_UL;
break;
}
case ECMA_BUILTIN_ID_UINT16ARRAY:
{
proto_p = ecma_builtin_get (ECMA_BUILTIN_ID_UINT16ARRAY_PROTOTYPE);
element_size_shift = 1;
class_id = LIT_MAGIC_STRING_UINT16_ARRAY_UL;
break;
}
case ECMA_BUILTIN_ID_INT32ARRAY:
{
proto_p = ecma_builtin_get (ECMA_BUILTIN_ID_INT32ARRAY_PROTOTYPE);
element_size_shift = 2;
class_id = LIT_MAGIC_STRING_INT32_ARRAY_UL;
break;
}
case ECMA_BUILTIN_ID_UINT32ARRAY:
{
proto_p = ecma_builtin_get (ECMA_BUILTIN_ID_UINT32ARRAY_PROTOTYPE);
element_size_shift = 2;
class_id = LIT_MAGIC_STRING_UINT32_ARRAY_UL;
break;
}
case ECMA_BUILTIN_ID_FLOAT32ARRAY:
{
proto_p = ecma_builtin_get (ECMA_BUILTIN_ID_FLOAT32ARRAY_PROTOTYPE);
element_size_shift = 2;
class_id = LIT_MAGIC_STRING_FLOAT32_ARRAY_UL;
break;
}
#if CONFIG_ECMA_NUMBER_TYPE == CONFIG_ECMA_NUMBER_FLOAT64
case ECMA_BUILTIN_ID_FLOAT64ARRAY:
{
proto_p = ecma_builtin_get (ECMA_BUILTIN_ID_FLOAT64ARRAY_PROTOTYPE);
element_size_shift = 3;
class_id = LIT_MAGIC_STRING_FLOAT64_ARRAY_UL;
break;
}
#endif /* CONFIG_ECMA_NUMBER_TYPE == CONFIG_ECMA_NUMBER_FLOAT64 */
default:
{
return ecma_raise_type_error (ECMA_ERR_MSG ("'this' is not a typedarray constructor"));
}
}
ecma_deref_object (proto_p);
return ecma_op_typedarray_from (source,
map_fn,
this_in_fn,
proto_p,
element_size_shift,
class_id);
return ecma_make_simple_value (ECMA_SIMPLE_VALUE_EMPTY);
} /* ecma_builtin_typedarray_from */
/**
@@ -82,9 +198,9 @@ ecma_builtin_typedarray_of (ecma_value_t this_arg, /**< 'this' argument */
JERRY_UNUSED (arguments_list_p);
JERRY_UNUSED (arguments_list_len);
/* TODO: inplement 'of' */
/* TODO: implement 'of' */
return ecma_make_simple_value (ECMA_SIMPLE_VALUE_EMPTY);
return ecma_make_simple_value (ECMA_SIMPLE_VALUE_UNDEFINED);
} /* ecma_builtin_typedarray_of */
/**