about summary refs log tree commit diff
path: root/tvix/eval/src/value/function.rs
blob: 2b5fcf6c9819cc3827d4b499b605e58a18da6bfc (plain) (blame)
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
//! This module implements the runtime representation of functions.
use std::rc::Rc;

use crate::{chunk::Chunk, Value};

#[derive(Clone, Debug)]
pub struct Lambda {
    // name: Option<NixString>,
    pub(crate) chunk: Rc<Chunk>,
    pub(crate) upvalue_count: usize,
}

impl Lambda {
    pub fn new_anonymous() -> Self {
        Lambda {
            // name: None,
            chunk: Default::default(),
            upvalue_count: 0,
        }
    }

    pub fn chunk(&mut self) -> &mut Rc<Chunk> {
        &mut self.chunk
    }
}

#[derive(Clone, Debug)]
pub struct Closure {
    pub lambda: Lambda,
    pub upvalues: Vec<Value>,
}

impl Closure {
    pub fn new(lambda: Lambda) -> Self {
        Closure {
            upvalues: Vec::with_capacity(lambda.upvalue_count),
            lambda,
        }
    }
}