diff --git a/src/compile.rs b/src/compile.rs index e1e3b5e..2a5f1b1 100644 --- a/src/compile.rs +++ b/src/compile.rs @@ -131,7 +131,7 @@ impl ModuleDependency { let tc_scope = &mut v8::TryCatch::new(scope); module - .instantiate_module(tc_scope, Runtime::resolve) + .instantiate_module(tc_scope, Runtime::resolve_module_callback) .unwrap(); let expose = &module.get_module_namespace(); let v8_module = v8::Global::new(tc_scope, module); @@ -154,7 +154,10 @@ impl ModuleDependency { self.deps.iter().for_each(|url| { let graph = graph_rc.borrow(); let table = graph.table.borrow(); - let dep = table.get(url).unwrap(); + let url = resolve(url, &self.filename); + let dep = table + .get(&url) + .expect(&format!("table get failure `{url}`")); dep.evaluate(isolate); }); @@ -181,8 +184,8 @@ impl ModuleDependency { } } -pub fn compile(file_name: &str, source: &str) -> anyhow::Result { - return compile_oxc::compile(file_name, source); +pub fn compile(file_name: &str, source_text: &str) -> anyhow::Result { + return compile_oxc::compile(file_name, source_text); let cm = Arc::::default(); let handler = Arc::new(Handler::with_tty_emitter( @@ -198,7 +201,7 @@ pub fn compile(file_name: &str, source: &str) -> anyhow::Result FileName::Url(url), Err(_) => FileName::Real(PathBuf::from(file_name)), }, - source.to_string(), + source_text.to_string(), ); let lexer = Lexer::new( diff --git a/src/compile_oxc.rs b/src/compile_oxc.rs index c9cd6ba..3d0c34f 100644 --- a/src/compile_oxc.rs +++ b/src/compile_oxc.rs @@ -55,15 +55,15 @@ impl<'a> oxc_ast::Visit<'a> for ImportParser { } } -pub fn compile(file_name: &str, source: &str) -> anyhow::Result { +pub fn compile(file_name: &str, source_text: &str) -> anyhow::Result { let allo = Allocator::default(); let source_type = oxc_span::SourceType::from_path(file_name).unwrap(); - let ret = oxc_parser::Parser::new(&allo, &source, source_type).parse(); + let ret = oxc_parser::Parser::new(&allo, &source_text, source_type).parse(); if !ret.errors.is_empty() { for error in ret.errors { - let error = error.with_source_code(source.to_string()); + let error = error.with_source_code(source_text.to_string()); println!("{error:?}"); } return Err(anyhow!("compile error")); @@ -72,7 +72,7 @@ pub fn compile(file_name: &str, source: &str) -> anyhow::Result anyhow::Result::new(source.len(), CodegenOptions).build(program); + let code = Codegen::::new(source_text.len(), CodegenOptions).build(program); Ok(ModuleDependency { deps: pass.sync_imports, diff --git a/src/graph.rs b/src/graph.rs index 623e6e9..dec6a82 100644 --- a/src/graph.rs +++ b/src/graph.rs @@ -81,7 +81,7 @@ impl DependencyGraph { } Ok(()) } - pub fn get(&self, specifier: &String) -> Option<&ModuleDependency> { - self.0.get(specifier) + pub fn get(&self, source: &String) -> Option<&ModuleDependency> { + self.0.get(source) } } diff --git a/src/runtime/asynchronous.rs b/src/runtime/asynchronous.rs index 1d87b67..ed94b53 100644 --- a/src/runtime/asynchronous.rs +++ b/src/runtime/asynchronous.rs @@ -81,8 +81,7 @@ impl AsynchronousKind { let table = graph.table.borrow(); let dep = table .get(source) - .ok_or(anyhow!("specifier `{}` not found", source))?; - // .expect(&format!("specifier `{}` not found", specifier)); + .ok_or(anyhow!("source `{}` not found", source))?; if dep.initialize(isolate).is_some() { dep.evaluate(isolate); @@ -101,6 +100,12 @@ impl AsynchronousKind { }; }; + let module = graph.module.borrow(); + if module.get(source).is_none() { + let t = table.get(source).unwrap(); + t.initialize(isolate); + } + return Ok(Poll::Ready(())); } } diff --git a/src/runtime/static_fn.rs b/src/runtime/static_fn.rs index cb2293f..a8a719f 100644 --- a/src/runtime/static_fn.rs +++ b/src/runtime/static_fn.rs @@ -4,9 +4,9 @@ use std::{task::Poll, time::Duration}; use url::Url; impl Runtime { - pub fn resolve<'s>( + pub fn resolve_module_callback<'s>( context: v8::Local<'s, v8::Context>, - source: v8::Local<'s, v8::String>, + specifier: v8::Local<'s, v8::String>, _import_assertions: v8::Local<'s, v8::FixedArray>, referrer: v8::Local<'s, v8::Module>, ) -> Option> { @@ -16,7 +16,7 @@ impl Runtime { let state = graph_rc.borrow(); - let source = source.to_rust_string_lossy(scope); + let source = specifier.to_rust_string_lossy(scope); let url = if source.starts_with("http") { let url = Url::parse(&source).expect(format!("parse url failed: {}", source).as_str()); @@ -28,7 +28,8 @@ impl Runtime { let hash = state.hash.borrow(); let url = hash.get(&module_id).unwrap(); - url.clone() + resolve(&source, url) + // url.clone() }; let module = state.module.borrow();