• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_arraybuffer.h"
17 
18 #include "ecmascript/ecma_runtime_call_info.h"
19 #include "ecmascript/ecma_vm.h"
20 #include "ecmascript/global_env.h"
21 
22 #include "ecmascript/js_arraybuffer.h"
23 #include "ecmascript/js_handle.h"
24 #include "ecmascript/js_tagged_value.h"
25 #include "ecmascript/tests/test_helper.h"
26 
27 using namespace panda::ecmascript;
28 using namespace panda::ecmascript::builtins;
29 
30 namespace panda::test {
31 class BuiltinsArrayBufferTest : public testing::Test {
32 public:
SetUpTestCase()33     static void SetUpTestCase()
34     {
35         GTEST_LOG_(INFO) << "SetUpTestCase";
36     }
37 
TearDownTestCase()38     static void TearDownTestCase()
39     {
40         GTEST_LOG_(INFO) << "TearDownCase";
41     }
42 
SetUp()43     void SetUp() override
44     {
45         TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
46     }
47 
TearDown()48     void TearDown() override
49     {
50         TestHelper::DestroyEcmaVMWithScope(instance, scope);
51     }
52 
53     EcmaVM *instance {nullptr};
54     EcmaHandleScope *scope {nullptr};
55     JSThread *thread {nullptr};
56 };
57 
CreateBuiltinsArrayBuffer(JSThread * thread,int32_t length)58 JSTaggedValue CreateBuiltinsArrayBuffer(JSThread *thread, int32_t length)
59 {
60     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
61     JSHandle<JSFunction> arrayBuffer(thread, env->GetArrayBufferFunction().GetTaggedValue());
62     JSHandle<JSObject> globalObject(thread, env->GetGlobalObject());
63     // 6 : test case
64     auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, arrayBuffer.GetTaggedValue(), 6);
65     ecmaRuntimeCallInfo->SetFunction(arrayBuffer.GetTaggedValue());
66     ecmaRuntimeCallInfo->SetThis(globalObject.GetTaggedValue());
67     ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(length));
68 
69     [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
70     JSTaggedValue result = BuiltinsArrayBuffer::ArrayBufferConstructor(ecmaRuntimeCallInfo);
71     TestHelper::TearDownFrame(thread, prev);
72     return result;
73 }
74 
75 // new ArrayBuffer(8)
HWTEST_F_L0(BuiltinsArrayBufferTest,Constructor1)76 HWTEST_F_L0(BuiltinsArrayBufferTest, Constructor1)
77 {
78     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
79     JSHandle<JSFunction> arrayBuffer(thread, env->GetArrayBufferFunction().GetTaggedValue());
80     JSHandle<JSObject> globalObject(thread, env->GetGlobalObject());
81     auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, arrayBuffer.GetTaggedValue(), 6);
82     ecmaRuntimeCallInfo->SetFunction(arrayBuffer.GetTaggedValue());
83     ecmaRuntimeCallInfo->SetThis(globalObject.GetTaggedValue());
84     ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(static_cast<int32_t>(8)));
85 
86     [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
87     JSTaggedValue result = BuiltinsArrayBuffer::ArrayBufferConstructor(ecmaRuntimeCallInfo);
88     ASSERT_TRUE(result.IsECMAObject());
89     TestHelper::TearDownFrame(thread, prev);
90 }
91 
92 // (new ArrayBuffer(5)).byteLength
HWTEST_F_L0(BuiltinsArrayBufferTest,byteLength1)93 HWTEST_F_L0(BuiltinsArrayBufferTest, byteLength1)
94 {
95     JSTaggedValue tagged = CreateBuiltinsArrayBuffer(thread, 5);
96     JSHandle<JSArrayBuffer> arrBuf(thread, JSArrayBuffer::Cast(reinterpret_cast<TaggedObject *>(tagged.GetRawData())));
97     auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
98     ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
99     ecmaRuntimeCallInfo->SetThis(arrBuf.GetTaggedValue());
100 
101     [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
102     JSTaggedValue result = BuiltinsArrayBuffer::GetByteLength(ecmaRuntimeCallInfo);
103     ASSERT_EQ(result.GetRawData(), JSTaggedValue(5).GetRawData());
104     TestHelper::TearDownFrame(thread, prev);
105 }
106 
107 // (new ArrayBuffer(10)).slice(1, 5).bytelength
HWTEST_F_L0(BuiltinsArrayBufferTest,slice1)108 HWTEST_F_L0(BuiltinsArrayBufferTest, slice1)
109 {
110     JSTaggedValue tagged = CreateBuiltinsArrayBuffer(thread, 10);
111     JSHandle<JSArrayBuffer> arrBuf(thread, JSArrayBuffer::Cast(reinterpret_cast<TaggedObject *>(tagged.GetRawData())));
112     auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
113     ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
114     ecmaRuntimeCallInfo->SetThis(arrBuf.GetTaggedValue());
115     ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(static_cast<int32_t>(1)));
116     ecmaRuntimeCallInfo->SetCallArg(1, JSTaggedValue(static_cast<int32_t>(5)));
117 
118     [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
119     JSTaggedValue result1 = BuiltinsArrayBuffer::Slice(ecmaRuntimeCallInfo);
120     TestHelper::TearDownFrame(thread, prev);
121     JSHandle<JSArrayBuffer> arrBuf1(thread,
122                                      JSArrayBuffer::Cast(reinterpret_cast<TaggedObject *>(result1.GetRawData())));
123     auto ecmaRuntimeCallInfo1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
124     ecmaRuntimeCallInfo1->SetFunction(JSTaggedValue::Undefined());
125     ecmaRuntimeCallInfo1->SetThis(arrBuf1.GetTaggedValue());
126     prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1);
127     JSTaggedValue result2 = BuiltinsArrayBuffer::GetByteLength(ecmaRuntimeCallInfo1);
128 
129     ASSERT_EQ(result2.GetRawData(), JSTaggedValue(4).GetRawData());
130     TestHelper::TearDownFrame(thread, prev);
131 }
132 }  // namespace panda::test
133