Add typedarray routine:reverse (#1585)
JerryScript-DCO-1.0-Signed-off-by: Zidong Jiang zidong.jiang@intel.com
This commit is contained in:
committed by
Dániel Bátyai
parent
188dc46fe0
commit
4727202c8e
@@ -22,6 +22,7 @@
|
||||
#include "ecma-conversion.h"
|
||||
#include "ecma-function-object.h"
|
||||
#include "ecma-typedarray-object.h"
|
||||
#include "ecma-arraybuffer-object.h"
|
||||
#include "ecma-try-catch-macro.h"
|
||||
#include "jrt.h"
|
||||
#include "jrt-libc-includes.h"
|
||||
@@ -608,6 +609,48 @@ ecma_builtin_typedarray_prototype_filter (ecma_value_t this_arg, /**< this argum
|
||||
return ret_value;
|
||||
} /* ecma_builtin_typedarray_prototype_filter */
|
||||
|
||||
/**
|
||||
* The %TypedArray%.prototype object's 'reverse' routine
|
||||
*
|
||||
* See also:
|
||||
* ES2015, 22.2.3.21
|
||||
*
|
||||
* @return ecma value
|
||||
* Returned value must be freed with ecma_free_value.
|
||||
*/
|
||||
ecma_value_t
|
||||
ecma_builtin_typedarray_prototype_reverse (ecma_value_t this_arg) /**< this argument */
|
||||
{
|
||||
if (!ecma_is_typedarray (this_arg))
|
||||
{
|
||||
return ecma_raise_type_error (ECMA_ERR_MSG ("Argument 'this' is not a TypedArray."));
|
||||
}
|
||||
|
||||
ecma_object_t *obj_p = ecma_get_object_from_value (this_arg);
|
||||
uint32_t len = ecma_typedarray_get_length (obj_p);
|
||||
ecma_object_t *arraybuffer_p = ecma_typedarray_get_arraybuffer (obj_p);
|
||||
lit_utf8_byte_t *buffer = (ecma_arraybuffer_get_buffer (arraybuffer_p)
|
||||
+ ecma_typedarray_get_offset (obj_p));
|
||||
uint8_t shift = ecma_typedarray_get_element_size_shift (obj_p);
|
||||
uint8_t element_size = (uint8_t) (1 << shift);
|
||||
uint32_t middle = (len / 2) << shift;
|
||||
uint32_t buffer_last = (len << shift) - element_size;
|
||||
|
||||
for (uint32_t lower = 0; lower < middle; lower += element_size)
|
||||
{
|
||||
uint32_t upper = buffer_last - lower;
|
||||
lit_utf8_byte_t *lower_p = buffer + lower;
|
||||
lit_utf8_byte_t *upper_p = buffer + upper;
|
||||
|
||||
lit_utf8_byte_t tmp[8];
|
||||
memcpy (&tmp[0], lower_p, element_size);
|
||||
memcpy (lower_p, upper_p, element_size);
|
||||
memcpy (upper_p, &tmp[0], element_size);
|
||||
}
|
||||
|
||||
return ecma_copy_value (this_arg);
|
||||
} /* ecma_builtin_typedarray_prototype_reverse */
|
||||
|
||||
/**
|
||||
* @}
|
||||
* @}
|
||||
|
||||
@@ -63,6 +63,7 @@ ROUTINE (LIT_MAGIC_STRING_MAP, ecma_builtin_typedarray_prototype_map, 2, 1)
|
||||
ROUTINE (LIT_MAGIC_STRING_REDUCE, ecma_builtin_typedarray_prototype_reduce, 2, 1)
|
||||
ROUTINE (LIT_MAGIC_STRING_REDUCE_RIGHT_UL, ecma_builtin_typedarray_prototype_reduce_right, 2, 1)
|
||||
ROUTINE (LIT_MAGIC_STRING_FILTER, ecma_builtin_typedarray_prototype_filter, 2, 1)
|
||||
ROUTINE (LIT_MAGIC_STRING_REVERSE, ecma_builtin_typedarray_prototype_reverse, 0, 0)
|
||||
|
||||
#undef SIMPLE_VALUE
|
||||
#undef NUMBER_VALUE
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
/* 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.5, 0, 1.5]);
|
||||
var b = a.reverse()
|
||||
|
||||
assert(a === b);
|
||||
assert(a[0] === 1.5);
|
||||
assert(a[1] === 0);
|
||||
assert(a[2] === -1.5);
|
||||
Reference in New Issue
Block a user