Skip to content

Commit

Permalink
add more unit tests for V8
Browse files Browse the repository at this point in the history
  • Loading branch information
Sharktheone committed Feb 23, 2024
1 parent 3bb1e61 commit 0d8a4ab
Show file tree
Hide file tree
Showing 7 changed files with 718 additions and 27 deletions.
1 change: 1 addition & 0 deletions src/web_executor/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ pub trait JSArray {
//TODO: implement other things when needed. Maybe also `Iterator`?
}

#[derive(Debug, Clone, PartialEq)]
pub enum JSType {
Undefined,
Null,
Expand Down
10 changes: 8 additions & 2 deletions src/web_executor/js/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ pub trait JSFunction {
where
Self: Sized;

fn call(&mut self, callback: &mut <Self::RT as JSRuntime>::FunctionCallBack);
fn call(
&mut self,
args: &[<Self::RT as JSRuntime>::Value],
) -> Result<<Self::RT as JSRuntime>::Value>;
}

pub trait JSFunctionCallBack {
Expand Down Expand Up @@ -64,7 +67,10 @@ pub trait JSFunctionVariadic {
where
Self: Sized;

fn call(&mut self, callback: &mut <Self::RT as JSRuntime>::FunctionCallBackVariadic);
fn call(
&mut self,
args: &[<Self::RT as JSRuntime>::Value],
) -> Result<<Self::RT as JSRuntime>::Value>;
}

pub trait JSFunctionCallBackVariadic {
Expand Down
1 change: 0 additions & 1 deletion src/web_executor/js/v8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ impl V8Engine<'_> {
return;
}


V8_INITIALIZED.call_once(|| {
V8_INITIALIZING.store(true, Ordering::SeqCst);
//https://github.com/denoland/rusty_v8/issues/1381
Expand Down
131 changes: 131 additions & 0 deletions src/web_executor/js/v8/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ pub struct V8Array<'a> {
ctx: V8Context<'a>,
}

impl<'a> V8Array<'a> {
pub fn new(ctx: &V8Context<'a>, len: u32) -> Result<Self> {
let value = Array::new(ctx.borrow_mut().scope(), len as i32);

Ok(Self {
value,
ctx: ctx.clone(),
})
}
}

impl<'a> JSArray for V8Array<'a> {
type RT = V8Engine<'a>;

Expand Down Expand Up @@ -91,3 +102,123 @@ impl<'a> JSArray for V8Array<'a> {
Ok(self.value.length())
}
}

#[cfg(test)]
mod tests {
use crate::web_executor::js::v8::{V8Array, V8Engine};
use crate::web_executor::js::{JSArray, JSRuntime, JSValue, ValueConversion};

#[test]
fn set() {
let mut engine = V8Engine::new();
let mut context = engine.new_context().unwrap();

let array = V8Array::new(&context, 2).unwrap();

array
.set(0u32, &1234.0.to_js_value(context.clone()).unwrap())
.unwrap();
array
.set(1u32, &"Hello World!".to_js_value(context).unwrap())
.unwrap();

assert_eq!(array.length().unwrap(), 2);
}

#[test]
fn get() {
let mut engine = V8Engine::new();
let mut context = engine.new_context().unwrap();

let array = V8Array::new(&context, 2).unwrap();

array
.set(0u32, &1234.0.to_js_value(context.clone()).unwrap())
.unwrap();
array
.set(1u32, &"Hello World!".to_js_value(context).unwrap())
.unwrap();

assert_eq!(array.get(0u32).unwrap().as_number().unwrap(), 1234.0);
assert_eq!(
array.get(1u32).unwrap().as_string().unwrap(),
"Hello World!"
);
}

#[test]
fn push() {
let mut engine = V8Engine::new();
let mut context = engine.new_context().unwrap();

let array = V8Array::new(&context, 2).unwrap();

array
.push(1234.0.to_js_value(context.clone()).unwrap())
.unwrap();
array
.push("Hello World!".to_js_value(context).unwrap())
.unwrap();

assert_eq!(array.length().unwrap(), 4);
}

#[test]
fn out_of_bounds() {
let mut engine = V8Engine::new();
let mut context = engine.new_context().unwrap();

let array = V8Array::new(&context, 2).unwrap();

array
.set(0u32, &1234.0.to_js_value(context.clone()).unwrap())
.unwrap();
array
.set(1u32, &"Hello World!".to_js_value(context).unwrap())
.unwrap();

assert!(array.get(2u32).unwrap().is_undefined());
}

#[test]
fn pop() {
let mut engine = V8Engine::new();
let mut context = engine.new_context().unwrap();

let array = V8Array::new(&context, 2).unwrap();

array
.set(0u32, &1234.0.to_js_value(context.clone()).unwrap())
.unwrap();
array
.set(1u32, &"Hello World!".to_js_value(context).unwrap())
.unwrap();

assert_eq!(array.pop().unwrap().as_string().unwrap(), "Hello World!");
assert_eq!(array.get(0u32).unwrap().as_number().unwrap(), 1234.0);
assert!(array.get(1u32).unwrap().is_undefined());
}

#[test]
fn dynamic_resize() {
let mut engine = V8Engine::new();
let mut context = engine.new_context().unwrap();

let array = V8Array::new(&context, 2).unwrap();

array
.set(0u32, &1234.0.to_js_value(context.clone()).unwrap())
.unwrap();
array
.set(1u32, &"Hello World!".to_js_value(context.clone()).unwrap())
.unwrap();
array
.set(2u32, &1234.0.to_js_value(context.clone()).unwrap())
.unwrap();
array
.set(3u32, &"Hello World!".to_js_value(context.clone()).unwrap())
.unwrap();

assert_eq!(array.length().unwrap(), 4);
}
}
Loading

0 comments on commit 0d8a4ab

Please sign in to comment.