Skip to content

Commit

Permalink
feat: remove panic and pass
Browse files Browse the repository at this point in the history
feat: update dependencies
  • Loading branch information
ofzo committed May 30, 2024
1 parent c4f84e8 commit 7665b07
Show file tree
Hide file tree
Showing 15 changed files with 659 additions and 871 deletions.
1,154 changes: 410 additions & 744 deletions Cargo.lock

Large diffs are not rendered by default.

45 changes: 18 additions & 27 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,29 @@ version = "0.1.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
chrono = "0.4.31"
chrono = "0.4.38"
colored = "2.1.0"
console-subscriber = "0.2.0"
env_logger = "0.10.1"
env_logger = "0.11.3"
futures = "0.3.30"
log = "0.4.20"
queues = "1.1.0"
quote = "1.0.35"
regex = "1.10.2"
relative-path = "1.9.2"
reqwest = { version = "0.11.23", features = ["blocking"] }
serde_json = "1.0.111"
serde_v8 = "0.154.0"
# swc = "0.270.20"
# swc_common = { version = "0.33.12", features = ["tty-emitter"] }
# swc_ecma_ast = "0.110.17"
# swc_ecma_parser = "0.141.37"
# swc_ecma_visit = "0.96.17"
# swc_ecma_transforms_module = "0.178.16"
quote = "1.0.36"
regex = "1.10.4"
relative-path = "1.9.3"
reqwest = { version = "0.12.4", features = ["blocking"] }
serde_json = "1.0.117"
serde_v8 = "0.182.0"
url = "2.5.0"
v8 = "0.82.0"
anyhow = "1.0.79"
tracing = "0.1.40"
v8 = "0.89.0"
anyhow = "1.0.83"
# oxc
oxc_allocator = "^0.6.0"
oxc_codegen = "^0.6.0"
oxc_parser = "^0.6.0"
oxc_transformer = "^0.6.0"
oxc_span= "^0.6.0"
oxc_ast = "^0.6.0"
oxc_semantic = "^0.6.0"
oxc_allocator = "^0.13.0"
oxc_codegen = "^0.13.0"
oxc_parser = "^0.13.0"
oxc_transformer = "^0.13.0"
oxc_span = "^0.13.0"
oxc_ast = "^0.13.0"
oxc_semantic = "^0.13.0"

