Struct dyon::runtime::Runtime

source ·
pub struct Runtime {
    pub module: Arc<Module>,
    pub stack: Vec<Variable>,
    pub call_stack: Vec<Call>,
    pub local_stack: Vec<(Arc<String>, usize)>,
    pub current_stack: Vec<(Arc<String>, usize)>,
    pub module_resolver: fn(source: &str, target: &mut String) -> Result<(), String>,
    pub arg_err_index: Cell<Option<usize>>,
    /* private fields */
}
Expand description

Stores data needed for running a Dyon program.

Fields§

§module: Arc<Module>

Stores the current module in use.

§stack: Vec<Variable>

Stores variables on the stack.

§call_stack: Vec<Call>

name, file, stack_len, local_len.

§local_stack: Vec<(Arc<String>, usize)>

Stores stack of locals.

§current_stack: Vec<(Arc<String>, usize)>

Stores stack of current objects.

When a current object is used, the runtime searches backwards until it finds the last current variable with the name.

§module_resolver: fn(source: &str, target: &mut String) -> Result<(), String>

The module resolver instance

§arg_err_index: Cell<Option<usize>>

External functions can choose to report an error on an argument.

Implementations§

source§

impl Runtime

source

pub fn new() -> Runtime

Creates a new Runtime.

Examples found in repository?
examples/functions.rs (line 10)
7
8
9
10
11
12
13
14
15
fn main() {
    use dyon::{error, Runtime};

    let mut dyon_runtime = Runtime::new();
    let dyon_module = load_module().unwrap();
    if error(dyon_runtime.run(&Arc::new(dyon_module))) {
        return;
    }
}
More examples
Hide additional examples
examples/dyon.rs (line 186)
178
179
180
181
182
183
184
185
186
187
188
189
fn run_str_with_module(
    source: &str,
    d: Arc<String>,
    module: &Module,
) -> Result<(), String> {
    let mut m = Module::new();
    m.import(module);
    load_str(source, d, &mut m)?;
    let mut runtime = Runtime::new();
    runtime.run(&Arc::new(m))?;
    Ok(())
}
examples/call.rs (line 60)
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
fn main() {
    let mut module = Module::new();

    // Add functions to read `a` and `b` from `RustArgs`.
    module.add(
        Arc::new("a_of".into()),
        a_of,
        Dfn::nl(vec![Type::Any], Type::F64),
    );
    module.add(
        Arc::new("b_of".into()),
        b_of,
        Dfn::nl(vec![Type::Any], Type::F64),
    );

    error(load_str(
        "main.dyon",
        Arc::new(
            r#"
        fn add_args(a: f64, b: f64) {
            println("add_args:")
            println(link {a" + "b" = "a + b})
        }

        fn add_obj(obj: {}) {
            println("add_obj:")
            println(link {obj.a" + "obj.b" = "obj.a + obj.b})
        }

        fn add_rust(obj: any) {
            println("add_rust")
            a := a_of(obj)
            b := b_of(obj)
            println(link {a" + "b" = "a + b})
        }

        add(a, b) = a + b

        create_vec(a, b) = (a, b)

        id(obj) = clone(obj)
    "#
            .into(),
        ),
        &mut module,
    ));
    let ref module = Arc::new(module);

    let a = 20.0;
    let b = 30.0;

    // Call with multiple arguments.
    let call = Call::new("add_args").arg(a).arg(b);
    error(call.run(&mut Runtime::new(), module));

    // Call with object.
    let call = Call::new("add_obj").arg(Args { a, b });
    error(call.run(&mut Runtime::new(), module));

    // Call with rust object.
    let call = Call::new("add_rust").rust(RustArgs { a, b });
    error(call.run(&mut Runtime::new(), module));

    // Call function with return value.
    let call = Call::new("add").arg(a).arg(b);
    match call.run_ret::<f64>(&mut Runtime::new(), module) {
        Ok(answer) => {
            println!("{}", answer);
        }
        Err(err) => {
            error(Err(err));
        }
    }

    // Call function that returns vec4.
    let call = Call::new("create_vec").arg(a).arg(b);
    match call.run_vec4::<[f64; 2]>(&mut Runtime::new(), module) {
        Ok(answer) => {
            println!("{:?}", answer);
        }
        Err(err) => {
            error(Err(err));
        }
    }

    // Call function that returns Rust object.
    let call = Call::new("id").rust(RustArgs { a, b });
    match call.run_ret::<RustObject>(&mut Runtime::new(), module) {
        Ok(answer) => {
            println!("{:?}", answer.lock().unwrap().downcast_ref::<RustArgs>());
        }
        Err(err) => {
            error(Err(err));
        }
    }
}
source

