datex-core 0.0.12

The DATEX Core Rust implementation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
use crate::{
    channel::mpsc::{UnboundedReceiver, create_unbounded_channel},
    collections::HashMap,
    core_compiler::core_compilation_context::DXBWithSharedValues,
    dif::dif_interface::DIFInterface,
    disassembler::{
        options::DisassemblerOptions, print_disassembled_with_options,
    },
    global::{
        dxb_block::{
            DXBBlock, IncomingEndpointContextSectionId, IncomingSection,
            OutgoingContextId,
        },
        protocol_structures::{
            block_header::BlockHeader, encrypted_header::EncryptedHeader,
            routing_header::RoutingHeader,
        },
    },
    libs::core::CoreLibrary,
    network::{
        com_hub::{
            ComHub, InterfacePriority, network_response::ResponseOptions,
        },
        com_interfaces::local_loopback_interface::LocalLoopbackInterfaceSetupData,
    },
    prelude::*,
    runtime::{
        Runtime, RuntimeConfig, RuntimeConfigInterface,
        cache::shared_references_cache::SharedReferencesCache,
        execution::{
            ExecutionError, InvalidProgramError,
            context::{
                ExecutionContext, ExecutionMode, RemoteExecutionContext,
                ScriptExecutionError,
            },
            execution_input::ExecutionCallerMetadata,
        },
        pointer_address_provider::SelfOwnedPointerAddressProvider,
        pointer_availability_lookup::PointerAvailabilityLookup,
        request_move::compile_request_move,
    },
    shared_values::{
        OwnedSharedContainer, PointerAddress, RemotePointerAddress,
        SelfOwnedPointerAddress, SharedContainer, SharedContainerMutability,
        base_shared_value_container::observers::TransceiverId,
    },
    time::Instant,
    utils::task_manager::TaskManager,
    values::{
        core_value::CoreValue, core_values::endpoint::Endpoint, value::Value,
        value_container::ValueContainer,
    },
};
use alloc::rc::Rc;
use core::{
    cell::{Ref, RefCell, RefMut},
    pin::Pin,
    slice,
};
use log::{debug, error, info};

#[derive(Debug)]
pub struct RuntimeInternal {
    version: String,
    endpoint: Endpoint,

    core_library: CoreLibrary,

    memory: RefCell<SharedReferencesCache>,
    pointer_address_provider: Rc<RefCell<SelfOwnedPointerAddressProvider>>,
    com_hub: Rc<ComHub>,
    config: RuntimeConfig,

    /// counter to keep track of transceiver ids
    transceiver_counter: RefCell<u32>,

    task_manager: TaskManager,

    // receiver for incoming sections from com hub
    incoming_sections_receiver: RefCell<UnboundedReceiver<IncomingSection>>,

    /// active execution contexts, stored by context_id
    execution_contexts:
        RefCell<HashMap<IncomingEndpointContextSectionId, ExecutionContext>>,

    /// list of currently owned shared values that were approved to be transfered to another endpoint
    moving_pointers: RefCell<
        HashMap<
            Endpoint,
            HashMap<SelfOwnedPointerAddress, OwnedSharedContainer>,
        >,
    >,

    pointer_availability_lookup: RefCell<PointerAvailabilityLookup>,
}

macro_rules! get_execution_context {
    // take context and self_rc as parameters
    ($self_rc:expr, $execution_context:expr) => {
        match $execution_context {
            Some(context) => {
                // set current runtime in execution context if local execution context
                if let &mut ExecutionContext::Local(ref mut local_context) = context {
                    local_context.set_runtime_internal($self_rc.clone());
                }
                context
            },
            None => {
               &mut ExecutionContext::local(ExecutionMode::Static, $self_rc.clone(), ExecutionCallerMetadata::local_default())
            }
        }
    };
}

impl From<Rc<RuntimeInternal>> for Runtime {
    fn from(value: Rc<RuntimeInternal>) -> Self {
        Runtime { internal: value }
    }
}

