diff options
Diffstat (limited to 'components/script')
-rw-r--r-- | components/script/dom/window.rs | 17 | ||||
-rw-r--r-- | components/script/script_thread.rs | 1 | ||||
-rw-r--r-- | components/script/webdriver_handlers.rs | 103 |
3 files changed, 91 insertions, 30 deletions
diff --git a/components/script/dom/window.rs b/components/script/dom/window.rs index efe61007ef8..9053f7f7e86 100644 --- a/components/script/dom/window.rs +++ b/components/script/dom/window.rs @@ -1216,15 +1216,26 @@ impl WindowMethods<crate::DomTypeHolder> for Window { } } - #[allow(unsafe_code)] - fn WebdriverCallback(&self, cx: JSContext, val: HandleValue) { - let rv = unsafe { jsval_to_webdriver(*cx, &self.globalscope, val) }; + fn WebdriverCallback(&self, cx: JSContext, val: HandleValue, realm: InRealm, can_gc: CanGc) { + let rv = jsval_to_webdriver(cx, &self.globalscope, val, realm, can_gc); let opt_chan = self.webdriver_script_chan.borrow_mut().take(); if let Some(chan) = opt_chan { chan.send(rv).unwrap(); } } + fn WebdriverException(&self, cx: JSContext, val: HandleValue, realm: InRealm, can_gc: CanGc) { + let rv = jsval_to_webdriver(cx, &self.globalscope, val, realm, can_gc); + let opt_chan = self.webdriver_script_chan.borrow_mut().take(); + if let Some(chan) = opt_chan { + if let Ok(rv) = rv { + chan.send(Err(WebDriverJSError::JSException(rv))).unwrap(); + } else { + chan.send(rv).unwrap(); + } + } + } + fn WebdriverTimeout(&self) { let opt_chan = self.webdriver_script_chan.borrow_mut().take(); if let Some(chan) = opt_chan { diff --git a/components/script/script_thread.rs b/components/script/script_thread.rs index 07310073949..f78b5bf281b 100644 --- a/components/script/script_thread.rs +++ b/components/script/script_thread.rs @@ -2274,6 +2274,7 @@ impl ScriptThread { node_id, name, reply, + can_gc, ) }, WebDriverScriptCommand::GetElementCSS(node_id, name, reply) => { diff --git a/components/script/webdriver_handlers.rs b/components/script/webdriver_handlers.rs index c6303ca89e0..781ac53f415 100644 --- a/components/script/webdriver_handlers.rs +++ b/components/script/webdriver_handlers.rs @@ -3,7 +3,7 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ use std::cmp; -use std::collections::HashMap; +use std::collections::{HashMap, HashSet}; use std::ffi::CString; use std::ptr::NonNull; @@ -48,7 +48,7 @@ use crate::dom::bindings::conversions::{ ConversionBehavior, ConversionResult, FromJSValConvertible, StringificationBehavior, get_property, get_property_jsval, jsid_to_string, jsstring_to_str, root_from_object, }; -use crate::dom::bindings::error::{Error, throw_dom_exception}; +use crate::dom::bindings::error::{Error, report_pending_exception, throw_dom_exception}; use crate::dom::bindings::inheritance::Castable; use crate::dom::bindings::reflector::{DomGlobal, DomObject}; use crate::dom::bindings::root::DomRoot; @@ -67,7 +67,7 @@ use crate::dom::node::{Node, NodeTraits, ShadowIncluding}; use crate::dom::nodelist::NodeList; use crate::dom::window::Window; use crate::dom::xmlserializer::XMLSerializer; -use crate::realms::enter_realm; +use crate::realms::{AlreadyInRealm, InRealm, enter_realm}; use crate::script_module::ScriptFetchOptions; use crate::script_runtime::{CanGc, JSContext as SafeJSContext}; use crate::script_thread::ScriptThread; @@ -183,12 +183,44 @@ unsafe fn is_arguments_object(cx: *mut JSContext, value: HandleValue) -> bool { jsstring_to_str(cx, class_name) == "[object Arguments]" } +#[derive(Eq, Hash, PartialEq)] +struct HashableJSVal(u64); + +impl From<HandleValue<'_>> for HashableJSVal { + fn from(v: HandleValue<'_>) -> HashableJSVal { + HashableJSVal(v.get().asBits_) + } +} + #[allow(unsafe_code)] -pub(crate) unsafe fn jsval_to_webdriver( +pub(crate) fn jsval_to_webdriver( + cx: SafeJSContext, + global_scope: &GlobalScope, + val: HandleValue, + realm: InRealm, + can_gc: CanGc, +) -> WebDriverJSResult { + let mut seen = HashSet::new(); + let result = unsafe { jsval_to_webdriver_inner(*cx, global_scope, val, &mut seen) }; + if result.is_err() { + report_pending_exception(cx, true, realm, can_gc); + } + result +} + +#[allow(unsafe_code)] +unsafe fn jsval_to_webdriver_inner( cx: *mut JSContext, global_scope: &GlobalScope, val: HandleValue, + seen: &mut HashSet<HashableJSVal>, ) -> WebDriverJSResult { + let hashable = val.into(); + if seen.contains(&hashable) { + return Err(WebDriverJSError::JSError); + } + seen.insert(hashable); + let _ac = enter_realm(global_scope); if val.get().is_undefined() { Ok(WebDriverJSValue::Undefined) @@ -254,9 +286,11 @@ pub(crate) unsafe fn jsval_to_webdriver( for i in 0..length { rooted!(in(cx) let mut item = UndefinedValue()); match get_property_jsval(cx, object.handle(), &i.to_string(), item.handle_mut()) { - Ok(_) => match jsval_to_webdriver(cx, global_scope, item.handle()) { - Ok(converted_item) => result.push(converted_item), - err @ Err(_) => return err, + Ok(_) => { + match jsval_to_webdriver_inner(cx, global_scope, item.handle(), seen) { + Ok(converted_item) => result.push(converted_item), + err @ Err(_) => return err, + } }, Err(error) => { throw_dom_exception( @@ -298,7 +332,7 @@ pub(crate) unsafe fn jsval_to_webdriver( &HandleValueArray::empty(), value.handle_mut(), ) { - jsval_to_webdriver(cx, global_scope, value.handle()) + jsval_to_webdriver_inner(cx, global_scope, value.handle(), seen) } else { throw_dom_exception( SafeJSContext::from_ptr(cx), @@ -349,7 +383,9 @@ pub(crate) unsafe fn jsval_to_webdriver( return Err(WebDriverJSError::JSError); }; - if let Ok(value) = jsval_to_webdriver(cx, global_scope, property.handle()) { + if let Ok(value) = + jsval_to_webdriver_inner(cx, global_scope, property.handle(), seen) + { result.insert(name.into(), value); } else { return Err(WebDriverJSError::JSError); @@ -373,18 +409,22 @@ pub(crate) fn handle_execute_script( ) { match window { Some(window) => { - let result = unsafe { - let cx = window.get_cx(); - rooted!(in(*cx) let mut rval = UndefinedValue()); - let global = window.as_global_scope(); - global.evaluate_js_on_global_with_result( - &eval, - rval.handle_mut(), - ScriptFetchOptions::default_classic_script(global), - global.api_base_url(), - can_gc, - ); - jsval_to_webdriver(*cx, global, rval.handle()) + let cx = window.get_cx(); + let realm = AlreadyInRealm::assert_for_cx(cx); + let realm = InRealm::already(&realm); + + rooted!(in(*cx) let mut rval = UndefinedValue()); + let global = window.as_global_scope(); + let result = if global.evaluate_js_on_global_with_result( + &eval, + rval.handle_mut(), + ScriptFetchOptions::default_classic_script(global), + global.api_base_url(), + can_gc, + ) { + jsval_to_webdriver(cx, global, rval.handle(), realm, can_gc) + } else { + Err(WebDriverJSError::JSError) }; reply.send(result).unwrap(); @@ -406,17 +446,20 @@ pub(crate) fn handle_execute_async_script( match window { Some(window) => { let cx = window.get_cx(); + let reply_sender = reply.clone(); window.set_webdriver_script_chan(Some(reply)); rooted!(in(*cx) let mut rval = UndefinedValue()); let global_scope = window.as_global_scope(); - global_scope.evaluate_js_on_global_with_result( + if !global_scope.evaluate_js_on_global_with_result( &eval, rval.handle_mut(), ScriptFetchOptions::default_classic_script(global_scope), global_scope.api_base_url(), can_gc, - ); + ) { + reply_sender.send(Err(WebDriverJSError::JSError)).unwrap(); + } }, None => { reply @@ -1136,12 +1179,13 @@ pub(crate) fn handle_get_property( node_id: String, name: String, reply: IpcSender<Result<WebDriverJSValue, ErrorStatus>>, + can_gc: CanGc, ) { reply .send( find_node_by_unique_id(documents, pipeline, node_id).map(|node| { let document = documents.find_document(pipeline).unwrap(); - let _ac = enter_realm(&*document); + let realm = enter_realm(&*document); let cx = document.window().get_cx(); rooted!(in(*cx) let mut property = UndefinedValue()); @@ -1154,14 +1198,19 @@ pub(crate) fn handle_get_property( ) } { Ok(_) => { - match unsafe { jsval_to_webdriver(*cx, &node.global(), property.handle()) } - { + match jsval_to_webdriver( + cx, + &node.global(), + property.handle(), + InRealm::entered(&realm), + can_gc, + ) { Ok(property) => property, Err(_) => WebDriverJSValue::Undefined, } }, Err(error) => { - throw_dom_exception(cx, &node.global(), error, CanGc::note()); + throw_dom_exception(cx, &node.global(), error, can_gc); WebDriverJSValue::Undefined }, } |