mirror of
https://github.com/openharmony/ark_js_runtime.git
synced 2026-08-27 02:31:17 -04:00
f11810349e
Signed-off-by: g00416891 <guobingbing3@huawei.com> Change-Id: I466a4107e6e073ffecea12e6986c112731d2d3bf
129 lines
5.6 KiB
C++
129 lines
5.6 KiB
C++
/*
|
|
* Copyright (c) 2022 Huawei Device Co., Ltd.
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
#include "ecmascript/builtins/builtins_sharedarraybuffer.h"
|
|
#include "ecmascript/ecma_runtime_call_info.h"
|
|
#include "ecmascript/ecma_vm.h"
|
|
#include "ecmascript/global_env.h"
|
|
|
|
#include "ecmascript/js_arraybuffer.h"
|
|
#include "ecmascript/js_handle.h"
|
|
#include "ecmascript/js_tagged_value.h"
|
|
#include "ecmascript/tests/test_helper.h"
|
|
|
|
using namespace panda::ecmascript;
|
|
using namespace panda::ecmascript::builtins;
|
|
|
|
namespace panda::test {
|
|
class BuiltinsSharedArrayBufferTest : public testing::Test {
|
|
public:
|
|
static void SetUpTestCase()
|
|
{
|
|
GTEST_LOG_(INFO) << "SetUpTestCase";
|
|
}
|
|
|
|
static void TearDownTestCase()
|
|
{
|
|
GTEST_LOG_(INFO) << "TearDownCase";
|
|
}
|
|
|
|
void SetUp() override
|
|
{
|
|
TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
|
|
}
|
|
|
|
void TearDown() override
|
|
{
|
|
TestHelper::DestroyEcmaVMWithScope(instance, scope);
|
|
}
|
|
|
|
EcmaVM *instance {nullptr};
|
|
EcmaHandleScope *scope {nullptr};
|
|
JSThread *thread {nullptr};
|
|
};
|
|
|
|
JSTaggedValue CreateBuiltinsSharedArrayBuffer(JSThread *thread, int32_t length)
|
|
{
|
|
JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
|
|
JSHandle<JSFunction> sharedArrayBuffer(thread, env->GetSharedArrayBufferFunction().GetTaggedValue());
|
|
JSHandle<JSObject> globalObject(thread, env->GetGlobalObject());
|
|
// 6 : test case
|
|
auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, sharedArrayBuffer.GetTaggedValue(), 6);
|
|
ecmaRuntimeCallInfo->SetFunction(sharedArrayBuffer.GetTaggedValue());
|
|
ecmaRuntimeCallInfo->SetThis(globalObject.GetTaggedValue());
|
|
ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(length));
|
|
|
|
[[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
|
|
JSTaggedValue result = BuiltinsSharedArrayBuffer::SharedArrayBufferConstructor(ecmaRuntimeCallInfo);
|
|
TestHelper::TearDownFrame(thread, prev);
|
|
return result;
|
|
}
|
|
|
|
// new ArrayBuffer(20)
|
|
HWTEST_F_L0(BuiltinsSharedArrayBufferTest, Constructor1)
|
|
{
|
|
JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
|
|
JSHandle<JSFunction> sharedArrayBuffer(thread, env->GetSharedArrayBufferFunction().GetTaggedValue());
|
|
JSHandle<JSObject> globalObject(thread, env->GetGlobalObject());
|
|
auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, sharedArrayBuffer.GetTaggedValue(), 6);
|
|
ecmaRuntimeCallInfo->SetFunction(sharedArrayBuffer.GetTaggedValue());
|
|
ecmaRuntimeCallInfo->SetThis(globalObject.GetTaggedValue());
|
|
ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(static_cast<int32_t>(20)));
|
|
|
|
[[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
|
|
JSTaggedValue result = BuiltinsSharedArrayBuffer::SharedArrayBufferConstructor(ecmaRuntimeCallInfo);
|
|
ASSERT_TRUE(result.IsECMAObject());
|
|
}
|
|
|
|
// (new ArrayBuffer(18)).byteLength
|
|
HWTEST_F_L0(BuiltinsSharedArrayBufferTest, byteLength1)
|
|
{
|
|
JSTaggedValue tagged = CreateBuiltinsSharedArrayBuffer(thread, 18);
|
|
JSHandle<JSArrayBuffer> arrBuf(thread, JSArrayBuffer::Cast(reinterpret_cast<TaggedObject *>(tagged.GetRawData())));
|
|
auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
|
|
ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
|
|
ecmaRuntimeCallInfo->SetThis(arrBuf.GetTaggedValue());
|
|
|
|
[[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
|
|
JSTaggedValue result = BuiltinsSharedArrayBuffer::GetByteLength(ecmaRuntimeCallInfo);
|
|
ASSERT_EQ(result.GetRawData(), JSTaggedValue(18).GetRawData());
|
|
}
|
|
|
|
// (new ArrayBuffer(10)).slice(1, 5).bytelength
|
|
HWTEST_F_L0(BuiltinsSharedArrayBufferTest, slice1)
|
|
{
|
|
JSTaggedValue tagged = CreateBuiltinsSharedArrayBuffer(thread, 10);
|
|
JSHandle<JSArrayBuffer> arrBuf(thread, JSArrayBuffer::Cast(reinterpret_cast<TaggedObject *>(tagged.GetRawData())));
|
|
auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
|
|
ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
|
|
ecmaRuntimeCallInfo->SetThis(arrBuf.GetTaggedValue());
|
|
ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(static_cast<int32_t>(1)));
|
|
ecmaRuntimeCallInfo->SetCallArg(1, JSTaggedValue(static_cast<int32_t>(5)));
|
|
|
|
[[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
|
|
JSTaggedValue result1 = BuiltinsSharedArrayBuffer::Slice(ecmaRuntimeCallInfo);
|
|
TestHelper::TearDownFrame(thread, prev);
|
|
JSHandle<JSArrayBuffer> arrBuf1(thread,
|
|
JSArrayBuffer::Cast(reinterpret_cast<TaggedObject *>(result1.GetRawData())));
|
|
auto ecmaRuntimeCallInfo1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
|
|
ecmaRuntimeCallInfo1->SetFunction(JSTaggedValue::Undefined());
|
|
ecmaRuntimeCallInfo1->SetThis(arrBuf1.GetTaggedValue());
|
|
prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1);
|
|
JSTaggedValue result2 = BuiltinsSharedArrayBuffer::GetByteLength(ecmaRuntimeCallInfo1);
|
|
|
|
ASSERT_EQ(result2.GetRawData(), JSTaggedValue(4).GetRawData());
|
|
}
|
|
} // namespace panda::test
|