Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Embedding with Rust #171

Closed
bvssvni opened this issue May 16, 2016 · 0 comments
Closed

Embedding with Rust #171

bvssvni opened this issue May 16, 2016 · 0 comments

Comments

@bvssvni
Copy link
Member

bvssvni commented May 16, 2016

Dyon is designed to be used with Rust only, but not designed to replace Rust. It complements Rust on some areas where Rust has lacking features.

  • It uses no garbage collector, but instead uses a lifetime checker
  • For simple game and interactive programming environments
  • A simple object model to make introspection and reflection easy

Because Dyon uses a simple object model, the values must be converted when talking with Rust.

See functions example

A more complex example is the dyon_interactive library.

Functions that helps talking with Rust

  • dyon::embed::obj_field
  • Runtime::pop
  • Runtime::push
  • Runtime::pop_vec4
  • Runtime::push_vec4
  • Runtime::var
  • Runtime::expected

External functions

Example:

fn say_hello(_: &mut Runtime) -> Result<(), String> {
    println!("hi!");
    Ok(())
}

To use it with Dyon, it must be registered with a name and type information:

module.add(Arc::new("say_hello".into()), say_hello, PreludeFunction {
        lts: vec![], // lifetime constraints
        tys: vec![], // type constraints
        ret: Type::Void // return type
    });

If any arguments are mutated, add pattern (mut,_,_,...) to the function name.

For example, if 2 of 2 arguments are mutable to function foo, you register foo(mut,mut).

Mutating a variable on the stack

When mutating a variable on the stack, you need to resolve the reference first. This is because

let ind = rt.stack.pop().expect("There is no value on the stack");
let id = if let Variable::Ref(id) = rt.stack[ind] {
    id
} else {
    rt.stack[ind]
};
if let Variable::Array(ref mut arr) = rt.stack[id] {
    // mutate array here
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant