[oden] Start on custom loader
This commit is contained in:
parent
1b8f8d41e5
commit
cc21e8c406
1 changed files with 33 additions and 2 deletions
|
|
@ -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<String> {
|
||||
Ok(input)
|
||||
}
|
||||
|
||||
struct Loader {}
|
||||
|
||||
impl Loader {
|
||||
pub fn new() -> Loader {
|
||||
Loader {}
|
||||
}
|
||||
}
|
||||
|
||||
impl ModuleLoader for Loader {
|
||||
fn load(&self, _context: &ContextRef, name: &str) -> Result<ModuleSource> {
|
||||
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();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue