From 4727202c8ee3ef6b722c9e0a1ba593faf1e5414b Mon Sep 17 00:00:00 2001 From: Zidong Jiang Date: Fri, 17 Feb 2017 01:32:13 +0800 Subject: [PATCH] Add typedarray routine:reverse (#1585) JerryScript-DCO-1.0-Signed-off-by: Zidong Jiang zidong.jiang@intel.com --- .../ecma-builtin-typedarray-prototype.c | 43 +++++++++++++++++++ .../ecma-builtin-typedarray-prototype.inc.h | 1 + .../es2015/22/22.02/22.02.03/22.02.03-018.js | 23 ++++++++++ 3 files changed, 67 insertions(+) create mode 100644 tests/jerry-test-suite/es2015/22/22.02/22.02.03/22.02.03-018.js diff --git a/jerry-core/ecma/builtin-objects/typedarray/ecma-builtin-typedarray-prototype.c b/jerry-core/ecma/builtin-objects/typedarray/ecma-builtin-typedarray-prototype.c index 85bf94221..b2e39f648 100644 --- a/jerry-core/ecma/builtin-objects/typedarray/ecma-builtin-typedarray-prototype.c +++ b/jerry-core/ecma/builtin-objects/typedarray/ecma-builtin-typedarray-prototype.c @@ -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 */ + /** * @} * @} diff --git a/jerry-core/ecma/builtin-objects/typedarray/ecma-builtin-typedarray-prototype.inc.h b/jerry-core/ecma/builtin-objects/typedarray/ecma-builtin-typedarray-prototype.inc.h index 9086354af..759c94d70 100644 --- a/jerry-core/ecma/builtin-objects/typedarray/ecma-builtin-typedarray-prototype.inc.h +++ b/jerry-core/ecma/builtin-objects/typedarray/ecma-builtin-typedarray-prototype.inc.h @@ -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 diff --git a/tests/jerry-test-suite/es2015/22/22.02/22.02.03/22.02.03-018.js b/tests/jerry-test-suite/es2015/22/22.02/22.02.03/22.02.03-018.js new file mode 100644 index 000000000..a55d5eab9 --- /dev/null +++ b/tests/jerry-test-suite/es2015/22/22.02/22.02.03/22.02.03-018.js @@ -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);