~boringcactus/rust-scripting-languages

0d235014390c1789e380b65b87da0daa0a510377 — Melody Horn 3 years ago 3ae0766 master
write ruwren example
4 files changed, 257 insertions(+), 1 deletions(-)

M Cargo.lock
M Cargo.toml
A examples/ruwren.rs
A scripts/ruwren-sample.wren
M Cargo.lock => Cargo.lock +165 -1
@@ 46,6 46,15 @@ dependencies = [
]

[[package]]
name = "ansi_term"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b"
dependencies = [
 "winapi 0.3.9",
]

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


@@ 159,6 168,30 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3441f0f7b02788e948e47f457ca01f1d7e6d92c693bc132c22b087d3141c03ff"

[[package]]
name = "bindgen"
version = "0.54.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "66c0bb6167449588ff70803f4127f0684f9063097eca5016f37eb52b92c2cf36"
dependencies = [
 "bitflags",
 "cexpr",
 "cfg-if",
 "clang-sys",
 "clap",
 "env_logger",
 "lazy_static 1.4.0",
 "lazycell",
 "log",
 "peeking_take_while",
 "proc-macro2",
 "quote",
 "regex 1.3.9",
 "rustc-hash",
 "shlex",
 "which 3.1.1",
]

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


@@ 262,6 295,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ef611cc68ff783f18535d77ddd080185275713d852c4f5cbb6122c462a7a825c"

[[package]]
name = "cexpr"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f4aedb84272dbe89af497cf81375129abda4fc0a9e7c5d317498c15cc30c0d27"
dependencies = [
 "nom 5.1.2",
]

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


@@ 278,6 320,32 @@ dependencies = [
]

[[package]]
name = "clang-sys"
version = "0.29.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fe6837df1d5cba2397b835c8530f51723267e16abbf83892e9e5af4f0e5dd10a"
dependencies = [
 "glob",
 "libc",
 "libloading",
]

[[package]]
name = "clap"
version = "2.33.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002"
dependencies = [
 "ansi_term",
 "atty",
 "bitflags",
 "strsim 0.8.0",
 "textwrap",
 "unicode-width",
 "vec_map",
]

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


@@ 530,7 598,7 @@ dependencies = [
 "serde_json",
 "uname",
 "walkdir",
 "which",
 "which 4.0.2",
 "whoami",
]



@@ 640,6 708,19 @@ dependencies = [
]

[[package]]
name = "env_logger"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36"
dependencies = [
 "atty",
 "humantime",
 "log",
 "regex 1.3.9",
 "termcolor",
]

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


@@ 1213,6 1294,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cd179ae861f0c2e53da70d892f5f3029f9594be0c41dc5269cd371691b1dc2f9"

[[package]]
name = "humantime"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df004cfca50ef23c36850aaaa59ad52cc70d0e90243c3c7737a4dd32dc7a3c4f"
dependencies = [
 "quick-error",
]

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


@@ 1396,12 1486,28 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"

[[package]]
name = "lazycell"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55"

[[package]]
name = "libc"
version = "0.2.77"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2f96b10ec2560088a8e76961b00d47107b3a625fecb76dedb29ee7ccbf98235"

[[package]]
name = "libloading"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2b111a074963af1d37a139918ac6d49ad1d0d5e47f72fd55388619691a7d753"
dependencies = [
 "cc",
 "winapi 0.3.9",
]

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


@@ 1885,6 1991,12 @@ dependencies = [
]

[[package]]
name = "peeking_take_while"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099"

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


@@ 2452,6 2564,7 @@ dependencies = [
 "rlua",
 "rune",
 "runestick",
 "ruwren",
]

[[package]]


@@ 2476,6 2589,27 @@ dependencies = [
]

[[package]]
name = "ruwren"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85322732ed65ec8dd636d7dbe27739425ed8f1bdd5db038678a73bab6d92156c"
dependencies = [
 "ruwren-sys",
]

[[package]]
name = "ruwren-sys"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "04a4e95df99b662a80bcfa659de5d22f853f5a8f61e5bf12c993468400c3120b"
dependencies = [
 "bindgen",
 "cc",
 "glob",
 "libc",
]

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


@@ 2609,6 2743,12 @@ dependencies = [
]

[[package]]
name = "shlex"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7fdf1b9db47230893d76faad238fd6097fd6d6a9245cd7a4d90dbd639536bbd2"

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


@@ 2674,6 2814,12 @@ dependencies = [

[[package]]
name = "strsim"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"

[[package]]
name = "strsim"
version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6446ced80d6c486436db5c078dde11a9f73d42b57fb273121e160b84f63d894c"


@@ 2755,6 2901,15 @@ dependencies = [
]

[[package]]
name = "textwrap"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
dependencies = [
 "unicode-width",
]

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


@@ 3234,6 3389,15 @@ dependencies = [

[[package]]
name = "which"
version = "3.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d011071ae14a2f6671d0b74080ae0cd8ebf3a6f8c9589a2cd45f23126fe29724"
dependencies = [
 "libc",
]

[[package]]
name = "which"
version = "4.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "87c14ef7e1b8b8ecfc75d5eca37949410046e66f15d185c01d70824f1f8111ef"

M Cargo.toml => Cargo.toml +8 -0
@@ 71,3 71,11 @@ optional = true
[[example]]
name = "rune"
required-features = ["rune", "runestick"]

[dependencies.ruwren]
version = "0.3.1"
optional = true

[[example]]
name = "ruwren"
required-features = ["ruwren"]

A examples/ruwren.rs => examples/ruwren.rs +73 -0
@@ 0,0 1,73 @@
use std::{error::Error, fs::read_to_string};

use ruwren::{Class, create_module, FunctionSignature, get_slot_checked, ModuleLibrary, VM, VMConfig, VMWrapper};

use rust_scripting_languages::*;

struct Demo;

impl Class for Demo {
    fn initialize(_: &VM) -> Self {
        Demo
    }
}

impl Demo {
    fn print_fancy(vm: &VM) {
        let text = get_slot_checked!(vm => string 1);
        print_fancy(&text);
    }
}

create_module! {
    class("Demo") crate::Demo => foo {
        static(fn "print_fancy", 1) print_fancy
    }

    module => demo
}

struct RuwrenScriptHost;

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

    fn new_context() -> Result<Self::ScriptContext, Self::Error> {
        let mut modules = ModuleLibrary::new();
        demo::publish_module(&mut modules);

        let vm = VMConfig::new()
            .library(&modules)
            .build();

        vm.interpret("demo", r##"
        foreign class Demo {
            foreign static print_fancy(string)
        }
        "##)?;

        let script = read_to_string("scripts/ruwren-sample.wren")?;
        vm.interpret("main", &script)?;

        Ok(vm)
    }

    fn handle_event(vm: &mut VMWrapper, event: Event) -> Result<(), Self::Error> {
        vm.execute(|vm| {
            vm.ensure_slots(2);
            vm.get_variable("main", "EventHandler", 0);
            match &event {
                Event::Number(n) => vm.set_slot_double(1, *n as f64),
                Event::Text(t) => vm.set_slot_string(1, t),
            }
        });
        vm.call(FunctionSignature::new_function("handleEvent", 1))?;

        Ok(())
    }
}

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

A scripts/ruwren-sample.wren => scripts/ruwren-sample.wren +11 -0
@@ 0,0 1,11 @@
import "demo" for Demo
class EventHandler {
    static handleEvent(data) {
        if (data is Num) {
            System.print("Number! %(data)")
        } else {
            System.print("Text! %(data)")
        }
        Demo.print_fancy("got an event")
    }
}