OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/builtins.h" | 5 #include "src/builtins.h" |
6 | 6 |
7 #include "src/api-arguments.h" | 7 #include "src/api-arguments.h" |
8 #include "src/api-natives.h" | 8 #include "src/api-natives.h" |
9 #include "src/api.h" | 9 #include "src/api.h" |
10 #include "src/base/ieee754.h" | 10 #include "src/base/ieee754.h" |
(...skipping 3066 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3077 | 3077 |
3078 // 2. If Type(buffer) is not Object, throw a TypeError exception. | 3078 // 2. If Type(buffer) is not Object, throw a TypeError exception. |
3079 // 3. If buffer does not have an [[ArrayBufferData]] internal slot, throw a | 3079 // 3. If buffer does not have an [[ArrayBufferData]] internal slot, throw a |
3080 // TypeError exception. | 3080 // TypeError exception. |
3081 if (!buffer->IsJSArrayBuffer()) { | 3081 if (!buffer->IsJSArrayBuffer()) { |
3082 THROW_NEW_ERROR_RETURN_FAILURE( | 3082 THROW_NEW_ERROR_RETURN_FAILURE( |
3083 isolate, NewTypeError(MessageTemplate::kDataViewNotArrayBuffer)); | 3083 isolate, NewTypeError(MessageTemplate::kDataViewNotArrayBuffer)); |
3084 } | 3084 } |
3085 Handle<JSArrayBuffer> array_buffer = Handle<JSArrayBuffer>::cast(buffer); | 3085 Handle<JSArrayBuffer> array_buffer = Handle<JSArrayBuffer>::cast(buffer); |
3086 | 3086 |
3087 // 4. Let numberOffset be ? ToNumber(byteOffset). | 3087 // 4. Let offset be ToIndex(byteOffset). |
3088 Handle<Object> number_offset; | |
3089 if (byte_offset->IsUndefined(isolate)) { | |
3090 // We intentionally violate the specification at this point to allow | |
3091 // for new DataView(buffer) invocations to be equivalent to the full | |
3092 // new DataView(buffer, 0) invocation. | |
3093 number_offset = handle(Smi::FromInt(0), isolate); | |
3094 } else { | |
3095 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, number_offset, | |
3096 Object::ToNumber(byte_offset)); | |
3097 } | |
3098 | |
3099 // 5. Let offset be ToInteger(numberOffset). | |
3100 Handle<Object> offset; | 3088 Handle<Object> offset; |
3101 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, offset, | 3089 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, offset, |
3102 Object::ToInteger(isolate, number_offset)); | 3090 Object::ToIndex(isolate, byte_offset)); |
3103 | 3091 |
3104 // 6. If numberOffset ≠ offset or offset < 0, throw a RangeError exception. | 3092 // 5. If IsDetachedBuffer(buffer) is true, throw a TypeError exception. |
3105 if (number_offset->Number() != offset->Number() || offset->Number() < 0.0) { | |
3106 THROW_NEW_ERROR_RETURN_FAILURE( | |
3107 isolate, NewRangeError(MessageTemplate::kInvalidDataViewOffset)); | |
3108 } | |
3109 | |
3110 // 7. If IsDetachedBuffer(buffer) is true, throw a TypeError exception. | |
3111 // We currently violate the specification at this point. | 3093 // We currently violate the specification at this point. |
3112 | 3094 |
3113 // 8. Let bufferByteLength be the value of buffer's [[ArrayBufferByteLength]] | 3095 // 6. Let bufferByteLength be the value of buffer's [[ArrayBufferByteLength]] |
3114 // internal slot. | 3096 // internal slot. |
3115 double const buffer_byte_length = array_buffer->byte_length()->Number(); | 3097 double const buffer_byte_length = array_buffer->byte_length()->Number(); |
3116 | 3098 |
3117 // 9. If offset > bufferByteLength, throw a RangeError exception | 3099 // 7. If offset > bufferByteLength, throw a RangeError exception |
3118 if (offset->Number() > buffer_byte_length) { | 3100 if (offset->Number() > buffer_byte_length) { |
3119 THROW_NEW_ERROR_RETURN_FAILURE( | 3101 THROW_NEW_ERROR_RETURN_FAILURE( |
3120 isolate, NewRangeError(MessageTemplate::kInvalidDataViewOffset)); | 3102 isolate, NewRangeError(MessageTemplate::kInvalidDataViewOffset)); |
3121 } | 3103 } |
3122 | 3104 |
3123 Handle<Object> view_byte_length; | 3105 Handle<Object> view_byte_length; |
3124 if (byte_length->IsUndefined(isolate)) { | 3106 if (byte_length->IsUndefined(isolate)) { |
3125 // 10. If byteLength is undefined, then | 3107 // 8. If byteLength is undefined, then |
3126 // a. Let viewByteLength be bufferByteLength - offset. | 3108 // a. Let viewByteLength be bufferByteLength - offset. |
3127 view_byte_length = | 3109 view_byte_length = |
3128 isolate->factory()->NewNumber(buffer_byte_length - offset->Number()); | 3110 isolate->factory()->NewNumber(buffer_byte_length - offset->Number()); |
3129 } else { | 3111 } else { |
3130 // 11. Else, | 3112 // 9. Else, |
3131 // a. Let viewByteLength be ? ToLength(byteLength). | 3113 // a. Let viewByteLength be ? ToIndex(byteLength). |
3132 // b. If offset+viewByteLength > bufferByteLength, throw a RangeError | 3114 // b. If offset+viewByteLength > bufferByteLength, throw a RangeError |
3133 // exception | 3115 // exception |
3134 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( | 3116 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, view_byte_length, |
3135 isolate, view_byte_length, Object::ToLength(isolate, byte_length)); | 3117 Object::ToIndex(isolate, byte_length)); |
3136 if (offset->Number() + view_byte_length->Number() > buffer_byte_length) { | 3118 if (offset->Number() + view_byte_length->Number() > buffer_byte_length) { |
3137 THROW_NEW_ERROR_RETURN_FAILURE( | 3119 THROW_NEW_ERROR_RETURN_FAILURE( |
3138 isolate, NewRangeError(MessageTemplate::kInvalidDataViewLength)); | 3120 isolate, NewRangeError(MessageTemplate::kInvalidDataViewLength)); |
3139 } | 3121 } |
3140 } | 3122 } |
3141 | 3123 |
3142 // 12. Let O be ? OrdinaryCreateFromConstructor(NewTarget, | 3124 // 10. Let O be ? OrdinaryCreateFromConstructor(NewTarget, |
3143 // "%DataViewPrototype%", «[[DataView]], [[ViewedArrayBuffer]], | 3125 // "%DataViewPrototype%", «[[DataView]], [[ViewedArrayBuffer]], |
3144 // [[ByteLength]], [[ByteOffset]]»). | 3126 // [[ByteLength]], [[ByteOffset]]»). |
3145 // 13. Set O's [[DataView]] internal slot to true. | 3127 // 11. Set O's [[DataView]] internal slot to true. |
3146 Handle<JSObject> result; | 3128 Handle<JSObject> result; |
3147 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, | 3129 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, |
3148 JSObject::New(target, new_target)); | 3130 JSObject::New(target, new_target)); |
3149 for (int i = 0; i < ArrayBufferView::kInternalFieldCount; ++i) { | 3131 for (int i = 0; i < ArrayBufferView::kInternalFieldCount; ++i) { |
3150 Handle<JSDataView>::cast(result)->SetInternalField(i, Smi::FromInt(0)); | 3132 Handle<JSDataView>::cast(result)->SetInternalField(i, Smi::FromInt(0)); |
3151 } | 3133 } |
3152 | 3134 |
3153 // 14. Set O's [[ViewedArrayBuffer]] internal slot to buffer. | 3135 // 12. Set O's [[ViewedArrayBuffer]] internal slot to buffer. |
3154 Handle<JSDataView>::cast(result)->set_buffer(*array_buffer); | 3136 Handle<JSDataView>::cast(result)->set_buffer(*array_buffer); |
3155 | 3137 |
3156 // 15. Set O's [[ByteLength]] internal slot to viewByteLength. | 3138 // 13. Set O's [[ByteLength]] internal slot to viewByteLength. |
3157 Handle<JSDataView>::cast(result)->set_byte_length(*view_byte_length); | 3139 Handle<JSDataView>::cast(result)->set_byte_length(*view_byte_length); |
3158 | 3140 |
3159 // 16. Set O's [[ByteOffset]] internal slot to offset. | 3141 // 14. Set O's [[ByteOffset]] internal slot to offset. |
3160 Handle<JSDataView>::cast(result)->set_byte_offset(*offset); | 3142 Handle<JSDataView>::cast(result)->set_byte_offset(*offset); |
3161 | 3143 |
3162 // 17. Return O. | 3144 // 15. Return O. |
3163 return *result; | 3145 return *result; |
3164 } | 3146 } |
3165 | 3147 |
3166 // ES6 section 24.2.4.1 get DataView.prototype.buffer | 3148 // ES6 section 24.2.4.1 get DataView.prototype.buffer |
3167 BUILTIN(DataViewPrototypeGetBuffer) { | 3149 BUILTIN(DataViewPrototypeGetBuffer) { |
3168 HandleScope scope(isolate); | 3150 HandleScope scope(isolate); |
3169 CHECK_RECEIVER(JSDataView, data_view, "get DataView.prototype.buffer"); | 3151 CHECK_RECEIVER(JSDataView, data_view, "get DataView.prototype.buffer"); |
3170 return data_view->buffer(); | 3152 return data_view->buffer(); |
3171 } | 3153 } |
3172 | 3154 |
(...skipping 2967 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6140 BUILTIN_LIST_H(DEFINE_BUILTIN_ACCESSOR_H) | 6122 BUILTIN_LIST_H(DEFINE_BUILTIN_ACCESSOR_H) |
6141 BUILTIN_LIST_DEBUG_A(DEFINE_BUILTIN_ACCESSOR_A) | 6123 BUILTIN_LIST_DEBUG_A(DEFINE_BUILTIN_ACCESSOR_A) |
6142 #undef DEFINE_BUILTIN_ACCESSOR_C | 6124 #undef DEFINE_BUILTIN_ACCESSOR_C |
6143 #undef DEFINE_BUILTIN_ACCESSOR_A | 6125 #undef DEFINE_BUILTIN_ACCESSOR_A |
6144 #undef DEFINE_BUILTIN_ACCESSOR_T | 6126 #undef DEFINE_BUILTIN_ACCESSOR_T |
6145 #undef DEFINE_BUILTIN_ACCESSOR_S | 6127 #undef DEFINE_BUILTIN_ACCESSOR_S |
6146 #undef DEFINE_BUILTIN_ACCESSOR_H | 6128 #undef DEFINE_BUILTIN_ACCESSOR_H |
6147 | 6129 |
6148 } // namespace internal | 6130 } // namespace internal |
6149 } // namespace v8 | 6131 } // namespace v8 |
OLD | NEW |