pub fn pop<T: PopVariable>(&mut self) -> Result<T, String>

Pops variable from stack.

source

pub fn pop_vec4<T: ConvertVec4>(&mut self) -> Result<T, String>

Pops 4D vector from stack.

source

pub fn pop_mat4<T: ConvertMat4>(&mut self) -> Result<T, String>

Pops 4D matrix from stack.

source

pub fn var<T: PopVariable>(&self, var: &Variable) -> Result<T, String>

Gets variable.

source

pub fn current_object<T: PopVariable>(&self, name: &str) -> Result<T, String>

Gets Current Object variable from the stack for Current Objects by finding the corresponding entry in the normal stack. If the Current Object can’t be found in the stack of current objects, the error (“Could not find current variable {}”, name) is thrown.

##Examples

Dyon code:

~ entity := 42
teleport()

Rust code:

use dyon::Runtime;

fn teleport(rt: &mut Runtime) -> Result<(), String> {
    let current_entity_id = rt.current_object::<u32>("entity")?;
    assert_eq!(current_entity_id, 42);
    Ok(())
}
source

pub fn var_vec4<T: ConvertVec4>(&self, var: &Variable) -> Result<T, String>

Gets 4D vector.

source

pub fn var_mat4<T: ConvertMat4>(&self, var: &Variable) -> Result<T, String>

Gets 4D matrix.

source

pub fn push<T: PushVariable>(&mut self, val: T)

Push value to stack.

source

pub fn push_vec4<T: ConvertVec4>(&mut self, val: T)

Push Vec4 to stack.

source

pub fn push_mat4<T: ConvertMat4>(&mut self, val: T)

Push Mat4 to stack.

source

pub fn push_rust<T: 'static>(&mut self, val: T)

Pushes Rust object to stack.

source

pub fn expected_arg(&self, arg: usize, var: &Variable, ty: &str) -> String

Generates error message that a certain type was expected for argument.

Sets argument error index on runtime such that external functions can report proper range.

source

pub fn expected(&self, var: &Variable, ty: &str) -> String

Generates error message that a certain type was expected.

source

pub fn resolve<'a>(&'a self, var: &'a Variable) -> &'a Variable

Resolves a variable reference if any, getting a pointer to the variable on the stack.

source

pub fn run(&mut self, module: &Arc<Module>) -> Result<(), String>

Run main function in a module.

Examples found in repository?
examples/functions.rs (line 12)
7
8
9
10
11
12
13
14
15
fn main() {
    use dyon::{error, Runtime};

    let mut dyon_runtime = Runtime::new();
    let dyon_module = load_module().unwrap();
    if error(dyon_runtime.run(&Arc::new(dyon_module))) {
        return;
    }
}
More examples
Hide additional examples
examples/dyon.rs (line 187)
178
179
180
181
182
183
184
185
186
187
188
189
fn run_str_with_module(
    source: &str,
    d: Arc<String>,
    module: &Module,
) -> Result<(), String> {
    let mut m = Module::new();
    m.import(module);
    load_str(source, d, &mut m)?;
    let mut runtime = Runtime::new();
    runtime.run(&Arc::new(m))?;
    Ok(())
}
source

pub fn go(&mut self, go: &Go) -> Result<(Option<Variable>, Flow), String>

Start a new thread and return the handle.

source

pub fn call_closure( &mut self, call: &CallClosure ) -> Result<(Option<Variable>, Flow), String>

Call closure.

source

pub fn call( &mut self, call: &Call, module: &Arc<Module> ) -> Result<(Option<Variable>, Flow), String>

Called from the outside, e.g. a loader script by call or call_ret intrinsic.

source

pub fn call_str( &mut self, function: &str, args: &[Variable], module: &Arc<Module> ) -> Result<(), String>

Calls function by name.

source

pub fn call_str_ret( &mut self, function: &str, args: &[Variable], module: &Arc<Module> ) -> Result<Variable, String>

Call function by name, returning a value.

Trait Implementations§

source§

impl Default for Runtime

source§

fn default() -> Runtime

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Err = <U as TryFrom<T>>::Err

§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Err>