quickjs_rusty/value/
compiled_function.rs

1use crate::errors::*;
2use crate::value::*;
3
4/// A bytecode compiled function.
5#[derive(Clone, Debug)]
6pub struct JsCompiledFunction {
7    value: OwnedJsValue,
8}
9
10impl JsCompiledFunction {
11    pub(crate) fn try_from_value(value: OwnedJsValue) -> Result<Self, ValueError> {
12        if !value.is_compiled_function() {
13            Err(ValueError::Internal(format!(
14                "Expected a compiled function, got {:?}",
15                value.tag()
16            )))
17        } else {
18            Ok(Self { value })
19        }
20    }
21
22    pub(crate) fn as_value(&self) -> &OwnedJsValue {
23        &self.value
24    }
25
26    pub(crate) fn into_value(self) -> OwnedJsValue {
27        self.value
28    }
29
30    /// Evaluate this compiled function and return the resulting value.
31    // FIXME: add example
32    pub fn eval(&self) -> Result<OwnedJsValue, ExecutionError> {
33        crate::compile::run_compiled_function(self)
34    }
35
36    /// Convert this compiled function into QuickJS bytecode.
37    ///
38    /// Bytecode can be stored and loaded with [`Context::compile`].
39    // FIXME: add example
40    pub fn to_bytecode(&self) -> Result<Vec<u8>, ExecutionError> {
41        Ok(crate::compile::to_bytecode(self.value.context(), self))
42    }
43}