1 /*
2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "ecmascript/builtins/builtins_dataview.h"
17
18 #include "ecmascript/base/number_helper.h"
19 #include "ecmascript/builtins/builtins_arraybuffer.h"
20 #include "ecmascript/ecma_macros.h"
21 #include "ecmascript/global_env.h"
22 #include "ecmascript/js_arraybuffer.h"
23 #include "ecmascript/js_tagged_number.h"
24 #include "ecmascript/js_tagged_value-inl.h"
25 #include "ecmascript/js_tagged_value.h"
26
27 namespace panda::ecmascript::builtins {
28 // 24.2.2.1
DataViewConstructor(EcmaRuntimeCallInfo * argv)29 JSTaggedValue BuiltinsDataView::DataViewConstructor(EcmaRuntimeCallInfo *argv)
30 {
31 ASSERT(argv);
32 BUILTINS_API_TRACE(argv->GetThread(), DataView, Constructor);
33 JSThread *thread = argv->GetThread();
34 [[maybe_unused]] EcmaHandleScope handleScope(thread);
35 JSHandle<JSTaggedValue> ctor = GetConstructor(argv);
36 JSHandle<JSTaggedValue> newTarget = GetNewTarget(argv);
37 // 1. If NewTarget is undefined, throw a TypeError exception.
38 if (newTarget->IsUndefined()) {
39 THROW_TYPE_ERROR_AND_RETURN(thread, "newtarget is undefined", JSTaggedValue::Exception());
40 }
41 JSHandle<JSTaggedValue> bufferHandle = GetCallArg(argv, 0);
42 // 2. If Type(buffer) is not Object, throw a TypeError exception.
43 if (!bufferHandle->IsECMAObject()) {
44 THROW_TYPE_ERROR_AND_RETURN(thread, "buffer is not Object", JSTaggedValue::Exception());
45 }
46 // 3. If buffer does not have an [[ArrayBufferData]] internal slot, throw a TypeError exception.
47 if (!bufferHandle->IsArrayBuffer() && !bufferHandle->IsSharedArrayBuffer()) {
48 THROW_TYPE_ERROR_AND_RETURN(thread, "buffer is not ArrayBuffer", JSTaggedValue::Exception());
49 }
50 JSHandle<JSTaggedValue> offsetHandle = GetCallArg(argv, 1);
51 // 4. Let numberOffset be ToNumber(byteOffset).
52 JSTaggedNumber offsetNumber = JSTaggedValue::ToNumber(thread, offsetHandle);
53 // 6. ReturnIfAbrupt(offset).
54 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
55 int32_t offsetInt = base::NumberHelper::DoubleInRangeInt32(offsetNumber.GetNumber());
56 // 7. If numberOffset ≠ offset or offset < 0, throw a RangeError exception.
57 if (offsetInt < 0) {
58 THROW_RANGE_ERROR_AND_RETURN(thread, "Offset out of range", JSTaggedValue::Exception());
59 }
60 uint32_t offset = static_cast<uint32_t>(offsetInt);
61 // 8. If IsDetachedBuffer(buffer) is true, throw a TypeError exception.
62 if (BuiltinsArrayBuffer::IsDetachedBuffer(bufferHandle.GetTaggedValue())) {
63 THROW_TYPE_ERROR_AND_RETURN(thread, "buffer is Detached Buffer", JSTaggedValue::Exception());
64 }
65 // 9. Let bufferByteLength be the value of buffer’s [[ArrayBufferByteLength]] internal slot.
66 JSHandle<JSArrayBuffer> arrBufHandle(bufferHandle);
67 uint32_t bufByteLen = arrBufHandle->GetArrayBufferByteLength();
68 // 10. If offset > bufferByteLength, throw a RangeError exception.
69 if (offset > bufByteLen) {
70 THROW_RANGE_ERROR_AND_RETURN(thread, "offset > bufferByteLength", JSTaggedValue::Exception());
71 }
72 uint32_t viewByteLen = 0;
73 JSHandle<JSTaggedValue> byteLenHandle = GetCallArg(argv, BuiltinsBase::ArgsPosition::THIRD);
74 // 11. If byteLength is undefined, then Let viewByteLength be bufferByteLength – offset.
75 if (byteLenHandle->IsUndefined()) {
76 viewByteLen = bufByteLen - offset;
77 } else {
78 // Let viewByteLength be ToIndex(byteLength).
79 JSTaggedNumber byteLen = JSTaggedValue::ToIndex(thread, byteLenHandle);
80 // ReturnIfAbrupt(viewByteLength).
81 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
82 viewByteLen = static_cast<uint32_t>(byteLen.ToInt32());
83 // If offset+viewByteLength > bufferByteLength, throw a RangeError exception.
84 if (offset + viewByteLen > bufByteLen) {
85 THROW_RANGE_ERROR_AND_RETURN(thread, "offset + viewByteLen > bufByteLen", JSTaggedValue::Exception());
86 }
87 }
88 // 13. Let O be OrdinaryCreateFromConstructor OrdinaryCreateFromConstructor(NewTarget, "%DataViewPrototype%",
89 // «[[DataView]],[[ViewedArrayBuffer]], [[ByteLength]], [[ByteOffset]]» ).
90 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
91 JSHandle<JSObject> obj = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(ctor), newTarget);
92 // 14. ReturnIfAbrupt(O).
93 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
94 JSHandle<JSDataView> dataView(obj);
95 // 15. Set O’s [[DataView]] internal slot to true.
96 dataView->SetDataView(thread, JSTaggedValue::True());
97 // 16. Set O’s [[ViewedArrayBuffer]] internal slot to buffer.
98 dataView->SetViewedArrayBuffer(thread, bufferHandle.GetTaggedValue());
99 // 17. Set O’s [[ByteLength]] internal slot to viewByteLength.
100 dataView->SetByteLength(viewByteLen);
101 // 18. Set O’s [[ByteOffset]] internal slot to offset.
102 dataView->SetByteOffset(offset);
103 // 19. Return O.
104 return JSTaggedValue(dataView.GetTaggedValue());
105 }
106
107 // 24.2.4.1
GetBuffer(EcmaRuntimeCallInfo * argv)108 JSTaggedValue BuiltinsDataView::GetBuffer(EcmaRuntimeCallInfo *argv)
109 {
110 ASSERT(argv);
111 BUILTINS_API_TRACE(argv->GetThread(), DataView, GetBuffer);
112 JSThread *thread = argv->GetThread();
113 [[maybe_unused]] EcmaHandleScope handleScope(thread);
114 // 1. Let O be the this value.
115 JSHandle<JSTaggedValue> thisHandle = GetThis(argv);
116 // 2. f Type(O) is not Object, throw a TypeError exception.
117 if (!thisHandle->IsECMAObject()) {
118 THROW_TYPE_ERROR_AND_RETURN(thread, "Type(O) is not Object", JSTaggedValue::Exception());
119 }
120 // 3. If O does not have a [[ViewedArrayBuffer]] internal slot, throw a TypeError exception.
121 if (!thisHandle->IsDataView()) {
122 THROW_TYPE_ERROR_AND_RETURN(thread, "O does not have a [[ViewedArrayBuffer]]", JSTaggedValue::Exception());
123 }
124 JSHandle<JSDataView> dataView(thisHandle);
125 // 4. Let buffer be the value of O’s [[ViewedArrayBuffer]] internal slot.
126 JSTaggedValue buffer = dataView->GetViewedArrayBuffer();
127 // 5. Return buffer.
128 return JSTaggedValue(buffer);
129 }
130
131 // 24.2.4.2
GetByteLength(EcmaRuntimeCallInfo * argv)132 JSTaggedValue BuiltinsDataView::GetByteLength(EcmaRuntimeCallInfo *argv)
133 {
134 ASSERT(argv);
135 BUILTINS_API_TRACE(argv->GetThread(), DataView, GetByteLength);
136 JSThread *thread = argv->GetThread();
137 [[maybe_unused]] EcmaHandleScope handleScope(thread);
138 // 1. Let O be the this value.
139 JSHandle<JSTaggedValue> thisHandle = GetThis(argv);
140 // 2. If Type(O) is not Object, throw a TypeError exception.
141 if (!thisHandle->IsECMAObject()) {
142 THROW_TYPE_ERROR_AND_RETURN(thread, "Type(O) is not Object", JSTaggedValue::Exception());
143 }
144 // 3. If O does not have a [[ViewedArrayBuffer]] internal slot, throw a TypeError exception.
145 if (!thisHandle->IsDataView()) {
146 THROW_TYPE_ERROR_AND_RETURN(thread, "O does not have a [[ViewedArrayBuffer]]", JSTaggedValue::Exception());
147 }
148 JSHandle<JSDataView> dataView(thisHandle);
149 // 4. Let buffer be the value of O’s [[ViewedArrayBuffer]] internal slot.
150 JSTaggedValue buffer = dataView->GetViewedArrayBuffer();
151 // 5. If IsDetachedBuffer(buffer) is true, throw a TypeError exception.
152 if (BuiltinsArrayBuffer::IsDetachedBuffer(buffer)) {
153 THROW_TYPE_ERROR_AND_RETURN(thread, "Is Detached Buffer", JSTaggedValue::Exception());
154 }
155 // 6. Let size be the value of O’s [[ByteLength]] internal slot.
156 uint32_t size = dataView->GetByteLength();
157 // 7. Return size.
158 return JSTaggedValue(size);
159 }
160
161 // 24.2.4.3
GetOffset(EcmaRuntimeCallInfo * argv)162 JSTaggedValue BuiltinsDataView::GetOffset(EcmaRuntimeCallInfo *argv)
163 {
164 ASSERT(argv);
165 BUILTINS_API_TRACE(argv->GetThread(), DataView, GetOffset);
166 JSThread *thread = argv->GetThread();
167 [[maybe_unused]] EcmaHandleScope handleScope(thread);
168 // 1. Let O be the this value.
169 JSHandle<JSTaggedValue> thisHandle = GetThis(argv);
170 // 2. If Type(O) is not Object, throw a TypeError exception.
171 if (!thisHandle->IsECMAObject()) {
172 THROW_TYPE_ERROR_AND_RETURN(thread, "Type(O) is not Object", JSTaggedValue::Exception());
173 }
174 // 3. If O does not have a [[ViewedArrayBuffer]] internal slot, throw a TypeError exception.
175 if (!thisHandle->IsDataView()) {
176 THROW_TYPE_ERROR_AND_RETURN(thread, "O does not have a [[ViewedArrayBuffer]]", JSTaggedValue::Exception());
177 }
178 JSHandle<JSDataView> dataView(thisHandle);
179 // 4. Let buffer be the value of O’s [[ViewedArrayBuffer]] internal slot.
180 JSTaggedValue buffer = dataView->GetViewedArrayBuffer();
181 // 5. If IsDetachedBuffer(buffer) is true, throw a TypeError exception.
182 if (BuiltinsArrayBuffer::IsDetachedBuffer(buffer)) {
183 THROW_TYPE_ERROR_AND_RETURN(thread, "Is Detached Buffer", JSTaggedValue::Exception());
184 }
185 // 6. Let offset be the value of O’s [[ByteOffset]] internal slot.
186 uint32_t offset = dataView->GetByteOffset();
187 // 7. Return offset.
188 return JSTaggedValue(offset);
189 }
190
191 // 24.2.4.5
GetFloat32(EcmaRuntimeCallInfo * argv)192 JSTaggedValue BuiltinsDataView::GetFloat32(EcmaRuntimeCallInfo *argv)
193 {
194 ASSERT(argv);
195 return GetTypedValue(argv, DataViewType::FLOAT32);
196 }
197
198 // 24.2.4.6
GetFloat64(EcmaRuntimeCallInfo * argv)199 JSTaggedValue BuiltinsDataView::GetFloat64(EcmaRuntimeCallInfo *argv)
200 {
201 ASSERT(argv);
202 return GetTypedValue(argv, DataViewType::FLOAT64);
203 }
204
205 // 24.2.4.7
GetInt8(EcmaRuntimeCallInfo * argv)206 JSTaggedValue BuiltinsDataView::GetInt8(EcmaRuntimeCallInfo *argv)
207 {
208 ASSERT(argv);
209 return GetTypedValue(argv, DataViewType::INT8);
210 }
211
212 // 24.2.4.8
GetInt16(EcmaRuntimeCallInfo * argv)213 JSTaggedValue BuiltinsDataView::GetInt16(EcmaRuntimeCallInfo *argv)
214 {
215 ASSERT(argv);
216 return GetTypedValue(argv, DataViewType::INT16);
217 }
218
219 // 24.2.4.9
GetInt32(EcmaRuntimeCallInfo * argv)220 JSTaggedValue BuiltinsDataView::GetInt32(EcmaRuntimeCallInfo *argv)
221 {
222 ASSERT(argv);
223 return GetTypedValue(argv, DataViewType::INT32);
224 }
225
226 // 24.2.4.10
GetUint8(EcmaRuntimeCallInfo * argv)227 JSTaggedValue BuiltinsDataView::GetUint8(EcmaRuntimeCallInfo *argv)
228 {
229 ASSERT(argv);
230 return GetTypedValue(argv, DataViewType::UINT8);
231 }
232
233 // 24.2.4.11
GetUint16(EcmaRuntimeCallInfo * argv)234 JSTaggedValue BuiltinsDataView::GetUint16(EcmaRuntimeCallInfo *argv)
235 {
236 ASSERT(argv);
237 return GetTypedValue(argv, DataViewType::UINT16);
238 }
239
240 // 24.2.4.12
GetUint32(EcmaRuntimeCallInfo * argv)241 JSTaggedValue BuiltinsDataView::GetUint32(EcmaRuntimeCallInfo *argv)
242 {
243 ASSERT(argv);
244 return GetTypedValue(argv, DataViewType::UINT32);
245 }
246 // 25.3.4.5
GetBigInt64(EcmaRuntimeCallInfo * argv)247 JSTaggedValue BuiltinsDataView::GetBigInt64(EcmaRuntimeCallInfo *argv)
248 {
249 ASSERT(argv);
250 return GetTypedValue(argv, DataViewType::BIGINT64);
251 }
252 // 25.3.4.6
GetBigUint64(EcmaRuntimeCallInfo * argv)253 JSTaggedValue BuiltinsDataView::GetBigUint64(EcmaRuntimeCallInfo *argv)
254 {
255 ASSERT(argv);
256 return GetTypedValue(argv, DataViewType::BIGUINT64);
257 }
258 // 24.2.4.13
SetFloat32(EcmaRuntimeCallInfo * argv)259 JSTaggedValue BuiltinsDataView::SetFloat32(EcmaRuntimeCallInfo *argv)
260 {
261 ASSERT(argv);
262 return SetTypedValue(argv, DataViewType::FLOAT32);
263 }
264
265 // 24.2.4.14
SetFloat64(EcmaRuntimeCallInfo * argv)266 JSTaggedValue BuiltinsDataView::SetFloat64(EcmaRuntimeCallInfo *argv)
267 {
268 ASSERT(argv);
269 return SetTypedValue(argv, DataViewType::FLOAT64);
270 }
271
272 // 24.2.4.15
SetInt8(EcmaRuntimeCallInfo * argv)273 JSTaggedValue BuiltinsDataView::SetInt8(EcmaRuntimeCallInfo *argv)
274 {
275 ASSERT(argv);
276 return SetTypedValue(argv, DataViewType::INT8);
277 }
278
279 // 24.2.4.16
SetInt16(EcmaRuntimeCallInfo * argv)280 JSTaggedValue BuiltinsDataView::SetInt16(EcmaRuntimeCallInfo *argv)
281 {
282 ASSERT(argv);
283 return SetTypedValue(argv, DataViewType::INT16);
284 }
285
286 // 24.2.4.17
SetInt32(EcmaRuntimeCallInfo * argv)287 JSTaggedValue BuiltinsDataView::SetInt32(EcmaRuntimeCallInfo *argv)
288 {
289 ASSERT(argv);
290 return SetTypedValue(argv, DataViewType::INT32);
291 }
292
293 // 24.2.4.18
SetUint8(EcmaRuntimeCallInfo * argv)294 JSTaggedValue BuiltinsDataView::SetUint8(EcmaRuntimeCallInfo *argv)
295 {
296 ASSERT(argv);
297 return SetTypedValue(argv, DataViewType::UINT8);
298 }
299
300 // 24.2.4.19
SetUint16(EcmaRuntimeCallInfo * argv)301 JSTaggedValue BuiltinsDataView::SetUint16(EcmaRuntimeCallInfo *argv)
302 {
303 ASSERT(argv);
304 return SetTypedValue(argv, DataViewType::UINT16);
305 }
306
307 // 24.2.4.20
SetUint32(EcmaRuntimeCallInfo * argv)308 JSTaggedValue BuiltinsDataView::SetUint32(EcmaRuntimeCallInfo *argv)
309 {
310 ASSERT(argv);
311 return SetTypedValue(argv, DataViewType::UINT32);
312 }
313
314 // 25.3.4.15
SetBigInt64(EcmaRuntimeCallInfo * argv)315 JSTaggedValue BuiltinsDataView::SetBigInt64(EcmaRuntimeCallInfo *argv)
316 {
317 ASSERT(argv);
318 return SetTypedValue(argv, DataViewType::BIGINT64);
319 }
320
321 // 25.3.4.16
SetBigUint64(EcmaRuntimeCallInfo * argv)322 JSTaggedValue BuiltinsDataView::SetBigUint64(EcmaRuntimeCallInfo *argv)
323 {
324 ASSERT(argv);
325 return SetTypedValue(argv, DataViewType::BIGUINT64);
326 }
327
328 // 24.2.1.1
GetViewValue(JSThread * thread,const JSHandle<JSTaggedValue> & view,const JSHandle<JSTaggedValue> & requestIndex,JSTaggedValue littleEndian,DataViewType type)329 JSTaggedValue BuiltinsDataView::GetViewValue(JSThread *thread, const JSHandle<JSTaggedValue> &view,
330 const JSHandle<JSTaggedValue> &requestIndex, JSTaggedValue littleEndian,
331 DataViewType type)
332 {
333 BUILTINS_API_TRACE(thread, DataView, GetViewValue);
334 // 1. If Type(view) is not Object, throw a TypeError exception.
335 if (!view->IsECMAObject()) {
336 THROW_TYPE_ERROR_AND_RETURN(thread, "Type(O) is not Object", JSTaggedValue::Exception());
337 }
338 // 2. If view does not have a [[DataView]] internal slot, throw a TypeError exception.
339 if (!view->IsDataView()) {
340 THROW_TYPE_ERROR_AND_RETURN(thread, "view is not dataview", JSTaggedValue::Exception());
341 }
342 // 3. Let numberIndex be ToNumber(requestIndex).
343 JSTaggedNumber numberIndex = JSTaggedValue::ToNumber(thread, requestIndex);
344 // 5. ReturnIfAbrupt(getIndex).
345 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
346 int32_t indexInt = base::NumberHelper::DoubleInRangeInt32(numberIndex.GetNumber());
347 // 6. If numberIndex ≠ getIndex or getIndex < 0, throw a RangeError exception.
348 if (indexInt < 0) {
349 THROW_RANGE_ERROR_AND_RETURN(thread, "getIndex < 0", JSTaggedValue::Exception());
350 }
351 uint32_t index = static_cast<uint32_t>(indexInt);
352 // 7. Let isLittleEndian be ToBoolean(isLittleEndian).
353 bool isLittleEndian = false;
354 if (littleEndian.IsUndefined()) {
355 isLittleEndian = false;
356 } else {
357 isLittleEndian = littleEndian.ToBoolean();
358 }
359 // 8. Let buffer be the value of view’s [[ViewedArrayBuffer]] internal slot.
360 JSHandle<JSDataView> dataView(view);
361 JSTaggedValue buffer = dataView->GetViewedArrayBuffer();
362 // 9. If IsDetachedBuffer(buffer) is true, throw a TypeError exception.
363 if (BuiltinsArrayBuffer::IsDetachedBuffer(buffer)) {
364 THROW_TYPE_ERROR_AND_RETURN(thread, "Is Detached Buffer", JSTaggedValue::Exception());
365 }
366 // 10. Let viewOffset be the value of view’s [[ByteOffset]] internal slot.
367 uint32_t offset = dataView->GetByteOffset();
368 // 11. Let viewSize be the value of view’s [[ByteLength]] internal slot.
369 uint32_t size = dataView->GetByteLength();
370 // 12. Let elementSize be the Number value of the Element Size value specified in Table 49 for Element Type type.
371 uint32_t elementSize = JSDataView::GetElementSize(type);
372 // 13. If getIndex +elementSize > viewSize, throw a RangeError exception.
373 if (index + elementSize > size) {
374 THROW_RANGE_ERROR_AND_RETURN(thread, "getIndex +elementSize > viewSize", JSTaggedValue::Exception());
375 }
376 // 14. Let bufferIndex be getIndex + viewOffset.
377 uint32_t bufferIndex = index + offset;
378 // 15. Return GetValueFromBuffer(buffer, bufferIndex, type, isLittleEndian).
379 return BuiltinsArrayBuffer::GetValueFromBuffer(thread, buffer, bufferIndex, type, isLittleEndian);
380 }
381
382 // 24.2.1.2
SetViewValue(JSThread * thread,const JSHandle<JSTaggedValue> & view,const JSHandle<JSTaggedValue> & requestIndex,JSTaggedValue littleEndian,DataViewType type,const JSHandle<JSTaggedValue> & value)383 JSTaggedValue BuiltinsDataView::SetViewValue(JSThread *thread, const JSHandle<JSTaggedValue> &view,
384 const JSHandle<JSTaggedValue> &requestIndex, JSTaggedValue littleEndian,
385 DataViewType type, const JSHandle<JSTaggedValue> &value)
386 {
387 // 1. If Type(view) is not Object, throw a TypeError exception.
388 BUILTINS_API_TRACE(thread, DataView, SetViewValue);
389 if (!view->IsECMAObject()) {
390 THROW_TYPE_ERROR_AND_RETURN(thread, "Type(O) is not Object", JSTaggedValue::Exception());
391 }
392 // 2. If view does not have a [[DataView]] internal slot, throw a TypeError exception.
393 if (!view->IsDataView()) {
394 THROW_TYPE_ERROR_AND_RETURN(thread, "view is not dataview", JSTaggedValue::Exception());
395 }
396 // 3. Let numberIndex be ToNumber(requestIndex).
397 JSTaggedNumber numberIndex = JSTaggedValue::ToIndex(thread, requestIndex);
398 // 5. ReturnIfAbrupt(getIndex).
399 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
400 int64_t index = base::NumberHelper::DoubleInRangeInt32(numberIndex.GetNumber());
401 // 6. If numberIndex ≠ getIndex or getIndex < 0, throw a RangeError exception.
402 if (index < 0) {
403 THROW_RANGE_ERROR_AND_RETURN(thread, "getIndex < 0", JSTaggedValue::Exception());
404 }
405 JSHandle<JSTaggedValue> numValueHandle = JSTaggedValue::ToNumeric(thread, value);
406 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
407 // 7. Let isLittleEndian be ToBoolean(isLittleEndian).
408 bool isLittleEndian = false;
409 if (littleEndian.IsUndefined()) {
410 isLittleEndian = false;
411 } else {
412 isLittleEndian = littleEndian.ToBoolean();
413 }
414 // 8. Let buffer be the value of view’s [[ViewedArrayBuffer]] internal slot.
415 JSHandle<JSDataView> dataView(view);
416 JSTaggedValue buffer = dataView->GetViewedArrayBuffer();
417 // 9. If IsDetachedBuffer(buffer) is true, throw a TypeError exception.
418 if (BuiltinsArrayBuffer::IsDetachedBuffer(buffer)) {
419 THROW_TYPE_ERROR_AND_RETURN(thread, "Is Detached Buffer", JSTaggedValue::Exception());
420 }
421 // 10. Let viewOffset be the value of view’s [[ByteOffset]] internal slot.
422 uint32_t offset = dataView->GetByteOffset();
423 // 11. Let viewSize be the value of view’s [[ByteLength]] internal slot.
424 uint32_t size = dataView->GetByteLength();
425 // 12. Let elementSize be the Number value of the Element Size value specified in Table 49 for Element Type type.
426 uint32_t elementSize = JSDataView::GetElementSize(type);
427 // 13. If getIndex +elementSize > viewSize, throw a RangeError exception.
428 if (static_cast<uint32_t>(index) + elementSize > size) {
429 THROW_RANGE_ERROR_AND_RETURN(thread, "getIndex +elementSize > viewSize", JSTaggedValue::Exception());
430 }
431 // 14. Let bufferIndex be getIndex + viewOffset.
432 uint32_t bufferIndex = static_cast<uint32_t>(index) + offset;
433 // 15. Return SetValueFromBuffer(buffer, bufferIndex, type, value, isLittleEndian).
434 return BuiltinsArrayBuffer::SetValueInBuffer(thread, buffer, bufferIndex, type, numValueHandle, isLittleEndian);
435 }
436
GetTypedValue(EcmaRuntimeCallInfo * argv,DataViewType type)437 JSTaggedValue BuiltinsDataView::GetTypedValue(EcmaRuntimeCallInfo *argv, DataViewType type)
438 {
439 JSThread *thread = argv->GetThread();
440 [[maybe_unused]] EcmaHandleScope handleScope(thread);
441 JSHandle<JSTaggedValue> thisHandle = GetThis(argv);
442 JSHandle<JSTaggedValue> offsetHandle = GetCallArg(argv, 0);
443 if (type == DataViewType::UINT8 || type == DataViewType::INT8) {
444 return GetViewValue(thread, thisHandle, offsetHandle, JSTaggedValue::True(), type);
445 }
446 JSHandle<JSTaggedValue> littleEndianHandle = GetCallArg(argv, 1);
447 return GetViewValue(thread, thisHandle, offsetHandle, littleEndianHandle.GetTaggedValue(), type);
448 }
449
SetTypedValue(EcmaRuntimeCallInfo * argv,DataViewType type)450 JSTaggedValue BuiltinsDataView::SetTypedValue(EcmaRuntimeCallInfo *argv, DataViewType type)
451 {
452 JSThread *thread = argv->GetThread();
453 [[maybe_unused]] EcmaHandleScope handleScope(thread);
454 JSHandle<JSTaggedValue> thisHandle = GetThis(argv);
455 JSHandle<JSTaggedValue> offsetHandle = GetCallArg(argv, 0);
456 JSHandle<JSTaggedValue> value = GetCallArg(argv, 1);
457 if (type == DataViewType::UINT8 || type == DataViewType::INT8) {
458 return SetViewValue(thread, thisHandle, offsetHandle, JSTaggedValue::True(), type, value);
459 }
460 JSHandle<JSTaggedValue> littleEndianHandle = GetCallArg(argv, BuiltinsBase::ArgsPosition::THIRD);
461 return SetViewValue(thread, thisHandle, offsetHandle, littleEndianHandle.GetTaggedValue(), type, value);
462 }
463 } // namespace panda::ecmascript::builtins
464