Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more tests for V8 #383

Merged
merged 3 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion crates/gosub_webexecutor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ derive_more = "0.99"
lazy_static = "1.4"
thiserror = "1.0.57"
v8 = "0.84.0"
anyhow = "1.0.80"
anyhow = "1.0.80"

[dev-dependencies]
serde_json = "1.0.114"
16 changes: 9 additions & 7 deletions crates/gosub_webexecutor/src/js.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
use lazy_static::lazy_static;
use std::sync::Mutex;

use lazy_static::lazy_static;
use thiserror::Error;

use crate::js::v8::V8Engine;
pub use compile::*;
pub use context::*;
pub use function::*;
use gosub_shared::types::Result;
pub use interop::*;
pub use object::*;
pub use runtime::*;
pub use value::*;
pub use value_conversion::*;

use gosub_shared::types::Result;
use crate::js::v8::V8Engine;

mod compile;
mod context;
Expand Down Expand Up @@ -52,14 +53,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 +75,7 @@ pub trait JSArray {
//TODO: implement other things when needed. Maybe also `Iterator`?
}

#[derive(Debug, Clone, PartialEq)]
pub enum JSType {
Undefined,
Null,
Expand Down
3 changes: 2 additions & 1 deletion crates/gosub_webexecutor/src/js/compile.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::js::{JSContext, JSRuntime, JSValue};
use gosub_shared::types::Result;

use crate::js::{JSContext, JSRuntime, JSValue};

//compiled code will be stored with this trait for later execution (e.g HTML parsing not done yet)
pub trait JSCompiled {
type RT: JSRuntime;
Expand Down
3 changes: 2 additions & 1 deletion crates/gosub_webexecutor/src/js/context.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::js::{JSArray, JSCompiled, JSFunction, JSObject, JSRuntime, JSValue};
use gosub_shared::types::Result;

use crate::js::{JSArray, JSCompiled, JSFunction, JSObject, JSRuntime, JSValue};

//main trait for JS context (can be implemented for different JS engines like V8, SpiderMonkey, JSC, etc.)
pub trait JSContext: Clone {
type RT: JSRuntime;
Expand Down
14 changes: 11 additions & 3 deletions crates/gosub_webexecutor/src/js/function.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::js::{JSContext, JSError, JSObject, JSRuntime, JSValue};
use core::fmt::Display;

use gosub_shared::types::Result;

use crate::js::{JSContext, JSObject, JSRuntime, JSValue};

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

//trait for JS functions (interop between JS and Rust)
Expand All @@ -15,7 +17,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 +69,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
6 changes: 4 additions & 2 deletions crates/gosub_webexecutor/src/js/interop.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::js::JSRuntime;
use gosub_shared::types::Result;
use std::cell::RefCell;
use std::rc::Rc;

use gosub_shared::types::Result;

use crate::js::JSRuntime;

pub trait JSInterop {
fn implement<RT: JSRuntime>(s: Rc<RefCell<Self>>, ctx: RT::Context) -> Result<()>;
}
4 changes: 3 additions & 1 deletion crates/gosub_webexecutor/src/js/object.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::js::{JSContext, JSFunction, JSFunctionVariadic, JSRuntime, JSValue};
use core::fmt::Display;

use gosub_shared::types::Result;

use crate::js::{JSContext, JSFunction, JSFunctionVariadic, JSRuntime, JSValue};

pub trait JSObject {
type RT: JSRuntime;

Expand Down
5 changes: 3 additions & 2 deletions crates/gosub_webexecutor/src/js/runtime.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use gosub_shared::types::Result;

use crate::js::{
Args, JSArray, JSCompiled, JSContext, JSFunction, JSFunctionCallBack,
JSFunctionCallBackVariadic, JSFunctionVariadic, JSGetterCallback, JSObject, JSSetterCallback,
JSValue, ValueConversion, VariadicArgs, VariadicArgsInternal,
JSValue, VariadicArgs, VariadicArgsInternal,
};
use gosub_shared::types::Result;

//trait around the main JS engine (e.g V8, SpiderMonkey, JSC, etc.)
pub trait JSRuntime {
Expand Down
50 changes: 19 additions & 31 deletions crates/gosub_webexecutor/src/js/v8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ use std::cell::RefCell;
use std::ops::{Deref, DerefMut};
use std::rc::Rc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Once;

pub use array::*;
pub use compile::*;
pub use context::*;
pub use function::*;
use gosub_shared::types::Result;
pub use object::*;
pub use value::*;

use crate::js::{JSArray, JSContext, JSFunction, JSObject, JSRuntime, JSValue, ValueConversion};
use gosub_shared::types::Result;

mod array;
mod compile;
Expand All @@ -22,8 +23,8 @@ mod object;
mod value;

// status of the V8 engine
static PLATFORM_INITIALIZED: AtomicBool = AtomicBool::new(false);
static PLATFORM_INITIALIZING: AtomicBool = AtomicBool::new(false);
static V8_INITIALIZING: AtomicBool = AtomicBool::new(false);
static V8_INITIALIZED: Once = Once::new();

trait FromContext<'a, T> {
fn from_ctx(ctx: V8Context<'a>, value: T) -> Self;
Expand All @@ -44,14 +45,10 @@ 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) {
if V8_INITIALIZING.load(Ordering::SeqCst) {
while !V8_INITIALIZED.is_completed() {
std::thread::sleep(std::time::Duration::from_millis(10));
wait_time -= 10;
if wait_time <= 9 {
Expand All @@ -64,15 +61,14 @@ impl V8Engine<'_> {
return;
}

PLATFORM_INITIALIZING.store(true, Ordering::SeqCst);

//https://github.com/denoland/rusty_v8/issues/1381
let platform = v8::new_unprotected_default_platform(0, false).make_shared();
v8::V8::initialize_platform(platform);
v8::V8::initialize();

PLATFORM_INITIALIZED.store(true, Ordering::SeqCst);
PLATFORM_INITIALIZING.store(false, Ordering::SeqCst);
V8_INITIALIZED.call_once(|| {
V8_INITIALIZING.store(true, Ordering::SeqCst);
//https://github.com/denoland/rusty_v8/issues/1381
let platform = v8::new_unprotected_default_platform(0, false).make_shared();
v8::V8::initialize_platform(platform);
v8::V8::initialize();
V8_INITIALIZING.store(false, Ordering::SeqCst);
});
}

pub fn new() -> Self {
Expand Down Expand Up @@ -115,21 +111,16 @@ impl<'a> JSRuntime for V8Engine<'a> {

#[cfg(test)]
mod tests {
use std::sync::atomic::Ordering;

use anyhow;
use colored::Colorize;

use crate::js::v8::PLATFORM_INITIALIZED;
use crate::js::{JSContext, JSError, JSRuntime, JSValue};
use crate::Error;
use crate::Error::JS;
use crate::js::v8::V8_INITIALIZED;
use crate::js::{JSContext, JSRuntime, JSValue};

#[test]
fn v8_engine_initialization() {
let mut engine = crate::js::v8::V8Engine::new();

assert!(PLATFORM_INITIALIZED.load(Ordering::SeqCst));
assert!(V8_INITIALIZED.is_completed());
}

#[test]
Expand All @@ -151,6 +142,7 @@ mod tests {
}

#[test]
#[should_panic = "called `Result::unwrap()` on an `Err` value: js: compile error: SyntaxError: missing ) after argument list\n\nCaused by:\n compile error: SyntaxError: missing ) after argument list"]
fn v8_run_invalid_syntax() {
let mut engine = crate::js::v8::V8Engine::new();

Expand All @@ -164,11 +156,7 @@ mod tests {
);

assert!(result.is_err());
// This assertion fails because the error type is not correct
// assert!(matches!(
// result,
// Err(anyhow::Error::new(JSError::Compile(_)))
// ));
result.unwrap();
}

#[test]
Expand Down
Loading
Loading