Add typedarray routine: every, some, foreach, and map (#1566)

JerryScript-DCO-1.0-Signed-off-by: Zidong Jiang zidong.jiang@intel.com
This commit is contained in:
Zidong Jiang 2017-02-09 15:49:00 +08:00 committed by László Langó
parent a976b0c061
commit 7b01b29ec8
12 changed files with 560 additions and 2 deletions

View File

@ -19,6 +19,8 @@
#include "ecma-globals.h"
#include "ecma-helpers.h"
#include "ecma-objects.h"
#include "ecma-conversion.h"
#include "ecma-function-object.h"
#include "ecma-typedarray-object.h"
#include "ecma-try-catch-macro.h"
#include "jrt.h"
@ -135,6 +137,223 @@ ecma_builtin_typedarray_prototype_length_getter (ecma_value_t this_arg) /**< thi
return ecma_raise_type_error (ECMA_ERR_MSG ("Argument 'this' is not a TypedArray."));
} /* ecma_builtin_typedarray_prototype_length_getter */
/**
* Type of routine.
*/
typedef enum
{
TYPEDARRAY_ROUTINE_EVERY, /**< routine: every ES2015, 22.2.3.7 */
TYPEDARRAY_ROUTINE_SOME, /**< routine: some ES2015, 22.2.3.9 */
TYPEDARRAY_ROUTINE_FOREACH, /**< routine: forEach ES2015, 15.4.4.18 */
TYPEDARRAY_ROUTINE__COUNT /**< count of the modes */
} typedarray_routine_mode;
/**
* The common function for 'every', 'some' and 'forEach'
* because they have a similar structure.
*
* @return ecma value
* Returned value must be freed with ecma_free_value.
*/
static ecma_value_t
ecma_builtin_typedarray_prototype_exec_routine (ecma_value_t this_arg, /**< this argument */
ecma_value_t cb_func_val, /**< callback function */
ecma_value_t cb_this_arg, /**< 'this' of the callback function */
typedarray_routine_mode mode) /**< mode: which routine */
{
JERRY_ASSERT (mode < TYPEDARRAY_ROUTINE__COUNT);
if (!ecma_is_typedarray (this_arg))
{
return ecma_raise_type_error (ECMA_ERR_MSG ("Argument 'this' is not a TypedArray."));
}
if (!ecma_op_is_callable (cb_func_val))
{
return ecma_raise_type_error (ECMA_ERR_MSG ("Callback function is not callable."));
}
ecma_object_t *obj_p = ecma_get_object_from_value (this_arg);
uint32_t len = ecma_typedarray_get_length (obj_p);
ecma_object_t *func_object_p = ecma_get_object_from_value (cb_func_val);
ecma_value_t ret_value = ecma_make_simple_value (ECMA_SIMPLE_VALUE_EMPTY);
for (uint32_t index = 0; index < len && ecma_is_value_empty (ret_value); index++)
{
ecma_value_t current_index = ecma_make_uint32_value (index);
ecma_value_t get_value = ecma_op_typedarray_get_index_prop (obj_p, index);
JERRY_ASSERT (ecma_is_value_number (get_value));
ecma_value_t call_args[] = { get_value, current_index, this_arg };
ECMA_TRY_CATCH (call_value, ecma_op_function_call (func_object_p, cb_this_arg, call_args, 3), ret_value);
if (mode == TYPEDARRAY_ROUTINE_EVERY)
{
if (!ecma_op_to_boolean (call_value))
{
ret_value = ecma_make_simple_value (ECMA_SIMPLE_VALUE_FALSE);
}
}
else if (mode == TYPEDARRAY_ROUTINE_SOME)
{
if (ecma_op_to_boolean (call_value))
{
ret_value = ecma_make_simple_value (ECMA_SIMPLE_VALUE_TRUE);
}
}
ECMA_FINALIZE (call_value);
ecma_fast_free_value (current_index);
ecma_fast_free_value (get_value);
}
if (ecma_is_value_empty (ret_value))
{
if (mode == TYPEDARRAY_ROUTINE_EVERY)
{
ret_value = ecma_make_simple_value (ECMA_SIMPLE_VALUE_TRUE);
}
else if (mode == TYPEDARRAY_ROUTINE_SOME)
{
ret_value = ecma_make_simple_value (ECMA_SIMPLE_VALUE_FALSE);
}
else
{
ret_value = ecma_make_simple_value (ECMA_SIMPLE_VALUE_UNDEFINED);
}
}
return ret_value;
} /* ecma_builtin_typedarray_prototype_exec_routine */
/**
* The %TypedArray%.prototype object's 'every' routine
*
* See also:
* ES2015, 22.2.3.7
*
* @return ecma value
* Returned value must be freed with ecma_free_value.
*/
static ecma_value_t
ecma_builtin_typedarray_prototype_every (ecma_value_t this_arg, /**< this argument */
ecma_value_t cb_func_val, /**< callback function */
ecma_value_t cb_this_arg) /**< this' of the callback function */
{
return ecma_builtin_typedarray_prototype_exec_routine (this_arg,
cb_func_val,
cb_this_arg,
TYPEDARRAY_ROUTINE_EVERY);
} /* ecma_builtin_typedarray_prototype_every */
/**
* The %TypedArray%.prototype object's 'some' routine
*
* See also:
* ES2015, 22.2.3.9
*
* @return ecma value
* Returned value must be freed with ecma_free_value.
*/
static ecma_value_t
ecma_builtin_typedarray_prototype_some (ecma_value_t this_arg, /**< this argument */
ecma_value_t cb_func_val, /**< callback function */
ecma_value_t cb_this_arg) /**< this' of the callback function */
{
return ecma_builtin_typedarray_prototype_exec_routine (this_arg,
cb_func_val,
cb_this_arg,
TYPEDARRAY_ROUTINE_SOME);
} /* ecma_builtin_typedarray_prototype_some */
/**
* The %TypedArray%.prototype object's 'forEach' routine
*
* See also:
* ES2015, 15.4.4.18
*
* @return ecma value
* Returned value must be freed with ecma_free_value.
*/
static ecma_value_t
ecma_builtin_typedarray_prototype_for_each (ecma_value_t this_arg, /**< this argument */
ecma_value_t cb_func_val, /**< callback function */
ecma_value_t cb_this_arg) /**< this' of the callback function */
{
return ecma_builtin_typedarray_prototype_exec_routine (this_arg,
cb_func_val,
cb_this_arg,
TYPEDARRAY_ROUTINE_FOREACH);
} /* ecma_builtin_typedarray_prototype_for_each */
/**
* The %TypedArray%.prototype object's 'map' routine
*
* See also:
* ES2015, 22.2.3.8
*
* @return ecma value
* Returned value must be freed with ecma_free_value.
*/
static ecma_value_t
ecma_builtin_typedarray_prototype_map (ecma_value_t this_arg, /**< this argument */
ecma_value_t cb_func_val, /**< callback function */
ecma_value_t cb_this_arg) /**< this' of the callback function */
{
if (!ecma_is_typedarray (this_arg))
{
return ecma_raise_type_error (ECMA_ERR_MSG ("Argument 'this' is not a TypedArray."));
}
if (!ecma_op_is_callable (cb_func_val))
{
return ecma_raise_type_error (ECMA_ERR_MSG ("Callback function is not callable."));
}
ecma_object_t *obj_p = ecma_get_object_from_value (this_arg);
uint32_t len = ecma_typedarray_get_length (obj_p);
ecma_object_t *func_object_p = ecma_get_object_from_value (cb_func_val);
ecma_value_t ret_value = ecma_make_simple_value (ECMA_SIMPLE_VALUE_EMPTY);
ecma_value_t new_typedarray = ecma_op_create_typedarray_with_type_and_length (obj_p, len);
ecma_object_t *new_obj_p = ecma_get_object_from_value (new_typedarray);
for (uint32_t index = 0; index < len && ecma_is_value_empty (ret_value); index++)
{
ecma_value_t current_index = ecma_make_uint32_value (index);
ecma_value_t get_value = ecma_op_typedarray_get_index_prop (obj_p, index);
ecma_value_t call_args[] = { get_value, current_index, this_arg };
ECMA_TRY_CATCH (mapped_value, ecma_op_function_call (func_object_p, cb_this_arg, call_args, 3), ret_value);
ecma_value_t set_status = ecma_op_typedarray_set_index_prop (new_obj_p, index, mapped_value);
if (!set_status)
{
ret_value = ecma_raise_type_error (ECMA_ERR_MSG ("error in typedarray set"));
}
ECMA_FINALIZE (mapped_value);
ecma_fast_free_value (current_index);
ecma_fast_free_value (get_value);
}
if (ecma_is_value_empty (ret_value))
{
ret_value = new_typedarray;
}
else
{
ecma_free_value (new_typedarray);
}
return ret_value;
} /* ecma_builtin_typedarray_prototype_map */
/**
* @}
* @}

View File

@ -55,6 +55,12 @@ ACCESSOR_READ_ONLY (LIT_MAGIC_STRING_BYTE_OFFSET_UL,
ACCESSOR_READ_ONLY (LIT_MAGIC_STRING_LENGTH,
ecma_builtin_typedarray_prototype_length_getter,
ECMA_PROPERTY_FIXED)
ROUTINE (LIT_MAGIC_STRING_EVERY, ecma_builtin_typedarray_prototype_every, 2, 1)
ROUTINE (LIT_MAGIC_STRING_SOME, ecma_builtin_typedarray_prototype_some, 2, 1)
ROUTINE (LIT_MAGIC_STRING_FOR_EACH_UL, ecma_builtin_typedarray_prototype_for_each, 2, 1)
ROUTINE (LIT_MAGIC_STRING_MAP, ecma_builtin_typedarray_prototype_map, 2, 1)
#undef SIMPLE_VALUE
#undef NUMBER_VALUE
#undef STRING_VALUE

View File

@ -808,7 +808,7 @@ ecma_op_typedarray_define_index_prop (ecma_object_t *obj_p, /**< a TypedArray ob
* @return boolean, false if failed
*/
bool
ecma_op_typedarray_set_index_prop (ecma_object_t *obj_p,/**< a TypedArray object */
ecma_op_typedarray_set_index_prop (ecma_object_t *obj_p, /**< a TypedArray object */
uint32_t index, /**< the index number */
ecma_value_t value) /**< value of the property */
{
@ -843,6 +843,98 @@ ecma_op_typedarray_set_index_prop (ecma_object_t *obj_p,/**< a TypedArray object
return false;
} /* ecma_op_typedarray_set_index_prop */
/**
* Create a typedarray object based on the "type" and arraylength
* The "type" is same with arg1
*
* @return ecma_value_t
*/
ecma_value_t
ecma_op_create_typedarray_with_type_and_length (ecma_object_t *obj_p, /**< TypedArray object
* indicates the type */
ecma_length_t array_length) /**< length of the typedarray */
{
JERRY_ASSERT (ecma_is_typedarray (ecma_make_object_value (obj_p)));
ecma_extended_object_t *ext_object_p = (ecma_extended_object_t *) obj_p;
lit_magic_string_id_t class_id = ext_object_p->u.pseudo_array.u1.class_id;
ecma_object_t *proto_p;
uint8_t element_size_shift = 0;
switch (class_id)
{
case LIT_MAGIC_STRING_INT8_ARRAY_UL:
{
proto_p = ecma_builtin_get (ECMA_BUILTIN_ID_INT8ARRAY_PROTOTYPE);
element_size_shift = 0;
break;
}
case LIT_MAGIC_STRING_UINT8_ARRAY_UL:
{
proto_p = ecma_builtin_get (ECMA_BUILTIN_ID_UINT8ARRAY_PROTOTYPE);
element_size_shift = 0;
break;
}
case LIT_MAGIC_STRING_UINT8_CLAMPED_ARRAY_UL:
{
proto_p = ecma_builtin_get (ECMA_BUILTIN_ID_UINT8CLAMPEDARRAY_PROTOTYPE);
element_size_shift = 0;
break;
}
case LIT_MAGIC_STRING_INT16_ARRAY_UL:
{
proto_p = ecma_builtin_get (ECMA_BUILTIN_ID_INT16ARRAY_PROTOTYPE);
element_size_shift = 1;
break;
}
case LIT_MAGIC_STRING_UINT16_ARRAY_UL:
{
proto_p = ecma_builtin_get (ECMA_BUILTIN_ID_UINT16ARRAY_PROTOTYPE);
element_size_shift = 1;
break;
}
case LIT_MAGIC_STRING_INT32_ARRAY_UL:
{
proto_p = ecma_builtin_get (ECMA_BUILTIN_ID_INT32ARRAY_PROTOTYPE);
element_size_shift = 2;
break;
}
case LIT_MAGIC_STRING_UINT32_ARRAY_UL:
{
proto_p = ecma_builtin_get (ECMA_BUILTIN_ID_UINT32ARRAY_PROTOTYPE);
element_size_shift = 2;
break;
}
case LIT_MAGIC_STRING_FLOAT32_ARRAY_UL:
{
proto_p = ecma_builtin_get (ECMA_BUILTIN_ID_FLOAT32ARRAY_PROTOTYPE);
element_size_shift = 2;
break;
}
#if CONFIG_ECMA_NUMBER_TYPE == CONFIG_ECMA_NUMBER_FLOAT64
case LIT_MAGIC_STRING_FLOAT64_ARRAY_UL:
{
proto_p = ecma_builtin_get (ECMA_BUILTIN_ID_FLOAT64ARRAY_PROTOTYPE);
element_size_shift = 3;
break;
}
#endif /* CONFIG_ECMA_NUMBER_TYPE == CONFIG_ECMA_NUMBER_FLOAT64 */
default:
{
JERRY_UNREACHABLE ();
}
}
ecma_object_t *new_obj_p = ecma_typedarray_create_object_with_length (array_length,
proto_p,
element_size_shift,
class_id);
ecma_deref_object (proto_p);
return ecma_make_object_value (new_obj_p);
} /* ecma_op_create_typedarray_with_type_and_length */
/**
* @}
* @}

View File

@ -49,7 +49,8 @@ bool ecma_op_typedarray_define_index_prop (ecma_object_t *obj_p,
uint32_t index,
const ecma_property_descriptor_t *property_desc_p);
bool ecma_op_typedarray_set_index_prop (ecma_object_t *obj_p, uint32_t index, ecma_value_t value);
ecma_value_t ecma_op_create_typedarray_with_type_and_length (ecma_object_t *obj_p,
ecma_length_t array_length);
/**
* @}

View File

@ -0,0 +1,37 @@
/* 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.
*/
var a = new Uint8Array([10, 20, 30, 40]);
var o = {
"small":0,
"large":0
};
var func = function(v, k)
{
if (v < 25)
{
this.small = this.small + k;
}
else
{
this.large = this.large + k;
}
}
var ret = a.forEach(func, o);
assert(ret === undefined);
assert(o.small === 1); // 0+1
assert(o.large === 5); // 2+3

View File

@ -0,0 +1,27 @@
/* 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.
*/
var a = new Uint8Array([1, 2, 3]);
var b = a.map(function(num) {
return num * 2;
});
assert(a[0] === 1);
assert(a[1] === 2);
assert(a[2] === 3);
assert(b[0] === 2);
assert(b[1] === 4);
assert(b[2] === 6);

View File

@ -0,0 +1,28 @@
/* 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.
*/
var a = new Uint8Array([1, 2, 3, 4]);
var count = 0;
function f_every(num)
{
count++;
return num < 3;
}
var ret = a.every(f_every);
assert(ret === false);
assert(count === 3);

View File

@ -0,0 +1,28 @@
/* 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.
*/
var a = new Uint8Array([1, 2, 3, 4]);
var count = 0;
function f_some(num)
{
count++;
return num > 3;
}
var ret = a.some(f_some);
assert(ret === true);
assert(count === 4);

View File

@ -0,0 +1,27 @@
/* 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.
*/
var a = new Float32Array([1.25, 2.5, 3.75]);
var b = a.map(function(num) {
return num * 2;
});
assert(a[0] === 1.25);
assert(a[1] === 2.5);
assert(a[2] === 3.75);
assert(b[0] === 2.5);
assert(b[1] === 5);
assert(b[2] === 7.5);

View File

@ -0,0 +1,28 @@
/* 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.
*/
var a = new Float32Array([1.1, 2.2, 3.3, 4.4]);
var count = 0;
function f_every(num)
{
count++;
return num < 3;
}
var ret = a.every(f_every);
assert(ret === false);
assert(count === 3);

View File

@ -0,0 +1,28 @@
/* 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.
*/
var a = new Float32Array([1.1, 2.2, 3.3, 4.4]);
var count = 0;
function f_some(num)
{
count++;
return num > 3;
}
var ret = a.some(f_some);
assert(ret === true);
assert(count === 3);

View File

@ -0,0 +1,37 @@
/* 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.
*/
var a = new Float32Array([-1.1, 0.1, 2.5, 3.0]);
var o = {
"small":0,
"large":0
};
var func = function(v, k)
{
if (v < 2)
{
this.small = this.small + k;
}
else
{
this.large = this.large + k;
}
}
var ret = a.forEach(func, o);
assert(ret === undefined);
assert(o.small === 1); // 0+1
assert(o.large === 5); // 2+3