feat(core): update swift-rs

This commit is contained in:
Lucas Nogueira
2023-02-24 11:13:07 -03:00
parent 971ad7ae06
commit a0a5eb2170
11 changed files with 61 additions and 42 deletions

View File

@@ -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" ]

View File

@@ -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"

View File

@@ -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.

View File

@@ -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<SRString>, command: UnsafePointer<SRString>, 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<SRString>, 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<SRString>,
command: UnsafePointer<SRString>,
name: SRString,
command: SRString,
data: NSDictionary,
callback: @escaping @convention(c) (Int, Bool, UnsafePointer<CChar>?) -> 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)
}

View File

@@ -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 {

View File

@@ -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")]

View File

@@ -1408,7 +1408,7 @@ impl<R: Runtime> WindowManager<R> {
{
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");
}

View File

@@ -284,8 +284,8 @@ impl<R: Runtime> PluginHandle<R> {
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()

View File

@@ -1504,12 +1504,12 @@ impl<R: Runtime> Window<R> {
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,
)

View File

@@ -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",

View File

@@ -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);
}