-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow calling from more than one language #2
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -126,6 +126,14 @@ open class {{ impl_class_name }}: Disposable, AutoCloseable, {{ interface_name } | |
this.cleanable = UniffiLib.CLEANER.register(this, UniffiCleanAction(pointer)) | ||
} | ||
|
||
constructor(rbuf: RustBuffer) : this( | ||
rbuf.asByteBuffer()!!.let { buf -> | ||
val pointer = buf.getLong() | ||
buf.getInt() | ||
Pointer(pointer) | ||
} | ||
) | ||
|
||
/** | ||
* This constructor can be used to instantiate a fake object. Only used for tests. Any | ||
* attempt to actually use an object constructed this way will fail as there is no | ||
|
@@ -171,7 +179,11 @@ open class {{ impl_class_name }}: Disposable, AutoCloseable, {{ interface_name } | |
this.destroy() | ||
} | ||
|
||
{% if obj.has_callback_interface() -%} | ||
internal inline fun <R> callWithPointer(block: (ptr: RustBuffer.ByValue) -> R): R { | ||
{%- else %} | ||
internal inline fun <R> callWithPointer(block: (ptr: Pointer) -> R): R { | ||
{%- endif %} | ||
// Check and increment the call counter, to keep the object alive. | ||
// This needs a compare-and-set retry loop in case of concurrent updates. | ||
do { | ||
|
@@ -185,7 +197,11 @@ open class {{ impl_class_name }}: Disposable, AutoCloseable, {{ interface_name } | |
} while (! this.callCounter.compareAndSet(c, c + 1L)) | ||
// Now we can safely do the method call without the pointer being freed concurrently. | ||
try { | ||
{% if obj.has_callback_interface() -%} | ||
return block({{ ffi_converter_name }}.lowerPointer(this.uniffiClonePointer())) | ||
{%- else %} | ||
return block(this.uniffiClonePointer()) | ||
{%- endif %} | ||
} finally { | ||
// This decrement always matches the increment we performed above. | ||
if (this.callCounter.decrementAndGet() == 0L) { | ||
|
@@ -267,20 +283,52 @@ open class {{ impl_class_name }}: Disposable, AutoCloseable, {{ interface_name } | |
{% include "CallbackInterfaceImpl.kt" %} | ||
{%- endif %} | ||
|
||
{%- let trait_impl=format!("uniffiCallbackInterface{}", name) %} | ||
|
||
{%- if obj.has_callback_interface() %} | ||
/** | ||
* @suppress | ||
*/ | ||
public object {{ ffi_converter_name }}: FfiConverter<{{ type_name }}, Pointer> { | ||
{%- if obj.has_callback_interface() %} | ||
public object {{ ffi_converter_name }}: FfiConverterRustBuffer<{{ type_name }}> { | ||
internal val handleMap = UniffiHandleMap<{{ type_name }}>() | ||
{%- endif %} | ||
internal val langIndex: Int = {{ trait_impl }}.register(UniffiLib.INSTANCE) | ||
|
||
override fun read(buf: ByteBuffer): {{ type_name }} { | ||
// The Rust code always writes pointers as 8 bytes, and will | ||
// fail to compile if they don't fit. | ||
val handle = buf.getLong() | ||
val langIndex = buf.getInt() | ||
if (handleMap.has(handle)) { | ||
return handleMap.get(handle) | ||
} else { | ||
return {{ impl_class_name }}(Pointer(handle)) | ||
} | ||
Comment on lines
+301
to
+305
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure that this right yet: we should be able to detect here if the trait was implemented in our language, Rust or another foreign language. |
||
} | ||
|
||
override fun allocationSize(value: {{ type_name }}) = 12UL | ||
|
||
fun lowerPointer(pointer: Pointer): RustBuffer.ByValue = | ||
RustBuffer.write(12UL) { bbuf -> | ||
bbuf.putLong(Pointer.nativeValue(pointer)) | ||
bbuf.putInt(langIndex) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is how we have to handle it with the current handle system. I'm hoping to implement something like mozilla#1823, which would allow us to pack both the pointer and the This means creating some sort of tagged pointer. The simplest version of that would be using the lower bits for the langIndex. We know that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 3 languages at the same time sounds like it should be enough. Just for the sake of not boxing ourselves into a corner: I should imagine if we want more than 3 then we'd need to move to 64bits for everything, which also sounds do-able, if absolutely necessary. |
||
} | ||
|
||
override fun write(value: {{ type_name }}, buf: ByteBuffer) { | ||
// The Rust code always expects pointers written as 8 bytes, | ||
// and will fail to compile if they don't fit. | ||
val handle = handleMap.insert(value) | ||
buf.putLong(handle) | ||
buf.putInt(this.langIndex) | ||
} | ||
} | ||
|
||
{%- else %} | ||
/** | ||
* @suppress | ||
*/ | ||
public object {{ ffi_converter_name }}: FfiConverter<{{ type_name }}, Pointer> { | ||
override fun lower(value: {{ type_name }}): Pointer { | ||
{%- if obj.has_callback_interface() %} | ||
return Pointer(handleMap.insert(value)) | ||
{%- else %} | ||
return value.uniffiClonePointer() | ||
{%- endif %} | ||
} | ||
|
||
override fun lift(value: Pointer): {{ type_name }} { | ||
|
@@ -301,3 +349,5 @@ public object {{ ffi_converter_name }}: FfiConverter<{{ type_name }}, Pointer> { | |
buf.putLong(Pointer.nativeValue(lower(value))) | ||
} | ||
} | ||
|
||
{%- endif %} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,29 +8,38 @@ | |
//! code loads the exported library. For each callback type, we also define a "cell" type for | ||
//! storing the callback. | ||
|
||
use std::{ | ||
ptr::{null_mut, NonNull}, | ||
sync::atomic::{AtomicPtr, Ordering}, | ||
}; | ||
use std::{ptr::NonNull, sync::Mutex}; | ||
|
||
// Cell type that stores any NonNull<T> | ||
#[doc(hidden)] | ||
pub struct UniffiForeignPointerCell<T>(AtomicPtr<T>); | ||
pub struct UniffiForeignPointerCell<T> { | ||
pointers: Mutex<Vec<*mut T>>, | ||
} | ||
Comment on lines
+15
to
+17
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm sure this could be a better name. Is it still a Would this also be better as a |
||
|
||
impl<T> UniffiForeignPointerCell<T> { | ||
pub const fn new() -> Self { | ||
Self(AtomicPtr::new(null_mut())) | ||
Self { | ||
pointers: Mutex::new(Vec::new()), | ||
} | ||
} | ||
|
||
pub fn set(&self, callback: NonNull<T>) { | ||
self.0.store(callback.as_ptr(), Ordering::Relaxed); | ||
pub fn set(&self, callback: NonNull<T>) -> usize { | ||
let mut pointers = self.pointers.lock().unwrap(); | ||
let index = pointers.len(); | ||
pointers.push(callback.as_ptr()); | ||
index | ||
} | ||
|
||
pub fn get(&self) -> &T { | ||
pub fn get(&self, index: usize) -> &T { | ||
let pointers = self.pointers.lock().unwrap(); | ||
if index >= pointers.len() { | ||
panic!("Foreign pointer used before being set. This is likely a uniffi bug.") | ||
} | ||
unsafe { | ||
NonNull::new(self.0.load(Ordering::Relaxed)) | ||
.expect("Foreign pointer not set. This is likely a uniffi bug.") | ||
.as_mut() | ||
let ptr = pointers[index]; | ||
NonNull::new(ptr) | ||
.expect("Foreign pointer not set. This is likely a uniffi bug.") | ||
.as_ref() | ||
} | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't true in all cases: we should be able to call from one language to another via Rust.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I think there's 2 options here:
The second option seems better for performance and I kind of like not special-casing Rust. I might be a pain to implement though.