forked from mozilla/uniffi-rs
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Consolidate/Simplify the foreign bindings handlemap code
I'm trying to get mozilla#1823 merged now that 0.26.0 is out, but I'll break it up a bit to make it easier. The first step is consolidating the foreign handle map code. Foreign now languages define 1 handle map and use it for all object types. I tried to simplify the handle map implementations, the had a lot of features that we weren't using at all and seemed to be only half-implemneted to me, like the right map and counter map. Handles are always `u64` values. This allows us to remove some code like the `USize` handling on Kotlin and the `FfiType::RustFutureContinuationData` variant.
- Loading branch information
Showing
35 changed files
with
170 additions
and
355 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Handle from a UniffiHandleMap | ||
internal typealias UniffiHandle = Long | ||
|
||
// Map handles to objects | ||
// | ||
// This is used pass an opaque 64-bit handle representing a foreign object to the Rust code. | ||
internal class UniffiHandleMap<T: Any> { | ||
private val map = ConcurrentHashMap<UniffiHandle, T>() | ||
private val counter = java.util.concurrent.atomic.AtomicLong(0) | ||
|
||
val size: Int | ||
get() = map.size | ||
|
||
// Insert a new object into the handle map and get a handle for it | ||
fun insert(obj: T): UniffiHandle { | ||
val handle = counter.getAndAdd(1) | ||
map.put(handle, obj) | ||
return handle | ||
} | ||
|
||
// Get an object from the handle map | ||
fun get(handle: UniffiHandle): T { | ||
return map.get(handle) ?: throw InternalException("UniffiHandleMap.get: Invalid handle") | ||
} | ||
|
||
// Remove an entry from the handlemap and get the Kotlin object back | ||
fun remove(handle: UniffiHandle): T { | ||
return map.remove(handle) ?: throw InternalException("UniffiHandleMap: Invalid handle") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
class _UniffiHandleMap: | ||
""" | ||
A map where inserting, getting and removing data is synchronized with a lock. | ||
""" | ||
|
||
def __init__(self): | ||
# type Handle = int | ||
self._map = {} # type: Dict[Handle, Any] | ||
self._lock = threading.Lock() | ||
self._counter = itertools.count() | ||
|
||
def insert(self, obj): | ||
with self._lock: | ||
handle = next(self._counter) | ||
self._map[handle] = obj | ||
return handle | ||
|
||
def get(self, handle): | ||
try: | ||
with self._lock: | ||
return self._map[handle] | ||
except KeyError: | ||
raise InternalError("UniffiHandleMap.get: Invalid handle") | ||
|
||
def remove(self, handle): | ||
try: | ||
with self._lock: | ||
return self._map.pop(handle) | ||
except KeyError: | ||
raise InternalError("UniffiHandleMap.remove: Invalid handle") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.