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