beamr 0.4.3

A Rust runtime with the BEAM's execution model, targeting Gleam
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
//! Process introspection BIFs — process_info/1,2.
//!
//! This module intentionally implements only the process information items
//! currently needed by OTP/Gleam compatibility. The scheduler snapshots process
//! metadata through [`ProcessInfoFacility`]; this BIF module is responsible for
//! turning that allocation-free snapshot into caller-heap Erlang terms.

use crate::atom::{Atom, AtomTable};
use crate::native::{
    BifRegistryImpl, Capability, NativeFn, NativeRegistrationError, ProcessContext,
};
use crate::process::Priority;
use crate::term::Term;

/// Supported `erlang:process_info/*` item names in deterministic result order.
const SUPPORTED_ITEMS: &[ProcessInfoItem] = &[
    ProcessInfoItem::CurrentFunction,
    ProcessInfoItem::HeapSize,
    ProcessInfoItem::MessageQueueLen,
    ProcessInfoItem::RegisteredName,
    ProcessInfoItem::Status,
    ProcessInfoItem::TrapExit,
    ProcessInfoItem::Priority,
    ProcessInfoItem::Links,
    ProcessInfoItem::Monitors,
];

type ProcessInfoBif = (&'static str, u8, Capability, NativeFn);

const PROCESS_INFO_BIFS: &[ProcessInfoBif] = &[
    ("process_info", 1, Capability::Pure, bif_process_info_1),
    ("process_info", 2, Capability::Pure, bif_process_info_2),
    (
        "group_leader",
        0,
        Capability::ProcessLocal,
        bif_group_leader_0,
    ),
    (
        "group_leader",
        2,
        Capability::ProcessLocal,
        bif_group_leader_2,
    ),
];

/// Process-info item understood by the scheduler-backed introspection facility.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub enum ProcessInfoItem {
    /// `current_function` → `{Module, Function, Arity}`.
    CurrentFunction,
    /// `heap_size` → words currently allocated by the process heap.
    HeapSize,
    /// `message_queue_len` → number of queued mailbox messages.
    MessageQueueLen,
    /// `registered_name` → registered atom name, or `[]`.
    RegisteredName,
    /// `status` → `running | waiting | suspended`.
    Status,
    /// `trap_exit` → boolean atom.
    TrapExit,
    /// `priority` → `low | normal | high | max`.
    Priority,
    /// `links` → list of linked process identifiers.
    Links,
    /// `monitors` → list of monitored process descriptors.
    Monitors,
}

fn priority_atom(context: &ProcessContext, priority: Priority) -> Result<Atom, Term> {
    let atom_table = context.atom_table().ok_or_else(badarg)?;
    Ok(match priority {
        Priority::Low => atom_table.intern("low"),
        Priority::Normal => Atom::NORMAL,
        Priority::High => atom_table.intern("high"),
        Priority::Max => atom_table.intern("max"),
    })
}

impl ProcessInfoItem {
    fn name(self) -> &'static str {
        match self {
            Self::CurrentFunction => "current_function",
            Self::HeapSize => "heap_size",
            Self::MessageQueueLen => "message_queue_len",
            Self::RegisteredName => "registered_name",
            Self::Status => "status",
            Self::TrapExit => "trap_exit",
            Self::Priority => "priority",
            Self::Links => "links",
            Self::Monitors => "monitors",
        }
    }
}

/// Public, allocation-free process status snapshot for process_info.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum ProcessInfoStatus {
    /// Process is running or runnable.
    Running,
    /// Process is waiting for a message or timeout.
    Waiting,
    /// Process is scheduler-suspended.
    Suspended,
}

/// Monitor metadata snapshot safe to expose through `process_info(Pid, monitors)`.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct ProcessMonitorInfo {
    /// PID that owns the monitor.
    pub watcher: u64,
    /// PID being monitored.
    pub target: u64,
}

/// Allocation-free snapshot returned by the scheduler and rendered by this BIF.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ProcessInfoValue {
    /// Current module/function/arity metadata.
    CurrentFunction(Option<(Atom, Atom, u8)>),
    /// Heap words currently used.
    HeapSize(usize),
    /// Number of queued messages.
    MessageQueueLen(usize),
    /// Registered process name, if one is available.
    RegisteredName(Option<Atom>),
    /// Observable process status.
    Status(ProcessInfoStatus),
    /// Trap-exit flag.
    TrapExit(bool),
    /// Scheduling priority.
    Priority(Priority),
    /// Linked process identifiers.
    Links(Vec<u64>),
    /// Monitor records attached to the process.
    Monitors(Vec<ProcessMonitorInfo>),
}

