Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions crates/perry-ui-macos/src/app.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::ffi::sel_registerName;
use crate::ffi::class_addMethod;
use objc2::rc::Retained;
use objc2::runtime::{AnyObject, Sel};
use objc2::{define_class, msg_send, AnyThread, DefinedClass, MainThreadOnly};
Expand Down Expand Up @@ -829,17 +831,10 @@ pub fn app_set_frameless(app_handle: i64, value: f64) {
extra: usize,
) -> *mut std::ffi::c_void;
fn objc_registerClassPair(cls: *mut std::ffi::c_void);
fn class_addMethod(
cls: *mut std::ffi::c_void,
sel: *const std::ffi::c_void,
imp: extern "C" fn(*mut std::ffi::c_void, *mut std::ffi::c_void) -> i8,
types: *const i8,
) -> i8;
fn object_setClass(
obj: *mut std::ffi::c_void,
cls: *mut std::ffi::c_void,
) -> *mut std::ffi::c_void;
fn sel_registerName(name: *const i8) -> *mut std::ffi::c_void;
fn object_getClass(obj: *const std::ffi::c_void) -> *mut std::ffi::c_void;
}
extern "C" fn can_become_key(
Expand All @@ -859,7 +854,7 @@ pub fn app_set_frameless(app_handle: i64, value: f64) {
let cls = objc_allocateClassPair(parent_class, subclass_name.as_ptr(), 0);
if !cls.is_null() {
let sel = sel_registerName(c"canBecomeKeyWindow".as_ptr());
class_addMethod(cls, sel, can_become_key, c"B@:".as_ptr());
class_addMethod(cls, sel, can_become_key as *const std::ffi::c_void, c"B@:".as_ptr());
objc_registerClassPair(cls);
}
cls
Expand Down
8 changes: 4 additions & 4 deletions crates/perry-ui-macos/src/audio.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::ffi::js_array_push_f64;
use crate::ffi::js_string_from_bytes;
use objc2::msg_send;
use objc2::rc::Retained;
use objc2::runtime::{AnyClass, AnyObject};
Expand Down Expand Up @@ -144,9 +146,7 @@ thread_local! {
// =============================================================================

extern "C" {
fn js_string_from_bytes(ptr: *const u8, len: i32) -> i64;
fn js_array_create() -> i64;
fn js_array_push_f64(array_ptr: i64, value: f64);
}

/// ObjC type encoding for AVAudioPCMBuffer block:
Expand Down Expand Up @@ -291,7 +291,7 @@ pub fn get_waveform(count: f64) -> f64 {
// Read from ring buffer, starting from oldest
let idx = (write_idx + WAVEFORM_SIZE - n + i) % WAVEFORM_SIZE;
let sample = WAVEFORM_BUFFER[idx];
js_array_push_f64(array, sample);
js_array_push_f64(array as *mut std::ffi::c_void, sample);
}
// Return as f64 (NaN-boxed pointer)
f64::from_bits(array as u64)
Expand All @@ -305,7 +305,7 @@ pub fn get_waveform(count: f64) -> f64 {
/// pointer bits through as a double and callers read `NaN`).
pub fn get_device_model() -> i64 {
let model = get_sysctl_model();
unsafe { js_string_from_bytes(model.as_ptr(), model.len() as i32) }
unsafe { js_string_from_bytes(model.as_ptr(), model.len() as u32) as i64 }
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}

/// Set the output filename a subsequent `start_recording()` will write to.
Expand Down
9 changes: 2 additions & 7 deletions crates/perry-ui-macos/src/audio_playback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
//! PlaybackId 0x10000001 ..= 0x1FFFFFFF — live AVAudioPlayerNode voice
//! Bus 0x20000001 ..= 0x2FFFFFFF — AVAudioMixerNode group (0 = master)

use crate::ffi::sel_registerName;
use crate::ffi::class_addMethod;
use objc2::msg_send;
use objc2::rc::Retained;
use objc2::runtime::{AnyClass, AnyObject, Sel};
Expand All @@ -31,13 +33,6 @@ extern "C" {
extra_bytes: usize,
) -> *mut std::ffi::c_void;
fn objc_registerClassPair(cls: *mut std::ffi::c_void);
fn class_addMethod(
cls: *mut std::ffi::c_void,
sel: *const std::ffi::c_void,
imp: *const std::ffi::c_void,
types: *const i8,
) -> bool;
fn sel_registerName(name: *const i8) -> *const std::ffi::c_void;
fn objc_getClass(name: *const i8) -> *const std::ffi::c_void;
}

Expand Down
4 changes: 2 additions & 2 deletions crates/perry-ui-macos/src/clipboard.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::ffi::js_string_from_bytes;
use objc2::msg_send;
use objc2::rc::Retained;
use objc2_app_kit::NSPasteboard;
use objc2_foundation::NSString;

extern "C" {
fn js_string_from_bytes(ptr: *const u8, len: i64) -> *const u8;
fn js_nanbox_string(ptr: i64) -> f64;
}

Expand All @@ -18,7 +18,7 @@ pub fn read() -> f64 {
if let Some(text) = result {
let rust_str = text.to_string();
let bytes = rust_str.as_bytes();
let str_ptr = js_string_from_bytes(bytes.as_ptr(), bytes.len() as i64);
let str_ptr = js_string_from_bytes(bytes.as_ptr(), bytes.len() as u32);
js_nanbox_string(str_ptr as i64)
} else {
// Return TAG_UNDEFINED
Expand Down
2 changes: 1 addition & 1 deletion crates/perry-ui-macos/src/deeplinks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
//! handler is held in `PENDING_COLD_START` and replayed synchronously
//! when the handler eventually arrives.

use crate::ffi::js_string_from_bytes;
use std::cell::RefCell;

use objc2::msg_send;
Expand All @@ -33,7 +34,6 @@ extern "C" {
fn js_promise_run_microtasks() -> i32;
fn js_nanbox_get_pointer(value: f64) -> i64;
fn js_closure_call2(closure: *const u8, arg0: f64, arg1: f64) -> f64;
fn js_string_from_bytes(ptr: *const u8, len: u32) -> *mut u8;
fn js_nanbox_string(ptr: i64) -> f64;
}

Expand Down
4 changes: 2 additions & 2 deletions crates/perry-ui-macos/src/drag_drop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
//! advertises copy. When no provider is set, `mouseDown:` forwards to the
//! original class's implementation so interactive controls keep working.

use crate::ffi::js_array_push_f64;
use crate::ffi::js_string_from_bytes;
use objc2::rc::Retained;
use objc2::runtime::{AnyClass, AnyObject, Bool, ClassBuilder, Imp, Sel};
use objc2::{msg_send, sel};
Expand All @@ -39,11 +41,9 @@ extern "C" {
fn js_nanbox_get_pointer(value: f64) -> i64;
fn js_nanbox_pointer(ptr: i64) -> f64;
fn js_nanbox_string(ptr: i64) -> f64;
fn js_string_from_bytes(ptr: *const u8, len: u32) -> *mut c_void;
fn js_object_alloc(class_id: u32, field_count: u32) -> *mut c_void;
fn js_object_set_field_by_name(obj: *mut c_void, key: *const c_void, value: f64);
fn js_array_alloc(capacity: u32) -> *mut c_void;
fn js_array_push_f64(arr: *mut c_void, value: f64) -> *mut c_void;
fn js_jsvalue_to_string(value: f64) -> *const u8;
}

Expand Down
68 changes: 68 additions & 0 deletions crates/perry-ui-macos/src/ffi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//! Canonical `extern "C"` declarations shared across this crate.
//!
//! These symbols were previously re-declared per-file with divergent signatures,
//! triggering ~44 `clashing_extern_declarations` warnings — and, for
//! `js_string_from_bytes`, a real ABI mismatch (`len: i64` vs the runtime's
//! `len: u32`) that only worked by register luck. Declare each exactly once here,
//! matching perry-runtime's real ABI (or the system ABI for objc/CoreGraphics),
//! and have every call site `use crate::ffi::*`.

use std::ffi::c_void;

use crate::string_header::StringHeader;

// ---------------------------------------------------------------------------
// perry-runtime symbols
// ---------------------------------------------------------------------------

extern "C" {
/// Canonical: perry-runtime `js_string_from_bytes(data: *const u8, len: u32) -> *mut StringHeader`.
pub fn js_string_from_bytes(data: *const u8, len: u32) -> *mut StringHeader;

/// Canonical: perry-runtime returns the array header pointer. Opaque here.
pub fn js_array_push_f64(arr: *mut c_void, value: f64) -> *mut c_void;

/// Canonical: perry-runtime `js_get_string_pointer_unified(value: f64) -> i64`.
pub fn js_get_string_pointer_unified(value: f64) -> i64;
}

// ---------------------------------------------------------------------------
// Objective-C runtime symbols (system ABI)
// ---------------------------------------------------------------------------

extern "C" {
/// objc `sel_registerName(const char *) -> SEL`. SEL is an opaque pointer.
pub fn sel_registerName(name: *const i8) -> *const c_void;

/// objc `class_addMethod(Class, SEL, IMP, const char *types) -> BOOL`.
/// `imp` and `sel` are opaque pointers; pass function pointers via `as *const c_void`.
pub fn class_addMethod(
cls: *mut c_void,
sel: *const c_void,
imp: *const c_void,
types: *const i8,
) -> bool;
}

// ---------------------------------------------------------------------------
// CoreGraphics symbols (system ABI)
//
// The context is an opaque `CGContextRef` (`*mut c_void`); pass any objc
// `*mut AnyObject` handle via `as *mut c_void`. Only the functions declared by
// more than one file live here — file-specific ones (gradients, image drawing,
// arcs) stay local to their single caller.
// ---------------------------------------------------------------------------

extern "C" {
pub fn CGContextSetRGBFillColor(c: *mut c_void, r: f64, g: f64, b: f64, a: f64);
pub fn CGContextSetRGBStrokeColor(c: *mut c_void, r: f64, g: f64, b: f64, a: f64);
pub fn CGContextSetLineWidth(c: *mut c_void, width: f64);
pub fn CGContextFillRect(c: *mut c_void, rect: objc2_core_foundation::CGRect);
pub fn CGContextStrokeRect(c: *mut c_void, rect: objc2_core_foundation::CGRect);
pub fn CGContextBeginPath(c: *mut c_void);
pub fn CGContextMoveToPoint(c: *mut c_void, x: f64, y: f64);
pub fn CGContextAddLineToPoint(c: *mut c_void, x: f64, y: f64);
pub fn CGContextStrokePath(c: *mut c_void);
pub fn CGContextFillPath(c: *mut c_void);
pub fn CGContextClosePath(c: *mut c_void);
}
8 changes: 4 additions & 4 deletions crates/perry-ui-macos/src/file_dialog.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::ffi::js_string_from_bytes;
use objc2::msg_send;
use objc2_app_kit::NSOpenPanel;
use objc2_foundation::{MainThreadMarker, NSString};

extern "C" {
fn js_closure_call1(closure: *const u8, arg: f64) -> f64;
fn js_nanbox_get_pointer(value: f64) -> i64;
fn js_string_from_bytes(ptr: *const u8, len: i64) -> *const u8;
fn js_nanbox_string(ptr: i64) -> f64;
}

Expand Down Expand Up @@ -34,7 +34,7 @@ pub fn open_dialog(callback: f64) {
let path_str: objc2::rc::Retained<NSString> = msg_send![url, path];
let rust_str = path_str.to_string();
let bytes = rust_str.as_bytes();
let str_ptr = js_string_from_bytes(bytes.as_ptr(), bytes.len() as i64);
let str_ptr = js_string_from_bytes(bytes.as_ptr(), bytes.len() as u32);
let nanboxed = js_nanbox_string(str_ptr as i64);
js_closure_call1(closure_ptr, nanboxed);
} else {
Expand Down Expand Up @@ -70,7 +70,7 @@ pub fn open_folder_dialog(callback: f64) {
let path_str: objc2::rc::Retained<NSString> = msg_send![url, path];
let rust_str = path_str.to_string();
let bytes = rust_str.as_bytes();
let str_ptr = js_string_from_bytes(bytes.as_ptr(), bytes.len() as i64);
let str_ptr = js_string_from_bytes(bytes.as_ptr(), bytes.len() as u32);
let nanboxed = js_nanbox_string(str_ptr as i64);
js_closure_call1(closure_ptr, nanboxed);
} else {
Expand Down Expand Up @@ -121,7 +121,7 @@ pub fn save_dialog(callback: f64, default_name_ptr: *const u8, _allowed_types_pt
let path_str: objc2::rc::Retained<NSString> = msg_send![url, path];
let rust_str = path_str.to_string();
let bytes = rust_str.as_bytes();
let str_ptr = js_string_from_bytes(bytes.as_ptr(), bytes.len() as i64);
let str_ptr = js_string_from_bytes(bytes.as_ptr(), bytes.len() as u32);
let nanboxed = js_nanbox_string(str_ptr as i64);
js_closure_call1(closure_ptr, nanboxed);
} else {
Expand Down
11 changes: 3 additions & 8 deletions crates/perry-ui-macos/src/geolocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
//! older API; this module ships the issue-#552 surface with a 4-arg success
//! callback `(lat, lng, accuracy, timestamp)` plus a separate error callback.

use crate::ffi::sel_registerName;
use crate::ffi::class_addMethod;
use crate::ffi::js_string_from_bytes;
use objc2::rc::Retained;
use objc2::runtime::{AnyClass, AnyObject};
use objc2::{msg_send, Encode, Encoding, RefEncode};
Expand All @@ -17,7 +20,6 @@ extern "C" {
fn js_nanbox_get_pointer(value: f64) -> i64;
fn js_closure_call1(closure: *const u8, arg: f64) -> f64;
fn js_closure_call4(closure: *const u8, arg0: f64, arg1: f64, arg2: f64, arg3: f64) -> f64;
fn js_string_from_bytes(ptr: *const u8, len: u32) -> *mut u8;
fn js_nanbox_string(ptr: i64) -> f64;
}

Expand All @@ -28,13 +30,6 @@ extern "C" {
extra_bytes: usize,
) -> *mut std::ffi::c_void;
fn objc_registerClassPair(cls: *mut std::ffi::c_void);
fn class_addMethod(
cls: *mut std::ffi::c_void,
sel: *const std::ffi::c_void,
imp: *const std::ffi::c_void,
types: *const i8,
) -> bool;
fn sel_registerName(name: *const i8) -> *const std::ffi::c_void;
fn objc_getClass(name: *const i8) -> *const std::ffi::c_void;
}

Expand Down
4 changes: 2 additions & 2 deletions crates/perry-ui-macos/src/image_picker.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! Photo-library image picker (issue #552) — macOS NSOpenPanel filtered
//! to common image UTIs. Returns an array of absolute filesystem paths.

use crate::ffi::js_array_push_f64;
use crate::ffi::js_string_from_bytes;
use objc2::msg_send;
use objc2_app_kit::NSOpenPanel;
use objc2_foundation::{MainThreadMarker, NSArray, NSString};
Expand All @@ -10,10 +12,8 @@ extern "C" {
fn js_promise_run_microtasks() -> i32;
fn js_nanbox_get_pointer(value: f64) -> i64;
fn js_closure_call1(closure: *const u8, arg: f64) -> f64;
fn js_string_from_bytes(ptr: *const u8, len: u32) -> *mut u8;
fn js_nanbox_string(ptr: i64) -> f64;
fn js_array_alloc(capacity: u32) -> *mut std::ffi::c_void;
fn js_array_push_f64(arr: *mut std::ffi::c_void, value: f64) -> *mut std::ffi::c_void;
fn js_nanbox_pointer(ptr: i64) -> f64;
}

Expand Down
4 changes: 2 additions & 2 deletions crates/perry-ui-macos/src/keychain.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::ffi::js_string_from_bytes;
use std::ffi::c_void;

extern "C" {
fn js_string_from_bytes(ptr: *const u8, len: i64) -> *const u8;
fn js_nanbox_string(ptr: i64) -> f64;
}

Expand Down Expand Up @@ -126,7 +126,7 @@ pub fn get(key_ptr: *const u8) -> f64 {
let bytes: *const u8 = objc2::msg_send![data, bytes];
let length: usize = objc2::msg_send![data, length];

let str_ptr = js_string_from_bytes(bytes, length as i64);
let str_ptr = js_string_from_bytes(bytes, length as u32);
js_nanbox_string(str_ptr as i64)
} else {
f64::from_bits(0x7FFC_0000_0000_0001) // TAG_UNDEFINED
Expand Down
1 change: 1 addition & 0 deletions crates/perry-ui-macos/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub mod background;
pub mod clipboard;
pub mod crash_log;
pub mod deeplinks;
pub mod ffi;
pub mod drag_drop;
pub mod file_dialog;
pub mod geolocation;
Expand Down
11 changes: 3 additions & 8 deletions crates/perry-ui-macos/src/lib_ffi/core_widgets.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::ffi::js_string_from_bytes;
use crate::*;

// =============================================================================
Expand Down Expand Up @@ -79,15 +80,9 @@ pub extern "C" fn perry_ui_poll_open_file() -> i64 {
let path = app::poll_open_file();
if path.is_empty() {
// Return empty string
extern "C" {
fn js_string_from_bytes(ptr: *const u8, len: i32) -> i64;
}
unsafe { js_string_from_bytes(std::ptr::null(), 0) }
unsafe { js_string_from_bytes(std::ptr::null(), 0) as i64 }
} else {
extern "C" {
fn js_string_from_bytes(ptr: *const u8, len: i32) -> i64;
}
unsafe { js_string_from_bytes(path.as_ptr(), path.len() as i32) }
unsafe { js_string_from_bytes(path.as_ptr(), path.len() as u32) as i64 }
}
}

Expand Down
Loading
Loading