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 25, 2024
1 parent 1b93e69 commit e664d70
Show file tree
Hide file tree
Showing 7 changed files with 731 additions and 39 deletions.
9 changes: 5 additions & 4 deletions crates/gosub_webexecutor/src/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ lazy_static! {
pub trait JSArray {
type RT: JSRuntime;

fn get<T: Into<<Self::RT as JSRuntime>::ArrayIndex>>(
fn get(
&self,
index: T,
index: <Self::RT as JSRuntime>::ArrayIndex,
) -> Result<<Self::RT as JSRuntime>::Value>;

fn set<T: Into<<Self::RT as JSRuntime>::ArrayIndex>>(
fn set(
&self,
index: T,
index: <Self::RT as JSRuntime>::ArrayIndex,
value: &<Self::RT as JSRuntime>::Value,
) -> Result<()>;

Expand All @@ -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 crates/gosub_webexecutor/src/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 crates/gosub_webexecutor/src/js/v8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ impl V8Engine<'_> {
return;
}


V8_INITIALIZED.call_once(|| {
V8_INITIALIZING.store(true, Ordering::SeqCst);
//https://github.com/denoland/rusty_v8/issues/1381
Expand Down
138 changes: 134 additions & 4 deletions crates/gosub_webexecutor/src/js/v8/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,24 @@ 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>;

fn get<T: Into<u32>>(&self, index: T) -> Result<<Self::RT as JSRuntime>::Value> {
fn get(&self, index: u32) -> Result<<Self::RT as JSRuntime>::Value> {
let Some(value) = self
.value
.get_index(self.ctx.borrow_mut().scope(), index.into())
.get_index(self.ctx.borrow_mut().scope(), index)
else {
return Err(Error::JS(JSError::Generic(
"failed to get a value from an array".to_owned(),
Expand All @@ -27,10 +38,10 @@ impl<'a> JSArray for V8Array<'a> {
Ok(V8Value::from_value(self.ctx.clone(), value))
}

fn set<T: Into<u32>>(&self, index: T, value: &V8Value) -> Result<()> {
fn set(&self, index: u32, value: &V8Value) -> Result<()> {
match self
.value
.set_index(self.ctx.borrow_mut().scope(), index.into(), value.value)
.set_index(self.ctx.borrow_mut().scope(), index, value.value)
{
Some(_) => Ok(()),
None => Err(Error::JS(JSError::Conversion(
Expand Down Expand Up @@ -97,3 +108,122 @@ 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(0, &1234.0.to_js_value(context.clone()).unwrap())
.unwrap();
array
.set(1, &"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(0, &1234.0.to_js_value(context.clone()).unwrap())
.unwrap();
array
.set(1, &"Hello World!".to_js_value(context).unwrap())
.unwrap();

assert_eq!(array.get(0).unwrap().as_number().unwrap(), 1234.0);
assert_eq!(
array.get(1).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(0, &1234.0.to_js_value(context.clone()).unwrap())
.unwrap();
array
.set(1, &"Hello World!".to_js_value(context).unwrap())
.unwrap();

assert!(array.get(2).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(0, &1234.0.to_js_value(context.clone()).unwrap())
.unwrap();
array
.set(1, &"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(0, &1234.0.to_js_value(context.clone()).unwrap())
.unwrap();
array
.set(1, &"Hello World!".to_js_value(context.clone()).unwrap())
.unwrap();
array
.set(2, &1234.0.to_js_value(context.clone()).unwrap())
.unwrap();
array
.set(3, &"Hello World!".to_js_value(context.clone()).unwrap())
.unwrap();

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

0 comments on commit e664d70

Please sign in to comment.