/// Scheduler-provided process information reader.
pub trait ProcessInfoFacility: Send + Sync {
    /// Snapshot a single process-info item. Returns `None` when `pid` is not a
    /// live process or the process body is absent.
    fn process_info(&self, pid: u64, item: ProcessInfoItem) -> Option<ProcessInfoValue>;
}

/// Registers process introspection BIFs into the VM-owned BIF registry.
pub fn register_process_info_bifs(
    registry: &BifRegistryImpl,
    atom_table: &AtomTable,
) -> Result<(), NativeRegistrationError> {
    let erlang = atom_table.intern("erlang");

    for &(function_name, arity, capability, native_function) in PROCESS_INFO_BIFS {
        let function = atom_table.intern(function_name);
        registry.register(erlang, function, arity, native_function, capability)?;
    }

    Ok(())
}

/// erlang:process_info/2 — query one process information item.
pub fn bif_process_info_2(args: &[Term], context: &mut ProcessContext) -> Result<Term, Term> {
    let [pid_term, item_term] = args else {
        return Err(badarg());
    };
    let pid = pid_term.as_pid().ok_or_else(badarg)?;
    let item_atom = item_term.as_atom().ok_or_else(badarg)?;
    let item = parse_item(context, item_atom)?;
    let Some(value) = query_process_info(context, pid, item)? else {
        return Ok(Term::atom(Atom::UNDEFINED));
    };

    let words = 3 + value_heap_words(pid, &value);
    context.ensure_heap_space(words)?;
    let value_term = alloc_value(context, pid, value)?;
    context.alloc_tuple(&[*item_term, value_term])
}

/// erlang:process_info/1 — query all supported process information items.
pub fn bif_process_info_1(args: &[Term], context: &mut ProcessContext) -> Result<Term, Term> {
    let [pid_term] = args else {
        return Err(badarg());
    };
    let pid = pid_term.as_pid().ok_or_else(badarg)?;

    let mut values = Vec::with_capacity(SUPPORTED_ITEMS.len());
    for item in SUPPORTED_ITEMS {
        let Some(value) = query_process_info(context, pid, *item)? else {
            return Ok(Term::atom(Atom::UNDEFINED));
        };
        values.push((*item, value));
    }

    let words = values
        .iter()
        .map(|(_, value)| 3 + value_heap_words(pid, value) + 2)
        .sum();
    context.ensure_heap_space(words)?;

    let mut tuples = Vec::with_capacity(values.len());
    for (item, value) in values {
        let item_atom = intern_item_atom(context, item)?;
        let value_term = alloc_value(context, pid, value)?;
        tuples.push(context.alloc_tuple(&[Term::atom(item_atom), value_term])?);
    }
    context.alloc_list(&tuples)
}

fn query_process_info(
    context: &ProcessContext,
    pid: u64,
    item: ProcessInfoItem,
) -> Result<Option<ProcessInfoValue>, Term> {
    if item == ProcessInfoItem::Priority
        && context.pid() == Some(pid)
        && let Ok(priority) = context.priority()
    {
        return Ok(Some(ProcessInfoValue::Priority(priority)));
    }

    let facility = context.process_info_facility().ok_or_else(badarg)?;
    Ok(facility.process_info(pid, item))
}

fn parse_item(context: &ProcessContext, atom: Atom) -> Result<ProcessInfoItem, Term> {
    for item in SUPPORTED_ITEMS {
        if intern_item_atom(context, *item)? == atom {
            return Ok(*item);
        }
    }
    Err(badarg())
}

fn intern_item_atom(context: &ProcessContext, item: ProcessInfoItem) -> Result<Atom, Term> {
    let atom_table = context.atom_table().ok_or_else(badarg)?;
    Ok(atom_table.intern(item.name()))
}

fn alloc_value(
    context: &mut ProcessContext,
    queried_pid: u64,
    value: ProcessInfoValue,
) -> Result<Term, Term> {
    match value {
        ProcessInfoValue::CurrentFunction(current_mfa) => {
            let (module, function, arity) =
                current_mfa.unwrap_or((Atom::UNDEFINED, Atom::UNDEFINED, 0));
            context.alloc_tuple(&[
                Term::atom(module),
                Term::atom(function),
                Term::small_int(i64::from(arity)),
            ])
        }
        ProcessInfoValue::HeapSize(words) | ProcessInfoValue::MessageQueueLen(words) => {
            usize_to_small_int(words)
        }
        ProcessInfoValue::RegisteredName(Some(name)) => Ok(Term::atom(name)),
        ProcessInfoValue::RegisteredName(None) => Ok(Term::NIL),
        ProcessInfoValue::Status(status) => Ok(Term::atom(status_atom(context, status)?)),
        ProcessInfoValue::TrapExit(value) => Ok(bool_to_atom(value)),
        ProcessInfoValue::Priority(priority) => Ok(Term::atom(priority_atom(context, priority)?)),
        ProcessInfoValue::Links(links) => alloc_pid_list(context, &links),
        ProcessInfoValue::Monitors(monitors) => alloc_monitor_list(context, queried_pid, &monitors),
    }
}

