beamr 0.6.4

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
//! Borrowed accessor structs for reading boxed term layouts.

use crate::atom::Atom;
use crate::term::{Term, binary::Binary, shared_binary::SharedBinary};

use super::{BoxedHeader, BoxedTag};

/// Borrowed accessor for a tuple boxed term.
#[derive(Copy, Clone, Debug)]
pub struct Tuple {
    ptr: *const u64,
}

impl Tuple {
    pub fn new(term: Term) -> Option<Self> {
        let ptr = header_ptr(term, BoxedTag::Tuple)?;
        Some(Self { ptr })
    }

    pub fn arity(self) -> usize {
        BoxedHeader::size(self.header())
    }

    pub fn get(self, index: usize) -> Option<Term> {
        if index < self.arity() {
            Some(Term::from_raw(self.word(1 + index)))
        } else {
            None
        }
    }

    fn header(self) -> u64 {
        self.word(0)
    }

    fn word(self, offset: usize) -> u64 {
        // SAFETY: instances are only built from term pointers to stack/heap word
        // arrays created by this module; callers must keep the backing storage
        // alive while using the borrowed accessor.
        unsafe { *self.ptr.add(offset) }
    }
}

/// Borrowed accessor for a list cons cell.
#[derive(Copy, Clone, Debug)]
pub struct Cons {
    ptr: *const u64,
}

impl Cons {
    pub fn new(term: Term) -> Option<Self> {
        if !term.is_list() {
            return None;
        }

        Some(Self {
            ptr: term.heap_ptr()?,
        })
    }

    pub fn head(self) -> Term {
        Term::from_raw(self.word(0))
    }

    pub fn tail(self) -> Term {
        Term::from_raw(self.word(1))
    }

    fn word(self, offset: usize) -> u64 {
        // SAFETY: see Tuple::word; cons accessors read the fixed two-word cell.
        unsafe { *self.ptr.add(offset) }
    }
}

/// Borrowed accessor for a boxed float.
#[derive(Copy, Clone, Debug)]
pub struct Float {
    ptr: *const u64,
}

impl Float {
    pub fn new(term: Term) -> Option<Self> {
        let ptr = header_ptr(term, BoxedTag::Float)?;
        Some(Self { ptr })
    }

    pub fn value(self) -> f64 {
        // SAFETY: float payload is one u64 word immediately after the header.
        f64::from_bits(unsafe { *self.ptr.add(1) })
    }
}

/// Borrowed accessor for a boxed big integer storage layout.
#[derive(Copy, Clone, Debug)]
pub struct BigInt {
    ptr: *const u64,
}

impl BigInt {
    pub fn new(term: Term) -> Option<Self> {
        let ptr = header_ptr(term, BoxedTag::BigInt)?;
        Some(Self { ptr })
    }

    pub fn is_negative(self) -> bool {
        self.word(1) == super::BIGINT_NEGATIVE_SIGN
    }

    pub fn limb_count(self) -> usize {
        self.word(2) as usize
    }

    pub fn limbs(self) -> &'static [u64] {
        let count = self.limb_count();
        // SAFETY: the limb count is written by write_bigint, and the returned
        // borrow points into caller-owned heap storage that must outlive use.
        unsafe { std::slice::from_raw_parts(self.ptr.add(3), count) }
    }

    fn word(self, offset: usize) -> u64 {
        // SAFETY: see Tuple::word.
        unsafe { *self.ptr.add(offset) }
    }
}

/// Borrowed accessor for a boxed closure.
#[derive(Copy, Clone, Debug)]
pub struct Closure {
    ptr: *const u64,
}

impl Closure {
    pub fn new(term: Term) -> Option<Self> {
        let ptr = header_ptr(term, BoxedTag::Closure)?;
        // SAFETY: `header_ptr` returned a boxed closure header pointer.
        let header = unsafe { *ptr };
        let size = BoxedHeader::size(header);
        if size < 6 {
            return None;
        }

        // SAFETY: closure payloads of size at least six contain the num_free
        // word at offset four. Reject inconsistent sizes before exposing the
        // accessor so metadata/free-var reads stay within the boxed object.
        let num_free = unsafe { *ptr.add(4) } as usize;
        if size != 6 + num_free {
            return None;
        }

        Some(Self { ptr })
    }

    pub fn module(self) -> Option<Atom> {
        Term::from_raw(self.word(1)).as_atom()
    }

