Skip to content

Commit

Permalink
fixup! fixup! print js error in console.log
Browse files Browse the repository at this point in the history
  • Loading branch information
ofzo committed Feb 20, 2024
1 parent 8ef5c4f commit 9d04037
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 18 deletions.
13 changes: 8 additions & 5 deletions src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
});

Expand All @@ -181,8 +184,8 @@ impl ModuleDependency {
}
}

pub fn compile(file_name: &str, source: &str) -> anyhow::Result<ModuleDependency> {
return compile_oxc::compile(file_name, source);
pub fn compile(file_name: &str, source_text: &str) -> anyhow::Result<ModuleDependency> {
return compile_oxc::compile(file_name, source_text);

let cm = Arc::<SourceMap>::default();
let handler = Arc::new(Handler::with_tty_emitter(
Expand All @@ -198,7 +201,7 @@ pub fn compile(file_name: &str, source: &str) -> anyhow::Result<ModuleDependency
Ok(url) => FileName::Url(url),
Err(_) => FileName::Real(PathBuf::from(file_name)),
},
source.to_string(),
source_text.to_string(),
);

let lexer = Lexer::new(
Expand Down
10 changes: 5 additions & 5 deletions src/compile_oxc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ impl<'a> oxc_ast::Visit<'a> for ImportParser {
}
}

pub fn compile(file_name: &str, source: &str) -> anyhow::Result<ModuleDependency> {
pub fn compile(file_name: &str, source_text: &str) -> anyhow::Result<ModuleDependency> {
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"));
Expand All @@ -72,7 +72,7 @@ pub fn compile(file_name: &str, source: &str) -> anyhow::Result<ModuleDependency
let mut pass = ImportParser::default();
pass.visit_program(&ret.program);

let semantic = SemanticBuilder::new(&source, source_type)
let semantic = SemanticBuilder::new(&source_text, source_type)
.with_trivias(ret.trivias)
.build(&ret.program)
.semantic;
Expand All @@ -84,7 +84,7 @@ pub fn compile(file_name: &str, source: &str) -> anyhow::Result<ModuleDependency
.build(program)
.unwrap();

let code = Codegen::<false>::new(source.len(), CodegenOptions).build(program);
let code = Codegen::<false>::new(source_text.len(), CodegenOptions).build(program);

Ok(ModuleDependency {
deps: pass.sync_imports,
Expand Down
4 changes: 2 additions & 2 deletions src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
9 changes: 7 additions & 2 deletions src/runtime/asynchronous.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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(()));
}
}
9 changes: 5 additions & 4 deletions src/runtime/static_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<v8::Local<'s, v8::Module>> {
Expand All @@ -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());
Expand All @@ -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();
Expand Down

0 comments on commit 9d04037

Please sign in to comment.