diff --git a/Cargo.lock b/Cargo.lock index 3c7c3e77f..c6136a15c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -783,6 +783,7 @@ dependencies = [ "derive_more", "gosub_shared", "lazy_static", + "serde_json", "thiserror", "v8", ] @@ -1556,9 +1557,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.113" +version = "1.0.114" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "69801b70b1c3dac963ecb03a364ba0ceda9cf60c71cfe475e99864759c8b8a79" +checksum = "c5f09b1bd632ef549eaa9f60a1f8de742bdbc698e6cee2095fc84dde5f549ae0" dependencies = [ "indexmap", "itoa", diff --git a/crates/gosub_webexecutor/Cargo.toml b/crates/gosub_webexecutor/Cargo.toml index f27e4b487..4725bb639 100644 --- a/crates/gosub_webexecutor/Cargo.toml +++ b/crates/gosub_webexecutor/Cargo.toml @@ -12,4 +12,5 @@ derive_more = "0.99" lazy_static = "1.4" thiserror = "1.0.57" v8 = "0.83.2" -anyhow = "1.0.80" \ No newline at end of file +anyhow = "1.0.80" +serde_json = "1.0.114" \ No newline at end of file diff --git a/crates/gosub_webexecutor/src/js/v8.rs b/crates/gosub_webexecutor/src/js/v8.rs index bfb7ee19a..4fb26bfd6 100644 --- a/crates/gosub_webexecutor/src/js/v8.rs +++ b/crates/gosub_webexecutor/src/js/v8.rs @@ -147,6 +147,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(); @@ -160,11 +161,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] diff --git a/crates/gosub_webexecutor/src/js/v8/array.rs b/crates/gosub_webexecutor/src/js/v8/array.rs index b8e3e1d84..51df405ca 100644 --- a/crates/gosub_webexecutor/src/js/v8/array.rs +++ b/crates/gosub_webexecutor/src/js/v8/array.rs @@ -25,10 +25,7 @@ impl<'a> JSArray for V8Array<'a> { type RT = V8Engine<'a>; fn get(&self, index: u32) -> Result<::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(), )) @@ -111,8 +108,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() { @@ -145,10 +142,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] diff --git a/crates/gosub_webexecutor/src/js/v8/function.rs b/crates/gosub_webexecutor/src/js/v8/function.rs index f293610d0..6161dcd88 100644 --- a/crates/gosub_webexecutor/src/js/v8/function.rs +++ b/crates/gosub_webexecutor/src/js/v8/function.rs @@ -31,9 +31,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()), }) } } @@ -283,8 +281,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()) } } } @@ -566,17 +563,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, }; diff --git a/crates/gosub_webexecutor/src/js/v8/object.rs b/crates/gosub_webexecutor/src/js/v8/object.rs index 7a3f834de..456e22589 100644 --- a/crates/gosub_webexecutor/src/js/v8/object.rs +++ b/crates/gosub_webexecutor/src/js/v8/object.rs @@ -115,7 +115,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 @@ -253,7 +253,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) { @@ -351,13 +351,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, }; diff --git a/crates/gosub_webexecutor/src/js/v8/value.rs b/crates/gosub_webexecutor/src/js/v8/value.rs index cb3e19cf2..f1c126433 100644 --- a/crates/gosub_webexecutor/src/js/v8/value.rs +++ b/crates/gosub_webexecutor/src/js/v8/value.rs @@ -154,7 +154,7 @@ impl<'a> JSValue for V8Value<'a> { #[cfg(test)] mod tests { - use crate::web_executor::js::JSContext; + use crate::js::JSContext; use super::*; diff --git a/crates/gosub_webexecutor/src/test.rs b/crates/gosub_webexecutor/src/test.rs index c95baa500..d4bf029f1 100644 --- a/crates/gosub_webexecutor/src/test.rs +++ b/crates/gosub_webexecutor/src/test.rs @@ -1,18 +1,19 @@ +#![allow(dead_code)] +#![allow(unused_variables)] + use std::cell::RefCell; use std::collections::HashMap; -use std::ops::Add; use std::rc::Rc; -//use webinterop::{web_fns, web_interop}; -use crate::js::v8::{ - GetterCallback, SetterCallback, V8Context, V8Engine, V8Function, V8FunctionVariadic, V8Value, -}; +use gosub_shared::types::Result; + use crate::js::{ Args, JSContext, JSFunction, JSFunctionCallBack, JSFunctionCallBackVariadic, JSFunctionVariadic, JSGetterCallback, JSInterop, JSObject, JSRuntime, JSSetterCallback, JSValue, ValueConversion, VariadicArgs, VariadicArgsInternal, }; -use gosub_shared::types::Result; +//use webinterop::{web_fns, web_interop}; +use crate::js::v8::{V8Engine, V8Value}; //#[web_interop] struct TestStruct { @@ -147,7 +148,6 @@ impl JSInterop for Test2 { let setter = { let s = Rc::clone(&s); Box::new(move |cb: &mut RT::SetterCB| { - let ctx = cb.context(); let value = cb.value(); let value = match value.as_number() { Ok(value) => value, @@ -188,7 +188,6 @@ impl JSInterop for Test2 { let setter = { let s = Rc::clone(&s); Box::new(move |cb: &mut RT::SetterCB| { - let ctx = cb.context(); let value = cb.value(); let value = match value.as_string() { Ok(value) => value, @@ -243,8 +242,6 @@ impl JSInterop for Test2 { let ctx = cb.context(); - let args = cb.args(); - let Some(arg0) = cb.args().get(0, ctx.clone()) else { cb.error("failed to get argument"); return; @@ -278,8 +275,6 @@ impl JSInterop for Test2 { let ctx = cb.context(); - let args = cb.args(); - let Some(arg0) = cb.args().get(0, ctx.clone()) else { cb.error("failed to get argument"); return; @@ -308,8 +303,6 @@ impl JSInterop for Test2 { let ctx = cb.context(); - let args = cb.args(); - let Some(arg0) = cb.args().get(0, ctx.clone()) else { cb.error("failed to get argument"); return;