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