diff --git a/ecmascript/builtins/builtins_gc.cpp b/ecmascript/builtins/builtins_gc.cpp index fd745c574c11c9a3406a4d0bc09c83d882d479e5..3fa04db1a027bef9d370ea0fea2fa045879e5e29 100644 --- a/ecmascript/builtins/builtins_gc.cpp +++ b/ecmascript/builtins/builtins_gc.cpp @@ -81,4 +81,18 @@ JSTaggedValue BuiltinsGc::RegisterNativeFree(EcmaRuntimeCallInfo *info) heap->DecreaseNativeBindingSize(std::min(allocated, static_cast(size))); return JSTaggedValue::Undefined(); } + +JSTaggedValue BuiltinsGc::WaitForFinishGC(EcmaRuntimeCallInfo *info) +{ + auto *heap = const_cast(info->GetThread()->GetEcmaVM()->GetHeap()); + heap->WaitAllTasksFinished(); + return JSTaggedValue::Undefined(); +} + +JSTaggedValue BuiltinsGc::StartGC(EcmaRuntimeCallInfo *info) +{ + auto *heap = const_cast(info->GetThread()->GetEcmaVM()->GetHeap()); + heap->CollectGarbage(TriggerGCType::FULL_GC, GCReason::EXTERNAL_TRIGGER); + return JSTaggedValue::Undefined(); +} } // namespace panda::ecmascript::builtins diff --git a/ecmascript/builtins/builtins_gc.h b/ecmascript/builtins/builtins_gc.h index e6640c696870cc1d5f218b937e15dfae7d429a87..44533b615155c7e0863b82fbf2751ded5b159ab9 100644 --- a/ecmascript/builtins/builtins_gc.h +++ b/ecmascript/builtins/builtins_gc.h @@ -19,19 +19,6 @@ #include "ecmascript/base/builtins_base.h" #include "ecmascript/js_thread.h" -// List of functions in ArkTools.GC, extension of JS engine. -// V(name, func, length, stubIndex) -// where BuiltinsGc::func refers to the native implementation of GC[name]. -// kungfu::BuiltinsStubCSigns::stubIndex refers to the builtin stub index, or INVALID if no stub available. -#define BUILTIN_GC_FUNCTIONS(V) \ - V("getFreeHeapSize", GetFreeHeapSize, 0, INVALID) \ - V("getReservedHeapSize", GetReservedHeapSize, 0, INVALID) \ - V("getUsedHeapSize", GetUsedHeapSize, 0, INVALID) \ - V("getObjectAddress", GetObjectAddress, 1, INVALID) \ - V("getObjectSpaceType", GetObjectSpaceType, 1, INVALID) \ - V("registerNativeAllocation", RegisterNativeAllocation, 1, INVALID) \ - V("registerNativeFree", RegisterNativeFree, 1, INVALID) - namespace panda::ecmascript::builtins { class BuiltinsGc : public base::BuiltinsBase { public: @@ -49,6 +36,10 @@ public: static JSTaggedValue RegisterNativeFree(EcmaRuntimeCallInfo *info); + static JSTaggedValue WaitForFinishGC(EcmaRuntimeCallInfo *info); + + static JSTaggedValue StartGC(EcmaRuntimeCallInfo *info); + static Span GetGcFunctions() { return Span(GC_FUNCTIONS); @@ -58,8 +49,20 @@ private: #define BUILTINS_GC_FUNCTION_ENTRY(name, method, length, id) \ base::BuiltinFunctionEntry::Create(name, BuiltinsGc::method, length, kungfu::BuiltinsStubCSigns::id), - static constexpr std::array GC_FUNCTIONS = { - BUILTIN_GC_FUNCTIONS(BUILTINS_GC_FUNCTION_ENTRY) +// List of functions in ArkTools.GC, extension of JS engine. +// where BuiltinsGc::func refers to the native implementation of GC[name]. +// kungfu::BuiltinsStubCSigns::stubIndex refers to the builtin stub index, or INVALID if no stub available. + + static constexpr std::array GC_FUNCTIONS = { + BUILTINS_GC_FUNCTION_ENTRY("getFreeHeapSize", GetFreeHeapSize, 0, INVALID) + BUILTINS_GC_FUNCTION_ENTRY("getReservedHeapSize", GetReservedHeapSize, 0, INVALID) + BUILTINS_GC_FUNCTION_ENTRY("getUsedHeapSize", GetUsedHeapSize, 0, INVALID) + BUILTINS_GC_FUNCTION_ENTRY("getObjectAddress", GetObjectAddress, 1, INVALID) + BUILTINS_GC_FUNCTION_ENTRY("getObjectSpaceType", GetObjectSpaceType, 1, INVALID) + BUILTINS_GC_FUNCTION_ENTRY("registerNativeAllocation", RegisterNativeAllocation, 1, INVALID) + BUILTINS_GC_FUNCTION_ENTRY("registerNativeFree", RegisterNativeFree, 1, INVALID) + BUILTINS_GC_FUNCTION_ENTRY("waitForFinishGC", WaitForFinishGC, 0, INVALID) + BUILTINS_GC_FUNCTION_ENTRY("startGC", StartGC, 0, INVALID) }; #undef BUILTINS_GC_FUNCTION_ENTRY }; diff --git a/test/moduletest/builtins/builtinsgc.js b/test/moduletest/builtins/builtinsgc.js index b543e199db9163fb17fc67a5408f32ef477ff240..fc1e1c2c98de4f490c92b42cd86ec7918aebad7d 100644 --- a/test/moduletest/builtins/builtinsgc.js +++ b/test/moduletest/builtins/builtinsgc.js @@ -58,3 +58,5 @@ try { ArkTools.GC.registerNativeFree(100); ArkTools.GC.registerNativeAllocation(20000); +ArkTools.GC.waitForFinishGC(); +ArkTools.GC.startGC();