Move scripting runtime out of main

This commit is contained in:
Jarrod Doyle 2024-03-08 12:18:20 +00:00
parent 1e610991be
commit b884af0380
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
3 changed files with 59 additions and 28 deletions

View File

@ -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();
})?;

View File

@ -1 +1,4 @@
pub mod api;
mod runtime;
pub use runtime::Runtime;

51
src/scripting/runtime.rs Normal file
View File

@ -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<String>,
}
impl Runtime {
pub fn new(source_paths: &[&str]) -> Result<Self> {
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,
})
}
}