Skip to content

Commit

Permalink
feat: remove panic and pass
Browse files Browse the repository at this point in the history
  • Loading branch information
ofzo committed Feb 22, 2024
1 parent c4f84e8 commit b927a54
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 51 deletions.
34 changes: 20 additions & 14 deletions src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,23 @@ use crate::{
runtime::{ModuleInstance, Runtime},
};
use anyhow::anyhow;
use core::panic;
use std::{io::Result, path::PathBuf, sync::Arc};
use v8::Isolate;

#[derive(Debug)]
pub struct ModuleDependency {
pub deps: Vec<String>,
pub async_deps: Vec<String>,
pub specifier: Vec<String>,
pub specifiers: Vec<String>,
pub source: String,
pub map: Option<String>,
pub filename: String,
pub is_main: bool,
}

impl ModuleDependency {
pub fn initialize(&self, isolate: &mut Isolate) -> Option<()> {
pub fn initialize(&self, isolate: &mut Isolate) -> anyhow::Result<()> {
let graph_rc = Runtime::graph(isolate);

{
Expand All @@ -28,25 +29,25 @@ impl ModuleDependency {

let module = module.borrow();
if module.get(&self.filename).is_some() {
return Some(());
return Ok(());
}
}

// {
self.deps.iter().for_each(|url| {
let state = graph_rc.borrow();
let graph = state.table.borrow();
let state = graph_rc.borrow();
let graph = state.table.borrow();
for url in self.deps.iter() {
let url = resolve(url, &self.filename);
let dep = graph.get(&url).unwrap();
dep.initialize(isolate);
});
dep.initialize(isolate)?
}

// }
self.instantiate_module(isolate);
self.instantiate_module(isolate)?;

return Some(());
Ok(())
}
fn instantiate_module(&self, isolate: &mut Isolate) {
fn instantiate_module(&self, isolate: &mut Isolate) -> anyhow::Result<()> {
let state_rc = Runtime::state(isolate);
let graph_rc = Runtime::graph(isolate);

Expand Down Expand Up @@ -80,9 +81,13 @@ impl ModuleDependency {
.insert(module_id, self.filename.clone());

let tc_scope = &mut v8::TryCatch::new(scope);
module
.instantiate_module(tc_scope, Runtime::resolve_module_callback)
.unwrap();
let result = module.instantiate_module(tc_scope, Runtime::resolve_module_callback);
if result.is_none() {
let expection = tc_scope.exception().unwrap();
let msg = expection.to_rust_string_lossy(tc_scope);
return Err(anyhow!("{}", msg));
}

let expose = &module.get_module_namespace();
let v8_module = v8::Global::new(tc_scope, module);
let expose = v8::Global::new(tc_scope, expose);
Expand All @@ -95,6 +100,7 @@ impl ModuleDependency {
expose,
},
);
Ok(())
}

pub fn evaluate(&self, isolate: &mut Isolate) {
Expand Down
51 changes: 32 additions & 19 deletions src/compile_oxc.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
use core::panic;

use anyhow::anyhow;
use oxc_allocator::Allocator;
use oxc_ast::{AstKind, Visit};
use oxc_ast::{ast::ImportAttribute, AstKind, Visit};
use oxc_codegen::{Codegen, CodegenOptions};
use oxc_semantic::SemanticBuilder;
use oxc_span::{GetSpan, SourceType};
use oxc_transformer::{TransformOptions, TransformTarget, Transformer};
use tracing::Instrument;

use crate::compile::ModuleDependency;

#[derive(Debug, Default)]
struct ImportParser {
sync_imports: Vec<String>,
async_imports: Vec<String>,
specifiers: Vec<String>,
namespaces: Vec<String>,
default_import: bool,
}

impl<'a> oxc_ast::Visit<'a> for ImportParser {
Expand All @@ -35,13 +39,17 @@ impl<'a> oxc_ast::Visit<'a> for ImportParser {
use oxc_ast::ast::ImportDeclarationSpecifier;
match specifier {
ImportDeclarationSpecifier::ImportSpecifier(import_specifer) => {
println!("ImportDeclarationSpecifier: {:?}", import_specifer.span);
// println!("ImportSpecifier: {:?}", import_specifer.imported.name());
self.specifiers
.push(import_specifer.imported.name().to_string());
}
ImportDeclarationSpecifier::ImportDefaultSpecifier(import_specfier) => {
println!("ImportDeclarationSpecifier: {:?}", import_specfier.span);
ImportDeclarationSpecifier::ImportDefaultSpecifier(_import_specfier) => {
// println!("ImportDefaultSpecifier: {:?}", import_specfier);
self.default_import = true;
}
ImportDeclarationSpecifier::ImportNamespaceSpecifier(import_specifer) => {
println!("ImportDeclarationSpecifier: {:?}", import_specifer.span);
// println!("ImportNamespaceSpecifier: {:?}", import_specifer);
self.namespaces.push(import_specifer.local.name.to_string());
}
}
}
Expand All @@ -55,41 +63,46 @@ impl<'a> oxc_ast::Visit<'a> for ImportParser {
}
}

pub fn compile(file_name: &str, source_text: &str) -> anyhow::Result<ModuleDependency> {
pub fn compile(file_name: &str, content: &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_text, source_type).parse();
let ret = oxc_parser::Parser::new(&allo, &content, source_type).parse();

if !ret.errors.is_empty() {
for error in ret.errors {
let error = error.with_source_code(source_text.to_string());
let error = error.with_source_code(content.to_string());
println!("{error:?}");
}
return Err(anyhow!("compile error"));
}

let mut pass = ImportParser::default();
pass.visit_program(&ret.program);
let mut import_parser = ImportParser::default();
import_parser.visit_program(&ret.program);

let semantic = SemanticBuilder::new(&source_text, source_type)
let semantic = SemanticBuilder::new(&content, source_type)
.with_trivias(ret.trivias)
.build(&ret.program)
.semantic;

let program = allo.alloc(ret.program);
let transform_options = TransformOptions::default();

Transformer::new(&allo, source_type, semantic, transform_options)
.build(program)
.unwrap();
let transformer = Transformer::new(&allo, source_type, semantic, transform_options);

if let Err(errors) = transformer.build(program) {
for err in errors {
println!("{}", err);
}
panic!("");
}

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

Ok(ModuleDependency {
deps: pass.sync_imports,
async_deps: pass.async_imports,
specifier: vec![],
deps: import_parser.sync_imports,
async_deps: import_parser.async_imports,
specifiers: import_parser.specifiers,
source: code,
map: None,
filename: file_name.to_string(),
Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ mod compile_oxc;
use graph::resolve;
use graph::DependencyGraph;
use runtime::Runtime;
use tokio::main;

#[tokio::main]
#[main]
async fn main() -> anyhow::Result<()> {
let args = env::args().collect::<Vec<_>>();
if args.len() <= 1 {
Expand Down
13 changes: 5 additions & 8 deletions src/runtime/asynchronous.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,10 @@ pub enum AsynchronousKind {
}

impl AsynchronousKind {
pub fn exec(&self, isolate: &mut Isolate) -> Poll<()> {
match match self {
pub fn exec(&self, isolate: &mut Isolate) -> anyhow::Result<Poll<()>> {
match self {
AsynchronousKind::Operation(id) => Self::operation(isolate, id.clone()),
AsynchronousKind::Import((source, resolver)) => Self::import(isolate, source, resolver),
} {
Ok(v) => v,
Err(e) => panic!("error {}", e),
}
}

Expand Down Expand Up @@ -83,7 +80,7 @@ impl AsynchronousKind {
.get(source)
.ok_or(anyhow!("source `{}` not found", source))?;

if dep.initialize(isolate).is_some() {
if dep.initialize(isolate).is_ok() {
dep.evaluate(isolate);

let scope = &mut v8::HandleScope::with_context(isolate, context);
Expand All @@ -103,9 +100,9 @@ impl AsynchronousKind {
let module = graph.module.borrow();
if module.get(source).is_none() {
let t = table.get(source).unwrap();
t.initialize(isolate);
t.initialize(isolate)?;
}

return Ok(Poll::Ready(()));
Ok(Poll::Ready(()))
}
}
9 changes: 7 additions & 2 deletions src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::{
use crate::{compile, graph::DependencyGraph};
use futures::{stream::FuturesUnordered, Future, StreamExt};
use tokio::sync::mpsc::{self, Receiver, Sender};
use tracing::Instrument;
use v8::{Isolate, OwnedIsolate};

mod asynchronous;
Expand Down Expand Up @@ -123,7 +124,7 @@ impl Runtime {
&include_str!("../../bootstrap/main.ts").to_string(),
)?;

dependency.initialize(isolate);
dependency.initialize(isolate)?;
dependency.evaluate(isolate);

//
Expand Down Expand Up @@ -178,7 +179,11 @@ impl Runtime {
}
result
} {
break op.exec(isolate);
match op.exec(isolate) {
Ok(v) => break v,
Err(err) => eprintln!("{err:?}"),
}
break Poll::Ready(());
}
break Poll::Pending;
})
Expand Down
6 changes: 3 additions & 3 deletions src/runtime/static_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use url::Url;
impl Runtime {
pub fn resolve_module_callback<'s>(
context: v8::Local<'s, v8::Context>,
specifier: v8::Local<'s, v8::String>,
source: 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 = specifier.to_rust_string_lossy(scope);
let source = source.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 @@ -38,7 +38,7 @@ impl Runtime {
.expect(format!("get module failure: {}", url).as_str());
let module = v8::Local::new(scope, &info.module);

return Some(module);
Some(module)
}

pub fn dynamically_import<'a>(
Expand Down
8 changes: 4 additions & 4 deletions test/test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// import "https://deno.land/[email protected]/examples/welcome.ts";
// import "https://deno.land/[email protected]/examples/welcome.ts";
import "./welcome.ts"
// import {a ,b,c} from "https://deno.land/[email protected]/examples/welcome.ts";
import "https://deno.land/[email protected]/examples/welcome.ts";
import {a} from "./welcome.ts"

// const res = await import("./test1.ts");
// console.log("res", res);
console.log(a);
// const text = await res.json();
// console.log("🚀 text", text);
// test.copy();
Expand Down
3 changes: 3 additions & 0 deletions test/welcome.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
console.log("Welcome edon!");

const a = 1;
export {a}

0 comments on commit b927a54

Please sign in to comment.