• 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 #ifndef FOUNDATION_ACE_NAPI_NATIVE_ENGINE_IMPL_ARK_NATIVE_VALUE_ARK_NATIVE_FUNCTION_H
17 #define FOUNDATION_ACE_NAPI_NATIVE_ENGINE_IMPL_ARK_NATIVE_VALUE_ARK_NATIVE_FUNCTION_H
18 
19 #include "ark_native_object.h"
20 
21 #ifdef ENABLE_HITRACE
22 #include <sys/prctl.h>
23 #include "hitrace_meter.h"
24 #endif
25 
26 class ArkNativeFunction : public ArkNativeObject, public NativeFunction {
27 public:
28     ArkNativeFunction(ArkNativeEngine* engine, Local<JSValueRef> value);
29     ArkNativeFunction(ArkNativeEngine* engine, const char* name, size_t length, NativeCallback cb, void* value);
30     ArkNativeFunction(ArkNativeEngine* engine, const char* name, NativeCallback cb, void* value);  // Used for class
31     ~ArkNativeFunction() override;
32 
33     void* GetInterface(int interfaceId) override;
34 
35     static void* GetNativePtrCallBack(void* data);
36 
37     NativeValue* GetFunctionPrototype() override;
38 
39     std::string GetSourceCodeInfo(ErrorPos pos) override;
40 
41 #ifdef ENABLE_CONTAINER_SCOPE
GetScopeId()42     inline int32_t GetScopeId()
43     {
44         return scopeId_;
45     }
46 #endif
47 
48 private:
49     static Local<JSValueRef> NativeFunctionCallBack(panda::JsiRuntimeCallInfo *info);
50 #ifdef ENABLE_CONTAINER_SCOPE
51     int32_t scopeId_ = -1;
52 #endif
53 
StartNapiProfilerTrace(panda::JsiRuntimeCallInfo * runtimeInfo)54     static inline void StartNapiProfilerTrace(panda::JsiRuntimeCallInfo *runtimeInfo)
55     {
56 #ifdef ENABLE_HITRACE
57         if (ArkNativeEngine::napiProfilerEnabled) {
58             EcmaVM *vm = runtimeInfo->GetVM();
59             LocalScope scope(vm);
60             Local<panda::FunctionRef> fn = runtimeInfo->GetFunctionRef();
61             Local<panda::StringRef> nameRef = fn->GetName(vm);
62             char threadName[128];
63             prctl(PR_GET_NAME, threadName);
64             StartTraceArgs(HITRACE_TAG_ACE, "Napi called:%s, tname:%s", nameRef->ToString().c_str(), threadName);
65         }
66 #endif
67     }
68 
FinishNapiProfilerTrace()69     static inline void FinishNapiProfilerTrace()
70     {
71 #ifdef ENABLE_HITRACE
72         if (ArkNativeEngine::napiProfilerEnabled) {
73             FinishTrace(HITRACE_TAG_ACE);
74         }
75 #endif
76     }
77 };
78 
79 #endif /* FOUNDATION_ACE_NAPI_NATIVE_ENGINE_IMPL_ARK_NATIVE_VALUE_ARK_NATIVE_FUNCTION_H */
80