From 4e64cefc9cfc132962785c00a4ba3462b2f3fcbf Mon Sep 17 00:00:00 2001 From: Bert Belder Date: Fri, 26 Jun 2020 14:47:13 +0200 Subject: [PATCH] Remove unnecessary 'DerefMut' impl from 'Local' (#406) Local handles never need to be mutable. This patch also rounds up the last few places where we were still asking the user to pass an `&mut T` to an API method. --- src/function.rs | 2 +- src/local.rs | 7 ------- src/module.rs | 2 +- src/object.rs | 2 +- src/proxy.rs | 8 ++++---- src/script.rs | 2 +- src/template.rs | 8 ++++---- tests/slots.rs | 2 +- tests/test_api.rs | 47 +++++++++++++++++++++-------------------------- 9 files changed, 34 insertions(+), 46 deletions(-) diff --git a/src/function.rs b/src/function.rs index 670b4c32..1f3f260b 100644 --- a/src/function.rs +++ b/src/function.rs @@ -90,7 +90,7 @@ impl<'cb> ReturnValue<'cb> { /// Getter. Creates a new Local<> so it comes with a certain performance /// hit. If the ReturnValue was not yet set, this will return the undefined /// value. - pub fn get<'s>(&mut self, scope: &mut HandleScope<'s>) -> Local<'s, Value> { + pub fn get<'s>(&self, scope: &mut HandleScope<'s>) -> Local<'s, Value> { unsafe { scope.cast_local(|_| v8__ReturnValue__Get(self)) }.unwrap() } } diff --git a/src/local.rs b/src/local.rs index 114adead..217cd9c9 100644 --- a/src/local.rs +++ b/src/local.rs @@ -1,7 +1,6 @@ use std::marker::PhantomData; use std::mem::transmute; use std::ops::Deref; -use std::ops::DerefMut; use std::ptr::NonNull; /// An object reference managed by the v8 garbage collector. @@ -82,12 +81,6 @@ impl<'s, T> Deref for Local<'s, T> { } } -impl<'s, T> DerefMut for Local<'s, T> { - fn deref_mut(&mut self) -> &mut T { - unsafe { self.0.as_mut() } - } -} - #[test] fn test_size_of_local() { use crate::Value; diff --git a/src/module.rs b/src/module.rs index 6f17b2b2..7fb03562 100644 --- a/src/module.rs +++ b/src/module.rs @@ -204,7 +204,7 @@ impl Module { /// exception is propagated.) #[must_use] pub fn instantiate_module<'a>( - &mut self, + &self, scope: &mut HandleScope, callback: impl MapFnTo>, ) -> Option { diff --git a/src/object.rs b/src/object.rs index 080996b4..1d386db2 100644 --- a/src/object.rs +++ b/src/object.rs @@ -250,7 +250,7 @@ impl Object { /// Note: SideEffectType affects the getter only, not the setter. pub fn set_accessor( - &mut self, + &self, scope: &mut HandleScope, name: Local, getter: impl for<'s> MapFnTo>, diff --git a/src/proxy.rs b/src/proxy.rs index 064ccdb4..fbe2bfa1 100644 --- a/src/proxy.rs +++ b/src/proxy.rs @@ -31,24 +31,24 @@ impl Proxy { } pub fn get_handler<'s>( - &mut self, + &self, scope: &mut HandleScope<'s>, ) -> Local<'s, Value> { unsafe { scope.cast_local(|_| v8__Proxy__GetHandler(&*self)) }.unwrap() } pub fn get_target<'s>( - &mut self, + &self, scope: &mut HandleScope<'s>, ) -> Local<'s, Value> { unsafe { scope.cast_local(|_| v8__Proxy__GetTarget(&*self)) }.unwrap() } - pub fn is_revoked(&mut self) -> bool { + pub fn is_revoked(&self) -> bool { unsafe { v8__Proxy__IsRevoked(self) } } - pub fn revoke(&mut self) { + pub fn revoke(&self) { unsafe { v8__Proxy__Revoke(self) }; } } diff --git a/src/script.rs b/src/script.rs index bc4dc783..29c0c443 100644 --- a/src/script.rs +++ b/src/script.rs @@ -62,7 +62,7 @@ impl Script { /// context in which it was created (ScriptCompiler::CompileBound or /// UnboundScript::BindToCurrentContext()). pub fn run<'s>( - &mut self, + &self, scope: &mut HandleScope<'s>, ) -> Option> { unsafe { diff --git a/src/template.rs b/src/template.rs index 55e2e483..b4c0e56a 100644 --- a/src/template.rs +++ b/src/template.rs @@ -80,12 +80,12 @@ impl FunctionTemplate { /// Returns the unique function instance in the current execution context. pub fn get_function<'s>( - &mut self, + &self, scope: &mut HandleScope<'s>, ) -> Option> { unsafe { scope.cast_local(|sd| { - v8__FunctionTemplate__GetFunction(&*self, sd.get_current_context()) + v8__FunctionTemplate__GetFunction(self, sd.get_current_context()) }) } } @@ -93,8 +93,8 @@ impl FunctionTemplate { /// Set the class name of the FunctionTemplate. This is used for /// printing objects created with the function created from the /// FunctionTemplate as its constructor. - pub fn set_class_name(&mut self, name: Local) { - unsafe { v8__FunctionTemplate__SetClassName(&*self, &*name) }; + pub fn set_class_name(&self, name: Local) { + unsafe { v8__FunctionTemplate__SetClassName(self, &*name) }; } } diff --git a/tests/slots.rs b/tests/slots.rs index e0795158..e5f44008 100644 --- a/tests/slots.rs +++ b/tests/slots.rs @@ -43,7 +43,7 @@ impl CoreIsolate { let context = v8::Context::new(scope); let scope = &mut v8::ContextScope::new(scope, context); let source = v8::String::new(scope, code).unwrap(); - let mut script = v8::Script::compile(scope, source, None).unwrap(); + let script = v8::Script::compile(scope, source, None).unwrap(); let r = script.run(scope); r.is_some() } diff --git a/tests/test_api.rs b/tests/test_api.rs index d4832902..5f1a84d2 100644 --- a/tests/test_api.rs +++ b/tests/test_api.rs @@ -529,7 +529,7 @@ fn eval<'s>( ) -> Option> { let scope = &mut v8::EscapableHandleScope::new(scope); let source = v8::String::new(scope, code).unwrap(); - let mut script = v8::Script::compile(scope, source, None).unwrap(); + let script = v8::Script::compile(scope, source, None).unwrap(); let r = script.run(scope); r.map(|v| scope.escape(v)) } @@ -689,7 +689,7 @@ fn terminate_execution() { // Rn an infinite loop, which should be terminated. let source = v8::String::new(scope, "for(;;) {}").unwrap(); let r = v8::Script::compile(scope, source, None); - let mut script = r.unwrap(); + let script = r.unwrap(); let result = script.run(scope); assert!(result.is_none()); // TODO assert_eq!(e.to_string(), "Uncaught Error: execution terminated") @@ -774,7 +774,7 @@ fn add_message_listener() { let context = v8::Context::new(scope); let scope = &mut v8::ContextScope::new(scope, context); let source = v8::String::new(scope, "throw 'foo'").unwrap(); - let mut script = v8::Script::compile(scope, source, None).unwrap(); + let script = v8::Script::compile(scope, source, None).unwrap(); assert!(script.run(scope).is_none()); assert_eq!(CALL_COUNT.load(Ordering::SeqCst), 1); } @@ -814,8 +814,7 @@ fn set_host_initialize_import_meta_object_callback() { let context = v8::Context::new(scope); let scope = &mut v8::ContextScope::new(scope, context); let source = mock_source(scope, "google.com", "import.meta;"); - let mut module = - v8::script_compiler::compile_module(scope, source).unwrap(); + let module = v8::script_compiler::compile_module(scope, source).unwrap(); let result = module.instantiate_module(scope, unexpected_module_resolve_callback); assert!(result.is_some()); @@ -839,7 +838,7 @@ fn script_compile_and_run() { let context = v8::Context::new(scope); let scope = &mut v8::ContextScope::new(scope, context); let source = v8::String::new(scope, "'Hello ' + 13 + 'th planet'").unwrap(); - let mut script = v8::Script::compile(scope, source, None).unwrap(); + let script = v8::Script::compile(scope, source, None).unwrap(); source.to_rust_string_lossy(scope); let result = script.run(scope).unwrap(); let result = result.to_string(scope).unwrap(); @@ -880,7 +879,7 @@ fn script_origin() { ); let source = v8::String::new(scope, "1+2").unwrap(); - let mut script = + let script = v8::Script::compile(scope, source, Some(&script_origin)).unwrap(); source.to_rust_string_lossy(scope); let _result = script.run(scope).unwrap(); @@ -1090,8 +1089,7 @@ fn object_template_from_function_template() { let isolate = &mut v8::Isolate::new(Default::default()); { let scope = &mut v8::HandleScope::new(isolate); - let mut function_templ = - v8::FunctionTemplate::new(scope, fortytwo_callback); + let function_templ = v8::FunctionTemplate::new(scope, fortytwo_callback); let expected_class_name = v8::String::new(scope, "fortytwo").unwrap(); function_templ.set_class_name(expected_class_name); let object_templ = @@ -1240,7 +1238,7 @@ fn object_set_accessor() { CALL_COUNT.fetch_add(1, Ordering::SeqCst); }; - let mut obj = v8::Object::new(scope); + let obj = v8::Object::new(scope); let getter_key = v8::String::new(scope, "getter_key").unwrap(); obj.set_accessor(scope, getter_key.into(), getter); @@ -1334,7 +1332,7 @@ fn proxy() { let handler = v8::Object::new(scope); let maybe_proxy = v8::Proxy::new(scope, target, handler); assert!(maybe_proxy.is_some()); - let mut proxy = maybe_proxy.unwrap(); + let proxy = maybe_proxy.unwrap(); assert!(target == proxy.get_target(scope)); assert!(handler == proxy.get_handler(scope)); assert!(!proxy.is_revoked()); @@ -1406,7 +1404,7 @@ fn function() { let global = context.global(scope); let recv: v8::Local = global.into(); // create function using template - let mut fn_template = v8::FunctionTemplate::new(scope, fn_callback); + let fn_template = v8::FunctionTemplate::new(scope, fn_callback); let function = fn_template .get_function(scope) .expect("Unable to create function"); @@ -1547,8 +1545,7 @@ fn module_instantiation_failures1() { let origin = mock_script_origin(scope, "foo.js"); let source = v8::script_compiler::Source::new(source_text, &origin); - let mut module = - v8::script_compiler::compile_module(scope, source).unwrap(); + let module = v8::script_compiler::compile_module(scope, source).unwrap(); assert_eq!(v8::ModuleStatus::Uninstantiated, module.get_status()); assert_eq!(2, module.get_module_requests_length()); @@ -1624,8 +1621,7 @@ fn module_evaluation() { let origin = mock_script_origin(scope, "foo.js"); let source = v8::script_compiler::Source::new(source_text, &origin); - let mut module = - v8::script_compiler::compile_module(scope, source).unwrap(); + let module = v8::script_compiler::compile_module(scope, source).unwrap(); assert_eq!(v8::ModuleStatus::Uninstantiated, module.get_status()); let result = module @@ -1710,7 +1706,7 @@ fn array_buffer_view() { let scope = &mut v8::ContextScope::new(scope, context); let source = v8::String::new(scope, "new Uint8Array([23,23,23,23])").unwrap(); - let mut script = v8::Script::compile(scope, source, None).unwrap(); + let script = v8::Script::compile(scope, source, None).unwrap(); source.to_rust_string_lossy(scope); let result: v8::Local = script.run(scope).unwrap().try_into().unwrap(); @@ -1746,7 +1742,7 @@ fn snapshot_creator() { let scope = &mut v8::ContextScope::new(scope, context); let source = v8::String::new(scope, "a = 1 + 2").unwrap(); - let mut script = v8::Script::compile(scope, source, None).unwrap(); + let script = v8::Script::compile(scope, source, None).unwrap(); script.run(scope).unwrap(); snapshot_creator.set_default_context(context); @@ -1767,7 +1763,7 @@ fn snapshot_creator() { let context = v8::Context::new(scope); let scope = &mut v8::ContextScope::new(scope, context); let source = v8::String::new(scope, "a === 3").unwrap(); - let mut script = v8::Script::compile(scope, source, None).unwrap(); + let script = v8::Script::compile(scope, source, None).unwrap(); let result = script.run(scope).unwrap(); let true_val = v8::Boolean::new(scope, true).into(); assert!(result.same_value(true_val)); @@ -1799,7 +1795,7 @@ fn external_references() { let scope = &mut v8::ContextScope::new(scope, context); // create function using template - let mut fn_template = v8::FunctionTemplate::new(scope, fn_callback); + let fn_template = v8::FunctionTemplate::new(scope, fn_callback); let function = fn_template .get_function(scope) .expect("Unable to create function"); @@ -1863,7 +1859,7 @@ fn uint8_array() { let scope = &mut v8::ContextScope::new(scope, context); let source = v8::String::new(scope, "new Uint8Array([23,23,23,23])").unwrap(); - let mut script = v8::Script::compile(scope, source, None).unwrap(); + let script = v8::Script::compile(scope, source, None).unwrap(); source.to_rust_string_lossy(scope); let result: v8::Local = script.run(scope).unwrap().try_into().unwrap(); @@ -1949,7 +1945,7 @@ fn shared_array_buffer() { sharedBytes[5] + sharedBytes[12]", ) .unwrap(); - let mut script = v8::Script::compile(scope, source, None).unwrap(); + let script = v8::Script::compile(scope, source, None).unwrap(); let result: v8::Local = script.run(scope).unwrap().try_into().unwrap(); @@ -2897,8 +2893,7 @@ fn module_snapshot() { let origin = mock_script_origin(scope, "foo.js"); let source = v8::script_compiler::Source::new(source_text, &origin); - let mut module = - v8::script_compiler::compile_module(scope, source).unwrap(); + let module = v8::script_compiler::compile_module(scope, source).unwrap(); assert_eq!(v8::ModuleStatus::Uninstantiated, module.get_status()); let result = module.instantiate_module( @@ -2931,12 +2926,12 @@ fn module_snapshot() { let true_val = v8::Boolean::new(scope, true).into(); let source = v8::String::new(scope, "a === 3").unwrap(); - let mut script = v8::Script::compile(scope, source, None).unwrap(); + let script = v8::Script::compile(scope, source, None).unwrap(); let result = script.run(scope).unwrap(); assert!(result.same_value(true_val)); let source = v8::String::new(scope, "b === 42").unwrap(); - let mut script = v8::Script::compile(scope, source, None).unwrap(); + let script = v8::Script::compile(scope, source, None).unwrap(); let result = script.run(scope).unwrap(); assert!(result.same_value(true_val)); }