[dependencies.tokio]
features = ["full"]
version = "1.35.1"
version = "1.37.0"
2 changes: 1 addition & 1 deletion bootstrap/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default async function bootstrap(entry: string) {
asyncHandle: [],
count: 0,
}
//@ts-ignore
// @ts-ignore
globalThis.setTimeout = (fn: Function, delay: number, ...arg: any[]) => {
runtime.asyncHandle[runtime.count] = () => {
fn(...arg)
Expand Down
2 changes: 1 addition & 1 deletion src/builtin/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ pub fn console_format(
.get_own_property_names(scope, Default::default())
.unwrap();

let mut fmt = (0..names.length())
let fmt = (0..names.length())
.map(|index| {
let name = names.get_index(scope, index).unwrap();
let val = obj.get(scope, name).unwrap();
Expand Down
88 changes: 88 additions & 0 deletions src/builtin/fetch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
use crate::runtime::AsynchronousKind;
use std::{str::FromStr, sync::Arc, task::Poll};

use core::pin::Pin;
use reqwest::{self, Client, Method};

use crate::runtime::Runtime;

#[derive(Debug, Default)]
struct FetchOption {
// A BodyInit object or null to set request's body.
// body: Option<BodyInit>,
// A string indicating how the request will interact with the browser's cache to set request's cache.
// cache: Option<RequestCache>,
// A string indicating whether credentials will be sent with the request always, never, or only when sent to a same-origin URL. Sets request's credentials.
// credentials: Option<RequestCredentials>,
// A Headers object, an object literal, or an array of two-item arrays to set request's headers.
// headers: Option<HeadersInit>,
// A cryptographic hash of the resource to be fetched by request. Sets request's integrity.
// integrity: Option<String>,
// A boolean to set request's keepalive.
// keepalive: Option<bool>,
// A string to set request's method.
method: Method,
// A string to indicate whether the request will use CORS, or will be restricted to same-origin URLs. Sets request's mode.
// mode: Option<RequestMode>,
// A string indicating whether request follows redirects, results in an error upon encountering a redirect, or returns the redirect (in an opaque fashion). Sets request's redirect.
// redirect: Option<RequestRedirect>,
// A string whose value is a same-origin URL, "about:client", or the empty string, to set request's referrer.
// referrer: Option<String>,
// A referrer policy to set request's referrerPolicy.
// referrer_policy: Option<ReferrerPolicy>,
// An AbortSignal to set request's signal.
// signal: Option<AbortSignal>,
}

pub fn fetch(
scope: &mut v8::HandleScope,
args: v8::FunctionCallbackArguments,
mut rv: v8::ReturnValue,
) {
let state_rc = Runtime::state(scope);

let url = args.get(0);
let options = args.get(1);

let init = if options.is_object() {
let obj = v8::Local::<v8::Object>::try_from(options).unwrap();

let mut init = FetchOption::default();

let method_key = v8::String::new(scope, "method").unwrap();
init.method = if let Some(method) = obj.get(scope, method_key.into()) {
Method::from_str(method.to_rust_string_lossy(scope).as_str()).expect("invalida method")
} else {
Method::GET
};
init
} else {
FetchOption::default()
};
let client = reqwest::Client::new();
let builder = client.request(init.method, url.to_rust_string_lossy(scope));

let resolver = v8::PromiseResolver::new(scope).unwrap();

// let resolver = Arc::new(resolver);

let state = state_rc.borrow_mut();
// let move_resolver = resolver.clone();
// state.pending_ops.push(Pin::new(Poll::Ready(AsynchronousKind::Callback(async move {
// let builder = builder.build().unwrap();

// let response = Client::execute(&client, builder);

// let status = response.status();
// let value = v8::ObjectTemplate::new(scope);
// let status_key = v8::String::new(scope, "status").unwrap();
// let status_value = v8::String::new(scope, status.as_str()).unwrap();
// value.set(status_key.into(), status_value.into());

// let value = value.new_instance(scope).unwrap();
// move_resolver.resolve(scope, value.into());

// }))));

rv.set(resolver.into());
}
3 changes: 2 additions & 1 deletion src/builtin/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub(crate) mod console;
pub(crate) mod fetch;
pub(crate) mod modules;
pub(crate) mod set_timeout;
// pub(crate) use edon_fs;
pub(crate) use modules::native_module_inject;
// pub(crate) use modules::native_module_inject;
68 changes: 39 additions & 29 deletions src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ use v8::Isolate;
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 +28,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 +80,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,21 +99,22 @@ impl ModuleDependency {
expose,
},
);
Ok(())
}

pub fn evaluate(&self, isolate: &mut Isolate) {
pub fn evaluate(&self, isolate: &mut Isolate) -> anyhow::Result<()> {
let state_rc = Runtime::state(isolate);
let graph_rc = Runtime::graph(isolate);

self.deps.iter().for_each(|url| {
for url in &self.deps {
let graph = graph_rc.borrow();
let table = graph.table.borrow();
let url = resolve(url, &self.filename);
let url = resolve(&url, &self.filename);
let dep = table
.get(&url)
.expect(&format!("table get failure `{url}`"));
dep.evaluate(isolate);
});
.ok_or(anyhow!("table get failure `{url}`"))?;
dep.evaluate(isolate)?;
}

let context = state_rc.borrow().context.clone();
let scope = &mut v8::HandleScope::with_context(isolate, context);
Expand All @@ -122,20 +127,25 @@ impl ModuleDependency {
let module = v8::Local::new(tc_scope, &info.module);
let result = module.evaluate(tc_scope).unwrap();

if tc_scope.has_caught() {
let expection = tc_scope.exception().unwrap();
return Err(anyhow!("{}", expection.to_rust_string_lossy(tc_scope)));
}

if result.is_promise() {
let promise = v8::Local::<v8::Promise>::try_from(result).unwrap();
match promise.state() {
v8::PromiseState::Rejected => {
println!("evaluate fail: {}", self.filename);
}
_ => {}
if let v8::PromiseState::Rejected = promise.state() {
let result = promise.result(tc_scope);
let stack = tc_scope.stack_trace();
return Err(anyhow!(
"{}\n at {:?}",
result.to_rust_string_lossy(tc_scope),
stack
));
}
}
Ok(())
}
}

pub use compile_oxc::compile;
// pub fn compile(file_name: &str, source_text: &str) -> anyhow::Result<ModuleDependency> {
// return compile_oxc::compile(file_name, source_text);
// // return compile_swc::compile(file_name, source_text);
// }
Loading

0 comments on commit 7665b07

Please sign in to comment.