1 /*
2 * Copyright (c) 2021 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/builtins/builtins_arraybuffer.h"
19 #include "ecmascript/ecma_runtime_call_info.h"
20 #include "ecmascript/ecma_vm.h"
21 #include "ecmascript/global_env.h"
22 #include "ecmascript/js_arraybuffer.h"
23 #include "ecmascript/js_dataview.h"
24 #include "ecmascript/js_handle.h"
25 #include "ecmascript/js_tagged_value.h"
26 #include "ecmascript/tests/test_helper.h"
27
28 using namespace panda::ecmascript;
29 using namespace panda::ecmascript::builtins;
30
31 namespace panda::test {
32 using DataViewType = ecmascript::DataViewType;
33 class BuiltinsDataViewTest : public testing::Test {
34 public:
SetUpTestCase()35 static void SetUpTestCase()
36 {
37 GTEST_LOG_(INFO) << "SetUpTestCase";
38 }
39
TearDownTestCase()40 static void TearDownTestCase()
41 {
42 GTEST_LOG_(INFO) << "TearDownCase";
43 }
44
SetUp()45 void SetUp() override
46 {
47 TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
48 }
49
TearDown()50 void TearDown() override
51 {
52 TestHelper::DestroyEcmaVMWithScope(instance, scope);
53 }
54
55 EcmaVM *instance {nullptr};
56 EcmaHandleScope *scope {nullptr};
57 JSThread *thread {nullptr};
58 };
59
CreateBuiltinsDataviewArrayBuffer(JSThread * thread,int32_t length)60 JSTaggedValue CreateBuiltinsDataviewArrayBuffer(JSThread *thread, int32_t length)
61 {
62 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
63 JSHandle<JSFunction> arrayBuffer(thread, env->GetArrayBufferFunction().GetTaggedValue());
64 JSHandle<JSObject> globalObject(thread, env->GetGlobalObject());
65 // 6 : test case
66 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*arrayBuffer), 6);
67 ecmaRuntimeCallInfo->SetFunction(arrayBuffer.GetTaggedValue());
68 ecmaRuntimeCallInfo->SetThis(globalObject.GetTaggedValue());
69 ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(length));
70
71 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
72 JSTaggedValue result = BuiltinsArrayBuffer::ArrayBufferConstructor(ecmaRuntimeCallInfo);
73 TestHelper::TearDownFrame(thread, prev);
74 return result;
75 }
76
CreateBuiltinsDataView(JSThread * thread,int32_t length,int32_t byte_offset)77 JSTaggedValue CreateBuiltinsDataView(JSThread *thread, int32_t length, int32_t byte_offset)
78 {
79 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
80 JSHandle<JSFunction> dataView(thread, env->GetDataViewFunction().GetTaggedValue());
81 JSHandle<JSObject> globalObject(thread, env->GetGlobalObject());
82 JSTaggedValue tagged = CreateBuiltinsDataviewArrayBuffer(thread, length);
83 JSHandle<JSArrayBuffer> arrBuf(thread, JSArrayBuffer::Cast(reinterpret_cast<TaggedObject *>(tagged.GetRawData())));
84 // 8 : test case
85 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*dataView), 8);
86 ecmaRuntimeCallInfo->SetFunction(dataView.GetTaggedValue());
87 ecmaRuntimeCallInfo->SetThis(globalObject.GetTaggedValue());
88 ecmaRuntimeCallInfo->SetCallArg(0, arrBuf.GetTaggedValue());
89 ecmaRuntimeCallInfo->SetCallArg(1, JSTaggedValue(byte_offset));
90
91 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
92 JSTaggedValue result = BuiltinsDataView::DataViewConstructor(ecmaRuntimeCallInfo);
93 TestHelper::TearDownFrame(thread, prev);
94 return result;
95 }
96
CreateArrayBuffer(JSThread * thread,int32_t length)97 JSTaggedValue CreateArrayBuffer(JSThread *thread, int32_t length)
98 {
99 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
100 JSHandle<JSFunction> arrayBuffer(thread, env->GetArrayBufferFunction().GetTaggedValue());
101 JSHandle<JSObject> globalObject(thread, env->GetGlobalObject());
102 // 6 : test case
103 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, arrayBuffer.GetTaggedValue(), 6);
104 ecmaRuntimeCallInfo->SetFunction(arrayBuffer.GetTaggedValue());
105 ecmaRuntimeCallInfo->SetThis(globalObject.GetTaggedValue());
106 ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(length));
107
108 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
109 JSTaggedValue result = BuiltinsArrayBuffer::ArrayBufferConstructor(ecmaRuntimeCallInfo);
110 TestHelper::TearDownFrame(thread, prev);
111 return result;
112 }
113
SetUint8(JSThread * thread,const JSHandle<JSDataView> & view,int32_t offset,JSTaggedValue value)114 void SetUint8(JSThread *thread, const JSHandle<JSDataView> &view, int32_t offset, JSTaggedValue value)
115 {
116 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
117 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
118 ecmaRuntimeCallInfo->SetThis(view.GetTaggedValue());
119 ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(offset));
120 ecmaRuntimeCallInfo->SetCallArg(1, value);
121
122 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
123 BuiltinsDataView::SetUint8(ecmaRuntimeCallInfo);
124 TestHelper::TearDownFrame(thread, prev);
125 }
126
127 // new DataView(new ArrayBuffer(10), 1)
HWTEST_F_L0(BuiltinsDataViewTest,Constructor)128 HWTEST_F_L0(BuiltinsDataViewTest, Constructor)
129 {
130 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
131 JSHandle<JSFunction> dataView(thread, env->GetDataViewFunction().GetTaggedValue());
132 JSHandle<JSObject> globalObject(thread, env->GetGlobalObject());
133 JSTaggedValue tagged = CreateBuiltinsDataviewArrayBuffer(thread, 10);
134 JSHandle<JSArrayBuffer> arrBuf(thread, JSArrayBuffer::Cast(reinterpret_cast<TaggedObject *>(tagged.GetRawData())));
135 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*dataView), 8);
136 ecmaRuntimeCallInfo->SetFunction(dataView.GetTaggedValue());
137 ecmaRuntimeCallInfo->SetThis(globalObject.GetTaggedValue());
138 ecmaRuntimeCallInfo->SetCallArg(0, arrBuf.GetTaggedValue());
139 ecmaRuntimeCallInfo->SetCallArg(1, JSTaggedValue(1));
140
141 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
142 JSTaggedValue result = BuiltinsDataView::DataViewConstructor(ecmaRuntimeCallInfo);
143 ASSERT_TRUE(result.IsECMAObject());
144 TestHelper::TearDownFrame(thread, prev);
145
146 // case: Detached Buffer
147 arrBuf->SetArrayBufferData(thread, JSTaggedValue::Null());
148 ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*dataView), 8);
149 ecmaRuntimeCallInfo->SetFunction(dataView.GetTaggedValue());
150 ecmaRuntimeCallInfo->SetThis(globalObject.GetTaggedValue());
151 ecmaRuntimeCallInfo->SetCallArg(0, arrBuf.GetTaggedValue());
152 ecmaRuntimeCallInfo->SetCallArg(1, JSTaggedValue(1));
153
154 prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
155 result = BuiltinsDataView::DataViewConstructor(ecmaRuntimeCallInfo);
156 TestHelper::TearDownFrame(thread, prev);
157 EXPECT_TRUE(thread->HasPendingException());
158 EXPECT_EQ(result, JSTaggedValue::Exception());
159 thread->ClearException();
160 }
161
162 // new DataView(new ArrayBuffer(10), 1).byteOffset
HWTEST_F_L0(BuiltinsDataViewTest,byteOffset)163 HWTEST_F_L0(BuiltinsDataViewTest, byteOffset)
164 {
165 JSTaggedValue tagged = CreateBuiltinsDataView(thread, 10, 1);
166 JSHandle<JSDataView> view(thread, JSDataView::Cast(reinterpret_cast<TaggedObject *>(tagged.GetRawData())));
167 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
168 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
169 ecmaRuntimeCallInfo->SetThis(view.GetTaggedValue());
170
171 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
172 JSTaggedValue result = BuiltinsDataView::GetOffset(ecmaRuntimeCallInfo);
173 ASSERT_EQ(result.GetRawData(), JSTaggedValue(1).GetRawData());
174 TestHelper::TearDownFrame(thread, prev);
175
176 // case: Detached Buffer
177 JSTaggedValue tagged1 = CreateArrayBuffer(thread, 10);
178 JSHandle<JSArrayBuffer> arrBuf(thread, JSArrayBuffer::Cast(reinterpret_cast<TaggedObject *>(tagged1.GetRawData())));
179 arrBuf->SetArrayBufferData(thread, JSTaggedValue::Null());
180 view->SetViewedArrayBuffer(thread, arrBuf);
181 ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
182 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
183 ecmaRuntimeCallInfo->SetThis(view.GetTaggedValue());
184
185 prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
186 result = BuiltinsDataView::GetOffset(ecmaRuntimeCallInfo);
187 TestHelper::TearDownFrame(thread, prev);
188 EXPECT_TRUE(thread->HasPendingException());
189 EXPECT_EQ(result, JSTaggedValue::Exception());
190 thread->ClearException();
191 }
192
193 // new DataView(new ArrayBuffer(10), 2).byteLength
HWTEST_F_L0(BuiltinsDataViewTest,byteLength)194 HWTEST_F_L0(BuiltinsDataViewTest, byteLength)
195 {
196 JSTaggedValue tagged = CreateBuiltinsDataView(thread, 10, 2);
197 JSHandle<JSDataView> view(thread, JSDataView::Cast(reinterpret_cast<TaggedObject *>(tagged.GetRawData())));
198 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
199 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
200 ecmaRuntimeCallInfo->SetThis(view.GetTaggedValue());
201
202 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
203 JSTaggedValue result = BuiltinsDataView::GetByteLength(ecmaRuntimeCallInfo);
204 ASSERT_EQ(result.GetRawData(), JSTaggedValue(8).GetRawData());
205 TestHelper::TearDownFrame(thread, prev);
206
207 // case: Detached Buffer
208 JSTaggedValue tagged1 = CreateArrayBuffer(thread, 10);
209 JSHandle<JSArrayBuffer> arrBuf(thread, JSArrayBuffer::Cast(reinterpret_cast<TaggedObject *>(tagged1.GetRawData())));
210 arrBuf->SetArrayBufferData(thread, JSTaggedValue::Null());
211 view->SetViewedArrayBuffer(thread, arrBuf);
212 ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
213 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
214 ecmaRuntimeCallInfo->SetThis(view.GetTaggedValue());
215
216 prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
217 result = BuiltinsDataView::GetByteLength(ecmaRuntimeCallInfo);
218 TestHelper::TearDownFrame(thread, prev);
219 EXPECT_TRUE(thread->HasPendingException());
220 EXPECT_EQ(result, JSTaggedValue::Exception());
221 thread->ClearException();
222 }
223
224 // new DataView(new ArrayBuffer(10), 1).buffer
HWTEST_F_L0(BuiltinsDataViewTest,buffer)225 HWTEST_F_L0(BuiltinsDataViewTest, buffer)
226 {
227 JSTaggedValue tagged = CreateBuiltinsDataView(thread, 10, 1);
228 JSHandle<JSDataView> view(thread, JSDataView::Cast(reinterpret_cast<TaggedObject *>(tagged.GetRawData())));
229 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
230 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
231 ecmaRuntimeCallInfo->SetThis(view.GetTaggedValue());
232
233 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
234 JSTaggedValue result = BuiltinsDataView::GetBuffer(ecmaRuntimeCallInfo);
235 ASSERT_EQ(result.IsArrayBuffer(), true);
236 TestHelper::TearDownFrame(thread, prev);
237 }
238
239 // new DataView(new ArrayBuffer(8), 0).SetUint16/GetUint16
HWTEST_F_L0(BuiltinsDataViewTest,getUint16)240 HWTEST_F_L0(BuiltinsDataViewTest, getUint16)
241 {
242 JSTaggedValue tagged = CreateBuiltinsDataView(thread, 8, 0);
243 JSHandle<JSDataView> view(thread, JSDataView::Cast(reinterpret_cast<TaggedObject *>(tagged.GetRawData())));
244 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 10);
245 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
246 ecmaRuntimeCallInfo->SetThis(view.GetTaggedValue());
247 ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(0));
248 ecmaRuntimeCallInfo->SetCallArg(1, JSTaggedValue(-1870724872));
249 ecmaRuntimeCallInfo->SetCallArg(2, JSTaggedValue::False());
250
251 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
252 JSTaggedValue result = BuiltinsDataView::SetUint16(ecmaRuntimeCallInfo);
253 ASSERT_EQ(result.GetRawData(), JSTaggedValue::VALUE_UNDEFINED);
254 TestHelper::TearDownFrame(thread, prev);
255
256 auto ecmaRuntimeCallInfo1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
257 ecmaRuntimeCallInfo1->SetFunction(JSTaggedValue::Undefined());
258 ecmaRuntimeCallInfo1->SetThis(view.GetTaggedValue());
259 ecmaRuntimeCallInfo1->SetCallArg(0, JSTaggedValue(0));
260 ecmaRuntimeCallInfo1->SetCallArg(1, JSTaggedValue::True());
261
262 [[maybe_unused]] auto prev1 = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1);
263 JSTaggedValue result1 = BuiltinsDataView::GetUint16(ecmaRuntimeCallInfo1);
264 ASSERT_EQ(result1.GetRawData(), JSTaggedValue(63488).GetRawData());
265 TestHelper::TearDownFrame(thread, prev1);
266 }
267
268 // new DataView(new ArrayBuffer(8), 0).SetInt16/GetInt16
HWTEST_F_L0(BuiltinsDataViewTest,getInt16)269 HWTEST_F_L0(BuiltinsDataViewTest, getInt16)
270 {
271 JSTaggedValue tagged = CreateBuiltinsDataView(thread, 8, 0);
272 JSHandle<JSDataView> view(thread, JSDataView::Cast(reinterpret_cast<TaggedObject *>(tagged.GetRawData())));
273 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 10);
274 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
275 ecmaRuntimeCallInfo->SetThis(view.GetTaggedValue());
276 ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(0));
277 ecmaRuntimeCallInfo->SetCallArg(1, JSTaggedValue(-1870724872));
278 ecmaRuntimeCallInfo->SetCallArg(2, JSTaggedValue::False());
279
280 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
281 JSTaggedValue result = BuiltinsDataView::SetInt16(ecmaRuntimeCallInfo);
282 ASSERT_EQ(result.GetRawData(), JSTaggedValue::VALUE_UNDEFINED);
283 TestHelper::TearDownFrame(thread, prev);
284
285 auto ecmaRuntimeCallInfo1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
286 ecmaRuntimeCallInfo1->SetFunction(JSTaggedValue::Undefined());
287 ecmaRuntimeCallInfo1->SetThis(view.GetTaggedValue());
288 ecmaRuntimeCallInfo1->SetCallArg(0, JSTaggedValue(0));
289 ecmaRuntimeCallInfo1->SetCallArg(1, JSTaggedValue::True());
290
291 [[maybe_unused]] auto prev1 = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1);
292 JSTaggedValue result1 = BuiltinsDataView::GetInt16(ecmaRuntimeCallInfo1);
293 ASSERT_EQ(result1.GetRawData(), JSTaggedValue(-2048).GetRawData());
294 TestHelper::TearDownFrame(thread, prev1);
295 }
296
297 // new DataView(new ArrayBuffer(8), 0).SetUint8/GetUint32
HWTEST_F_L0(BuiltinsDataViewTest,GetUint32)298 HWTEST_F_L0(BuiltinsDataViewTest, GetUint32)
299 {
300 JSTaggedValue tagged = CreateBuiltinsDataView(thread, 8, 0);
301 JSHandle<JSDataView> view(thread, JSDataView::Cast(reinterpret_cast<TaggedObject *>(tagged.GetRawData())));
302 SetUint8(thread, view, 0, JSTaggedValue(127));
303 SetUint8(thread, view, 1, JSTaggedValue(255));
304 SetUint8(thread, view, 2, JSTaggedValue(255));
305 SetUint8(thread, view, 3, JSTaggedValue(255));
306
307 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
308 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
309 ecmaRuntimeCallInfo->SetThis(view.GetTaggedValue());
310 ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(0));
311 ecmaRuntimeCallInfo->SetCallArg(1, JSTaggedValue::False());
312
313 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
314 JSTaggedValue result = BuiltinsDataView::GetUint32(ecmaRuntimeCallInfo);
315 ASSERT_EQ(result.GetRawData(), JSTaggedValue(2147483647).GetRawData());
316 TestHelper::TearDownFrame(thread, prev);
317 }
318
319 // new DataView(new ArrayBuffer(8), 0).SetUint8/GetInt32
HWTEST_F_L0(BuiltinsDataViewTest,GetInt32)320 HWTEST_F_L0(BuiltinsDataViewTest, GetInt32)
321 {
322 JSTaggedValue tagged = CreateBuiltinsDataView(thread, 8, 0);
323 JSHandle<JSDataView> view(thread, JSDataView::Cast(reinterpret_cast<TaggedObject *>(tagged.GetRawData())));
324 SetUint8(thread, view, 0, JSTaggedValue(127));
325 SetUint8(thread, view, 1, JSTaggedValue(255));
326 SetUint8(thread, view, 2, JSTaggedValue(255));
327 SetUint8(thread, view, 3, JSTaggedValue(255));
328
329 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
330 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
331 ecmaRuntimeCallInfo->SetThis(view.GetTaggedValue());
332 ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(0));
333 ecmaRuntimeCallInfo->SetCallArg(1, JSTaggedValue::False());
334
335 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
336 JSTaggedValue result = BuiltinsDataView::GetInt32(ecmaRuntimeCallInfo);
337 ASSERT_EQ(result.GetRawData(), JSTaggedValue(2147483647).GetRawData());
338 TestHelper::TearDownFrame(thread, prev);
339 }
340
341 // new DataView(new ArrayBuffer(8), 0).SetUint8/GetInt8
HWTEST_F_L0(BuiltinsDataViewTest,GetInt8)342 HWTEST_F_L0(BuiltinsDataViewTest, GetInt8)
343 {
344 JSTaggedValue tagged = CreateBuiltinsDataView(thread, 8, 0);
345 JSHandle<JSDataView> view(thread, JSDataView::Cast(reinterpret_cast<TaggedObject *>(tagged.GetRawData())));
346 SetUint8(thread, view, 0, JSTaggedValue(255));
347
348 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
349 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
350 ecmaRuntimeCallInfo->SetThis(view.GetTaggedValue());
351 ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(0));
352
353 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
354 JSTaggedValue result = BuiltinsDataView::GetInt8(ecmaRuntimeCallInfo);
355 ASSERT_EQ(result.GetRawData(), JSTaggedValue(-1).GetRawData());
356 TestHelper::TearDownFrame(thread, prev);
357 }
358
359 // new DataView(new ArrayBuffer(8), 0).SetUint8/GetUint8
HWTEST_F_L0(BuiltinsDataViewTest,GetUint8)360 HWTEST_F_L0(BuiltinsDataViewTest, GetUint8)
361 {
362 JSTaggedValue tagged = CreateBuiltinsDataView(thread, 8, 0);
363 JSHandle<JSDataView> view(thread, JSDataView::Cast(reinterpret_cast<TaggedObject *>(tagged.GetRawData())));
364 SetUint8(thread, view, 0, JSTaggedValue(127));
365
366 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
367 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
368 ecmaRuntimeCallInfo->SetThis(view.GetTaggedValue());
369 ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(0));
370
371 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
372 JSTaggedValue result = BuiltinsDataView::GetUint8(ecmaRuntimeCallInfo);
373 ASSERT_EQ(result.GetRawData(), JSTaggedValue(127).GetRawData());
374 TestHelper::TearDownFrame(thread, prev);
375 }
376
377 // new DataView(new ArrayBuffer(8), 4).SetUint8/GetFloat32
HWTEST_F_L0(BuiltinsDataViewTest,GetFloat32)378 HWTEST_F_L0(BuiltinsDataViewTest, GetFloat32)
379 {
380 JSTaggedValue tagged = CreateBuiltinsDataView(thread, 8, 0);
381 JSHandle<JSDataView> view(thread, JSDataView::Cast(reinterpret_cast<TaggedObject *>(tagged.GetRawData())));
382 SetUint8(thread, view, 4, JSTaggedValue(75));
383 SetUint8(thread, view, 5, JSTaggedValue(75));
384 SetUint8(thread, view, 6, JSTaggedValue(75));
385 SetUint8(thread, view, 7, JSTaggedValue(75));
386
387 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
388 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
389 ecmaRuntimeCallInfo->SetThis(view.GetTaggedValue());
390 ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(4));
391 ecmaRuntimeCallInfo->SetCallArg(1, JSTaggedValue::False());
392
393 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
394 JSTaggedValue result = BuiltinsDataView::GetFloat32(ecmaRuntimeCallInfo);
395 ASSERT_EQ(result.GetRawData(), JSTaggedValue(static_cast<double>(13323083)).GetRawData());
396 TestHelper::TearDownFrame(thread, prev);
397 }
398
399 // new DataView(new ArrayBuffer(12), 4).SetUint8/GetFloat64
HWTEST_F_L0(BuiltinsDataViewTest,GetFloat64)400 HWTEST_F_L0(BuiltinsDataViewTest, GetFloat64)
401 {
402 JSTaggedValue tagged = CreateBuiltinsDataView(thread, 12, 0);
403 JSHandle<JSDataView> view(thread, JSDataView::Cast(reinterpret_cast<TaggedObject *>(tagged.GetRawData())));
404 SetUint8(thread, view, 4, JSTaggedValue(67));
405 SetUint8(thread, view, 5, JSTaggedValue(67));
406 SetUint8(thread, view, 6, JSTaggedValue(68));
407 SetUint8(thread, view, 7, JSTaggedValue(68));
408 SetUint8(thread, view, 8, JSTaggedValue(67));
409 SetUint8(thread, view, 9, JSTaggedValue(67));
410 SetUint8(thread, view, 10, JSTaggedValue(68));
411 SetUint8(thread, view, 11, JSTaggedValue(68));
412
413 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
414 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
415 ecmaRuntimeCallInfo->SetThis(view.GetTaggedValue());
416 ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(4));
417 ecmaRuntimeCallInfo->SetCallArg(1, JSTaggedValue::False());
418
419 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
420 JSTaggedValue result = BuiltinsDataView::GetFloat64(ecmaRuntimeCallInfo);
421 ASSERT_EQ(result.GetRawData(), JSTaggedValue(static_cast<double>(10846169068898440)).GetRawData());
422 TestHelper::TearDownFrame(thread, prev);
423 }
424
425 // new DataView(new ArrayBuffer(8), 0).SetUint32/GetUint32
HWTEST_F_L0(BuiltinsDataViewTest,SetUint32)426 HWTEST_F_L0(BuiltinsDataViewTest, SetUint32)
427 {
428 JSTaggedValue tagged = CreateBuiltinsDataView(thread, 8, 0);
429 JSHandle<JSDataView> view(thread, JSDataView::Cast(reinterpret_cast<TaggedObject *>(tagged.GetRawData())));
430 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 10);
431 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
432 ecmaRuntimeCallInfo->SetThis(view.GetTaggedValue());
433 ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(0));
434 ecmaRuntimeCallInfo->SetCallArg(1, JSTaggedValue(0x907f00f8));
435 ecmaRuntimeCallInfo->SetCallArg(2, JSTaggedValue::True());
436
437 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
438 JSTaggedValue result = BuiltinsDataView::SetUint32(ecmaRuntimeCallInfo);
439 ASSERT_EQ(result.GetRawData(), JSTaggedValue::VALUE_UNDEFINED);
440 TestHelper::TearDownFrame(thread, prev);
441
442 auto ecmaRuntimeCallInfo1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
443 ecmaRuntimeCallInfo1->SetFunction(JSTaggedValue::Undefined());
444 ecmaRuntimeCallInfo1->SetThis(view.GetTaggedValue());
445 ecmaRuntimeCallInfo1->SetCallArg(0, JSTaggedValue(0));
446 ecmaRuntimeCallInfo1->SetCallArg(1, JSTaggedValue::False());
447
448 [[maybe_unused]] auto prev1 = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1);
449 JSTaggedValue result1 = BuiltinsDataView::GetUint32(ecmaRuntimeCallInfo1);
450 ASSERT_EQ(result1.GetRawData(), JSTaggedValue(static_cast<double>(0xf8007f90)).GetRawData());
451 TestHelper::TearDownFrame(thread, prev1);
452 }
453
454 // new DataView(new ArrayBuffer(8), 0).SetInt32/GetInt32
HWTEST_F_L0(BuiltinsDataViewTest,SetInt32)455 HWTEST_F_L0(BuiltinsDataViewTest, SetInt32)
456 {
457 JSTaggedValue tagged = CreateBuiltinsDataView(thread, 8, 0);
458 JSHandle<JSDataView> view(thread, JSDataView::Cast(reinterpret_cast<TaggedObject *>(tagged.GetRawData())));
459 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 10);
460 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
461 ecmaRuntimeCallInfo->SetThis(view.GetTaggedValue());
462 ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(0));
463 ecmaRuntimeCallInfo->SetCallArg(1, JSTaggedValue(-1870724872));
464 ecmaRuntimeCallInfo->SetCallArg(2, JSTaggedValue::True());
465
466 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
467 JSTaggedValue result = BuiltinsDataView::SetInt32(ecmaRuntimeCallInfo);
468 ASSERT_EQ(result.GetRawData(), JSTaggedValue::VALUE_UNDEFINED);
469 TestHelper::TearDownFrame(thread, prev);
470
471 auto ecmaRuntimeCallInfo1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
472 ecmaRuntimeCallInfo1->SetFunction(JSTaggedValue::Undefined());
473 ecmaRuntimeCallInfo1->SetThis(view.GetTaggedValue());
474 ecmaRuntimeCallInfo1->SetCallArg(0, JSTaggedValue(0));
475 ecmaRuntimeCallInfo1->SetCallArg(1, JSTaggedValue::False());
476
477 [[maybe_unused]] auto prev1 = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1);
478 JSTaggedValue result1 = BuiltinsDataView::GetInt32(ecmaRuntimeCallInfo1);
479 ASSERT_EQ(result1.GetRawData(), JSTaggedValue(-134185072).GetRawData());
480 TestHelper::TearDownFrame(thread, prev1);
481 }
482
483 // new DataView(new ArrayBuffer(8), 0).SetInt8/GetUint8
HWTEST_F_L0(BuiltinsDataViewTest,SetInt8)484 HWTEST_F_L0(BuiltinsDataViewTest, SetInt8)
485 {
486 JSTaggedValue tagged = CreateBuiltinsDataView(thread, 8, 0);
487 JSHandle<JSDataView> view(thread, JSDataView::Cast(reinterpret_cast<TaggedObject *>(tagged.GetRawData())));
488 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
489 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
490 ecmaRuntimeCallInfo->SetThis(view.GetTaggedValue());
491 ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(0));
492 ecmaRuntimeCallInfo->SetCallArg(1, JSTaggedValue(-1));
493 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
494 JSTaggedValue result = BuiltinsDataView::SetInt8(ecmaRuntimeCallInfo);
495 ASSERT_EQ(result.GetRawData(), JSTaggedValue::VALUE_UNDEFINED);
496 TestHelper::TearDownFrame(thread, prev);
497
498 auto ecmaRuntimeCallInfo1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
499 ecmaRuntimeCallInfo1->SetFunction(JSTaggedValue::Undefined());
500 ecmaRuntimeCallInfo1->SetThis(view.GetTaggedValue());
501 ecmaRuntimeCallInfo1->SetCallArg(0, JSTaggedValue(0));
502
503 [[maybe_unused]] auto prev1 = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1);
504 JSTaggedValue result1 = BuiltinsDataView::GetUint8(ecmaRuntimeCallInfo1);
505 ASSERT_EQ(result1.GetRawData(), JSTaggedValue(255).GetRawData());
506 TestHelper::TearDownFrame(thread, prev1);
507 }
508
509 // new DataView(new ArrayBuffer(4), 0).SetFloat32/GetFloat32
HWTEST_F_L0(BuiltinsDataViewTest,SetFloat32)510 HWTEST_F_L0(BuiltinsDataViewTest, SetFloat32)
511 {
512 JSTaggedValue tagged = CreateBuiltinsDataView(thread, 4, 0);
513 JSHandle<JSDataView> view(thread, JSDataView::Cast(reinterpret_cast<TaggedObject *>(tagged.GetRawData())));
514 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 10);
515 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
516 ecmaRuntimeCallInfo->SetThis(view.GetTaggedValue());
517 ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(0));
518 ecmaRuntimeCallInfo->SetCallArg(1, JSTaggedValue(42));
519 ecmaRuntimeCallInfo->SetCallArg(2, JSTaggedValue::True());
520
521 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
522 JSTaggedValue result = BuiltinsDataView::SetFloat32(ecmaRuntimeCallInfo);
523 ASSERT_EQ(result.GetRawData(), JSTaggedValue::VALUE_UNDEFINED);
524 TestHelper::TearDownFrame(thread, prev);
525
526 auto ecmaRuntimeCallInfo1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
527 ecmaRuntimeCallInfo1->SetFunction(JSTaggedValue::Undefined());
528 ecmaRuntimeCallInfo1->SetThis(view.GetTaggedValue());
529 ecmaRuntimeCallInfo1->SetCallArg(0, JSTaggedValue(0));
530 ecmaRuntimeCallInfo1->SetCallArg(1, JSTaggedValue::False());
531
532 [[maybe_unused]] auto prev1 = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1);
533 JSTaggedValue result1 = BuiltinsDataView::GetFloat32(ecmaRuntimeCallInfo1);
534 ASSERT_EQ(result1.GetRawData(), JSTaggedValue(static_cast<double>(1.4441781973331565e-41)).GetRawData());
535 TestHelper::TearDownFrame(thread, prev1);
536 }
537
538 // new DataView(new ArrayBuffer(8), 0).SetFloat64/GetFloat64
HWTEST_F_L0(BuiltinsDataViewTest,SetFloat64)539 HWTEST_F_L0(BuiltinsDataViewTest, SetFloat64)
540 {
541 JSTaggedValue tagged = CreateBuiltinsDataView(thread, 8, 0);
542 JSHandle<JSDataView> view(thread, JSDataView::Cast(reinterpret_cast<TaggedObject *>(tagged.GetRawData())));
543 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 10);
544 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
545 ecmaRuntimeCallInfo->SetThis(view.GetTaggedValue());
546 ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(0));
547 ecmaRuntimeCallInfo->SetCallArg(1, JSTaggedValue(42));
548 ecmaRuntimeCallInfo->SetCallArg(2, JSTaggedValue::True());
549
550 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
551 JSTaggedValue result = BuiltinsDataView::SetFloat64(ecmaRuntimeCallInfo);
552 ASSERT_EQ(result.GetRawData(), JSTaggedValue::VALUE_UNDEFINED);
553 TestHelper::TearDownFrame(thread, prev);
554
555 auto ecmaRuntimeCallInfo1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
556 ecmaRuntimeCallInfo1->SetFunction(JSTaggedValue::Undefined());
557 ecmaRuntimeCallInfo1->SetThis(view.GetTaggedValue());
558 ecmaRuntimeCallInfo1->SetCallArg(0, JSTaggedValue(0));
559 ecmaRuntimeCallInfo1->SetCallArg(1, JSTaggedValue::False());
560
561 [[maybe_unused]] auto prev1 = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1);
562 JSTaggedValue result1 = BuiltinsDataView::GetFloat64(ecmaRuntimeCallInfo1);
563 ASSERT_EQ(result1.GetRawData(), JSTaggedValue(static_cast<double>(8.759e-320)).GetRawData());
564 TestHelper::TearDownFrame(thread, prev1);
565 }
566
HWTEST_F_L0(BuiltinsDataViewTest,GetBigInt64)567 HWTEST_F_L0(BuiltinsDataViewTest, GetBigInt64)
568 {
569 // case: Detached Buffer
570 JSTaggedValue tagged = CreateBuiltinsDataView(thread, 10, 2);
571 JSHandle<JSDataView> view(thread, JSDataView::Cast(reinterpret_cast<TaggedObject *>(tagged.GetRawData())));
572 JSTaggedValue tagged1 = CreateArrayBuffer(thread, 10);
573 JSHandle<JSArrayBuffer> arrBuf(thread, JSArrayBuffer::Cast(reinterpret_cast<TaggedObject *>(tagged1.GetRawData())));
574 arrBuf->SetArrayBufferData(thread, JSTaggedValue::Null());
575 view->SetViewedArrayBuffer(thread, arrBuf);
576 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
577 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
578 ecmaRuntimeCallInfo->SetThis(view.GetTaggedValue());
579 ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(0));
580 ecmaRuntimeCallInfo->SetCallArg(1, JSTaggedValue::False());
581
582 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
583 JSTaggedValue result = BuiltinsDataView::GetBigInt64(ecmaRuntimeCallInfo);
584 TestHelper::TearDownFrame(thread, prev);
585 EXPECT_TRUE(thread->HasPendingException());
586 EXPECT_EQ(result, JSTaggedValue::Exception());
587 thread->ClearException();
588 }
589
HWTEST_F_L0(BuiltinsDataViewTest,SetBigInt64)590 HWTEST_F_L0(BuiltinsDataViewTest, SetBigInt64)
591 {
592 JSTaggedValue tagged = CreateBuiltinsDataView(thread, 10, 2);
593 JSHandle<JSDataView> view(thread, JSDataView::Cast(reinterpret_cast<TaggedObject *>(tagged.GetRawData())));
594 JSTaggedValue tagged1 = CreateArrayBuffer(thread, 10);
595 JSHandle<JSArrayBuffer> arrBuf(thread, JSArrayBuffer::Cast(reinterpret_cast<TaggedObject *>(tagged1.GetRawData())));
596 arrBuf->SetArrayBufferData(thread, JSTaggedValue::Null());
597 view->SetViewedArrayBuffer(thread, arrBuf);
598 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
599 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
600 ecmaRuntimeCallInfo->SetThis(view.GetTaggedValue());
601 ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(0));
602 ecmaRuntimeCallInfo->SetCallArg(1, JSTaggedValue(10));
603
604 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
605 JSTaggedValue result = BuiltinsDataView::SetBigInt64(ecmaRuntimeCallInfo);
606 TestHelper::TearDownFrame(thread, prev);
607 EXPECT_TRUE(thread->HasPendingException());
608 EXPECT_EQ(result, JSTaggedValue::Exception());
609 thread->ClearException();
610 // case index < 0 is unreachable
611 }
612 } // namespace panda::test
613