Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
add CB::Error functions
  • Loading branch information
Sharktheone committed Feb 6, 2024
1 parent a936d2b commit 136f8a7
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 242 deletions.
5 changes: 5 additions & 0 deletions src/web_executor/js/function.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::types::Result;
use crate::web_executor::js::{JSContext, JSError, JSObject, JSRuntime, JSValue};
use core::fmt::Display;

struct Function<T: JSFunction>(pub T);

Expand Down Expand Up @@ -31,6 +32,8 @@ pub trait JSFunctionCallBack {
self.len() == 0
}

fn error(&mut self, error: impl Display);

fn ret(&mut self, value: Self::Value);
}

Expand Down Expand Up @@ -77,6 +80,8 @@ pub trait JSFunctionCallBackVariadic {
self.len() == 0
}

fn error(&mut self, error: impl Display);

fn ret(&mut self, value: Self::Value);
}

Expand Down
7 changes: 7 additions & 0 deletions src/web_executor/js/object.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::web_executor::js::{JSContext, JSFunction, JSFunctionVariadic, JSValue};
use core::fmt::Display;

pub trait JSObject {
type Value: JSValue;
Expand Down Expand Up @@ -35,6 +36,9 @@ pub trait JSGetterCallback {
type Context: JSContext;

fn context(&mut self) -> &mut Self::Context;

fn error(&mut self, error: impl Display);

fn ret(&mut self, value: Self::Value);
}

Expand All @@ -43,5 +47,8 @@ pub trait JSSetterCallback {
type Context: JSContext;

fn context(&mut self) -> &mut Self::Context;

fn error(&mut self, error: impl Display);

fn value(&mut self) -> &Self::Value;
}
27 changes: 8 additions & 19 deletions src/web_executor/js/v8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ mod compile;
mod context;
mod function;
mod object;
mod utils;
mod value;

// status of the V8 engine
Expand All @@ -43,15 +42,23 @@ impl Default for V8Engine<'_> {
}
}

const MAX_V8_INIT_SECONDS: u64 = 10;

