• 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/ecma_string.h"
17 #include "ecmascript/ecma_vm.h"
18 #include "ecmascript/global_env.h"
19 #include "ecmascript/js_function.h"
20 #include "ecmascript/js_handle.h"
21 #include "ecmascript/js_object-inl.h"
22 #include "ecmascript/object_factory.h"
23 #include "ecmascript/tests/test_helper.h"
24 
25 using namespace panda::ecmascript;
26 
27 using FunctionPtr = void (*)(panda::ecmascript::JSHandle<EcmaString> &);
28 
29 namespace panda::test {
30 class NativePointerTest : public BaseTestWithScope<false> {
31 };
32 
HWTEST_F_L0(NativePointerTest,Print)33 HWTEST_F_L0(NativePointerTest, Print)
34 {
35     // mock object needed in test
36     char array[] = "Hello World!";
37     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
38     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
39     JSHandle<EcmaString> str = factory->NewFromASCII(array);
40     EXPECT_TRUE(*str != nullptr);
41 
42     JSHandle<JSFunction> jsFunction = factory->NewJSFunction(env);
43     EXPECT_TRUE(*jsFunction != nullptr);
44 
45     JSHandle<Method> target = factory->NewMethodForNativeFunction(nullptr);
46     jsFunction->SetMethod(thread, target.GetTaggedValue());
47 
48     // run cpp methed 'Print'
49     ASSERT_EQ(Method::Cast(target.GetTaggedValue().GetTaggedObject()), jsFunction->GetCallTarget());
50 }
51 
HWTEST_F_L0(NativePointerTest,ToString)52 HWTEST_F_L0(NativePointerTest, ToString)
53 {
54     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
55     size_t length = 4;
56     uint8_t *data = static_cast<uint8_t *>(thread->GetEcmaVM()->GetNativeAreaAllocator()->AllocateBuffer(length));
57     if (memset_s(data, length, 0, length) != EOK) {
58         LOG_ECMA(FATAL) << "this branch is unreachable";
59         UNREACHABLE();
60     }
61     std::stringstream expected;
62     void *formBuffer = thread->GetEcmaVM()->GetNativeAreaAllocator()->AllocateBuffer(length);
63     JSHandle<JSNativePointer> fromNativePointer =
64         factory->NewJSNativePointer(formBuffer, nullptr, reinterpret_cast<void *>(data));
65     expected << "[External: " << std::hex << formBuffer << "]";
66     EcmaStringAccessor actual = EcmaStringAccessor(fromNativePointer->ToString(thread));
67     EXPECT_EQ(actual.ToCString().c_str(), expected.str());
68 }
69 }  // namespace panda::test
70