OLD | NEW |
1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 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/builtins.h" | 5 #include "src/builtins/builtins.h" |
6 #include "src/builtins/builtins-utils.h" | 6 #include "src/builtins/builtins-utils.h" |
7 | 7 |
8 namespace v8 { | 8 namespace v8 { |
9 namespace internal { | 9 namespace internal { |
10 | 10 |
(...skipping 20 matching lines...) Expand all Loading... |
31 | 31 |
32 // 2. If Type(buffer) is not Object, throw a TypeError exception. | 32 // 2. If Type(buffer) is not Object, throw a TypeError exception. |
33 // 3. If buffer does not have an [[ArrayBufferData]] internal slot, throw a | 33 // 3. If buffer does not have an [[ArrayBufferData]] internal slot, throw a |
34 // TypeError exception. | 34 // TypeError exception. |
35 if (!buffer->IsJSArrayBuffer()) { | 35 if (!buffer->IsJSArrayBuffer()) { |
36 THROW_NEW_ERROR_RETURN_FAILURE( | 36 THROW_NEW_ERROR_RETURN_FAILURE( |
37 isolate, NewTypeError(MessageTemplate::kDataViewNotArrayBuffer)); | 37 isolate, NewTypeError(MessageTemplate::kDataViewNotArrayBuffer)); |
38 } | 38 } |
39 Handle<JSArrayBuffer> array_buffer = Handle<JSArrayBuffer>::cast(buffer); | 39 Handle<JSArrayBuffer> array_buffer = Handle<JSArrayBuffer>::cast(buffer); |
40 | 40 |
41 // 4. Let numberOffset be ? ToNumber(byteOffset). | 41 // 4. Let offset be ToIndex(byteOffset). |
42 Handle<Object> number_offset; | 42 Handle<Object> offset; |
43 if (byte_offset->IsUndefined(isolate)) { | 43 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
44 // We intentionally violate the specification at this point to allow | 44 isolate, offset, |
45 // for new DataView(buffer) invocations to be equivalent to the full | 45 Object::ToIndex(isolate, byte_offset, |
46 // new DataView(buffer, 0) invocation. | 46 MessageTemplate::kInvalidDataViewOffset)); |
47 number_offset = handle(Smi::FromInt(0), isolate); | |
48 } else { | |
49 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, number_offset, | |
50 Object::ToNumber(byte_offset)); | |
51 } | |
52 | 47 |
53 // 5. Let offset be ToInteger(numberOffset). | 48 // 5. If IsDetachedBuffer(buffer) is true, throw a TypeError exception. |
54 Handle<Object> offset; | |
55 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, offset, | |
56 Object::ToInteger(isolate, number_offset)); | |
57 | |
58 // 6. If numberOffset ≠ offset or offset < 0, throw a RangeError exception. | |
59 if (number_offset->Number() != offset->Number() || offset->Number() < 0.0) { | |
60 THROW_NEW_ERROR_RETURN_FAILURE( | |
61 isolate, NewRangeError(MessageTemplate::kInvalidDataViewOffset)); | |
62 } | |
63 | |
64 // 7. If IsDetachedBuffer(buffer) is true, throw a TypeError exception. | |
65 // We currently violate the specification at this point. | 49 // We currently violate the specification at this point. |
66 | 50 |
67 // 8. Let bufferByteLength be the value of buffer's [[ArrayBufferByteLength]] | 51 // 6. Let bufferByteLength be the value of buffer's [[ArrayBufferByteLength]] |
68 // internal slot. | 52 // internal slot. |
69 double const buffer_byte_length = array_buffer->byte_length()->Number(); | 53 double const buffer_byte_length = array_buffer->byte_length()->Number(); |
70 | 54 |
71 // 9. If offset > bufferByteLength, throw a RangeError exception | 55 // 7. If offset > bufferByteLength, throw a RangeError exception |
72 if (offset->Number() > buffer_byte_length) { | 56 if (offset->Number() > buffer_byte_length) { |
73 THROW_NEW_ERROR_RETURN_FAILURE( | 57 THROW_NEW_ERROR_RETURN_FAILURE( |
74 isolate, NewRangeError(MessageTemplate::kInvalidDataViewOffset)); | 58 isolate, |
| 59 NewRangeError(MessageTemplate::kInvalidDataViewOffset, offset)); |
75 } | 60 } |
76 | 61 |
77 Handle<Object> view_byte_length; | 62 Handle<Object> view_byte_length; |
78 if (byte_length->IsUndefined(isolate)) { | 63 if (byte_length->IsUndefined(isolate)) { |
79 // 10. If byteLength is undefined, then | 64 // 8. If byteLength is undefined, then |
80 // a. Let viewByteLength be bufferByteLength - offset. | 65 // a. Let viewByteLength be bufferByteLength - offset. |
81 view_byte_length = | 66 view_byte_length = |
82 isolate->factory()->NewNumber(buffer_byte_length - offset->Number()); | 67 isolate->factory()->NewNumber(buffer_byte_length - offset->Number()); |
83 } else { | 68 } else { |
84 // 11. Else, | 69 // 9. Else, |
85 // a. Let viewByteLength be ? ToLength(byteLength). | 70 // a. Let viewByteLength be ? ToIndex(byteLength). |
86 // b. If offset+viewByteLength > bufferByteLength, throw a RangeError | 71 // b. If offset+viewByteLength > bufferByteLength, throw a RangeError |
87 // exception | 72 // exception |
88 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, view_byte_length, | 73 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
89 Object::ToLength(isolate, byte_length)); | 74 isolate, view_byte_length, |
| 75 Object::ToIndex(isolate, byte_length, |
| 76 MessageTemplate::kInvalidDataViewLength)); |
90 if (offset->Number() + view_byte_length->Number() > buffer_byte_length) { | 77 if (offset->Number() + view_byte_length->Number() > buffer_byte_length) { |
91 THROW_NEW_ERROR_RETURN_FAILURE( | 78 THROW_NEW_ERROR_RETURN_FAILURE( |
92 isolate, NewRangeError(MessageTemplate::kInvalidDataViewLength)); | 79 isolate, NewRangeError(MessageTemplate::kInvalidDataViewLength)); |
93 } | 80 } |
94 } | 81 } |
95 | 82 |
96 // 12. Let O be ? OrdinaryCreateFromConstructor(NewTarget, | 83 // 10. Let O be ? OrdinaryCreateFromConstructor(NewTarget, |
97 // "%DataViewPrototype%", «[[DataView]], [[ViewedArrayBuffer]], | 84 // "%DataViewPrototype%", «[[DataView]], [[ViewedArrayBuffer]], |
98 // [[ByteLength]], [[ByteOffset]]»). | 85 // [[ByteLength]], [[ByteOffset]]»). |
99 // 13. Set O's [[DataView]] internal slot to true. | 86 // 11. Set O's [[DataView]] internal slot to true. |
100 Handle<JSObject> result; | 87 Handle<JSObject> result; |
101 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, | 88 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, |
102 JSObject::New(target, new_target)); | 89 JSObject::New(target, new_target)); |
103 for (int i = 0; i < ArrayBufferView::kInternalFieldCount; ++i) { | 90 for (int i = 0; i < ArrayBufferView::kInternalFieldCount; ++i) { |
104 Handle<JSDataView>::cast(result)->SetInternalField(i, Smi::FromInt(0)); | 91 Handle<JSDataView>::cast(result)->SetInternalField(i, Smi::FromInt(0)); |
105 } | 92 } |
106 | 93 |
107 // 14. Set O's [[ViewedArrayBuffer]] internal slot to buffer. | 94 // 12. Set O's [[ViewedArrayBuffer]] internal slot to buffer. |
108 Handle<JSDataView>::cast(result)->set_buffer(*array_buffer); | 95 Handle<JSDataView>::cast(result)->set_buffer(*array_buffer); |
109 | 96 |
110 // 15. Set O's [[ByteLength]] internal slot to viewByteLength. | 97 // 13. Set O's [[ByteLength]] internal slot to viewByteLength. |
111 Handle<JSDataView>::cast(result)->set_byte_length(*view_byte_length); | 98 Handle<JSDataView>::cast(result)->set_byte_length(*view_byte_length); |
112 | 99 |
113 // 16. Set O's [[ByteOffset]] internal slot to offset. | 100 // 14. Set O's [[ByteOffset]] internal slot to offset. |
114 Handle<JSDataView>::cast(result)->set_byte_offset(*offset); | 101 Handle<JSDataView>::cast(result)->set_byte_offset(*offset); |
115 | 102 |
116 // 17. Return O. | 103 // 15. Return O. |
117 return *result; | 104 return *result; |
118 } | 105 } |
119 | 106 |
120 // ES6 section 24.2.4.1 get DataView.prototype.buffer | 107 // ES6 section 24.2.4.1 get DataView.prototype.buffer |
121 BUILTIN(DataViewPrototypeGetBuffer) { | 108 BUILTIN(DataViewPrototypeGetBuffer) { |
122 HandleScope scope(isolate); | 109 HandleScope scope(isolate); |
123 CHECK_RECEIVER(JSDataView, data_view, "get DataView.prototype.buffer"); | 110 CHECK_RECEIVER(JSDataView, data_view, "get DataView.prototype.buffer"); |
124 return data_view->buffer(); | 111 return data_view->buffer(); |
125 } | 112 } |
126 | 113 |
(...skipping 10 matching lines...) Expand all Loading... |
137 BUILTIN(DataViewPrototypeGetByteOffset) { | 124 BUILTIN(DataViewPrototypeGetByteOffset) { |
138 HandleScope scope(isolate); | 125 HandleScope scope(isolate); |
139 CHECK_RECEIVER(JSDataView, data_view, "get DataView.prototype.byteOffset"); | 126 CHECK_RECEIVER(JSDataView, data_view, "get DataView.prototype.byteOffset"); |
140 // TODO(bmeurer): According to the ES6 spec, we should throw a TypeError | 127 // TODO(bmeurer): According to the ES6 spec, we should throw a TypeError |
141 // here if the JSArrayBuffer of the {data_view} was neutered. | 128 // here if the JSArrayBuffer of the {data_view} was neutered. |
142 return data_view->byte_offset(); | 129 return data_view->byte_offset(); |
143 } | 130 } |
144 | 131 |
145 } // namespace internal | 132 } // namespace internal |
146 } // namespace v8 | 133 } // namespace v8 |
OLD | NEW |