    pub fn function_index(self) -> u64 {
        self.word(2)
    }

    pub fn arity(self) -> u8 {
        self.word(3) as u8
    }

    pub fn num_free(self) -> usize {
        self.word(4) as usize
    }

    pub fn generation(self) -> u64 {
        self.word(5)
    }

    pub fn unique_id(self) -> u64 {
        self.word(6)
    }

    pub fn free_var(self, index: usize) -> Option<Term> {
        if index < self.num_free() {
            Some(Term::from_raw(self.word(7 + index)))
        } else {
            None
        }
    }

    /// True when this closure is an export fun (`fun M:F/A`) written by
    /// `write_export_fun`, marked by the sentinel generation.
    pub fn is_export(self) -> bool {
        self.generation() == super::EXPORT_FUN_GENERATION
    }

    /// Function atom of an export fun; `None` for ordinary closures.
    pub fn export_function(self) -> Option<Atom> {
        if self.is_export() {
            Term::from_raw(self.word(2)).as_atom()
        } else {
            None
        }
    }

    fn word(self, offset: usize) -> u64 {
        // SAFETY: see Tuple::word.
        unsafe { *self.ptr.add(offset) }
    }
}

/// Borrowed accessor for a flatmap boxed term.
#[derive(Copy, Clone, Debug)]
pub struct Map {
    ptr: *const u64,
}

impl Map {
    pub fn new(term: Term) -> Option<Self> {
        let ptr = header_ptr(term, BoxedTag::Map)?;
        Some(Self { ptr })
    }

    pub fn len(self) -> usize {
        self.word(1) as usize
    }

    pub fn is_empty(self) -> bool {
        self.len() == 0
    }

    pub fn key(self, index: usize) -> Option<Term> {
        if index < self.len() {
            Some(Term::from_raw(self.word(2 + index)))
        } else {
            None
        }
    }

    pub fn value(self, index: usize) -> Option<Term> {
        if index < self.len() {
            Some(Term::from_raw(self.word(2 + self.len() + index)))
        } else {
            None
        }
    }

    pub fn get(self, key: Term) -> Option<Term> {
        (0..self.len()).find_map(|index| {
            if self.key(index) == Some(key) {
                self.value(index)
            } else {
                None
            }
        })
    }

    fn word(self, offset: usize) -> u64 {
        // SAFETY: see Tuple::word.
        unsafe { *self.ptr.add(offset) }
    }
}

/// Borrowed accessor for an off-heap reference-counted binary.
#[derive(Copy, Clone, Debug)]
pub struct ProcBin {
    ptr: *const u64,
}

impl ProcBin {
    pub fn new(term: Term) -> Option<Self> {
        let ptr = header_ptr(term, BoxedTag::ProcBin)?;
        // SAFETY: `header_ptr` returned a boxed ProcBin header pointer.
        let header = unsafe { *ptr };
        if BoxedHeader::size(header) != 2 {
            return None;
        }
        // SAFETY: validated ProcBin layout has two payload words; word two is
        // the raw Arc pointer and must be present/non-null before access.
        if unsafe { *ptr.add(2) } == 0 {
            return None;
        }

        Some(Self { ptr })
    }

    pub fn as_bytes(self) -> &'static [u8] {
        SharedBinary::bytes_from_raw_word(self.arc_ptr_word())
    }

    pub fn len(self) -> usize {
        self.as_bytes().len()
    }

    pub fn is_empty(self) -> bool {
        self.len() == 0
    }

    pub fn shared_binary(self) -> SharedBinary {
        SharedBinary::clone_from_raw_word(self.arc_ptr_word())
    }

    fn arc_ptr_word(self) -> u64 {
        // SAFETY: ProcBin payload word two stores the raw `Arc<Vec<u8>>` pointer.
        unsafe { *self.ptr.add(2) }
    }
}

/// Borrowed accessor for a sub-binary view into an inline Binary or ProcBin.
#[derive(Copy, Clone, Debug)]
pub struct SubBinary {
    ptr: *const u64,
}

impl SubBinary {
    pub fn new(term: Term) -> Option<Self> {
        let ptr = header_ptr(term, BoxedTag::SubBinary)?;
        // SAFETY: `header_ptr` returned a boxed SubBinary header pointer.
        let header = unsafe { *ptr };
        if BoxedHeader::size(header) != 4 {
            return None;
        }

        let sub_binary = Self { ptr };
        let parent_bytes = parent_bytes(sub_binary.parent())?;
        let end = sub_binary.offset().checked_add(sub_binary.len())?;
        if end > parent_bytes.len() {
            return None;
        }

        Some(sub_binary)
    }

