From b884af0380251aad792edd2aed0ab90f1a566626 Mon Sep 17 00:00:00 2001 From: Jarrod Doyle Date: Fri, 8 Mar 2024 12:18:20 +0000 Subject: [PATCH] Move scripting runtime out of main --- src/main.rs | 33 ++++---------------------- src/scripting/mod.rs | 3 +++ src/scripting/runtime.rs | 51 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 28 deletions(-) create mode 100644 src/scripting/runtime.rs diff --git a/src/main.rs b/src/main.rs index 0eaa5bf..ce00b33 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,16 +2,9 @@ mod gfx; mod input; mod scripting; -use std::sync::Arc; - use anyhow::Result; use gfx::Context; use input::Input; -use rune::{ - runtime::Object, - termcolor::{ColorChoice, StandardStream}, - Diagnostics, Source, Sources, Vm, -}; use wgpu::Limits; use winit::{ dpi::LogicalSize, @@ -32,27 +25,9 @@ pub fn main() -> Result<()> { } pub fn run(event_loop: EventLoop<()>, mut context: Context) -> Result<()> { - // !HACK: Temporrary scripting engine setup - let source_dir = format!("{}/scripts", env!("CARGO_MANIFEST_DIR")); - let mut rune_context = rune::Context::with_default_modules()?; - rune_context.install(scripting::api::log::module()?)?; - - let runtime = Arc::new(rune_context.runtime()?); - let mut sources = Sources::new(); - sources.insert(Source::from_path(format!("{source_dir}/frame_counter.rn"))?)?; - sources.insert(Source::memory("pub fn add(a, b) { a + b }")?)?; - let mut diagnostics = Diagnostics::new(); - let result = rune::prepare(&mut sources) - .with_context(&rune_context) - .with_diagnostics(&mut diagnostics) - .build(); - if !diagnostics.is_empty() { - let mut writer = StandardStream::stderr(ColorChoice::Always); - diagnostics.emit(&mut writer, &sources)?; - } - let mut vm = Vm::new(runtime, Arc::new(result?)); - let frame_counter = vm.call(["FrameCounter", "new"], ())?; + let mut runtime = scripting::Runtime::new(&["frame_counter"])?; + let frame_counter = runtime.vm.call(["FrameCounter", "new"], ())?; let mut input = Input::new(); event_loop.run(|event, elwt| { @@ -76,7 +51,9 @@ pub fn run(event_loop: EventLoop<()>, mut context: Context) -> Result<()> { elwt.exit(); } - vm.call(["FrameCounter", "update"], (&frame_counter,)) + runtime + .vm + .call(["FrameCounter", "update"], (&frame_counter,)) .unwrap(); })?; diff --git a/src/scripting/mod.rs b/src/scripting/mod.rs index e5fdf85..8cfccbd 100644 --- a/src/scripting/mod.rs +++ b/src/scripting/mod.rs @@ -1 +1,4 @@ pub mod api; +mod runtime; + +pub use runtime::Runtime; diff --git a/src/scripting/runtime.rs b/src/scripting/runtime.rs new file mode 100644 index 0000000..4a00aad --- /dev/null +++ b/src/scripting/runtime.rs @@ -0,0 +1,51 @@ +use std::sync::Arc; + +use anyhow::Result; +use rune::{ + termcolor::{ColorChoice, StandardStream}, + Context, Diagnostics, Source, Sources, Vm, +}; + +use super::api; + +pub struct Runtime { + pub vm: Vm, + pub sources: Vec, +} + +impl Runtime { + pub fn new(source_paths: &[&str]) -> Result { + let mut full_source_paths = vec![]; + + let source_dir = format!("{}/scripts", env!("CARGO_MANIFEST_DIR")); + let mut sources = Sources::new(); + for i in 0..source_paths.len() { + let path = format!("{source_dir}/{}.rn", source_paths[i]); + full_source_paths.push(path.clone()); + sources.insert(Source::from_path(path)?)?; + } + + let mut context = Context::with_default_modules()?; + context.install(api::log::module()?)?; + + let runtime = Arc::new(context.runtime()?); + let mut diagnostics = Diagnostics::new(); + + let unit = rune::prepare(&mut sources) + .with_context(&context) + .with_diagnostics(&mut diagnostics) + .build()?; + + if !diagnostics.is_empty() { + let mut writer = StandardStream::stderr(ColorChoice::Always); + diagnostics.emit(&mut writer, &sources)?; + } + + let vm = Vm::new(runtime, Arc::new(unit)); + + Ok(Self { + vm, + sources: full_source_paths, + }) + } +}