From a0a5eb2170ec829f5951d4aea40c90a4bb732ef4 Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Fri, 24 Feb 2023 11:13:07 -0300 Subject: [PATCH] feat(core): update swift-rs --- core/tauri-build/Cargo.toml | 2 +- core/tauri/Cargo.toml | 2 +- core/tauri/mobile/ios-api/Package.swift | 2 +- .../mobile/ios-api/Sources/Tauri/Tauri.swift | 17 ++++--- core/tauri/src/ios.rs | 50 +++++++++++-------- core/tauri/src/lib.rs | 2 +- core/tauri/src/manager.rs | 2 +- core/tauri/src/plugin/mobile.rs | 4 +- core/tauri/src/window.rs | 4 +- examples/api/src-tauri/Cargo.lock | 16 ++++-- examples/api/src-tauri/src/lib.rs | 2 +- 11 files changed, 61 insertions(+), 42 deletions(-) diff --git a/core/tauri-build/Cargo.toml b/core/tauri-build/Cargo.toml index 04ad1b58e..8a8620cc4 100644 --- a/core/tauri-build/Cargo.toml +++ b/core/tauri-build/Cargo.toml @@ -33,7 +33,7 @@ filetime = "0.2" tauri-winres = "0.1" semver = "1" [target."cfg(target_os = \"macos\")".dependencies] -swift-rs = { git = "https://github.com/Brendonovich/swift-rs", rev = "eb6de914ad57501da5019154d476d45660559999", features = ["build"] } +swift-rs = { git = "https://github.com/Brendonovich/swift-rs", rev = "56b14aa4aa61e93d0fddf695d0cad78b6dd392b4", features = ["build"] } [features] codegen = [ "tauri-codegen", "quote" ] diff --git a/core/tauri/Cargo.toml b/core/tauri/Cargo.toml index 5b3a94634..e6fbc9aa3 100644 --- a/core/tauri/Cargo.toml +++ b/core/tauri/Cargo.toml @@ -120,7 +120,7 @@ jni = "0.20" libc = "0.2" objc = "0.2" cocoa = "0.24" -swift-rs = { git = "https://github.com/Brendonovich/swift-rs", rev = "eb6de914ad57501da5019154d476d45660559999" } +swift-rs = { git = "https://github.com/Brendonovich/swift-rs", rev = "56b14aa4aa61e93d0fddf695d0cad78b6dd392b4" } [build-dependencies] heck = "0.4" diff --git a/core/tauri/mobile/ios-api/Package.swift b/core/tauri/mobile/ios-api/Package.swift index 38abed349..321bb63d1 100644 --- a/core/tauri/mobile/ios-api/Package.swift +++ b/core/tauri/mobile/ios-api/Package.swift @@ -16,7 +16,7 @@ let package = Package( ], dependencies: [ // Dependencies declare other packages that this package depends on. - .package(url: "https://github.com/Brendonovich/swift-rs", revision: "eb6de914ad57501da5019154d476d45660559999"), + .package(url: "https://github.com/Brendonovich/swift-rs", revision: "56b14aa4aa61e93d0fddf695d0cad78b6dd392b4"), ], targets: [ // Targets are the basic building blocks of a package. A target can define a module or a test suite. diff --git a/core/tauri/mobile/ios-api/Sources/Tauri/Tauri.swift b/core/tauri/mobile/ios-api/Sources/Tauri/Tauri.swift index c209069bb..22cf28e7d 100644 --- a/core/tauri/mobile/ios-api/Sources/Tauri/Tauri.swift +++ b/core/tauri/mobile/ios-api/Sources/Tauri/Tauri.swift @@ -67,7 +67,8 @@ public class PluginManager { } if let error = error { invoke.reject("\(error)") - let _ = toRust(error) // TODO app is crashing without this memory leak (when an error is thrown) + // TODO: app crashes without this leak + let _ = Unmanaged.passRetained(error) } } else { let selector = Selector(("\(invoke.command):")) @@ -105,8 +106,8 @@ func onWebviewCreated(webview: WKWebView, viewController: UIViewController) { } @_cdecl("post_ipc_message") -func postIpcMessage(webview: WKWebView, name: UnsafePointer, command: UnsafePointer, data: NSDictionary, callback: UInt, error: UInt) { - let invoke = Invoke(command: command.pointee.to_string(), sendResponse: { (successResult: JsonValue?, errorResult: JsonValue?) -> Void in +func postIpcMessage(webview: WKWebView, name: SRString, command: SRString, data: NSDictionary, callback: UInt, error: UInt) { + let invoke = Invoke(command: command.to_string(), sendResponse: { (successResult: JsonValue?, errorResult: JsonValue?) -> Void in let (fn, payload) = errorResult == nil ? (callback, successResult) : (error, errorResult) var payloadJson: String do { @@ -116,18 +117,18 @@ func postIpcMessage(webview: WKWebView, name: UnsafePointer, command: } webview.evaluateJavaScript("window['_\(fn)'](\(payloadJson))") }, data: JSTypes.coerceDictionaryToJSObject(data, formattingDatesAsStrings: true)) - PluginManager.shared.invoke(name: name.pointee.to_string(), invoke: invoke) + PluginManager.shared.invoke(name: name.to_string(), invoke: invoke) } @_cdecl("run_plugin_method") func runPluginMethod( id: Int, - name: UnsafePointer, - command: UnsafePointer, + name: SRString, + command: SRString, data: NSDictionary, callback: @escaping @convention(c) (Int, Bool, UnsafePointer?) -> Void ) { - let invoke = Invoke(command: command.pointee.to_string(), sendResponse: { (successResult: JsonValue?, errorResult: JsonValue?) -> Void in + let invoke = Invoke(command: command.to_string(), sendResponse: { (successResult: JsonValue?, errorResult: JsonValue?) -> Void in let (success, payload) = errorResult == nil ? (true, successResult) : (false, errorResult) var payloadJson: String = "" do { @@ -137,5 +138,5 @@ func runPluginMethod( } callback(id, success, payloadJson.cString(using: String.Encoding.utf8)) }, data: JSTypes.coerceDictionaryToJSObject(data, formattingDatesAsStrings: true)) - PluginManager.shared.invoke(name: name.pointee.to_string(), invoke: invoke) + PluginManager.shared.invoke(name: name.to_string(), invoke: invoke) } diff --git a/core/tauri/src/ios.rs b/core/tauri/src/ios.rs index 4223e202d..24fec912b 100644 --- a/core/tauri/src/ios.rs +++ b/core/tauri/src/ios.rs @@ -5,33 +5,41 @@ use cocoa::base::{id, nil, NO, YES}; use objc::*; use serde_json::Value as JsonValue; -use swift_rs::SRString; +use swift_rs::{swift, SRString, SwiftArg}; -use std::os::raw::{c_char, c_int}; +use std::{ + ffi::c_void, + os::raw::{c_char, c_int}, +}; -type PluginMessageCallback = unsafe extern "C" fn(c_int, c_int, *const c_char); +type PluginMessageCallbackFn = unsafe extern "C" fn(c_int, c_int, *const c_char); +pub struct PluginMessageCallback(pub PluginMessageCallbackFn); -extern "C" { - pub fn post_ipc_message( - webview: id, - name: &SRString, - method: &SRString, - data: id, - callback: usize, - error: usize, - ); +impl<'a> SwiftArg<'a> for PluginMessageCallback { + type ArgType = PluginMessageCallbackFn; - pub fn run_plugin_method( - id: i32, - name: &SRString, - method: &SRString, - data: id, - callback: PluginMessageCallback, - ); - - pub fn on_webview_created(webview: id, controller: id); + unsafe fn as_arg(&'a self) -> Self::ArgType { + self.0 + } } +swift!(pub fn post_ipc_message( + webview: *const c_void, + name: &SRString, + method: &SRString, + data: *const c_void, + callback: usize, + error: usize +)); +swift!(pub fn run_plugin_method( + id: i32, + name: &SRString, + method: &SRString, + data: *const c_void, + callback: PluginMessageCallback +)); +swift!(pub fn on_webview_created(webview: *const c_void, controller: *const c_void)); + pub fn json_to_dictionary(json: JsonValue) -> id { if let serde_json::Value::Object(map) = json { unsafe { diff --git a/core/tauri/src/lib.rs b/core/tauri/src/lib.rs index 554bc6dd3..28da9d2c9 100644 --- a/core/tauri/src/lib.rs +++ b/core/tauri/src/lib.rs @@ -163,7 +163,7 @@ #![warn(missing_docs, rust_2018_idioms)] #![cfg_attr(doc_cfg, feature(doc_cfg))] -#[cfg(any(target_os = "macos", target_os = "ios"))] +#[cfg(target_os = "ios")] #[doc(hidden)] pub use cocoa; #[cfg(target_os = "macos")] diff --git a/core/tauri/src/manager.rs b/core/tauri/src/manager.rs index bbf8a23c4..648a47545 100644 --- a/core/tauri/src/manager.rs +++ b/core/tauri/src/manager.rs @@ -1408,7 +1408,7 @@ impl WindowManager { { window .with_webview(|w| { - unsafe { crate::ios::on_webview_created(w.inner(), w.view_controller()) }; + unsafe { crate::ios::on_webview_created(w.inner() as _, w.view_controller() as _) }; }) .expect("failed to run on_webview_created hook"); } diff --git a/core/tauri/src/plugin/mobile.rs b/core/tauri/src/plugin/mobile.rs index 45b52994b..43c1b3f48 100644 --- a/core/tauri/src/plugin/mobile.rs +++ b/core/tauri/src/plugin/mobile.rs @@ -284,8 +284,8 @@ impl PluginHandle { id, &self.name.into(), &method.as_ref().into(), - crate::ios::json_to_dictionary(serde_json::to_value(payload).unwrap()), - plugin_method_response_handler, + crate::ios::json_to_dictionary(serde_json::to_value(payload).unwrap()) as _, + crate::ios::PluginMessageCallback(plugin_method_response_handler), ); } rx.recv() diff --git a/core/tauri/src/window.rs b/core/tauri/src/window.rs index a276f23cb..656d25ea1 100644 --- a/core/tauri/src/window.rs +++ b/core/tauri/src/window.rs @@ -1504,12 +1504,12 @@ impl Window { self.with_webview(move |webview| { unsafe { crate::ios::post_ipc_message( - webview.inner(), + webview.inner() as _, &plugin.as_str().into(), &heck::ToLowerCamelCase::to_lower_camel_case(message.command.as_str()) .as_str() .into(), - crate::ios::json_to_dictionary(message.payload), + crate::ios::json_to_dictionary(message.payload) as _, callback.0, error.0, ) diff --git a/examples/api/src-tauri/Cargo.lock b/examples/api/src-tauri/Cargo.lock index 5ff0e7f50..c9a463082 100644 --- a/examples/api/src-tauri/Cargo.lock +++ b/examples/api/src-tauri/Cargo.lock @@ -2893,6 +2893,16 @@ version = "2.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6bdef32e8150c2a081110b42772ffe7d7c9032b606bc226c8260fd97e0976601" +[[package]] +name = "swift-rs" +version = "0.3.0" +source = "git+https://github.com/Brendonovich/swift-rs?rev=56b14aa4aa61e93d0fddf695d0cad78b6dd392b4#56b14aa4aa61e93d0fddf695d0cad78b6dd392b4" +dependencies = [ + "base64 0.13.1", + "serde", + "serde_json", +] + [[package]] name = "swift-rs" version = "0.3.0" @@ -3045,7 +3055,7 @@ dependencies = [ "serialize-to-javascript", "shared_child", "state", - "swift-rs", + "swift-rs 0.3.0 (git+https://github.com/Brendonovich/swift-rs?rev=56b14aa4aa61e93d0fddf695d0cad78b6dd392b4)", "tar", "tauri-build", "tauri-macros", @@ -3078,7 +3088,7 @@ dependencies = [ "semver 1.0.16", "serde", "serde_json", - "swift-rs", + "swift-rs 0.3.0 (git+https://github.com/Brendonovich/swift-rs?rev=56b14aa4aa61e93d0fddf695d0cad78b6dd392b4)", "tauri-codegen", "tauri-utils", "tauri-winres", @@ -3134,7 +3144,7 @@ dependencies = [ "serde", "serde_json", "serde_repr", - "swift-rs", + "swift-rs 0.3.0 (git+https://github.com/Brendonovich/swift-rs?rev=eb6de914ad57501da5019154d476d45660559999)", "tauri", "tauri-build", "time", diff --git a/examples/api/src-tauri/src/lib.rs b/examples/api/src-tauri/src/lib.rs index 9eaf0d989..2d2c00838 100644 --- a/examples/api/src-tauri/src/lib.rs +++ b/examples/api/src-tauri/src/lib.rs @@ -65,7 +65,7 @@ pub fn run() { let response = app.sample().ping(PingRequest { value: value.clone(), }); - println!("got response: {:?}", response); + log::info!("got response: {:?}", response); if let Ok(res) = response { assert_eq!(res.value, value); }