no clue what this is
parent: tbd commit: 7889bf6
1 | //! Giterated ABI |
2 | //! # ABI |
3 | //! |
4 | //! ## Value ABI |
5 | //! |
6 | //! At its core, the Giterated Runtime uses the `extern "C"` ABI. What that means is likely platform specific, and doesn't matter. |
7 | //! You are intended to compile the Giterated Runtime and Plugins for your local machine, all with a similar idea of what |
8 | //! your "local machine" is. |
9 | //! |
10 | //! Values are passed using the `FFI` type. There are four categories of value that the `FFI` type enables you to pass: |
11 | //! |
12 | //! | `FFI` Type Category | Placed Backing? | Owned? | |
13 | //! |---------------------|-----------------|--------| |
14 | //! | Slice | Heap/Stack | No | |
15 | //! | Referenced Slice | Stack | No | |
16 | //! | Referenced Value | No | No | |
17 | //! | Owned Value | Heap | Yes | |
18 | //! |
19 | //! For an FFI type to have a "placed backing" is for it to have some data structure beyond the data it represents, placed |
20 | //! somewhere in memory. Some types only require stack placement while some offer both stack and heap placement. |
21 | //! |
22 | //! Stack-placed values can be shared by `PinnedRef` and `PinnedMut`, and thus can only be owned by the caller. |
23 | //! |
24 | //! Heap-placed values can be shared by `Owned`, `PinnedRef`, and `PinnedMut`. They can be owned by any one consumer, |
25 | //! When the handle with ownership is `Drop`'d by the sole consumer, it will free the object using the associated `Drop` callback. |
26 | //! |
27 | //! ### Safety Intents |
28 | //! |
29 | //! This API is designed to simplify interaction with FFI values, and provide a static ABI for those values to be passed. It |
30 | //! is key to enabling ownership across FFI while ensuring associated dropping and allocation freeing logic is ran. |
31 | //! |
32 | //! The contract the developer has to follow is made simpler by this system, and it allows for generic code to be written to |
33 | //! interact with FFI-given values and pass values using FFI. |
34 | //! |
35 | //! ### Stability Guarantees |
36 | //! |
37 | //! There are no plans to guarantee stability until 1.0.0. At that point you can expect the ABI to remain stable until the major version |
38 | //! is incremented again. There will be an appropriate deprecation process and changeover period. |
39 | //! |
40 | //! ### Memory Representation |
41 | //! |
42 | //! Please check out the source code, sorry if you needed that from the docs! |
43 | //! |
44 | //! ## Object, Operation, Setting, Value, Plugin, and Runtime ABIs |
45 | //! |
46 | //! The Giterated Runtime uses vtables to accomplish the goal of ensuring maximum compatibility. For every object that is shared |
47 | //! between plugins, a vtable is used to allow each plugin to provide their own code for interacting with the object. |
48 | //! |
49 | //! When objects switch "runtime domains" (e.g. host -> plugin, plugin -> plugin, plugin -> host), their vtable is swapped out |
50 | //! for the new runtime domain's own vtables. |
51 | //! |
52 | //! ### Untyped "Objects" (see above header for list) |
53 | //! |
54 | //! Untyped objects, in memory, are represented by a data pointer and a vtable pointer. Exactly like Rust traits. However, to |
55 | //! prevent small compilation differences and other random garbage from making the interface not perfectly compatible we use |
56 | //! the local plugin's idea of the vtable for the object at all times. An object that the plugin does not have a vtable for cannot |
57 | //! be relevant to the plugin. |
58 | //! |
59 | //! It is important that the object's base representation in memory remain unchanged between major versions, but the vtables that provide methods for |
60 | //! that object may be grown. The methods that operate on that object may be changed in an non-breaking fashion, and bugs can be |
61 | //! fixed. |
62 | //! |
63 | //! ## Futures ABI |
64 | //! |
65 | //! The Giterated Runtime has an async runtime that allows for futures to be shared and awaited across FFI boundaries while only |
66 | //! executing the future within the context of the Plugin who is running the underlying future. |
67 | //! |
68 | //! Futures are spawned onto the `RuntimeState` with the `RuntimeFuturesExt` trait. This takes a Rust future, boxes it, and |
69 | //! provides a `RuntimeFuture` handle that can be used to drive the underlying Rust future locally. The `RuntimeFuture` handle |
70 | //! is thread-safe and can be shared with the callee and `.await`'d directly like any other future. |
71 | //! |
72 | //! ### RuntimeFuture |
73 | //! |
74 | //! The `RuntimeFuture` mixes a vtable with data to allow for any caller to drive a spawned future. It contains: |
75 | //! |
76 | //! - A `poll_fn` which is used to poll the future for `Ready`-ness |
77 | //! - A `wake_fn` which is used to wake the callee to poll for (expected) `Ready`-ness, it is populated when the `RuntimeFuture` is `await`'d. |
78 | //! |
79 | //! When the `RuntimeFuture` is polled, it causes the inner future to also be polled. We provide the inner future with a waker |
80 | //! that triggers the `RuntimeFuture`'s waker so it is polled again. Breaking character to point out how freaking cool that is. |
81 | //! |
82 | //! `RuntimeFuture`s drop the associated inner future as they drop. |
83 | |
84 | |
85 | |
86 | |
87 | |
88 | |
89 | |
90 | |
91 | |
92 | |
93 | use ; |
94 | pub use ; |
95 | use HeapPlacable; |
96 | use FfiValueUntyped; |
97 | |
98 | use |
99 | , | PhantomData
100 | , |
101 | , |
102 | ; |
103 | |
104 | use ; |
105 | use ; |
106 | |
107 | |
108 | |
109 | pub use crate Ffi; |
110 | pub use crate StackPinned; |
111 | pub use crate::*; |
112 | pub use crate::; |
113 | |
114 | |
115 | /// Slice Reference |
116 | /// Heap or Stack Placed |
117 | pub type FfiSliceRef<T> = ; |
118 | |
119 | /// Mutable Slice Reference |
120 | /// Heap or Stack Placed |
121 | pub type FfiSliceMut<T> = ; |
122 | |
123 | /// Value Reference |
124 | /// Heap or Stack Placed |
125 | pub type FfiValueRef<T> = ; |
126 | |
127 | /// Mutable Value Reference |
128 | /// Heap or Stack Placed |
129 | pub type FfiValueMut<T> = ; |
130 | |
131 | /// Owned Value |
132 | /// Heap Placed |
133 | pub type FfiValue<T> = ; |
134 | |
135 | /// Owned Slice |
136 | /// Heap Placed |
137 | pub type FfiSlice<T> = ; |
138 | |
139 | |
140 | use crate::; |
141 | |
142 | pub type FfiValueUntyped = ; |
143 | pub type FfiValueRefUntyped = ; |
144 | |
145 | |
146 | /// A value passed over FFI, following the Giterated ABI. |
147 | /// |
148 | /// The function of the [`Ffi`] type is to take an arbitrary pointer and send it over FFI. |
149 | /// Both the caller and callee **must** have the same understanding of what the pointer represents. |
150 | /// The [`Ffi`] type is also used to encode ownership information. |
151 | /// |
152 | /// # The Pointer |
153 | /// The pointer contained within the [`Ffi`] is transmuted based on the provided `ABI` on the |
154 | /// [`Ffi`] type signature. |
155 | |
156 | |
157 | inner: *const (), |
158 | _type_marker: , |
159 | _abi_marker: , |
160 | |
161 | |
162 | |
163 | |
164 | |
165 | unsafe |
166 | |
167 | |
168 | |
169 | |
170 | type Target = ; |
171 | |
172 | |
173 | |
174 | let inner: *const = unsafe ; |
175 | let backing = unsafe ; |
176 | |
177 | unsafe |
178 | from_raw_parts |
179 | backing.slice as *mut T, |
180 | usize try_from .unwrap_unchecked, |
181 | |
182 | |
183 | |
184 | |
185 | |
186 | |
187 | |
188 | let inner: *mut = unsafe ; |
189 | let backing = unsafe ; |
190 | |
191 | unsafe |
192 | from_raw_parts_mut |
193 | backing.slice as *mut T, |
194 | usize try_from .unwrap_unchecked, |
195 | |
196 | |
197 | |
198 | |
199 | |
200 | |
201 | |
202 | todo! |
203 | |
204 | |
205 | |
206 | |
207 | type Target = ; |
208 | |
209 | |
210 | |
211 | let inner: *const = unsafe ; |
212 | |
213 | let backing = unsafe ; |
214 | |
215 | unsafe |
216 | from_raw_parts |
217 | backing.slice as *const T, |
218 | usize try_from .unwrap_unchecked, |
219 | |
220 | |
221 | |
222 | |
223 | |
224 | |
225 | |
226 | |
227 | type Target = T; |
228 | |
229 | |
230 | |
231 | let inner: *const T = unsafe ; |
232 | |
233 | match unsafe |
234 | Some => val, |
235 | _ => unreachable!, |
236 | |
237 | |
238 | |
239 | |
240 | |
241 | pub unsafe |
242 | todo! |
243 | |
244 | |
245 | pub unsafe *const T |
246 | self.inner as *const T |
247 | |
248 | |
249 | |
250 | |
251 | type Target = T; |
252 | |
253 | |
254 | let inner: *mut T = unsafe ; |
255 | |
256 | unsafe |
257 | |
258 | |
259 | |
260 | |
261 | let inner: *mut T = unsafe ; |
262 | |
263 | unsafe |
264 | |
265 | |
266 | |
267 | |
268 | |
269 | T: Display, |
270 | |
271 | |
272 | unsafe .fmt |
273 | |
274 | |
275 | |
276 | |
277 | |
278 | let value = Box new |
279 | value, |
280 | drop_fn: free, |
281 | ; |
282 | |
283 | FfiValue |
284 | inner: Box into_raw as _, |
285 | _type_marker: PhantomData, |
286 | _abi_marker: PhantomData, |
287 | |
288 | |
289 | |
290 | |
291 | unsafe |
292 | |
293 | |
294 | |
295 | unsafe |
296 | |
297 | |
298 | |
299 | // This all boils down to moving `T` out of the `FfiValue` and dropping the backing |
300 | // storage for said `FfiValue`. Despite the use of unsafe this is exactly how moving |
301 | // a value onto the stack works. |
302 | |
303 | let inner = self.inner as *mut T; |
304 | let mut move_target: = zeroed; |
305 | |
306 | unsafe |
307 | |
308 | let inner_descriptor: *mut = unsafe ; |
309 | |
310 | unsafe ; |
311 | |
312 | unsafe |
313 | |
314 | |
315 | |
316 | |
317 | type Target = T; |
318 | |
319 | |
320 | |
321 | let inner: *const T = unsafe ; |
322 | |
323 | unsafe |
324 | |
325 | |
326 | |
327 | |
328 | |
329 | let inner: *mut T = unsafe ; |
330 | |
331 | unsafe |
332 | |
333 | |
334 | |
335 | |
336 | use ; |
337 | |
338 | use crate::; |
339 | |
340 | |
341 | |
342 | pub(super) value: T, |
343 | pub(super) drop_fn: unsafe extern "C" fn , |
344 | |
345 | |
346 | |
347 | pub(crate) count: u64, |
348 | pub(crate) slice: *const (), |
349 | _marker: , |
350 | |
351 | |
352 | |
353 | /// Creates a new slice backing from a raw slice pointer and a count. |
354 | /// |
355 | /// # SAFETY |
356 | /// |
357 | /// `slice` **must** refer to a valid slice, with a length greater than or equal to the |
358 | /// value provided as `count`. |
359 | |
360 | pub unsafe |
361 | Self |
362 | count, |
363 | slice, |
364 | _marker: PhantomData, |
365 | |
366 | |
367 | |
368 | /// Creates a new slice backing from an [`FfiSlice`]. |
369 | /// |
370 | /// # SAFETY |
371 | /// |
372 | /// The resultant [`SliceBacking`] **must not** outlive the backing [`FfiSlice`]. |
373 | |
374 | pub unsafe |
375 | let heap_backing: *const = unsafe ; |
376 | |
377 | let heap_backing = unsafe ; |
378 | |
379 | Self |
380 | count: heap_backing.count, |
381 | slice: heap_backing.slice, |
382 | _marker: PhantomData, |
383 | |
384 | |
385 | |
386 | |
387 | |
388 | |
389 | use PhantomData; |
390 | |
391 | use crate:: |
392 | , Ffi, FfiSlice, FfiSliceMut, FfiSliceRef, FfiValue, FfiValueMut, | SliceBacking
393 | FfiValueRef, |
394 | ; |
395 | |
396 | |
397 | |
398 | _lifetime: , |
399 | slice: , |
400 | |
401 | |
402 | |
403 | |
404 | |
405 | FfiSliceRef |
406 | inner: &self.slice as *const _ as *const , |
407 | _type_marker: PhantomData, |
408 | _abi_marker: PhantomData, |
409 | |
410 | |
411 | |
412 | |
413 | |
414 | FfiSliceMut |
415 | inner: &mut self.slice as *mut _ as *mut , |
416 | _type_marker: PhantomData, |
417 | _abi_marker: PhantomData, |
418 | |
419 | |
420 | |
421 | |
422 | |
423 | /// Creates a stack pinned slice guard from a borrowed slice. |
424 | /// |
425 | /// # SAFETY |
426 | /// This function itself isn't "unsafe" but other code will become unsafe if the `slice` |
427 | /// becomes invalid or moves. You'd have to violate safety rules somewhere else to do that, |
428 | /// though. |
429 | |
430 | pub unsafe |
431 | Self |
432 | _lifetime: PhantomData, |
433 | slice: from_raw |
434 | u64 try_from .unwrap, |
435 | slice.as_ptr as *const , |
436 | , |
437 | |
438 | |
439 | |
440 | |
441 | |
442 | value_ref: &'v T, |
443 | |
444 | |
445 | |
446 | /// Grants a reference to the pinned value. |
447 | /// |
448 | /// # SAFETY |
449 | /// - The granted reference **must not** outlive the lifetime of `&self`. |
450 | /// - There **must not** be a mutable reference created or mutable dereference performed during the lifetime of the [`FfiValueRef`]. |
451 | |
452 | pub unsafe |
453 | Ffi |
454 | inner: self.value_ref as *const _ as *const , |
455 | _type_marker: PhantomData, |
456 | _abi_marker: PhantomData, |
457 | |
458 | |
459 | |
460 | /// Grants a reference to the pinned value. |
461 | /// |
462 | /// # SAFETY |
463 | /// - The granted reference **must not** outlive the lifetime of `&self`. |
464 | /// - There **must not** be a mutable reference created or mutable dereference performed during the lifetime of the [`FfiValueRef`]. |
465 | |
466 | pub unsafe |
467 | Ffi |
468 | inner: self.value_ref as *const _ as *const , |
469 | _type_marker: PhantomData, |
470 | _abi_marker: PhantomData, |
471 | |
472 | |
473 | |
474 | |
475 | |
476 | |
477 | pub |
478 | Self |
479 | |
480 | |
481 | |
482 | |
483 | _lifetime: , |
484 | slice: , |
485 | |
486 | |
487 | |
488 | /// Creates a pin guard from a heap placed slice. |
489 | /// |
490 | /// # SAFETY |
491 | /// The `slice` **must not** be moved and **must not** have a mutable reference given during the lifetime |
492 | /// of the returned [`HeapPinnedSlice`] guard. |
493 | |
494 | pub unsafe |
495 | Self |
496 | _lifetime: PhantomData, |
497 | slice: from_heap, |
498 | |
499 | |
500 | |
501 | pub unsafe |
502 | FfiSliceRef |
503 | inner: &self.slice as *const _ as *const , |
504 | _type_marker: PhantomData, |
505 | _abi_marker: PhantomData, |
506 | |
507 | |
508 | |
509 | pub unsafe |
510 | FfiSliceMut |
511 | inner: &mut self.slice as *mut _ as *mut , |
512 | _type_marker: PhantomData, |
513 | _abi_marker: PhantomData, |
514 | |
515 | |
516 | |
517 | |
518 | |
519 | |
520 | value: &'v , |
521 | |
522 | |
523 | |
524 | |
525 | pub unsafe |
526 | Self |
527 | |
528 | |
529 | |
530 | pub unsafe |
531 | FfiValueRef |
532 | inner: self.value.inner, |
533 | _type_marker: PhantomData, |
534 | _abi_marker: PhantomData, |
535 | |
536 | |
537 | |
538 | |
539 | pub unsafe |
540 | FfiValueMut |
541 | inner: self.value.inner, |
542 | _type_marker: PhantomData, |
543 | _abi_marker: PhantomData, |
544 | |
545 | |
546 | |
547 | |
548 | |
549 | |
550 | ; |
551 | |
552 | ; |
553 | |
554 | ; |
555 | |
556 | ; |
557 | |
558 | ; |
559 | |
560 | ; |
561 | |
562 | |
563 | |
564 | type Pinned: ?Sized + 'p; |
565 | |
566 | ; |
567 | |
568 | |
569 | |
570 | type Pinned = ; |
571 | |
572 | |
573 | |
574 | unsafe |
575 | |
576 | |
577 | |
578 | |
579 | type Pinned = ; |
580 | |
581 | |
582 | |
583 | from_raw |
584 | |
585 | |
586 |