quickjs_rusty/value/
compiled_function.rs1use crate::errors::*;
2use crate::value::*;
3
4#[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 pub fn eval(&self) -> Result<OwnedJsValue, ExecutionError> {
33 crate::compile::run_compiled_function(self)
34 }
35
36 pub fn to_bytecode(&self) -> Result<Vec<u8>, ExecutionError> {
41 Ok(crate::compile::to_bytecode(self.value.context(), self))
42 }
43}