impl V8Engine<'_> {
pub fn initialize() {
if PLATFORM_INITIALIZED.load(Ordering::SeqCst) {
return;
}

let mut wait_time = MAX_V8_INIT_SECONDS * 1000;

if PLATFORM_INITIALIZING.load(Ordering::SeqCst) {
while !PLATFORM_INITIALIZED.load(Ordering::SeqCst) {
std::thread::sleep(std::time::Duration::from_millis(10));
wait_time -= 10;
if wait_time <= 9 {
panic!("V8 initialization timed out after {} seconds", MAX_V8_INIT_SECONDS);
}
}
return;
}
Expand Down Expand Up @@ -108,24 +115,6 @@ mod tests {
assert!(PLATFORM_INITIALIZED.load(Ordering::SeqCst));
}

// #[test]
// fn v8_bindings_test() {
// let platform = v8::new_default_platform(0, false).make_shared();
// v8::V8::initialize_platform(platform);
// v8::V8::initialize();
//
// let isolate = &mut v8::Isolate::new(Default::default());
// let hs = &mut v8::HandleScope::new(isolate);
// let c = v8::Context::new(hs);
// let s = &mut v8::ContextScope::new(hs, c);
//
// let code = v8::String::new(s, "console.log(\"Hello World!\"); 1234").unwrap();
//
// let value = v8::Script::compile(s, code, None).unwrap().run(s).unwrap();
//
// println!("{}", value.to_rust_string_lossy(s));
// }

#[test]
fn v8_js_execution() {
let mut engine = crate::web_executor::js::v8::V8Engine::new();
Expand Down
35 changes: 27 additions & 8 deletions src/web_executor/js/v8/function.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use alloc::rc::Rc;
use core::fmt::Display;

use v8::{
CallbackScope, External, Function, FunctionBuilder, FunctionCallbackArguments,
Expand Down Expand Up @@ -105,6 +106,16 @@ impl<'a> JSFunctionCallBack for V8FunctionCallBack<'a> {
fn ret(&mut self, value: Self::Value) {
self.ret = Ok(value.value);
}

fn error(&mut self, error: impl Display) {
let scope = self.ctx.borrow_mut().scope();
let err = error.to_string();
let Some(e) = v8::String::new(scope, &err) else {
eprintln!("failed to create exception string\nexception was: {}", err);
return;
};
scope.throw_exception(Local::from(e));
}
}

impl<'a> V8Function<'a> {
Expand Down Expand Up @@ -142,7 +153,7 @@ impl<'a> V8Function<'a> {
{
exception.into()
} else {
eprintln!("failed to create exception string\nexception was: {e}"); //TODO: replace with our own logger
eprintln!("failed to create exception string\nexception was: {e}");
v8::undefined(ctx.borrow_mut().scope()).into()
};

Expand All @@ -161,7 +172,7 @@ extern "C" fn callback(info: *const FunctionCallbackInfo) {
Ok(external) => external,
Err(e) => {
let Some(e) = v8::String::new(scope, &e.to_string()) else {
eprintln!("failed to create exception string\nexception was: {e}"); //TODO: replace with our own logger
eprintln!("failed to create exception string\nexception was: {e}");
return;
};
scope.throw_exception(Local::from(e));
Expand All @@ -179,7 +190,7 @@ extern "C" fn callback(info: *const FunctionCallbackInfo) {
Err((mut st, e)) => {
let scope = st.get();
let Some(e) = v8::String::new(scope, &e.to_string()) else {
eprintln!("failed to create exception string\nexception was: {e}"); //TODO: replace with our own logger
eprintln!("failed to create exception string\nexception was: {e}");
return;
};
scope.throw_exception(Local::from(e));
Expand Down Expand Up @@ -252,8 +263,6 @@ impl<'a> JSFunction for V8Function<'a> {
}
}

//TODO: maybe move both implementations into a macro, so we have less code duplication

pub struct V8FunctionVariadic<'a> {
pub(super) ctx: V8Context<'a>,
pub(super) function: Local<'a, Function>,
Expand Down Expand Up @@ -373,6 +382,16 @@ impl<'a> JSFunctionCallBackVariadic for V8FunctionCallBackVariadic<'a> {
self.args.len()
}

fn error(&mut self, error: impl Display) {
let scope = self.ctx.borrow_mut().scope();
let err = error.to_string();
let Some(e) = v8::String::new(scope, &err) else {
eprintln!("failed to create exception string\nexception was: {}", err);
return;
};
scope.throw_exception(Local::from(e));
}

fn ret(&mut self, value: Self::Value) {
self.ret = Ok(value.value);
}
Expand Down Expand Up @@ -413,7 +432,7 @@ impl<'a> V8FunctionVariadic<'a> {
{
exception.into()
} else {
eprintln!("failed to create exception string\nexception was: {e}"); //TODO: replace with our own logger
eprintln!("failed to create exception string\nexception was: {e}");
v8::undefined(ctx.borrow_mut().scope()).into()
};

Expand All @@ -432,7 +451,7 @@ extern "C" fn callback_variadic(info: *const FunctionCallbackInfo) {
Ok(external) => external,
Err(e) => {
let Some(e) = v8::String::new(&mut scope, &e.to_string()) else {
eprintln!("failed to create exception string\nexception was: {e}"); //TODO: replace with our own logger
eprintln!("failed to create exception string\nexception was: {e}");
return;
};
scope.throw_exception(Local::from(e));
Expand All @@ -447,7 +466,7 @@ extern "C" fn callback_variadic(info: *const FunctionCallbackInfo) {
Err((mut st, e)) => {
let scope = st.get();
let Some(e) = v8::String::new(scope, &e.to_string()) else {
eprintln!("failed to create exception string\nexception was: {e}"); //TODO: replace with our own logger
eprintln!("failed to create exception string\nexception was: {e}");
return;
};
scope.throw_exception(Local::from(e));
Expand Down
31 changes: 26 additions & 5 deletions src/web_executor/js/v8/object.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use core::fmt::Display;
use std::ffi::c_void;

use v8::{
Expand Down Expand Up @@ -31,6 +32,16 @@ impl<'a> JSGetterCallback for GetterCallback<'a, '_> {
&mut self.ctx
}

fn error(&mut self, error: impl Display) {
let scope = self.ctx.borrow_mut().scope();
let err = error.to_string();
let Some(e) = v8::String::new(scope, &err) else {
eprintln!("failed to create exception string\nexception was: {}", err);
return;
};
scope.throw_exception(Local::from(e));
}

fn ret(&mut self, value: Self::Value) {
*self.ret = value;
}
Expand All @@ -49,6 +60,16 @@ impl<'a, 'v> JSSetterCallback for SetterCallback<'a, 'v> {
&mut self.ctx
}

fn error(&mut self, error: impl Display) {
let scope = self.ctx.borrow_mut().scope();
let err = error.to_string();
let Some(e) = v8::String::new(scope, &err) else {
eprintln!("failed to create exception string\nexception was: {}", err);
return;
};
scope.throw_exception(Local::from(e));
}

fn value(&mut self) -> &'v Self::Value {
self.value
}
Expand Down Expand Up @@ -217,7 +238,7 @@ impl<'a> JSObject for V8Object<'a> {
Ok(external) => external,
Err(e) => {
let Some(e) = v8::String::new(scope, &e.to_string()) else {
eprintln!("failed to create exception string\nexception was: {e}"); //TODO: replace with our own logger
eprintln!("failed to create exception string\nexception was: {e}");
return;
};
scope.throw_exception(Local::from(e));
Expand All @@ -234,7 +255,7 @@ impl<'a> JSObject for V8Object<'a> {
Err((mut st, e)) => {
let scope = st.get();
let Some(e) = v8::String::new(scope, &e.to_string()) else {
eprintln!("failed to create exception string\nexception was: {e}"); //TODO: replace with our own logger
eprintln!("failed to create exception string\nexception was: {e}");
return;
};
scope.throw_exception(Local::from(e));
Expand All @@ -247,7 +268,7 @@ impl<'a> JSObject for V8Object<'a> {
Err(e) => {
let scope = ctx.borrow_mut().scope();
let Some(e) = v8::String::new(scope, &e.to_string()) else {
eprintln!("failed to create exception string\nexception was: {e}"); //TODO: replace with our own logger
eprintln!("failed to create exception string\nexception was: {e}");
return;
};
scope.throw_exception(Local::from(e));
Expand All @@ -272,7 +293,7 @@ impl<'a> JSObject for V8Object<'a> {
Ok(external) => external,
Err(e) => {
let Some(e) = v8::String::new(scope, &e.to_string()) else {
eprintln!("failed to create exception string\nexception was: {e}"); //TODO: replace with our own logger
eprintln!("failed to create exception string\nexception was: {e}");
return;
};
scope.throw_exception(Local::from(e));
Expand All @@ -289,7 +310,7 @@ impl<'a> JSObject for V8Object<'a> {
Err((mut st, e)) => {
let scope = st.get();
let Some(e) = v8::String::new(scope, &e.to_string()) else {
eprintln!("failed to create exception string\nexception was: {e}"); //TODO: replace with our own logger
eprintln!("failed to create exception string\nexception was: {e}");
return;
};
scope.throw_exception(Local::from(e));
Expand Down
Loading

0 comments on commit 136f8a7

Please sign in to comment.