|
| 1 | +/* Copyright JS Foundation and other contributors, http://js.foundation |
| 2 | + * |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +#include "ecma-builtin-helpers.h" |
| 17 | +#include "ecma-builtins.h" |
| 18 | +#include "ecma-exceptions.h" |
| 19 | +#include "ecma-globals.h" |
| 20 | +#include "ecma-helpers.h" |
| 21 | +#include "ecma-objects.h" |
| 22 | +#include "ecma-arraybuffer-object.h" |
| 23 | +#include "ecma-try-catch-macro.h" |
| 24 | +#include "jrt.h" |
| 25 | +#include "jrt-libc-includes.h" |
| 26 | + |
| 27 | +#ifndef CONFIG_DISABLE_ARRAYBUFFER_BUILTIN |
| 28 | + |
| 29 | +#define ECMA_BUILTINS_INTERNAL |
| 30 | +#include "ecma-builtins-internal.h" |
| 31 | + |
| 32 | +#define BUILTIN_INC_HEADER_NAME "ecma-builtin-arraybuffer-prototype.inc.h" |
| 33 | +#define BUILTIN_UNDERSCORED_ID arraybuffer_prototype |
| 34 | +#include "ecma-builtin-internal-routines-template.inc.h" |
| 35 | + |
| 36 | +/** \addtogroup ecma ECMA |
| 37 | + * @{ |
| 38 | + * |
| 39 | + * \addtogroup ecmabuiltins |
| 40 | + * @{ |
| 41 | + * |
| 42 | + * \addtogroup arraybuffer prototype ECMA ArrayBuffer.prototype object built-in |
| 43 | + * @{ |
| 44 | + */ |
| 45 | + |
| 46 | +/** |
| 47 | + * The ArrayBuffer.prototype.bytelength accessor |
| 48 | + * |
| 49 | + * See also: |
| 50 | + * ES2015, 24.1.4.1 |
| 51 | + * |
| 52 | + * @return ecma value |
| 53 | + * Returned value must be freed with ecma_free_value. |
| 54 | + */ |
| 55 | +static ecma_value_t |
| 56 | +ecma_builtin_arraybuffer_prototype_bytelength_getter (ecma_value_t this_arg) /**< this argument */ |
| 57 | +{ |
| 58 | + if (ecma_is_value_object (this_arg)) |
| 59 | + { |
| 60 | + ecma_object_t *object_p = ecma_get_object_from_value (this_arg); |
| 61 | + |
| 62 | + if (ecma_object_class_is (object_p, LIT_MAGIC_STRING_ARRAY_BUFFER_UL)) |
| 63 | + { |
| 64 | + ecma_length_t len = ecma_arraybuffer_get_length (object_p); |
| 65 | + |
| 66 | + return ecma_make_uint32_value (len); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + return ecma_raise_type_error (ECMA_ERR_MSG ("Argument 'this' is not a ArrayBuffer object.")); |
| 71 | +} /* ecma_builtin_arraybuffer_prototype_bytelength_getter */ |
| 72 | + |
| 73 | +/** |
| 74 | + * The ArrayBuffer.prototype object's 'slice' routine |
| 75 | + * |
| 76 | + * See also: |
| 77 | + * ES2015, 24.1.4.3 |
| 78 | + * |
| 79 | + * @return ecma value |
| 80 | + * Returned value must be freed with ecma_free_value. |
| 81 | + */ |
| 82 | +static ecma_value_t |
| 83 | +ecma_builtin_arraybuffer_prototype_object_slice (ecma_value_t this_arg, /**< this argument */ |
| 84 | + ecma_value_t arg1, /**< routine's first argument */ |
| 85 | + ecma_value_t arg2) /**< routine's second argument */ |
| 86 | +{ |
| 87 | + if (!ecma_is_value_object (this_arg)) |
| 88 | + { |
| 89 | + return ecma_raise_type_error (ECMA_ERR_MSG ("Argument 'this' is not object.")); |
| 90 | + } |
| 91 | + |
| 92 | + ecma_object_t *object_p = ecma_get_object_from_value (this_arg); |
| 93 | + |
| 94 | + if (!ecma_object_class_is (object_p, LIT_MAGIC_STRING_ARRAY_BUFFER_UL)) |
| 95 | + { |
| 96 | + return ecma_raise_type_error (ECMA_ERR_MSG ("Argument 'this' is not an ArrayBuffer object.")); |
| 97 | + } |
| 98 | + |
| 99 | + ecma_length_t len = ecma_arraybuffer_get_length (object_p); |
| 100 | + |
| 101 | + ecma_length_t start = 0, end = len; |
| 102 | + |
| 103 | + ecma_value_t ret_value = ecma_make_simple_value (ECMA_SIMPLE_VALUE_EMPTY); |
| 104 | + |
| 105 | + ECMA_OP_TO_NUMBER_TRY_CATCH (start_num, |
| 106 | + arg1, |
| 107 | + ret_value); |
| 108 | + |
| 109 | + start = ecma_builtin_helper_array_index_normalize (start_num, len); |
| 110 | + |
| 111 | + if (!ecma_is_value_undefined (arg2)) |
| 112 | + { |
| 113 | + ECMA_OP_TO_NUMBER_TRY_CATCH (end_num, |
| 114 | + arg2, |
| 115 | + ret_value); |
| 116 | + |
| 117 | + end = ecma_builtin_helper_array_index_normalize (end_num, len); |
| 118 | + |
| 119 | + ECMA_OP_TO_NUMBER_FINALIZE (end_num); |
| 120 | + } |
| 121 | + |
| 122 | + ECMA_OP_TO_NUMBER_FINALIZE (start_num); |
| 123 | + |
| 124 | + JERRY_ASSERT (start <= len && end <= len); |
| 125 | + ecma_length_t new_len = (end >= start) ? (end - start) : 0; |
| 126 | + ecma_object_t *new_arraybuffer_p = ecma_arraybuffer_new_object (new_len); |
| 127 | + lit_utf8_byte_t *old_buf = ecma_arraybuffer_get_buffer (object_p); |
| 128 | + lit_utf8_byte_t *new_buf = ecma_arraybuffer_get_buffer (new_arraybuffer_p); |
| 129 | + |
| 130 | + memcpy (new_buf, old_buf + start, new_len); |
| 131 | + |
| 132 | + return ecma_make_object_value (new_arraybuffer_p); |
| 133 | +} /* ecma_builtin_arraybuffer_prototype_object_slice */ |
| 134 | + |
| 135 | +/** |
| 136 | + * @} |
| 137 | + * @} |
| 138 | + * @} |
| 139 | + */ |
| 140 | + |
| 141 | +#endif /* !CONFIG_DISABLE_ARRAYBUFFER_BUILTIN */ |
0 commit comments