UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
VVMEnterVMInline.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#if WITH_VERSE_VM || defined(__INTELLISENSE__)
6
7#include "Misc/ScopeExit.h"
10#include "VerseVM/VVMContext.h"
12#include "VerseVM/VVMFrame.h"
14#include "VerseVM/VVMTask.h"
15
16namespace Verse
17{
18// The entry point that all `C++` calls into Verse should go through
19//
20// Style note: Try not to split this function out into other calls too much...
21// we want anyone who reads this to easily be able to grasp the logic-flow.
22template <typename TFunctor>
23void FRunningContext::EnterVM_Internal(EEnterVMMode Mode, TFunctor F)
24{
25 AutoRTFM::UnreachableIfClosed("#jira SOL-8415");
26
27 FContextImpl* Impl = GetImpl();
28
29 // TODO: ideally, we would always have an active content scope here and wouldn't need to check `IsActive`.
30 // At present, omitting `IsActive` breaks many tests, as well as FVerseVmAssembler::TranslateExpression.
31 // #jira SOL-8492
32 VTaskGroup* PreviousTaskGroup = Impl->CurrentTaskGroup();
34 {
36 if (!Scope->ShouldExecuteCodeWithThisScope())
37 {
38 // The active content scope was terminated.
39 return;
40 }
41 Impl->SetCurrentTaskGroup(Scope->GetTaskGroup());
42 }
43
45 {
46 Impl->SetCurrentTaskGroup(PreviousTaskGroup);
47 };
48
49 const bool bTopLevel = Impl->_NativeFrame == nullptr;
50 if (bTopLevel)
51 {
52 Impl->StartComputationWatchdog();
54 {
55 Sampler->SetMutatorContext(this);
56 Sampler->Start();
57 }
58 }
59
60 auto SetupAndRun = [&]() {
61 AutoRTFM::UnreachableIfClosed("#jira SOL-8415");
62
64 if (bTopLevel)
65 {
66 // We need to create a 'root' frame as this call into the interpreter could either come
67 // from the top level or from some C++ that Verse called into higher up the stack.
68 // So, this frame represents that top level C++. This task is meant as a "last resort,"
69 // and the interpreter is expected to create a new task in BeginTask instead of using this.
70 // (However, it's valid for the interpreter to just stay in this task and never BeginTask,
71 // if we're just running non-task-y code that never uses spawn/sync/race/etc.)
72 VTask* Task = &VTask::New(*this, &StopInterpreterSentry, VFrame::GlobalEmptyFrame.Get(), /*YieldTask*/ nullptr, /*Parent*/ nullptr);
73 VFailureContext* FailureContext = &VFailureContext::New(*this, Task, nullptr, *VFrame::GlobalEmptyFrame.Get(), VValue(), &StopInterpreterSentry);
75 .FailureContext = FailureContext,
76 .Task = Task,
77 .EffectToken = VValue::EffectDoneMarker()};
79
80 NewNativeFrame.Start(*this);
81 F();
82 NewNativeFrame.CommitIfNoAbort(*this);
83 }
84 else if (Mode == EEnterVMMode::NewTransaction)
85 {
86 const FNativeFrame* NativeFrame = GetImpl()->NativeFrame();
87 VFailureContext* NewFailureContext = &VFailureContext::New(*this, NativeFrame->Task, NativeFrame->FailureContext, *VFrame::GlobalEmptyFrame.Get(), VValue(), &StopInterpreterSentry);
89
90 Impl->_NativeFrame->Start(*this);
91 F();
92 Impl->_NativeFrame->CommitIfNoAbort(*this);
93 }
94 else
95 {
96 F();
97 }
98 };
99
100 if (AutoRTFM::IsTransactional())
101 {
102 SetupAndRun();
103 }
104 else
105 {
106 // `NativeContext.Start` calls `AutoRTFM::ForTheRuntime::StartTransaction()`
107 // which can't start a new transaction stack, so we need to make one first.
108 AutoRTFM::TransactThenOpen([&] { SetupAndRun(); });
109 }
110
111 if (bTopLevel)
112 {
113 Impl->PauseComputationWatchdog();
115 {
116 Sampler->Pause();
117 Sampler->SetMutatorContext(nullptr);
118 }
119 }
120}
121
122} // namespace Verse
123#endif // WITH_VERSE_VM
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
#define ON_SCOPE_EXIT
Definition ScopeExit.h:73
Definition SharedPointer.h:153
static const TSharedRef< FContentScope > & GetActiveScope()
Definition VVMContentScope.h:101
static bool IsActive()
Definition VVMContentScope.h:100
Definition ExpressionParserTypes.h:21
FORCEINLINE T * Get(const FObjectPtr &ObjectPtr)
Definition ObjectPtr.h:426
Definition Archive.h:36
Definition UnrealTemplate.h:341