quickjs_rusty/value/
compiled_function.rs

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
use crate::errors::*;
use crate::value::*;

/// A bytecode compiled function.
#[derive(Clone, Debug)]
pub struct JsCompiledFunction {
    value: OwnedJsValue,
}

impl JsCompiledFunction {
    pub(crate) fn try_from_value(value: OwnedJsValue) -> Result<Self, ValueError> {
        if !value.is_compiled_function() {
            Err(ValueError::Internal(format!(
                "Expected a compiled function, got {:?}",
                value.tag()
            )))
        } else {
            Ok(Self { value })
        }
    }

    pub(crate) fn as_value(&self) -> &OwnedJsValue {
        &self.value
    }

    pub(crate) fn into_value(self) -> OwnedJsValue {
        self.value
    }

    /// Evaluate this compiled function and return the resulting value.
    // FIXME: add example
    pub fn eval(&self) -> Result<OwnedJsValue, ExecutionError> {
        crate::compile::run_compiled_function(self)
    }

    /// Convert this compiled function into QuickJS bytecode.
    ///
    /// Bytecode can be stored and loaded with [`Context::compile`].
    // FIXME: add example
    pub fn to_bytecode(&self) -> Result<Vec<u8>, ExecutionError> {
        Ok(crate::compile::to_bytecode(self.value.context(), self))
    }
}