    pub fn parent(self) -> Term {
        Term::from_raw(self.word(1))
    }

    pub fn len(self) -> usize {
        self.word(3) as usize
    }

    pub fn is_empty(self) -> bool {
        self.len() == 0
    }

    pub fn as_bytes(self) -> &'static [u8] {
        let bytes = parent_bytes(self.parent()).unwrap_or(&[]);
        let start = self.offset();
        let end = start.checked_add(self.len()).unwrap_or(start);
        bytes.get(start..end).unwrap_or(&[])
    }

    fn offset(self) -> usize {
        self.word(2) as usize
    }

    fn word(self, offset: usize) -> u64 {
        // SAFETY: validated SubBinary layout contains fixed payload words.
        unsafe { *self.ptr.add(offset) }
    }
}

fn parent_bytes(parent: Term) -> Option<&'static [u8]> {
    if let Some(binary) = Binary::new(parent) {
        return Some(binary.as_bytes());
    }
    ProcBin::new(parent).map(ProcBin::as_bytes)
}

/// Borrowed accessor for a boxed reference.
#[derive(Copy, Clone, Debug)]
pub struct Reference {
    ptr: *const u64,
}

impl Reference {
    pub fn new(term: Term) -> Option<Self> {
        let ptr = header_ptr(term, BoxedTag::Reference)?;
        if BoxedHeader::size(word_at(ptr, 0)) != 1 {
            return None;
        }

        Some(Self { ptr })
    }

    pub fn id(self) -> u64 {
        // SAFETY: reference payload is one u64 id immediately after the header.
        unsafe { *self.ptr.add(1) }
    }
}

/// Borrowed accessor for a boxed remote PID.
#[derive(Copy, Clone, Debug)]
pub struct ExternalPid {
    ptr: *const u64,
}

impl ExternalPid {
    pub fn new(term: Term) -> Option<Self> {
        let ptr = header_ptr(term, BoxedTag::ExternalPid)?;
        if BoxedHeader::size(word_at(ptr, 0)) != 3
            || Term::from_raw(word_at(ptr, 1)).as_atom().is_none()
        {
            return None;
        }

        Some(Self { ptr })
    }

    pub fn node(self) -> Option<Atom> {
        Term::from_raw(self.word(1)).as_atom()
    }

    pub fn pid_number(self) -> u64 {
        self.word(2)
    }

    pub fn serial(self) -> u64 {
        self.word(3)
    }

    fn word(self, offset: usize) -> u64 {
        // SAFETY: external PID payload contains fixed words after the header.
        unsafe { *self.ptr.add(offset) }
    }
}

/// Borrowed accessor for a boxed remote reference.
#[derive(Copy, Clone, Debug)]
pub struct ExternalReference {
    ptr: *const u64,
}

impl ExternalReference {
    pub fn new(term: Term) -> Option<Self> {
        let ptr = header_ptr(term, BoxedTag::ExternalReference)?;
        if BoxedHeader::size(word_at(ptr, 0)) != 2
            || Term::from_raw(word_at(ptr, 1)).as_atom().is_none()
        {
            return None;
        }

        Some(Self { ptr })
    }

    pub fn node(self) -> Option<Atom> {
        Term::from_raw(self.word(1)).as_atom()
    }

    pub fn id(self) -> u64 {
        self.word(2)
    }

    fn word(self, offset: usize) -> u64 {
        // SAFETY: external reference payload contains fixed words after the header.
        unsafe { *self.ptr.add(offset) }
    }
}

fn word_at(ptr: *const u64, offset: usize) -> u64 {
    // SAFETY: caller has verified that `ptr` is a boxed object header pointer.
    unsafe { *ptr.add(offset) }
}

fn header_ptr(term: Term, expected_tag: BoxedTag) -> Option<*const u64> {
    if !term.is_boxed() {
        return None;
    }

    let ptr = term.heap_ptr()?;
    // SAFETY: boxed terms point to a header word in caller-owned heap storage.
    let header = unsafe { *ptr };
    if BoxedHeader::tag(header) == Some(expected_tag) {
        Some(ptr)
    } else {
        None
    }
}