Repository: tauri-apps/wry (language: rust)

Issue:
Remove webview ivar in WryWebview class
In #900, we define a ivar `webview` in Wrywebview.
This is unnecessary since we could just call `this` when handling its callback.
This also break ios build during runtime.


Discussion / hints:

Fix patch:
diff --git a/.changes/remove-webview-ivar.md b/.changes/remove-webview-ivar.md
new file mode 100644
index 000000000..2b60c25fa
--- /dev/null
+++ b/.changes/remove-webview-ivar.md
@@ -0,0 +1,5 @@
+---
+"wry": patch
+---
+
+On iOS, fix panic at runtime due to setting webview ivar.
diff --git a/src/webview/wkwebview/mod.rs b/src/webview/wkwebview/mod.rs
index 058c5c155..af347e175 100644
--- a/src/webview/wkwebview/mod.rs
+++ b/src/webview/wkwebview/mod.rs
@@ -280,7 +280,6 @@ impl InnerWebView {
         Some(mut decl) => {
           #[cfg(target_os = "macos")]
           {
-            decl.add_ivar::<id>("webview");
             add_file_drop_methods(&mut decl);
             synthetic_mouse_events::setup(&mut decl);
             decl.add_ivar::<bool>(ACCEPT_FIRST_MOUSE);
@@ -305,7 +304,6 @@ impl InnerWebView {
         _ => class!(WryWebView),
       };
       let webview: id = msg_send![cls, alloc];
-      (*webview).set_ivar("webview", webview);
 
       let () = msg_send![config, setWebsiteDataStore: data_store];
       let _preference: id = msg_send![config, preferences];
diff --git a/src/webview/wkwebview/synthetic_mouse_events.rs b/src/webview/wkwebview/synthetic_mouse_events.rs
index caf2a3a3e..d965090b9 100644
--- a/src/webview/wkwebview/synthetic_mouse_events.rs
+++ b/src/webview/wkwebview/synthetic_mouse_events.rs
@@ -12,63 +12,56 @@ use std::{ffi::c_void, ptr::null};
 pub unsafe fn setup(decl: &mut ClassDecl) {
   decl.add_method(
     sel!(otherMouseDown:),
-    other_mouse_down as extern "C" fn(&Object, Sel, id),
+    other_mouse_down as extern "C" fn(&mut Object, Sel, id),
   );
   decl.add_method(
     sel!(otherMouseUp:),
-    other_mouse_up as extern "C" fn(&Object, Sel, id),
+    other_mouse_up as extern "C" fn(&mut Object, Sel, id),
   );
 }
 
-extern "C" fn other_mouse_down(this: &Object, _sel: Sel, event: id) {
+extern "C" fn other_mouse_down(this: &mut Object, _sel: Sel, event: id) {
   unsafe {
     if event.eventType() == NSEventType::NSOtherMouseDown {
-      let webview = this.get_ivar::<id>("webview");
-      if *webview != nil {
-        let button_number = event.buttonNumber();
-        match button_number {
-          // back button
-          3 => {
-            let js = create_js_mouse_event(*webview, event, true, true);
-            let _: id = msg_send![*webview, evaluateJavaScript:NSString::new(&js) completionHandler:null::<*const c_void>()];
-            return;
-          }
-          // forward button
-          4 => {
-            let js = create_js_mouse_event(*webview, event, true, false);
-            let _: id = msg_send![*webview, evaluateJavaScript:NSString::new(&js) completionHandler:null::<*const c_void>()];
-            return;
-          }
-
-          _ => {}
+      let button_number = event.buttonNumber();
+      match button_number {
+        // back button
+        3 => {
+          let js = create_js_mouse_event(this, event, true, true);
+          let _: id = msg_send![this, evaluateJavaScript:NSString::new(&js) completionHandler:null::<*const c_void>()];
+          return;
+        }
+        // forward button
+        4 => {
+          let js = create_js_mouse_event(this, event, true, false);
+          let _: id = msg_send![this, evaluateJavaScript:NSString::new(&js) completionHandler:null::<*const c_void>()];
+          return;
         }
+        _ => {}
       }
     }
 
     let _: () = msg_send![this, mouseDown: event];
   }
 }
-extern "C" fn other_mouse_up(this: &Object, _sel: Sel, event: id) {
+extern "C" fn other_mouse_up(this: &mut Object, _sel: Sel, event: id) {
   unsafe {
     if event.eventType() == NSEventType::NSOtherMouseUp {
-      let webview = this.get_ivar::<id>("webview");
-      if *webview != nil {
-        let button_number = event.buttonNumber();
-        match button_number {
-          // back button
-          3 => {
-            let js = create_js_mouse_event(*webview, event, false, true);
-            let _: id = msg_send![*webview, evaluateJavaScript:NSString::new(&js) completionHandler:null::<*const c_void>()];
-            return;
-          }
-          // forward button
-          4 => {
-            let js = create_js_mouse_event(*webview, event, false, false);
-            let _: id = msg_send![*webview, evaluateJavaScript:NSString::new(&js) completionHandler:null::<*const c_void>()];
-            return;
-          }
-          _ => {}
+      let button_number = event.buttonNumber();
+      match button_number {
+        // back button
+        3 => {
+          let js = create_js_mouse_event(this, event, false, true);
+          let _: id = msg_send![this, evaluateJavaScript:NSString::new(&js) completionHandler:null::<*const c_void>()];
+          return;
+        }
+        // forward button
+        4 => {
+          let js = create_js_mouse_event(this, event, false, false);
+          let _: id = msg_send![this, evaluateJavaScript:NSString::new(&js) completionHandler:null::<*const c_void>()];
+          return;
         }
+        _ => {}
       }
     }
 


Test patch: