• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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_ARK_NATIVE_ENGINE_IMPL_H
17 #define FOUNDATION_ACE_NAPI_NATIVE_ENGINE_IMPL_ARK_ARK_NATIVE_ENGINE_IMPL_H
18 
19 #include <unordered_map>
20 
21 #include "ark_headers.h"
22 #include "ecmascript/napi/include/jsnapi.h"
23 #include "ecmascript/napi/include/dfx_jsnapi.h"
24 #include "native_engine/native_engine_interface.h"
25 #include "ark_native_engine.h"
26 #include "native_value/ark_native_object.h"
27 
28 namespace panda::ecmascript {
29 struct JsFrameInfo {
30     std::string functionName;
31     std::string fileName;
32     std::string pos;
33     uintptr_t *nativePointer = nullptr;
34 };
35 }
36 using ArkJsFrameInfo = panda::ecmascript::JsFrameInfo;
37 
38 using panda::ecmascript::EcmaVM;
39 using panda::Local;
40 using panda::LocalScope;
41 using panda::JSValueRef;
42 using panda::JSNApi;
43 using panda::DFXJSNApi;
44 class SerializationData {
45 public:
SerializationData()46     SerializationData() : data_(nullptr), size_(0) {}
47     ~SerializationData() = default;
48 
GetData()49     uint8_t* GetData() const
50     {
51         return data_.get();
52     }
GetSize()53     size_t GetSize() const
54     {
55         return size_;
56     }
57 
58 private:
59     struct DataDeleter {
operatorDataDeleter60         void operator()(uint8_t* p) const
61         {
62             free(p);
63         }
64     };
65 
66     std::unique_ptr<uint8_t, DataDeleter> data_;
67     size_t size_;
68 };
69 
70 class ArkNativeEngineImpl : public NativeEngineInterface {
71 friend struct MoudleNameLocker;
72 public:
73     // ArkNativeEngineImpl constructor
74     ArkNativeEngineImpl(EcmaVM* vm, NativeEngine* engine, void* jsEngine);
75     // ArkNativeEngineImpl destructor
76     ~ArkNativeEngineImpl() override;
77 
GetEcmaVm()78     EcmaVM* GetEcmaVm() const
79     {
80         return vm_;
81     }
82 
83     void Loop(LoopMode mode, bool needSync = false) override;
84 
85     // Get global native object value
86     NativeValue* GetGlobal(NativeEngine* engine) override;
87     // Create native null value
88     NativeValue* CreateNull(NativeEngine* engine) override;
89     // Create native undefined value
90     NativeValue* CreateUndefined(NativeEngine* engine) override;
91     // Create native boolean value
92     NativeValue* CreateBoolean(NativeEngine* engine, bool value) override;
93     // Create number value by int32_t
94     NativeValue* CreateNumber(NativeEngine* engine, int32_t value) override;
95     // Create number value by uint32_t
96     NativeValue* CreateNumber(NativeEngine* engine, uint32_t value) override;
97     // Create native number value by int64_t
98     NativeValue* CreateNumber(NativeEngine* engine, int64_t value) override;
99     // Create native number value by double
100     NativeValue* CreateNumber(NativeEngine* engine, double value) override;
101     // Create native bigint value by int64_t
102     NativeValue* CreateBigInt(NativeEngine* engine, int64_t value) override;
103     // Create native bigint value by uint64_t
104     NativeValue* CreateBigInt(NativeEngine* engine, uint64_t value) override;
105     // Create native string value by const char pointer
106     NativeValue* CreateString(NativeEngine* engine, const char* value, size_t length) override;
107     // Create native string value by const char16_t pointer
108     NativeValue* CreateString16(NativeEngine* engine, const char16_t* value, size_t length) override;
109     // Create native symbol value
110     NativeValue* CreateSymbol(NativeEngine* engine, NativeValue* value) override;
111     // Create native value of external pointer
112     NativeValue* CreateExternal(NativeEngine* engine, void* value, NativeFinalize callback, void* hint,
113         size_t nativeBindingSize = 0) override;
114     // Create native object value
115     NativeValue* CreateObject(NativeEngine* engine) override;
116     // Create special native object value
117     NativeValue* CreateNativeBindingObject(NativeEngine* engine, void* detach, void* attach) override;
118     NativeValue* CreateNBObject(NativeEngine* engine, DetachCallback detach, AttachCallback attach) override;
119     // Create native function value
120     NativeValue* CreateFunction(
121         NativeEngine* engine, const char* name, size_t length, NativeCallback cb, void* value) override;
122     // Create native array value
123     NativeValue* CreateArray(NativeEngine* engine, size_t length) override;
124     // Create native array buffer value
125     NativeValue* CreateArrayBuffer(NativeEngine* engine, void** value, size_t length) override;
126     // Create native array buffer value of external
127     NativeValue* CreateArrayBufferExternal(
128         NativeEngine* engine, void* value, size_t length, NativeFinalize cb, void* hint) override;
129     NativeValue* CreateBuffer(NativeEngine* engine, void** value, size_t length) override;
130     NativeValue* CreateBufferCopy(NativeEngine* engine, void** value, size_t length, const void* data) override;
131     NativeValue* CreateBufferExternal(
132         NativeEngine* engine, void* value, size_t length, NativeFinalize cb, void* hint) override;
133     // Create native typed array value
134     NativeValue* CreateTypedArray(
135         NativeEngine* engine, NativeTypedArrayType type, NativeValue* value, size_t length, size_t offset) override;
136     // Create native data view value
137     NativeValue* CreateDataView(NativeEngine* engine, NativeValue* value, size_t length, size_t offset) override;
138     // Create native promise value
139     NativeValue* CreatePromise(NativeEngine* engine, NativeDeferred** deferred) override;
140     void SetPromiseRejectCallback(
141         NativeEngine* engine, NativeReference* rejectCallbackRef, NativeReference* checkCallbackRef) override;
142     static void PromiseRejectCallback(void* values);
143     // Create native error value
144     NativeValue* CreateError(NativeEngine* engine, NativeValue* code, NativeValue* message) override;
145     bool InitTaskPoolThread(NativeEngine* engine, NapiConcurrentCallback callback) override;
146     bool InitTaskPoolFunc(NativeEngine* engine, NativeValue* func) override;
147     // Call function
148     NativeValue* CallFunction(NativeEngine* engine, NativeValue* thisVar, NativeValue* function,
149         NativeValue* const *argv, size_t argc) override;
150     // Run script
151     NativeValue* RunScript(NativeEngine* engine, NativeValue* script) override;
152     NativeValue* RunScriptPath(NativeEngine* engine, const char* path) override;
153 
154     NativeValue* RunScriptBuffer(NativeEngine* engine, const char* path,
155                                  std::vector<uint8_t>& buffer, bool isBundle) override;
156 
157     // Run buffer script
158     NativeValue* RunBufferScript(NativeEngine* engine, std::vector<uint8_t>& buffer) override;
159     // Run actor
160     NativeValue* RunActor(NativeEngine* engine, std::vector<uint8_t>& buffer, const char* descriptor) override;
161     // Set lib path
162     void SetPackagePath(const std::string appLibPathKey, const std::vector<std::string>& packagePath);
163     // Define native class
164     NativeValue* DefineClass(NativeEngine* engine, const char* name, NativeCallback callback, void* data,
165         const NativePropertyDescriptor* properties, size_t length) override;
166     // Create instance by defined class
167     NativeValue* CreateInstance(
168         NativeEngine* engine, NativeValue* constructor, NativeValue* const *argv, size_t argc) override;
169 
170     // Create native reference
171     NativeReference* CreateReference(NativeEngine* engine, NativeValue* value, uint32_t initialRefcount,
172         NativeFinalize callback = nullptr, void* data = nullptr, void* hint = nullptr) override;
173     bool IsExceptionPending() const override;
174     NativeValue* GetAndClearLastException(NativeEngine* engine) override;
175     // Throw exception
176     bool Throw(NativeValue* error) override;
177     // Throw exception
178     bool Throw(NativeEngine* engine, NativeErrorType type, const char* code, const char* message) override;
179 
180     void* CreateRuntime(NativeEngine* engine) override;
181     NativeValue* Serialize(NativeEngine* context, NativeValue* value, NativeValue* transfer) override;
182     NativeValue* Deserialize(NativeEngine* engine, NativeEngine* context, NativeValue* recorder) override;
183     void DeleteSerializationData(NativeValue* value) const override;
184     NativeValue* LoadModule(NativeEngine* engine, NativeValue* str, const std::string& fileName) override;
185     NativeValue* LoadArkModule(NativeEngine* engine, const char* str, int32_t len, const std::string& fileName);
186 
187     static NativeValue* ArkValueToNativeValue(ArkNativeEngine* engine, Local<JSValueRef> value);
188 
189     NativeValue* ValueToNativeValue(NativeEngine* engine, JSValueWrapper& value) override;
190 
191     bool ExecuteJsBin(const std::string& fileName);
192     panda::Local<panda::ObjectRef> LoadModuleByName(ArkNativeEngine* engine, const std::string& moduleName,
193         bool isAppModule, const std::string& param, const std::string& instanceName, void* instance,
194         const std::string& path = "");
195 
196     virtual bool TriggerFatalException(NativeValue* error) override;
197     NativeValue* CreateDate(NativeEngine* engine, double value) override;
198     NativeValue* CreateBigWords(NativeEngine* engine, int sign_bit, size_t word_count, const uint64_t* words) override;
199     bool AdjustExternalMemory(int64_t ChangeInBytes, int64_t* AdjustedValue) override;
200 
201     // Detect performance to obtain cpuprofiler file
202     void StartCpuProfiler(const std::string& fileName = "") override;
203     void StopCpuProfiler() override;
204 
205     void ResumeVM() override;
206     bool SuspendVM() override;
207     bool IsSuspended() override;
208     bool CheckSafepoint() override;
209 
210     void DumpHeapSnapshot(const std::string& path, bool isVmMode = true,
211         DumpFormat dumpFormat = DumpFormat::JSON) override;
212     void DumpHeapSnapshotExt(bool isVmMode = true, DumpFormat dumpFormat = DumpFormat::JSON,
213         bool isPrivate = false) override;
214     bool BuildNativeAndJsStackTrace(std::string& stackTraceStr) override;
215     bool BuildJsStackTrace(std::string& stackTraceStr) override;
216     bool BuildJsStackInfoList(uint32_t tid, std::vector<JsFrameInfo>& jsFrames) override;
217     bool DeleteWorker(NativeEngine* hostEngine, NativeEngine* workerEngine) override;
218     bool StartHeapTracking(double timeInterval, bool isVmMode = true) override;
219     bool StopHeapTracking(const std::string& filePath) override;
220 
221     void PrintStatisticResult() override;
222     void StartRuntimeStat() override;
223     void StopRuntimeStat() override;
224     size_t GetArrayBufferSize() override;
225     size_t GetHeapTotalSize() override;
226     size_t GetHeapUsedSize() override;
227     void NotifyApplicationState(bool inBackground) override;
228     void NotifyIdleTime(int idleMicroSec) override;
229     virtual void NotifyMemoryPressure(bool inHighMemoryPressure = false) override;
230 
231     void RegisterUncaughtExceptionHandler(UncaughtExceptionCallback callback) override;
232     void HandleUncaughtException(NativeEngine* engine) override;
233     bool HasPendingException() override;
234 
GetPromiseRejectCallBackRef()235     NativeReference* GetPromiseRejectCallBackRef()
236     {
237         return promiseRejectCallbackRef_;
238     }
GetConcurrentCallbackFunc()239     NapiConcurrentCallback GetConcurrentCallbackFunc()
240     {
241         return concurrentCallbackFunc_;
242     }
243 
GetCheckCallbackRef()244     NativeReference* GetCheckCallbackRef()
245     {
246         return checkCallbackRef_;
247     }
248     panda::Local<panda::ObjectRef> GetModuleFromName(NativeEngine* engine,
249         const std::string& moduleName, bool isAppModule, const std::string& id, const std::string& param,
250         const std::string& instanceName, void** instance);
251 
252     static bool napiProfilerEnabled;
253 
254     // debugger
255     bool IsMixedDebugEnabled();
256     void NotifyNativeCalling(const void *nativeAddressess);
257 
258 private:
259     static NativeEngine* CreateRuntimeFunc(NativeEngine* engine, void* jsEngine);
260 
261     EcmaVM* vm_ = nullptr;
262     panda::LocalScope topScope_;
263     NativeReference* promiseRejectCallbackRef_ { nullptr };
264     NativeReference* checkCallbackRef_ { nullptr };
265     std::unordered_map<NativeModule*, panda::Global<panda::JSValueRef>> loadedModules_;
266     UncaughtExceptionCallback uncaughtExceptionCallback_ { nullptr };
267     NapiConcurrentCallback concurrentCallbackFunc_ { nullptr };
268     inline void SetModuleName(ArkNativeObject *nativeObj, std::string moduleName);
269     static bool napiProfilerParamReaded;
270     static std::string tempModuleName_;
271     NativeValue* lastException_ = nullptr;
272 };
273 
274 #endif /* FOUNDATION_ACE_NAPI_NATIVE_ENGINE_IMPL_ARK_ARK_NATIVE_ENGINE_IMPL_H */
275