OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "src/builtins/builtins.h" |
| 6 #include "src/builtins/builtins-utils.h" |
| 7 |
| 8 namespace v8 { |
| 9 namespace internal { |
| 10 |
| 11 // ----------------------------------------------------------------------------- |
| 12 // ES6 section 24.2 DataView Objects |
| 13 |
| 14 // ES6 section 24.2.2 The DataView Constructor for the [[Call]] case. |
| 15 BUILTIN(DataViewConstructor) { |
| 16 HandleScope scope(isolate); |
| 17 THROW_NEW_ERROR_RETURN_FAILURE( |
| 18 isolate, |
| 19 NewTypeError(MessageTemplate::kConstructorNotFunction, |
| 20 isolate->factory()->NewStringFromAsciiChecked("DataView"))); |
| 21 } |
| 22 |
| 23 // ES6 section 24.2.2 The DataView Constructor for the [[Construct]] case. |
| 24 BUILTIN(DataViewConstructor_ConstructStub) { |
| 25 HandleScope scope(isolate); |
| 26 Handle<JSFunction> target = args.target<JSFunction>(); |
| 27 Handle<JSReceiver> new_target = Handle<JSReceiver>::cast(args.new_target()); |
| 28 Handle<Object> buffer = args.atOrUndefined(isolate, 1); |
| 29 Handle<Object> byte_offset = args.atOrUndefined(isolate, 2); |
| 30 Handle<Object> byte_length = args.atOrUndefined(isolate, 3); |
| 31 |
| 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 |
| 34 // TypeError exception. |
| 35 if (!buffer->IsJSArrayBuffer()) { |
| 36 THROW_NEW_ERROR_RETURN_FAILURE( |
| 37 isolate, NewTypeError(MessageTemplate::kDataViewNotArrayBuffer)); |
| 38 } |
| 39 Handle<JSArrayBuffer> array_buffer = Handle<JSArrayBuffer>::cast(buffer); |
| 40 |
| 41 // 4. Let numberOffset be ? ToNumber(byteOffset). |
| 42 Handle<Object> number_offset; |
| 43 if (byte_offset->IsUndefined(isolate)) { |
| 44 // We intentionally violate the specification at this point to allow |
| 45 // for new DataView(buffer) invocations to be equivalent to the full |
| 46 // new DataView(buffer, 0) invocation. |
| 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 |
| 53 // 5. Let offset be ToInteger(numberOffset). |
| 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. |
| 66 |
| 67 // 8. Let bufferByteLength be the value of buffer's [[ArrayBufferByteLength]] |
| 68 // internal slot. |
| 69 double const buffer_byte_length = array_buffer->byte_length()->Number(); |
| 70 |
| 71 // 9. If offset > bufferByteLength, throw a RangeError exception |
| 72 if (offset->Number() > buffer_byte_length) { |
| 73 THROW_NEW_ERROR_RETURN_FAILURE( |
| 74 isolate, NewRangeError(MessageTemplate::kInvalidDataViewOffset)); |
| 75 } |
| 76 |
| 77 Handle<Object> view_byte_length; |
| 78 if (byte_length->IsUndefined(isolate)) { |
| 79 // 10. If byteLength is undefined, then |
| 80 // a. Let viewByteLength be bufferByteLength - offset. |
| 81 view_byte_length = |
| 82 isolate->factory()->NewNumber(buffer_byte_length - offset->Number()); |
| 83 } else { |
| 84 // 11. Else, |
| 85 // a. Let viewByteLength be ? ToLength(byteLength). |
| 86 // b. If offset+viewByteLength > bufferByteLength, throw a RangeError |
| 87 // exception |
| 88 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, view_byte_length, |
| 89 Object::ToLength(isolate, byte_length)); |
| 90 if (offset->Number() + view_byte_length->Number() > buffer_byte_length) { |
| 91 THROW_NEW_ERROR_RETURN_FAILURE( |
| 92 isolate, NewRangeError(MessageTemplate::kInvalidDataViewLength)); |
| 93 } |
| 94 } |
| 95 |
| 96 // 12. Let O be ? OrdinaryCreateFromConstructor(NewTarget, |
| 97 // "%DataViewPrototype%", «[[DataView]], [[ViewedArrayBuffer]], |
| 98 // [[ByteLength]], [[ByteOffset]]»). |
| 99 // 13. Set O's [[DataView]] internal slot to true. |
| 100 Handle<JSObject> result; |
| 101 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, |
| 102 JSObject::New(target, new_target)); |
| 103 for (int i = 0; i < ArrayBufferView::kInternalFieldCount; ++i) { |
| 104 Handle<JSDataView>::cast(result)->SetInternalField(i, Smi::FromInt(0)); |
| 105 } |
| 106 |
| 107 // 14. Set O's [[ViewedArrayBuffer]] internal slot to buffer. |
| 108 Handle<JSDataView>::cast(result)->set_buffer(*array_buffer); |
| 109 |
| 110 // 15. Set O's [[ByteLength]] internal slot to viewByteLength. |
| 111 Handle<JSDataView>::cast(result)->set_byte_length(*view_byte_length); |
| 112 |
| 113 // 16. Set O's [[ByteOffset]] internal slot to offset. |
| 114 Handle<JSDataView>::cast(result)->set_byte_offset(*offset); |
| 115 |
| 116 // 17. Return O. |
| 117 return *result; |
| 118 } |
| 119 |
| 120 // ES6 section 24.2.4.1 get DataView.prototype.buffer |
| 121 BUILTIN(DataViewPrototypeGetBuffer) { |
| 122 HandleScope scope(isolate); |
| 123 CHECK_RECEIVER(JSDataView, data_view, "get DataView.prototype.buffer"); |
| 124 return data_view->buffer(); |
| 125 } |
| 126 |
| 127 // ES6 section 24.2.4.2 get DataView.prototype.byteLength |
| 128 BUILTIN(DataViewPrototypeGetByteLength) { |
| 129 HandleScope scope(isolate); |
| 130 CHECK_RECEIVER(JSDataView, data_view, "get DataView.prototype.byteLength"); |
| 131 // TODO(bmeurer): According to the ES6 spec, we should throw a TypeError |
| 132 // here if the JSArrayBuffer of the {data_view} was neutered. |
| 133 return data_view->byte_length(); |
| 134 } |
| 135 |
| 136 // ES6 section 24.2.4.3 get DataView.prototype.byteOffset |
| 137 BUILTIN(DataViewPrototypeGetByteOffset) { |
| 138 HandleScope scope(isolate); |
| 139 CHECK_RECEIVER(JSDataView, data_view, "get DataView.prototype.byteOffset"); |
| 140 // TODO(bmeurer): According to the ES6 spec, we should throw a TypeError |
| 141 // here if the JSArrayBuffer of the {data_view} was neutered. |
| 142 return data_view->byte_offset(); |
| 143 } |
| 144 |
| 145 } // namespace internal |
| 146 } // namespace v8 |
OLD | NEW |