@@ -38,69 +38,60 @@ pub async fn execute_vm_with_context(
3838 let program = program. program . clone ( ) ;
3939 let async_ops = new_shared_vm_async_ops ( ) ;
4040
41- if debug. attach_debugger {
42- let async_ops_for_debug = async_ops. clone ( ) ;
43- let vm_context_for_debug = vm_context. clone ( ) ;
44- let program_for_debug = program. clone ( ) ;
45- let task = tokio:: task:: spawn_blocking ( move || {
46- let mut vm = Vm :: with_locals_shared ( program_for_debug, local_count) ;
47- vm. set_async_bridge ( Box :: new ( VmAsyncOpBridge :: new ( async_ops_for_debug. clone ( ) ) ) ) ;
48- register_host_modules ( & mut vm, vm_context_for_debug. clone ( ) , async_ops_for_debug)
49- . map_err ( VmExecutionError :: HostRegistration ) ?;
50-
51- loop {
52- let status = run_vm_with_optional_debugger (
53- & debug_session,
54- & debug. request_headers ,
55- & debug. request_path ,
56- & debug. request_id ,
57- & mut vm,
58- )
59- . map_err ( VmExecutionError :: Vm ) ?;
60-
61- match status {
62- VmStatus :: Halted => break ,
63- VmStatus :: Yielded => continue ,
64- VmStatus :: Waiting ( _op_id) => tokio:: runtime:: Handle :: current ( )
65- . block_on ( vm. await_waiting_host_op ( ) )
66- . map_err ( VmExecutionError :: Vm ) ?,
67- }
68- }
69-
70- Ok ( snapshot_execution_outcome ( & vm_context_for_debug) )
71- } ) ;
41+ let task = tokio:: task:: spawn_blocking ( move || {
42+ run_vm_blocking (
43+ program,
44+ local_count,
45+ vm_context,
46+ debug_session,
47+ debug,
48+ async_ops,
49+ register_host_modules,
50+ )
51+ } ) ;
7252
73- return task. await . map_err ( |err| {
74- VmExecutionError :: Vm ( vm:: VmError :: HostError ( format ! (
75- "vm blocking execution task failed: {err}"
76- ) ) )
77- } ) ?;
78- }
53+ task. await . map_err ( |err| {
54+ VmExecutionError :: Vm ( vm:: VmError :: HostError ( format ! (
55+ "vm blocking execution task failed: {err}"
56+ ) ) )
57+ } ) ?
58+ }
7959
60+ fn run_vm_blocking (
61+ program : std:: sync:: Arc < vm:: Program > ,
62+ local_count : usize ,
63+ vm_context : SharedProxyVmContext ,
64+ debug_session : SharedDebugSession ,
65+ debug : VmDebugInvocation ,
66+ async_ops : SharedVmAsyncOps ,
67+ register_host_modules : HostModuleRegistrar ,
68+ ) -> Result < VmExecutionOutcome , VmExecutionError > {
8069 let mut vm = Vm :: with_locals_shared ( program, local_count) ;
8170 vm. set_async_bridge ( Box :: new ( VmAsyncOpBridge :: new ( async_ops. clone ( ) ) ) ) ;
8271 register_host_modules ( & mut vm, vm_context. clone ( ) , async_ops)
8372 . map_err ( VmExecutionError :: HostRegistration ) ?;
8473
8574 loop {
86- let status = run_vm_with_optional_debugger (
87- & debug_session,
88- & debug. request_headers ,
89- & debug. request_path ,
90- & debug. request_id ,
91- & mut vm,
92- )
75+ let status = if debug. attach_debugger {
76+ run_vm_with_optional_debugger (
77+ & debug_session,
78+ & debug. request_headers ,
79+ & debug. request_path ,
80+ & debug. request_id ,
81+ & mut vm,
82+ )
83+ } else {
84+ vm. run ( )
85+ }
9386 . map_err ( VmExecutionError :: Vm ) ?;
9487 match status {
9588 VmStatus :: Halted => break ,
9689 VmStatus :: Yielded => {
97- tokio:: task:: yield_now ( ) . await ;
98- }
99- VmStatus :: Waiting ( _op_id) => {
100- vm. await_waiting_host_op ( )
101- . await
102- . map_err ( VmExecutionError :: Vm ) ?;
90+ std:: thread:: yield_now ( ) ;
10391 }
92+ VmStatus :: Waiting ( _op_id) => tokio:: runtime:: Handle :: current ( )
93+ . block_on ( vm. await_waiting_host_op ( ) )
94+ . map_err ( VmExecutionError :: Vm ) ?,
10495 }
10596 }
10697
0 commit comments