• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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 ECMASCRIPT_NAPI_INCLUDE_JSNAPI_H
17 #define ECMASCRIPT_NAPI_INCLUDE_JSNAPI_H
18 
19 #include <cassert>
20 #include <cstdint>
21 #include <functional>
22 #include <memory>
23 #include <shared_mutex>
24 #include <string>
25 #include <vector>
26 #include <map>
27 #include <sys/time.h>
28 
29 #include "common_components/log/log_base.h"
30 #include "ecmascript/base/aligned_struct.h"
31 #include "ecmascript/base/config.h"
32 #include "ecmascript/napi/include/jsnapi_expo.h"
33 #ifndef NDEBUG
34 #include "libpandabase/utils/debug.h"
35 #endif
36 
37 #ifdef ERROR
38 #undef ERROR
39 #endif
40 
41 namespace panda {
42 class JSNApiHelper;
43 class EscapeLocalScope;
44 class PromiseRejectInfo;
45 template<typename T>
46 class CopyableGlobal;
47 template<typename T>
48 class Global;
49 class JSNApi;
50 template<typename T>
51 class Local;
52 class JSValueRef;
53 class PrimitiveRef;
54 class ArrayRef;
55 class BigIntRef;
56 class StringRef;
57 class ObjectRef;
58 class FunctionRef;
59 class NumberRef;
60 class BooleanRef;
61 class NativePointerRef;
62 class JsiRuntimeCallInfo;
63 namespace test {
64 class JSNApiTests;
65 }  // namespace test
66 class BufferRef;
67 namespace ecmascript {
68 class EcmaVM;
69 class JSTaggedValue;
70 class JSRuntimeOptions;
71 class JSThread;
72 struct EcmaRuntimeCallInfo;
73 static constexpr uint32_t DEFAULT_GC_POOL_SIZE = 256 * 1024 * 1024;
74 namespace base {
75 template<size_t ElementAlign, typename... Ts>
76 struct AlignedStruct;
77 struct AlignedPointer;
78 }
79 }  // namespace ecmascript
80 
81 using WeakRefClearCallBack = void (*)(void *);
82 using EcmaVM = ecmascript::EcmaVM;
83 using JSThread = ecmascript::JSThread;
84 using JSTaggedType = uint64_t;
85 using ConcurrentCallback = void (*)(Local<JSValueRef> result, bool success, void *taskInfo, void *data);
86 using SourceMapTranslateCallback = std::function<bool(std::string& url, int& line, int& column,
87     std::string &packageName)>;
88 using DeviceDisconnectCallback = std::function<bool()>;
89 
90 static constexpr size_t DEFAULT_GC_THREAD_NUM = 7;
91 static constexpr size_t DEFAULT_LONG_PAUSE_TIME = 40;
92 
93 class PUBLIC_API RegExpRef : public ObjectRef {
94 public:
95     Local<StringRef> GetOriginalSource(const EcmaVM *vm);
96     std::string GetOriginalFlags(const EcmaVM *vm);
97     Local<JSValueRef> IsGlobal(const EcmaVM *vm);
98     Local<JSValueRef> IsIgnoreCase(const EcmaVM *vm);
99     Local<JSValueRef> IsMultiline(const EcmaVM *vm);
100     Local<JSValueRef> IsDotAll(const EcmaVM *vm);
101     Local<JSValueRef> IsUtf16(const EcmaVM *vm);
102     Local<JSValueRef> IsStick(const EcmaVM *vm);
103 };
104 
105 class PUBLIC_API GeneratorFunctionRef : public ObjectRef {
106 public:
107     bool IsGenerator(const EcmaVM *vm);
108 };
109 
110 class PUBLIC_API GeneratorObjectRef : public ObjectRef {
111 public:
112     Local<JSValueRef> GetGeneratorState(const EcmaVM *vm);
113     Local<JSValueRef> GetGeneratorFunction(const EcmaVM *vm);
114     Local<JSValueRef> GetGeneratorReceiver(const EcmaVM *vm);
115 };
116 
117 class PUBLIC_API CollatorRef : public ObjectRef {
118 public:
119     Local<JSValueRef> GetCompareFunction(const EcmaVM *vm);
120 };
121 
122 class PUBLIC_API DataTimeFormatRef : public ObjectRef {
123 public:
124     Local<JSValueRef> GetFormatFunction(const EcmaVM *vm);
125 };
126 
127 class PUBLIC_API NumberFormatRef : public ObjectRef {
128 public:
129     Local<JSValueRef> GetFormatFunction(const EcmaVM *vm);
130 };
131 
132 class PUBLIC_API JSON {
133 public:
134     static Local<JSValueRef> Parse(const EcmaVM *vm, Local<StringRef> string);
135     static Local<JSValueRef> Stringify(const EcmaVM *vm, Local<JSValueRef> json);
136 };
137 
138 using LOG_PRINT = int (*)(int id, int level, const char *tag, const char *fmt, const char *message);
139 
140 class PUBLIC_API RuntimeOption {
141 public:
142     enum class PUBLIC_API GC_TYPE : uint8_t { EPSILON, GEN_GC, STW };
143     using LOG_LEVEL = common::LOG_LEVEL;
144 
145     // This enum should follow the same value as defined in the BMS subsystem.
146     // Refer to the specification in aot-guide_zh.md.
147     enum class AOTCompileStatus {
148         NOT_COMPILED = 0,
149         COMPILE_SUCCESS = 1,
150         COMPILE_FAILED = 2,
151         COMPILE_CRASH = 3,
152         COMPILE_CANCELLED = 4,
153     };
154 
SetGcType(GC_TYPE type)155     void SetGcType(GC_TYPE type)
156     {
157         gcType_ = type;
158     }
159 
SetGcPoolSize(uint32_t size)160     void SetGcPoolSize(uint32_t size)
161     {
162         gcPoolSize_ = size;
163     }
164 
SetLogLevel(LOG_LEVEL logLevel)165     void SetLogLevel(LOG_LEVEL logLevel)
166     {
167         logLevel_ = logLevel;
168     }
169 
SetLogBufPrint(LOG_PRINT out)170     void SetLogBufPrint(LOG_PRINT out)
171     {
172         logBufPrint_ = out;
173     }
174 
SetDebuggerLibraryPath(const std::string & path)175     void SetDebuggerLibraryPath(const std::string &path)
176     {
177         debuggerLibraryPath_ = path;
178     }
179 
SetEnableArkTools(bool value)180     void SetEnableArkTools(bool value)
181     {
182         enableArkTools_ = value;
183     }
184 
SetEnableCpuprofiler(bool value)185     void SetEnableCpuprofiler(bool value)
186     {
187         enableCpuprofiler_ = value;
188     }
189 
SetArkProperties(int prop)190     void SetArkProperties(int prop)
191     {
192         arkProperties_ = prop;
193     }
194 
SetArkBundleName(const std::string & bundleName)195     void SetArkBundleName(const std::string &bundleName)
196     {
197         arkBundleName_ = bundleName;
198     }
199 
SetMemConfigProperty(std::string configProperty)200     void SetMemConfigProperty(std::string configProperty)
201     {
202         memConfigProperty_ = configProperty;
203     }
204 
SetGcThreadNum(size_t num)205     void SetGcThreadNum(size_t num)
206     {
207         gcThreadNum_ = num;
208     }
209 
SetLongPauseTime(size_t time)210     void SetLongPauseTime(size_t time)
211     {
212         longPauseTime_ = time;
213     }
214 
SetEnableAsmInterpreter(bool value)215     void SetEnableAsmInterpreter(bool value)
216     {
217         enableAsmInterpreter_ = value;
218     }
219 
SetEnableBuiltinsLazy(bool value)220     void SetEnableBuiltinsLazy(bool value)
221     {
222         enableBuiltinsLazy_ = value;
223     }
224 
SetAsmOpcodeDisableRange(const std::string & value)225     void SetAsmOpcodeDisableRange(const std::string &value)
226     {
227         asmOpcodeDisableRange_ = value;
228     }
229 
SetIsWorker()230     void SetIsWorker()
231     {
232         isWorker_ = true;
233     }
234 
GetIsWorker()235     bool GetIsWorker() const
236     {
237         return isWorker_;
238     }
239 
SetIsRestrictedWorker(bool isRestrictedWorker)240     inline void SetIsRestrictedWorker(bool isRestrictedWorker)
241     {
242         isRestrictedWorker_ = isRestrictedWorker;
243     }
244 
GetIsRestrictedWorker()245     bool GetIsRestrictedWorker() const
246     {
247         return isRestrictedWorker_;
248     }
249 
SetBundleName(const std::string & value)250     void SetBundleName(const std::string &value)
251     {
252         bundleName_ = value;
253     }
254 
SetEnableAOT(bool value)255     void SetEnableAOT(bool value)
256     {
257         enableAOT_ = value;
258     }
259 
SetAnDir(const std::string & value)260     void SetAnDir(const std::string &value)
261     {
262         anDir_ = value;
263     }
264 
SetEnableProfile(bool value)265     void SetEnableProfile(bool value)
266     {
267         enableProfile_ = value;
268     }
269 
270     // Valid only when SetEnableProfile(true)
SetProfileDir(const std::string & value)271     void SetProfileDir(const std::string &value)
272     {
273         profileDir_ = value;
274     }
275 
SetEnableJIT(bool value)276     void SetEnableJIT(bool value)
277     {
278         enableFastJIT_ = value;
279     }
280 
SetEnableBaselineJIT(bool value)281     void SetEnableBaselineJIT(bool value)
282     {
283         enableBaselineJIT_ = value;
284     }
285 
SetLargeHeap(bool largeHeap)286     void SetLargeHeap(bool largeHeap)
287     {
288         enableLargeHeap_ = largeHeap;
289     }
290 
GetLargeHeap()291     bool GetLargeHeap() const
292     {
293         return enableLargeHeap_;
294     }
295 
SetAOTCompileStatusMap(const std::map<std::string,int32_t> & value)296     void SetAOTCompileStatusMap(const std::map<std::string, int32_t> &value)
297     {
298         aotCompileStatusMap_ = value;
299     }
300 
GetAOTCompileStatusMap()301     const std::map<std::string, int32_t> &GetAOTCompileStatusMap() const
302     {
303         return aotCompileStatusMap_;
304     }
305 
SetEnableWarmStartupSmartGC(bool value)306     void SetEnableWarmStartupSmartGC(bool value)
307     {
308         enableWarmStartupSmartGC_ = value;
309     }
310 
GetEnableWarmStartupSmartGC()311     bool GetEnableWarmStartupSmartGC() const
312     {
313         return enableWarmStartupSmartGC_;
314     }
315 
316 private:
GetGcType()317     std::string GetGcType() const
318     {
319         std::string gcType;
320         switch (gcType_) {
321             case GC_TYPE::GEN_GC:
322                 gcType = "gen-gc";
323                 break;
324             case GC_TYPE::STW:
325                 gcType = "stw";
326                 break;
327             case GC_TYPE::EPSILON:
328                 gcType = "epsilon";
329                 break;
330             default:
331                 break;
332         }
333         return gcType;
334     }
335 
GetLogLevel()336     LOG_LEVEL GetLogLevel() const
337     {
338         return logLevel_;
339     }
340 
GetGcPoolSize()341     uint32_t GetGcPoolSize() const
342     {
343         return gcPoolSize_;
344     }
345 
GetLogBufPrint()346     LOG_PRINT GetLogBufPrint() const
347     {
348         return logBufPrint_;
349     }
350 
GetDebuggerLibraryPath()351     std::string GetDebuggerLibraryPath() const
352     {
353         return debuggerLibraryPath_;
354     }
355 
GetEnableArkTools()356     bool GetEnableArkTools() const
357     {
358         return enableArkTools_;
359     }
360 
GetEnableCpuprofiler()361     bool GetEnableCpuprofiler() const
362     {
363         return enableCpuprofiler_;
364     }
365 
GetArkProperties()366     int GetArkProperties() const
367     {
368         return arkProperties_;
369     }
370 
GetArkBundleName()371     std::string GetArkBundleName() const
372     {
373         return arkBundleName_;
374     }
375 
GetMemConfigProperty()376     std::string GetMemConfigProperty() const
377     {
378         return memConfigProperty_;
379     }
380 
GetGcThreadNum()381     size_t GetGcThreadNum() const
382     {
383         return gcThreadNum_;
384     }
385 
GetLongPauseTime()386     size_t GetLongPauseTime() const
387     {
388         return longPauseTime_;
389     }
390 
GetEnableAsmInterpreter()391     bool GetEnableAsmInterpreter() const
392     {
393         return enableAsmInterpreter_;
394     }
395 
GetEnableBuiltinsLazy()396     bool GetEnableBuiltinsLazy() const
397     {
398         return enableBuiltinsLazy_;
399     }
400 
GetAsmOpcodeDisableRange()401     std::string GetAsmOpcodeDisableRange() const
402     {
403         return asmOpcodeDisableRange_;
404     }
405 
GetBundleName()406     std::string GetBundleName() const
407     {
408         return bundleName_;
409     }
410 
GetEnableAOT()411     bool GetEnableAOT() const
412     {
413         return enableAOT_;
414     }
415 
GetAnDir()416     std::string GetAnDir() const
417     {
418         return anDir_;
419     }
420 
GetEnableProfile()421     bool GetEnableProfile() const
422     {
423         return enableProfile_;
424     }
425 
GetProfileDir()426     std::string GetProfileDir() const
427     {
428         return profileDir_;
429     }
430 
GetEnableJIT()431     bool GetEnableJIT() const
432     {
433         return enableFastJIT_;
434     }
435 
GetEnableDFXHiSysEvent()436     bool GetEnableDFXHiSysEvent() const
437     {
438         return enableDFXHiSysEvent_;
439     }
440 
GetEnableBaselineJIT()441     bool GetEnableBaselineJIT() const
442     {
443         return enableBaselineJIT_;
444     }
445 
446     GC_TYPE gcType_ = GC_TYPE::EPSILON;
447     LOG_LEVEL logLevel_ = LOG_LEVEL::DEBUG;
448     uint32_t gcPoolSize_ = ecmascript::DEFAULT_GC_POOL_SIZE;
449     LOG_PRINT logBufPrint_ {nullptr};
450     std::string debuggerLibraryPath_ {};
451     bool enableArkTools_ {false};
452     bool enableCpuprofiler_ {false};
453     int arkProperties_ {-1};
454     std::string arkBundleName_ = {""};
455     std::string memConfigProperty_ = {""};
456     size_t gcThreadNum_ {DEFAULT_GC_THREAD_NUM};
457     size_t longPauseTime_ {DEFAULT_LONG_PAUSE_TIME};
458     bool enableAsmInterpreter_ {true};
459     bool enableBuiltinsLazy_ {true};
460     bool isWorker_ {false};
461     bool isRestrictedWorker_ {false};
462     std::string asmOpcodeDisableRange_ {""};
463     std::string bundleName_ {};
464     bool enableAOT_ {false};
465     std::string anDir_ {};
466     bool enableProfile_ {false};
467     std::string profileDir_ {};
468     bool enableFastJIT_ {false};
469     bool enableDFXHiSysEvent_ {true};
470     bool enableBaselineJIT_ {false};
471     bool enableLargeHeap_ {false};
472     std::map<std::string, int32_t> aotCompileStatusMap_;
473     bool enableWarmStartupSmartGC_ {false};
474     friend JSNApi;
475 };
476 }  // namespace panda
477 
478 #endif  // ECMASCRIPT_NAPI_INCLUDE_JSNAPI_H
479