impl RuntimeInternal {
    pub(crate) fn new(
        endpoint: Endpoint,
        memory: RefCell<SharedReferencesCache>,
        pointer_address_provider: Rc<RefCell<SelfOwnedPointerAddressProvider>>,
        config: RuntimeConfig,
        com_hub: Rc<ComHub>,
        task_manager: TaskManager,
        incoming_sections_receiver: UnboundedReceiver<IncomingSection>,
    ) -> RuntimeInternal {
        let pointer_availability_lookup =
            PointerAvailabilityLookup::new(endpoint.clone());
        RuntimeInternal {
            version: env!("CARGO_PKG_VERSION").to_string(),
            endpoint,
            memory,
            pointer_address_provider,
            config,
            com_hub,
            task_manager,
            core_library: CoreLibrary::default(),
            incoming_sections_receiver: RefCell::new(
                incoming_sections_receiver,
            ),
            execution_contexts: RefCell::new(HashMap::new()),
            moving_pointers: RefCell::new(HashMap::new()),
            transceiver_counter: RefCell::new(0),
            pointer_availability_lookup: RefCell::new(
                pointer_availability_lookup,
            ),
        }
    }
    pub fn version(&self) -> &str {
        &self.version
    }
    pub fn config(&self) -> &RuntimeConfig {
        &self.config
    }
    pub fn com_hub(&self) -> Rc<ComHub> {
        self.com_hub.clone()
    }
    pub fn endpoint(&self) -> &Endpoint {
        &self.endpoint
    }
    pub fn pointer_availability_lookup(
        &self,
    ) -> Ref<'_, PointerAvailabilityLookup> {
        self.pointer_availability_lookup.borrow()
    }
    pub fn pointer_availability_lookup_mut(
        &self,
    ) -> RefMut<'_, PointerAvailabilityLookup> {
        self.pointer_availability_lookup.borrow_mut()
    }
    pub fn memory(&self) -> &RefCell<SharedReferencesCache> {
        &self.memory
    }
    pub fn core_library(&self) -> &CoreLibrary {
        &self.core_library
    }

    pub fn pointer_address_provider(
        &self,
    ) -> &RefCell<SelfOwnedPointerAddressProvider> {
        &self.pointer_address_provider
    }
    pub fn incoming_sections_receiver_mut(
        &self,
    ) -> RefMut<'_, UnboundedReceiver<IncomingSection>> {
        self.incoming_sections_receiver.borrow_mut()
    }
    pub fn incoming_sections_receiver(
        &self,
    ) -> Ref<'_, UnboundedReceiver<IncomingSection>> {
        self.incoming_sections_receiver.borrow()
    }
    pub fn task_manager(&self) -> &TaskManager {
        &self.task_manager
    }

    pub fn stub() -> RuntimeInternal {
        let (sender, receiver) = create_unbounded_channel();
        RuntimeInternal::new(
            Endpoint::default(),
            RefCell::new(SharedReferencesCache::default()),
            Rc::new(RefCell::new(SelfOwnedPointerAddressProvider::new(
                Endpoint::default(),
            ))),
            RuntimeConfig::default(),
            ComHub::create(Endpoint::default(), sender).0,
            TaskManager::create().0,
            receiver,
        )
    }

    /// Creates all interfaces configured in the runtime config
    async fn create_configured_interfaces(&self) {
        if let Some(interfaces) = &self.config.interfaces {
            for RuntimeConfigInterface {
                interface_type,
                config,
                priority,
            } in interfaces.iter()
            {
                let create_future = self
                    .com_hub
                    .clone()
                    .create_interface(interface_type, config.clone(), *priority)
                    .await;
                match create_future {
                    Err(err) => {
                        error!(
                            "Failed to create interface \"{interface_type}\": {err}"
                        )
                    }
                    Ok((_, ready_receiver)) => {
                        if let Some(ready_receiver) = ready_receiver {
                            let _ = ready_receiver.await;
                        }
                    }
                }
            }
        }
    }

    async fn init_local_loopback_interface(&self) {
        // add default local loopback interface
        let local_interface_setup_data =
            LocalLoopbackInterfaceSetupData.create_interface().unwrap();

        let ready_signal = self
            .com_hub
            .clone()
            .add_interface_from_configuration(
                local_interface_setup_data,
                InterfacePriority::None,
            )
            .expect("Failed to add local loopback interface");
        // local loopback interface is single socket interface and should always return a ready signal
        // which should always resolve to Ok
        ready_signal.unwrap().await.unwrap()
    }

    /// Performs asynchronous initialization of the runtime
    pub(crate) async fn init_async(&self) {
        // create local loopback interface and other configured interfaces
        self.init_local_loopback_interface().await;
        self.create_configured_interfaces().await;
    }

    #[cfg(feature = "compiler")]
    pub async fn execute(
        self: Rc<RuntimeInternal>,
        script: &str,
        inserted_values: &[ValueContainer],
        execution_context: Option<&mut ExecutionContext>,
    ) -> Result<Option<ValueContainer>, ScriptExecutionError> {
        let execution_context = get_execution_context!(
            Runtime::from(self.clone()),
            execution_context
        );
        let compile_start = Instant::now();
        let dxb = execution_context.compile(
            script,
            inserted_values,
            execution_context.receivers(),
        )?;
        debug!(
            "[Compilation took {} ms]",
            compile_start.elapsed().as_millis()
        );
        let execute_start = Instant::now();
        let result = RuntimeInternal::execute_dxb(
            self,
            dxb,
            Some(execution_context),
            true,
        )
        .await
        .map_err(ScriptExecutionError::from);
        debug!(
            "[Execution took {} ms]",
            execute_start.elapsed().as_millis()
        );
        result
    }

    #[cfg(feature = "compiler")]
    pub fn execute_sync(
        self: Rc<RuntimeInternal>,
        script: &str,
        inserted_values: &[ValueContainer],
        execution_context: Option<&mut ExecutionContext>,
    ) -> Result<Option<ValueContainer>, ScriptExecutionError> {
        let execution_context = get_execution_context!(
            Runtime::from(self.clone()),
            execution_context
        );
        let compile_start = Instant::now();
        let dxb = execution_context.compile(
            script,
            inserted_values,
            execution_context.receivers(),
        )?;
        debug!(
            "[Compilation took {} ms]",
            compile_start.elapsed().as_millis()
        );
        let execute_start = Instant::now();
        let result = RuntimeInternal::execute_dxb_sync(
            self,
            dxb,
            Some(execution_context),
            true,
        )
        .map_err(ScriptExecutionError::from);
        debug!(
            "[Execution took {} ms]",
            execute_start.elapsed().as_millis()
        );
        result
    }

    pub fn execute_dxb<'a>(
        self: Rc<RuntimeInternal>,
        input: DXBWithSharedValues,
        execution_context: Option<&'a mut ExecutionContext>,
        _end_execution: bool,
    ) -> Pin<
        Box<
            dyn Future<Output = Result<Option<ValueContainer>, ExecutionError>>
                + 'a,
        >,
    > {
        Box::pin(async move {
            let execution_context = get_execution_context!(
                Runtime::from(self.clone()),
                execution_context
            );
            match execution_context {
                ExecutionContext::Remote(context) => {
                    RuntimeInternal::execute_remote(self, context, input).await
                }
                ExecutionContext::Local(_) => {
                    execution_context.execute_dxb(input).await
                }
            }
        })
    }

    pub fn execute_dxb_sync(
        self: Rc<RuntimeInternal>,
        dxb: DXBWithSharedValues,
        execution_context: Option<&mut ExecutionContext>,
        _end_execution: bool,
    ) -> Result<Option<ValueContainer>, ExecutionError> {
        let execution_context =
            get_execution_context!(Runtime::from(self), execution_context);
        match execution_context {
            ExecutionContext::Remote(_) => {
                Err(ExecutionError::RequiresAsyncExecution)
            }
            ExecutionContext::Local(_) => {
                execution_context.execute_dxb_sync(dxb)
            }
        }
    }

    /// Returns the existing execution context for the given context_id,
    /// or creates a new one if it doesn't exist.
    /// To reuse the context later, the caller must store it back in the map after use.
    fn take_execution_context(
        self: Rc<RuntimeInternal>,
        context_id: &IncomingEndpointContextSectionId,
        incoming_section: &IncomingSection,
    ) -> ExecutionContext {
        let runtime = Runtime::from(self.clone());
        let mut execution_contexts = self.execution_contexts.borrow_mut();
        // get execution context by context_id or create a new one if it doesn't exist
        let execution_context = execution_contexts.remove(context_id);
        if let Some(context) = execution_context {
            context
        } else {
            let caller_metadata = ExecutionCallerMetadata {
                endpoint: incoming_section.get_sender(),
            };
            ExecutionContext::local(
                ExecutionMode::unbounded(),
                runtime,
                caller_metadata,
            )
        }
    }

    pub async fn execute_remote(
        self: Rc<RuntimeInternal>,
        remote_execution_context: &mut RemoteExecutionContext,
        input: DXBWithSharedValues,
    ) -> Result<Option<ValueContainer>, ExecutionError> {
        let routing_header: RoutingHeader = RoutingHeader::default()
            .with_sender(self.endpoint.clone())
            .to_owned();

        // get existing context_id for context, or create a new one
        let context_id =
            remote_execution_context.context_id.unwrap_or_else(|| {
                // if the context_id is not set, we create a new one
                remote_execution_context.context_id =
                    Some(self.com_hub.block_handler.get_new_context_id());
                remote_execution_context.context_id.unwrap()
            });

        let block_header = BlockHeader {
            context_id,
            ..BlockHeader::default()
        };
        let encrypted_header = EncryptedHeader::default();

        let mut block = DXBBlock::new(
            routing_header,
            block_header,
            encrypted_header,
            input.dxb,
        );

        block
            .set_receivers(slice::from_ref(&remote_execution_context.endpoint));

        let response = self
            .com_hub
            .send_own_block_await_response(block, ResponseOptions::default())
            .await
            .remove(0)?;
        let incoming_section = response.take_incoming_section();

        RuntimeInternal::execute_incoming_section(
            self,
            incoming_section,
            Some(input.shared_values),
        )
        .await
        .0
    }

    pub(crate) async fn execute_incoming_section(
        self: Rc<RuntimeInternal>,
        mut incoming_section: IncomingSection,
        shared_values: Option<Vec<SharedContainer>>,
    ) -> (
        Result<Option<ValueContainer>, ExecutionError>,
        Endpoint,
        OutgoingContextId,
    ) {
        let section_context_id =
            incoming_section.get_section_context_id().clone();
        let mut context = Self::take_execution_context(
            self.clone(),
            &section_context_id,
            &incoming_section,
        );
        info!(
            "Executing incoming section with index: {}",
            incoming_section.get_section_index()
        );

        let mut result = None;
        let mut last_block = None;

        // iterate over the blocks in the incoming section
        loop {
            let block = incoming_section.next().await;
            if let Some(block) = block {
                let res = RuntimeInternal::execute_dxb_block_local(
                    self.clone(),
                    block.clone(),
                    Some(&mut context),
                    // NOTE: this assumes that all shared values are only needed as references, not as owned values
                    shared_values.clone(),
                )
                .await;
                if let Err(err) = res {
                    return (
                        Err(err),
                        block.sender().clone(),
                        block.block_header.context_id,
                    );
                }
                result = res.unwrap();
                last_block = Some(block);
            } else {
                break;
            }
        }

        if last_block.is_none() {
            unreachable!("Incoming section must contain at least one block");
        }
        let last_block = last_block.unwrap();
        let sender_endpoint = last_block.sender().clone();
        let context_id = last_block.block_header.context_id;

        // insert the context back into the map for future use
        // TODO #638: is this needed or can we drop the context after execution here?
        self.execution_contexts
            .borrow_mut()
            .insert(section_context_id, context);

        (Ok(result), sender_endpoint, context_id)
    }

    async fn execute_dxb_block_local(
        self: Rc<RuntimeInternal>,
        block: DXBBlock,
        execution_context: Option<&mut ExecutionContext>,
        shared_values: Option<Vec<SharedContainer>>,
    ) -> Result<Option<ValueContainer>, ExecutionError> {
        let execution_context = get_execution_context!(
            Runtime::from(self.clone()),
            execution_context
        );
        // assert that the execution context is local
        if !core::matches!(execution_context, ExecutionContext::Local(_)) {
            unreachable!(
                "Execution context must be local for executing a DXB block"
            );
        }
        let dxb = block.body;
        info!("executing on {}:\n", self.endpoint,);

        print_disassembled_with_options(&dxb, DisassemblerOptions::default());

        let end_execution =
            block.block_header.flags_and_timestamp.is_end_of_section();

        RuntimeInternal::execute_dxb(
            self,
            DXBWithSharedValues::new(dxb, shared_values.unwrap_or_default()),
            Some(execution_context),
            end_execution,
        )
        .await
    }

    /// Registers a list of shared containers for a single endpoint.
    pub fn register_shared_containers_for_single_endpoint(
        &self,
        endpoint: &Endpoint,
        shared_containers: Vec<SharedContainer>,
    ) {
        unsafe {
            self.register_shared_containers_for_endpoints(
                &[endpoint],
                shared_containers,
            )
            .unwrap()
        }
    }

    /// Registers a list of shared containers for a list of endpoints.
    /// The caller must ensure that endpoints is not empty.
    pub unsafe fn register_shared_containers_for_endpoints(
        &self,
        endpoints: &[&Endpoint],
        shared_containers: Vec<SharedContainer>,
    ) -> Result<(), ()> {
        if endpoints.is_empty() {
            panic!("endpoints must not be empty");
        }

        for shared_container in shared_containers {
            match shared_container {
                SharedContainer::Owned(owned_shared_container) => {
                    if endpoints.len() == 1 {
                        self.add_moving_shared_container(
                            endpoints[0].clone(),
                            owned_shared_container,
                        );
                    } else {
                        return Err(());
                    }
                }
                SharedContainer::Referenced(_referenced_shared_container) => {
                    // TODO: Subscribe
                }
            }
        }

        Ok(())
    }

    /// Request to move a list of external pointers from an endpoint to the local endpoint
    /// This only works if the local endpoint has the permission to move the pointers, either because
    /// it was allowed via a PERFORM_MOVE from the remote endpoint, or because the local endpoint has
    /// extended permissions
    pub(crate) async fn request_pointer_move(
        self: Rc<RuntimeInternal>,
        from_endpoint: &Endpoint,
        pointers: Vec<(SharedContainerMutability, SelfOwnedPointerAddress)>,
    ) -> Result<Vec<OwnedSharedContainer>, ExecutionError> {
        let pointer_mapping = pointers
            .into_iter()
            .map(|original| {
                (
                    original,
                    self.pointer_address_provider
                        .borrow_mut()
                        .get_new_self_owned_address(),
                )
            })
            .collect::<Vec<_>>();
        let body = compile_request_move(
            pointer_mapping
                .iter()
                .map(|((_, original), new)| (original.clone(), new.clone()))
                .collect::<Vec<_>>(),
        );
        let moved_values = self
            .clone()
            .execute_dxb(
                DXBWithSharedValues::new(body, vec![]),
                Some(&mut ExecutionContext::Remote(
                    RemoteExecutionContext::new(
                        from_endpoint.clone(),
                        ExecutionMode::Static,
                        Runtime::from(self),
                    ),
                )),
                true,
            )
            .await?;

        // moved values should be list
        match moved_values {
            Some(ValueContainer::Local(Value {
                inner: CoreValue::List(list),
                ..
            })) => {
                let pointer_values = list.into_vec();
                let owned_values = pointer_values.into_iter()
                    .zip(pointer_mapping)
                    .map(|(value, ((mutability, _), new_address))| {
                        // SAFETY: we got the new address from the pointer address provider above, is ensured to be unique
                        unsafe {
                            // TODO: also pass type information
                            OwnedSharedContainer::new_with_inferred_allowed_type_unsafe(
                                value,
                                mutability,
                                new_address,
                            )
                        }
                }).collect::<Vec<_>>();
                Ok(owned_values)
            }
            _ => Err(ExecutionError::InvalidProgram(
                InvalidProgramError::ExpectedValue,
            )),
        }
    }

    /// Adds a pointer that is approved for move to a specific endpoint
    /// Returns an error if any moving shared container is not an owned pointer
    pub(crate) fn add_moving_shared_container(
        &self,
        new_owner: Endpoint,
        moving_owned_container: OwnedSharedContainer,
    ) {
        let address = moving_owned_container.pointer_address().clone();

        self.moving_pointers
            .borrow_mut()
            .entry(new_owner)
            .or_default()
            .insert(address, moving_owned_container);
    }

    pub(crate) fn handle_pointer_move_to_remote(
        self: Rc<RuntimeInternal>,
        from_endpoint: &Endpoint,
        pointer_mapping: Vec<(
            SelfOwnedPointerAddress,
            SelfOwnedPointerAddress,
        )>,
        memory: &SharedReferencesCache,
    ) -> Result<Vec<ValueContainer>, ExecutionError> {
        let mut pointer_borrow = self.moving_pointers.borrow_mut();
        let moving_pointers = pointer_borrow
            .get_mut(from_endpoint)
            .ok_or(ExecutionError::UnauthorizedMove)?;

        let values = pointer_mapping
            .into_iter()
            .map(|(original_address, new)| {
                let new_address =
                    RemotePointerAddress::for_endpoint(from_endpoint, &new);
                let shared_container = moving_pointers
                    .remove(&original_address)
                    .ok_or(ExecutionError::UnauthorizedMove)?;

                let value = shared_container.value_container().clone();

                // make sure external pointer does not already exist in memory
                if memory
                    .has_reference(&PointerAddress::Remote(new_address.clone()))
                {
                    return Err(ExecutionError::InvalidMove);
                } else {
                    // Note: safe because we checked before if address is already in memory
                    unsafe {
                        shared_container.move_to_remote(new_address, memory);
                    }
                }

                Ok(value)
            })
            .collect::<Result<Vec<_>, ExecutionError>>()?;
        Ok(values)
    }

    pub fn get_env(&self) -> HashMap<String, String> {
        self.config.env.clone().unwrap_or_default()
    }

    /// Creates a new [DIFInterface] with a unique transceiver id and the runtime's pointer address provider
    pub fn create_dif_interface(&self) -> DIFInterface {
        let id = TransceiverId(*self.transceiver_counter.borrow());
        self.transceiver_counter.replace(id.0 + 1);
        DIFInterface::new(id, self.pointer_address_provider.clone())
    }
}