[quickjs][oden][oden-js] Source maps

The worst support but it should cost very little if nobody is using
them (psst we're using them)
This commit is contained in:
John Doty 2023-09-20 09:31:57 -07:00
parent eb9fed759a
commit c96a1a4979
10 changed files with 554 additions and 374 deletions

View file

@ -1,6 +1,5 @@
use super::ModuleRef;
use crate::{throw_error, ContextRef, Error, Result};
use oden_js_sys as sys;
use crate::{Atom, AtomRef, ContextRef, Error, Result};
use std::path::Path;
pub enum ModuleSource<'a> {
@ -9,7 +8,22 @@ pub enum ModuleSource<'a> {
}
pub trait ModuleLoader {
fn load(&self, context: &ContextRef, name: &str) -> Result<ModuleSource>;
fn load<'a>(&mut self, context: &'a ContextRef, name: &str) -> Result<ModuleSource<'a>>;
// Return true if this loader also supports source maps, otherwise false.
// NOTE: This must return `true` if you want to have a non-trivial
// implementation of `map_source`; if this returns `false` (the
// default) then your object will never even receive a map_source
// call.
fn support_source_map(&self) -> bool {
false
}
// Map a given file/line pair to the source file/line pair. This is only
// called if `support_source_map` returns true.
fn map_source(&self, context: &ContextRef, file: &AtomRef, line: i32) -> Result<(Atom, i32)> {
Ok((file.dup(context), line))
}
}
pub struct DefaultModuleLoader {}
@ -21,7 +35,7 @@ impl DefaultModuleLoader {
}
impl ModuleLoader for DefaultModuleLoader {
fn load(&self, _context: &ContextRef, name: &str) -> Result<ModuleSource> {
fn load<'a>(&mut self, _context: &'a ContextRef, name: &str) -> Result<ModuleSource<'a>> {
// Attempt to open the file.
let path = Path::new(name);
match std::fs::read_to_string(path) {
@ -30,24 +44,3 @@ impl ModuleLoader for DefaultModuleLoader {
}
}
}
pub(crate) fn load_module(
context: &ContextRef,
name: &str,
loader: &Box<dyn ModuleLoader>,
) -> *mut sys::JSModuleDef {
match loader.load(context, name) {
Ok(ModuleSource::Native(native)) => native.module,
Ok(ModuleSource::JavaScript(js)) => match context.eval_module(&js, name) {
Ok(v) => v.module,
Err(e) => {
throw_error(context, e);
return std::ptr::null_mut();
}
},
Err(e) => {
throw_error(context, e);
return std::ptr::null_mut();
}
}
}

View file

@ -8,7 +8,7 @@ pub mod native;
#[derive(Debug, Clone)]
pub struct ModuleRef {
module: *mut sys::JSModuleDef,
pub(crate) module: *mut sys::JSModuleDef,
}
impl ModuleRef {