fn value_heap_words(queried_pid: u64, value: &ProcessInfoValue) -> usize {
    match value {
        ProcessInfoValue::CurrentFunction(_) => 4,
        ProcessInfoValue::HeapSize(_)
        | ProcessInfoValue::MessageQueueLen(_)
        | ProcessInfoValue::RegisteredName(_)
        | ProcessInfoValue::Status(_)
        | ProcessInfoValue::Priority(_)
        | ProcessInfoValue::TrapExit(_) => 0,
        ProcessInfoValue::Links(links) => links.len() * 2,
        ProcessInfoValue::Monitors(monitors) => {
            monitors
                .iter()
                .filter(|monitor| monitor.watcher == queried_pid)
                .count()
                * 4
        }
    }
}

fn status_atom(context: &ProcessContext, status: ProcessInfoStatus) -> Result<Atom, Term> {
    let atom_table = context.atom_table().ok_or_else(badarg)?;
    Ok(match status {
        ProcessInfoStatus::Running => atom_table.intern("running"),
        ProcessInfoStatus::Waiting => atom_table.intern("waiting"),
        ProcessInfoStatus::Suspended => atom_table.intern("suspended"),
    })
}

fn alloc_pid_list(context: &mut ProcessContext, pids: &[u64]) -> Result<Term, Term> {
    let mut terms = Vec::with_capacity(pids.len());
    for pid in pids {
        terms.push(Term::try_pid(*pid).ok_or_else(badarg)?);
    }
    context.alloc_list(&terms)
}

fn alloc_monitor_list(
    context: &mut ProcessContext,
    queried_pid: u64,
    monitors: &[ProcessMonitorInfo],
) -> Result<Term, Term> {
    let process_atom = Term::atom(Atom::PROCESS);
    let mut terms = Vec::new();
    for monitor in monitors {
        if monitor.watcher == queried_pid {
            let target = Term::try_pid(monitor.target).ok_or_else(badarg)?;
            terms.push(context.alloc_tuple(&[process_atom, target])?);
        }
    }
    context.alloc_list(&terms)
}

fn usize_to_small_int(value: usize) -> Result<Term, Term> {
    let value = i64::try_from(value).map_err(|_| badarg())?;
    Term::try_small_int(value).ok_or_else(badarg)
}

fn bool_to_atom(value: bool) -> Term {
    Term::atom(if value { Atom::TRUE } else { Atom::FALSE })
}

fn badarg() -> Term {
    Term::atom(Atom::BADARG)
}

pub fn bif_group_leader_0(args: &[Term], context: &mut ProcessContext) -> Result<Term, Term> {
    if !args.is_empty() {
        return Err(badarg());
    }
    context.group_leader()
}

pub fn bif_group_leader_2(args: &[Term], context: &mut ProcessContext) -> Result<Term, Term> {
    let [new_leader, pid] = args else {
        return Err(badarg());
    };
    if !new_leader.is_pid() {
        return Err(badarg());
    }
    let Some(target_pid) = pid.as_pid() else {
        return Err(badarg());
    };
    let facility = context.group_leader_facility().ok_or_else(badarg)?;
    facility
        .set_group_leader(target_pid, *new_leader)
        .map_err(|_| badarg())?;
    context.set_attached_group_leader(target_pid, *new_leader);
    Ok(Term::atom(Atom::TRUE))
}

#[cfg(test)]
mod tests {
    use std::collections::HashMap;
    use std::sync::{Arc, Mutex};

    use super::*;
    use crate::process::{Priority, Process};
    use crate::term::boxed::{Cons, Tuple};

    #[derive(Default)]
    struct MockProcessInfoFacility {
        values: Mutex<HashMap<(u64, ProcessInfoItem), ProcessInfoValue>>,
    }

    impl MockProcessInfoFacility {
        fn insert(&self, pid: u64, item: ProcessInfoItem, value: ProcessInfoValue) {
            self.values
                .lock()
                .expect("values mutex")
                .insert((pid, item), value);
        }
    }

    impl ProcessInfoFacility for MockProcessInfoFacility {
        fn process_info(&self, pid: u64, item: ProcessInfoItem) -> Option<ProcessInfoValue> {
            self.values
                .lock()
                .expect("values mutex")
                .get(&(pid, item))
                .cloned()
        }
    }

    fn context_with_facility(
        atom_table: Arc<AtomTable>,
        facility: Arc<MockProcessInfoFacility>,
        process: &mut Process,
    ) -> ProcessContext<'_> {
        let mut context = ProcessContext::new();
        context.set_atom_table(Some(atom_table));
        context.set_process_info_facility(Some(facility));
        context.attach_process(process, 0);
        context
    }

    fn tuple_elements(term: Term) -> Vec<Term> {
        let tuple = Tuple::new(term).expect("tuple term");
        (0..tuple.arity())
            .filter_map(|index| tuple.get(index))
            .collect()
    }

    fn list_elements(mut term: Term) -> Vec<Term> {
        let mut elements = Vec::new();
        while term != Term::NIL {
            let cons = Cons::new(term).expect("proper list cons");
            elements.push(cons.head());
            term = cons.tail();
        }
        elements
    }

    #[test]
    fn register_process_info_bifs_registers_process_info_1_and_2() {
        let atom_table = AtomTable::with_common_atoms();
        let registry = BifRegistryImpl::new();
        register_process_info_bifs(&registry, &atom_table).expect("registration");
        let erlang = atom_table.intern("erlang");
        let process_info = atom_table.intern("process_info");
        assert!(registry.lookup(erlang, process_info, 1).is_some());
        assert!(registry.lookup(erlang, process_info, 2).is_some());
    }

    #[test]
    fn process_info_2_returns_tuple_for_each_supported_item() {
        let atom_table = Arc::new(AtomTable::with_common_atoms());
        let facility = Arc::new(MockProcessInfoFacility::default());
        let pid = 7;
        let module = atom_table.intern("mod");
        let function = atom_table.intern("fun");
        facility.insert(
            pid,
            ProcessInfoItem::CurrentFunction,
            ProcessInfoValue::CurrentFunction(Some((module, function, 2))),
        );
        facility.insert(
            pid,
            ProcessInfoItem::HeapSize,
            ProcessInfoValue::HeapSize(10),
        );
        facility.insert(
            pid,
            ProcessInfoItem::MessageQueueLen,
            ProcessInfoValue::MessageQueueLen(3),
        );
        facility.insert(
            pid,
            ProcessInfoItem::RegisteredName,
            ProcessInfoValue::RegisteredName(Some(atom_table.intern("name"))),
        );
        facility.insert(
            pid,
            ProcessInfoItem::Status,
            ProcessInfoValue::Status(ProcessInfoStatus::Running),
        );
        facility.insert(
            pid,
            ProcessInfoItem::TrapExit,
            ProcessInfoValue::TrapExit(true),
        );
        facility.insert(
            pid,
            ProcessInfoItem::Priority,
            ProcessInfoValue::Priority(Priority::High),
        );
        facility.insert(
            pid,
            ProcessInfoItem::Links,
            ProcessInfoValue::Links(vec![1, 2]),
        );
        facility.insert(
            pid,
            ProcessInfoItem::Monitors,
            ProcessInfoValue::Monitors(vec![ProcessMonitorInfo {
                watcher: pid,
                target: 9,
            }]),
        );

        let item_names = [
            "current_function",
            "heap_size",
            "message_queue_len",
            "registered_name",
            "status",
            "trap_exit",
            "priority",
            "links",
            "monitors",
        ];
        for item_name in item_names {
            let mut process = Process::new(0, 128);
            let mut context =
                context_with_facility(Arc::clone(&atom_table), Arc::clone(&facility), &mut process);
            let item = atom_table.intern(item_name);
            let result = bif_process_info_2(&[Term::pid(pid), Term::atom(item)], &mut context)
                .expect("process_info/2 succeeds");
            let elements = tuple_elements(result);
            assert_eq!(elements.len(), 2);
            assert_eq!(elements[0], Term::atom(item));
        }
    }

    #[test]
    fn process_info_2_returns_undefined_for_missing_process_and_badarg_for_unknown_item() {
        let atom_table = Arc::new(AtomTable::with_common_atoms());
        let facility = Arc::new(MockProcessInfoFacility::default());
        let mut process = Process::new(0, 128);
        let mut context = context_with_facility(atom_table.clone(), facility, &mut process);
        let heap_size = atom_table.intern("heap_size");
        assert_eq!(
            bif_process_info_2(&[Term::pid(99), Term::atom(heap_size)], &mut context),
            Ok(Term::atom(Atom::UNDEFINED))
        );
        let unknown = atom_table.intern("unknown_process_info_item");
        assert_eq!(
            bif_process_info_2(&[Term::pid(99), Term::atom(unknown)], &mut context),
            Err(Term::atom(Atom::BADARG))
        );
    }

    #[test]
    fn process_info_requires_configured_facility() {
        let atom_table = Arc::new(AtomTable::with_common_atoms());
        let mut process = Process::new(0, 128);
        let mut context = ProcessContext::new();
        context.set_atom_table(Some(atom_table.clone()));
        context.attach_process(&mut process, 0);
        let heap_size = atom_table.intern("heap_size");

        assert_eq!(
            bif_process_info_2(&[Term::pid(1), Term::atom(heap_size)], &mut context),
            Err(Term::atom(Atom::BADARG))
        );
        assert_eq!(
            bif_process_info_1(&[Term::pid(1)], &mut context),
            Err(Term::atom(Atom::BADARG))
        );
    }

    #[test]
    fn process_info_1_returns_deterministic_list_of_all_supported_items() {
        let atom_table = Arc::new(AtomTable::with_common_atoms());
        let facility = Arc::new(MockProcessInfoFacility::default());
        let pid = 11;
        for item in SUPPORTED_ITEMS {
            let value = match item {
                ProcessInfoItem::CurrentFunction => ProcessInfoValue::CurrentFunction(None),
                ProcessInfoItem::HeapSize => ProcessInfoValue::HeapSize(0),
                ProcessInfoItem::MessageQueueLen => ProcessInfoValue::MessageQueueLen(0),
                ProcessInfoItem::RegisteredName => ProcessInfoValue::RegisteredName(None),
                ProcessInfoItem::Status => ProcessInfoValue::Status(ProcessInfoStatus::Waiting),
                ProcessInfoItem::TrapExit => ProcessInfoValue::TrapExit(false),
                ProcessInfoItem::Priority => ProcessInfoValue::Priority(Priority::Normal),
                ProcessInfoItem::Links => ProcessInfoValue::Links(Vec::new()),
                ProcessInfoItem::Monitors => ProcessInfoValue::Monitors(Vec::new()),
            };
            facility.insert(pid, *item, value);
        }

        let mut process = Process::new(0, 256);
        let mut context = context_with_facility(atom_table.clone(), facility, &mut process);
        let result = bif_process_info_1(&[Term::pid(pid)], &mut context).expect("process_info/1");
        let entries = list_elements(result);
        assert_eq!(entries.len(), SUPPORTED_ITEMS.len());
        for (entry, item) in entries.into_iter().zip(SUPPORTED_ITEMS) {
            let tuple = tuple_elements(entry);
            assert_eq!(tuple[0], Term::atom(atom_table.intern(item.name())));
        }
    }

    #[test]
    fn process_info_2_returns_priority_atom() {
        let atom_table = Arc::new(AtomTable::with_common_atoms());
        let facility = Arc::new(MockProcessInfoFacility::default());
        let pid = 21;
        facility.insert(
            pid,
            ProcessInfoItem::Priority,
            ProcessInfoValue::Priority(Priority::High),
        );
        let mut process = Process::new(0, 128);
        let mut context =
            context_with_facility(Arc::clone(&atom_table), Arc::clone(&facility), &mut process);
        let priority = atom_table.intern("priority");

        let result = bif_process_info_2(&[Term::pid(pid), Term::atom(priority)], &mut context)
            .expect("process_info/2 succeeds");
        let tuple = tuple_elements(result);

        assert_eq!(tuple[0], Term::atom(priority));
        assert_eq!(tuple[1], Term::atom(atom_table.intern("high")));
    }

    #[test]
    fn process_info_2_reports_attached_process_priority_after_process_flag_change() {
        let atom_table = Arc::new(AtomTable::with_common_atoms());
        let facility = Arc::new(MockProcessInfoFacility::default());
        let pid = 21;
        facility.insert(
            pid,
            ProcessInfoItem::Priority,
            ProcessInfoValue::Priority(Priority::Normal),
        );
        let mut process = Process::new(pid, 128);
        let mut context =
            context_with_facility(Arc::clone(&atom_table), Arc::clone(&facility), &mut process);
        context.set_priority(Priority::High).expect("set priority");
        let priority = atom_table.intern("priority");

        let result = bif_process_info_2(&[Term::pid(pid), Term::atom(priority)], &mut context)
            .expect("process_info/2 succeeds");
        let tuple = tuple_elements(result);

        assert_eq!(tuple[0], Term::atom(priority));
        assert_eq!(tuple[1], Term::atom(atom_table.intern("high")));
    }
}