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 3083 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3094 | 3094 |
3095 // 2. If Type(buffer) is not Object, throw a TypeError exception. | 3095 // 2. If Type(buffer) is not Object, throw a TypeError exception. |
3096 // 3. If buffer does not have an [[ArrayBufferData]] internal slot, throw a | 3096 // 3. If buffer does not have an [[ArrayBufferData]] internal slot, throw a |
3097 // TypeError exception. | 3097 // TypeError exception. |
3098 if (!buffer->IsJSArrayBuffer()) { | 3098 if (!buffer->IsJSArrayBuffer()) { |
3099 THROW_NEW_ERROR_RETURN_FAILURE( | 3099 THROW_NEW_ERROR_RETURN_FAILURE( |
3100 isolate, NewTypeError(MessageTemplate::kDataViewNotArrayBuffer)); | 3100 isolate, NewTypeError(MessageTemplate::kDataViewNotArrayBuffer)); |
3101 } | 3101 } |
3102 Handle<JSArrayBuffer> array_buffer = Handle<JSArrayBuffer>::cast(buffer); | 3102 Handle<JSArrayBuffer> array_buffer = Handle<JSArrayBuffer>::cast(buffer); |
3103 | 3103 |
3104 // 4. Let numberOffset be ? ToNumber(byteOffset). | 3104 // 4. Let offset be ToIndex(byteOffset). |
3105 Handle<Object> number_offset; | 3105 Handle<Object> offset; |
3106 if (byte_offset->IsUndefined(isolate)) { | 3106 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
3107 // We intentionally violate the specification at this point to allow | 3107 isolate, offset, |
3108 // for new DataView(buffer) invocations to be equivalent to the full | 3108 Object::ToIndex(isolate, byte_offset, |
3109 // new DataView(buffer, 0) invocation. | 3109 MessageTemplate::kInvalidDataViewOffset)); |
3110 number_offset = handle(Smi::FromInt(0), isolate); | |
3111 } else { | |
3112 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, number_offset, | |
3113 Object::ToNumber(byte_offset)); | |
3114 } | |
3115 | 3110 |
3116 // 5. Let offset be ToInteger(numberOffset). | 3111 // 5. If IsDetachedBuffer(buffer) is true, throw a TypeError exception. |
3117 Handle<Object> offset; | |
3118 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, offset, | |
3119 Object::ToInteger(isolate, number_offset)); | |
3120 | |
3121 // 6. If numberOffset ≠ offset or offset < 0, throw a RangeError exception. | |
3122 if (number_offset->Number() != offset->Number() || offset->Number() < 0.0) { | |
3123 THROW_NEW_ERROR_RETURN_FAILURE( | |
3124 isolate, NewRangeError(MessageTemplate::kInvalidDataViewOffset)); | |
3125 } | |
3126 | |
3127 // 7. If IsDetachedBuffer(buffer) is true, throw a TypeError exception. | |
3128 // We currently violate the specification at this point. | 3112 // We currently violate the specification at this point. |
3129 | 3113 |
3130 // 8. Let bufferByteLength be the value of buffer's [[ArrayBufferByteLength]] | 3114 // 6. Let bufferByteLength be the value of buffer's [[ArrayBufferByteLength]] |
3131 // internal slot. | 3115 // internal slot. |
3132 double const buffer_byte_length = array_buffer->byte_length()->Number(); | 3116 double const buffer_byte_length = array_buffer->byte_length()->Number(); |
3133 | 3117 |
3134 // 9. If offset > bufferByteLength, throw a RangeError exception | 3118 // 7. If offset > bufferByteLength, throw a RangeError exception |
3135 if (offset->Number() > buffer_byte_length) { | 3119 if (offset->Number() > buffer_byte_length) { |
3136 THROW_NEW_ERROR_RETURN_FAILURE( | 3120 THROW_NEW_ERROR_RETURN_FAILURE( |
3137 isolate, NewRangeError(MessageTemplate::kInvalidDataViewOffset)); | 3121 isolate, |
| 3122 NewRangeError(MessageTemplate::kInvalidDataViewOffset, offset)); |
3138 } | 3123 } |
3139 | 3124 |
3140 Handle<Object> view_byte_length; | 3125 Handle<Object> view_byte_length; |
3141 if (byte_length->IsUndefined(isolate)) { | 3126 if (byte_length->IsUndefined(isolate)) { |
3142 // 10. If byteLength is undefined, then | 3127 // 8. If byteLength is undefined, then |
3143 // a. Let viewByteLength be bufferByteLength - offset. | 3128 // a. Let viewByteLength be bufferByteLength - offset. |
3144 view_byte_length = | 3129 view_byte_length = |
3145 isolate->factory()->NewNumber(buffer_byte_length - offset->Number()); | 3130 isolate->factory()->NewNumber(buffer_byte_length - offset->Number()); |
3146 } else { | 3131 } else { |
3147 // 11. Else, | 3132 // 9. Else, |
3148 // a. Let viewByteLength be ? ToLength(byteLength). | 3133 // a. Let viewByteLength be ? ToIndex(byteLength). |
3149 // b. If offset+viewByteLength > bufferByteLength, throw a RangeError | 3134 // b. If offset+viewByteLength > bufferByteLength, throw a RangeError |
3150 // exception | 3135 // exception |
3151 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( | 3136 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
3152 isolate, view_byte_length, Object::ToLength(isolate, byte_length)); | 3137 isolate, view_byte_length, |
| 3138 Object::ToIndex(isolate, byte_length, |
| 3139 MessageTemplate::kInvalidDataViewLength)); |
3153 if (offset->Number() + view_byte_length->Number() > buffer_byte_length) { | 3140 if (offset->Number() + view_byte_length->Number() > buffer_byte_length) { |
3154 THROW_NEW_ERROR_RETURN_FAILURE( | 3141 THROW_NEW_ERROR_RETURN_FAILURE( |
3155 isolate, NewRangeError(MessageTemplate::kInvalidDataViewLength)); | 3142 isolate, NewRangeError(MessageTemplate::kInvalidDataViewLength)); |
3156 } | 3143 } |
3157 } | 3144 } |
3158 | 3145 |
3159 // 12. Let O be ? OrdinaryCreateFromConstructor(NewTarget, | 3146 // 10. Let O be ? OrdinaryCreateFromConstructor(NewTarget, |
3160 // "%DataViewPrototype%", «[[DataView]], [[ViewedArrayBuffer]], | 3147 // "%DataViewPrototype%", «[[DataView]], [[ViewedArrayBuffer]], |
3161 // [[ByteLength]], [[ByteOffset]]»). | 3148 // [[ByteLength]], [[ByteOffset]]»). |
3162 // 13. Set O's [[DataView]] internal slot to true. | 3149 // 11. Set O's [[DataView]] internal slot to true. |
3163 Handle<JSObject> result; | 3150 Handle<JSObject> result; |
3164 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, | 3151 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, |
3165 JSObject::New(target, new_target)); | 3152 JSObject::New(target, new_target)); |
3166 for (int i = 0; i < ArrayBufferView::kInternalFieldCount; ++i) { | 3153 for (int i = 0; i < ArrayBufferView::kInternalFieldCount; ++i) { |
3167 Handle<JSDataView>::cast(result)->SetInternalField(i, Smi::FromInt(0)); | 3154 Handle<JSDataView>::cast(result)->SetInternalField(i, Smi::FromInt(0)); |
3168 } | 3155 } |
3169 | 3156 |
3170 // 14. Set O's [[ViewedArrayBuffer]] internal slot to buffer. | 3157 // 12. Set O's [[ViewedArrayBuffer]] internal slot to buffer. |
3171 Handle<JSDataView>::cast(result)->set_buffer(*array_buffer); | 3158 Handle<JSDataView>::cast(result)->set_buffer(*array_buffer); |
3172 | 3159 |
3173 // 15. Set O's [[ByteLength]] internal slot to viewByteLength. | 3160 // 13. Set O's [[ByteLength]] internal slot to viewByteLength. |
3174 Handle<JSDataView>::cast(result)->set_byte_length(*view_byte_length); | 3161 Handle<JSDataView>::cast(result)->set_byte_length(*view_byte_length); |
3175 | 3162 |
3176 // 16. Set O's [[ByteOffset]] internal slot to offset. | 3163 // 14. Set O's [[ByteOffset]] internal slot to offset. |
3177 Handle<JSDataView>::cast(result)->set_byte_offset(*offset); | 3164 Handle<JSDataView>::cast(result)->set_byte_offset(*offset); |
3178 | 3165 |
3179 // 17. Return O. | 3166 // 15. Return O. |
3180 return *result; | 3167 return *result; |
3181 } | 3168 } |
3182 | 3169 |
3183 // ES6 section 24.2.4.1 get DataView.prototype.buffer | 3170 // ES6 section 24.2.4.1 get DataView.prototype.buffer |
3184 BUILTIN(DataViewPrototypeGetBuffer) { | 3171 BUILTIN(DataViewPrototypeGetBuffer) { |
3185 HandleScope scope(isolate); | 3172 HandleScope scope(isolate); |
3186 CHECK_RECEIVER(JSDataView, data_view, "get DataView.prototype.buffer"); | 3173 CHECK_RECEIVER(JSDataView, data_view, "get DataView.prototype.buffer"); |
3187 return data_view->buffer(); | 3174 return data_view->buffer(); |
3188 } | 3175 } |
3189 | 3176 |
(...skipping 3060 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6250 BUILTIN_LIST_H(DEFINE_BUILTIN_ACCESSOR_H) | 6237 BUILTIN_LIST_H(DEFINE_BUILTIN_ACCESSOR_H) |
6251 BUILTIN_LIST_DEBUG_A(DEFINE_BUILTIN_ACCESSOR_A) | 6238 BUILTIN_LIST_DEBUG_A(DEFINE_BUILTIN_ACCESSOR_A) |
6252 #undef DEFINE_BUILTIN_ACCESSOR_C | 6239 #undef DEFINE_BUILTIN_ACCESSOR_C |
6253 #undef DEFINE_BUILTIN_ACCESSOR_A | 6240 #undef DEFINE_BUILTIN_ACCESSOR_A |
6254 #undef DEFINE_BUILTIN_ACCESSOR_T | 6241 #undef DEFINE_BUILTIN_ACCESSOR_T |
6255 #undef DEFINE_BUILTIN_ACCESSOR_S | 6242 #undef DEFINE_BUILTIN_ACCESSOR_S |
6256 #undef DEFINE_BUILTIN_ACCESSOR_H | 6243 #undef DEFINE_BUILTIN_ACCESSOR_H |
6257 | 6244 |
6258 } // namespace internal | 6245 } // namespace internal |
6259 } // namespace v8 | 6246 } // namespace v8 |
OLD | NEW |