~boringcactus/rust-scripting-languages

3ae0766184a252e247b3bad1001c66a63bfe3bcb — Melody Horn 3 years ago 0f65b28
write rune example
4 files changed, 163 insertions(+), 4 deletions(-)

M Cargo.lock
M Cargo.toml
A examples/rune.rs
A scripts/rune-sample.rn
M Cargo.lock => Cargo.lock +99 -4
@@ 46,6 46,12 @@ dependencies = [
]

[[package]]
name = "anyhow"
version = "1.0.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6b602bfe940d21c130f3895acd65221e8a61270debe89d628b9cb4e3ccb8569b"

[[package]]
name = "archery"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"


@@ 1633,11 1639,25 @@ version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8536030f9fea7127f841b45bb6243b27255787fb4eb83958aa1ef9d2fdc0c36"
dependencies = [
 "num-bigint",
 "num-complex",
 "num-bigint 0.2.6",
 "num-complex 0.2.4",
 "num-integer",
 "num-iter",
 "num-rational 0.2.4",
 "num-traits",
]

[[package]]
name = "num"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ab3e176191bc4faad357e3122c4747aa098ac880e88b168f106386128736cf4a"
dependencies = [
 "num-bigint 0.3.0",
 "num-complex 0.3.0",
 "num-integer",
 "num-iter",
 "num-rational",
 "num-rational 0.3.0",
 "num-traits",
]



@@ 1653,6 1673,17 @@ dependencies = [
]

[[package]]
name = "num-bigint"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b7f3fc75e3697059fb1bc465e3d8cca6cf92f56854f201158b3f9c77d5a3cfa0"
dependencies = [
 "autocfg 1.0.1",
 "num-integer",
 "num-traits",
]

[[package]]
name = "num-complex"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"


@@ 1663,6 1694,15 @@ dependencies = [
]

[[package]]
name = "num-complex"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b05ad05bd8977050b171b3f6b48175fea6e0565b7981059b486075e1026a9fb5"
dependencies = [
 "num-traits",
]

[[package]]
name = "num-integer"
version = "0.1.43"
source = "registry+https://github.com/rust-lang/crates.io-index"


@@ 1690,7 1730,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c000134b5dbf44adc5cb772486d335293351644b801551abe8f75c84cfa4aef"
dependencies = [
 "autocfg 1.0.1",
 "num-bigint",
 "num-bigint 0.2.6",
 "num-integer",
 "num-traits",
]

[[package]]
name = "num-rational"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a5b4d7360f362cfb50dde8143501e6940b22f644be75a4cc90b2d81968908138"
dependencies = [
 "autocfg 1.0.1",
 "num-bigint 0.3.0",
 "num-integer",
 "num-traits",
]


@@ 2343,6 2395,38 @@ dependencies = [
]

[[package]]
name = "rune"
version = "0.6.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "75936bbf8412d979592e859048e78f48ee00f1ffb93290cc6a16d7e7047bf15c"
dependencies = [
 "codespan-reporting",
 "hashbrown 0.8.2",
 "log",
 "num 0.3.0",
 "runestick",
 "thiserror",
]

[[package]]
name = "runestick"
version = "0.6.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f45fc56d81f3c53ff3ae3120b8ff588019ed75caa3349dafc741145dc66ddf84"
dependencies = [
 "anyhow",
 "futures 0.3.5",
 "hashbrown 0.8.2",
 "itoa",
 "log",
 "pin-project",
 "ryu",
 "serde",
 "thiserror",
 "twox-hash",
]

[[package]]
name = "rust-argon2"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"


@@ 2366,6 2450,8 @@ dependencies = [
 "ketos",
 "rhai",
 "rlua",
 "rune",
 "runestick",
]

[[package]]


@@ 2894,6 2980,15 @@ dependencies = [
]

[[package]]
name = "twox-hash"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3bfd5b7557925ce778ff9b9ef90e3ade34c524b5ff10e239c69a42d546d2af56"
dependencies = [
 "rand 0.7.3",
]

[[package]]
name = "typed-arena"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"

M Cargo.toml => Cargo.toml +12 -0
@@ 59,3 59,15 @@ optional = true
[[example]]
name = "ketos"
required-features = ["ketos"]

[dependencies.rune]
version = "0.6.16"
optional = true

[dependencies.runestick]
version = "0.6.16"
optional = true

[[example]]
name = "rune"
required-features = ["rune", "runestick"]

A examples/rune.rs => examples/rune.rs +45 -0
@@ 0,0 1,45 @@
use std::{sync::Arc, error::Error, fs::read_to_string};

use runestick::{Source, Value, Vm};

use rust_scripting_languages::*;

struct RuneScriptHost;

impl ScriptHost for RuneScriptHost {
    type ScriptContext = Vm;
    type Error = Box<dyn Error + 'static>;

    fn new_context() -> Result<Vm, Self::Error> {
        let mut context = rune::default_context()?;

        let mut module = runestick::Module::default();
        module.function(&["print_fancy"], print_fancy)?;

        context.install(&module)?;

        let script = read_to_string("scripts/rune-sample.rn")?;

        let mut warnings = rune::Warnings::new();
        let unit = rune::load_source(&context, &Default::default(), Source::new("scripts/rune-sample.rn", &script), &mut warnings)?;

        let vm = Vm::new(Arc::new(context), Arc::new(unit));

        Ok(vm)
    }

    fn handle_event(vm: &mut Vm, event: Event) -> Result<(), Self::Error> {
        let arg = match event {
            Event::Number(n) => Value::from(n),
            Event::Text(t) => Value::from(t)
        };
        vm.clone().call(&["handle_event"], (arg,))?.complete()?;

        Ok(())
    }
}

fn main() -> Result<(), Box<dyn Error + 'static>> {
    run_main::<RuneScriptHost>()
}


A scripts/rune-sample.rn => scripts/rune-sample.rn +7 -0
@@ 0,0 1,7 @@
fn handle_event(event) {
    match event {
        n if n is int => println(`Number! {n}`),
        t => println(`Text! {t}`),
    }
    print_fancy("got an event!")
}