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