1 /*
2  * Copyright (c) 2024 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_BUILTINS_BUILTINS_GC_H
17 #define ECMASCRIPT_BUILTINS_BUILTINS_GC_H
18 
19 #include "ecmascript/base/builtins_base.h"
20 #include "ecmascript/js_thread.h"
21 
22 namespace panda::ecmascript::builtins {
23 class BuiltinsGc : public base::BuiltinsBase {
24 public:
25     static JSTaggedValue GetFreeHeapSize(EcmaRuntimeCallInfo *info);
26 
27     static JSTaggedValue GetReservedHeapSize(EcmaRuntimeCallInfo *info);
28 
29     static JSTaggedValue GetUsedHeapSize(EcmaRuntimeCallInfo *info);
30 
31     static JSTaggedValue GetObjectAddress(EcmaRuntimeCallInfo *info);
32 
33     static JSTaggedValue GetObjectSpaceType(EcmaRuntimeCallInfo *info);
34 
35     static JSTaggedValue RegisterNativeAllocation(EcmaRuntimeCallInfo *info);
36 
37     static JSTaggedValue RegisterNativeFree(EcmaRuntimeCallInfo *info);
38 
39     static JSTaggedValue WaitForFinishGC(EcmaRuntimeCallInfo *info);
40 
41     static JSTaggedValue StartGC(EcmaRuntimeCallInfo *info);
42 
43     static JSTaggedValue AllocateArrayObject(EcmaRuntimeCallInfo *info);
44 
GetGcFunctions()45     static Span<const base::BuiltinFunctionEntry> GetGcFunctions()
46     {
47         return Span<const base::BuiltinFunctionEntry>(GC_FUNCTIONS);
48     }
49 
50 private:
51 #define BUILTINS_GC_FUNCTION_ENTRY(name, method, length, id) \
52     base::BuiltinFunctionEntry::Create(name, BuiltinsGc::method, length, kungfu::BuiltinsStubCSigns::id),
53 
54 // List of functions in ArkTools.GC, extension of JS engine.
55 // where BuiltinsGc::func refers to the native implementation of GC[name].
56 //       kungfu::BuiltinsStubCSigns::stubIndex refers to the builtin stub index, or INVALID if no stub available.
57 
58     static constexpr std::array GC_FUNCTIONS = {
59         BUILTINS_GC_FUNCTION_ENTRY("getFreeHeapSize",             GetFreeHeapSize,               0, INVALID)
60         BUILTINS_GC_FUNCTION_ENTRY("getReservedHeapSize",         GetReservedHeapSize,           0, INVALID)
61         BUILTINS_GC_FUNCTION_ENTRY("getUsedHeapSize",             GetUsedHeapSize,               0, INVALID)
62         BUILTINS_GC_FUNCTION_ENTRY("getObjectAddress",            GetObjectAddress,              1, INVALID)
63         BUILTINS_GC_FUNCTION_ENTRY("getObjectSpaceType",          GetObjectSpaceType,            1, INVALID)
64         BUILTINS_GC_FUNCTION_ENTRY("registerNativeAllocation",    RegisterNativeAllocation,      1, INVALID)
65         BUILTINS_GC_FUNCTION_ENTRY("registerNativeFree",          RegisterNativeFree,            1, INVALID)
66         BUILTINS_GC_FUNCTION_ENTRY("waitForFinishGC",             WaitForFinishGC,               1, INVALID)
67         BUILTINS_GC_FUNCTION_ENTRY("startGC",                     StartGC,                       3, INVALID)
68         BUILTINS_GC_FUNCTION_ENTRY("allocateArrayObject",         AllocateArrayObject,           1, INVALID)
69     };
70 #undef BUILTINS_GC_FUNCTION_ENTRY
71 
72     static void WaitAndHandleConcurrentMarkingFinished(Heap *heap);
73     static TriggerGCType StringToGcType(JSThread *thread, JSTaggedValue cause);
74 };
75 }  // namespace panda::ecmascript::builtins
76 
77 #endif  // ECMASCRIPT_BUILTINS_BUILTINS_GC_H
78