Skip to content

Commit

Permalink
fix merge / crate seperation issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Sharktheone committed Feb 28, 2024
1 parent 8bc2b3e commit 24a9295
Show file tree
Hide file tree
Showing 19 changed files with 131 additions and 144 deletions.
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"
7 changes: 4 additions & 3 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
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
4 changes: 3 additions & 1 deletion 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 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
16 changes: 4 additions & 12 deletions crates/gosub_webexecutor/src/js/v8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ 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 Down Expand Up @@ -111,15 +111,10 @@ impl<'a> JSRuntime for V8Engine<'a> {

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

use anyhow;
use colored::Colorize;

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

#[test]
fn v8_engine_initialization() {
Expand Down Expand Up @@ -147,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 @@ -160,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
17 changes: 6 additions & 11 deletions crates/gosub_webexecutor/src/js/v8/array.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use v8::{Array, Local};

use gosub_shared::types::Result;

use crate::js::v8::{V8Context, V8Engine, V8Value};
use crate::js::{JSArray, JSError, JSRuntime};
use crate::Error;
use gosub_shared::types::Result;

pub struct V8Array<'a> {
value: Local<'a, Array>,
Expand All @@ -25,10 +26,7 @@ impl<'a> JSArray for V8Array<'a> {
type RT = V8Engine<'a>;

fn get(&self, index: u32) -> Result<<Self::RT as JSRuntime>::Value> {
let Some(value) = self
.value
.get_index(self.ctx.borrow_mut().scope(), index)
else {
let Some(value) = self.value.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 Down Expand Up @@ -111,8 +109,8 @@ impl<'a> JSArray for V8Array<'a> {

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

#[test]
fn set() {
Expand Down Expand Up @@ -145,10 +143,7 @@ mod tests {
.unwrap();

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

#[test]
Expand Down
10 changes: 7 additions & 3 deletions crates/gosub_webexecutor/src/js/v8/compile.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
use crate::js::v8::{FromContext, V8Context, V8Ctx, V8Engine, V8Value};
use crate::js::{JSCompiled, JSRuntime};
use gosub_shared::types::Result;
use std::rc::Rc;

use v8::{Local, Script};

use gosub_shared::types::Result;

use crate::js::v8::{FromContext, V8Context, V8Ctx, V8Engine, V8Value};
use crate::js::{JSCompiled, JSRuntime};

pub struct V8Compiled<'a> {
compiled: Local<'a, Script>,
context: V8Context<'a>,
Expand Down
5 changes: 3 additions & 2 deletions crates/gosub_webexecutor/src/js/v8/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ use v8::{
TryCatch,
};

use gosub_shared::types::Result;

use crate::js::compile::JSCompiled;
use crate::js::v8::compile::V8Compiled;
use crate::js::v8::{FromContext, V8Context, V8Engine, V8Object, V8Value};
use crate::js::v8::{FromContext, V8Context, V8Engine, V8Object};
use crate::js::{JSContext, JSError, JSRuntime};
use crate::Error;
use gosub_shared::types::Result;

/// SAFETY: This is NOT thread safe, as the rest of the engine is not thread safe.
/// This struct uses `NonNull` internally to store pointers to the V8Context "values" in one struct.
Expand Down
19 changes: 8 additions & 11 deletions crates/gosub_webexecutor/src/js/v8/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ use v8::{
FunctionCallbackInfo, Local, ReturnValue, TryCatch,
};

use gosub_shared::types::Result;

use crate::js::function::{JSFunctionCallBack, JSFunctionCallBackVariadic};
use crate::js::v8::{ctx_from_function_callback_info, V8Context, V8Engine, V8Value};
use crate::js::{
Args, JSError, JSFunction, JSFunctionVariadic, JSRuntime, JSValue, VariadicArgs,
VariadicArgsInternal,
};
use crate::Error;
use gosub_shared::types::Result;

pub struct V8Function<'a> {
pub(super) ctx: V8Context<'a>,
Expand All @@ -31,9 +32,7 @@ impl<'a> V8FunctionCallBack<'a> {
Ok(Self {
ctx,
args,
ret: Err(Error::JS(JSError::Execution(
"function was not called".to_owned(),
))),
ret: Err(Error::JS(JSError::Execution("function was not called".to_owned())).into()),
})
}
}
Expand Down Expand Up @@ -283,8 +282,7 @@ impl<'a> JSFunction for V8Function<'a> {
value,
})
} else {
Err(Error::JS(JSError::Execution(
"failed to call a function".to_owned())).into())
Err(Error::JS(JSError::Execution("failed to call a function".to_owned())).into())
}
}
}
Expand Down Expand Up @@ -566,17 +564,16 @@ impl<'a> JSFunctionVariadic for V8FunctionVariadic<'a> {
value,
})
} else {
Err(Error::JS(JSError::Execution(
"failed to call a function".to_owned())).into())
Err(Error::JS(JSError::Execution("failed to call a function".to_owned())).into())
}
}
}

#[cfg(test)]
mod tests {
use crate::web_executor::js::v8::{V8Engine, V8Function, V8FunctionVariadic, V8Value};
use crate::web_executor::js::{
Args, JSContext, JSFunction, JSFunctionCallBack, JSFunctionVariadic, JSRuntime, JSValue,
use crate::js::v8::{V8Engine, V8Function, V8FunctionVariadic};
use crate::js::{
Args, JSFunction, JSFunctionCallBack, JSFunctionVariadic, JSRuntime, JSValue,
ValueConversion,
};

Expand Down
13 changes: 7 additions & 6 deletions crates/gosub_webexecutor/src/js/v8/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use v8::{
ReturnValue, Value,
};

use gosub_shared::types::Result;

use crate::js::v8::{
ctx_from, FromContext, V8Context, V8Ctx, V8Engine, V8Function, V8FunctionCallBack,
V8FunctionVariadic, V8Value,
Expand All @@ -14,7 +16,6 @@ use crate::js::{
JSArray, JSError, JSGetterCallback, JSObject, JSRuntime, JSSetterCallback, JSValue,
};
use crate::Error;
use gosub_shared::types::Result;

pub struct V8Object<'a> {
ctx: V8Context<'a>,
Expand Down Expand Up @@ -115,7 +116,7 @@ impl<'a> JSObject for V8Object<'a> {
let Some(name) = v8::String::new(self.ctx.borrow_mut().scope(), name) else {
return Err(Error::JS(JSError::Generic("failed to create a string".to_owned())).into());
};

let scope = self.ctx.borrow_mut().scope();

self.value
Expand Down Expand Up @@ -253,7 +254,7 @@ impl<'a> JSObject for V8Object<'a> {
};

let gs = unsafe { &*(external.value() as *const GetterSetter) };

let isolate = gs.ctx.borrow().isolate;

let ctx = match ctx_from(scope, isolate) {
Expand Down Expand Up @@ -351,13 +352,13 @@ impl<'a> FromContext<'a, Local<'a, Object>> for V8Object<'a> {

#[cfg(test)]
mod tests {
use alloc::rc::Rc;
use std::cell::RefCell;
use std::rc::Rc;

use serde_json::to_string;

use crate::web_executor::js::v8::V8FunctionCallBackVariadic;
use crate::web_executor::js::{
use crate::js::v8::V8FunctionCallBackVariadic;
use crate::js::{
JSFunction, JSFunctionCallBack, JSFunctionCallBackVariadic, JSFunctionVariadic,
ValueConversion, VariadicArgsInternal,
};
Expand Down
5 changes: 3 additions & 2 deletions crates/gosub_webexecutor/src/js/v8/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ use std::rc::Rc;

use v8::{Local, Value};

use gosub_shared::types::Result;

use crate::js::v8::{FromContext, V8Context, V8Engine, V8Object};
use crate::js::{JSError, JSRuntime, JSType, JSValue, ValueConversion};
use crate::Error;
use gosub_shared::types::Result;

pub struct V8Value<'a> {
pub(crate) context: V8Context<'a>,
Expand Down Expand Up @@ -154,7 +155,7 @@ impl<'a> JSValue for V8Value<'a> {

#[cfg(test)]
mod tests {
use crate::web_executor::js::JSContext;
use crate::js::JSContext;

use super::*;

Expand Down
3 changes: 2 additions & 1 deletion crates/gosub_webexecutor/src/js/value.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::js::{JSArray, JSContext, JSObject, JSRuntime, JSType};
use gosub_shared::types::Result;

use crate::js::{JSArray, JSContext, JSObject, JSRuntime, JSType};

pub trait JSValue
where
Self: Sized,
Expand Down
3 changes: 2 additions & 1 deletion crates/gosub_webexecutor/src/js/value_conversion.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};

//trait to easily convert Rust types to JS values (just call .to_js_value() on the type)
pub trait ValueConversion<V: JSValue> {
type Value: JSValue;
Expand Down
Loading

0 comments on commit 24a9295

Please sign in to comment.