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