diff --git a/src/script.rs b/src/script.rs index 320d1c25..d57a392f 100644 --- a/src/script.rs +++ b/src/script.rs @@ -1,9 +1,40 @@ -use oden_js::{Context, Runtime, Value}; +use oden_js::{ + module::loader::{ModuleLoader, ModuleSource}, + Context, ContextRef, Result, Runtime, Value, +}; +use std::ffi::OsStr; +use std::path::Path; use std::sync::mpsc::{channel, Receiver}; pub mod graphics; use graphics::GraphicsCommand; +fn transpile_to_javascript(_path: &str, input: String) -> Result { + Ok(input) +} + +struct Loader {} + +impl Loader { + pub fn new() -> Loader { + Loader {} + } +} + +impl ModuleLoader for Loader { + fn load(&self, _context: &ContextRef, name: &str) -> Result { + let path = Path::new(name); + let contents = std::fs::read_to_string(path)?; + let contents = if path.extension().and_then(OsStr::to_str) == Some("ts") { + transpile_to_javascript(name, contents)? + } else { + contents + }; + + Ok(ModuleSource::JavaScript(contents)) + } +} + pub struct ScriptContext { context: Context, init: Value, @@ -16,7 +47,7 @@ pub struct ScriptContext { impl ScriptContext { pub fn new() -> Self { - let runtime = Runtime::new(); + let runtime = Runtime::with_loader(Loader::new()); let mut context = Context::new(runtime); context.add_intrinsic_bigfloat();