1 /*
2 * Copyright (c) 2024-2025 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 #include "ecmascript/cross_vm/cross_vm_operator.h"
17
18 #include "ecmascript/cross_vm/unified_gc/unified_gc_marker.h"
19 #include "ecmascript/cross_vm/heap_hybrid-inl.h"
20 #include "ecmascript/cross_vm/unified_gc/unified_gc.h"
21 #include "ecmascript/ecma_vm.h"
22 #include "ecmascript/mem/heap-inl.h"
23
24 namespace panda::ecmascript {
25
CrossVMOperator(EcmaVM * vm)26 CrossVMOperator::CrossVMOperator(EcmaVM *vm) : vm_(vm)
27 {
28 ecmaVMInterface_ = std::make_unique<EcmaVMInterfaceImpl>(vm);
29 }
30
31 /*static*/
DoHandshake(EcmaVM * vm,void * stsIface,void ** ecmaIface)32 void CrossVMOperator::DoHandshake(EcmaVM *vm, void *stsIface, void **ecmaIface)
33 {
34 auto vmOperator = vm->GetCrossVMOperator();
35 *ecmaIface = vmOperator->ecmaVMInterface_.get();
36 vmOperator->stsVMInterface_ = static_cast<arkplatform::STSVMInterface *>(stsIface);
37 UnifiedGC *unifiedGC = SharedHeap::GetInstance()->GetUnifiedGC();
38 if (unifiedGC->GetSTSVMInterface() == nullptr) {
39 unifiedGC->SetSTSVMInterface(vmOperator->stsVMInterface_);
40 }
41 }
42
MarkFromObject(JSTaggedType value)43 void CrossVMOperator::MarkFromObject(JSTaggedType value)
44 {
45 JSTaggedValue taggedValue(value);
46 if (!taggedValue.IsHeapObject()) {
47 return;
48 }
49 TaggedObject *object = taggedValue.GetHeapObject();
50 auto heap = vm_->GetHeap();
51 heap->GetUnifiedGCMarker()->MarkFromObject(object);
52 }
53
IsObjectAlive(JSTaggedType value)54 bool CrossVMOperator::IsObjectAlive(JSTaggedType value)
55 {
56 JSTaggedValue taggedValue(value);
57 if (!taggedValue.IsHeapObject()) {
58 return false;
59 }
60 TaggedObject *object = taggedValue.GetHeapObject();
61 return vm_->GetHeap()->IsAlive(object);
62 }
63
IsValidHeapObject(JSTaggedType value)64 bool CrossVMOperator::IsValidHeapObject(JSTaggedType value)
65 {
66 JSTaggedValue taggedValue(value);
67 if (!taggedValue.IsHeapObject()) {
68 return false;
69 }
70 TaggedObject *object = taggedValue.GetHeapObject();
71 return vm_->GetHeap()->ContainObject(object);
72 }
73
StartXRefMarking()74 bool CrossVMOperator::EcmaVMInterfaceImpl::StartXRefMarking()
75 {
76 return SharedHeap::GetInstance()->TriggerUnifiedGCMark<
77 TriggerGCType::UNIFIED_GC, GCReason::CROSSREF_CAUSE>(vm_->GetJSThread());
78 }
79
NotifyXGCInterruption()80 void CrossVMOperator::EcmaVMInterfaceImpl::NotifyXGCInterruption()
81 {
82 UnifiedGC *unifiedGC = SharedHeap::GetInstance()->GetUnifiedGC();
83 unifiedGC->SetInterruptUnifiedGC(true);
84 }
85
86 } // namespace panda::ecmascript