diff --git a/AGENTS.md b/AGENTS.md index dba54de50..95303c416 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -5,7 +5,7 @@ Each project lives in its own top-level directory; the [quest tree](quest/README - An unsupported construct or malformed input is a compile error with a source span, never a warning, a silent fallback, or a runtime failure. Generated code panics only where the Rust source does. One exception is an `unsafe` pointer from elsewhere whose origin translation can't see, such as a parameter's: a loud run-time check may stand in for the refusal, under m3's [unsafe policy](quest/m3/README.md), since refusing every such pointer would refuse pointer arithmetic on any parameter. - The other is a binding, which checks at run time only the Rust rules its language cannot express: borrow exclusivity, use after a move or close, and the thread of a value that is not `Send`. A failed check raises the language's own error, or aborts where it has none; a Rust panic aborts. + The other is a binding, which checks at run time only the Rust rules its language cannot express: borrow exclusivity, use after a move or close, the thread of a value that is not `Send`, and a call of an item the library's target lacks. A failed check raises the language's own error, or aborts where it has none; a Rust panic aborts. - Generated output must match the source's semantics exactly: integer widths and overflow, drop order, laziness, and error paths. Prove it with differential tests against native Rust, not by inspection. - A binding's types are lossless: every value of the Rust type crosses and returns exactly. A native type stands in only where it is exact; otherwise the runtime's own type crosses, with checked conversions to and from the idiomatic one. Speed ranks above ergonomics. - Everything is configless, with opinionated names and defaults. Translators take stock crates as they are: no annotations, and no config beyond true platform boundaries. Each config key or source change a consumer needs is a gap to close. diff --git a/bench/ffi/README.md b/bench/ffi/README.md index 7a0fce907..f450b15c0 100644 --- a/bench/ffi/README.md +++ b/bench/ffi/README.md @@ -396,23 +396,60 @@ frame path: 7. **The binding's own call.** Over C's getter: Swift adds 8 ns (its hold count's two atomics, since dropped from all but its classes, as below), Kotlin 14 wall (JNI and its hold count), Dart 19 (an `_Arena` per call and its hold), Go 31 (cgo, `defer`, and its hold), and Python 37 (argument parsing and the - GIL released and taken back, so Rust may call Python from another thread). BoltFFI's + GIL released and taken back, so Rust may call Python from another thread); Python's and + Dart's hold counts are since dropped for a checked type, as below. BoltFFI's Python getter, which holds the GIL, measured 63 ns. ### Per-handle baseline -For [checks-raise](/quest/m1/ffi-export/checks-raise.md)'s one accounting: the u64 -getter is one borrowed handle and nothing else, so its row less Rust's 0.4 ns is what a -handle costs a call. C, C++: 10 ns, the runtime's two atomics. Swift: 9.9, with no hold -count since the change below. Kotlin: 24 wall, Go 38, Dart 28, Python 49, each with its call's own -transition besides. - -Swift has since made each object a noncopyable struct and dropped its hold count, but for -a class: one thrown as an error. A `Copy` type is a value since [Copy values](#copy-values). Measured 2026-11-11 at that change, 5 -rounds interleaved with C at a load average of 7 to 12, its getter costs 10 ns, as C's -does, and `track.is_closed()`, on `TrackProducer`, still a class since moq throws it, 26 -against C's 18, as in the table. A change to the accounting moves this row and the handle call's by the -same amount; measure both, CPU first, at the load beside them. +For [#730](https://github.com/kixel-ai/rust2/pull/730)'s one accounting: the u64 getter +is one borrowed handle and nothing else, so its row less Rust's 0.4 ns is what a handle +costs a call. Measured 2026-11-13 on desktop at a load average of 2.4 to 4.1, main at +`7b0958b3` against #730 before Go and Kotlin took their counts back, run alternately: 12 +rounds of main and 6 of #730, median wall ns (CPU ns where it differs). #730 drops the hold +counts Python and Dart kept beside the runtime's for a checked type, so its call borrows +through the runtime's two atomics alone, and close drops the value through Rust. + +| row | C | Python | Swift | Dart | +|---|---|---|---|---| +| handle call, main | 18 | 54 | 26 | 41 (45) | +| handle call, #730 | 18 | 53 | 26 | 42 (45) | +| u64 getter, main | 10 | 48 | 10 | 27 | +| u64 getter, #730 | 10 | 47 | 10 | 28 | +| object return, main | 29 | 99 | 37 | 238 | +| object return, #730 | 30 | 98 | 38 | 214 | +| stamped write, main | 159 | 337 | 205 | 433 | +| stamped write, #730 | 168 | 332 | 212 | 450 | + +Go and Kotlin dropped theirs too at first, and gained 5 to 8 ns on the getter, but a +handle closed or moved out of was then freed only once the GC found its object +unreachable, which kept Go's cleanup and Kotlin's phantom reference registered: Go's +object return went from 183 to 691 ns and its stamped write from 429 to 1,061, Kotlin's +stamped write from 310 to 613. So each still counts its calls, freeing the handle once a +close or a move leaves no call holding it. Measured 2026-11-13 on nixos (AMD, 32 threads) +at a load average of 28 to 47, main at `e9cca797` against #730, 10 runs of each program +alternating, median wall ns: + +| row | Kotlin | Go | +|---|---|---| +| handle call, main | 13 | 35 | +| handle call, #730 | 14 | 49 | +| u64 getter, main | 12 | 34 | +| u64 getter, #730 | 11 | 50 | +| object return, main | 156 | 261 | +| object return, #730 | 66 | 317 | +| stamped write, main | 457 | 745 | +| stamped write, #730 | 441 | 835 | + +Kotlin's rows match main's within the noise: its object return's fastest runs took 57 and +60 ns. Go's checked call allocates its status, +since cgo moves to the heap a local whose address it passes C, which costs 6 ns in a +cgo call alone; its close calls Rust's `_drop`, a second crossing an object return and a +stamped write pay. Dart's checked calls first lost 4 ns to a status allocated per call, +now one cell per isolate, which the glue fills from the call's own once the call stopped +serving the isolate, for 1 ns more. Swift's getter costs C's since it dropped its hold count for +each noncopyable object (2026-11-11), and a `Copy` type is a value since [Copy values](#copy-values). A change to the accounting moves these rows; +measure them at the load beside them. ### Copy values diff --git a/ffi/README.md b/ffi/README.md index 07bc485f8..bbb9a0fb2 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -186,11 +186,16 @@ item that target compiles works exactly, and no other does: - A function, method, or constructor only some targets have is declared everywhere. On another target, the library defines it to fail, and the binding checks the library's target before calling it, raising the language's own error, as for a closed object: - `UnsupportedOperationException` in Kotlin, `NotImplementedError` in Python, a trap in - Swift, a panic in Go, and `UnsupportedError` in Dart. C and C++ abort with the message. + `UnsupportedOperationException` in Kotlin, `NotImplementedError` in Python, a + `CheckError` thrown in Swift, an error matching `errors.ErrUnsupported` returned in Go, + and `UnsupportedError` in Dart. C and C++ abort with the message. A type's std traits + are its methods here, and its `Default` a constructor, except where the language's own + protocol cannot fail: Swift traps in a `description`, `debugDescription`, or `==` that + does not throw, and Go's `String` and `GoString` return the error's text. - So does a variant only some targets have, when a value holding it crosses into a target - without it. A record's fields cross whole, so one whose fields differ between targets - is refused, as below. + without it, except that Swift traps and Go panics, as a value's conversion there returns + no error. A record's fields cross whole, so one whose fields differ between targets is + refused, as below. - A type only some targets have is declared everywhere, and only those targets' items make or take it. - A constant only some targets have keeps its value everywhere, since reading it calls @@ -227,6 +232,9 @@ API breaks the one in `file`, with an error at each item that names how: tag, one added to an enum another crate matches exhaustively, and an enum gaining its first variant with fields or losing its last, which changes it between a C enum and a struct; +- whether a type's handle is checked at run time, which gives or takes a status from each + call taking it, and makes it throw in Swift: borrowing it exclusively or moving it out in + an added method makes it checked; - the library's name, which every C name starts with; - a C name that now names another item: since a name depends on every item exported ([The C ABI](#the-c-abi)), adding `audio::encoder_finish` renames `audio_encoder::finish` @@ -260,13 +268,26 @@ where it was while its file is unchanged. decides are initialized, as an `Option` in it does, is copied out instead, leaving its handle as it was. A value that is not `Sync` is shared on one thread at a time, and held exclusively by a - future, which may be polled on any thread. A borrow Rust would - refuse at compile time aborts, naming the function, as does a null or moved-out handle: - there is no Rust caller to return an error to. A setter borrows as `&mut T` and takes + future, which may be polled on any thread. A borrow Rust would refuse at compile time, + or a moved-out handle, fails the call before any Rust code runs, so it changes nothing, + and frees each buffer the caller handed it, as a call that ran would: + a function whose handles can fail a check takes a trailing `_status *status`, + through which it writes the `_STATUS_*` that failed, returning a zeroed value, + and `_status_message` names it. A call that passes its checks leaves `*status` + as it was, so a caller zeroes it before each call it reads it after. A NULL `status` aborts with the message, naming + the function, as C++ passes; each other binding raises its language's own error, as + below. A null handle aborts. Only a type some export can misuse is checked: one that is + not `Sync`, or that an export borrows as `&mut`, moves out, or lends a closure; a call + borrowing none of those takes no `status`, and neither does `_free`. A checked type also + has `_drop`, which drops the value at once, as a move out would, and leaves the handle to + `_free`: a later call fails with `_STATUS_MOVED`, a second drop does nothing, + and one while a call or future borrows the value fails with + `_STATUS_BORROWED`. A record's or data enum's `_eq`, `_to_string`, and `_debug` + take a `status` too when it holds a checked handle. A setter borrows as `&mut T` and takes its value as `T`, so it writes the value the handle holds, or the one Rust lent it, while a getter's copy is C's own: a nested field is got, set, and set back. A type whose C++ copies share one value has `_borrowed`, true while a call or future borrows - it, so moving it out would abort. + it, so moving it out would fail. - **Opaque values** are `typedef struct { uintN_t opaque[n]; } ;`, the bytes of a `Copy` type with private fields, in units of its alignment up to 8 bytes: C copies one as any struct, and passes `&self` as a `const *` and `&mut self`, a field's setter @@ -289,9 +310,9 @@ where it was while its file is unchanged. assigned what Rust wrote; Kotlin, Python, and Dart take a `Mut` holding the value, whose `value` Rust's write replaces, before any error raises. - **Lent handles** borrow from the handle a call lent them from: each use holds that one - shared and aborts, naming the function, once it was since borrowed exclusively, moved - out, or freed, which Rust's borrow checker would refuse. Borrowing a lent handle - exclusively aborts too, as Rust lent it shared. + shared and fails with `_STATUS_OUTLIVED` once it was since borrowed exclusively, + moved out, or freed, which Rust's borrow checker would refuse. Borrowing a lent handle + exclusively, or moving it out, fails with `_STATUS_LENT`, as Rust lent it. - **Interned strings**: a `&str` or `&[u8]` a function needs for `'static`, as `Path::from_static` and `CertificateDer::from_slice` do, is leaked once per distinct content and reused for every later equal one, so a caller passing ever-new strings grows the set without bound. @@ -310,7 +331,8 @@ where it was while its file is unchanged. one again, and owns what it borrowed: the handle it was called on stays borrowed until it finishes or is freed. `_start` is the call and a first poll with no waker in one: a future ready at once costs one call and no handle, and one that pends writes its - handle through the last parameter, which must not be null. A null waker wakes nothing + handle through its `_future` out pointer, which must not be null: the last parameter, or + the one before a checked call's `status`. A null waker wakes nothing and costs no allocation, and a future is one allocation. - **Wakers** move into Rust, which calls `drop` once it lets go of the last clone, so a state machine polled with a waker, such as moq's `Driver`s through kio's `Waiter`, runs @@ -419,6 +441,8 @@ packages place it in. run-time check, since Clang does not track a returned handle class. No export keeps a view or reference past its call, so none carries `[[clang::lifetime_capture_by]]`; the first that does adds it. +- **Checks**: a call passes no `status`, so a borrow or move Rust's rules refuse aborts + with its message, naming the function, as C's does with a NULL one. - **Sharing** stands in for `clone` where no call can tell the two apart but by counting an `Arc`'s or `Rc`'s clones, which the exporter decides at compile time. A type's copies share when it is `Clone`; `Sync`, since one that is not is borrowed on one thread at a time, which copies on two threads @@ -511,13 +535,21 @@ out, with only its other fields encoded. such as a function's or an exception's, takes a trailing `_` where a type, the runtime, or an earlier name has it. A keyword is quoted in backticks, and a member named as one Kotlin gives its type takes a trailing `_`. A constructor such as `new` stays a companion function: `Counter.new(1)`. -- **Objects** are classes owning their handle, `AutoCloseable`, which `close()` frees once - no call holds it; a call after it throws. One never closed is closed once the GC - collects it, on a daemon thread a phantom reference queue feeds. A `self` method, or passing the object by - value, closes it once the call returns. `Clone` is `clone()`, and `PartialEq` is `equals`. +- **Objects** are classes owning their handle, `AutoCloseable`, which each call holds + until it returns, so the handle is freed only once no call holds it; a call after + `close()` throws. Of a checked type, `close()` drops the value through Rust's own + `_drop`, as Rust's `drop` would, and throws instead while a call borrows the value, as + Rust refuses that drop, leaving the value to a later `close()`. Of any other type, + `close()` frees the handle, and throws while a call holds it. One never closed is freed + once the GC collects it, on a daemon thread a phantom reference queue feeds. A `self` + method, or passing the object by value, moves its value out and closes it once the call + returns. `Clone` is `clone()`, and `PartialEq` is `equals`. A field is a `val`, `firstGet`, and its setter a function, `firstSet(value)`, named as the C ones, since setting an object moves it in and closes its handle, which an assignment would hide. +- **Checks**: a borrow or move Rust's rules refuse, such as a `&mut` borrow of an object + another call borrows, or a move out of one a closure was lent, throws an + `IllegalStateException` naming the function and the rule, before any Rust code runs, so the call changed nothing. - **Records** are data classes, compared by their fields, and **enums** an `enum class`, or for one with data a sealed class of a data class per variant. `Display` is `toString()`, `Debug` `debug()`, and `Default` a companion's `default()`. An **opaque @@ -623,11 +655,14 @@ caller imports it with no ignores. builtin's (`bytes`, `int`): the module reads each builtin through an alias (`_bytes`). Constructors such as `new` stay static methods: `Counter.new(1)`. - **Objects** are classes owning their handle, freed once their last reference goes, - which a call holds until it returns, so there is no `close()`. A `self` method, or - passing the object by value, moves its value out and closes it once the call returns, - after which a call raises `ValueError`. `Clone` is `clone()`, and `PartialEq` is `==`, unhashable. A field is a + which the caller's reference keeps through a call, so there is no `close()`. A `self` + method, or passing the object by value, moves its value out, after which a call raises + `ValueError`. `Clone` is `clone()`, and `PartialEq` is `==`, unhashable. A field is a property, `first_get`, and its setter a method, `first_set(value)`, named as the C ones, since setting an object moves it in and closes it, which an assignment would hide. +- **Checks**: a borrow or move Rust's rules refuse raises, naming the function and the rule, + before any Rust code runs, so the call changed nothing: `ValueError` for a value moved out, or a lent object that outlived the one it + borrows from, as for a closed object, and `RuntimeError` for any other. - **Records** are slotted dataclasses, compared by their fields, a float by IEEE `==` even against itself, as Rust does, and **enums** an `Enum` of upper-case members, or for one with data a class of a dataclass per variant, @@ -726,19 +761,30 @@ and, for `Swift.Duration`, macOS 13 or iOS 16, and builds in either language mod is an overload with and without it, labelled by its name, as a defaulted argument is: `clamped()`, `clamped(low:)`, `clamped(high:)` and `clamped(low:high:)`, up to two per function; `Option<&mut T>` is `inout T?`. `Clone` is `clone()`, and `PartialEq` is `==`, which Swift 6.4 makes - `Equatable`; `Display` is `description` and `Debug` `debugDescription`, which Swift - conforms no noncopyable type to yet. A `Copy` object, which Rust copies where it takes - one by value, and an object thrown as an error, which `Swift.Error` requires be - copyable, are final classes instead, whose handle a call - holds until it returns, and moving its value out closes it, after which a call traps. A + `Equatable` unless it throws, as below; `Display` is `description` and `Debug` + `debugDescription`, which Swift conforms no noncopyable type to yet. A `Copy` object, + which Rust copies where it takes one by value, and an object thrown as an error, which + `Swift.Error` requires be copyable, are final classes instead, whose handle a call + holds until it returns, and moving its value out closes it, after which a call throws. A field is a computed property, `firstGet`, and its setter a function, `firstSet(value)`, named as the C ones, since setting an object moves it in, which an assignment would hide. An object is `Sendable`, since Rust checks each borrow at run time. +- **Checks**: a borrow or move Rust's rules refuse at run time, such as a borrow of a value + that is not `Sync` on a second thread, throws a `CheckError`, holding the function and + the rule, before any Rust code runs, so the call changed nothing. So every function whose objects can fail a check `throws`, and a + field read stays a property whose getter throws, except one of a noncopyable value, + whose getter Swift cannot let throw, which is a method, `firstGet()`. Of a type whose + checks can fail, or a record or enum holding one, `description` and `debugDescription` + are getters that throw, and `clone()` and `==` throw, so it conforms to none of + `CustomStringConvertible`, `CustomDebugStringConvertible`, and `Equatable`, whose + requirements cannot throw. A closed class anywhere in a call's arguments throws too. A + copy a closure returns, which has no caller to throw to, traps instead. - **Records** are structs and **enums** Swift enums, with a variant's data as associated values, all `Sendable`. `PartialEq` is `==`, through Rust's; `Display` is `description`, - `Debug` `debugDescription`, and `Default` a static `default()`. An **opaque value** is - a struct holding its C struct, with no public initializer, so only Rust makes one; a - `&mut self` method is `mutating`, and assigns `self` what Rust wrote back. + `Debug` `debugDescription`, each throwing as an object's does when it holds a checked + one, and `Default` a static `default()`. An **opaque value** is a struct holding its C + struct, with no public initializer, so only Rust makes one; a `&mut self` method is + `mutating`, and assigns `self` what Rust wrote back. - **Bytes** are the runtime's `Bytes`, a `RandomAccessCollection` of `UInt8` whose slices are `Bytes` too. Bytes Rust returns lend its buffer until Swift releases the last `Bytes` or slice reading it, and pass back to Rust as a second owner of it, with no copy, except @@ -830,14 +876,25 @@ reads a go.mod. named as a Go keyword, a predeclared name, or a package the code uses, take a trailing `_` too, as does a variant's or error's name or any other in a package that another there, or the runtime's, takes. -- **Objects** are pointers to structs owning their handle. `Close()` frees it once no call - holds it, and a second `Close()` does nothing; one never closed is closed once the GC - collects it, through `runtime.AddCleanup`. A call that meets a closed object, as its - receiver or anywhere in its arguments, returns an error matching `rt.ErrClosed`, even when - `Close` races it on another goroutine, so every such call returns an `error`; `String()` - and `GoString()`, which return none, return its text. A `self` method, or passing the object by value, closes - it once the call returns. `Clone` is `Clone()`, and a `pub` field a getter, `FirstGet()`, +- **Objects** are pointers to structs owning their handle, each an `io.Closer`, which + each call holds until it returns, so the handle is freed only once no call holds it. Of + a checked type, `Close()` drops the value through Rust's own `_drop`, as Rust's `drop` + would, and returns an error matching `rt.ErrBorrowed` instead while a call on another + goroutine borrows the value, as Rust refuses that drop, leaving the value to a later + `Close()`. Of any other type, `Close()` frees the handle, and returns an error matching + `rt.ErrBorrowed` while a call holds it. A second `Close()` does nothing, and one never + closed is freed once the GC collects it, through `runtime.AddCleanup`. A call that meets + a closed object, as its receiver or anywhere in its arguments, returns an error matching + `rt.ErrClosed`, so every such call returns an `error`; `String()` and `GoString()`, + which return none, return the error's text. A `self` method, or passing the object by + value, moves its value out and closes it once the call returns. `Clone` is `Clone()`, and a `pub` field a getter, `FirstGet()`, and a setter, `FirstSet(v)`. +- **Checks**: a borrow or move Rust's rules refuse returns an error naming the function + and the rule, before any Rust code runs, so the call changed nothing: one matching `rt.ErrClosed` for a value moved out, or a lent + object that outlived the one it borrows from, and `rt.ErrBorrowed` for any other. So + every function whose objects can fail a check returns an `error`. `String()` and + `GoString()` return a failed check's text, having no error to return; what a closure + returns has no caller to return one to, so it aborts with the message. - **Records** are structs, and **plain enums** an `int32` type with a constant per variant (`ShapeSquare`). An enum with data is an interface each variant's struct implements (`EventMoved{V0: p}`), sealed so only the package's do; its methods are the @@ -937,14 +994,19 @@ the library's own URI, mapped to the library by a build hook or by `--native-ass a trailing `_` (`Point.default_()`), and names that clash, such as one the runtime declares or a data enum's variant class, are kept apart as Kotlin's are. The glue's C names are `_dart_`, with a `_` more for each C name of the export already there. -- **Objects** are final classes owning their handle. `close()` frees it once no call - holds it, and a second does nothing; one never closed is freed once the garbage - collector finds it unreachable, through a finalizable handle of Dart's C API. Any call that meets a - closed object, as its receiver or anywhere in its arguments, throws a `StateError` - before Rust sees it, and moves none of the call's other arguments. An isolate cannot - send an object to another, so no `close()` races a call. A `self` method, or passing the - object by value, closes it once the call returns. `Clone` is `clone()`, and a `pub` +- **Objects** are final classes owning their handle. Of a checked type, `close()` drops + the value through Rust's own `_drop`, as Rust's `drop` would: a call after it throws a + `StateError` as for a value moved out, and a `close()` while a call borrows the value, + from a closure that call runs, throws one instead, as Rust refuses that drop. Of any + other type, `close()` frees the handle, and throws while a call holds it. A second + `close()` does nothing, and the handle itself is freed once the garbage collector finds + the object unreachable, through a finalizable handle of Dart's C API. Any call that meets + a closed object, as its receiver or anywhere in its arguments, throws a `StateError` + before Rust sees it. A `self` method, or passing the object by value, moves its value + out. `Clone` is `clone()`, and a `pub` field a getter and a setter. +- **Checks**: a borrow or move Rust's rules refuse throws a `StateError` naming the + function and the rule, before any Rust code runs, so the call changed nothing. - **Records** are final classes with a named, required parameter per field, and positional fields are `$0`, `$1`, and so on. **Plain enums** are Dart enums, and an enum with data a sealed class with a final class per variant (`EventMoved`), so a `switch` diff --git a/ffi/fixtures/api/examples/transcript.rs b/ffi/fixtures/api/examples/transcript.rs index 86c6d001f..be71fe07f 100644 --- a/ffi/fixtures/api/examples/transcript.rs +++ b/ffi/fixtures/api/examples/transcript.rs @@ -381,6 +381,19 @@ fn main() { weighted.total(), weighted.score.count() + step.count() ); + // A checked mark beside bytes in a tuple and in a record lent, and bytes beside a move. + let mut mark = api::Mark::of(1); + mark.add(2); + let parcel = api::Parcel { + mark, + payload: Bytes::from_static(b"abc"), + }; + let moved = (Counter::new("w", 1), Bytes::from_static(b"abcd")); + println!("heft {}", api::heft((mark, Bytes::from_static(b"ab")), &parcel, moved)); + // A value's `&mut self` beside a checked handle. + let mut noted = api::Score::of(2); + let before = noted.note(&api::Mark::of(5)); + println!("noted {before} {}", noted.count()); println!("kept {}", api::emit(Print, vec![1, 2, 3, 4])); let logger = Logger::new(Box::new(Print)); @@ -907,6 +920,10 @@ fn main() { let given = api::reissue(api::Ticket::new(7), |t| api::Ticket::new(t.number() + 1)).number(); let held = api::reissue(api::Ticket::new(7), |_| kept).number(); println!("reissued {given} {held}, cloned {}", api::Ticket::clones()); + // A record's traits borrow the checked handle it holds. + let nines = api::Mark::of(9); + let (front, back) = (api::seat(nines, 1), api::seat(nines, 2)); + println!("seat {front:?} {} {}", front == front.clone(), front == back); // A kept closure, called on another thread after the call that took it returned. println!("spawned {}", api::Spawned::new(|| 7).join()); // A kept closure: called again at the instant it asks for, then once its waiter wakes. diff --git a/ffi/fixtures/api/src/lib.rs b/ffi/fixtures/api/src/lib.rs index 44e0012b2..e4d56cc8d 100644 --- a/ffi/fixtures/api/src/lib.rs +++ b/ffi/fixtures/api/src/lib.rs @@ -945,6 +945,84 @@ pub fn reissue_lent(ticket: &Ticket, swap: impl Fn(&Ticket) -> Ticket) -> Ticket swap(ticket) } +/// A record holding a `Copy` handle, so its `PartialEq` and `Debug` check that handle. +#[derive(Clone, Debug, PartialEq)] +pub struct Seat { + pub mark: Mark, + pub row: u32, +} + +/// The seat in `row` for `mark`. +pub fn seat(mark: Mark, row: u32) -> Seat { + Seat { mark, row } +} + +impl Seat { + /// Its mark's count: a record's method whose receiver holds a checked object. + pub fn count(&self) -> u64 { + self.mark.count + } +} + +/// The counter's value plus the count of each mark beside it: a counter moved in beside +/// copied checked objects, alone and held in a record and an enum. +pub fn admit(entry: (Counter, Mark, Seat, Choice)) -> u64 { + let (counter, mark, seat, choice) = entry; + counter + .value + .wrapping_add(mark.count) + .wrapping_add(seat.mark.count) + .wrapping_add(choose(choice)) +} + +/// A mark picked, or none: a data enum with an empty variant beside a checked object. +pub enum Choice { + Empty, + Picked(Mark), +} + +/// The count of the mark picked, or 0. +pub fn choose(choice: Choice) -> u64 { + match choice { + Choice::Empty => 0, + Choice::Picked(mark) => mark.count, + } +} + +/// A mark and a payload: a record holding a checked handle beside a buffer. +pub struct Parcel { + pub mark: Mark, + pub payload: Bytes, +} + +/// Each count and each payload's length, summed: a checked mark beside bytes in a tuple +/// and in a record lent, and bytes beside a moved counter, so a call a failed check +/// refuses frees every buffer it was given. +pub fn heft(marked: (Mark, Bytes), lent: &Parcel, moved: (Counter, Bytes)) -> u64 { + let (mark, bytes) = marked; + let (counter, more) = moved; + let lengths = bytes.len() + lent.payload.len() + more.len(); + mark.count + lent.mark.count + counter.value + lengths as u64 +} + +/// Both counters' values, or an error once they overflow: moved parameters named as what +/// a binding's own cleanup might bind, an exception `e` and a lambda's `it`. +pub fn spend(e: Counter, it: Counter) -> Result { + e.value.checked_add(it.value).ok_or_else(|| "overflow".to_string()) +} + +/// An enum named as the runtime's status, whose constants C would name as its statuses. +#[derive(Clone, Copy, Debug, PartialEq)] +pub enum Status { + Moved, + Kept, +} + +/// Whether `status` is `Kept`. +pub fn is_kept(status: Status) -> bool { + status == Status::Kept +} + /// What `check` returns, a closure with no value but its error. pub fn attempt(check: impl FnOnce() -> Result<(), String>) -> Result<(), String> { check() @@ -1314,6 +1392,14 @@ impl Score { std::mem::take(&mut self.count) } + /// Adds `mark`'s count to this one, returning the count before: a value's `&mut self` + /// beside a checked handle, so a call a failed check refuses leaves the value as it was. + pub fn note(&mut self, mark: &Mark) -> u64 { + let before = self.count; + self.count = self.count.wrapping_add(mark.count); + before + } + /// Moves `other`'s count into this one, as `&mut` of a value Rust writes back too, and /// returns both counts before, as a tuple. pub fn absorb(&mut self, other: &mut Score) -> (u64, u64) { @@ -1323,6 +1409,31 @@ impl Score { } } +/// A count beside a slot whose `None` leaves its bytes uninitialized, so a `Copy` handle +/// rather than a value, which `&mut self` makes checked. A call taking one by value copies +/// it out of its handle, leaving the caller's as it was. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct Mark { + count: u64, + slot: Option, +} + +impl Mark { + pub fn of(count: u64) -> Self { + Self { count, slot: None } + } + + pub fn count(&self) -> u64 { + self.count + } + + /// Adds `by` to the count, in place. + pub fn add(&mut self, by: u64) { + self.count = self.count.wrapping_add(by); + self.slot = Some(0); + } +} + /// A tally, as moq's `stats::Presence`: `Copy`, its fields `pub` but `#[non_exhaustive]`, /// so a value whose fields read and write through accessors, each a `u64`, so it has no /// padding. @@ -2649,6 +2760,12 @@ pub fn nudge(point: &mut Point, by: i32) { point.y += by; } +/// Nudge `point` by the length of `refusal`'s reason: a value Rust writes back beside an +/// object a check of can fail. +pub fn nudge_by(point: &mut Point, refusal: &Refusal) { + nudge(point, i32::try_from(refusal.reason.len()).unwrap_or(i32::MAX)); +} + /// A config of a crate the facade re-exports nothing from, behind an `Arc`, as moq-tokio's /// listener returns rustls's: an opaque handle, which Rust takes back. pub fn secure(alpn: &str) -> Arc { @@ -3326,6 +3443,18 @@ pub fn reasons(pairs: Vec<(Counter, Refusal)>, last: Option<(Counter, Refusal)>) .join(", ") } +/// The length of the note, the bytes, and the reason: bytes beside a class Swift closes +/// once a move took it, so a call refused before Rust runs frees the bytes it gave Rust. +pub fn excuse(note: Bytes, entry: (Bytes, Refusal)) -> usize { + note.len() + entry.0.len() + entry.1.reason.len() +} + +/// How many of `refusals` `log` keeps, each logged at level 0: a callback the binding gives +/// before a list whose classes a move may have closed. +pub fn relay(log: Box, refusals: Vec) -> u32 { + refusals.iter().filter(|r| log.log(0, &r.to_string())).count() as u32 +} + /// Always refuses, for `reason`. pub fn refuse(reason: &str) -> Result> { Err(Box::new(Refusal { @@ -3416,12 +3545,26 @@ impl Worker { } } -/// A record only wasm32 has. +/// A record only wasm32 has, whose std traits fail on another target as its functions do. #[cfg(target_arch = "wasm32")] +#[derive(Debug, Default, PartialEq)] pub struct Page { pub size: u32, } +/// A record only wasm32 has, holding a checked object, so its std traits take a status. +#[cfg(target_arch = "wasm32")] +#[derive(Debug, PartialEq)] +pub struct Booth { + pub mark: Mark, +} + +/// The count of `booth`'s mark. +#[cfg(target_arch = "wasm32")] +pub fn booth_count(booth: Booth) -> u64 { + booth.mark.count +} + /// wasm32's page. #[cfg(target_arch = "wasm32")] pub fn page() -> Page { diff --git a/ffi/fixtures/lock/expected.txt b/ffi/fixtures/lock/expected.txt index 5ac7bfb49..b356f8fc0 100644 --- a/ffi/fixtures/lock/expected.txt +++ b/ffi/fixtures/lock/expected.txt @@ -11,6 +11,7 @@ fixtures/lock/src/lib.rs:231:1: rust2-ffi: the API breaks the baseline at struct fixtures/lock/src/lib.rs:30:1: rust2-ffi: the API breaks the baseline at fn `lock::removed`: it is removed fixtures/lock/src/lib.rs:356:1: rust2-ffi: the API breaks the baseline at enum `lock::Speed`: variant `Fast` is removed; variant `Quick` is added to an exhaustive enum fixtures/lock/src/lib.rs:36:1: rust2-ffi: the API breaks the baseline at fn `lock::portable`: it is no longer on wasm32-unknown-unknown +fixtures/lock/src/lib.rs:379:1: rust2-ffi: the API breaks the baseline at type `lock::Gauge`: its handle is now checked at run time, so each call taking it gains a status, and throws in Swift fixtures/lock/src/lib.rs:46:1: rust2-ffi: the API breaks the baseline at fn `lock::scale`: its parameter `x`'s type; its return type fixtures/lock/src/lib.rs:66:2: rust2-ffi: the API breaks the baseline at fn `lock::Meter::add`: its parameter `n`'s type fixtures/lock/src/lib.rs:7:2: rust2-ffi: the API breaks the baseline at fn `lock::audio_encoder::finish`: its C name `lock_audio_encoder_finish` is now `lock_audio_encoder_finish_`, and `lock_audio_encoder_finish` names fn `lock::audio::encoder_finish` diff --git a/ffi/fixtures/lock/src/lib.rs b/ffi/fixtures/lock/src/lib.rs index ceeca900d..bd083a004 100644 --- a/ffi/fixtures/lock/src/lib.rs +++ b/ffi/fixtures/lock/src/lib.rs @@ -373,3 +373,30 @@ pub fn speed(speed: Speed) -> u32 { pub fn failing_meter() -> Result<(), Meter> { Err(Meter::new()) } + +/// A handle `next` borrows exclusively, which checks each call taking it at run time: each +/// gains a status, and throws in Swift. +pub struct Gauge { + level: u32, +} + +impl Gauge { + pub fn new() -> Self { + Self { level: 0 } + } + + pub fn level(&self) -> u32 { + self.level + } + + #[cfg(feature = "next")] + pub fn raise(&mut self) { + self.level += 1; + } +} + +impl Default for Gauge { + fn default() -> Self { + Self::new() + } +} diff --git a/ffi/moq/bench/export.c b/ffi/moq/bench/export.c index 6e7ad7bfd..3981f6e1c 100644 --- a/ffi/moq/bench/export.c +++ b/ffi/moq/bench/export.c @@ -77,7 +77,7 @@ static const moq_waker waker = {0}; static volatile unsigned sink; static moq_group_Consumer *recv_group(moq_track_Subscriber *subscriber) { - moq_track_Subscriber_recv_group_future *receiving = moq_track_Subscriber_recv_group(subscriber); + moq_track_Subscriber_recv_group_future *receiving = moq_track_Subscriber_recv_group(subscriber, NULL); moq_group_Consumer *reader = NULL; moq_Error *err = NULL; if (moq_track_Subscriber_recv_group_poll(receiving, waker, &reader, &err) != MOQ_POLL_READY || reader == NULL) { @@ -92,7 +92,7 @@ static moq_frame_Frame *read_frame(moq_group_Consumer *reader) { moq_group_Consumer_read_frame_future *reading = NULL; moq_frame_Frame *read = NULL; moq_Error *err = NULL; - if (moq_group_Consumer_read_frame_start(reader, &read, &err, &reading) != MOQ_POLL_READY || read == NULL) { + if (moq_group_Consumer_read_frame_start(reader, &read, &err, &reading, NULL) != MOQ_POLL_READY || read == NULL) { fail("read_frame"); } return read; @@ -101,7 +101,7 @@ static moq_frame_Frame *read_frame(moq_group_Consumer *reader) { static moq_group_Producer *append_group(moq_track_Producer *track) { moq_group_Producer *group = NULL; moq_Error *err = NULL; - if (!moq_track_Producer_append_group(track, &group, &err)) { + if (!moq_track_Producer_append_group(track, &group, &err, NULL)) { fail("append_group"); } return group; @@ -109,14 +109,14 @@ static moq_group_Producer *append_group(moq_track_Producer *track) { static void write_frame(moq_group_Producer *group, moq_Timestamp at, moq_buf frame) { moq_Error *err = NULL; - if (!moq_group_Producer_write_frame(group, (moq_option_Timestamp){.some = true, .value = at}, frame, &err)) { + if (!moq_group_Producer_write_frame(group, (moq_option_Timestamp){.some = true, .value = at}, frame, &err, NULL)) { fail("write_frame"); } } static void finish(moq_group_Producer *group) { moq_Error *err = NULL; - if (!moq_group_Producer_finish(group, &err)) { + if (!moq_group_Producer_finish(group, &err, NULL)) { fail("finish"); } moq_group_Producer_free(group); @@ -138,7 +138,7 @@ static span throughput(moq_track_Producer *track, moq_track_Subscriber *subscrib for (int f = 0; f < FRAMES; f++) { write_frame(group, at, frame); moq_frame_Frame *read = read_frame(reader); - moq_buf bytes = moq_frame_Frame_payload_get(read); + moq_buf bytes = moq_frame_Frame_payload_get(read, NULL); *sum += bytes.ptr[0]; moq_buf_free(bytes); moq_frame_Frame_free(read); @@ -174,11 +174,11 @@ static void *work(void *arg) { } if (w->getter) { for (int i = 0; i < N; i++) { - sum += (unsigned)moq_group_Demand_sequence(w->handle); + sum += (unsigned)moq_group_Demand_sequence(w->handle, NULL); } } else { for (int i = 0; i < N; i++) { - sum += moq_track_Producer_is_closed(w->handle); + sum += moq_track_Producer_is_closed(w->handle, NULL); } } w->sum = sum; @@ -211,16 +211,16 @@ static span concurrent(const void *const *handles, int getter, unsigned *sum) { static void round_(int print) { moq_broadcast_Info *binfo = moq_broadcast_Info_new(); - moq_broadcast_Producer *broadcast = moq_broadcast_Info_produce(binfo); + moq_broadcast_Producer *broadcast = moq_broadcast_Info_produce(binfo, NULL); moq_broadcast_Info_free(binfo); moq_option_Timescale micro = {.some = true, .value = moq_Timescale_MICRO()}; /* Builders take the handle's value, leaving the handle to free. */ moq_track_Info *defaults = moq_track_Info_default(); - moq_track_Info *info = moq_track_Info_with_timescale(defaults, micro); + moq_track_Info *info = moq_track_Info_with_timescale(defaults, micro, NULL); moq_track_Info_free(defaults); moq_track_Producer *track = NULL; moq_Error *err = NULL; - if (!moq_broadcast_Producer_create_track(broadcast, str("bench"), info, &track, &err)) { + if (!moq_broadcast_Producer_create_track(broadcast, str("bench"), info, &track, &err, NULL)) { fail("create_track"); } moq_track_Info_free(info); @@ -232,15 +232,15 @@ static void round_(int print) { start = begin(); for (int i = 0; i < N; i++) { - sum += moq_track_Producer_is_closed(track); + sum += moq_track_Producer_is_closed(track, NULL); } span call = end(start, N, "call"); moq_group_Producer *demanded = append_group(track); - moq_group_Demand *demand = moq_group_Producer_demand(demanded); + moq_group_Demand *demand = moq_group_Producer_demand(demanded, NULL); start = begin(); for (int i = 0; i < N; i++) { - sum += (unsigned)moq_group_Demand_sequence(demand); + sum += (unsigned)moq_group_Demand_sequence(demand, NULL); } span getter = end(start, N, "getter"); @@ -253,8 +253,8 @@ static void round_(int print) { for (int i = 0; i < THREADS; i++) { shared_track[i] = track; shared_demand[i] = demand; - own_track[i] = moq_track_Producer_clone(track); - own_demand[i] = moq_group_Demand_clone(demand); + own_track[i] = moq_track_Producer_clone(track, NULL); + own_demand[i] = moq_group_Demand_clone(demand, NULL); } shared_call = concurrent(shared_track, 0, &sum); own_call = concurrent(own_track, 0, &sum); @@ -270,13 +270,13 @@ static void round_(int print) { start = begin(); for (int i = 0; i < N; i++) { - sum += moq_track_Producer_largest(track).some; + sum += moq_track_Producer_largest(track, NULL).some; } span record = end(start, N, "record"); start = begin(); for (int i = 0; i < N; i++) { - moq_track_Info_free(moq_track_Producer_info(track)); + moq_track_Info_free(moq_track_Producer_info(track, NULL)); } span object = end(start, N, "object"); @@ -312,8 +312,8 @@ static void round_(int print) { } span write = end(start, N, "write"); - moq_track_Consumer *consumer = moq_track_Producer_consume(track); - moq_track_Consumer_subscribe_future *subscribing = moq_track_Consumer_subscribe(consumer, NULL); + moq_track_Consumer *consumer = moq_track_Producer_consume(track, NULL); + moq_track_Consumer_subscribe_future *subscribing = moq_track_Consumer_subscribe(consumer, NULL, NULL); moq_track_Subscriber *subscriber = NULL; if (moq_track_Consumer_subscribe_poll(subscribing, waker, &subscriber, &err) != MOQ_POLL_READY) { fail("subscribe"); @@ -345,7 +345,7 @@ static void round_(int print) { moq_group_Producer *group = append_group(track); moq_group_Consumer *reader = recv_group(subscriber); for (int f = 0; f < FRAMES; f++) { - moq_group_Consumer_read_frame_future *reading = moq_group_Consumer_read_frame(reader); + moq_group_Consumer_read_frame_future *reading = moq_group_Consumer_read_frame(reader, NULL); moq_frame_Frame *read = NULL; if (moq_group_Consumer_read_frame_poll(reading, waker, &read, &err) != MOQ_POLL_PENDING) { fail("woken read_frame ready"); @@ -365,7 +365,7 @@ static void round_(int print) { /* The last frame's 100 bytes, owned without a copy, then freed. */ start = begin(); for (int i = 0; i < N; i++) { - moq_buf bytes = moq_frame_Frame_payload_get(last); + moq_buf bytes = moq_frame_Frame_payload_get(last, NULL); sum += bytes.ptr[0]; moq_buf_free(bytes); } diff --git a/ffi/moq/bench/export.go b/ffi/moq/bench/export.go index fc0bf0b44..ed15a1f44 100644 --- a/ffi/moq/bench/export.go +++ b/ffi/moq/bench/export.go @@ -11,6 +11,7 @@ import "C" import ( "context" "fmt" + "io" "runtime" "sync" "syscall" @@ -206,7 +207,7 @@ func round(measured bool) []result { return func() int { return int(must(d.Sequence())) } } var sharedCalls, ownCalls, sharedGetters, ownGetters []func() int - var clones []interface{ Close() } + var clones []io.Closer for range threads { t := must(track.Clone()) d := must(demand.Clone()) diff --git a/ffi/moq/bench/export.swift b/ffi/moq/bench/export.swift index 4cedd5cf4..0a8173bdf 100644 --- a/ffi/moq/bench/export.swift +++ b/ffi/moq/bench/export.swift @@ -172,22 +172,22 @@ func throughput(_ track: TrackProducer, _ subscriber: inout TrackSubscriber, _ a func round(_ measured: Bool) throws -> ([(String, Span)], Int) { var out: [(String, Span)] = [] - let broadcast = BroadcastInfo.new().produce() + let broadcast = try BroadcastInfo.new().produce() let track = try broadcast.createTrack("bench", TrackInfo.default().withTimescale(Timescale.MICRO)) let frame = Bytes(Data(count: 100)) var sum = 0 var start = begin() - for _ in 0.. ([(String, Span)], Int) { // clone each, a handle of its own to the same value. CodSpeed's simulation runs one thread // at a time, which contends for nothing, so it skips them. if !measured { - let isClosed = { (t: TrackProducer) -> @Sendable () -> Int in { t.isClosed() ? 1 : 0 } } - let sequence = { (d: Shared) -> @Sendable () -> Int in { Int(truncatingIfNeeded: d.value.sequence()) } } - let shared = Shared(demand.clone()) + let isClosed = { (t: TrackProducer) -> @Sendable () -> Int in { try! t.isClosed() ? 1 : 0 } } + let sequence = { (d: Shared) -> @Sendable () -> Int in { Int(truncatingIfNeeded: try! d.value.sequence()) } } + let shared = Shared(try demand.clone()) out.append(("shared:call", concurrent((0.. ([(String, Span)], Int) { // The last frame's 100 bytes, returned as Bytes, and one of them read. start = begin() for _ in 0..json, &hang_err)); - moq_mux_catalog_hang_Extra *sections = moq_hang_Catalog_Extra_ext_get(catalog); - moq_Json *app = moq_mux_catalog_hang_Extra_get(sections, str("app")); + CHECK(moq_hang_Catalog_Extra_to_json(catalog, &back->json, &hang_err, NULL)); + moq_mux_catalog_hang_Extra *sections = moq_hang_Catalog_Extra_ext_get(catalog, NULL); + moq_Json *app = moq_mux_catalog_hang_Extra_get(sections, str("app"), NULL); if (app != NULL) { - back->app = moq_Json_to_text(app); + back->app = moq_Json_to_text(app, NULL); moq_Json_free(app); } moq_mux_catalog_hang_Extra_free(sections); @@ -167,48 +167,48 @@ int main(void) { CHECK(moq_contract() == MOQ_CONTRACT); moq_broadcast_Info *info = moq_broadcast_Info_new(); - moq_broadcast_Producer *broadcast = moq_broadcast_Info_produce(info); + moq_broadcast_Producer *broadcast = moq_broadcast_Info_produce(info, NULL); moq_broadcast_Info_free(info); moq_track_Producer *track = NULL; - CHECK(moq_broadcast_Producer_create_track(broadcast, str("video"), NULL, &track, &err)); - CHECK(is(moq_track_Producer_name(track), "video")); + CHECK(moq_broadcast_Producer_create_track(broadcast, str("video"), NULL, &track, &err, NULL)); + CHECK(is(moq_track_Producer_name(track, NULL), "video")); /* A second track of the same name is an error, printed as Rust prints it. */ moq_track_Producer *duplicate = NULL; - CHECK(!moq_broadcast_Producer_create_track(broadcast, str("video"), NULL, &duplicate, &err)); - CHECK(err != NULL && is(moq_Error_to_string(err), "duplicate")); + CHECK(!moq_broadcast_Producer_create_track(broadcast, str("video"), NULL, &duplicate, &err, NULL)); + CHECK(err != NULL && is(moq_Error_to_string(err, NULL), "duplicate")); moq_Error_free(err); - moq_track_Consumer *consumer = moq_track_Producer_consume(track); - moq_track_Consumer_subscribe_future *subscribing = moq_track_Consumer_subscribe(consumer, NULL); + moq_track_Consumer *consumer = moq_track_Producer_consume(track, NULL); + moq_track_Consumer_subscribe_future *subscribing = moq_track_Consumer_subscribe(consumer, NULL, NULL); moq_track_Subscriber *subscriber = NULL; CHECK(moq_track_Consumer_subscribe_poll(subscribing, waker, &subscriber, &err) == MOQ_POLL_READY); moq_track_Consumer_subscribe_future_free(subscribing); /* A group's arrival wakes the subscriber's pending receive. */ - moq_track_Subscriber_recv_group_future *receiving = moq_track_Subscriber_recv_group(subscriber); + moq_track_Subscriber_recv_group_future *receiving = moq_track_Subscriber_recv_group(subscriber, NULL); moq_group_Consumer *group = NULL; CHECK(moq_track_Subscriber_recv_group_poll(receiving, waker, &group, &err) == MOQ_POLL_PENDING); moq_group_Producer *writer = NULL; - CHECK(moq_track_Producer_append_group(track, &writer, &err)); + CHECK(moq_track_Producer_append_group(track, &writer, &err, NULL)); CHECK(wakes == 1); CHECK(moq_track_Subscriber_recv_group_poll(receiving, waker, &group, &err) == MOQ_POLL_READY); moq_track_Subscriber_recv_group_future_free(receiving); - CHECK(group != NULL && moq_group_Consumer_index(group) == 0); + CHECK(group != NULL && moq_group_Consumer_index(group, NULL) == 0); /* So does a frame's. */ - moq_group_Consumer_read_frame_future *reading = moq_group_Consumer_read_frame(group); + moq_group_Consumer_read_frame_future *reading = moq_group_Consumer_read_frame(group, NULL); moq_frame_Frame *frame = NULL; CHECK(moq_group_Consumer_read_frame_poll(reading, waker, &frame, &err) == MOQ_POLL_PENDING); - CHECK(moq_group_Producer_write_frame(writer, (moq_option_Timestamp){0}, str("hello"), &err)); + CHECK(moq_group_Producer_write_frame(writer, (moq_option_Timestamp){0}, str("hello"), &err, NULL)); CHECK(wakes == 2); CHECK(moq_group_Consumer_read_frame_poll(reading, waker, &frame, &err) == MOQ_POLL_READY); moq_group_Consumer_read_frame_future_free(reading); - CHECK(frame != NULL && is(moq_frame_Frame_payload_get(frame), "hello")); - CHECK(!moq_frame_Frame_timestamp_get(frame).some); + CHECK(frame != NULL && is(moq_frame_Frame_payload_get(frame, NULL), "hello")); + CHECK(!moq_frame_Frame_timestamp_get(frame, NULL).some); /* When it arrived: kio's clock's `Instant`, from kio's own export. */ - moq_clock_Instant arrived = moq_frame_Frame_arrived(frame); + moq_clock_Instant arrived = moq_frame_Frame_arrived(frame, NULL); moq_clock_Instant now = moq_clock_now(); CHECK(moq_clock_Instant_checked_duration_since(&now, arrived).some); moq_frame_Frame_free(frame); @@ -219,20 +219,20 @@ int main(void) { moq_TimeOverflow overflow; CHECK(moq_Timestamp_from_millis(1500, &at, &overflow)); CHECK(moq_Timestamp_as_secs(&at) == 1); - CHECK(!moq_group_Producer_write_frame(writer, (moq_option_Timestamp){.some = true, .value = at}, str("late"), &err)); - CHECK(err != NULL && is(moq_Error_to_string(err), "frame timestamp doesn't fit its track")); + CHECK(!moq_group_Producer_write_frame(writer, (moq_option_Timestamp){.some = true, .value = at}, str("late"), &err, NULL)); + CHECK(err != NULL && is(moq_Error_to_string(err, NULL), "frame timestamp doesn't fit its track")); moq_Error_free(err); CHECK(moq_Timestamp_as_secs(&at) == 1); /* The last frame, bytes that are not UTF-8, then the group's end, which reads as None. */ - CHECK(moq_group_Producer_write_frame(writer, (moq_option_Timestamp){0}, str("\xffworld"), &err)); - CHECK(moq_group_Producer_finish(writer, &err)); - reading = moq_group_Consumer_read_frame(group); + CHECK(moq_group_Producer_write_frame(writer, (moq_option_Timestamp){0}, str("\xffworld"), &err, NULL)); + CHECK(moq_group_Producer_finish(writer, &err, NULL)); + reading = moq_group_Consumer_read_frame(group, NULL); CHECK(moq_group_Consumer_read_frame_poll(reading, waker, &frame, &err) == MOQ_POLL_READY); moq_group_Consumer_read_frame_future_free(reading); - CHECK(frame != NULL && is(moq_frame_Frame_payload_get(frame), "\xffworld")); + CHECK(frame != NULL && is(moq_frame_Frame_payload_get(frame, NULL), "\xffworld")); moq_frame_Frame_free(frame); - reading = moq_group_Consumer_read_frame(group); + reading = moq_group_Consumer_read_frame(group, NULL); frame = (moq_frame_Frame *)1; CHECK(moq_group_Consumer_read_frame_poll(reading, waker, &frame, &err) == MOQ_POLL_READY); CHECK(frame == NULL); @@ -250,11 +250,11 @@ int main(void) { moq_origin_Config *config = moq_origin_Config_default(); moq_origin_Producer *origin = NULL; moq_origin_Driver *driver = NULL; - moq_origin_Producer_new(config, &origin, &driver); + moq_origin_Producer_new(config, &origin, &driver, NULL); moq_origin_Config_free(config); CHECK(origin != NULL && driver != NULL); - moq_Path *root = moq_origin_Producer_root(origin); - CHECK(is(moq_Path_as_str(root), "")); + moq_Path *root = moq_origin_Producer_root(origin, NULL); + CHECK(is(moq_Path_as_str(root, NULL), "")); moq_Path_free(root); /* Scoped beneath a root to patterns, which moq-pattern's own export builds. */ @@ -262,56 +262,56 @@ int main(void) { moq_InvalidPattern invalid; CHECK(moq_Pattern_subtree(str("alice"), &alice, &invalid)); moq_Patterns *patterns = moq_Patterns_new(); - CHECK(moq_Patterns_insert(patterns, alice)); + CHECK(moq_Patterns_insert(patterns, alice, NULL)); moq_Pattern_free(alice); moq_origin_Producer *scoped = NULL; - CHECK(moq_origin_Producer_scope(origin, str("room"), patterns, &scoped, &err)); + CHECK(moq_origin_Producer_scope(origin, str("room"), patterns, &scoped, &err, NULL)); moq_Patterns_free(patterns); - root = moq_origin_Producer_root(scoped); - CHECK(is(moq_Path_as_str(root), "room")); + root = moq_origin_Producer_root(scoped, NULL); + CHECK(is(moq_Path_as_str(root, NULL), "room")); moq_Path_free(root); moq_Path *absolute = NULL; - CHECK(moq_origin_Producer_absolute(scoped, str("alice"), &absolute, &err)); - CHECK(is(moq_Path_as_str(absolute), "room/alice")); + CHECK(moq_origin_Producer_absolute(scoped, str("alice"), &absolute, &err, NULL)); + CHECK(is(moq_Path_as_str(absolute, NULL), "room/alice")); moq_Path_free(absolute); - moq_Patterns *allowed = moq_origin_Producer_allowed(scoped); - CHECK(moq_Patterns_matches(allowed, str("alice/cam"))); - CHECK(!moq_Patterns_matches(allowed, str("bob"))); + moq_Patterns *allowed = moq_origin_Producer_allowed(scoped, NULL); + CHECK(moq_Patterns_matches(allowed, str("alice/cam"), NULL)); + CHECK(!moq_Patterns_matches(allowed, str("bob"), NULL)); moq_Patterns_free(allowed); - moq_origin_Consumer *reader = moq_origin_Producer_consume(scoped); - CHECK(moq_origin_Consumer_absolute(reader, str("bob"), &absolute, &err)); - CHECK(is(moq_Path_as_str(absolute), "room/bob")); + moq_origin_Consumer *reader = moq_origin_Producer_consume(scoped, NULL); + CHECK(moq_origin_Consumer_absolute(reader, str("bob"), &absolute, &err, NULL)); + CHECK(is(moq_Path_as_str(absolute, NULL), "room/bob")); moq_Path_free(absolute); moq_origin_Consumer_free(reader); moq_origin_Producer_free(scoped); /* A broadcast the origin creates is stamped with its path, which its info lends. */ moq_origin_Publication *publication = NULL; - CHECK(moq_origin_Producer_create_broadcast(origin, str("room/alice"), &publication, &err)); + CHECK(moq_origin_Producer_create_broadcast(origin, str("room/alice"), &publication, &err, NULL)); moq_broadcast_Producer *created = moq_origin_Publication_broadcast(publication); moq_origin_Publication_free(publication); - moq_broadcast_Info *stamped = moq_broadcast_Producer_info(created); - moq_Path *path = moq_broadcast_Info_path_get(stamped); - CHECK(is(moq_Path_as_str(path), "room/alice")); + moq_broadcast_Info *stamped = moq_broadcast_Producer_info(created, NULL); + moq_Path *path = moq_broadcast_Info_path_get(stamped, NULL); + CHECK(is(moq_Path_as_str(path, NULL), "room/alice")); /* Paths own their segments, so C builds them, and relative ones, from a string. */ moq_Path *cam = NULL; moq_path_Error path_error; CHECK(!moq_Path_new(str("/cam/"), &cam, &path_error) && path_error == MOQ_PATH_ERROR_EMPTY_SEGMENT); CHECK(moq_Path_new(str("cam"), &cam, &path_error)); - CHECK(is(moq_Path_as_str(cam), "cam")); + CHECK(is(moq_Path_as_str(cam, NULL), "cam")); moq_Path *joined = NULL; - CHECK(moq_Path_join(path, cam, &joined, &path_error)); - CHECK(is(moq_Path_as_str(joined), "room/alice/cam")); - moq_Path *stripped = moq_Path_strip_prefix(joined, path); - CHECK(stripped != NULL && moq_Path_eq(stripped, cam)); + CHECK(moq_Path_join(path, cam, &joined, &path_error, NULL)); + CHECK(is(moq_Path_as_str(joined, NULL), "room/alice/cam")); + moq_Path *stripped = moq_Path_strip_prefix(joined, path, NULL); + CHECK(stripped != NULL && moq_Path_eq(stripped, cam, NULL)); moq_Path_free(stripped); - CHECK(moq_Path_strip_prefix(path, joined) == NULL); + CHECK(moq_Path_strip_prefix(path, joined, NULL) == NULL); moq_path_Relative *up = NULL; CHECK(moq_path_Relative_new(str("../bob"), &up, &path_error)); moq_Path *resolved = NULL; - CHECK(moq_Path_resolve(joined, up, &resolved, &path_error)); - CHECK(is(moq_Path_as_str(resolved), "room/bob")); + CHECK(moq_Path_resolve(joined, up, &resolved, &path_error, NULL)); + CHECK(is(moq_Path_as_str(resolved, NULL), "room/bob")); moq_Path_free(resolved); moq_path_Relative_free(up); moq_Path_free(joined); @@ -326,11 +326,11 @@ int main(void) { moq_Waiter *waiter = moq_Waiter_new(waker); now = moq_clock_now(); moq_option_clock_Instant deadline; - CHECK(moq_origin_Driver_poll(driver, now, waiter, &deadline, &err)); + CHECK(moq_origin_Driver_poll(driver, now, waiter, &deadline, &err, NULL)); /* A client publishes the origin through moq-net's generic `with_publisher`, at each * type that implements its bound, by reference, so C keeps its handle. */ moq_Client *client = moq_Client_new(); - moq_Client *publishing = moq_Client_with_publisher_Producer(client, origin); + moq_Client *publishing = moq_Client_with_publisher_Producer(client, origin, NULL); CHECK(publishing != NULL); moq_Client_free(publishing); moq_Client_free(client); @@ -341,8 +341,8 @@ int main(void) { now = moq_clock_now(); deadline = (moq_option_clock_Instant){0}; err = NULL; - CHECK(!moq_origin_Driver_poll(driver, now, waiter, &deadline, &err)); - CHECK(!deadline.some && err != NULL && is(moq_Error_to_string(err), "closed")); + CHECK(!moq_origin_Driver_poll(driver, now, waiter, &deadline, &err, NULL)); + CHECK(!deadline.some && err != NULL && is(moq_Error_to_string(err, NULL), "closed")); moq_Error_free(err); err = NULL; moq_Waiter_free(waiter); @@ -354,12 +354,12 @@ int main(void) { moq_hang_catalog_AudioConfig *audio = NULL; moq_mux_Error *mux_err = NULL; CHECK(moq_mux_codec_opus_config((moq_buf){.ptr = head, .len = sizeof head}, &audio, &mux_err)); - CHECK(moq_hang_catalog_AudioConfig_sample_rate_get(audio) == 48000); - CHECK(moq_hang_catalog_AudioConfig_channel_count_get(audio) == 2); - moq_hang_catalog_AudioCodec codec = moq_hang_catalog_AudioConfig_codec_get(audio); + CHECK(moq_hang_catalog_AudioConfig_sample_rate_get(audio, NULL) == 48000); + CHECK(moq_hang_catalog_AudioConfig_channel_count_get(audio, NULL) == 2); + moq_hang_catalog_AudioCodec codec = moq_hang_catalog_AudioConfig_codec_get(audio, NULL); CHECK(codec.tag == MOQ_HANG_CATALOG_AUDIOCODEC_OPUS); moq_hang_catalog_AudioCodec_free(&codec); - moq_buf description = moq_hang_catalog_AudioConfig_description_get(audio); + moq_buf description = moq_hang_catalog_AudioConfig_description_get(audio, NULL); CHECK(description.len == sizeof head && memcmp(description.ptr, head, sizeof head) == 0); moq_buf_free(description); moq_hang_catalog_AudioConfig_free(audio); @@ -380,9 +380,9 @@ int main(void) { }; moq_track_Subscription *unranged = moq_track_Subscription_default(); moq_track_Subscription *ranged = - moq_track_Subscription_with_ranges(unranged, (moq_vec_track_Range){.ptr = ranges, .len = 2}); + moq_track_Subscription_with_ranges(unranged, (moq_vec_track_Range){.ptr = ranges, .len = 2}, NULL); moq_track_Subscription_free(unranged); - moq_vec_track_Range got = moq_track_Subscription_ranges_get(ranged); + moq_vec_track_Range got = moq_track_Subscription_ranges_get(ranged, NULL); CHECK(got.len == 2 && moq_track_Range_eq(&got.ptr[0], &ranges[0])); CHECK(got.len == 2 && got.ptr[1].start.tag == MOQ_TRACK_ANCHOR_ABSOLUTE && got.ptr[1].start.absolute._0 == 2); CHECK(got.len == 2 && got.ptr[1].end.some && got.ptr[1].end.value.absolute._0 == 6); @@ -395,69 +395,69 @@ int main(void) { * `is_claimed` is named by the last segment of its argument, and whose `mutate` lends * the catalog to C, which sets its fields in place. */ moq_broadcast_Info *listed = moq_broadcast_Info_new(); - moq_broadcast_Producer *publisher = moq_broadcast_Info_produce(listed); + moq_broadcast_Producer *publisher = moq_broadcast_Info_produce(listed, NULL); moq_broadcast_Info_free(listed); moq_mux_catalog_Config *catalog_config = moq_mux_catalog_Config_default(); moq_hang_Catalog_Extra *hang = moq_hang_Catalog_Extra_default(); - moq_mux_catalog_Config_Extra *extra = moq_mux_catalog_Config_with_catalog_Extra(catalog_config, hang); + moq_mux_catalog_Config_Extra *extra = moq_mux_catalog_Config_with_catalog_Extra(catalog_config, hang, NULL); moq_mux_catalog_Config_free(catalog_config); moq_hang_Catalog_Extra_free(hang); moq_mux_catalog_Producer_Extra *catalog = NULL; - CHECK(moq_mux_catalog_Producer_Extra_new(publisher, extra, &catalog, &mux_err)); + CHECK(moq_mux_catalog_Producer_Extra_new(publisher, extra, &catalog, &mux_err, NULL)); moq_mux_catalog_Config_Extra_free(extra); - CHECK(!moq_mux_catalog_Producer_Extra_is_claimed_BinaryConfig(catalog, str("state"))); + CHECK(!moq_mux_catalog_Producer_Extra_is_claimed_BinaryConfig(catalog, str("state"), NULL)); /* A field of the lent catalog, set in place: the next call reads it. */ moq_hang_catalog_Stats *stats = moq_hang_catalog_Stats_new(str("stats")); - CHECK(moq_mux_catalog_Producer_Extra_mutate(catalog, sets_stats, stats, &mux_err)); + CHECK(moq_mux_catalog_Producer_Extra_mutate(catalog, sets_stats, stats, &mux_err, NULL)); moq_hang_catalog_Stats_free(stats); int listed_stats = 0; - CHECK(moq_mux_catalog_Producer_Extra_mutate(catalog, lists_stats, &listed_stats, &mux_err)); + CHECK(moq_mux_catalog_Producer_Extra_mutate(catalog, lists_stats, &listed_stats, &mux_err, NULL)); CHECK(listed_stats == 1); /* A JSON track the catalog lists, published through moq-mux's generic `json_snapshot` * at a JSON document, and a section C parses from text, set in the lent catalog and * read back as text. Its track has a timescale, since every write is stamped. */ moq_track_Info *status_info = - moq_track_Info_with_timescale(moq_track_Info_default(), (moq_option_Timescale){.some = true, .value = moq_Timescale_MILLI()}); + moq_track_Info_with_timescale(moq_track_Info_default(), (moq_option_Timescale){.some = true, .value = moq_Timescale_MILLI()}, NULL); moq_track_Producer *status = NULL; - CHECK(moq_broadcast_Producer_create_track(publisher, str("status"), status_info, &status, &err)); + CHECK(moq_broadcast_Producer_create_track(publisher, str("status"), status_info, &status, &err, NULL)); moq_mux_json_Config *json_config = moq_mux_json_Config_default(); moq_mux_json_Options *json_options = moq_mux_json_Options_default(); moq_mux_json_Snapshot_Json *status_snapshot = NULL; CHECK(moq_mux_catalog_Producer_Extra_json_snapshot_Config(catalog, status, json_config, json_options, &status_snapshot, - &mux_err)); + &mux_err, NULL)); moq_mux_json_Config_free(json_config); moq_mux_json_Options_free(json_options); moq_track_Producer_free(status); - CHECK(is(moq_mux_json_Snapshot_Json_name(status_snapshot), "status")); - CHECK(moq_mux_catalog_Producer_Extra_is_claimed_JsonConfig(catalog, str("status"))); + CHECK(is(moq_mux_json_Snapshot_Json_name(status_snapshot, NULL), "status")); + CHECK(moq_mux_catalog_Producer_Extra_is_claimed_JsonConfig(catalog, str("status"), NULL)); /* A value C parses is published as the track's latest, which a subscriber reads back * as JSON text. */ moq_Json *latest = NULL; moq_buf json_err; CHECK(moq_Json_parse(str("{\"on\": true}"), &latest, &json_err)); - CHECK(moq_mux_json_Snapshot_Json_update(status_snapshot, latest, moq_Timestamp_now(), &mux_err)); + CHECK(moq_mux_json_Snapshot_Json_update(status_snapshot, latest, moq_Timestamp_now(), &mux_err, NULL)); moq_Json_free(latest); - moq_track_Subscriber *status_subscriber = moq_mux_json_Snapshot_Json_consume(status_snapshot); - moq_track_Subscriber_recv_group_future *status_receiving = moq_track_Subscriber_recv_group(status_subscriber); + moq_track_Subscriber *status_subscriber = moq_mux_json_Snapshot_Json_consume(status_snapshot, NULL); + moq_track_Subscriber_recv_group_future *status_receiving = moq_track_Subscriber_recv_group(status_subscriber, NULL); moq_group_Consumer *status_group = NULL; CHECK(moq_track_Subscriber_recv_group_poll(status_receiving, waker, &status_group, &err) == MOQ_POLL_READY); moq_track_Subscriber_recv_group_future_free(status_receiving); moq_frame_Frame *status_frame = NULL; if (status_group != NULL) { - moq_group_Consumer_read_frame_future *status_reading = moq_group_Consumer_read_frame(status_group); + moq_group_Consumer_read_frame_future *status_reading = moq_group_Consumer_read_frame(status_group, NULL); CHECK(moq_group_Consumer_read_frame_poll(status_reading, waker, &status_frame, &err) == MOQ_POLL_READY); moq_group_Consumer_read_frame_future_free(status_reading); moq_group_Consumer_free(status_group); } - CHECK(status_frame != NULL && is(moq_frame_Frame_payload_get(status_frame), "{\"on\":true}")); + CHECK(status_frame != NULL && is(moq_frame_Frame_payload_get(status_frame, NULL), "{\"on\":true}")); moq_frame_Frame_free(status_frame); moq_track_Subscriber_free(status_subscriber); moq_Json *app = NULL; CHECK(moq_Json_parse(str("{\"v\": [1, true]}"), &app, &json_err)); - CHECK(moq_mux_catalog_Producer_Extra_mutate(catalog, sets_app, app, &mux_err)); + CHECK(moq_mux_catalog_Producer_Extra_mutate(catalog, sets_app, app, &mux_err, NULL)); moq_Json_free(app); struct readback back = {0}; - CHECK(moq_mux_catalog_Producer_Extra_mutate(catalog, reads_back, &back, &mux_err)); + CHECK(moq_mux_catalog_Producer_Extra_mutate(catalog, reads_back, &back, &mux_err, NULL)); CHECK(contains(back.json, "\"status\"")); moq_buf_free(back.json); CHECK(is(back.app, "{\"v\":[1,true]}")); @@ -466,7 +466,7 @@ int main(void) { CHECK(is(json_err, "EOF while parsing a value at line 1 column 5")); moq_mux_catalog_hang_Extra *sections = moq_mux_catalog_hang_Extra_default(); CHECK(moq_Json_parse(str("1"), &app, &json_err)); - CHECK(!moq_mux_catalog_hang_Extra_set(sections, str("json"), app, &mux_err)); + CHECK(!moq_mux_catalog_hang_Extra_set(sections, str("json"), app, &mux_err, NULL)); CHECK(mux_err != NULL && is(moq_mux_Error_to_string(mux_err), "reserved catalog section: json")); moq_mux_Error_free(mux_err); mux_err = NULL; @@ -483,7 +483,7 @@ int main(void) { moq_tokio_quic_Config *quic = moq_tokio_quic_Config_default(); moq_tokio_Client *tokio_client = NULL; moq_tokio_Error *tokio_err = NULL; - CHECK(moq_tokio_connect_Config_init(client_config, quic, &tokio_client, &tokio_err)); + CHECK(moq_tokio_connect_Config_init(client_config, quic, &tokio_client, &tokio_err, NULL)); moq_tokio_connect_Config_free(client_config); moq_tokio_quic_Config_free(quic); moq_tokio_Client_free(tokio_client); @@ -496,44 +496,44 @@ int main(void) { /* A session's config, filled in as Rust assigns it, and a nested field by getting a copy, * editing it, and setting it back. */ moq_tokio_connect_Config *connect = moq_tokio_connect_Config_default(); - moq_tokio_tls_Connect *tls = moq_tokio_connect_Config_tls_get(connect); - moq_tokio_tls_Connect_insecure_set(tls, (moq_option_bool){.some = true, .value = true}); - moq_tokio_tls_Connect_host_name_set(tls, str("relay.example")); + moq_tokio_tls_Connect *tls = moq_tokio_connect_Config_tls_get(connect, NULL); + moq_tokio_tls_Connect_insecure_set(tls, (moq_option_bool){.some = true, .value = true}, NULL); + moq_tokio_tls_Connect_host_name_set(tls, str("relay.example"), NULL); /* A `Vec`, an array of buffers C keeps. */ moq_buf pins[] = {str("ab:cd")}; - moq_tokio_tls_Connect_fingerprint_set(tls, (moq_vec_string){.ptr = pins, .len = 1}); - moq_tokio_connect_Config_tls_set(connect, tls); + moq_tokio_tls_Connect_fingerprint_set(tls, (moq_vec_string){.ptr = pins, .len = 1}, NULL); + moq_tokio_connect_Config_tls_set(connect, tls, NULL); moq_tokio_tls_Connect_free(tls); - moq_tokio_connect_Config_implementation_set(connect, str("moq.c")); + moq_tokio_connect_Config_implementation_set(connect, str("moq.c"), NULL); /* A URL, parsed from text, failing with why, and moved in. */ moq_Url *url = NULL; moq_buf parse_err; CHECK(!moq_Url_parse(str("relay.example"), &url, &parse_err)); CHECK(is(parse_err, "relative URL without a base")); CHECK(moq_Url_parse(str("https://relay.example:4443/demo"), &url, &parse_err)); - moq_tokio_connect_Config_url_set(connect, url); + moq_tokio_connect_Config_url_set(connect, url, NULL); moq_Url_free(url); - tls = moq_tokio_connect_Config_tls_get(connect); - moq_option_bool insecure = moq_tokio_tls_Connect_insecure_get(tls); + tls = moq_tokio_connect_Config_tls_get(connect, NULL); + moq_option_bool insecure = moq_tokio_tls_Connect_insecure_get(tls, NULL); CHECK(insecure.some && insecure.value); - CHECK(is(moq_tokio_tls_Connect_host_name_get(tls), "relay.example")); + CHECK(is(moq_tokio_tls_Connect_host_name_get(tls, NULL), "relay.example")); /* TLS files, as OS paths: none by default. */ - moq_vec_path roots = moq_tokio_tls_Connect_root_get(tls); - moq_vec_string fingerprints = moq_tokio_tls_Connect_fingerprint_get(tls); + moq_vec_path roots = moq_tokio_tls_Connect_root_get(tls, NULL); + moq_vec_string fingerprints = moq_tokio_tls_Connect_fingerprint_get(tls, NULL); CHECK(roots.ptr != NULL && roots.len == 0); CHECK(fingerprints.len == 1 && fingerprints.ptr[0].len == 5 && memcmp(fingerprints.ptr[0].ptr, "ab:cd", 5) == 0); moq_vec_path_free(roots); moq_vec_string_free(fingerprints); moq_tokio_tls_Connect_free(tls); - CHECK(is(moq_tokio_connect_Config_implementation_get(connect), "moq.c")); - url = moq_tokio_connect_Config_url_get(connect); - CHECK(url != NULL && is(moq_Url_to_string(url), "https://relay.example:4443/demo")); + CHECK(is(moq_tokio_connect_Config_implementation_get(connect, NULL), "moq.c")); + url = moq_tokio_connect_Config_url_get(connect, NULL); + CHECK(url != NULL && is(moq_Url_to_string(url, NULL), "https://relay.example:4443/demo")); /* The config's URL, dialed with no tokens. */ - moq_auth_Tokens *tokens = moq_auth_Tokens_new((moq_vec_auth_Token){0}); + moq_auth_Tokens *tokens = moq_auth_Tokens_new((moq_vec_auth_Token){0}, NULL); moq_Versions *versions = moq_Versions_default(); - moq_auth_Dial *dial = moq_auth_Tokens_dial(tokens, url, versions); - moq_Url *dialed = moq_auth_Dial_url_get(dial); - CHECK(moq_Url_eq(dialed, url)); + moq_auth_Dial *dial = moq_auth_Tokens_dial(tokens, url, versions, NULL); + moq_Url *dialed = moq_auth_Dial_url_get(dial, NULL); + CHECK(moq_Url_eq(dialed, url, NULL)); moq_Url_free(dialed); moq_auth_Dial_free(dial); moq_auth_Tokens_free(tokens); @@ -542,13 +542,13 @@ int main(void) { /* A backoff's fields are settings left unset, and the methods of their names resolve them. */ moq_tokio_Backoff *backoff = moq_tokio_Backoff_default(); - CHECK(moq_tokio_Backoff_initial_get(backoff) == NULL); - moq_duration initial = moq_tokio_Backoff_initial(backoff); + CHECK(moq_tokio_Backoff_initial_get(backoff, NULL) == NULL); + moq_duration initial = moq_tokio_Backoff_initial(backoff, NULL); CHECK(initial.secs == 1 && initial.nanos == 0); moq_tokio_Backoff_free(backoff); /* A `Vec`, each element a buffer the array frees. */ - moq_vec_string alpns = moq_Versions_alpns(versions); + moq_vec_string alpns = moq_Versions_alpns(versions, NULL); CHECK(alpns.len > 0); for (size_t i = 0; i < alpns.len; i++) { CHECK(alpns.ptr[i].len > 0); @@ -562,16 +562,16 @@ int main(void) { CHECK(is(moq_tokio_listen_Bind_to_string(&v4), "127.0.0.1:0")); CHECK(!moq_tokio_listen_Bind_eq(&bind, &v4)); moq_tokio_listen_Config *listen = moq_tokio_listen_Config_default(); - moq_tokio_listen_Config_bind_set(listen, (moq_option_tokio_listen_Bind){.some = true, .value = bind}); - moq_option_tokio_listen_Bind bound = moq_tokio_listen_Config_bind_get(listen); + moq_tokio_listen_Config_bind_set(listen, (moq_option_tokio_listen_Bind){.some = true, .value = bind}, NULL); + moq_option_tokio_listen_Bind bound = moq_tokio_listen_Config_bind_get(listen, NULL); CHECK(bound.some && moq_tokio_listen_Bind_eq(&bound.value, &bind)); moq_tokio_listen_Bind_free(&bound.value); /* The preferred address of each family, which takes only that family. */ moq_socket_addr preferred = {.ip = {10, 0, 0, 1}, .port = 4443}; - moq_tokio_listen_Config_preferred_v4_set(listen, (moq_option_socket_addr){.some = true, .value = preferred}); - moq_option_socket_addr preferred_v4 = moq_tokio_listen_Config_preferred_v4_get(listen); + moq_tokio_listen_Config_preferred_v4_set(listen, (moq_option_socket_addr){.some = true, .value = preferred}, NULL); + moq_option_socket_addr preferred_v4 = moq_tokio_listen_Config_preferred_v4_get(listen, NULL); CHECK(preferred_v4.some && !preferred_v4.value.v6 && preferred_v4.value.ip[0] == 10 && preferred_v4.value.port == 4443); - CHECK(!moq_tokio_listen_Config_preferred_v6_get(listen).some); + CHECK(!moq_tokio_listen_Config_preferred_v6_get(listen, NULL).some); moq_tokio_listen_Config_free(listen); /* A constant slice, as an array of clones. */ moq_vec_string alpns_all = moq_ALPNS(); @@ -583,38 +583,38 @@ int main(void) { * back. Neither has a runtime of C's, so moq-tokio's fallback runs both. */ moq_origin_Producer *served = moq_tokio_origin_spawn(); moq_origin_Publication *live = NULL; - CHECK(moq_origin_Producer_create_broadcast(served, str("live"), &live, &err)); + CHECK(moq_origin_Producer_create_broadcast(served, str("live"), &live, &err, NULL)); moq_origin_Route *route = moq_origin_Route_default(); - CHECK(moq_origin_Publication_announce(live, route, &err)); + CHECK(moq_origin_Publication_announce(live, route, &err, NULL)); moq_origin_Route_free(route); moq_broadcast_Producer *live_broadcast = moq_origin_Publication_broadcast(live); moq_track_Producer *live_track = NULL; - CHECK(moq_broadcast_Producer_create_track(live_broadcast, str("video"), NULL, &live_track, &err)); + CHECK(moq_broadcast_Producer_create_track(live_broadcast, str("video"), NULL, &live_track, &err, NULL)); moq_group_Producer *live_group = NULL; - CHECK(moq_track_Producer_append_group(live_track, &live_group, &err)); - CHECK(moq_group_Producer_write_frame(live_group, (moq_option_Timestamp){0}, str("hello"), &err)); - CHECK(moq_group_Producer_finish(live_group, &err)); + CHECK(moq_track_Producer_append_group(live_track, &live_group, &err, NULL)); + CHECK(moq_group_Producer_write_frame(live_group, (moq_option_Timestamp){0}, str("hello"), &err, NULL)); + CHECK(moq_group_Producer_finish(live_group, &err, NULL)); moq_group_Producer_free(live_group); moq_tokio_server_Config *server_config = moq_tokio_server_Config_default(); - moq_tokio_listen_Config *listen_config = moq_tokio_server_Config_listen_get(server_config); + moq_tokio_listen_Config *listen_config = moq_tokio_server_Config_listen_get(server_config, NULL); moq_tokio_listen_Bind any = {.tag = MOQ_TOKIO_LISTEN_BIND_ADDR, .addr = {._0 = {.port = 0, .v6 = true}}}; - moq_tokio_listen_Config_bind_set(listen_config, (moq_option_tokio_listen_Bind){.some = true, .value = any}); + moq_tokio_listen_Config_bind_set(listen_config, (moq_option_tokio_listen_Bind){.some = true, .value = any}, NULL); moq_tokio_tls_Listen *tls_listen = moq_tokio_tls_Listen_default(); moq_buf names[] = {str("localhost")}; - moq_tokio_tls_Listen_generate_set(tls_listen, (moq_vec_string){.ptr = names, .len = 1}); - moq_tokio_listen_Config_tls_set(listen_config, tls_listen); + moq_tokio_tls_Listen_generate_set(tls_listen, (moq_vec_string){.ptr = names, .len = 1}, NULL); + moq_tokio_listen_Config_tls_set(listen_config, tls_listen, NULL); moq_tokio_tls_Listen_free(tls_listen); - moq_tokio_server_Config_listen_set(server_config, listen_config); + moq_tokio_server_Config_listen_set(server_config, listen_config, NULL); moq_tokio_listen_Config_free(listen_config); moq_tokio_Server *server = NULL; - CHECK(moq_tokio_server_Config_init(server_config, &server, &tokio_err)); + CHECK(moq_tokio_server_Config_init(server_config, &server, &tokio_err, NULL)); moq_tokio_server_Config_free(server_config); moq_socket_addr local; - CHECK(moq_tokio_Server_local_addr(server, &local, &tokio_err)); + CHECK(moq_tokio_Server_local_addr(server, &local, &tokio_err, NULL)); CHECK(local.v6 && local.port != 0); - moq_origin_Consumer *published = moq_origin_Producer_consume(served); - serving = moq_tokio_Server_serve_publish(server, published); + moq_origin_Consumer *published = moq_origin_Producer_consume(served, NULL); + serving = moq_tokio_Server_serve_publish(server, published, NULL); moq_tokio_Server_free(server); moq_origin_Consumer_free(published); @@ -622,67 +622,67 @@ int main(void) { snprintf(address, sizeof address, "moqt://localhost:%u", (unsigned)local.port); moq_tokio_connect_Config *dialing = moq_tokio_connect_Config_default(); CHECK(moq_Url_parse(str(address), &url, &parse_err)); - moq_tokio_connect_Config_url_set(dialing, url); + moq_tokio_connect_Config_url_set(dialing, url, NULL); moq_Url_free(url); - tls = moq_tokio_connect_Config_tls_get(dialing); - moq_tokio_tls_Connect_insecure_set(tls, (moq_option_bool){.some = true, .value = true}); - moq_tokio_connect_Config_tls_set(dialing, tls); + tls = moq_tokio_connect_Config_tls_get(dialing, NULL); + moq_tokio_tls_Connect_insecure_set(tls, (moq_option_bool){.some = true, .value = true}, NULL); + moq_tokio_connect_Config_tls_set(dialing, tls, NULL); moq_tokio_tls_Connect_free(tls); moq_tokio_client_Config *session_client = moq_tokio_client_Config_default(); - moq_tokio_client_Config_connect_set(session_client, dialing); + moq_tokio_client_Config_connect_set(session_client, dialing, NULL); moq_tokio_connect_Config_free(dialing); moq_tokio_Client *dialer = NULL; - CHECK(moq_tokio_client_Config_init(session_client, &dialer, &tokio_err)); + CHECK(moq_tokio_client_Config_init(session_client, &dialer, &tokio_err, NULL)); moq_tokio_client_Config_free(session_client); - moq_tokio_Client *dialer_copy = moq_tokio_Client_clone(dialer); - moq_tokio_Client *once = moq_tokio_Client_with_reconnect(dialer_copy, false); + moq_tokio_Client *dialer_copy = moq_tokio_Client_clone(dialer, NULL); + moq_tokio_Client *once = moq_tokio_Client_with_reconnect(dialer_copy, false, NULL); moq_tokio_Client_free(dialer_copy); moq_origin_Producer *received = moq_tokio_origin_spawn(); - moq_origin_Consumer *remote = moq_origin_Producer_consume(received); - moq_tokio_Connection *connection = moq_tokio_Client_consume(once, received); + moq_origin_Consumer *remote = moq_origin_Producer_consume(received, NULL); + moq_tokio_Connection *connection = moq_tokio_Client_consume(once, received, NULL); moq_tokio_Client_free(once); moq_origin_Producer_free(received); CHECK(connection != NULL); - moq_tokio_Connection_established_future *establishing = moq_tokio_Connection_established(connection); + moq_tokio_Connection_established_future *establishing = moq_tokio_Connection_established(connection, NULL); moq_tokio_Connection_free(connection); moq_tokio_Connection *established = NULL; AWAIT(moq_tokio_Connection_established_poll(establishing, session_waker, &established, &tokio_err)); moq_tokio_Connection_established_future_free(establishing); CHECK(established != NULL); - moq_origin_Consumer_request_future *requesting = moq_origin_Consumer_request(remote, str("live"), NULL); + moq_origin_Consumer_request_future *requesting = moq_origin_Consumer_request(remote, str("live"), NULL, NULL); moq_broadcast_Consumer *remote_broadcast = NULL; AWAIT(moq_origin_Consumer_request_poll(requesting, session_waker, &remote_broadcast, &err)); moq_origin_Consumer_request_future_free(requesting); CHECK(remote_broadcast != NULL); moq_track_Consumer *remote_track = NULL; - CHECK(moq_broadcast_Consumer_track(remote_broadcast, str("video"), &remote_track, &err)); - subscribing = moq_track_Consumer_subscribe(remote_track, NULL); + CHECK(moq_broadcast_Consumer_track(remote_broadcast, str("video"), &remote_track, &err, NULL)); + subscribing = moq_track_Consumer_subscribe(remote_track, NULL, NULL); AWAIT(moq_track_Consumer_subscribe_poll(subscribing, session_waker, &subscriber, &err)); moq_track_Consumer_subscribe_future_free(subscribing); CHECK(subscriber != NULL); - receiving = moq_track_Subscriber_recv_group(subscriber); + receiving = moq_track_Subscriber_recv_group(subscriber, NULL); group = NULL; AWAIT(moq_track_Subscriber_recv_group_poll(receiving, session_waker, &group, &err)); moq_track_Subscriber_recv_group_future_free(receiving); CHECK(group != NULL); - reading = moq_group_Consumer_read_frame(group); + reading = moq_group_Consumer_read_frame(group, NULL); frame = NULL; AWAIT(moq_group_Consumer_read_frame_poll(reading, session_waker, &frame, &err)); moq_group_Consumer_read_frame_future_free(reading); - CHECK(frame != NULL && is(moq_frame_Frame_payload_get(frame), "hello")); + CHECK(frame != NULL && is(moq_frame_Frame_payload_get(frame, NULL), "hello")); moq_frame_Frame_free(frame); moq_group_Consumer_free(group); moq_track_Subscriber_free(subscriber); moq_track_Consumer_free(remote_track); moq_broadcast_Consumer_free(remote_broadcast); - moq_tokio_Connection_close_future *closing = moq_tokio_Connection_close(established); + moq_tokio_Connection_close_future *closing = moq_tokio_Connection_close(established, NULL); moq_tokio_Connection_free(established); AWAIT(moq_tokio_Connection_close_poll(closing, session_waker, &tokio_err)); moq_tokio_Connection_close_future_free(closing); - moq_tokio_Client_close_future *client_closing = moq_tokio_Client_close(dialer); + moq_tokio_Client_close_future *client_closing = moq_tokio_Client_close(dialer, NULL); moq_tokio_Client_free(dialer); AWAIT(moq_tokio_Client_close_poll(client_closing, session_waker)); moq_tokio_Client_close_future_free(client_closing); diff --git a/ffi/moq/moq.swift b/ffi/moq/moq.swift index 2ebd8279e..8a545fe0e 100644 --- a/ffi/moq/moq.swift +++ b/ffi/moq/moq.swift @@ -142,9 +142,9 @@ func block(_ body: @escaping @Sendable () async throws -> T) throw } /// Whether `path` is `other`: `==` takes a `Path`, which an optional one holds. -func same(_ path: borrowing Path?, _ other: borrowing Path) -> Bool { +func same(_ path: borrowing Path?, _ other: borrowing Path) throws -> Bool { switch path { - case .some(let path): return path == other + case .some(let path): return try path == other case .none: return false } } @@ -155,28 +155,28 @@ func same(_ path: borrowing Path?, _ other: borrowing Path) -> Bool { func origin(_ waiter: borrowing Waiter, _ wakes: Cell, _ before: inout Int) throws -> OriginDriver { // An origin and the driver that runs it, a pair. Its root, which it lends, is returned // as a new handle to a clone. - let pair = OriginProducer.new(OriginConfig.default()) + let pair = try OriginProducer.new(OriginConfig.default()) let origin = pair._0 var driver = pair._1 - expect(origin.root().asStr() == "", "an origin's root is empty") + expect(try origin.root().asStr() == "", "an origin's root is empty") // Scoped beneath a root to patterns, which moq-pattern's own export builds. var patterns = Patterns.new() expect(try patterns.insert(Pattern.subtree("alice")), "a pattern inserts") let scoped = try origin.scope("room", patterns) - expect(scoped.root().asStr() == "room", "a scope has its root") + expect(try scoped.root().asStr() == "room", "a scope has its root") expect(try scoped.absolute("alice").asStr() == "room/alice", "a scope's paths are absolute") - let allowed = scoped.allowed() - expect(allowed.matches("alice/cam"), "a scope allows its patterns") - expect(!allowed.matches("bob"), "a scope allows nothing else") - let consumer = scoped.consume() + let allowed = try scoped.allowed() + expect(try allowed.matches("alice/cam"), "a scope allows its patterns") + expect(try !allowed.matches("bob"), "a scope allows nothing else") + let consumer = try scoped.consume() expect(try consumer.absolute("bob").asStr() == "room/bob", "a scope's consumer shares its root") // A broadcast the origin creates is stamped with its path, which its info lends. do { let publication = try origin.createBroadcast("room/alice") - let path = publication.broadcast().info().pathGet - expect(path.asStr() == "room/alice", "a broadcast has its path") + let path = try publication.broadcast().info().pathGet() + expect(try path.asStr() == "room/alice", "a broadcast has its path") // Paths own their segments, so Swift builds them, and relative ones, from a string. do { @@ -186,18 +186,18 @@ func origin(_ waiter: borrowing Waiter, _ wakes: Cell, _ before: inout Int) expect(e == .emptySegment, "an empty segment is its own error") } let cam = try Path.new("cam") - expect(cam.asStr() == "cam", "a path builds from its segments") + expect(try cam.asStr() == "cam", "a path builds from its segments") let joined = try path.join(cam) - expect(joined.asStr() == "room/alice/cam", "paths join") - expect(same(joined.stripPrefix(path), cam), "a path strips its prefix") - expect(path.stripPrefix(joined) == nil, "a path strips no longer prefix") + expect(try joined.asStr() == "room/alice/cam", "paths join") + expect(try same(joined.stripPrefix(path), cam), "a path strips its prefix") + expect(try path.stripPrefix(joined) == nil, "a path strips no longer prefix") expect(try joined.resolve(PathRelative.new("../bob")).asStr() == "room/bob", "a relative path resolves") } _ = try driver.poll(clockNow(), waiter) // A client publishes the origin through moq-net's generic `with_publisher`, at each // type that implements its bound, by reference, so Swift keeps its handle. - _ = Client.new().withPublisherProducer(origin) + _ = try Client.new().withPublisherProducer(origin) before = wakes.value return driver } @@ -205,16 +205,16 @@ func origin(_ waiter: borrowing Waiter, _ wakes: Cell, _ before: inout Int) func script() throws { let q = Queue() - let broadcast = BroadcastInfo.new().produce() + let broadcast = try BroadcastInfo.new().produce() let track = try broadcast.createTrack("video", nil) - expect(track.name() == "video", "a track has its name") + expect(try track.name() == "video", "a track has its name") // A second track of the same name is an error, printed as Rust prints it. do { _ = try broadcast.createTrack("video", nil) expect(false, "a duplicate track is an error") } catch let e as Moq.Error { - expect("\(e)" == "duplicate", "a duplicate track's error is Rust's") + expect(try e.description == "duplicate", "a duplicate track's error is Rust's") } let subscriber = Held(try block { try await track.consume().subscribe(nil) }) @@ -232,7 +232,7 @@ func script() throws { expect(q.size == 1, "a group's arrival wakes the receive") q.drain() let first = Held(group.take()!) - expect(first.value.index() == 0, "the first group is 0") + expect(try first.value.index() == 0, "the first group is 0") // So does a frame's. let frame = Slot() @@ -247,10 +247,10 @@ func script() throws { expect(q.size == 1, "a frame's arrival wakes the read") q.drain() let hello = frame.take()! - expect(text(hello.payloadGet) == "hello", "a frame has its payload") - expect(hello.timestampGet == nil, "an untimed frame has no timestamp") + expect(text(try hello.payloadGet) == "hello", "a frame has its payload") + expect(try hello.timestampGet == nil, "an untimed frame has no timestamp") // When it arrived: kio's clock's `Instant`, from kio's own export. - expect(clockNow().checkedDurationSince(hello.arrived()) != nil, "a frame arrived before now") + expect(try clockNow().checkedDurationSince(hello.arrived()) != nil, "a frame arrived before now") // A timestamp is a `Copy` value copied across, which allocates nothing. The track is // untimed, so a timestamped frame is refused. @@ -260,7 +260,7 @@ func script() throws { try writer.writeFrame(at, Bytes("late".utf8)) expect(false, "a timestamped frame on an untimed track is an error") } catch let e as Moq.Error { - expect("\(e)" == "frame timestamp doesn't fit its track", "the late frame's error is Rust's") + expect(try e.description == "frame timestamp doesn't fit its track", "the late frame's error is Rust's") } expect(at.asSecs() == 1, "a copied timestamp stays usable") @@ -270,7 +270,7 @@ func script() throws { try writer.finish() try block { let last = try await first.value.readFrame() - expect(last?.payloadGet == world, "the last frame reads") + expect(try last?.payloadGet == world, "the last frame reads") expect(try await first.value.readFrame() == nil, "a finished group reads nil") } @@ -287,17 +287,17 @@ func script() throws { _ = try driver.poll(clockNow(), waiter) expect(false, "a driver whose origin is gone finishes") } catch let e as Moq.Error { - expect("\(e)" == "closed", "a finished driver's error is Rust's") + expect(try e.description == "closed", "a finished driver's error is Rust's") } // moq-mux reads an OpusHead into hang's catalog config: 44.1 kHz in, stereo, which the // catalog lists at the decoder's 48 kHz, keeping the head as its description. let head = Bytes(Data("OpusHead".utf8) + Data([1, 2, 0x38, 0x01, 0x44, 0xac, 0, 0, 0, 0, 0])) let audio = try muxCodecOpusConfig(head) - expect(audio.sampleRateGet == 48000, "Opus decodes at 48 kHz") - expect(audio.channelCountGet == 2, "the head is stereo") - expect(audio.codecGet == .opus, "the codec is Opus") - expect(audio.descriptionGet == head, "the head is the description") + expect(try audio.sampleRateGet == 48000, "Opus decodes at 48 kHz") + expect(try audio.channelCountGet == 2, "the head is stereo") + expect(try audio.codecGet == .opus, "the codec is Opus") + expect(try audio.descriptionGet == head, "the head is the description") // A truncated head is moq-mux's error. do { _ = try muxCodecOpusConfig(head.prefix(8)) @@ -308,18 +308,21 @@ func script() throws { // A subscription's ranges, from any iterable of them: an array. let groups = TrackRange.groups(BoundsU64(start: .included(2), end: .excluded(6))) - let ranged = TrackSubscription.default().withRanges([TrackRange.live(), groups]).rangesGet - expect(ranged == [TrackRange.live(), groups], "a subscription keeps its ranges") + let ranged = try TrackSubscription.default().withRanges([TrackRange.live(), groups]).rangesGet + expect( + ranged.count == 2 && ranged[0] == TrackRange.live() && ranged[1] == groups, + "a subscription keeps its ranges" + ) expect(groups.start == .absolute(2), "groups start at their first") - expect(groups.end == .absolute(6), "groups end before their end") + expect(groups.end.map { $0 == .absolute(6) } == true, "groups end before their end") // A catalog at moq-mux's untyped extension, from the config Swift builds, whose generic // `is_claimed` is named by the last segment of its argument, and whose `mutate` lends // the catalog to Swift. - var publisher = BroadcastInfo.new().produce() - let extra = MuxCatalogConfig.default().withCatalogExtra(HangCatalogExtra.default()) + var publisher = try BroadcastInfo.new().produce() + let extra = try MuxCatalogConfig.default().withCatalogExtra(HangCatalogExtra.default()) var catalog = try MuxCatalogProducerExtra.new(&publisher, extra) - expect(!catalog.isClaimedBinaryConfig("state"), "an unclaimed name is not claimed") + expect(try !catalog.isClaimedBinaryConfig("state"), "an unclaimed name is not claimed") let read = Cell(0) try catalog.mutate { lent in if try! lent.toJson().hasPrefix("{") { @@ -333,27 +336,27 @@ func script() throws { // read back as text. var status = try catalog.jsonSnapshotConfig( try publisher.createTrack("status", TrackInfo.default().withTimescale(Timescale.MILLI)), MuxJsonConfig.default(), MuxJsonOptions.default()) - expect(status.name() == "status", "a JSON snapshot has its track's name") - expect(catalog.isClaimedJsonConfig("status"), "the JSON snapshot claims its name") + expect(try status.name() == "status", "a JSON snapshot has its track's name") + expect(try catalog.isClaimedJsonConfig("status"), "the JSON snapshot claims its name") // A value Swift parses is published as the track's latest, which a subscriber reads // back as JSON text. let on = try Json.parse(#"{"on": true}"#) try status.update(on, Timestamp.now()) - let latest = Held(status.consume()) + let latest = Held(try status.consume()) try block { var group = try await latest.value.recvGroup() let frame = try await group?.readFrame() - expect(text(frame?.payloadGet) == #"{"on":true}"#, "a subscriber reads the latest value") + expect(text(try frame?.payloadGet) == #"{"on":true}"#, "a subscriber reads the latest value") } try catalog.mutate { lent in - var sections = lent.extGet + var sections = try! lent.extGet() try! sections.set("app", Json.parse(#"{"v": [1, true]}"#)) - lent.extSet(sections) + try! lent.extSet(sections) } let app = Cell(nil) try catalog.mutate { lent in expect(try! lent.toJson().contains("\"status\""), "the catalog lists the JSON track") - app.value = lent.extGet.get("app")?.toText() + app.value = try! lent.extGet().get("app")?.toText() } expect(app.value == #"{"v":[1,true]}"#, "the catalog reads its section back as text") // Malformed text fails to parse, and a reserved section is moq-mux's error. @@ -384,19 +387,19 @@ func script() throws { } // A session's config, its URL and pinned fingerprints set as Rust assigns them. var connect = TokioConnectConfig.default() - var tls = connect.tlsGet - tls.fingerprintSet(["ab:cd"]) - connect.tlsSet(tls) - connect.urlSet(try Url.parse("https://relay.example:4443/demo")) - expect(connect.tlsGet.fingerprintGet == ["ab:cd"], "a TLS config keeps its fingerprints") - let url = connect.urlGet! + var tls = try connect.tlsGet() + try tls.fingerprintSet(["ab:cd"]) + try connect.tlsSet(tls) + try connect.urlSet(try Url.parse("https://relay.example:4443/demo")) + expect(try connect.tlsGet().fingerprintGet == ["ab:cd"], "a TLS config keeps its fingerprints") + let url = try connect.urlGet()! let versions = Versions.default() - let dialed = AuthTokens.new(UniqueArray()).dial(url, versions).urlGet - expect(dialed == url, "a dial keeps its URL") - expect(dialed.description == "https://relay.example:4443/demo", "a URL prints as Rust's") + let dialed = try AuthTokens.new(UniqueArray()).dial(url, versions).urlGet() + expect(try dialed == url, "a dial keeps its URL") + expect(try dialed.description == "https://relay.example:4443/demo", "a URL prints as Rust's") // A `Vec`, as an array. expect(!ALPNS.isEmpty, "a constant slice crosses as an array") - let alpns = versions.alpns() + let alpns = try versions.alpns() expect(!alpns.isEmpty && alpns.allSatisfy { !$0.isEmpty }, "each version has an ALPN") // A socket address, crossing in a listener's bind. let loopback: [UInt8] = Array(repeating: 0, count: 15) + [1] @@ -405,14 +408,14 @@ func script() throws { let v4 = SocketAddr(ip: [127, 0, 0, 1], port: 0) expect("\(TokioListenBind.addr(v4))" == "127.0.0.1:0", "an IPv4 bind prints as Rust's") var listen = TokioListenConfig.default() - listen.bindSet(bind) + try listen.bindSet(bind) // The preferred address of each family, which takes only that family. - listen.preferredV4Set(v4) - expect(listen.preferredV4Get == v4 && listen.preferredV6Get == nil, "a listener keeps its preferred IPv4 address") - expect(listen.bindGet == bind, "a listener's config keeps its bind") + try listen.preferredV4Set(v4) + expect(try listen.preferredV4Get == v4 && listen.preferredV6Get == nil, "a listener keeps its preferred IPv4 address") + expect(try listen.bindGet == bind, "a listener's config keeps its bind") // TLS files, as OS paths, and fingerprints, as strings: none by default. let defaults = TokioTlsConnect.default() - expect(defaults.rootGet.isEmpty && defaults.fingerprintGet.isEmpty, "a default TLS config lists none") + expect(try defaults.rootGet.isEmpty && defaults.fingerprintGet.isEmpty, "a default TLS config lists none") // A session, as moq.c runs it: a server bound to port 0 reads back the port it got and // serves with one call, in a task of its own, and a client dials it with one call and @@ -432,12 +435,12 @@ func script() throws { try liveGroup.finish() var serverConfig = TokioServerConfig.default() - var listen = serverConfig.listenGet - listen.bindSet(.addr(SocketAddr(ip: Array(repeating: 0, count: 16), port: 0))) - var tls = listen.tlsGet - tls.generateSet(["localhost"]) - listen.tlsSet(tls) - serverConfig.listenSet(listen) + var listen = try serverConfig.listenGet() + try listen.bindSet(.addr(SocketAddr(ip: Array(repeating: 0, count: 16), port: 0))) + var tls = try listen.tlsGet() + try tls.generateSet(["localhost"]) + try listen.tlsSet(tls) + try serverConfig.listenSet(listen) let server = try serverConfig.init_() let local = try server.localAddr() expect(local.port != 0, "a server bound to port 0 reads back the port it got") @@ -447,23 +450,23 @@ func script() throws { let serving = Task { try await owned.take()!.servePublish(served.consume()) } var connect = TokioConnectConfig.default() - connect.urlSet(try Url.parse("moqt://localhost:\(local.port)")) - var insecure = connect.tlsGet - insecure.insecureSet(true) - connect.tlsSet(insecure) + try connect.urlSet(try Url.parse("moqt://localhost:\(local.port)")) + var insecure = try connect.tlsGet() + try insecure.insecureSet(true) + try connect.tlsSet(insecure) var clientConfig = TokioClientConfig.default() - clientConfig.connectSet(connect) + try clientConfig.connectSet(connect) let client = try clientConfig.init_() let received = tokioOriginSpawn() - let remote = received.consume() - let connection = client.clone().withReconnect(false).consume(received) + let remote = try received.consume() + let connection = try client.clone().withReconnect(false).consume(received) expect(connection != nil, "a client with a URL dials it") let established = try await connection!.established() var subscriber = try await remote.request("live", nil).track("video").subscribe(nil) var group = try await subscriber.recvGroup() let frame = try await group?.readFrame() - expect(text(frame?.payloadGet) == "hello", "a session reads the frame the server serves") + expect(text(try frame?.payloadGet) == "hello", "a session reads the frame the server serves") try await established.close() try await client.close() diff --git a/ffi/runtime/jni.rs b/ffi/runtime/jni.rs index fda934b83..963c925e3 100644 --- a/ffi/runtime/jni.rs +++ b/ffi/runtime/jni.rs @@ -104,6 +104,36 @@ pub fn fail(env: *mut JNIEnv, what: &str, msg: &str) -> ! { rt::fail(what, msg) } +static REFUSED: OnceLock = OnceLock::new(); + +/// Throw the `_Native.__Refused`, an `IllegalStateException`, a call of `what` raises once +/// its check failed with `status`, returning a zeroed value Kotlin never reads, as JNI's +/// pending exception throws once the native method returns. Its own class tells Kotlin +/// the call moved nothing. +#[cold] +pub fn refuse(env: *mut JNIEnv, status: i32, what: &str) -> R { + let message = match rt::Status::of(status) { + Some(status) => format!("{what}: {}", status.message()), + None => format!("{what}: an unknown status {status}"), + }; + let message = std::ffi::CString::new(message).expect("no NUL in a message"); + let id = method(env, &REFUSED, c"__refused", c"(Ljava/lang/String;)Ljava/lang/Throwable;"); + // SAFETY: `env` is this thread's, and `__refused` takes the message. + unsafe { + let text = ((**env).v1_2.NewStringUTF)(env, message.as_ptr()); + if text.is_null() { + fail(env, what, "a message did not fit"); + } + let args = [jvalue { l: text }]; + let refused = ((**env).v1_2.CallStaticObjectMethodA)(env, class(), id, args.as_ptr()); + if refused.is_null() || ((**env).v1_2.ExceptionCheck)(env) { + fail(env, what, "a call into Kotlin threw"); + } + ((**env).v1_2.Throw)(env, refused); + std::mem::zeroed() + } +} + /// Run `f` calling into Kotlin on this thread, freeing the local references it makes, and /// abort if what it calls throws: Rust has nowhere to unwind it to. pub fn upcall(what: &str, f: impl FnOnce(*mut JNIEnv) -> R) -> R { diff --git a/ffi/runtime/python.rs b/ffi/runtime/python.rs index 02be9650e..5bd119c16 100644 --- a/ffi/runtime/python.rs +++ b/ffi/runtime/python.rs @@ -361,15 +361,14 @@ impl Key { } /// What a call borrows from Python until it returns: the objects and copies whose memory -/// it reads, the handles it holds and those it moves out of, and what it -/// gives Rust, taken back if the call never happens. +/// it reads, the lent handles it holds, and what it gives Rust, taken back if the call +/// never happens. #[derive(Default)] pub struct Scope { keep: Vec, arrays: Vec>, - /// The handles held, each with whether the call moves its value out. Inline, as a - /// call holds few, so holding one allocates nothing. - held: smallvec::SmallVec<[(Obj, bool); 4]>, + /// The lent handles held. Inline, as a call holds few, so holding one allocates nothing. + held: smallvec::SmallVec<[Obj; 4]>, /// The objects Rust lends a callback, ended once it returns. lent: Vec, /// Python implementations and wakers given to Rust, taken back unless the call @@ -390,15 +389,30 @@ impl Scope { ptr } - /// Note that the call happened: what it moved out of is closed, and what it was given - /// is Rust's. + /// Note that the call happened: what it was given is Rust's. pub fn done(&mut self) { - for (held, moved) in &self.held { - if *moved { - // SAFETY: a handle this scope holds, so live. - unsafe { close(held.ptr().cast()) }; - } - } + self.given(); + } + + /// Raise for the call of `what` unless its `status` is zero: a run-time check failed, + /// so Rust moved nothing out, though it took and dropped what it was given. + pub fn status(&mut self, status: i32, what: &str) -> R<()> { + if status == 0 { + return Ok(()); + } + self.given(); + let message = format!("{what}: {}", rt::Status::text(status).to_string_lossy()); + // A value moved out or outlived is as a closed one, and any other check is a misuse. + // SAFETY: static exception types. + let exc = match rt::Status::of(status) { + Some(rt::Status::Moved | rt::Status::Outlived) => unsafe { PyExc_ValueError }, + _ => unsafe { PyExc_RuntimeError }, + }; + raise(exc, &message) + } + + /// Note that what the call was given is Rust's. + fn given(&mut self) { while let Some(given) = self.given.pop() { given.into_ptr(); } @@ -409,7 +423,7 @@ impl Scope { /// The handles held, which a future keeps until it ends. pub fn take_held(&mut self) -> Vec { - self.held.drain(..).map(|(held, _)| held).collect() + self.held.drain(..).collect() } } @@ -441,7 +455,7 @@ impl Drop for Scope { // SAFETY: an object [`lent`] made. unsafe { end(lent.ptr().cast()) }; } - while let Some((held, _)) = self.held.pop() { + while let Some(held) = self.held.pop() { // SAFETY: a handle this scope holds. unsafe { release(held.ptr().cast()) }; } @@ -1652,7 +1666,9 @@ pub unsafe extern "C" fn free_callback(data: *mut c_void) { } /// An object owning a handle Rust returned, or one Rust lends for a call: the instance -/// of a class of the generated Python, which subclasses `Object`. +/// of a class of the generated Python, which subclasses `Object`. An owned one is freed +/// once its last reference goes, which no call outlives: Rust's own check fails a use +/// after its value moved out. A lent one counts its calls, so its lend ends with none. #[repr(C)] pub struct Handle { ob_base: PyObject, @@ -1661,7 +1677,7 @@ pub struct Handle { raw: *const c_void, /// What frees the handle: none for one Rust lends, or once freed. free: Option, - /// The calls holding it, each of which it outlives. + /// The calls holding a lent one, each of which it outlives. holds: usize, closed: bool, } @@ -1709,9 +1725,9 @@ fn handle(raw: *const c_void, index: usize, free: Option R<*const c_void> { +/// The handle `obj` owns, an object of the class registered at `index`, for the call, +/// which the caller's reference keeps; a lent one held until the call returns. +pub fn hold(obj: *mut PyObject, index: usize, s: &mut Scope) -> R<*const c_void> { check(obj, index)?; let handle = obj.cast::(); // SAFETY: an object of a class that subclasses `Object`, and the GIL is held. @@ -1719,52 +1735,33 @@ pub fn hold(obj: *mut PyObject, index: usize, s: &mut Scope, moved: bool) -> R<* if (*handle).index != index { return expected(obj, index); } + if (*handle).free.is_some() { + return Ok((*handle).raw); + } if (*handle).closed { return value_error(&format!("{} is closed", type_name(ty(index)))); } (*handle).holds += 1; - s.held.push((Obj::borrowed(obj), moved)); + s.held.push(Obj::borrowed(obj)); Ok((*handle).raw) } } /// [`hold`], or a null handle for `None`. -pub fn hold_opt(obj: *mut PyObject, index: usize, s: &mut Scope, moved: bool) -> R<*const c_void> { +pub fn hold_opt(obj: *mut PyObject, index: usize, s: &mut Scope) -> R<*const c_void> { match is_none(obj) { true => Ok(null()), - false => hold(obj, index, s, moved), + false => hold(obj, index, s), } } -/// Let go of a call's hold, freeing the handle if it was closed meanwhile. +/// Let go of a call's hold of a lent handle. /// /// # Safety /// `handle` is live, and held. unsafe fn release(handle: *mut Handle) { // SAFETY: the caller's contract. - unsafe { - (*handle).holds -= 1; - if (*handle).closed && (*handle).holds == 0 { - free_now(handle); - } - } -} - -/// Close a handle: freed now, or once the last call holding it returns. -/// -/// # Safety -/// `handle` is live. -pub unsafe fn close(handle: *mut Handle) { - // SAFETY: the caller's contract. - unsafe { - if (*handle).closed { - return; - } - (*handle).closed = true; - if (*handle).holds == 0 { - free_now(handle); - } - } + unsafe { (*handle).holds -= 1 }; } /// End a lend, closing its handle, which Rust frees next: a call still holding it would @@ -1779,7 +1776,7 @@ unsafe fn end(handle: *mut Handle) { let name = type_name(Py_TYPE(handle.cast()).cast()); rt::fail(&name, "a call still holds it when its lend ends"); } - close(handle); + (*handle).closed = true; } } diff --git a/ffi/runtime/rt.dart b/ffi/runtime/rt.dart index f3c25f430..0b81d3ea3 100644 --- a/ffi/runtime/rt.dart +++ b/ffi/runtime/rt.dart @@ -318,14 +318,14 @@ bool _readerSeek( /// on a thread calling into Dart. final class Waker extends _Object { final ffi.Pointer _wake; - final ffi.Pointer _drop; + final ffi.Pointer _dropFn; final ffi.NativeFinalizer _finalizer; Waker._(_Waker raw) : _wake = raw.wake, - _drop = raw.drop, + _dropFn = raw.drop, _finalizer = _n.finalizer(raw.drop), - super(raw.data, true) { + super(raw.data, true, false) { _finalizer.attach(this, _raw, detach: this); } @@ -335,7 +335,7 @@ final class Waker extends _Object { @override void _free() { _finalizer.detach(this); - _dartWakerCall(_n.home, _drop, _raw); + _dartWakerCall(_n.home, _dropFn, _raw); } /// Wake the task through Rust: a call after [close] throws [StateError]. @@ -351,40 +351,52 @@ final class _Callback extends ffi.Struct { external ffi.Pointer data; } -/// What an object owns: its handle, freed once closed and no call holds it, or once the -/// garbage collector finds it unreachable. A lent one's Rust frees. +/// What an object owns: its handle, or one Rust lends for a call, which Rust frees. An owned +/// one of a value Rust checks is Rust's alone to account: [close] drops the value as Rust's +/// own drop does, failing while a call borrows it, a call after it throws as Rust's check +/// of a moved value does, and the handle is freed once the garbage collector finds this +/// unreachable. Any other counts its calls: [close] frees it then, throwing while a call +/// holds it, and a call after it throws. abstract base class _Object implements ffi.Finalizable { final ffi.Pointer _raw; - /// The owner's hold until closed, and one for each call. + /// The owner's hold until closed, and one for each call, of a counted one. int _holds = 1; bool _closed = false; final bool _owned; + /// Whether it counts its calls, rather than Rust. + final bool _counted; + /// The garbage collector's hold of an owned one's handle, until freed. ffi.Pointer _own = ffi.nullptr; - _Object(this._raw, this._owned); + _Object(this._raw, bool owned, bool checked) : _owned = owned, _counted = !(owned && checked); String get _type; /// Free the handle and detach its finalizer. void _free(); + /// Drop the value of one Rust checks, throwing [StateError] if a call borrows it. + void _drop() {} + ffi.Pointer _hold() { + if (!_counted) return _raw; if (_closed) throw StateError('$_type is closed'); _holds++; return _raw; } void _release() { - if (--_holds == 0 && _owned) _free(); + if (_counted && --_holds == 0 && _owned) _free(); } - /// Free the handle once no call holds it, rather than once the garbage collector finds - /// this unreachable: a call after it throws [StateError]. + /// Close it as [_Object] says; again, it does nothing. void close() { + if (!_counted) return _drop(); if (_closed) return; + if (_holds != 1) throw StateError('$_type: a handle is already borrowed'); _closed = true; _release(); } @@ -608,6 +620,12 @@ final _n = _Native(); final class _Native { late final _Stack stack = _Stack(); + + /// Where a call that can fail a check writes the failure: one for the isolate, since the + /// glue writes it on the isolate's thread as the call returns, once it served every call + /// queued meanwhile, and the call reads it before any other can run, and a failure + /// clears it. + late final ffi.Pointer status = _dartAlloc(4).cast()..value = 0; late final isolate.RawReceivePort _port; late final ffi.Pointer home; diff --git a/ffi/runtime/rt.go b/ffi/runtime/rt.go index 947cab826..dd8a482c9 100644 --- a/ffi/runtime/rt.go +++ b/ffi/runtime/rt.go @@ -17,6 +17,35 @@ const ( // once that callback returned. var ErrClosed = errors.New("closed") +// ErrBorrowed is what a call returns, wrapped with the function's name, once Rust's rules +// refuse a borrow it makes: of an object another call or future borrows in a way the +// borrow checker forbids beside it, or of one that is not Sync from a second thread. +var ErrBorrowed = errors.New("borrowed") + +// checkError_ is what a call returns once Rust refuses it before running: its status, or 0 +// for an item not on the library's target, which matches errors.ErrUnsupported. +type checkError_ struct { + what string + message string + status int32 +} + +func (e checkError_) Error() string { + return e.what + ": " + e.message +} + +func (e checkError_) Is(target error) bool { + switch e.status { + case 0: + return target == errors.ErrUnsupported + // Moved out, or lent from a handle that since changed: closed either way. + case 1, 6: + return target == ErrClosed + default: + return target == ErrBorrowed + } +} + type closedError_ struct { name string } @@ -29,22 +58,27 @@ func (e closedError_) Is(target error) bool { return target == ErrClosed } -// Handle_ is what an object owns, or is lent for a call: each call holds it, so Close frees -// it only once no call does, and a call after Close returns ErrClosed. For the generated -// packages, which share it: use none of it. +// Handle_ is what an object owns, or is lent for a call: each call holds it, so it is +// freed only once no call does, and a call after Close returns ErrClosed. Close of one +// whose value Rust checks, which drop drops, drops the value as Rust's own drop does, +// failing while a call borrows it in Rust; of any other, it fails while a call holds it. +// For the generated packages, which share it: use none of it. type Handle_ struct { raw unsafe.Pointer state atomic.Int64 // Nil for a handle Rust lends, which it frees itself. free func(unsafe.Pointer) + // Non-nil for an owned one of a value Rust checks. + drop func(unsafe.Pointer) error name string - // What closes an owned handle the GC finds unclosed. + // What frees an owned handle once the GC collects its object. Cleanup runtime.Cleanup } -// NewHandle_ is a handle of raw, which free frees, or nil for one Rust lends. -func NewHandle_(raw unsafe.Pointer, free func(unsafe.Pointer), name string) *Handle_ { - h := &Handle_{raw: raw, free: free, name: name} +// NewHandle_ is a handle of raw, which free frees, or nil for one Rust lends, and drop +// drops the value of, or nil for one Rust does not check. +func NewHandle_(raw unsafe.Pointer, free func(unsafe.Pointer), drop func(unsafe.Pointer) error, name string) *Handle_ { + h := &Handle_{raw: raw, free: free, drop: drop, name: name} h.state.Store(held_) return h } @@ -74,9 +108,55 @@ func (h *Handle_) Release() { } } -// Close gives up the owner's hold, freeing the handle once no call holds it; again, it -// does nothing. -func (h *Handle_) Close() { +// Close closes h as Handle_ says; again, it does nothing. +func (h *Handle_) Close() error { + if h.drop != nil { + raw := h.Hold() + if raw == nil { + return nil + } + err := h.drop(raw) + h.Release() + if err != nil { + return err + } + h.Moved() + return nil + } + for { + n := h.state.Load() + if n&closed_ != 0 { + return nil + } + if n != held_ { + return checkError_{h.name, "a handle is already borrowed", 3} + } + if h.state.CompareAndSwap(n, closed_) { + // A cleanup left registered until the GC slows each one registered after it. + h.Cleanup.Stop() + if h.free != nil { + h.free(h.raw) + } + return nil + } + } +} + +// Moved closes an owned handle a call moved the value out of, or Close dropped, freeing it +// once no call holds it. +func (h *Handle_) Moved() { + // A cleanup left registered until the GC slows each one registered after it. + h.Cleanup.Stop() + h.abandon() +} + +// Collected frees an owned handle the GC collected the object of, once no call holds it. +func (h *Handle_) Collected() { + h.abandon() +} + +// abandon gives up the owner's hold, freeing it once no call holds it. +func (h *Handle_) abandon() { for { n := h.state.Load() if n&closed_ != 0 { @@ -84,8 +164,6 @@ func (h *Handle_) Close() { } next := (n | closed_) - held_ if h.state.CompareAndSwap(n, next) { - // A cleanup left registered until the GC slows each one registered after it. - h.Cleanup.Stop() if next < held_ && h.free != nil { h.free(h.raw) } @@ -98,7 +176,7 @@ func (h *Handle_) Close() { // would reach the freed handle, so it panics, as Rust's own check fails once a call // borrowed. func (h *Handle_) End() { - h.Close() + h.abandon() if h.state.Load() != closed_ { panic(h.name + ": a call still holds it when its lend ends") } diff --git a/ffi/runtime/rt.kt b/ffi/runtime/rt.kt index 21b9751b3..872724bda 100644 --- a/ffi/runtime/rt.kt +++ b/ffi/runtime/rt.kt @@ -392,12 +392,19 @@ abstract class __Cleanup(owner: Any?) : java.lang.ref.PhantomReference(owne } /** - * A handle [owner] owns, or is lent for a call: each call holds it, so [close] frees it - * only once no call does, and a call after [close] throws. An owned one is closed once - * the GC collects [owner], if nothing closed it. + * A handle [owner] owns, or is lent for a call: each call holds it, so it is freed only + * once no call does, and a call after [close] throws. [close] of one whose value Rust + * checks, which [drop] drops, drops the value as Rust's own drop does, throwing while a + * call borrows it in Rust; of any other, it throws while a call holds it. An owned one is + * freed once the GC collects [owner], if nothing closed it. */ -class __Handle(owner: Any, internal val raw: Long, private val free: __Drop?, private val type: String) : - __Cleanup(if (free == null) null else owner) { +class __Handle( + owner: Any, + internal val raw: Long, + private val free: __Drop?, + private val type: String, + private val drop: __Close? = null, +) : __Cleanup(if (free == null) null else owner) { // The holds, one for the owner until closed and one per call, shifted past a bit set // once closed: one word, so a handle allocates nothing beside itself. @Volatile @JvmField internal var state = HELD @@ -416,11 +423,26 @@ class __Handle(owner: Any, internal val raw: Long, private val free: __Drop?, pr fun release() = freed(STATE.addAndGet(this, -HELD)) + /** Close it as [__Handle] says; again, it does nothing. */ fun close() { + if (drop != null) { + while (true) { + val n = state + if (n and CLOSED != 0L) return + if (STATE.compareAndSet(this, n, n + HELD)) break + } + try { + drop.close(raw) + } finally { + release() + } + return abandon() + } while (true) { val n = state if (n and CLOSED != 0L) return - if (STATE.compareAndSet(this, n, (n or CLOSED) - HELD)) return freed((n or CLOSED) - HELD) + kotlin.check(n == HELD) { "$type: a handle is already borrowed" } + if (STATE.compareAndSet(this, n, CLOSED)) return freed(CLOSED) } } @@ -429,17 +451,29 @@ class __Handle(owner: Any, internal val raw: Long, private val free: __Drop?, pr * reach the freed handle, so it throws, as Rust's own check fails once a call borrowed. */ fun end() { - close() + abandon() kotlin.check(state == CLOSED) { "$type: a call still holds it when its lend ends" } } + /** + * Give up the owner's hold, freeing it once no call holds one: once a call moved its + * value out, or the GC collected the owner. + */ + fun abandon() { + while (true) { + val n = state + if (n and CLOSED != 0L) return + if (STATE.compareAndSet(this, n, (n or CLOSED) - HELD)) return freed((n or CLOSED) - HELD) + } + } + // Free it once no hold is left. private fun freed(n: Long) { if (n < HELD && free != null && untrack()) free.drop(raw) } // A call still holding it frees it once it returns. - override fun collected() = close() + override fun collected() = abandon() /** Run [block] with the raw handle, held until it returns. */ inline fun hold(block: (Long) -> R): R { @@ -465,7 +499,7 @@ class __Returned(raw: Long) : Waker, AutoCloseable { override fun wake() = handle.hold { __waker_wake(it) } - override fun close() = handle.close() + override fun close() = handle.abandon() } /** Frees a raw handle: a `fun interface`, so the handle crosses unboxed. */ @@ -473,6 +507,11 @@ fun interface __Drop { fun drop(raw: Long) } +/** Drops the value of a raw handle, leaving the handle. */ +fun interface __Close { + fun close(raw: Long) +} + /** * Rust's buffer, which [owner] holds, freed once the GC collects this. Each read passes it * to the JNI function reading the buffer, as JNI keeps an argument reachable until the call diff --git a/ffi/runtime/rt.rs b/ffi/runtime/rt.rs index 61cf66588..6b57d80a8 100644 --- a/ffi/runtime/rt.rs +++ b/ffi/runtime/rt.rs @@ -1,9 +1,11 @@ //! The runtime every export shares: buffers, handles, lent functions, wakers, and futures //! across the C ABI. rust2-ffi copies it beside the generated code. //! -//! A broken contract across the ABI (a null or consumed handle, a handle borrowed in a -//! way Rust's rules forbid, invalid UTF-8, a future polled after it finished) aborts with -//! a message naming the C function: there is no Rust caller to return an error to. +//! A handle borrowed or moved in a way Rust's rules forbid fails the call with a +//! [`Status`], before any Rust code runs, which a binding raises as its language's own +//! error. Any other broken contract across the ABI (a null handle, invalid UTF-8, a future +//! polled after it finished) aborts with a message naming the C function: no binding +//! makes one, and there is no Rust caller to return an error to. use std::cell::UnsafeCell; use std::ffi::c_void; @@ -21,6 +23,95 @@ pub fn fail(what: &str, msg: &str) -> ! { std::process::abort() } +/// Why a call's check of a handle failed: a borrow or move Rust's borrow checker would +/// refuse. The call returns before any Rust code runs, so nothing changed. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[repr(i32)] +pub enum Status { + /// The handle's value was moved out, or the handle closed. + Moved = 1, + /// Another call or future borrows the value exclusively. + Exclusive = 2, + /// Another call or future borrows the value, so it cannot be borrowed exclusively or + /// moved out. + Borrowed = 3, + /// A value that is not `Sync` is borrowed on another thread. + Thread = 4, + /// A handle a call lent is borrowed exclusively or moved out. + Lent = 5, + /// A handle lent from another outlived it. + Outlived = 6, +} + +impl Status { + /// Every status, by its number less one. + pub const ALL: [Self; 6] = [ + Self::Moved, + Self::Exclusive, + Self::Borrowed, + Self::Thread, + Self::Lent, + Self::Outlived, + ]; + + /// What broke, as a binding's error says it after the function's name. + pub fn message(self) -> &'static str { + self.c_message().to_str().expect("ASCII") + } + + fn c_message(self) -> &'static std::ffi::CStr { + match self { + Self::Moved => c"a handle's value was moved out", + Self::Exclusive => c"a handle is borrowed exclusively", + Self::Borrowed => c"a handle is already borrowed", + Self::Thread => c"a handle is borrowed on another thread", + Self::Lent => c"a handle a call lent is borrowed exclusively or moved out", + Self::Outlived => { + c"a handle lent from another outlived it: the other was borrowed exclusively, moved out, or freed" + } + } + } + + /// The message of the status numbered `n`, for `_status_message`. + pub fn text(n: i32) -> &'static std::ffi::CStr { + Self::of(n).map_or(c"an unknown status", Self::c_message) + } + + /// The status numbered `n`, as a function wrote it. + pub fn of(n: i32) -> Option { + Self::ALL.get(usize::try_from(n).ok()?.checked_sub(1)?).copied() + } + + /// Abort, naming the function `what`, where no caller can take the status. + #[cold] + pub fn fail(self, what: &str) -> ! { + fail(what, self.message()) + } +} + +/// The value of a check, or an abort naming `what` where no caller can take its status. +pub fn check(checked: Result, what: &str) -> T { + checked.unwrap_or_else(|status| status.fail(what)) +} + +/// Fail a C function with `status`: write it through the function's `out`, returning a +/// zeroed value the caller ignores, or abort naming `what` if `out` is null, as a C caller +/// that takes no status passes. +/// +/// # Safety +/// `out` is null or points where an `int32_t` goes, and a zeroed `R` is a valid value. +#[cold] +pub unsafe fn failed(out: *mut i32, status: Status, what: &str) -> R { + if out.is_null() { + status.fail(what); + } + // SAFETY: the caller's contract. + unsafe { + out.write(status as i32); + std::mem::zeroed() + } +} + /// `s` for as long as the program runs, for a parameter that must be `'static`: each /// distinct string is leaked once and lent to every later call with equal content, so the /// strings held grow only with the distinct ones passed. @@ -703,10 +794,12 @@ pub fn char(code: u32, what: &str) -> char { const EXCLUSIVE: usize = usize::MAX; const TAKEN: usize = usize::MAX - 1; +const RESERVED: usize = usize::MAX - 2; /// A value behind a handle: shared through an `Arc`, and borrowed as Rust borrows it, with /// the borrow checked at run time. `state` counts shared borrows, or is [`EXCLUSIVE`] -/// while one exclusive borrow lasts, or [`TAKEN`] once the value moved out. A value that is +/// while one exclusive borrow lasts, [`RESERVED`] while a call holds it to move out once its +/// checks pass, or [`TAKEN`] once the value moved out. A value that is /// not `Sync` is shared on one thread at a time instead: `state` is that thread's id, and /// `depth` counts its borrows, which only that thread touches. pub struct Object { @@ -715,8 +808,8 @@ pub struct Object { /// The value a lent handle points at, where Rust lent it, or null if it owns `value`. lent: *mut T, value: UnsafeCell>, - /// Bumped each time the value is borrowed exclusively or moved out, or the handle is - /// freed, so no handle lent from the value outlives any of them. + /// Bumped each time the value is changed through an exclusive borrow or moved out, or + /// the handle is freed, so no handle lent from the value outlives any of them. epoch: AtomicUsize, /// For a handle a call returned lent from another's value, that handle. lender: Option, @@ -734,24 +827,22 @@ pub struct Lender { /// A handle another is lent from. trait Lends: Send + Sync { /// Borrow the value shared for a use of a handle lent from it, if still at `epoch`. - fn hold(&self, epoch: usize, local: bool, what: &str); + fn hold(&self, epoch: usize, local: bool) -> Result<(), Status>; /// End what [`Self::hold`] took. fn unhold(&self, local: bool); } impl Lends for Object { - fn hold(&self, epoch: usize, local: bool, what: &str) { + fn hold(&self, epoch: usize, local: bool) -> Result<(), Status> { match local { - true => self.acquire_local(what), - false => self.acquire(what), + true => self.acquire_local()?, + false => self.acquire()?, } if self.epoch.load(Ordering::Acquire) != epoch { self.unhold(local); - fail( - what, - "a handle lent from another outlived it: the other was borrowed exclusively, moved out, or freed", - ); + return Err(Status::Outlived); } + Ok(()) } fn unhold(&self, local: bool) { @@ -799,7 +890,7 @@ impl Object { /// A handle to `value`, which a call returned lent from `lender`'s value: shared, as a /// returned `&T` is, and borrowed as its own type is, until that value is borrowed - /// exclusively, moved out, or freed, after which a use aborts. It is never borrowed + /// exclusively, moved out, or freed, after which a use fails. It is never borrowed /// exclusively nor moved out itself. pub fn lent(value: &T, lender: Lender) -> *const c_void { Arc::into_raw(Arc::new(Self { @@ -867,12 +958,12 @@ impl Object { unsafe { &*handle.cast::() } } - fn acquire(&self, what: &str) { + fn acquire(&self) -> Result<(), Status> { let mut state = self.state.load(Ordering::Relaxed); loop { match state { - TAKEN => fail(what, "a handle's value was moved out"), - EXCLUSIVE => fail(what, "a handle is borrowed exclusively"), + TAKEN => return Err(Status::Moved), + EXCLUSIVE | RESERVED => return Err(Status::Exclusive), _ => {} } match self @@ -883,7 +974,9 @@ impl Object { Err(now) => state = now, } } - self.hold_lender(what); + self.hold_lender().inspect_err(|_| { + self.state.fetch_sub(1, Ordering::Release); + }) } /// End a shared borrow [`Self::acquire`] took. @@ -894,23 +987,28 @@ impl Object { /// Borrow a value that is not `Sync` shared: any number of times on one thread, as /// Rust allows, and on no other thread meanwhile. - fn acquire_local(&self, what: &str) { + fn acquire_local(&self) -> Result<(), Status> { let me = thread(); match self.state.compare_exchange(0, me, Ordering::Acquire, Ordering::Relaxed) { Ok(_) => {} Err(owner) if owner == me => {} - Err(TAKEN) => fail(what, "a handle's value was moved out"), - Err(EXCLUSIVE) => fail(what, "a handle is borrowed exclusively"), - Err(_) => fail(what, "a handle is borrowed on another thread"), + Err(TAKEN) => return Err(Status::Moved), + Err(EXCLUSIVE | RESERVED) => return Err(Status::Exclusive), + Err(_) => return Err(Status::Thread), } // SAFETY: only the thread in `state` touches `depth`. unsafe { *self.depth.get() += 1 }; - self.hold_lender(what); + self.hold_lender().inspect_err(|_| self.unlocal()) } /// End a shared borrow [`Self::acquire_local`] took, on its thread. fn release_local(&self) { self.release_lender(); + self.unlocal(); + } + + /// Count down this thread's borrows, letting go of the value at the last. + fn unlocal(&self) { // SAFETY: this thread is the one in `state`, so `depth` is its own. let depth = unsafe { &mut *self.depth.get() }; *depth -= 1; @@ -920,9 +1018,10 @@ impl Object { } /// For a handle lent from another, hold that one for a use of this one. - fn hold_lender(&self, what: &str) { - if let Some(lender) = &self.lender { - lender.handle.hold(lender.epoch, lender.local, what); + fn hold_lender(&self) -> Result<(), Status> { + match &self.lender { + Some(lender) => lender.handle.hold(lender.epoch, lender.local), + None => Ok(()), } } @@ -932,34 +1031,44 @@ impl Object { } } - fn acquire_mut(&self, what: &str) { - if self.lender.is_some() { - fail(what, "a handle a call lent shared is borrowed exclusively"); + /// Take `state` from 0 to `to`, as an exclusive borrow or a move does. + fn acquire_from_zero(&self, to: usize) -> Result<(), Status> { + if self.lender.is_some() || (matches!(to, TAKEN | RESERVED) && !self.lent.is_null()) { + return Err(Status::Lent); } - if let Err(state) = self - .state - .compare_exchange(0, EXCLUSIVE, Ordering::Acquire, Ordering::Relaxed) - { - match state { - TAKEN => fail(what, "a handle's value was moved out"), - _ => fail(what, "a handle is already borrowed"), - } + match self.state.compare_exchange(0, to, Ordering::Acquire, Ordering::Relaxed) { + Ok(_) => Ok(()), + Err(TAKEN) => Err(Status::Moved), + Err(_) => Err(Status::Borrowed), } - self.epoch.fetch_add(1, Ordering::Release); } /// Borrow the value shared for a call. /// /// # Safety /// `handle` is a live handle of this type, kept alive through the borrow. - pub unsafe fn borrow<'a>(handle: *const c_void, what: &str) -> Shared<'a, T> + pub unsafe fn borrow<'a>(handle: *const c_void, what: &str) -> Result, Status> where T: Sync, { // SAFETY: the caller's contract. let object = unsafe { Self::get(handle, what) }; - object.acquire(what); - Shared { object } + object.acquire()?; + Ok(Shared { object }) + } + + /// Borrow the value shared for a call, uncounted: no export borrows a value of its type + /// exclusively, moves it out, or lends it, and it is `Sync`, so no borrow can conflict + /// with this one, which costs no atomic operation. + /// + /// # Safety + /// As [`Self::borrow`], for such a type alone. + pub unsafe fn peek<'a>(handle: *const c_void, what: &str) -> Peek<'a, T> + where + T: Sync, + { + // SAFETY: the caller's contract. + Peek(unsafe { &*Self::get(handle, what).value() }) } /// Borrow a value that is not `Sync` shared for a call: any number of times on one @@ -967,47 +1076,54 @@ impl Object { /// /// # Safety /// As [`Self::borrow`]. - pub unsafe fn borrow_local<'a>(handle: *const c_void, what: &str) -> Local<'a, T> { + pub unsafe fn borrow_local<'a>(handle: *const c_void, what: &str) -> Result, Status> { // SAFETY: the caller's contract. let object = unsafe { Self::get(handle, what) }; - object.acquire_local(what); - Local { object } + object.acquire_local()?; + Ok(Local { object }) } /// Borrow the value exclusively for a call. /// /// # Safety /// As [`Self::borrow`]. - pub unsafe fn borrow_mut<'a>(handle: *const c_void, what: &str) -> Exclusive<'a, T> { + pub unsafe fn borrow_mut<'a>(handle: *const c_void, what: &str) -> Result, Status> { // SAFETY: the caller's contract. let object = unsafe { Self::get(handle, what) }; - object.acquire_mut(what); - Exclusive { object } + object.acquire_from_zero(EXCLUSIVE)?; + Ok(Exclusive { object, changed: false }) } /// Borrow the value shared for as long as a future runs, keeping the handle alive. /// /// # Safety /// `handle` is a live handle of this type. - pub unsafe fn borrow_arc(handle: *const c_void, what: &str) -> SharedArc + pub unsafe fn borrow_arc(handle: *const c_void, what: &str) -> Result, Status> where T: Sync, { // SAFETY: the caller's contract. - let object = unsafe { Self::arc(handle, what) }; - object.acquire(what); - SharedArc { object } + let object = unsafe { Self::get(handle, what) }; + object.acquire()?; + // SAFETY: as above. + Ok(SharedArc { + object: unsafe { Self::arc(handle, what) }, + }) } /// Borrow the value exclusively for as long as a future runs. /// /// # Safety /// As [`Self::borrow_arc`]. - pub unsafe fn borrow_mut_arc(handle: *const c_void, what: &str) -> ExclusiveArc { + pub unsafe fn borrow_mut_arc(handle: *const c_void, what: &str) -> Result, Status> { // SAFETY: the caller's contract. - let object = unsafe { Self::arc(handle, what) }; - object.acquire_mut(what); - ExclusiveArc { object } + let object = unsafe { Self::get(handle, what) }; + object.acquire_from_zero(EXCLUSIVE)?; + // SAFETY: as above. + Ok(ExclusiveArc { + object: unsafe { Self::arc(handle, what) }, + changed: false, + }) } /// # Safety @@ -1065,8 +1181,8 @@ impl Object { result } - /// Whether a call or a future borrows the value, so moving it out would abort. A value - /// moved out is not, so the move refuses it, naming itself. + /// Whether a call or a future borrows the value, so moving it out would fail. A value + /// moved out is not, so the move fails as moved. /// /// # Safety /// As [`Self::borrow`]. @@ -1080,26 +1196,124 @@ impl Object { /// /// # Safety /// As [`Self::borrow`]. - pub unsafe fn take(handle: *const c_void, what: &str) -> T { + pub unsafe fn take(handle: *const c_void, what: &str) -> Result { + // SAFETY: the caller's contract. + unsafe { Self::get(handle, what) }.acquire_from_zero(TAKEN)?; + // SAFETY: as above, and the value is reserved. + Ok(unsafe { Self::taken(handle) }) + } + + /// Reserve the value to move out once every other check of the call passed, which + /// [`Self::taken`] then does: `takes` gives it back if the call fails first. + /// + /// # Safety + /// As [`Self::borrow`], kept alive until `takes` commits or drops. + pub unsafe fn reserve<'a>(handle: *const c_void, takes: &mut Takes<'a>, what: &str) -> Result<(), Status> { // SAFETY: the caller's contract. let object = unsafe { Self::get(handle, what) }; - if !object.lent.is_null() { - fail(what, "a lent handle's value cannot move out"); - } - if let Err(state) = object - .state - .compare_exchange(0, TAKEN, Ordering::Acquire, Ordering::Relaxed) - { - match state { - TAKEN => fail(what, "a handle's value was moved out"), - _ => fail(what, "a handle is borrowed"), - } - } + object.acquire_from_zero(RESERVED)?; + // SAFETY: the state outlives `takes`, by the caller's contract; a pointer to it, + // so the value's type need not. + takes.push(unsafe { &*::std::ptr::addr_of!(object.state) }); + Ok(()) + } + + /// Borrow a `Copy` value shared until `takes` drops, so [`Self::held`] copies it out + /// once the call's moves commit, and no check after them can fail. + /// + /// # Safety + /// As [`Self::reserve`]. + pub unsafe fn hold<'a>(handle: *const c_void, takes: &mut Takes<'a>, what: &str) -> Result<(), Status> + where + T: Sync, + { + // SAFETY: the caller's contract. + let object = unsafe { Self::get(handle, what) }; + object.acquire()?; + takes.held.push((handle, Self::unhold)); + Ok(()) + } + + /// [`Self::hold`], for a value that is not `Sync`. + /// + /// # Safety + /// As [`Self::reserve`]. + pub unsafe fn hold_local<'a>(handle: *const c_void, takes: &mut Takes<'a>, what: &str) -> Result<(), Status> { + // SAFETY: the caller's contract. + let object = unsafe { Self::get(handle, what) }; + object.acquire_local()?; + takes.held.push((handle, Self::unhold_local)); + Ok(()) + } + + /// # Safety + /// [`Self::hold`] borrowed `handle`'s value, and this ends that borrow. + unsafe fn unhold(handle: *const c_void) { + // SAFETY: the caller's contract. + unsafe { &*handle.cast::() }.release(); + } + + /// # Safety + /// [`Self::hold_local`] borrowed `handle`'s value, and this ends that borrow. + unsafe fn unhold_local(handle: *const c_void) { + // SAFETY: the caller's contract. + unsafe { &*handle.cast::() }.release_local(); + } + + /// A copy of the value [`Self::hold`] or [`Self::hold_local`] borrowed. + /// + /// # Safety + /// One of them borrowed `handle`'s value into a `Takes` not yet dropped. + pub unsafe fn held(handle: *const c_void) -> T + where + T: Copy, + { + // SAFETY: the caller's contract: the borrow excludes every write. + unsafe { *(*handle.cast::()).value() } + } + + /// Move out the value [`Self::take`] took, or [`Self::reserve`] reserved for a call whose + /// `Takes` committed, leaving the handle to be freed. + /// + /// # Safety + /// One of them did so for `handle`'s value, and no other read moves it out. + pub unsafe fn taken(handle: *const c_void) -> T { + // SAFETY: the caller's contract. + let object = unsafe { &*handle.cast::() }; object.epoch.fetch_add(1, Ordering::Release); // SAFETY: TAKEN excludes every other access, and the value is never read again. unsafe { (*object.value.get()).assume_init_read() } } + /// Drop the value now, as a move out would, leaving the handle to be freed: what a + /// binding's close does, so a later use fails as moved, as Rust refuses a use after a + /// drop. It fails, changing nothing, while a call or future borrows the value or a call + /// reserved it to move out, as Rust refuses a drop under a borrow, and does nothing once + /// the value is gone. A handle a + /// call returned lent from another's value only stops lending it; one lent to C for a + /// call fails, as Rust lent it. + /// + /// # Safety + /// As [`Self::borrow`]. + pub unsafe fn close(handle: *const c_void, what: &str) -> Result<(), Status> { + // SAFETY: the caller's contract. + let object = unsafe { Self::get(handle, what) }; + if !object.lent.is_null() && object.lender.is_none() { + return Err(Status::Lent); + } + match object.state.compare_exchange(0, TAKEN, Ordering::Acquire, Ordering::Relaxed) { + Ok(_) => {} + Err(TAKEN) => return Ok(()), + Err(_) => return Err(Status::Borrowed), + } + object.epoch.fetch_add(1, Ordering::Release); + if object.lent.is_null() { + // SAFETY: TAKEN excludes every other access, and `Drop` skips a taken value. + unsafe { (*object.value.get()).assume_init_drop() }; + } + Ok(()) + } + /// The value of a handle a closure C implements returned Rust, which copies it out and /// frees the handle. /// @@ -1110,7 +1324,7 @@ impl Object { T: Copy, { // SAFETY: the caller's contract. - let value = *unsafe { Self::borrow_local(handle, what) }; + let value = *check(unsafe { Self::borrow_local(handle, what) }, what); // SAFETY: as above; the borrow ended. unsafe { Self::free(handle) }; value @@ -1140,6 +1354,67 @@ impl Drop for Object { } } +/// The values a call reserved to move out, given back unless it commits: so a call whose +/// later check fails moved nothing. A call's first few allocate nothing. It also holds the +/// `Copy` values the call copies out borrowed until it drops, after they are copied. +#[derive(Default)] +pub struct Takes<'a> { + first: [Option<&'a AtomicUsize>; 2], + rest: Vec<&'a AtomicUsize>, + held: Vec<(*const c_void, unsafe fn(*const c_void))>, +} + +impl<'a> Takes<'a> { + fn push(&mut self, state: &'a AtomicUsize) { + match self.first.iter_mut().find(|slot| slot.is_none()) { + Some(slot) => *slot = Some(state), + None => self.rest.push(state), + } + } + + /// Mark every value reserved moved out, for the call to move out. + pub fn commit(&mut self) { + for state in self.first.iter_mut().filter_map(Option::take).chain(self.rest.drain(..)) { + state.store(TAKEN, Ordering::Release); + } + } +} + +impl Drop for Takes<'_> { + fn drop(&mut self) { + for state in self.first.iter().flatten().chain(&self.rest) { + state.store(0, Ordering::Release); + } + for (handle, unhold) in self.held.drain(..) { + // SAFETY: `hold` or `hold_local` borrowed it, for this to end. + unsafe { unhold(handle) }; + } + } +} + +/// Frees the buffers C handed Rust in arguments a call has not lifted yet, unless the call +/// lifts them: so a call a check refuses leaks none of what it was given. +pub struct Unlifted(Option); + +impl Unlifted { + pub fn new(release: F) -> Self { + Self(Some(release)) + } + + /// The call passed its checks and lifts the arguments, which then own the buffers. + pub fn lifted(mut self) { + self.0 = None; + } +} + +impl Drop for Unlifted { + fn drop(&mut self) { + if let Some(release) = self.0.take() { + release(); + } + } +} + pub struct Shared<'a, T> { object: &'a Object, } @@ -1158,6 +1433,16 @@ impl Drop for Shared<'_, T> { } } +/// [`Object::peek`]'s borrow, which ends with nothing to release. +pub struct Peek<'a, T>(&'a T); + +impl Deref for Peek<'_, T> { + type Target = T; + fn deref(&self) -> &T { + self.0 + } +} + pub struct Local<'a, T> { object: &'a Object, } @@ -1176,8 +1461,10 @@ impl Drop for Local<'_, T> { } } +/// An exclusive borrow, which once used to change the value ends every handle lent from it. pub struct Exclusive<'a, T> { object: &'a Object, + changed: bool, } impl Deref for Exclusive<'_, T> { @@ -1190,6 +1477,7 @@ impl Deref for Exclusive<'_, T> { impl DerefMut for Exclusive<'_, T> { fn deref_mut(&mut self) -> &mut T { + self.changed = true; // SAFETY: the exclusive borrow is held. unsafe { &mut *self.object.value() } } @@ -1197,6 +1485,9 @@ impl DerefMut for Exclusive<'_, T> { impl Drop for Exclusive<'_, T> { fn drop(&mut self) { + if self.changed { + self.object.epoch.fetch_add(1, Ordering::Release); + } self.object.state.store(0, Ordering::Release); } } @@ -1219,8 +1510,10 @@ impl Drop for SharedArc { } } +/// [`Exclusive`], for as long as a future runs. pub struct ExclusiveArc { object: Arc>, + changed: bool, } impl Deref for ExclusiveArc { @@ -1233,6 +1526,7 @@ impl Deref for ExclusiveArc { impl DerefMut for ExclusiveArc { fn deref_mut(&mut self) -> &mut T { + self.changed = true; // SAFETY: the exclusive borrow is held. unsafe { &mut *self.object.value() } } @@ -1240,6 +1534,9 @@ impl DerefMut for ExclusiveArc { impl Drop for ExclusiveArc { fn drop(&mut self) { + if self.changed { + self.object.epoch.fetch_add(1, Ordering::Release); + } self.object.state.store(0, Ordering::Release); } } diff --git a/ffi/runtime/rt.swift b/ffi/runtime/rt.swift index 02a8ae7d4..c5955db73 100644 --- a/ffi/runtime/rt.swift +++ b/ffi/runtime/rt.swift @@ -118,6 +118,18 @@ public struct U128: Hashable, Comparable, Sendable, CustomStringConvertible { } } +/// A call Rust's rules refuse, thrown before any Rust runs: a handle borrowed in a way +/// the borrow checker forbids beside another call's, used once its value moved out or its +/// lend ended, or an item called on a library whose target lacks it. +public struct CheckError: Swift.Error, CustomStringConvertible, Sendable { + /// The function called, by its C name. + public let function: Swift.String + /// What broke. + public let message: Swift.String + + public var description: Swift.String { "\(function): \(message)" } +} + /// Where a seek counts its offset from, as swift-system's `FileDescriptor.SeekOrigin` and /// Rust's `SeekFrom` do. public enum SeekOrigin: Sendable { @@ -537,14 +549,22 @@ extension Native { } func hold() -> OpaquePointer? { + guard held() else { + fatalError("\(type) is closed") + } + return raw + } + + /// Hold it for a call, unless it is closed. + func held() -> Bool { var n = load(state) while true { if n & Handle.closed != 0 { - fatalError("\(type) is closed") + return false } let seen = swap(state, n, n + Handle.held) if seen == n { - return raw + return true } n = seen } @@ -564,6 +584,17 @@ extension Native { return try body(raw) } + /// Run `body` with the raw handle, held until it returns, or throw for a call of + /// `what` if it is closed. + @inline(__always) + func with(_ what: StaticString, _ body: (OpaquePointer?) throws -> R) throws -> R { + guard held() else { + throw CheckError(function: "\(what)", message: "\(type) is closed") + } + defer { release() } + return try body(raw) + } + func close() { var n = load(state) while n & Handle.closed == 0 { @@ -602,11 +633,29 @@ extension Native { return try handle.with(body) } - /// What a call lowers nested values into, kept until it returns: copied bytes, arrays, - /// and the handles it holds. + /// As `with`, throwing for a call of `what` if `handle` is closed. + @inline(__always) + static func with( + _ what: StaticString, _ handle: Handle?, _ body: (OpaquePointer?) throws -> R + ) throws -> R { + guard let handle else { return try body(nil) } + return try handle.with(what, body) + } + + /// What a call of `what` lowers nested values into, kept until it returns: copied + /// bytes, arrays, and the handles it holds. final class Arena { private var blocks: [UnsafeMutableRawPointer] = [] private var held: [Handle] = [] + /// Each buffer given Rust to take, which the call frees unless it reached C. + private var given: [Buf] = [] + private var reached = false + private let what: StaticString + + init(_ what: StaticString) { + self.what = what + } + /// Whether bytes lowered now are passed by value, which Rust takes: set once the /// receiver, which Rust only reads, is lowered, and unset inside an array. var take = false @@ -626,8 +675,8 @@ extension Native { func buf(_ bytes: Bytes) -> Buf { bytes.storage.withUnsafeBytes { raw in guard take else { return copy(raw) } - if let owner = bytes.owner, raw.count > 0 { return owner.share(raw) } - return Native.copy(view(raw)) + if let owner = bytes.owner, raw.count > 0 { return give(owner.share(raw)) } + return give(Native.copy(view(raw))) } } @@ -635,10 +684,23 @@ extension Native { var text = text return text.withUTF8 { utf8 in guard take else { return copy(UnsafeRawBufferPointer(utf8)) } - return Native.copy(view(UnsafeRawBufferPointer(utf8))) + return give(Native.copy(view(UnsafeRawBufferPointer(utf8)))) } } + private func give(_ buf: Buf) -> Buf { + given.append(buf) + return buf + } + + /// `result`, what the call's C function returned: it ran, so Rust owns every buffer + /// given, taken or freed as the call was refused. + @inline(__always) + func ran(_ result: T) -> T { + reached = true + return result + } + func buf(_ bytes: Bytes?) -> Buf { bytes.map { buf($0) } ?? none } @@ -647,6 +709,7 @@ extension Native { text.map { buf($0) } ?? none } + /// The handle of a `Copy` object, which nothing closes, held until the call returns. func hold(_ handle: Handle) -> OpaquePointer? { let raw = handle.hold() held.append(handle) @@ -657,6 +720,20 @@ extension Native { handle.map { hold($0) } ?? nil } + /// The handle of an object a move may have closed, held until the call returns, or + /// a throw for the call if it is closed. + func held(_ handle: Handle) throws -> OpaquePointer? { + guard handle.held() else { + throw CheckError(function: "\(what)", message: "\(handle.type) is closed") + } + held.append(handle) + return handle.raw + } + + func held(_ handle: Handle?) throws -> OpaquePointer? { + try handle.map { try held($0) } ?? nil + } + /// `items` as a C array, each lowered by `lower`: never null, so an empty one is /// told apart from a missing one. func list(_ items: [T], _ lower: (T) throws -> C) rethrows -> (UnsafePointer, Int) { @@ -696,11 +773,17 @@ extension Native { return (UnsafePointer(array), items.count) } - /// Release what the call held, once it returns. + /// Release what the call held, once it returns, and, if a lowering threw before the + /// call reached C, each buffer given Rust. func finish() { for handle in held { handle.release() } + if !reached { + for buf in given { + Native.free(buf) + } + } for block in blocks { block.deallocate() } diff --git a/ffi/src/abi.rs b/ffi/src/abi.rs index 0da323c3c..cce3cb111 100644 --- a/ffi/src/abi.rs +++ b/ffi/src/abi.rs @@ -16,7 +16,7 @@ use std::collections::{BTreeMap, HashMap}; use rustc_span::Span; -use crate::ir::{Access, Api, Closure, Dyn, Function, Home, Int, Literal, On, Receiver, Type}; +use crate::ir::{Access, Api, Closure, Dyn, Function, Home, Int, Literal, Mutability, On, Receiver, Type}; /// A C name one item claims twice, which no rename can tell apart: the name, then what /// claimed it first and second. @@ -88,6 +88,9 @@ pub enum Ty { ForeignReader, /// `_poll` Poll, + /// `_status`, an `int32_t`: why a call failed a check of a handle, which a + /// function that can fail one writes through its last parameter. + Status, /// A pointer the callee writes a result through. Out(Box), /// A pointer the callee only reads through: a value's receiver. @@ -107,6 +110,21 @@ pub struct Sig { pub ret: Ty, } +impl Sig { + /// The `status` parameter it takes last, if a call can fail a check of a handle. + pub fn status(&self) -> Option<&str> { + self.params + .last() + .filter(|(_, ty)| is_status(ty)) + .map(|(name, _)| name.as_str()) + } +} + +/// Whether `ty` is a `status` parameter's. +pub fn is_status(ty: &Ty) -> bool { + matches!(ty, Ty::Out(inner) if **inner == Ty::Status) +} + /// The C side of an exported function. pub struct Export { /// The function itself: for a future, the call that makes it. @@ -130,6 +148,8 @@ pub struct Handed { unit: bool, /// Whether it returns a tuple, which the C function writes through its outs. tuple: bool, + /// Whether both take a status last, as a call that can fail a check does. + status: bool, } impl Handed { @@ -137,14 +157,19 @@ impl Handed { /// in place on a copy of the value that `args[0]` points to and handing the copy back. /// `args` are of that export's C signature: `self`, what both take after it, then the outs the value and /// each element Rust returns are written through, which the C function takes from the - /// one after the value's. + /// one after the value's, and last the status, if both take one. pub fn call(&self, args: &[String]) -> String { let lead = self.lead; + let (args, status) = match self.status { + true => args.split_at(args.len() - 1), + false => (args, &[][..]), + }; let mut real = vec!["&mut handed_".to_string()]; real.extend(args[1..=lead].iter().cloned()); if self.tuple { real.extend(args[lead + 2..].iter().cloned()); } + real.extend(status.iter().cloned()); let call = format!("crate::{}({})", self.name, real.join(", ")); // Apart from the names the glue gives its arguments, which are numbered. let body = match (self.unit, self.tuple) { @@ -171,6 +196,76 @@ pub struct Future { pub free: Sig, } +/// Each `_status` a call can fail with, numbered from 1, and what it means, as +/// the runtime's `Status` names them. +pub const STATUSES: [(&str, &str); 6] = [ + ("moved", "the handle's value was moved out"), + ("exclusive", "another call or future borrows the value exclusively"), + ("borrowed", "another call or future borrows the value"), + ("thread", "a value that is not Sync is borrowed on another thread"), + ("lent", "a handle a call lent is borrowed exclusively or moved out"), + ("outlived", "a handle lent from another outlived it"), +]; + +/// Whether a call can fail a check of each object's handle, by Rust's rules alone: its +/// value is not `Sync`, so a borrow on another thread fails; an export borrows it +/// exclusively or moves it out, so a borrow beside another call's fails; or a handle to +/// it is lent, by a call returning a borrow or to a closure or trait for a call, so a use +/// past the lend fails, as may a trait's method returning it. Every other object's handles +/// are only ever borrowed shared, which never fails, so a call borrows them uncounted. +fn checked(api: &Api) -> Vec { + /// Mark each object a parameter of type `ty` borrows exclusively, moves, or lends, + /// `owned` when `ty` is passed by value rather than behind a shared reference. + fn param(api: &Api, ty: &Type, owned: bool, checked: &mut [bool]) { + match ty { + Type::Object(i) if owned && !api.objects[*i].copy => checked[*i] = true, + Type::Ref(Mutability::Mut, inner) => { + if let Type::Object(i) = inner.base() { + checked[*i] = true; + } + param(api, inner, true, checked); + } + Type::Ref(Mutability::Not, inner) => param(api, inner, false, checked), + Type::Option(inner) | Type::Box(inner) | Type::List(_, inner) => param(api, inner, owned, checked), + Type::Tuple(elements) => elements.iter().for_each(|ty| param(api, ty, owned, checked)), + Type::Closure(closure) => closure.params.iter().for_each(|ty| lent(api, ty, checked)), + // A record or data enum passed by value moves each object it holds. + Type::Record(_) | Type::Enum(_) if owned => lent(api, ty, checked), + _ => {} + } + } + /// Mark each object a value of `ty` Rust lends the foreign side holds. + fn lent(api: &Api, ty: &Type, checked: &mut [bool]) { + let mut handles = Vec::new(); + api.handles(ty, &mut handles, &mut Vec::new()); + handles.into_iter().for_each(|i| checked[i] = true); + } + let mut checked: Vec = api.objects.iter().map(|o| !o.sync).collect(); + let functions = (api.records.iter().flat_map(|r| &r.methods)) + .chain(api.enums.iter().flat_map(|e| &e.methods)) + .chain(&api.functions) + .map(|f| (None, f)) + .chain((api.objects.iter().enumerate()).flat_map(|(i, o)| o.methods.iter().map(move |f| (Some(i), f)))); + for (owner, f) in functions { + match (owner, f.receiver) { + (Some(i), Some(Receiver::Mut)) => checked[i] = true, + (Some(i), Some(Receiver::Owned)) if !api.objects[i].copy => checked[i] = true, + _ => {} + } + f.params.iter().for_each(|p| param(api, &p.ty, true, &mut checked)); + if f.lends { + lent(api, &f.ok, &mut checked); + } + } + // What an implementation is lent, and what it hands Rust, which moves it out. + for method in api.callbacks.iter().flat_map(|c| &c.methods) { + method.params.iter().for_each(|p| lent(api, &p.ty, &mut checked)); + lent(api, &method.ok, &mut checked); + method.err.iter().for_each(|err| lent(api, err, &mut checked)); + } + checked +} + /// `ty` as a clone of each borrow it holds. fn owned(ty: &Type) -> Type { match ty { @@ -205,6 +300,8 @@ pub struct Abi<'a> { pub tuples: BTreeMap>, /// Each `[u8; N]` struct by C name, with `N`. pub arrays: BTreeMap, + /// Whether a call can fail a check of each object's handle: see [`checked`]. + pub checked: Vec, /// Every C name claimed. claims: Vec, /// The C name of each item whose own clashed. @@ -224,6 +321,7 @@ impl<'a> Abi<'a> { lists: BTreeMap::new(), tuples: BTreeMap::new(), arrays: BTreeMap::new(), + checked: checked(api), claims: Vec::new(), renames: HashMap::new(), clashes: Vec::new(), @@ -820,6 +918,13 @@ impl<'a> Abi<'a> { if let Some(err) = &function.err { outs.push(("err".to_string(), Ty::Out(Box::new(self.returned(err))))); } + let status = self.status(function, owner).then(|| { + let mut status = "status".to_string(); + while function.params.iter().any(|p| escape(&p.name) == status) { + status.push('_'); + } + (status, Ty::Out(Box::new(Ty::Status))) + }); if function.future { let handle = format!("{name}_future"); let mut poll_params = vec![ @@ -827,9 +932,12 @@ impl<'a> Abi<'a> { ("waker".to_string(), Ty::Waker), ]; poll_params.extend(outs.iter().cloned()); + // The status trails each, as every function's does. let mut start_params = params.clone(); start_params.extend(outs); start_params.push(("future".to_string(), Ty::Out(Box::new(Ty::Future(handle.clone()))))); + start_params.extend(status.clone()); + params.extend(status); let start = Sig { name: format!("{name}_start"), params: start_params, @@ -874,6 +982,7 @@ impl<'a> Abi<'a> { self.returned(ok) } }; + params.extend(status); Export { call: Sig { name, params, ret }, future: None, @@ -881,6 +990,53 @@ impl<'a> Abi<'a> { } } + /// Whether a call of `function`, a method of `owner` if it has one, can fail a check of + /// a handle it borrows or moves, so it takes a `status` to write why. + pub fn status(&self, function: &Function, owner: Option<&Ty>) -> bool { + let mut handles = Vec::new(); + let mut seen = Vec::new(); + match (function.receiver, owner) { + (Some(_), Some(Ty::Handle { name, .. })) => { + handles.extend((0..self.api.objects.len()).find(|&i| &self.object(i) == name)) + } + // A record's or enum's receiver lifts each handle it holds. + (Some(_), Some(owner)) => { + let records = (0..self.api.records.len()).map(Type::Record); + let enums = (0..self.api.enums.len()).map(Type::Enum); + if let Some(ty) = records.chain(enums).find(|ty| &self.ty(ty) == owner) { + self.api.handles(&ty, &mut handles, &mut seen); + } + } + _ => {} + } + for param in &function.params { + self.api.handles(¶m.ty, &mut handles, &mut seen); + } + handles.iter().any(|&i| self.checked[i]) + } + + /// Whether `_clone`, `_eq`, `_to_string`, `_debug`, and `_copy` of object `i` take a + /// `status`, as each borrows its handles shared. + pub fn traits_status(&self, i: usize) -> bool { + self.checked[i] + } + + /// Whether a record's or enum's `_eq`, `_to_string`, and `_debug` take a `status`, + /// as each borrows the handles it holds shared. + pub fn value_status(&self, ty: &Type) -> bool { + let mut handles = Vec::new(); + self.api.handles(ty, &mut handles, &mut Vec::new()); + handles.iter().any(|&i| self.checked[i]) + } + + /// Whether the std trait functions of `ty`, an object, record, or enum, take a `status`. + pub fn traits_status_of(&self, ty: &Type) -> bool { + match ty { + Type::Object(i) => self.traits_status(*i), + ty => self.value_status(ty), + } + } + /// The out pointer Rust writes back the value C passed for `&mut` as parameter `name` /// through, named apart from every parameter of `function`. pub fn written(function: &Function, name: &str) -> String { @@ -927,9 +1083,11 @@ impl<'a> Abi<'a> { Type::Tuple(elements) => elements.len(), _ => 0, }; + let status = usize::from(real.call.status().is_some()); let handed = Handed { + lead: real.call.params.len() - 1 - elements - status, + status: status == 1, name: real.call.name, - lead: real.call.params.len() - 1 - elements, unit: function.ok == Type::Unit, tuple: elements > 0, }; @@ -995,6 +1153,8 @@ impl<'a> Abi<'a> { for runtime in [ "contract", "target", + "status", + "status_message", "buf", "buf_free", "u128", @@ -1031,6 +1191,10 @@ impl<'a> Abi<'a> { let name = format!("{}_{constant}", self.prefix.to_uppercase()); self.claim(name, "the runtime".to_string(), crate_span, None); } + for (status, _) in STATUSES { + let name = format!("{}_STATUS_{}", self.prefix.to_uppercase(), status.to_uppercase()); + self.claim(name, "the runtime".to_string(), crate_span, None); + } let mut types: Vec<&Type> = Vec::new(); // Each function, with its type's C name and segments if it is a method. let mut functions: Vec<(&Function, Option, Vec)> = Vec::new(); @@ -1101,6 +1265,14 @@ impl<'a> Abi<'a> { object.span, Self::made(&item, &name), ); + if self.checked[i] { + self.claim( + format!("{name}_drop"), + what.clone(), + object.span, + Self::made(&item, &name), + ); + } if object.shared { self.claim( format!("{name}_borrowed"), diff --git a/ffi/src/c.rs b/ffi/src/c.rs index ea74e0601..ceebd927f 100644 --- a/ffi/src/c.rs +++ b/ffi/src/c.rs @@ -78,6 +78,12 @@ impl Header<'_> { self.line(format!( "/* The index of the library's target among those this header serves:{targets} */\nuint32_t {p}_target(void);\n" )); + let statuses: String = (crate::abi::STATUSES.iter().enumerate()) + .map(|(n, (status, _))| format!("\t{upper}_STATUS_{} = {},\n", status.to_uppercase(), n + 1)) + .collect(); + self.line(format!( + "/* Why a call failed a check of a handle, before it ran any Rust: a borrow or move\n * Rust's borrow checker would refuse. A function that can fail one takes a status\n * last, through which it writes the failure, returning a zeroed value to ignore;\n * passed null, it aborts with the message instead. */\ntypedef int32_t {p}_status;\nenum {{\n{statuses}}};\n\n/* What a status says broke. */\nconst char *{p}_status_message({p}_status status);\n" + )); self.line(format!( "/* Bytes. Returned, an owned buffer to free once with {p}_buf_free, or pass back to\n * Rust by value, ptr and len narrowed to any range of its bytes. Passed by value, one\n * whose owner is set is Rust's from then on, taken without a copy, and one whose owner\n * is NULL is a view Rust reads during the call and never frees; in a value passed by\n * pointer, or an array's item, either is only read. A null ptr is None. */\ntypedef struct {p}_buf {{\n\tconst uint8_t *ptr;\n\tsize_t len;\n\tvoid *owner;\n}} {p}_buf;\n\nvoid {p}_buf_free({p}_buf buf);\n\n/* An owned copy of view's bytes: what a closure or reader C implements returns Rust,\n * which then owns it. */\n{p}_buf {p}_buf_copy({p}_buf view);\n\n/* A second owned buffer of an owned buffer's bytes, sharing them without a copy: what\n * a caller passes Rust while keeping buf. */\n{p}_buf {p}_buf_clone({p}_buf buf);\n" )); @@ -205,7 +211,13 @@ impl Header<'_> { for (i, record) in api.records.iter().enumerate() { let name = abi.record(i); self.line(format!("/* {} */", abi.api.rust(&record.path, &record.args)).trim_end()); - self.value_functions(&name, Ty::Struct(name.clone()), record.traits, record.opaque.is_none()); + self.value_functions( + &name, + Ty::Struct(name.clone()), + record.traits, + record.opaque.is_none(), + abi.value_status(&Type::Record(i)), + ); let owner = Ty::Struct(name.clone()); for method in &record.methods { self.function(method, Some(&owner)); @@ -219,7 +231,13 @@ impl Header<'_> { continue; } self.line(format!("/* {} */", abi.api.rust(&e.path, &e.args))); - self.value_functions(&name, ty.clone(), e.traits, !e.plain()); + self.value_functions( + &name, + ty.clone(), + e.traits, + !e.plain(), + abi.value_status(&Type::Enum(i)), + ); for method in &e.methods { self.function(method, Some(&ty)); } @@ -230,21 +248,35 @@ impl Header<'_> { let docs = format!("{}\n\n{}", abi.api.rust(&object.path, &object.args), object.docs); self.line(comment(docs.trim(), "").trim_end()); self.line(format!("void {name}_free({name} *self);")); + if abi.checked[i] { + self.line( + comment( + "Drop the value now, as a move out would, leaving the handle to free: a later call fails as moved, and a second drop does nothing. Fails while a call or future borrows the value.", + "", + ) + .trim_end(), + ); + self.line(format!("void {name}_drop({name} *self, {p}_status *status);")); + } if object.shared { self.line(format!("bool {name}_borrowed(const {name} *self);")); } let t = object.traits; + let status = match abi.traits_status(i) { + true => format!(", {p}_status *status"), + false => String::new(), + }; if t.clone { - self.line(format!("{name} *{name}_clone(const {name} *self);")); + self.line(format!("{name} *{name}_clone(const {name} *self{status});")); } if t.eq { - self.line(format!("bool {name}_eq(const {name} *a, const {name} *b);")); + self.line(format!("bool {name}_eq(const {name} *a, const {name} *b{status});")); } if t.display { - self.line(format!("{p}_buf {name}_to_string(const {name} *self);")); + self.line(format!("{p}_buf {name}_to_string(const {name} *self{status});")); } if t.debug { - self.line(format!("{p}_buf {name}_debug(const {name} *self);")); + self.line(format!("{p}_buf {name}_debug(const {name} *self{status});")); } if t.default { self.line(format!("{name} *{name}_default(void);")); @@ -390,8 +422,12 @@ impl Header<'_> { } /// The functions a value type's std traits export, and its `free` if it owns memory. - fn value_functions(&mut self, name: &str, ty: Ty, traits: Traits, free: bool) { + fn value_functions(&mut self, name: &str, ty: Ty, traits: Traits, free: bool, status: bool) { let p = &self.abi.prefix; + let status = match status { + true => format!(", {p}_status *status"), + false => String::new(), + }; let this = self.decl(&Ty::In(Box::new(ty.clone())), "self"); let a = self.decl(&Ty::In(Box::new(ty.clone())), "a"); let b = self.decl(&Ty::In(Box::new(ty.clone())), "b"); @@ -399,13 +435,13 @@ impl Header<'_> { self.line(format!("void {name}_free({name} *self);")); } if traits.eq { - self.line(format!("bool {name}_eq({a}, {b});")); + self.line(format!("bool {name}_eq({a}, {b}{status});")); } if traits.display { - self.line(format!("{p}_buf {name}_to_string({this});")); + self.line(format!("{p}_buf {name}_to_string({this}{status});")); } if traits.debug { - self.line(format!("{p}_buf {name}_debug({this});")); + self.line(format!("{p}_buf {name}_debug({this}{status});")); } if traits.default { self.line(format!("{} {name}_default(void);", self.decl(&ty, ""))); @@ -531,6 +567,7 @@ pub fn decl(abi: &Abi<'_>, ty: &Ty, name: &str) -> String { Ty::ForeignFuture => space(&abi.rt("foreign_future")), Ty::ForeignReader => space(&abi.rt("foreign_reader")), Ty::Poll => space(&abi.rt("poll")), + Ty::Status => space(&abi.rt("status")), Ty::Out(inner) | Ty::InOut(inner) => decl(abi, inner, &format!("*{name}")), Ty::In(inner) => format!("const {}", decl(abi, inner, &format!("*{name}"))), Ty::Fn(params, ret) => { diff --git a/ffi/src/cpp.rs b/ffi/src/cpp.rs index 76b63be10..403b38932 100644 --- a/ffi/src/cpp.rs +++ b/ffi/src/cpp.rs @@ -298,6 +298,15 @@ impl<'a> Header<'a> { } } + /// The status a record's or enum's trait function takes, if a handle in it is checked: + /// null, so a failed check aborts, as each other call of C++'s does. + fn value_status(&self, ty: &Type) -> &'static str { + match self.abi.value_status(ty) { + true => ", nullptr", + false => "", + } + } + /// The arena a body lowering each of `tys` declares, if one needs it. fn scope<'t>(&self, tys: impl IntoIterator) -> String { match tys.into_iter().any(|ty| self.arena(ty)) { @@ -866,11 +875,22 @@ impl<'a> Header<'a> { let t = object.traits; let text = format!("{}\n\n{}", abi.api.rust(&object.path, &object.args), object.docs); let d = self.d(); + // A failed check of a trait's borrow aborts with its message, as C++ throws nothing. + let status = match abi.traits_status(i) { + true => ", nullptr", + false => "", + }; // Copies share a handle only where no call tells that apart from a clone, but by an // `Arc`'s or `Rc`'s count, which counts one for every copy. - let handle = match object.shared { - true => format!("{d}::shared<::{c}, &::{c}_free, &::{c}_clone, &::{c}_borrowed>"), - false => format!("{d}::unique<::{c}, &::{c}_free>"), + let handle = match (object.shared, status.is_empty()) { + (true, true) => format!("{d}::shared<::{c}, &::{c}_free, &::{c}_clone, &::{c}_borrowed>"), + (true, false) => { + self.line(format!( + "namespace detail {{\n\ninline ::{c} *{c}_clone(const ::{c} *raw) noexcept {{\n\treturn ::{c}_clone(raw, nullptr);\n}}\n\n}} // namespace detail\n" + )); + format!("{d}::shared<::{c}, &::{c}_free, &{d}::{c}_clone, &::{c}_borrowed>") + } + (false, _) => format!("{d}::unique<::{c}, &::{c}_free>"), }; let mut body = vec![ format!("\t{handle} raw_;\n"), @@ -892,17 +912,17 @@ impl<'a> Header<'a> { "\t/// Rust's `Clone`, a value of its own: copies sharing one value could be told apart\n\t/// from clones, so it does not copy.\n\tRUST2_FFI_CALLABLE {name} {clone}() const;" )); self.def(format!( - "inline {name} {name}::{clone}() const {{\n\treturn {d}::access::wrap<{name}>(::{c}_clone(raw_.get()));\n}}\n" + "inline {name} {name}::{clone}() const {{\n\treturn {d}::access::wrap<{name}>(::{c}_clone(raw_.get(){status}));\n}}\n" )); members.push(clone); } if t.eq { let (a, b) = self.operands(); body.push(format!( - "\tfriend bool operator==(const {name} &{a}, const {name} &{b}) {{\n\t\treturn ::{c}_eq({a}.raw_.get(), {b}.raw_.get());\n\t}}\n\tfriend bool operator!=(const {name} &{a}, const {name} &{b}) {{\n\t\treturn !({a} == {b});\n\t}}" + "\tfriend bool operator==(const {name} &{a}, const {name} &{b}) {{\n\t\treturn ::{c}_eq({a}.raw_.get(), {b}.raw_.get(){status});\n\t}}\n\tfriend bool operator!=(const {name} &{a}, const {name} &{b}) {{\n\t\treturn !({a} == {b});\n\t}}" )); } - self.traits(&name, &c, t, None, &mut body, &mut members); + self.traits(&name, &c, t, Ok(status), &mut body, &mut members); let owner = Ty::Handle { name: c.clone(), constant: false, @@ -922,18 +942,23 @@ impl<'a> Header<'a> { } /// The members a type's std traits export beyond its operators: an object's, which - /// holds a handle, or a record's, `value`, which is lowered. + /// holds a handle and passes each call `Ok` of its status argument, or a record's, of + /// type `Err`, which is lowered. fn traits( &mut self, name: &str, c: &str, t: Traits, - value: Option<&Type>, + of: Result<&str, &Type>, body: &mut Vec, members: &mut Vec, ) { let ns = self.ns.clone(); let d = self.d(); + let (value, status) = match of { + Ok(status) => (None, status), + Err(ty) => (Some(ty), self.value_status(ty)), + }; let object = value.is_none(); let callable = match object { true => "RUST2_FFI_CALLABLE ", @@ -950,7 +975,7 @@ impl<'a> Header<'a> { let member = apart("to_string", name); body.push(format!("\t{callable}::{ns}::buf {member}() const;")); self.def(format!( - "inline ::{ns}::buf {name}::{member}() const {{\n{lower}\treturn {d}::access::adopt(::{c}_to_string({this}));\n}}\n" + "inline ::{ns}::buf {name}::{member}() const {{\n{lower}\treturn {d}::access::adopt(::{c}_to_string({this}{status}));\n}}\n" )); members.push(member); } @@ -958,7 +983,7 @@ impl<'a> Header<'a> { let member = apart("debug", name); body.push(format!("\t{callable}::{ns}::buf {member}() const;")); self.def(format!( - "inline ::{ns}::buf {name}::{member}() const {{\n{lower}\treturn {d}::access::adopt(::{c}_debug({this}));\n}}\n" + "inline ::{ns}::buf {name}::{member}() const {{\n{lower}\treturn {d}::access::adopt(::{c}_debug({this}{status}));\n}}\n" )); members.push(member); } @@ -1110,13 +1135,14 @@ impl<'a> Header<'a> { "\tfriend bool operator==(const {name} &{a}, const {name} &{b});\n\tfriend bool operator!=(const {name} &{a}, const {name} &{b}) {{\n\t\treturn !({a} == {b});\n\t}}" )); self.def(format!( - "inline bool operator==(const {name} &{a}, const {name} &{b}) {{\n{}\tauto c_a_ = {};\n\tauto c_b_ = {};\n\treturn ::{c}_eq(&c_a_, &c_b_);\n}}\n", + "inline bool operator==(const {name} &{a}, const {name} &{b}) {{\n{}\tauto c_a_ = {};\n\tauto c_b_ = {};\n\treturn ::{c}_eq(&c_a_, &c_b_{});\n}}\n", self.scope([ty]), self.lower(ty, &a), self.lower(ty, &b), + self.value_status(ty), )); } - self.traits(&name, &c, t, Some(ty), &mut body, &mut members); + self.traits(&name, &c, t, Err(ty), &mut body, &mut members); let owner = Ty::Struct(c.clone()); let mut taken = fields.clone(); taken.push(name.clone()); @@ -1194,16 +1220,17 @@ impl<'a> Header<'a> { let (lower_a, lower_b, this) = (lower(&a), lower(&b), lower("self")); let scope = self.scope([&ty]); let t = e.traits; + let status = self.value_status(&ty); if t.eq { let (a_, b_) = (declare(¶m, &a), declare(¶m, &b)); self.def(format!( - "inline bool operator==({a_}, {b_}) {{\n{scope}\tauto c_a_ = {lower_a};\n\tauto c_b_ = {lower_b};\n\treturn ::{c}_eq(&c_a_, &c_b_);\n}}\n\ninline bool operator!=({a_}, {b_}) {{\n\treturn !({a} == {b});\n}}\n" + "inline bool operator==({a_}, {b_}) {{\n{scope}\tauto c_a_ = {lower_a};\n\tauto c_b_ = {lower_b};\n\treturn ::{c}_eq(&c_a_, &c_b_{status});\n}}\n\ninline bool operator!=({a_}, {b_}) {{\n\treturn !({a} == {b});\n}}\n" )); } for (on, trait_name) in [(t.display, "to_string"), (t.debug, "debug")] { if on { self.def(format!( - "inline ::{ns}::buf {name}_{trait_name}({}) {{\n{scope}\tauto c_self_ = {this};\n\treturn {d}::access::adopt(::{c}_{trait_name}(&c_self_));\n}}\n", + "inline ::{ns}::buf {name}_{trait_name}({}) {{\n{scope}\tauto c_self_ = {this};\n\treturn {d}::access::adopt(::{c}_{trait_name}(&c_self_{status}));\n}}\n", declare(¶m, "self"), d = self.d() )); @@ -1545,7 +1572,7 @@ impl<'a> Header<'a> { } } } - let outs: Vec<(String, Ty)> = c_params.cloned().collect(); + let outs: Vec<(String, Ty)> = c_params.filter(|(_, ty)| !abi::is_status(ty)).cloned().collect(); // Each value passed for `&mut` takes what Rust wrote back, on every path, as does a // value's own `&mut self`, which Rust wrote back in place. let mut writes: Vec = match export.call.params.first() { @@ -1566,6 +1593,10 @@ impl<'a> Header<'a> { for (name, _) in &outs { args.push(format!("&c_{name}_")); } + // A failed check aborts with its message, as C++ throws nothing. + if export.call.status().is_some() { + args.push("nullptr".to_string()); + } let call = format!("::{}({})", export.call.name, args.join(", ")); let mut body = prelude; if let Some(abi::Future { handle, .. }) = &export.future { diff --git a/ffi/src/dart.rs b/ffi/src/dart.rs index 92868cf2f..2bee9905e 100644 --- a/ffi/src/dart.rs +++ b/ffi/src/dart.rs @@ -27,7 +27,7 @@ use std::fmt::Write; use crate::abi::{self, Abi, Sig, Ty, pascal}; use crate::glue::rs; -use crate::ir::{Access, Closure, Family, Function, Int, Literal, On, Receiver, Type}; +use crate::ir::{Access, Closure, Family, Function, Int, Literal, On, Type}; use crate::kotlin::strip_refs; const RUNTIME: &str = include_str!("../runtime/rt.dart"); @@ -128,7 +128,9 @@ pub fn generate(abi: &Abi<'_>, packages: &str, contract: u64) -> Result>> _target) & 1 == 0) {{\n\t\tthrow UnsupportedError(\"$what: it is not on this library's target\");\n\t}}\n}}\n\n" + void _on(int on, String what) {{\n\tif ((on >>> _target) & 1 == 0) {{\n\t\tthrow UnsupportedError(\"$what: it is not on this library's target\");\n\t}}\n}}\n\n\ + /// Throws unless [status], which the call [what] wrote, is zero: a run-time check failed.\n\ + void _status(int status, String what) {{\n\tif (status == 0) return;\n\t_n.status.value = 0;\n\tfinal m = _statusMessage(status);\n\tvar n = 0;\n\twhile (m[n] != 0) n++;\n\tthrow StateError('$what: ${{const convert.Utf8Decoder().convert(m.asTypedList(n))}}');\n}}\n\n" ); for (name, params, ret, dart_params, dart_ret) in [ ("contract", "", "ffi.Uint64", "", "int"), @@ -229,6 +231,13 @@ pub fn generate(abi: &Abi<'_>, packages: &str, contract: u64) -> Result", + "int p0", + "ffi.Pointer", + ), ( "dart_reader_read", "ffi.Pointer, ffi.Pointer, ffi.Size, ffi.Pointer, ffi.Pointer<_Buf>", @@ -248,6 +257,7 @@ pub fn generate(abi: &Abi<'_>, packages: &str, contract: u64) -> Result format!("_{p}_{name}"), "buf_free" => "_bufFree".to_string(), "buf_copy" => "_bufCopy".to_string(), + "status_message" => "_statusMessage".to_string(), "buf_clone" => "_bufClone".to_string(), other => format!("_{}", camel(other)), }; @@ -594,6 +604,7 @@ impl<'a> Gen<'a> { Ty::F64 => "ffi.Double".to_string(), Ty::Char => "ffi.Uint32".to_string(), Ty::Enum(_) | Ty::Poll => "ffi.Int".to_string(), + Ty::Status => "ffi.Int32".to_string(), Ty::Handle { .. } | Ty::Future(_) | Ty::Data => "ffi.Pointer".to_string(), Ty::Out(inner) | Ty::In(inner) => format!("ffi.Pointer<{}>", self.nt(inner)), Ty::Fn(params, ret) => { @@ -615,7 +626,7 @@ impl<'a> Gen<'a> { match ty { Ty::Void => "void".to_string(), Ty::Bool => "bool".to_string(), - Ty::Int(_) | Ty::Char | Ty::Enum(_) | Ty::Poll => "int".to_string(), + Ty::Int(_) | Ty::Char | Ty::Enum(_) | Ty::Poll | Ty::Status => "int".to_string(), Ty::F32 | Ty::F64 => "double".to_string(), ty => self.nt(ty), } @@ -806,14 +817,32 @@ impl<'a> Gen<'a> { let decls: Vec = (call.params.iter().enumerate()) .map(|(n, (_, ty))| format!(", p{n}: {}", rs(ty))) .collect(); - let args: Vec = (0..call.params.len()).map(|n| format!("p{n}")).collect(); + let mut args: Vec = (0..call.params.len()).map(|n| format!("p{n}")).collect(); + // A status the call writes reaches the isolate's one cell only once `serve` stopped + // serving: a call it served meanwhile, on the isolate's thread, read its own first. A + // null one, which aborts on a failed check, passes through. + let status = call.params.last().is_some_and(|(_, ty)| abi::is_status(ty)); + let cell = match status { + true => { + let cell = args.pop().expect("a status is a parameter"); + args.push("s_".to_string()); + Some(cell) + } + false => None, + }; let call_ = match self.handed.get(&call.name) { Some(h) => h.call(&args), None => format!("crate::{}({})", call.name, args.join(", ")), }; + let body = match cell { + Some(cell) => format!( + "let mut status = 0;\n\tlet s_ = match {cell}.is_null() {{\n\t\ttrue => {cell},\n\t\tfalse => &raw mut status,\n\t}};\n\tlet r = unsafe {{ home.serve(move || {call_}) }};\n\tif !{cell}.is_null() {{\n\t\tunsafe {{ *{cell} = status }};\n\t}}\n\tr" + ), + None => format!("unsafe {{ home.serve(move || {call_}) }}"), + }; let _ = write!( self.glue, - "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}(home: *const c_void{}) -> {} {{\n\tlet home = unsafe {{ &*home.cast::() }};\n\tunsafe {{ home.serve(move || {call_}) }}\n}}\n\n", + "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}(home: *const c_void{}) -> {} {{\n\tlet home = unsafe {{ &*home.cast::() }};\n\t{body}\n}}\n\n", decls.concat(), rs(&call.ret), ); @@ -1427,38 +1456,65 @@ impl<'a> Gen<'a> { }, false => Ty::In(Box::new(c_ty.clone())), }; + // An object whose run-time checks may fail passes each its status, which throws. + let status = self.abi.traits_status_of(ty); + let params = |args: Vec| -> Vec { + let mut args = args; + if status { + args.push(Ty::Out(Box::new(Ty::Status))); + } + args + }; + // The statements calling `f` with `args`, returning what `lift` makes of its result. + let call = |f: &str, args: &str, what: &str, lift: &dyn Fn(&str) -> String| -> String { + match status { + true => format!( + "final s_ = _n.status;\n\t\t\tfinal r_ = {f}(_n.home, {args}, s_);\n\t\t\t_status(s_.value, '{what}');\n\t\t\treturn {};", + lift("r_") + ), + false => format!("return {};", lift(&format!("{f}(_n.home, {args})"))), + } + }; if t.display { members.push("toString".to_string()); - let f = self.served_fn(&format!("{c}_to_string"), vec![recv.clone()], Ty::Buf); + let what = format!("{c}_to_string"); + let f = self.served_fn(&what, params(vec![recv.clone()]), Ty::Buf); let (pre, arg) = this(self, "this"); + let call = call(&f, &arg, &what, &|r| format!("_n.string({r})")); body.push(format!( - "\t@override\n\tString toString() {{\n\t\tfinal a_ = _Arena();\n\t\ttry {{\n\t\t\t{pre}return _n.string({f}(_n.home, {arg}));\n\t\t}} finally {{\n\t\t\ta_.end();\n\t\t}}\n\t}}\n" + "\t@override\n\tString toString() {{\n\t\tfinal a_ = _Arena();\n\t\ttry {{\n\t\t\t{pre}{call}\n\t\t}} finally {{\n\t\t\ta_.end();\n\t\t}}\n\t}}\n" )); } if t.debug { members.push("debug".to_string()); - let f = self.served_fn(&format!("{c}_debug"), vec![recv.clone()], Ty::Buf); + let what = format!("{c}_debug"); + let f = self.served_fn(&what, params(vec![recv.clone()]), Ty::Buf); let (pre, arg) = this(self, "this"); + let call = call(&f, &arg, &what, &|r| format!("_n.string({r})")); body.push(format!( - "\t/// Rust's `Debug` form.\n\tString debug() {{\n\t\tfinal a_ = _Arena();\n\t\ttry {{\n\t\t\t{pre}return _n.string({f}(_n.home, {arg}));\n\t\t}} finally {{\n\t\t\ta_.end();\n\t\t}}\n\t}}\n" + "\t/// Rust's `Debug` form.\n\tString debug() {{\n\t\tfinal a_ = _Arena();\n\t\ttry {{\n\t\t\t{pre}{call}\n\t\t}} finally {{\n\t\t\ta_.end();\n\t\t}}\n\t}}\n" )); } if object && t.clone { members.push("clone".to_string()); - let f = self.served_fn(&format!("{c}_clone"), vec![recv.clone()], recv.clone()); + let what = format!("{c}_clone"); + let f = self.served_fn(&what, params(vec![recv.clone()]), recv.clone()); + let call = call(&f, "a_.hold(this)", &what, &|r| format!("{name}._({r})")); body.push(format!( - "\t/// A new handle to a clone.\n\t{name} clone() {{\n\t\tfinal a_ = _Arena();\n\t\ttry {{\n\t\t\treturn {name}._({f}(_n.home, a_.hold(this)));\n\t\t}} finally {{\n\t\t\ta_.end();\n\t\t}}\n\t}}\n" + "\t/// A new handle to a clone.\n\t{name} clone() {{\n\t\tfinal a_ = _Arena();\n\t\ttry {{\n\t\t\t{call}\n\t\t}} finally {{\n\t\t\ta_.end();\n\t\t}}\n\t}}\n" )); } // A Dart enum compares by identity, as a plain Rust enum does, and may not override `==`. let plain = matches!(ty, Type::Enum(i) if self.abi.api.enums[*i].plain()); if t.eq && !plain { members.push("hashCode".to_string()); - let f = self.served_fn(&format!("{c}_eq"), vec![recv.clone(), recv.clone()], Ty::Bool); + let what = format!("{c}_eq"); + let f = self.served_fn(&what, params(vec![recv.clone(), recv.clone()]), Ty::Bool); let (pa, a) = this(self, "this"); let (pb, b) = this(self, "other"); + let call = call(&f, &format!("{a}, {b}"), &what, &|r| r.to_string()); body.push(format!( - "\t/// Rust's `PartialEq`.\n\t@override\n\tbool operator ==(Object other) {{\n\t\tif (other is! {name}) return false;\n\t\tfinal a_ = _Arena();\n\t\ttry {{\n\t\t\t{pa}{pb}return {f}(_n.home, {a}, {b});\n\t\t}} finally {{\n\t\t\ta_.end();\n\t\t}}\n\t}}\n\n\t/// Rust has no `Hash` for it, so it keys no `Set` or `Map`.\n\t@override\n\tint get hashCode => throw UnsupportedError('{name} has no Rust Hash');\n" + "\t/// Rust's `PartialEq`.\n\t@override\n\tbool operator ==(Object other) {{\n\t\tif (other is! {name}) return false;\n\t\tfinal a_ = _Arena();\n\t\ttry {{\n\t\t\t{pa}{pb}{call}\n\t\t}} finally {{\n\t\t\ta_.end();\n\t\t}}\n\t}}\n\n\t/// Rust has no `Hash` for it, so it keys no `Set` or `Map`.\n\t@override\n\tint get hashCode => throw UnsupportedError('{name} has no Rust Hash');\n" )); } if t.default { @@ -1686,9 +1742,17 @@ impl<'a> Gen<'a> { }; let (own, free) = self.owned(&c, &owner); let mut members = vec!["close".to_string()]; + let checked = abi.checked[i]; let mut body = vec![format!( - "\t{name}._(ffi.Pointer raw) : super(raw, true) {{\n\t\t_own = {own}(this, raw);\n\t}}\n\n\t{name}._lent(ffi.Pointer raw) : super(raw, false);\n\n\t@override\n\tString get _type => '{name}';\n\n\t@override\n\tvoid _free() => {free}(_n.home, _own, this, _raw);\n" + "\t{name}._(ffi.Pointer raw) : super(raw, true, {checked}) {{\n\t\t_own = {own}(this, raw);\n\t}}\n\n\t{name}._lent(ffi.Pointer raw) : super(raw, false, {checked});\n\n\t@override\n\tString get _type => '{name}';\n\n\t@override\n\tvoid _free() => {free}(_n.home, _own, this, _raw);\n" )]; + if checked { + let what = format!("{c}_drop"); + let f = self.served_fn(&what, vec![owner.clone(), Ty::Out(Box::new(Ty::Status))], Ty::Void); + body.push(format!( + "\t@override\n\tvoid _drop() {{\n\t\tfinal a_ = _Arena();\n\t\ttry {{\n\t\t\tfinal s_ = _n.status;\n\t\t\t{f}(_n.home, _raw, s_);\n\t\t\t_status(s_.value, '{what}');\n\t\t}} finally {{\n\t\t\ta_.end();\n\t\t}}\n\t}}\n" + )); + } self.traits(&ty, &c, object.traits, &mut body, &mut members); self.methods(&object.methods, &owner, &ty, &mut body, &mut members); self.distinct(&rust, &members); @@ -1966,37 +2030,6 @@ impl<'a> Gen<'a> { } } - /// The Dart statements closing each object the value `e` of type `ty` holds, once a - /// call moved them out. - fn close(&mut self, ty: &Type, e: &str) -> String { - // A local named apart from `e`, which may be a parameter named `v`, and from the - // local an outer level declared, which Dart would read before it is declared. - let v: String = e - .chars() - .map(|c| if c.is_ascii_alphanumeric() { c } else { '_' }) - .collect::() - + "_"; - match ty { - Type::Option(inner) => { - let close = self.close(inner, &v); - format!("{{ final {v} = {e}; if ({v} != null) {{ {close} }} }}") - } - Type::Box(inner) => self.close(inner, e), - Type::Tuple(elements) => { - let closes: Vec = (elements.iter().enumerate()) - .filter(|(_, ty)| moved(self.abi, ty)) - .map(|(k, ty)| self.close(ty, &format!("{e}.${}", k + 1))) - .collect(); - closes.join(" ") - } - Type::List(_, inner) => { - let close = self.close(inner, &v); - format!("for (final {v} in {e}) {{ {close} }}") - } - _ => format!("{e}.close();"), - } - } - /// `function`'s Dart form and its member name, a member of `owner` (whose IR type is /// `owner_ty`) if it has one. fn function( @@ -2057,19 +2090,13 @@ impl<'a> Gen<'a> { let receiver = function.receiver.filter(|_| owner.is_some()); let mut pre = Vec::new(); let mut args: Vec = Vec::new(); - let mut after = Vec::new(); let mut c_params = export.call.params.iter(); - if let Some(receiver) = receiver { + if receiver.is_some() { let (_, c) = c_params.next().expect("a method has its receiver"); let ty = owner_ty.expect("a method has its type").clone(); match c { Ty::Handle { .. } => { args.push("a_.hold(this)".to_string()); - let copy = matches!(ty, Type::Object(i) if abi.api.objects[i].copy); - if receiver == Receiver::Owned && !copy { - // Qualified, apart from a parameter named `close`. - after.push("this.close();".to_string()); - } } Ty::In(inner) => { let (stmts, ptr) = self.value_ptr(&ty, inner, "this"); @@ -2113,9 +2140,6 @@ impl<'a> Gen<'a> { continue; } params.push(format!("{} {name}", self.dt(¶m.ty))); - if moved(abi, ¶m.ty) { - after.push(self.close(¶m.ty, &name)); - } match (¶m.ty, c) { (Type::Closure(closure_), Ty::Fn(..)) => { c_params.next().expect("a closure has its data"); @@ -2171,7 +2195,13 @@ impl<'a> Gen<'a> { } } } - let outs: Vec<(String, Ty)> = c_params.cloned().collect(); + let outs: Vec<(String, Ty)> = c_params.filter(|(_, ty)| !abi::is_status(ty)).cloned().collect(); + // A call that fails a run-time check writes its status, which throws once it returns. + let status = export.call.status().is_some(); + let check = format!("_status(s_.value, '{c_name}');"); + if status { + pre.push("final s_ = _n.status;".to_string()); + } // A call this target lacks throws before any argument is lowered or given to Rust. if !abi.api.everywhere(self.on) { pre.insert(0, format!("_on(0x{:x}, '{c_name}');", self.on.0 & abi.api.all().0)); @@ -2239,12 +2269,19 @@ impl<'a> Gen<'a> { } inner.push("final o_f_ = a_>();".to_string()); start_args.push("o_f_".to_string()); - inner.push(format!("final started_ = {start_fn}({});", start_args.join(", "),)); + let started = indent(&started.join("\n")); + if status { + start_args.push("s_".to_string()); + } + inner.push(format!("final started_ = {start_fn}({});", start_args.join(", "))); + // Rust took what it was given, even for a call its check refuses. if gives { inner.push("a_.took();".to_string()); } - inner.push(format!("switch (started_) {{\n{}\n}}", indent(&started.join("\n")))); - inner.extend(after.clone()); + if status { + inner.push(check.clone()); + } + inner.push(format!("switch (started_) {{\n{started}\n}}")); body.push(format!("final c_ = _Call<{ok_dt}>._({free_fn}, {poll_name});")); body.push(format!( "final a_ = _Arena();\ntry {{\n{}\n}} finally {{\n\ta_.end();\n}}", @@ -2259,16 +2296,25 @@ impl<'a> Gen<'a> { } let mut call_args = args.clone(); call_args.extend(outs.iter().map(|(n, _)| format!("o_{n}"))); + if status { + call_args.push("s_".to_string()); + } let call = format!("{call_fn}({})", call_args.join(", ")); + // Rust took what it was given, even for a call its check refuses. let took = match gives { true => "\na_.took();", false => "", }; + let checked = |inner: &mut Vec| { + if status { + inner.push(check.clone()); + } + }; match (&err, &ok) { (Some(err), ok) => { inner.push(format!("final ok_ = {call};{took}")); + checked(&mut inner); inner.extend(writes); - inner.extend(after.clone()); let ready = match ok { Type::Unit => "return;".to_string(), _ => format!("return {};", self.lift_outs(&function.ok, ok)), @@ -2283,20 +2329,20 @@ impl<'a> Gen<'a> { } (None, Type::Unit) => { inner.push(format!("{call};{took}")); + checked(&mut inner); inner.extend(writes); - inner.extend(after.clone()); } // A value C returns beside what Rust wrote back is its return, as with none. (None, ok) if outs.len() > writes.len() => { inner.push(format!("{call};{took}")); + checked(&mut inner); inner.extend(writes); - inner.extend(after.clone()); inner.push(format!("return {};", self.lift_outs(&function.ok, ok))); } (None, ok) => { inner.push(format!("final r_ = {call};{took}")); + checked(&mut inner); inner.extend(writes); - inner.extend(after.clone()); inner.push(format!("return {};", self.lift(ok, "r_"))); } } @@ -2491,17 +2537,6 @@ fn thrown_value(thrown: &Thrown, lifted: &str) -> String { } } -/// Whether passing `ty` moves an object's value out: by value, boxed, or in an `Option`, -/// a list, or a tuple, unless `Copy`, and never through a reference. -fn moved(abi: &Abi<'_>, ty: &Type) -> bool { - match ty { - Type::Object(i) => !abi.api.objects[*i].copy, - Type::Option(inner) | Type::Box(inner) | Type::List(_, inner) => moved(abi, inner), - Type::Tuple(elements) => elements.iter().any(|ty| moved(abi, ty)), - _ => false, - } -} - /// The `dart:typed_data` list holding a block's element exactly, bit for bit: a /// `Uint64List` reads a `u64` past `i64::MAX` as the negative `int` of its bits, as every /// `u64` crosses Dart. diff --git a/ffi/src/glue.rs b/ffi/src/glue.rs index c1ae8ef4f..cc4baffd7 100644 --- a/ffi/src/glue.rs +++ b/ffi/src/glue.rs @@ -5,6 +5,7 @@ //! never frees, and lowered into memory the caller frees. A handle is borrowed as the //! receiver or parameter is, or moved out of when taken by value. +use std::cell::Cell; use std::collections::HashSet; use std::fmt::Write; use std::hash::Hasher; @@ -105,7 +106,7 @@ pub fn rs(ty: &Ty) -> String { Ty::SocketAddr => "rt::SocketAddr".to_string(), Ty::Struct(name) => name.clone(), // A C enum is an `int`. - Ty::Enum(_) | Ty::Poll => "i32".to_string(), + Ty::Enum(_) | Ty::Poll | Ty::Status => "i32".to_string(), Ty::Handle { .. } => "*const ::std::ffi::c_void".to_string(), Ty::Future(_) => "*mut rt::Future".to_string(), Ty::Waker => "rt::Waker".to_string(), @@ -365,6 +366,8 @@ impl Crate<'_> { lends: false, on: On::ALL, out: String::new(), + checking: Cell::new(false), + reserved: Cell::new(false), }; generator.lib(); output.file(&out.join("Cargo.toml"), &manifest("0.0.0")); @@ -528,6 +531,12 @@ struct Gen<'a> { /// The targets of the item being written, which compile what names its Rust types. on: On, out: String, + /// Whether the code being written returns a failed check as an `Err`, with `?`, rather + /// than aborting, as a function with a `status` does. + checking: Cell, + /// Whether the argument being lifted had its moved handles reserved, so its lift moves + /// their values out unchecked. + reserved: Cell, } impl Gen<'_> { @@ -585,6 +594,158 @@ impl Gen<'_> { } } + /// `e`, a check's `Result`: its value, returning the failure from the function being + /// written if it takes a `status`, else aborting. + fn checked(&self, e: &str) -> String { + match self.checking.get() { + true => format!("{e}?"), + false => format!("rt::check({e}, w)"), + } + } + + /// Whether lifting a value of `ty` checks a handle that can fail. + fn checks(&self, ty: &Type) -> bool { + self.abi.value_status(ty) + } + + /// Whether `ty` moves an object's value out of its handle, as passed by value, held + /// alone or in any value. + fn moves(&self, ty: &Type) -> bool { + self.holds(ty, &|i| !self.abi.api.objects[i].copy, &mut Vec::new()) + } + + /// Whether lifting `ty` within a moved argument reserves anything into `takes`: a value + /// it moves out, or a checked `Copy` one it copies. + fn reserves(&self, ty: &Type) -> bool { + let pick = |i: usize| !self.abi.api.objects[i].copy || self.abi.checked[i]; + self.holds(ty, &pick, &mut Vec::new()) + } + + /// Whether a value of `ty`, passed by value, holds an object whose index `pick` picks. + fn holds(&self, ty: &Type, pick: &dyn Fn(usize) -> bool, seen: &mut Vec<(bool, usize)>) -> bool { + let api = self.abi.api; + match ty { + Type::Object(i) => pick(*i), + Type::Option(inner) | Type::Box(inner) | Type::List(_, inner) => self.holds(inner, pick, seen), + Type::Tuple(elements) => elements.iter().any(|ty| self.holds(ty, pick, seen)), + Type::Record(i) if !seen.contains(&(false, *i)) => { + seen.push((false, *i)); + (api.records[*i].fields.iter()).any(|f| self.holds(&f.ty, pick, seen)) + } + Type::Enum(i) if !seen.contains(&(true, *i)) => { + seen.push((true, *i)); + (api.enums[*i].variants.iter().flat_map(|v| &v.fields)).any(|f| self.holds(&f.ty, pick, seen)) + } + _ => false, + } + } + + /// Whether lifting a value of `ty` with `take` can take a buffer C handed Rust. A list's + /// items are only read, so a list takes none. + fn takes_bufs(&self, ty: &Type, seen: &mut Vec<(bool, usize)>) -> bool { + let api = self.abi.api; + match ty { + _ if Abi::is_buf(ty) => true, + Type::Option(inner) | Type::Box(inner) | Type::Ref(_, inner) => self.takes_bufs(inner, seen), + Type::Tuple(elements) => elements.iter().any(|ty| self.takes_bufs(ty, seen)), + Type::Record(i) if !seen.contains(&(false, *i)) => { + seen.push((false, *i)); + (api.records[*i].fields.iter()).any(|f| self.takes_bufs(&f.ty, seen)) + } + Type::Enum(i) if !seen.contains(&(true, *i)) => { + seen.push((true, *i)); + (api.enums[*i].variants.iter().flat_map(|v| &v.fields)).any(|f| self.takes_bufs(&f.ty, seen)) + } + _ => false, + } + } + + /// The statement freeing each buffer a lift of the C value `e` of type `ty` with `take` + /// would take, leaving its handles to the caller: what a refused call does with an + /// argument it never lifted. + fn release(&self, ty: &Type, e: &str) -> Option { + match ty { + _ if Abi::is_buf(ty) => Some(format!("unsafe {{ {e}.free() }};")), + Type::Option(inner) => match inner.base() { + base if Abi::nullable(base) => self.release(inner, e), + _ => (self.release(inner, &format!("{e}.value"))).map(|free| format!("if {e}.some {{ {free} }}")), + }, + Type::Box(inner) | Type::Ref(_, inner) => self.release(inner, e), + Type::Tuple(elements) => { + let frees: Vec = (elements.iter().enumerate()) + .filter_map(|(n, ty)| self.release(ty, &format!("{e}._{n}"))) + .collect(); + (!frees.is_empty()).then(|| frees.join(" ")) + } + Type::Record(i) if self.takes_bufs(ty, &mut Vec::new()) => { + Some(format!("release_{}(&{e});", self.abi.record(*i))) + } + Type::Enum(i) if !self.abi.api.enums[*i].plain() && self.takes_bufs(ty, &mut Vec::new()) => { + Some(format!("release_{}(&{e});", self.abi.enumeration(*i))) + } + _ => None, + } + } + + /// Whether an argument of `ty` is reserved though it moves nothing: its lift can fail a + /// check and takes a buffer, so every check runs before any buffer is taken. A borrowed + /// object takes none. + fn refusable(&self, ty: &Type) -> bool { + let value = match ty { + Type::Ref(_, inner) => inner, + Type::Option(inner) => match &**inner { + Type::Ref(_, inner) => inner, + _ => ty, + }, + _ => ty, + }; + !matches!(value, Type::Object(_)) && self.checks(value) && self.takes_bufs(value, &mut Vec::new()) + } + + /// The statement reserving into `takes` each value an argument `e` of type `ty` moves + /// out, and borrowing each checked `Copy` one it copies, so the move happens only once + /// every check passed, and no check after it can fail. + fn reserve(&self, ty: &Type, e: &str) -> String { + match ty { + Type::Object(i) if !self.abi.api.objects[*i].copy => { + self.checked(&format!( + "unsafe {{ rt::Object::<{}>::reserve({e}, takes, w) }}", + self.object_path(*i) + )) + ";" + } + Type::Object(i) if self.abi.checked[*i] => { + let hold = match self.abi.api.objects[*i].sync { + true => "hold", + false => "hold_local", + }; + self.checked(&format!( + "unsafe {{ rt::Object::<{}>::{hold}({e}, takes, w) }}", + self.object_path(*i) + )) + ";" + } + Type::Object(_) => String::new(), + Type::Record(i) => self.checked(&format!("reserve_{}(&{e}, takes, w)", self.abi.record(*i))) + ";", + Type::Enum(i) => self.checked(&format!("reserve_{}(&{e}, takes, w)", self.abi.enumeration(*i))) + ";", + // A borrowed record is lifted from its C value as an owned one is. + Type::Box(inner) | Type::Ref(_, inner) => self.reserve(inner, e), + Type::Option(inner) => match inner.base() { + Type::Object(_) => format!("if !{e}.is_null() {{ {} }}", self.reserve(inner, e)), + base if Abi::nullable(base) => format!("if !{e}.is_none() {{ {} }}", self.reserve(inner, e)), + _ => format!("if {e}.some {{ {} }}", self.reserve(inner, &format!("{e}.value"))), + }, + Type::List(_, inner) => format!( + "for c in unsafe {{ {e}.items(w) }} {{ {} }}", + self.reserve(inner, "(*c)") + ), + Type::Tuple(elements) => (elements.iter().enumerate()) + .filter(|(_, ty)| self.reserves(ty)) + .map(|(n, ty)| self.reserve(ty, &format!("{e}._{n}"))) + .collect::>() + .join(" "), + _ => String::new(), + } + } + /// An object's Rust type: its lifetimes `'static`, as the handle holds it. fn object_path(&self, i: usize) -> String { self.rust(&Type::Object(i)) @@ -698,11 +859,8 @@ impl Gen<'_> { self.lift(&Type::List(Collection::Vec, inner.clone()), e, take) ), Type::List(Collection::IterRef, _) => unreachable!("an iterator of borrows is lent by `arg`"), - Type::List(c, inner) => format!( - "unsafe {{ {e}.items(w) }}.iter().map(|c| {}).collect::<{}>()", - // An array's items are behind its pointer, so only read. - self.lift(inner, "(*c)", "false"), - match c { + Type::List(c, inner) => { + let collected = match c { Collection::BTreeMap | Collection::JsonMap => format!("{}<_, _>", collection(*c)), Collection::Box => format!("{}<[_]>", collection(*c)), Collection::Vec | Collection::Slice => format!("{}<_>", collection(Collection::Vec)), @@ -710,8 +868,18 @@ impl Gen<'_> { Collection::Array(_) | Collection::Iter | Collection::IterRef => { unreachable!("{c:?} is lifted apart") } + }; + // An array's items are behind its pointer, so only read. + let lifted = self.lift(inner, "(*c)", "false"); + // A failed check inside returns from the closure each item lifts in. + let fails = !self.reserved.get() && (self.checks(inner) || self.moves(inner)); + match self.checking.get() && fails { + true => format!( + "unsafe {{ {e}.items(w) }}.iter().map(|c| -> ::std::result::Result<_, rt::Status> {{ Ok({lifted}) }}).collect::<::std::result::Result<{collected}, rt::Status>>()?" + ), + false => format!("unsafe {{ {e}.items(w) }}.iter().map(|c| {lifted}).collect::<{collected}>()"), } - ), + } // A foreign enum's option converts into its filter, as `tracing::LevelFilter`. Type::Option(inner) if self.filtered(inner) => format!( "::std::convert::From::from(if {e}.some {{ Some({}) }} else {{ None }})", @@ -735,23 +903,50 @@ impl Gen<'_> { }, Type::Box(inner) => format!("::std::boxed::Box::new({})", self.lift(inner, e, take)), Type::Ref(_, inner) => self.lift(inner, e, take), - Type::Record(i) => format!("lift_{}(&{e}, w, {take})", self.abi.record(*i)), - Type::Enum(i) => match self.abi.api.enums[*i].plain() { - true => format!("lift_{}({e}, w)", self.abi.enumeration(*i)), - false => format!("lift_{}(&{e}, w, {take})", self.abi.enumeration(*i)), - }, - Type::Object(i) if let Some(held) = self.abi.api.objects[*i].opaque => match held { - Held::Arc => format!("unsafe {{ rt::Object::::take({e}, w) }}.arc(w)"), - Held::Value => format!("unsafe {{ rt::Object::::take({e}, w) }}.take(w)"), + // Reserved, its checked copies passed their checks. + Type::Record(i) if self.reserved.get() && self.reserves(ty) => { + format!("lift_{}_taken(&{e}, w, {take})", self.abi.record(*i)) + } + Type::Enum(i) if self.reserved.get() && self.reserves(ty) => { + format!("lift_{}_taken(&{e}, w, {take})", self.abi.enumeration(*i)) + } + Type::Record(i) => match self.checks(ty) { + true => self.checked(&format!("lift_{}(&{e}, w, {take})", self.abi.record(*i))), + false => format!("lift_{}(&{e}, w, {take})", self.abi.record(*i)), }, - Type::Object(i) => match self.abi.api.objects[*i].copy { - true => format!( - "*unsafe {{ rt::Object::<{}>::{}({e}, w) }}", - self.object_path(*i), - self.shared(*i) - ), - false => format!("unsafe {{ rt::Object::<{}>::take({e}, w) }}", self.object_path(*i)), + Type::Enum(i) => match (self.abi.api.enums[*i].plain(), self.checks(ty)) { + (true, _) => format!("lift_{}({e}, w)", self.abi.enumeration(*i)), + (false, true) => self.checked(&format!("lift_{}(&{e}, w, {take})", self.abi.enumeration(*i))), + (false, false) => format!("lift_{}(&{e}, w, {take})", self.abi.enumeration(*i)), }, + Type::Object(i) => { + let object = &self.abi.api.objects[*i]; + let path = match object.opaque { + Some(Held::Arc) => "rt::Opaque".to_string(), + Some(Held::Value) => "rt::Owned".to_string(), + None => self.object_path(*i), + }; + let value = match (object.copy, self.reserved.get()) { + // One no export can misuse is copied out uncounted, as it is borrowed. + (true, _) if !self.abi.checked[*i] => format!("*unsafe {{ rt::Object::<{path}>::peek({e}, w) }}"), + // Reserved, it is borrowed until the call's moves are made. + (true, true) => format!("unsafe {{ rt::Object::<{path}>::held({e}) }}"), + (true, _) => format!( + "*{}", + self.checked(&format!( + "unsafe {{ rt::Object::<{path}>::{}({e}, w) }}", + self.shared(*i) + )) + ), + (false, true) => format!("unsafe {{ rt::Object::<{path}>::taken({e}) }}"), + (false, false) => self.checked(&format!("unsafe {{ rt::Object::<{path}>::take({e}, w) }}")), + }; + match object.opaque { + Some(Held::Arc) => format!("{value}.arc(w)"), + Some(Held::Value) => format!("{value}.take(w)"), + None => value, + } + } Type::Callback(i, form) => abi::dyn_wrap(*form, &format!("Foreign_{}({e})", self.abi.callback(*i))), Type::Closure(closure) => self.closure(closure, e), Type::Range(_) => format!("{e}.lift()"), @@ -770,6 +965,14 @@ impl Gen<'_> { /// which lends each object the closure takes as a handle for that call. A kept one frees /// its data with `e_free` once dropped. fn closure(&self, closure: &Closure, e: &str) -> String { + // A failed check of what it returns has no caller to return to. + let checking = self.checking.replace(false); + let made = self.closure_body(closure, e); + self.checking.set(checking); + made + } + + fn closure_body(&self, closure: &Closure, e: &str) -> String { let typed: Vec = (closure.params.iter().enumerate()) .map(|(n, ty)| format!("c{n}: {}", self.rust(ty))) .collect(); @@ -1038,6 +1241,9 @@ impl Gen<'_> { self.line(format!( "#[unsafe(no_mangle)]\npub extern \"C\" fn {p}_target() -> u32 {{\n\tTARGET\n}}\n" )); + self.line(format!( + "#[unsafe(no_mangle)]\npub extern \"C\" fn {p}_status_message(status: i32) -> *const ::std::ffi::c_char {{\n\trt::Status::text(status).as_ptr()\n}}\n" + )); for (name, ty) in &self.abi.ranges { let (kind, int) = match ty { @@ -1094,15 +1300,27 @@ impl Gen<'_> { self.export(format!( "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_free(this: *const ::std::ffi::c_void) {{\n\tunsafe {{ rt::Object::<{path}>::free(this) }}\n}}\n" )); + if self.abi.checked[i] { + self.export(format!( + "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_drop(this: *const ::std::ffi::c_void, status: *mut i32) {{\n\tlet w = \"{name}_drop\";\n\tif let Err(s) = unsafe {{ rt::Object::<{path}>::close(this, w) }} {{\n\t\tunsafe {{ rt::failed::<()>(status, s, w) }}\n\t}}\n}}\n" + )); + } if object.shared { self.export(format!( "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_borrowed(this: *const ::std::ffi::c_void) -> bool {{\n\tunsafe {{ rt::Object::<{path}>::borrowed(this, \"{name}_borrowed\") }}\n}}\n" )); } let borrow = self.shared(i); - self.traits(&name, &path, object.traits, |e| { - format!("unsafe {{ rt::Object::<{path}>::{borrow}({e}, w) }}") - }); + // Each borrows its handles shared, which fails only as a call can. + let status = self.abi.traits_status(i).then_some("status"); + self.checking.set(status.is_some()); + let borrowing = |e: &str| match status { + Some(_) => self.checked(&format!("unsafe {{ rt::Object::<{path}>::{borrow}({e}, w) }}")), + None => format!("unsafe {{ rt::Object::<{path}>::peek({e}, w) }}"), + }; + let borrowing: Vec = ["this", "a", "b"].into_iter().map(borrowing).collect(); + self.checking.set(false); + self.traits(&name, &path, object.traits, status, &borrowing); let owner = Ty::Handle { name: name.clone(), constant: false, @@ -1116,32 +1334,52 @@ impl Gen<'_> { } } - /// The functions a type's std traits export; `borrow` reads the receiver `e`. - fn traits(&mut self, name: &str, path: &str, traits: Traits, borrow: impl Fn(&str) -> String) { + /// The functions an object's std traits export, borrowing its handles `this`, `a`, + /// and `b` as `borrow` does, which takes `status` if a borrow can fail. + fn traits(&mut self, name: &str, path: &str, traits: Traits, status: Option<&str>, borrow: &[String]) { let handle = "*const ::std::ffi::c_void"; + let [this, a, b] = borrow else { + unreachable!("three handles") + }; + let param = status.map(|s| format!(", {s}: *mut i32")).unwrap_or_default(); if traits.clone { + let body = wrap( + status, + handle, + &format!("let this = {this};\n\trt::Object::new(<{path} as ::std::clone::Clone>::clone(&*this))"), + ); self.export(format!( - "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_clone(this: {handle}) -> {handle} {{\n\tlet w = \"{name}_clone\";\n\tlet this = {};\n\trt::Object::new(<{path} as ::std::clone::Clone>::clone(&*this))\n}}\n", - borrow("this") + "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_clone(this: {handle}{param}) -> {handle} {{\n\tlet w = \"{name}_clone\";\n\t{body}\n}}\n" )); } if traits.eq { + let body = wrap( + status, + "bool", + &format!("let a = {a};\n\tlet b = {b};\n\t<{path} as ::std::cmp::PartialEq>::eq(&*a, &*b)"), + ); self.export(format!( - "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_eq(a: {handle}, b: {handle}) -> bool {{\n\tlet w = \"{name}_eq\";\n\tlet a = {};\n\tlet b = {};\n\t<{path} as ::std::cmp::PartialEq>::eq(&*a, &*b)\n}}\n", - borrow("a"), - borrow("b") + "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_eq(a: {handle}, b: {handle}{param}) -> bool {{\n\tlet w = \"{name}_eq\";\n\t{body}\n}}\n" )); } if traits.display { + let body = wrap( + status, + "rt::Buf", + &format!("let this = {this};\n\trt::Buf::string(::std::string::ToString::to_string(&*this))"), + ); self.export(format!( - "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_to_string(this: {handle}) -> rt::Buf {{\n\tlet w = \"{name}_to_string\";\n\tlet this = {};\n\trt::Buf::string(::std::string::ToString::to_string(&*this))\n}}\n", - borrow("this") + "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_to_string(this: {handle}{param}) -> rt::Buf {{\n\tlet w = \"{name}_to_string\";\n\t{body}\n}}\n" )); } if traits.debug { + let body = wrap( + status, + "rt::Buf", + &format!("let this = {this};\n\trt::Buf::string(format!(\"{{:?}}\", &*this))"), + ); self.export(format!( - "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_debug(this: {handle}) -> rt::Buf {{\n\tlet w = \"{name}_debug\";\n\tlet this = {};\n\trt::Buf::string(format!(\"{{:?}}\", &*this))\n}}\n", - borrow("this") + "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_debug(this: {handle}{param}) -> rt::Buf {{\n\tlet w = \"{name}_debug\";\n\t{body}\n}}\n" )); } if traits.default { @@ -1154,24 +1392,43 @@ impl Gen<'_> { /// The functions every value type has: its std traits, read through a pointer, and /// `free`. fn value_traits(&mut self, name: &str, path: &str, ty: &Type, traits: Traits) { - // Each is read through a pointer, so the caller keeps it. + // One holding a handle a check of can fail returns the failure through a `status`. + let status = self.checks(ty).then_some("status"); + self.checking.set(status.is_some()); let lift = self.lift(ty, "unsafe { *this }", "false"); let lift_a = self.lift(ty, "unsafe { *a }", "false"); let lift_b = self.lift(ty, "unsafe { *b }", "false"); + self.checking.set(false); + let param = status.map(|s| format!(", {s}: *mut i32")).unwrap_or_default(); let c = rs(&self.abi.ty(ty)); if traits.eq { + let body = wrap( + status, + "bool", + &format!("<{path} as ::std::cmp::PartialEq>::eq(&{lift_a}, &{lift_b})"), + ); self.export(format!( - "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_eq(a: *const {c}, b: *const {c}) -> bool {{\n\tlet w = \"{name}_eq\";\n\t<{path} as ::std::cmp::PartialEq>::eq(&{lift_a}, &{lift_b})\n}}\n" + "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_eq(a: *const {c}, b: *const {c}{param}) -> bool {{\n\tlet w = \"{name}_eq\";\n\t{body}\n}}\n" )); } if traits.display { + let body = wrap( + status, + "rt::Buf", + &format!("rt::Buf::string(::std::string::ToString::to_string(&{lift}))"), + ); self.export(format!( - "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_to_string(this: *const {c}) -> rt::Buf {{\n\tlet w = \"{name}_to_string\";\n\trt::Buf::string(::std::string::ToString::to_string(&{lift}))\n}}\n" + "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_to_string(this: *const {c}{param}) -> rt::Buf {{\n\tlet w = \"{name}_to_string\";\n\t{body}\n}}\n" )); } if traits.debug { + let body = wrap( + status, + "rt::Buf", + &format!("rt::Buf::string(format!(\"{{:?}}\", &{lift}))"), + ); self.export(format!( - "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_debug(this: *const {c}) -> rt::Buf {{\n\tlet w = \"{name}_debug\";\n\trt::Buf::string(format!(\"{{:?}}\", &{lift}))\n}}\n" + "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_debug(this: *const {c}{param}) -> rt::Buf {{\n\tlet w = \"{name}_debug\";\n\t{body}\n}}\n" )); } if traits.default { @@ -1207,12 +1464,16 @@ impl Gen<'_> { self.line(format!( "#[repr(C)]\n#[derive(Clone, Copy)]\npub struct {name} {{\n{fields}}}\n" )); + // One holding a handle a check of can fail returns the failure. + let checks = self.checks(&ty); + self.checking.set(checks); let lifted: Vec = record .fields .iter() .enumerate() .map(|(n, f)| self.lift(&f.ty, &format!("c.f{n}"), "take")) .collect(); + self.checking.set(false); let build = match record.tuple { true => format!("{path}({})", lifted.join(", ")), false => { @@ -1225,8 +1486,15 @@ impl Gen<'_> { format!("{path} {{ {} }}", fields.join(", ")) } }; + let (ret, build) = match checks { + true => ( + format!("::std::result::Result<{path}, rt::Status>"), + format!("Ok({build})"), + ), + false => (path.clone(), build), + }; self.item(format!( - "fn lift_{name}(c: &{name}, w: &str, take: bool) -> {path} {{\n\tlet _ = (w, take);\n\t{build}\n}}\n" + "fn lift_{name}(c: &{name}, w: &str, take: bool) -> {ret} {{\n\tlet _ = (w, take);\n\t{build}\n}}\n" )); let bindings: Vec = (0..record.fields.len()).map(|n| format!("f{n}")).collect(); let pattern = match record.tuple { @@ -1265,6 +1533,38 @@ impl Gen<'_> { .collect(); self.item(format!("fn free_{name}(c: {name}) {{\n\t{}\n}}\n", frees.join("\n\t"))); } + if self.takes_bufs(&ty, &mut Vec::new()) { + let releases: Vec = (record.fields.iter().enumerate()) + .filter_map(|(n, f)| self.release(&f.ty, &format!("c.f{n}"))) + .collect(); + self.item(format!( + "fn release_{name}(c: &{name}) {{\n\t{}\n}}\n", + releases.join("\n\t") + )); + } + if self.reserves(&ty) { + self.checking.set(true); + let reserves: Vec = (record.fields.iter().enumerate()) + .filter(|(_, f)| self.reserves(&f.ty)) + .map(|(n, f)| self.reserve(&f.ty, &format!("c.f{n}"))) + .collect(); + self.checking.set(false); + self.reserved.set(true); + let lifted: Vec = (record.fields.iter().enumerate()) + .map(|(n, f)| self.lift(&f.ty, &format!("c.f{n}"), "take")) + .collect(); + self.reserved.set(false); + let build = match record.tuple { + true => format!("{path}({})", lifted.join(", ")), + false => { + let fields: Vec = (record.fields.iter().zip(&lifted)) + .map(|(f, l)| format!("{}: {l}", f.name)) + .collect(); + format!("{path} {{ {} }}", fields.join(", ")) + } + }; + self.reserved_helpers(&name, &path, &reserves.join("\n\t"), &build); + } self.value_traits(&name, &path, &ty, record.traits); let owner = Ty::Struct(name.clone()); for method in &record.methods { @@ -1320,6 +1620,8 @@ impl Gen<'_> { let name = self.abi.enumeration(i); let path = self.rust(&Type::Enum(i)); let ty = Type::Enum(i); + // One holding a handle a check of can fail returns the failure. + let checks = self.checks(&ty); let wildcard = match e.exhaustive { true => "", false => "\n\t\t_ => unreachable!(\"a variant this export does not know\"),", @@ -1380,6 +1682,7 @@ impl Gen<'_> { let mut lift = String::new(); let mut lower = String::new(); let mut free = String::new(); + let mut release = String::new(); for (n, variant) in e.variants.iter().enumerate() { let bindings: Vec = (0..variant.fields.len()).map(|k| format!("f{k}")).collect(); let pattern = match (variant.fields.is_empty(), variant.tuple) { @@ -1403,7 +1706,11 @@ impl Gen<'_> { ); } if variant.fields.is_empty() { - let _ = write!(lift, "{arm}\n\t\t{n} => {pattern},"); + let lifted = match checks { + true => format!("Ok({pattern})"), + false => pattern.clone(), + }; + let _ = write!(lift, "{arm}\n\t\t{n} => {lifted},"); let _ = write!( lower, "{arm}\n\t\t{pattern} => {name} {{ tag: {n}, u: unsafe {{ ::std::mem::zeroed() }} }}," @@ -1411,12 +1718,14 @@ impl Gen<'_> { continue; } let payload = abi::payload(&name, &variant.name); + self.checking.set(checks); let lifted: Vec = variant .fields .iter() .enumerate() .map(|(k, f)| self.lift(&f.ty, &format!("p.f{k}"), "take")) .collect(); + self.checking.set(false); let build = match variant.tuple { true => format!("{path}::{}({})", variant.name, lifted.join(", ")), false => { @@ -1429,6 +1738,10 @@ impl Gen<'_> { format!("{path}::{} {{ {} }}", variant.name, fields.join(", ")) } }; + let build = match checks { + true => format!("Ok({build})"), + false => build, + }; let _ = write!( lift, "{arm}\n\t\t{n} => {{\n\t\t\tlet p = unsafe {{ c.u.v{n} }};\n\t\t\t{build}\n\t\t}}" @@ -1444,6 +1757,16 @@ impl Gen<'_> { "{arm}\n\t\t{pattern} => {name} {{ tag: {n}, u: {name}_union {{ v{n}: {payload} {{ {} }} }} }},", lowered.join(", ") ); + let releases: Vec = (variant.fields.iter().enumerate()) + .filter_map(|(k, f)| self.release(&f.ty, &format!("p.f{k}"))) + .collect(); + if !releases.is_empty() { + let _ = write!( + release, + "{arm}\n\t\t{n} => {{\n\t\t\tlet p = unsafe {{ c.u.v{n} }};\n\t\t\t{}\n\t\t}}", + releases.join("\n\t\t\t") + ); + } let frees: Vec = variant .fields .iter() @@ -1458,8 +1781,12 @@ impl Gen<'_> { ); } } + let ret = match checks { + true => format!("::std::result::Result<{path}, rt::Status>"), + false => path.clone(), + }; self.item(format!( - "fn lift_{name}(c: &{name}, w: &str, take: bool) -> {path} {{\n\tlet _ = take;\n\tmatch c.tag {{{lift}\n\t\t_ => rt::fail(w, \"an enum tag is invalid\"),\n\t}}\n}}\n" + "fn lift_{name}(c: &{name}, w: &str, take: bool) -> {ret} {{\n\tlet _ = take;\n\tmatch c.tag {{{lift}\n\t\t_ => rt::fail(w, \"an enum tag is invalid\"),\n\t}}\n}}\n" )); self.item(format!( "fn lower_{name}(v: {path}) -> {name} {{\n\tmatch v {{{lower}{wildcard}\n\t}}\n}}\n" @@ -1469,6 +1796,65 @@ impl Gen<'_> { "fn free_{name}(c: {name}) {{\n\tmatch c.tag {{{free}\n\t\t_ => {{}}\n\t}}\n}}\n" )); } + if self.takes_bufs(&ty, &mut Vec::new()) { + self.item(format!( + "fn release_{name}(c: &{name}) {{\n\tmatch c.tag {{{release}\n\t\t_ => {{}}\n\t}}\n}}\n" + )); + } + } + if !e.plain() && self.reserves(&ty) { + let mut reserve = String::new(); + let mut lift = String::new(); + for (n, variant) in e.variants.iter().enumerate() { + let arm = self.arm(variant.on); + let fields = |lifted: &[String]| match (variant.fields.is_empty(), variant.tuple) { + (true, _) => format!("{path}::{}", variant.name), + (false, true) => format!("{path}::{}({})", variant.name, lifted.join(", ")), + (false, false) => { + let fields: Vec = (variant.fields.iter().zip(lifted)) + .map(|(f, l)| format!("{}: {l}", f.name)) + .collect(); + format!("{path}::{} {{ {} }}", variant.name, fields.join(", ")) + } + }; + if let Some(cfg) = self.cfg(variant.on) { + let missing = format!( + "\n\t\t#[cfg(not({cfg}))]\n\t\t{n} => rt::fail(w, \"the variant is not on this library's target\")," + ); + reserve.push_str(&missing); + lift.push_str(&missing); + } + if variant.fields.is_empty() { + let _ = write!(lift, "{arm}\n\t\t{n} => {},", fields(&[])); + continue; + } + self.checking.set(true); + let reserves: Vec = (variant.fields.iter().enumerate()) + .filter(|(_, f)| self.reserves(&f.ty)) + .map(|(k, f)| self.reserve(&f.ty, &format!("p.f{k}"))) + .collect(); + self.checking.set(false); + if !reserves.is_empty() { + let _ = write!( + reserve, + "{arm}\n\t\t{n} => {{\n\t\t\tlet p = unsafe {{ c.u.v{n} }};\n\t\t\t{}\n\t\t}}", + reserves.join("\n\t\t\t") + ); + } + self.reserved.set(true); + let lifted: Vec = (variant.fields.iter().enumerate()) + .map(|(k, f)| self.lift(&f.ty, &format!("p.f{k}"), "take")) + .collect(); + self.reserved.set(false); + let _ = write!( + lift, + "{arm}\n\t\t{n} => {{\n\t\t\tlet p = unsafe {{ c.u.v{n} }};\n\t\t\t{}\n\t\t}}", + fields(&lifted) + ); + } + let reserve = format!("match c.tag {{{reserve}\n\t\t_ => {{}}\n\t}}"); + let build = format!("match c.tag {{{lift}\n\t\t_ => rt::fail(w, \"an enum tag is invalid\"),\n\t}}"); + self.reserved_helpers(&name, &path, &reserve, &build); } self.value_traits(&name, &path, &ty, e.traits); let owner = self.abi.ty(&ty); @@ -1477,6 +1863,18 @@ impl Gen<'_> { } } + /// A record's or data enum's helpers for a moved argument holding one: `reserve_`, + /// running `reserve`, which reserves into `takes` what it moves out and borrows what it + /// copies, and `lift__taken`, which makes `build` of what was reserved, unchecked. + fn reserved_helpers(&mut self, name: &str, path: &str, reserve: &str, build: &str) { + self.item(format!( + "fn reserve_{name}(c: &{name}, takes: &mut rt::Takes<'_>, w: &str) -> ::std::result::Result<(), rt::Status> {{\n\tlet _ = (c, &takes, w);\n\t{reserve}\n\tOk(())\n}}\n" + )); + self.item(format!( + "fn lift_{name}_taken(c: &{name}, w: &str, take: bool) -> {path} {{\n\tlet _ = (w, take);\n\t{build}\n}}\n" + )); + } + /// A callback: its table of foreign functions, and the Rust type that implements the /// trait by calling them. fn callback(&mut self, i: usize) { @@ -1573,10 +1971,29 @@ impl Gen<'_> { } } - /// An argument: the statement binding it from its C parameter `c` to `local`, and - /// the expression passing it. A future owns its arguments, so it borrows nothing - /// from the caller. - fn arg(&self, ty: &Type, c: &str, local: &str, future: bool) -> (String, String) { + /// An argument: the statement reserving each value it moves out of a handle, the + /// statement binding it from its C parameter `c` to `local`, and the expression passing + /// it. A future owns its arguments, so it borrows nothing from the caller. + fn arg(&self, ty: &Type, c: &str, local: &str, future: bool) -> (String, String, String) { + if self.moves(ty) { + let reserve = self.reserve(ty, c); + self.reserved.set(true); + let lifted = self.lift(ty, c, "true"); + self.reserved.set(false); + return (reserve, format!("let {local} = {lifted};"), local.to_string()); + } + if self.refusable(ty) { + let reserve = self.reserve(ty, c); + self.reserved.set(true); + let (stmt, pass) = self.arg_of(ty, c, local, future); + self.reserved.set(false); + return (reserve, stmt, pass); + } + let (stmt, pass) = self.arg_of(ty, c, local, future); + (String::new(), stmt, pass) + } + + fn arg_of(&self, ty: &Type, c: &str, local: &str, future: bool) -> (String, String) { match ty { Type::Ref(m, inner) => match (&**inner, m, future) { (Type::Object(i), _, _) => { @@ -1691,9 +2108,19 @@ impl Gen<'_> { true => "let mut", false => "let", }; + // A type no export can misuse is borrowed shared for a call uncounted. + if !exclusive && !future && !self.abi.checked[i] { + return ( + binding, + format!("unsafe {{ rt::Object::<{}>::peek({c}, w) }}", self.object_path(i)), + ); + } ( binding, - format!("unsafe {{ rt::Object::<{}>::{borrow}({c}, w) }}", self.object_path(i)), + self.checked(&format!( + "unsafe {{ rt::Object::<{}>::{borrow}({c}, w) }}", + self.object_path(i) + )), ) } @@ -1762,12 +2189,32 @@ impl Gen<'_> { fn function_on(&mut self, function: &Function, owner: Option<(&Ty, Type)>) { let export = self.abi.export(function, owner.as_ref().map(|(ty, _)| *ty)); let name = export.call.name.clone(); + let status = export.call.status(); + self.checking.set(status.is_some()); // An eager call borrows its arguments only for the call, as a synchronous one does, // unless the future it returns borrows them: it then owns them, as an `async fn`'s // future does, and holds each it lends in place until the future is dropped. let future = function.future && (!function.eager || function.holds); let mut held = Vec::new(); + // Each argument's statements: those that cannot fail a check first, so what C gave + // Rust, such as a kept closure or a buffer, drops if a check fails; then those that + // check a handle and take nothing, then the reserved ones, lifted once every check + // passed, so a call that fails a check changes nothing. A reserved argument's + // buffers are freed if the call is refused before it lifts them. + let mut checks = Vec::new(); + let mut reserves = Vec::new(); + let mut moves = Vec::new(); let mut stmts = Vec::new(); + let mut releases = Vec::new(); + let mut sort = + |ty: &Type, (reserve, stmt, _): &(String, String, String)| match (reserve.is_empty(), self.checks(ty)) { + (false, _) => { + reserves.push(reserve.clone()); + moves.push(stmt.clone()); + } + (true, true) => checks.push(stmt.clone()), + (true, false) => stmts.push(stmt.clone()), + }; let mut args = Vec::new(); let mut params = Vec::new(); let mut c_params = export.call.params.iter(); @@ -1782,9 +2229,9 @@ impl Gen<'_> { Receiver::Mut => Type::Ref(Mutability::Mut, Box::new(Type::Object(*i))), Receiver::Owned => Type::Object(*i), }; - let (stmt, arg) = self.arg(&ty, "this", "a_self", future); - stmts.push(stmt); - args.push(arg); + let made = self.arg(&ty, "this", "a_self", future); + sort(&ty, &made); + args.push(made.2); if function.holds && *receiver != Receiver::Owned { held.push("a_self".to_string()); } @@ -1799,10 +2246,12 @@ impl Gen<'_> { Receiver::Mut => "let mut", _ => "let", }; - stmts.push(format!( - "{binding} a_self = {};", - self.lift(value, "unsafe { *this }", "false") - )); + let made = ( + String::new(), + format!("{binding} a_self = {};", self.lift(value, "unsafe { *this }", "false")), + String::new(), + ); + sort(value, &made); args.push(match receiver { Receiver::Owned => "a_self".to_string(), Receiver::Ref => "&a_self".to_string(), @@ -1830,7 +2279,7 @@ impl Gen<'_> { params.push(format!("p{n}_free: {}", rs(free))); } } - let (stmt, arg) = match &function.task { + let made = match &function.task { // A context made of C's waker, lent for the call. Some(Task { at, @@ -1841,6 +2290,7 @@ impl Gen<'_> { Mutability::Not => ("let", format!("&a{n}")), }; ( + String::new(), format!( "let a{n}_waker = p{n}.lift();\n\t{binding} a{n} = ::std::task::Context::from_waker(&a{n}_waker);" ), @@ -1848,16 +2298,19 @@ impl Gen<'_> { ) } _ if param.interned && *param.ty.base() == Type::Slice => ( + String::new(), format!("let a{n} = rt::intern_bytes(unsafe {{ p{n}.hold(w) }}.bytes());"), format!("a{n}"), ), _ if param.interned => ( + String::new(), format!("let a{n} = rt::intern(unsafe {{ p{n}.hold(w) }}.str(w));"), format!("a{n}"), ), // A future made of a `Poll` owns a future C implements, and lends it pinned to // each poll. Some(_) if function.future && param.ty == Type::Future => ( + String::new(), format!("let mut a{n} = p{n}.lift(w);"), format!("::std::pin::Pin::new(&mut a{n})"), ), @@ -1869,12 +2322,46 @@ impl Gen<'_> { if function.holds && lent { held.push(format!("a{n}")); } - self.arg(¶m.ty, &format!("p{n}"), &format!("a{n}"), future) + let made = self.arg(¶m.ty, &format!("p{n}"), &format!("a{n}"), future); + if !made.0.is_empty() + && let Some(release) = self.release(¶m.ty, &format!("p{n}")) + { + releases.push(release); + } + made } }; - stmts.push(stmt); - args.push(arg); - } + sort(¶m.ty, &made); + args.push(made.2); + } + drop(sort); + let mut stmts = { + let mut all = stmts; + all.extend(checks); + let reserved = !reserves.is_empty(); + // Only a call with a status returns on a failed check; one without aborts. + let release = (status.is_some() && !releases.is_empty()).then(|| releases.join(" ")); + if reserved { + all.insert( + 0, + "let mut reserved = rt::Takes::default();\n\tlet takes = &mut reserved;".to_string(), + ); + if let Some(release) = &release { + all.insert(0, format!("let unlifted = rt::Unlifted::new(|| {{ {release} }});")); + } + all.extend(reserves); + all.push("takes.commit();".to_string()); + if release.is_some() { + all.push("unlifted.lifted();".to_string()); + } + } + all.extend(moves); + // The copies are made, so their borrows end before the call runs. + if reserved { + all.push("drop(reserved);".to_string()); + } + all + }; // Each owned argument the call lends moves to the heap, where it stays while the // future borrows it, and the call reborrows it through the reference it is now. for local in &held { @@ -2021,9 +2508,12 @@ impl Gen<'_> { "{body}{now}\n\trt::Future::new(async move {{ {later} }}, move |result, outs: &[*mut ::std::ffi::c_void], err: *mut ::std::ffi::c_void| -> i32 {{\n\t\tlet _ = (outs, err);\n\t\t{bind}{}\n\t}})", self.finish(function, &ok, ("v", "rt::READY", "rt::ERROR")) ); + self.checking.set(false); + let status_param = status.map(|status| format!("{status}: *mut i32")); self.export(format!( - "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}({}) -> *mut rt::Future {{\n\tlet w = \"{name}\";\n\tlet _ = w;\n\t{make}\n}}\n", - params.join(", "), + "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}({}) -> *mut rt::Future {{\n\tlet w = \"{name}\";\n\tlet _ = w;\n\t{}\n}}\n", + params.iter().chain(&status_param).cloned().collect::>().join(", "), + wrap(status, "*mut rt::Future", &make) )); let poll_params: Vec = poll.params[2..] .iter() @@ -2045,16 +2535,22 @@ impl Gen<'_> { "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {}(future: *mut rt::Future) {{\n\tunsafe {{ rt::Future::free(future) }}\n}}\n", free.name )); - // The call and its first poll in one: most futures are ready at once. + // The call and its first poll in one: most futures are ready at once. A failed + // check writes its status and returns before `future` is written. let start_params: Vec = (params.iter().cloned()) .chain(poll_params) .chain(["future: *mut *mut rt::Future".to_string()]) + .chain(status_param) .collect(); + let started = format!( + "let made = {{\n\t{make}\n\t}};\n\tunsafe {{ rt::Future::start(made, &[{outs}], {err}, future, w) }}" + ); self.export(format!( - "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {}({}) -> i32 {{\n\tlet w = \"{}\";\n\tlet _ = w;\n\tlet made = {{\n\t{make}\n\t}};\n\tunsafe {{ rt::Future::start(made, &[{outs}], {err}, future, w) }}\n}}\n", + "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {}({}) -> i32 {{\n\tlet w = \"{}\";\n\tlet _ = w;\n\t{}\n}}\n", start.name, start_params.join(", "), - start.name + start.name, + wrap(status, "i32", &started) )); return; } @@ -2070,22 +2566,42 @@ impl Gen<'_> { None => self.finish(function, &function.ok, ("", "", "")), }; self.lends = false; + let call = match (function.collect, &function.err, &function.access) { + // An iterator's items, all taken now. + (true, ..) => format!("::std::iter::Iterator::collect::<::std::vec::Vec<_>>({call})"), + // A parse's error is its message already. + (_, _, Access::Parse) => call, + // A `String` error may stand for another crate's, as its message. + (_, Some(Type::String), _) => format!("({call}).map_err(|e| ::std::string::ToString::to_string(&e))"), + _ => call, + }; + self.checking.set(false); self.export(format!( - "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}({}) -> {ret} {{\n\tlet w = \"{name}\";\n\tlet _ = w;\n\t{body}\n\tlet result = {};{writes}\n\t{finish}\n}}\n", + "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}({}) -> {ret} {{\n\tlet w = \"{name}\";\n\tlet _ = w;\n\t{}\n}}\n", params.join(", "), - match (function.collect, &function.err, &function.access) { - // An iterator's items, all taken now. - (true, ..) => format!("::std::iter::Iterator::collect::<::std::vec::Vec<_>>({call})"), - // A parse's error is its message already. - (_, _, Access::Parse) => call, - // A `String` error may stand for another crate's, as its message. - (_, Some(Type::String), _) => format!("({call}).map_err(|e| ::std::string::ToString::to_string(&e))"), - _ => call, - }, + wrap(status, &ret, &format!("{body}\n\tlet result = {call};{writes}\n\t{finish}")) )); } } +/// The body of a C function returning `ret`, which runs `body`, statements then the value +/// returned: if it takes `status`, as a closure whose failed check `?` returns, written +/// through `status`. +fn wrap(status: Option<&str>, ret: &str, body: &str) -> String { + let Some(status) = status else { + return body.to_string(); + }; + format!( + "let checked = (|| -> ::std::result::Result<{ret}, rt::Status> {{\n\t{body_ok}\n\t}})();\n\tmatch checked {{\n\t\tOk(v) => v,\n\t\tErr(s) => unsafe {{ rt::failed({status}, s, w) }},\n\t}}", + body_ok = ok_tail(body) + ) +} + +/// `body`, statements then a value, with the value wrapped in `Ok`. +fn ok_tail(body: &str) -> String { + format!("Ok({{\n\t{body}\n\t}})") +} + /// What a function returns, owned: each reference at its top or in a returned tuple as /// its clone's type, with the expression cloning `v` into it, if any is one. fn owned(ok: &Type) -> (Type, Option) { diff --git a/ffi/src/go.rs b/ffi/src/go.rs index 1a804397e..304567817 100644 --- a/ffi/src/go.rs +++ b/ffi/src/go.rs @@ -354,6 +354,7 @@ const TAKEN: &[&str] = &[ "BoundExcluded", "Bytes", "NewBytes", + "ErrBorrowed", "ErrClosed", "Handle_", "NewHandle_", @@ -607,6 +608,12 @@ impl<'a> Gen<'a> { var target_ uint64\n\n\ // On_ panics unless the library's target is among those of on, as the item what is.\n\ func On_(on uint64, what string) {{\n\tif on>>target_&1 == 0 {{\n\t\tpanic(what + \": it is not on this library's target\")\n\t}}\n}}\n\n\ + // Supported_ is the error a call of what returns, matching errors.ErrUnsupported,\n\ + // unless the library's target is among those of on, as the item what is.\n\ + func Supported_(on uint64, what string) error {{\n\tif on>>target_&1 == 0 {{\n\t\treturn checkError_{{what, \"it is not on this library's target\", 0}}\n\t}}\n\treturn nil\n}}\n\n\ + // Status_ is the error a call of what returns, given the status it wrote: nil for\n\ + // none.\n\ + func Status_(status int32, what string) error {{\n\tif status == 0 {{\n\t\treturn nil\n\t}}\n\treturn checkError_{{what, C.GoString(C.{p}_status_message(C.{p}_status(status))), status}}\n}}\n\n\ //export {go}_call\nfunc {go}_call(data unsafe.Pointer) {{\n\tgiven_(data).(func())()\n}}\n\n\ //export {go}_release\nfunc {go}_release(data unsafe.Pointer) {{\n\trelease_(data)\n}}\n\n\ //export {go}_poll\nfunc {go}_poll(data unsafe.Pointer, waker cWaker) C.bool {{\n\treturn C.bool(given_(data).(*pollable_).poll(waker))\n}}\n\n\ @@ -1421,6 +1428,9 @@ impl<'a> Gen<'a> { } // A value holding an object fails once that object is closed, as an object does. let closable = object || self.holds(ty); + // On a target the type is not on, only the binding makes a value of it, and each + // trait fails as a function the target lacks does. + let limited = !self.abi.api.everywhere(self.abi.on(ty)); // The receiver, a held handle or the value lowered, and the return of `failed`, an // expression of the error, once a closed object fails the call. let open = |failed: &dyn Fn(&str) -> String| match (object, arena) { @@ -1454,7 +1464,25 @@ impl<'a> Gen<'a> { results, body, }; - // fmt's String and GoString return no error, so a closed one reads as the error. + // A check each trait's borrow can fail. + let status = self.abi.traits_status_of(ty); + let status_error = match status { + true => self.qualify(Packages::RT, "Status_"), + false => String::new(), + }; + // The call `call`, binding its result to `r_` then returning `failed` of its status's + // error, if it can fail a check. + let checked = |call: &str, name: &str, failed: &dyn Fn(&str) -> String| match status { + true => format!( + "var c_status_ C.{}\nr_ := {}, &c_status_)\nif c_status_ != 0 {{\n\treturn {}\n}}\n", + self.abi.rt("status"), + call.strip_suffix(')').expect("a call"), + failed(&format!("{status_error}(int32(c_status_), \"{name}\")")) + ), + false => format!("r_ := {call}\n"), + }; + // fmt's String and GoString return no error, so a closed one, or a failed check, reads + // as the error. let text = |e: &str| format!("{e}.Error()"); let mut methods = Vec::new(); if t.display { @@ -1464,7 +1492,10 @@ impl<'a> Gen<'a> { "String", Vec::new(), vec!["string".to_string()], - format!("{open}return liftString_(C.{c}_to_string({this}))"), + format!( + "{open}{}return liftString_(r_)", + checked(&format!("C.{c}_to_string({this})"), &format!("{c}_to_string"), &text) + ), )); } if t.debug { @@ -1474,7 +1505,10 @@ impl<'a> Gen<'a> { "GoString", Vec::new(), vec!["string".to_string()], - format!("{open}return liftString_(C.{c}_debug({this}))"), + format!( + "{open}{}return liftString_(r_)", + checked(&format!("C.{c}_debug({this})"), &format!("{c}_debug"), &text) + ), )); } if object && t.clone { @@ -1484,7 +1518,12 @@ impl<'a> Gen<'a> { "Clone", Vec::new(), vec![name.clone(), "error".to_string()], - format!("{open}return own_{c}(C.{c}_clone({this})), nil"), + format!( + "{open}{}return own_{c}(r_), nil", + checked(&format!("C.{c}_clone({this})"), &format!("{c}_clone"), &|e| format!( + "nil, {e}" + )) + ), )); } if t.eq { @@ -1493,24 +1532,26 @@ impl<'a> Gen<'a> { true => open(&failed), false => open(&|_: &str| unreachable!("a value holding no object is never closed")), }; - let (results, ok) = match closable { + let (results, ok) = match closable || limited { true => (vec!["bool".to_string(), "error".to_string()], ", nil"), false => (vec!["bool".to_string()], ""), }; let body = match (object, arena) { (true, _) => format!( - "{open}o_ := other.Handle_.Hold()\nif o_ == nil {{\n\treturn {}\n}}\ndefer unhold_(other.Handle_, other)\nreturn bool(C.{c}_eq({this}, (*C.{c})(o_))){ok}", - failed("other.Handle_.Closed()") + "{open}o_ := other.Handle_.Hold()\nif o_ == nil {{\n\treturn {}\n}}\ndefer unhold_(other.Handle_, other)\n{}return bool(r_){ok}", + failed("other.Handle_.Closed()"), + checked(&format!("C.{c}_eq({this}, (*C.{c})(o_))"), &format!("{c}_eq"), &failed) ), (false, true) if closable => format!( - "{open}c_other_ := lower_{c}(a_, other)\nif a_.err != nil {{\n\treturn {}\n}}\nreturn bool(C.{c}_eq({this}, &c_other_)){ok}", - failed("a_.err") + "{open}c_other_ := lower_{c}(a_, other)\nif a_.err != nil {{\n\treturn {}\n}}\n{}return bool(r_){ok}", + failed("a_.err"), + checked(&format!("C.{c}_eq({this}, &c_other_)"), &format!("{c}_eq"), &failed) ), (false, true) => { - format!("{open}c_other_ := lower_{c}(a_, other)\nreturn bool(C.{c}_eq({this}, &c_other_))") + format!("{open}c_other_ := lower_{c}(a_, other)\nreturn bool(C.{c}_eq({this}, &c_other_)){ok}") } (false, false) => { - format!("{open}c_other_ := lower_{c}(other)\nreturn bool(C.{c}_eq({this}, &c_other_))") + format!("{open}c_other_ := lower_{c}(other)\nreturn bool(C.{c}_eq({this}, &c_other_)){ok}") } }; methods.push(method( @@ -1527,22 +1568,37 @@ impl<'a> Gen<'a> { true => format!("own_{c}(C.{c}_default())"), false => format!("lift_{c}(C.{c}_default())"), }; + let (results, ok) = match limited { + true => (vec![name.clone(), "error".to_string()], ", nil"), + false => (vec![name.clone()], ""), + }; Method { key: None, docs: format!("{func} is Rust's Default."), receiver: None, name: func, params: Vec::new(), - results: vec![name.clone()], - body: format!("return {lift}"), + results, + body: format!("return {lift}{ok}"), } }); let (mut methods, mut default) = (methods, default); - // On a target a value's type is not on, only the binding makes one, which calls in - // for none of these. - if let Some(check) = self.on(self.abi.on(ty), c) { + if limited { + let supported = self.qualify(Packages::RT, "Supported_"); + let on = self.abi.on(ty).0 & self.abi.api.all().0; + let zero = self.zero(ty); for method in methods.iter_mut().chain(default.as_mut()) { - method.body = format!("{check}\n{}", method.body); + let (what, failed) = match method.name.as_str() { + "String" => ("to_string", "err_.Error()".to_string()), + "GoString" => ("debug", "err_.Error()".to_string()), + "Clone" => ("clone", "nil, err_".to_string()), + "Equal" => ("eq", "false, err_".to_string()), + _ => ("default", format!("{zero}, err_")), + }; + method.body = format!( + "if err_ := {supported}({on}, \"{c}_{what}\"); err_ != nil {{\n\treturn {failed}\n}}\n{}", + method.body + ); } } (methods, default) @@ -1941,6 +1997,17 @@ impl<'a> Gen<'a> { let name = self.named(&ty).1; let rust = abi.api.rust(&object.path, &object.args); let handle = self.qualify(Packages::RT, "Handle_"); + let close = match abi.checked[i] { + true => { + "// Close drops the value now, as Rust's drop does, and frees the handle once no call holds\n\ + // it: a call after it fails, and Close fails while a call borrows the value, changing\n\ + // nothing.\n" + } + false => { + "// Close frees the handle now, rather than once the GC collects it: a call after it fails,\n\ + // and Close fails while a call holds it, changing nothing.\n" + } + }; let text = format!( "{name} is `{rust}`, a handle Close frees, or else the GC once it collects it.\n\n{}", object.docs @@ -1948,9 +2015,7 @@ impl<'a> Gen<'a> { let _ = write!( self.types, "{}type {name} struct {{\n\t// Handle_ is the handle it owns, for the generated packages: use none of it.\n\tHandle_ *{handle}\n}}\n\n\ - // Close frees the handle once no call holds it, rather than once the GC collects it: a\n\ - // call after it panics.\n\ - func (self *{name}) Close() {{\n\tself.Handle_.Close()\n}}\n\n", + {close}func (self *{name}) Close() error {{\n\treturn self.Handle_.Close()\n}}\n\n", docs(text.trim(), "") ); let mut members = vec!["Close".to_string(), "Handle_".to_string()]; @@ -1971,12 +2036,25 @@ impl<'a> Gen<'a> { let name = self.qualified(&ty); let handle = self.qualify(Packages::RT, "Handle_"); let new = self.qualify(Packages::RT, "NewHandle_"); + let (drop, dropping) = match self.abi.checked[i] { + true => ( + format!( + "// drop_{c} drops the value of the handle p, which Rust checks.\n\ + func drop_{c}(p unsafe.Pointer) error {{\n\tvar s C.{status}\n\tC.{c}_drop((*C.{c})(p), &s)\n\treturn {status_}(int32(s), \"{c}_drop\")\n}}\n\n", + status = self.abi.rt("status"), + status_ = self.qualify(Packages::RT, "Status_"), + ), + format!("drop_{c}"), + ), + false => (String::new(), "nil".to_string()), + }; format!( "func free_{c}(p unsafe.Pointer) {{\n\tC.{c}_free((*C.{c})(p))\n}}\n\n\ + {drop}\ // own_{c} owns a handle Rust returned, or is nil for none.\n\ - func own_{c}(raw *C.{c}) *{name} {{\n\tif raw == nil {{\n\t\treturn nil\n\t}}\n\tv := &{name}{{Handle_: {new}(unsafe.Pointer(raw), free_{c}, \"{local}\")}}\n\tv.Handle_.Cleanup = runtime.AddCleanup(v, (*{handle}).Close, v.Handle_)\n\treturn v\n}}\n\n\ + func own_{c}(raw *C.{c}) *{name} {{\n\tif raw == nil {{\n\t\treturn nil\n\t}}\n\tv := &{name}{{Handle_: {new}(unsafe.Pointer(raw), free_{c}, {dropping}, \"{local}\")}}\n\tv.Handle_.Cleanup = runtime.AddCleanup(v, (*{handle}).Collected, v.Handle_)\n\treturn v\n}}\n\n\ // lend_{c} is a handle Rust lends for a call, which it frees itself.\n\ - func lend_{c}(raw *C.{c}) *{name} {{\n\treturn &{name}{{Handle_: {new}(unsafe.Pointer(raw), nil, \"{local}\")}}\n}}\n\n\ + func lend_{c}(raw *C.{c}) *{name} {{\n\treturn &{name}{{Handle_: {new}(unsafe.Pointer(raw), nil, nil, \"{local}\")}}\n}}\n\n\ // holdOption_{c} is v's handle, held until the call returns, or nil for none.\n\ func holdOption_{c}(a_ *arena_, v *{name}) *C.{c} {{\n\tif v == nil {{\n\t\treturn nil\n\t}}\n\treturn (*C.{c})(a_.hold(v.Handle_, v))\n}}\n\n" ) @@ -2188,12 +2266,12 @@ impl<'a> Gen<'a> { } } - /// The Go statement closing each object the value `e` of type `ty` holds, once a call - /// moved them out. + /// The Go statement closing each handle the value `e` of type `ty` holds, once a call + /// moved their values out. fn close(&self, ty: &Type, e: &str, depth: usize) -> String { match ty { Type::Option(inner) => match inner.base() { - Type::Object(_) => format!("if {e} != nil {{ {e}.Close() }}"), + Type::Object(_) => format!("if {e} != nil {{ {e}.Handle_.Moved() }}"), _ => { let v = format!("v{depth}_"); format!("if {e} != nil {{ {v} := *{e}; {} }}", self.close(inner, &v, depth + 1)) @@ -2212,7 +2290,7 @@ impl<'a> Gen<'a> { let v = format!("v{depth}_"); format!("for _, {v} := range {e} {{ {} }}", self.close(inner, &v, depth + 1)) } - _ => format!("{e}.Close()"), + _ => format!("{e}.Handle_.Moved()"), } } @@ -2288,8 +2366,12 @@ impl<'a> Gen<'a> { let mut results: Vec = oks.iter().map(|ty| self.go(ty)).collect(); // A call that takes an object, as its receiver or held in any value it takes, // returns an error once that object is closed. + let status = export.call.status().is_some(); + let everywhere = abi.api.everywhere(function.on); let closable = receiver.is_some() && owner_ty.is_some_and(|ty| self.holds(ty)) - || function.params.iter().any(|p| self.holds(&p.ty)); + || function.params.iter().any(|p| self.holds(&p.ty)) + || status + || !everywhere; if function.err.is_some() || function.future || closable { results.push("error".to_string()); } @@ -2303,7 +2385,8 @@ impl<'a> Gen<'a> { }; let mut prelude: Vec = Vec::new(); - let mut after: Vec = Vec::new(); + // What closes each handle a call moved the value out of, once it passed its checks. + let mut moves: Vec = Vec::new(); let mut checked = false; let mut arena = false; let mut args: Vec = Vec::new(); @@ -2328,7 +2411,7 @@ impl<'a> Gen<'a> { args.push(format!("(*C.{name})(h_)")); let copy = matches!(ty, Type::Object(i) if abi.api.objects[i].copy); if receiver == Receiver::Owned && !copy { - after.push("self.Close()".to_string()); + moves.push("self.Handle_.Moved()".to_string()); } } _ => { @@ -2383,7 +2466,7 @@ impl<'a> Gen<'a> { } params.push((name.clone(), self.go_arg(¶m.ty))); if moved(abi, ¶m.ty) { - after.push(self.close(¶m.ty, &name, 0)); + moves.push(self.close(¶m.ty, &name, 0)); } match (¶m.ty, c) { (Type::Closure(closure), _) => { @@ -2464,8 +2547,30 @@ impl<'a> Gen<'a> { } } } - let outs: Vec<(String, Ty)> = c_params.cloned().collect(); + let outs: Vec<(String, Ty)> = c_params.filter(|(_, ty)| !abi::is_status(ty)).cloned().collect(); + // A failed check writes its status, whose error the call returns; once it passed, + // each handle the call moved out of closes. + let status_error = match status { + true => format!( + "{}(int32(c_status_), \"{c_name}\")", + self.qualify(Packages::RT, "Status_") + ), + false => String::new(), + }; + let moved = moves.join("\n"); + let failed = status.then(|| { + args.push("&c_status_".to_string()); + let failed = format!("if c_status_ != 0 {{\n\t{}\n}}", fail(&status_error)); + match moved.is_empty() { + true => failed, + false => format!("{failed}\n{moved}"), + } + }); + assert!(status || moves.is_empty(), "{c_name} moves a value Rust checks"); let mut body = Vec::new(); + if status { + body.push(format!("var c_status_ C.{}", abi.rt("status"))); + } if arena { body.push("a_ := &arena_{}\ndefer a_.finish()".to_string()); prelude.insert(take_at, "a_.take = true".to_string()); @@ -2486,12 +2591,6 @@ impl<'a> Gen<'a> { if arena { body.push("a_.made = true".to_string()); } - if !after.is_empty() { - match &after[..] { - [one] if one.ends_with("Close()") && !one.contains(' ') => body.push(format!("defer {one}")), - _ => body.push(format!("defer func() {{\n\t{}\n}}()", after.join("\n\t"))), - } - } if let Some(abi::Future { handle, @@ -2536,11 +2635,9 @@ impl<'a> Gen<'a> { // An eager call runs Rust, and any callback it makes, before its future // exists, and a callback may cancel the context: so it is made, then the // context checked, then polled, never started. - body.push(format!( - "f_ := C.{c_name}({})\ndefer C.{}(f_)", - args.join(", "), - free.name - )); + body.push(format!("f_ := C.{c_name}({})", args.join(", "))); + body.extend(failed.clone()); + body.push(format!("defer C.{}(f_)", free.name)); body.push(format!( "if err_ := ctx.Err(); err_ != nil {{\n\treturn {zeros}err_\n}}" )); @@ -2550,20 +2647,36 @@ impl<'a> Gen<'a> { // A context done already takes nothing, even what is ready: the future is made // and dropped unpolled, as Rust drops one it never awaits. Making an `async fn`'s // future runs nothing, so the check needs no repeat after it. + // What it moved out of closes once its checks passed, as the future owns it. + let dropped = match (status, moved.is_empty()) { + (true, false) => format!("\n\tif c_status_ == 0 {{\n\t\t{}\n\t}}", moved.replace('\n', "\n\t\t")), + _ => String::new(), + }; body.push(format!( - "if err_ := ctx.Err(); err_ != nil {{\n\tC.{}(C.{c_name}({}))\n\treturn {zeros}err_\n}}", + "if err_ := ctx.Err(); err_ != nil {{\n\tC.{}(C.{c_name}({})){dropped}\n\treturn {zeros}err_\n}}", free.name, args.join(", ") )); + // The status follows every out, and the future's handle. let mut start_args = args.clone(); + let status_arg = status.then(|| start_args.pop().expect("the status")); start_args.extend(ptrs.iter().cloned()); start_args.push("&f_".to_string()); + start_args.extend(status_arg); + let started = format!("C.{}({})", start.name, start_args.join(", ")); let mut inner = decls; - inner.push(format!( - "switch C.{}({}) {{\n{cases}\n}}\nreturn false, nil", - start.name, - start_args.join(", ") - )); + match status { + true => { + inner.push(format!( + "started_ := {started}\nif c_status_ != 0 {{\n\treturn true, {status_error}\n}}" + )); + if !moved.is_empty() { + inner.push(moved.clone()); + } + inner.push(format!("switch started_ {{\n{cases}\n}}\nreturn false, nil")); + } + false => inner.push(format!("switch {started} {{\n{cases}\n}}\nreturn false, nil")), + } body.push(format!("var f_ *C.{handle}")); body.push(format!( "done_, err_ := func() (bool, error) {{\n{}\n}}()", @@ -2583,8 +2696,31 @@ impl<'a> Gen<'a> { body.push(self.out_decl(n, ty)); } let mut call_args = args.clone(); + // The status follows every out. + let status_arg = status.then(|| call_args.pop().expect("the status")); call_args.extend(outs.iter().map(|(n, _)| format!("&c_{n}_"))); + call_args.extend(status_arg); let mut call = format!("C.{c_name}({})", call_args.join(", ")); + // Made first, so a failed check returns before any result is read. + let made = failed.map(|failed| { + let made = match (&err, &export.call.ret) { + (Some(_), _) => { + body.push(format!("ok_ := {call}")); + "ok_".to_string() + } + (None, Ty::Void) => { + body.push(call.clone()); + String::new() + } + // Lifted from the out the result names, as a value written beside one is. + (None, _) => { + body.push(format!("c_out_ := {call}")); + String::new() + } + }; + body.push(failed); + made + }); // Each value passed for `&mut` takes what Rust wrote back, on every path, as does a // value's own `&mut self`, which Rust wrote back in place. let mut writes: Vec = match export.call.params.first() { @@ -2602,7 +2738,10 @@ impl<'a> Gen<'a> { format!("*{name} = {}", self.lift(param.ty.base(), &format!("c_{out}_"))) }), ); - if !writes.is_empty() { + if let Some(made) = made { + body.extend(writes); + call = made; + } else if !writes.is_empty() { let made = match &err { Some(_) => { body.push(format!("ok_ := {call}")); @@ -2709,8 +2848,10 @@ impl<'a> Gen<'a> { "\n\nIt blocks until Rust's future is ready, or ctx is done: then it cancels the future in\nRust, losing nothing, and returns ctx's error.", ); } - if let Some(check) = self.on(function.on, &c_name) { - body.insert(0, check); + if !everywhere { + let supported = self.qualify(Packages::RT, "Supported_"); + let error = format!("{supported}({}, \"{c_name}\")", function.on.0 & abi.api.all().0); + body.insert(0, format!("if err_ := {error}; err_ != nil {{\n\t{}\n}}", fail("err_"))); } Method { key: Some(crate::lock::function_key(function)), diff --git a/ffi/src/ir.rs b/ffi/src/ir.rs index edd98c5a3..93944f386 100644 --- a/ffi/src/ir.rs +++ b/ffi/src/ir.rs @@ -35,6 +35,28 @@ pub struct Reached { } impl Api { + /// Each object whose handle a call borrows or moves as it lifts a value of `ty`, into + /// `out`: its own, and those its records and enums hold. `seen` holds the records and + /// enums already walked, as `(is_enum, index)`. + pub fn handles(&self, ty: &Type, out: &mut Vec, seen: &mut Vec<(bool, usize)>) { + match ty { + Type::Object(i) => out.push(*i), + Type::Option(inner) | Type::Box(inner) | Type::Ref(_, inner) | Type::List(_, inner) => { + self.handles(inner, out, seen) + } + Type::Tuple(elements) => elements.iter().for_each(|ty| self.handles(ty, out, seen)), + Type::Record(i) if !seen.contains(&(false, *i)) => { + seen.push((false, *i)); + (self.records[*i].fields.iter()).for_each(|f| self.handles(&f.ty, out, seen)); + } + Type::Enum(i) if !seen.contains(&(true, *i)) => { + seen.push((true, *i)); + (self.enums[*i].variants.iter().flat_map(|v| &v.fields)).for_each(|f| self.handles(&f.ty, out, seen)); + } + _ => {} + } + } + /// Whether a value of `ty` holds a float anywhere, its records' and enums' included, so a /// language comparing it itself must use IEEE `==`, as Rust does: NaN unequal even to /// itself, and -0.0 equal to 0.0. diff --git a/ffi/src/kotlin.rs b/ffi/src/kotlin.rs index 79d220b00..3596102e3 100644 --- a/ffi/src/kotlin.rs +++ b/ffi/src/kotlin.rs @@ -114,7 +114,7 @@ pub fn generate(abi: &Abi<'_>, packages: &str, contract: u64) -> Result, packages: &str, contract: u64) -> Result { } impl<'a> Gen<'a> { + /// The Kotlin call `raw`, then `after`, which closes what it moved out, however it ended + /// unless a failed check refused it, which moved nothing. It binds no name a parameter + /// `after` closes may take: its exception's is fresh, and its result is `_`. + fn moving(&mut self, raw: &str, after: &[String]) -> String { + let after = after.join("; "); + let thrown = self.fresh("e"); + format!( + "try {{\n{}\n}} catch ({thrown}: Throwable) {{\n\tif ({thrown} !is _Native.__Refused) {{ {after} }}\n\tthrow {thrown}\n}}.also {{ _ -> {after} }}", + indent_by(raw, 1) + ) + } + fn fresh(&mut self, base: &str) -> String { loop { self.fresh += 1; @@ -950,8 +962,8 @@ impl<'a> Gen<'a> { // Taken by value, the value moves out, leaving the handle only to free. if moved(abi, ty) { arg.after.push(match option { - true => format!("{e}?.close()"), - false => format!("{e}.close()"), + true => format!("{e}?.handle?.abandon()"), + false => format!("{e}.handle.abandon()"), }); } arg.exprs.push(h); @@ -1116,7 +1128,7 @@ impl<'a> Gen<'a> { arg.rust_args.push(format!("rt::future(env, {p})")); } // A value's `&mut self` is handed back, through `&self`. - Ty::Void | Ty::Future(_) | Ty::Poll | Ty::Out(_) | Ty::InOut(_) | Ty::Data => { + Ty::Void | Ty::Future(_) | Ty::Poll | Ty::Status | Ty::Out(_) | Ty::InOut(_) | Ty::Data => { unreachable!("{c:?} is no parameter Kotlin passes") } } @@ -1313,12 +1325,25 @@ impl<'a> Gen<'a> { ) } }; + // Each borrows its handles shared, throwing a failed check. + let status = self.abi.traits_status_of(ty); + let checked = |call: &str, name: &str, then: &str| match status { + true => format!( + "let mut status = 0;\n\t\tlet ret = {}, &mut status);\n\t\tif status != 0 {{\n\t\t\treturn rt::refuse(env, status, \"{name}\");\n\t\t}}\n\t\t{then}", + call.strip_suffix(')').expect("a call") + ), + false => format!("let ret = {call};\n\t\t{then}"), + }; let unary = |g: &mut Self, suffix: &str, sig: &str| { g.native( &format!("{bare}_{suffix}"), ¶ms, ("jbyteArray", "ByteArray?"), - &format!("rt::owned(env, crate::{c}_{suffix}({rust_self}))"), + &checked( + &format!("crate::{c}_{suffix}({rust_self})"), + &format!("{c}_{suffix}"), + "rt::owned(env, ret)", + ), ); format!("\t{sig} = {open}\n\t\t_Native.{bare}_{suffix}({arg})!!.decodeToString()\n\t}}\n") }; @@ -1339,7 +1364,11 @@ impl<'a> Gen<'a> { &format!("{bare}_clone"), &[("jlong", "Long")], ("jlong", "Long"), - &format!("crate::{c}_clone(p0 as usize as _) as usize as jlong"), + &checked( + &format!("crate::{c}_clone(p0 as usize as _)"), + &format!("{c}_clone"), + "ret as usize as jlong", + ), ); body.push(format!( "\t/** A new handle to a clone. */\n\tfun clone(): {class} = handle.hold {{ h -> {class}(_Native.{bare}_clone(h)) }}\n" @@ -1366,7 +1395,11 @@ impl<'a> Gen<'a> { &format!("{bare}_eq"), &[("jlong", "Long"), ("jlong", "Long")], ("jboolean", "Boolean"), - &format!("crate::{c}_eq(p0 as usize as _, p1 as usize as _)"), + &checked( + &format!("crate::{c}_eq(p0 as usize as _, p1 as usize as _)"), + &format!("{c}_eq"), + "ret", + ), ); body.push(format!( "\toverride fun equals(other: kotlin.Any?): kotlin.Boolean =\n\t\tother is {class} && handle.hold {{ a -> other.handle.hold {{ b -> _Native.{bare}_eq(a, b) }} }}\n\n\t/** One for every value: Rust's `PartialEq` comes with no `Hash`. */\n\toverride fun hashCode(): kotlin.Int = 0\n" @@ -1732,9 +1765,24 @@ impl<'a> Gen<'a> { ("()", "Unit"), &format!("crate::{c}_free(p0 as usize as _)"), ); - let mut body = vec![format!( - "\tinternal val handle = _Native.__Handle(this, raw, if (lent) null else _Native.__Drop {{ _Native.{bare}_free(it) }}, \"{name}\")\n\n\t/** Free the handle once no call holds it: a call after this throws. */\n\toverride fun close() = handle.close()\n" - )]; + let mut body = vec![match abi.checked[i] { + true => { + self.native( + &format!("{bare}_drop"), + &[("jlong", "Long")], + ("()", "Unit"), + &format!( + "let mut status = 0;\n\t\tcrate::{c}_drop(p0 as usize as _, &mut status);\n\t\tif status != 0 {{\n\t\t\treturn rt::refuse(env, status, \"{c}_drop\");\n\t\t}}" + ), + ); + format!( + "\tinternal val handle = _Native.__Handle(this, raw, if (lent) null else _Native.__Drop {{ _Native.{bare}_free(it) }}, \"{name}\", if (lent) null else _Native.__Close {{ _Native.{bare}_drop(it) }})\n\n\t/** Drop the value now, as Rust's drop does, and free the handle once no call holds it: a call after this throws, and one still borrowing the value makes this throw. */\n\toverride fun close() = handle.close()\n" + ) + } + false => format!( + "\tinternal val handle = _Native.__Handle(this, raw, if (lent) null else _Native.__Drop {{ _Native.{bare}_free(it) }}, \"{name}\")\n\n\t/** Free the handle now: a call after this throws, and one still running makes this throw. */\n\toverride fun close() = handle.close()\n" + ), + }]; let mut statics = Vec::new(); self.traits(&ty, &c, object.traits, &mut body, &mut statics); let owner = Ty::Handle { @@ -2177,7 +2225,7 @@ impl<'a> Gen<'a> { arg.rust_args.push(format!("{p} as usize as _")); let copy = matches!(ty, Type::Object(i) if abi.api.objects[i].copy); if receiver == Receiver::Owned && !copy { - arg.after.push("this.close()".to_string()); + arg.after.push("this.handle.abandon()".to_string()); } arg } @@ -2231,7 +2279,13 @@ impl<'a> Gen<'a> { (&kt, None), ); } - let outs: Vec<(String, Ty)> = c_params.cloned().collect(); + let outs: Vec<(String, Ty)> = c_params.filter(|(_, ty)| !abi::is_status(ty)).cloned().collect(); + // A failed check writes its status, which the glue throws once the call returns. + let status = export.call.status().is_some(); + let check = match status { + true => format!("\n\t\tif status != 0 {{\n\t\t\treturn rt::refuse(env, status, \"{c_name}\");\n\t\t}}"), + false => String::new(), + }; let native_params: Vec<(&str, String)> = args .iter() @@ -2241,7 +2295,10 @@ impl<'a> Gen<'a> { // A future's call also takes the signal its waker parks, if its first poll pends. let signal = export.future.is_some().then(|| self.fresh("s")); exprs.extend(signal.clone()); - let rust_pre: Vec = args.iter().flat_map(|a| a.rust_pre.clone()).collect(); + let mut rust_pre: Vec = args.iter().flat_map(|a| a.rust_pre.clone()).collect(); + if status { + rust_pre.push("let mut status = 0;".to_string()); + } let rust_args: Vec = args.iter().flat_map(|a| a.rust_args.clone()).collect(); let after: Vec = args.iter().flat_map(|a| a.after.clone()).collect(); @@ -2296,8 +2353,9 @@ impl<'a> Gen<'a> { // The value the function returns, or, returning nothing, its statement. let value; // The C call, with the out pointers after its arguments. + let status_arg = status.then(|| "&mut status".to_string()); let rust_call = |outs: &[String]| { - let args: Vec = rust_args.iter().chain(outs).cloned().collect(); + let args: Vec = rust_args.iter().chain(outs).chain(&status_arg).cloned().collect(); match &handed { Some(h) => h.call(&args), None => format!("crate::{c_name}({})", args.join(", ")), @@ -2347,7 +2405,7 @@ impl<'a> Gen<'a> { ); } self.native(&bare, ¶ms, ("jobject", "Any?"), &format!( - "{}\n\t\tlet f = {};\n\t\tlet w = rt::start(env, f.cast(), {p_signal}, jni_{}, jni_{});\n\t\tstatic FACTORY: std::sync::OnceLock = std::sync::OnceLock::new();\n\t\tw.value(env, &FACTORY, c\"{start_factory}\", std::ptr::null_mut())", + "{}\n\t\tlet f = {};{check}\n\t\tlet w = rt::start(env, f.cast(), {p_signal}, jni_{}, jni_{});\n\t\tstatic FACTORY: std::sync::OnceLock = std::sync::OnceLock::new();\n\t\tw.value(env, &FACTORY, c\"{start_factory}\", std::ptr::null_mut())", rust_pre.join("\n\t\t"), rust_call(&[]), poll.name, free.name)); self.native(&poll_bare, &[("jlong", "Long"), ("jlong", "Long")], ("jobject", "Any?"), &format!( "match rt::poll(p0 as usize as _, p1 as usize as _, jni_{}) {{\n\t\t Some(w) => {{\n\t\t\t\tstatic FACTORY: std::sync::OnceLock = std::sync::OnceLock::new();\n\t\t\t\tw.value(env, &FACTORY, c\"{poll_factory}\", std::ptr::null_mut())\n\t\t\t}},\n\t\t\tNone => std::ptr::null_mut(),\n\t\t}}", poll.name)); @@ -2362,7 +2420,10 @@ impl<'a> Gen<'a> { let call = if after.is_empty() { raw } else { - self.refused_first(&bare, format!("try {{ {raw} }} finally {{ {} }}", after.join("; "))) + { + let moving = self.moving(&raw, &after); + self.refused_first(&bare, moving) + } }; let awaited = format!( "_Native.__Signal().let {{ {s} -> _Native.await({call}, {s}, {{ f, n -> _Native.{poll_bare}(f, n) }}, {{ _Native.{free_bare}(it) }}) }}", @@ -2448,12 +2509,15 @@ impl<'a> Gen<'a> { format!("{{ {invoke}; true }}") }; let params = native_params.iter().map(|(j, k)| (*j, k.as_str())).collect::>(); - self.native(&bare, ¶ms, ("jobject", "Any"), &format!("{}\n\t\t{decl}\n\t\tlet success = {success};\n\t\t{error}\n\t\tlet args = [{}];\n\t\tstatic FACTORY: std::sync::OnceLock = std::sync::OnceLock::new();\n\t\tlet id = rt::method(env, &FACTORY, c\"{factory}\", c\"{sig}\");\n\t\t((**env).v1_2.CallStaticObjectMethodA)(env, rt::class(), id, args.as_ptr())", rust_pre.join("\n\t\t"), values.join(", "))); + self.native(&bare, ¶ms, ("jobject", "Any"), &format!("{}\n\t\t{decl}\n\t\tlet success = {success};{check}\n\t\t{error}\n\t\tlet args = [{}];\n\t\tstatic FACTORY: std::sync::OnceLock = std::sync::OnceLock::new();\n\t\tlet id = rt::method(env, &FACTORY, c\"{factory}\", c\"{sig}\");\n\t\t((**env).v1_2.CallStaticObjectMethodA)(env, rt::class(), id, args.as_ptr())", rust_pre.join("\n\t\t"), values.join(", "))); let raw = format!("_Native.{bare}({})", exprs.join(", ")); let raw = if after.is_empty() { raw } else { - self.refused_first(&bare, format!("try {{ {raw} }} finally {{ {} }}", after.join("; "))) + { + let moving = self.moving(&raw, &after); + self.refused_first(&bare, moving) + } }; value = format!("({raw}) as {ok_kt}"); } else if let Some(c_ok) = direct(&outs, &written, &function.ok, function.err.as_ref()) { @@ -2482,7 +2546,7 @@ impl<'a> Gen<'a> { ¶ms, (jni, native), &format!( - "{}\n\t\t{}\n\t\tif !{} {{\n\t\t\tlet mut w = rt::Writer::default();\n\t\t\t{};\n\t\t\trt::raise(env, w);\n\t\t\treturn std::mem::zeroed();\n\t\t}}\n\t\t{ret}", + "{}\n\t\t{}\n\t\tlet ok = {};{check}\n\t\tif !ok {{\n\t\t\tlet mut w = rt::Writer::default();\n\t\t\t{};\n\t\t\trt::raise(env, w);\n\t\t\treturn std::mem::zeroed();\n\t\t}}\n\t\t{ret}", rust_pre.join("\n\t\t"), decl.join("\n\t\t"), rust_call(&ptrs), @@ -2493,16 +2557,15 @@ impl<'a> Gen<'a> { let e = self.read_value(err.as_ref().expect("a result's error"), &r); let exception = exception.as_deref().expect("a result's exception"); let raw = format!("_Native.{bare}({})", exprs.join(", ")); + let caught = format!( + "try {{\n\t{raw}\n}} catch (f: _Native.__Failed) {{\n\t_Native.__Reader(f.error).let {{ {r} -> throw {exception}({e}) }}\n}}" + ); let caught = match after.is_empty() { - true => format!("try {{\n\t{raw}\n}} catch (f: _Native.__Failed) {{\n\t_Native.__Reader(f.error).let {{ {r} -> throw {exception}({e}) }}\n}}"), - false => self.refused_first( - &bare, - format!( - "try {{\n\t{raw}\n}} finally {{\n\t{}\n}}", - after.join("\n\t") - ) - .replace("try {\n\t", "try {\n\ttry {\n\t\t") - .replacen("\n} finally", &format!("\n\t}} catch (f: _Native.__Failed) {{\n\t\t_Native.__Reader(f.error).let {{ {r} -> throw {exception}({e}) }}\n\t}}\n}} finally"), 1)), + true => caught, + false => { + let moving = self.moving(&caught, &after); + self.refused_first(&bare, moving) + } }; value = match ok { Type::Unit => caught, @@ -2551,7 +2614,7 @@ impl<'a> Gen<'a> { self.native( &bare, ¶ms, ("jobject", "Any?"), &format!( - "{}\n\t\t{decl}\n\t\t{ret_set}{};\n\t\tlet mut w = rt::Writer::default();\n\t\t{write}\n\t\tstatic FACTORY: std::sync::OnceLock = std::sync::OnceLock::new();\n\t\tw.value(env, &FACTORY, c\"{factory}\", {back})", + "{}\n\t\t{decl}\n\t\t{ret_set}{};{check}\n\t\tlet mut w = rt::Writer::default();\n\t\t{write}\n\t\tstatic FACTORY: std::sync::OnceLock = std::sync::OnceLock::new();\n\t\tw.value(env, &FACTORY, c\"{factory}\", {back})", rust_pre.join("\n\t\t"), rust_call(&ptrs)), ); let mut call_args = exprs.clone(); @@ -2569,7 +2632,10 @@ impl<'a> Gen<'a> { let raw = if after.is_empty() { raw } else { - self.refused_first(&bare, format!("try {{ {raw} }} finally {{ {} }}", after.join("; "))) + { + let moving = self.moving(&raw, &after); + self.refused_first(&bare, moving) + } }; value = match ok { Type::Unit => format!("{raw}.let {{ }}"), @@ -2578,7 +2644,7 @@ impl<'a> Gen<'a> { } else if let Some(flat) = flat { let mut params: Vec<(&str, &str)> = native_params.iter().map(|(j, k)| (*j, k.as_str())).collect(); let rest = !flat.puts.is_empty(); - let mut body = vec![rust_pre.join("\n\t\t"), format!("let ret = {};", rust_call(&[]))]; + let mut body = vec![rust_pre.join("\n\t\t"), format!("let ret = {};{check}", rust_call(&[]))]; if flat.option { body.push(format!( "if !ret.some {{\n\t\t\treturn {};\n\t\t}}", @@ -2641,13 +2707,16 @@ impl<'a> Gen<'a> { ); let params: Vec<(&str, &str)> = native_params.iter().map(|(j, k)| (*j, k.as_str())).collect(); self.native(&bare, ¶ms, ("jobject", "Any?"), &format!( - "{}\n\t\tlet ret = {};\n\t\tlet mut w = rt::Writer::default();\n\t\t{};\n\t\tstatic FACTORY: std::sync::OnceLock = std::sync::OnceLock::new();\n\t\tw.value(env, &FACTORY, c\"{factory}\", std::ptr::null_mut())", + "{}\n\t\tlet ret = {};{check}\n\t\tlet mut w = rt::Writer::default();\n\t\t{};\n\t\tstatic FACTORY: std::sync::OnceLock = std::sync::OnceLock::new();\n\t\tw.value(env, &FACTORY, c\"{factory}\", std::ptr::null_mut())", rust_pre.join("\n\t\t"), rust_call(&[]), put(&function.ok, "ret"))); let raw = format!("_Native.{bare}({})", exprs.join(", ")); let raw = if after.is_empty() { raw } else { - self.refused_first(&bare, format!("try {{ {raw} }} finally {{ {} }}", after.join("; "))) + { + let moving = self.moving(&raw, &after); + self.refused_first(&bare, moving) + } }; value = format!("({raw}) as {ok_kt}"); } else { @@ -2671,7 +2740,7 @@ impl<'a> Gen<'a> { ¶ms, (jni, native), &format!( - "{}\n\t\tlet ret = {};\n\t\t{ret_expr}", + "{}\n\t\tlet ret = {};{check}\n\t\t{ret_expr}", rust_pre.join("\n\t\t"), rust_call(&[]) ), @@ -2780,8 +2849,8 @@ impl<'a> Gen<'a> { Some(indent_by(&code, indent_level)) } - /// The Kotlin statement closing each object the value `e` of type `ty` holds, once a - /// call moved them out. + /// The Kotlin statement closing each handle the value `e` of type `ty` holds, once a + /// call moved their values out. fn close(&mut self, ty: &Type, e: &str) -> String { match ty { Type::Option(inner) => { @@ -2803,7 +2872,7 @@ impl<'a> Gen<'a> { let close = self.close(inner, &v); format!("{e}.forEach {{ {v} -> {close} }}") } - _ => format!("{e}.close()"), + _ => format!("{e}.handle.abandon()"), } } @@ -3117,7 +3186,7 @@ fn bytes(ty: &Type) -> bool { } /// Whether passing `ty` moves an object's value out: by value, boxed, or in an `Option`, -/// unless `Copy`, and never through a reference. +/// a list, or a tuple, unless `Copy`, and never through a reference. fn moved(abi: &Abi<'_>, ty: &Type) -> bool { match ty { Type::Object(i) => !abi.api.objects[*i].copy, @@ -3174,7 +3243,7 @@ fn jni_type(abi: &Abi<'_>, c: &Ty, ty: &Type) -> (&'static str, &'static str) { Ty::Waker => ("jobject", "Waker"), Ty::ForeignReader => ("jobject", "java.nio.channels.SeekableByteChannel"), Ty::ForeignFuture => ("jobject", "Any"), - Ty::Poll | Ty::Out(_) | Ty::InOut(_) | Ty::Data => unreachable!("{c:?} never crosses JNI"), + Ty::Poll | Ty::Status | Ty::Out(_) | Ty::InOut(_) | Ty::Data => unreachable!("{c:?} never crosses JNI"), } } diff --git a/ffi/src/lock.rs b/ffi/src/lock.rs index fb0ea7795..af40e1a13 100644 --- a/ffi/src/lock.rs +++ b/ffi/src/lock.rs @@ -265,6 +265,18 @@ pub fn breaks(base: &Api, now: &Api, prefix: &str) -> Vec { changes.push(format!("it is no longer on {}", lost.join(", "))); } changes.extend(changed(&base, &now, &maps, &ours.item, &theirs.item)); + if let (Item::Object(i), Item::Object(j)) = (ours.item, theirs.item) { + match (base.checked[i], now.checked[j]) { + (false, true) => changes.push( + "its handle is now checked at run time, so each call taking it gains a status, and throws in Swift" + .to_string(), + ), + (true, false) => changes.push( + "its handle is no longer checked at run time, so each call taking it loses its status".to_string(), + ), + _ => {} + } + } if ours.c != theirs.c { let mut renamed = format!("its C name `{}` is now `{}`", ours.c, theirs.c); if let Some(other) = now.named(&ours.c) { @@ -325,6 +337,9 @@ struct Index<'a> { /// Each tuple's struct by its Rust spelling: its C name, and a function that takes or /// returns it. tuples: BTreeMap, + /// Whether a call can fail a check of each object's handle, which gives each call + /// taking it a status. + checked: Vec, } impl<'a> Index<'a> { @@ -460,7 +475,12 @@ impl<'a> Index<'a> { }); } } - Self { api, entries, tuples } + Self { + api, + entries, + tuples, + checked: abi.checked.clone(), + } } /// The item whose C name is `c`. diff --git a/ffi/src/python.rs b/ffi/src/python.rs index c0fb84301..e0c72f46f 100644 --- a/ffi/src/python.rs +++ b/ffi/src/python.rs @@ -551,11 +551,10 @@ impl<'a> Gen<'a> { } /// The glue's expression, an `R`, reading the Python object `o` as the C value of - /// `ty`, with what it borrows held in the scope `s`; `m` is the glue's `bool`, whether - /// the position moves an object's value out, closing it, and `t`, whether Rust takes - /// what the position holds, as it takes a value passed by value but not one read - /// through a pointer or an array's item, so a buffer crosses there as its own. - fn lift(&mut self, ty: &Type, o: &str, m: &str, t: &str) -> String { + /// `ty`, with what it borrows held in the scope `s`; `t` is the glue's `bool`, whether + /// Rust takes what the position holds, as it takes a value passed by value but not one + /// read through a pointer or an array's item, so a buffer crosses there as its own. + fn lift(&mut self, ty: &Type, o: &str, t: &str) -> String { let abi = self.abi; match ty { Type::Const(_) => unreachable!("a const argument is no value"), @@ -575,27 +574,21 @@ impl<'a> Gen<'a> { Type::Duration => format!("rt::duration_in({o})"), Type::SocketAddr(family) => format!("rt::addr_in({o}, {})", family_of(*family)), Type::List(_, inner) if ty.block().is_some() => format!("rt::block_in::<{}>({o}, s)", number(inner)), - Type::List(..) => format!("r_{}({o}, s, {m}, {t})", self.converter(ty)), + Type::List(..) => format!("r_{}({o}, s, {t})", self.converter(ty)), Type::Option(inner) => match inner.base() { - Type::Object(i) => { - let m = match abi.api.objects[*i].copy || matches!(**inner, Type::Ref(..)) { - true => "false", - false => m, - }; - format!("rt::hold_opt({o}, {}, s, {m})", self.t(&abi.object(*i))) - } + Type::Object(i) => format!("rt::hold_opt({o}, {}, s)", self.t(&abi.object(*i))), base if Abi::is_buf(base) => { - let some = self.lift(inner, o, m, t); + let some = self.lift(inner, o, t); format!("(if rt::is_none({o}) {{ Ok(crate::rt::Buf::NONE) }} else {{ {some} }})") } Type::List(..) => { - let some = self.lift(inner, o, m, t); + let some = self.lift(inner, o, t); format!("(if rt::is_none({o}) {{ Ok(crate::rt::List::NONE) }} else {{ {some} }})") } - _ => format!("r_{}({o}, s, {m}, {t})", self.converter(ty)), + _ => format!("r_{}({o}, s, {t})", self.converter(ty)), }, - Type::Box(inner) => self.lift(inner, o, m, t), - Type::Ref(_, inner) => self.lift(inner, o, "false", t), + Type::Box(inner) => self.lift(inner, o, t), + Type::Ref(_, inner) => self.lift(inner, o, t), Type::Record(_) => format!("r_{}({o}, s, {t})", self.c_struct(ty)), Type::Enum(i) if abi.api.enums[*i].variants.iter().any(|v| !abi.api.everywhere(v.on)) => { // Checks the member a value is against the library's target. @@ -609,13 +602,7 @@ impl<'a> Gen<'a> { true => format!("rt::member_in({o}, {})", self.t(&abi.enumeration(*i))), false => format!("r_{}({o}, s, {t})", abi.enumeration(*i)), }, - Type::Object(i) => { - let m = match abi.api.objects[*i].copy { - true => "false", - false => m, - }; - format!("rt::hold({o}, {}, s, {m})", self.t(&abi.object(*i))) - } + Type::Object(i) => format!("rt::hold({o}, {}, s)", self.t(&abi.object(*i))), Type::Callback(i, _) => { let c = abi.callback(*i); format!( @@ -628,7 +615,7 @@ impl<'a> Gen<'a> { Type::Future => format!("rt::future_in({o}, s)"), Type::Range(int) => format!("rt::range_in::<{}>({o})", int.rust()), Type::Bound(_) | Type::Bounds(_) => format!("r_{}({o}, s)", abi.range(ty)), - Type::Tuple(_) => format!("r_{}({o}, s, {m}, {t})", self.converter(ty)), + Type::Tuple(_) => format!("r_{}({o}, s, {t})", self.converter(ty)), Type::Unit | Type::Closure(..) => unreachable!("{ty:?} is no value Python passes"), } } @@ -875,11 +862,11 @@ impl<'a> Gen<'a> { /// The glue of an option struct `c` holding `inner`, as `r_` and `w_`. fn option(&mut self, name: &str, c: &str, inner: &Type) { - let read = self.lift(inner, "o", "m", "take"); + let read = self.lift(inner, "o", "take"); let write = self.lower(inner, "v.value"); let _ = write!( self.glue, - "fn r_{name}(o: *mut PyObject, s: &mut rt::Scope, m: bool, take: bool) -> R {{\n\tmatch rt::is_none(o) {{\n\t\ttrue => Ok(crate::{c} {{ some: false, value: unsafe {{ std::mem::zeroed() }} }}),\n\t\tfalse => Ok(crate::{c} {{ some: true, value: {}? }}),\n\t}}\n}}\n\nfn w_{name}(v: crate::{c}) -> R {{\n\tmatch v.some {{\n\t\ttrue => {write},\n\t\tfalse => rt::none(),\n\t}}\n}}\n\n", + "fn r_{name}(o: *mut PyObject, s: &mut rt::Scope, take: bool) -> R {{\n\tmatch rt::is_none(o) {{\n\t\ttrue => Ok(crate::{c} {{ some: false, value: unsafe {{ std::mem::zeroed() }} }}),\n\t\tfalse => Ok(crate::{c} {{ some: true, value: {}? }}),\n\t}}\n}}\n\nfn w_{name}(v: crate::{c}) -> R {{\n\tmatch v.some {{\n\t\ttrue => {write},\n\t\tfalse => rt::none(),\n\t}}\n}}\n\n", read ); } @@ -891,14 +878,14 @@ impl<'a> Gen<'a> { let mut fields = Vec::new(); let mut writes = Vec::new(); for (k, ty) in elements.iter().enumerate() { - let lift = self.lift(ty, &format!("x{k}"), "m", "take"); + let lift = self.lift(ty, &format!("x{k}"), "take"); reads.push(format!("let x{k} = rt::tuple_item(o, {k}, {n})?;")); fields.push(format!("_{k}: {lift}?")); writes.push(self.lower(ty, &format!("v._{k}"))); } let _ = write!( self.glue, - "fn r_{name}(o: *mut PyObject, s: &mut rt::Scope, m: bool, take: bool) -> R {{\n\t{}\n\tOk(crate::{c} {{ {} }})\n}}\n\nfn w_{name}(v: crate::{c}) -> R {{\n\trt::tuple(vec![{}])\n}}\n\n", + "fn r_{name}(o: *mut PyObject, s: &mut rt::Scope, take: bool) -> R {{\n\t{}\n\tOk(crate::{c} {{ {} }})\n}}\n\nfn w_{name}(v: crate::{c}) -> R {{\n\trt::tuple(vec![{}])\n}}\n\n", reads.join("\n\t"), fields.join(", "), writes.join(", ") @@ -908,11 +895,11 @@ impl<'a> Gen<'a> { /// The glue of a `Vec`'s array `c`, as `r_` and `w_`. fn list(&mut self, name: &str, c: &str, elem: &Type) { // An array's items are behind its pointer, which Rust only reads. - let read = self.lift(elem, "o", "m", "false"); + let read = self.lift(elem, "o", "false"); let write = self.lower(elem, "e"); let _ = write!( self.glue, - "fn r_{name}(o: *mut PyObject, s: &mut rt::Scope, m: bool, take: bool) -> R {{\n\trt::list_in(o, s, |o, s| {read})\n}}\n\nfn w_{name}(v: crate::{c}) -> R {{\n\trt::list_out(v, |e| {write})\n}}\n\n" + "fn r_{name}(o: *mut PyObject, s: &mut rt::Scope, take: bool) -> R {{\n\trt::list_in(o, s, |o, s| {read})\n}}\n\nfn w_{name}(v: crate::{c}) -> R {{\n\trt::list_out(v, |e| {write})\n}}\n\n" ); } @@ -930,14 +917,23 @@ impl<'a> Gen<'a> { &Type::Ref(crate::ir::Mutability::Not, Box::new(ty.clone())), "a[0]", "false", - "false", ), - false => self.lift(ty, "a[0]", "false", "false"), + false => self.lift(ty, "a[0]", "false"), }; let pass = match object { true => "this", false => "&this", }; + // An object whose run-time checks may fail passes each its status, which raises. + let checked = self.abi.traits_status_of(ty); + let (status, arg) = match checked { + true => ("let mut status = 0;\n\t\t", ", &mut status"), + false => ("", ""), + }; + let check = |suffix: &str| match checked { + true => format!("\n\t\ts.status(status, \"{c}_{suffix}\")?;"), + false => String::new(), + }; let unary = |g: &mut Self, suffix: &str, member: &str, docs: &str, lower: String| { let d = Direct { member, @@ -951,7 +947,8 @@ impl<'a> Gen<'a> { &format!("{bare}_{suffix}"), d, &format!( - "let this = {this}?;\n\t\tlet ret = rt::call(|| crate::{c}_{suffix}({pass}));\n\t\ts.done();\n\t\t{lower}" + "let this = {this}?;\n\t\t{status}let ret = rt::call(|| crate::{c}_{suffix}({pass}{arg}));{}\n\t\ts.done();\n\t\t{lower}", + check(suffix) ), ); }; @@ -979,9 +976,8 @@ impl<'a> Gen<'a> { &Type::Ref(crate::ir::Mutability::Not, Box::new(ty.clone())), "a[1]", "false", - "false", ), - false => self.lift(ty, "a[1]", "false", "false"), + false => self.lift(ty, "a[1]", "false"), }; let (a, b) = match object { true => ("this", "other"), @@ -991,7 +987,8 @@ impl<'a> Gen<'a> { &format!("{bare}_eq"), 2, &format!( - "let this = {this}?;\n\t\tlet other = {other}?;\n\t\tlet ret = rt::call(|| crate::{c}_eq({a}, {b}));\n\t\ts.done();\n\t\trt::bool_out(ret)" + "let this = {this}?;\n\t\tlet other = {other}?;\n\t\t{status}let ret = rt::call(|| crate::{c}_eq({a}, {b}{arg}));{}\n\t\ts.done();\n\t\trt::bool_out(ret)", + check("eq") ), ); // Python makes a class that defines `__eq__` alone unhashable, as `Eq` without `Hash` is. @@ -1140,10 +1137,7 @@ impl<'a> Gen<'a> { _ => format!("rt::attr(o, {key}, s)?"), }; reads.push(format!("let x{n} = {read};")); - fields.push(format!( - "f{n}: {}?", - self.lift(&field.ty, &format!("x{n}"), "false", "take") - )); + fields.push(format!("f{n}: {}?", self.lift(&field.ty, &format!("x{n}"), "take"))); writes.push(format!("({key}, {})", self.lower(&field.ty, &format!("v.f{n}")))); } if record.fields.is_empty() { @@ -1276,10 +1270,7 @@ impl<'a> Gen<'a> { for (k, f) in variant.fields.iter().enumerate() { let key = self.key(&field_name(&f.name)); binds.push(format!("let x{k} = rt::attr(o, {key}, s)?;")); - fields.push(format!( - "f{k}: {}?", - self.lift(&f.ty, &format!("x{k}"), "false", "take") - )); + fields.push(format!("f{k}: {}?", self.lift(&f.ty, &format!("x{k}"), "take"))); lowers.push(format!("({key}, {})", self.lower(&f.ty, &format!("p.f{k}")))); } reads.push(format!( @@ -1478,7 +1469,7 @@ impl<'a> Gen<'a> { fn given(&mut self, ty: &Type, o: &str) -> String { match ty { Type::String => format!("rt::str_given({o})"), - _ => self.lift(ty, o, "false", "false"), + _ => self.lift(ty, o, "false"), } } @@ -1674,18 +1665,12 @@ impl<'a> Gen<'a> { Receiver::Ref => Type::Ref(crate::ir::Mutability::Not, Box::new(ty)), Receiver::Mut => Type::Ref(crate::ir::Mutability::Mut, Box::new(ty)), }; - stmts.push(format!( - "let p{n} = {}?;", - self.lift(&ty, &format!("a[{n}]"), "true", "true") - )); + stmts.push(format!("let p{n} = {}?;", self.lift(&ty, &format!("a[{n}]"), "true"))); c_args.push(format!("p{n}")); } _ => { // Read through a pointer, so Rust takes nothing of it. - stmts.push(format!( - "let p{n} = {}?;", - self.lift(&ty, &format!("a[{n}]"), "false", "false") - )); + stmts.push(format!("let p{n} = {}?;", self.lift(&ty, &format!("a[{n}]"), "false"))); c_args.push(format!("&p{n}")); } } @@ -1708,7 +1693,7 @@ impl<'a> Gen<'a> { (Type::Ref(_, inner), _) if param.ty.written() => { stmts.push(format!( "let h{n} = rt::mut_value(a[{n}], s)?;\n\t\tlet p{n} = {}?;", - self.lift(inner, &format!("h{n}"), "true", "true") + self.lift(inner, &format!("h{n}"), "true") )); c_args.push(format!("p{n}")); written.push((n, (**inner).clone())); @@ -1736,7 +1721,7 @@ impl<'a> Gen<'a> { _ => { stmts.push(format!( "let p{n} = {}?;", - self.lift(¶m.ty, &format!("a[{n}]"), "true", "true") + self.lift(¶m.ty, &format!("a[{n}]"), "true") )); c_args.push(format!("p{n}")); } @@ -1754,7 +1739,16 @@ impl<'a> Gen<'a> { exception.as_deref(), ); } - let outs: Vec<(String, Ty)> = c_params.cloned().collect(); + let outs: Vec<(String, Ty)> = c_params.filter(|(_, ty)| !abi::is_status(ty)).cloned().collect(); + // A call that fails a run-time check writes its status, which raises once it returns. + let (status, check) = match export.call.status() { + Some(_) => ( + "let mut status = 0;\n\t\t".to_string(), + format!("\n\t\ts.status(status, \"{c_name}\")?;"), + ), + None => (String::new(), String::new()), + }; + let status_arg = export.call.status().map(|_| "&mut status".to_string()); // The outs follow the unstripped return: a borrowed tuple is one struct, not its elements. let ok = strip_refs(&function.ok); @@ -1778,8 +1772,8 @@ impl<'a> Gen<'a> { free.name, ); stmts.push(format!( - "let f = rt::call(|| crate::{c_name}({}));\n\t\ts.done();\n\t\trt::start(f.cast(), poll_{c_name}, free_{c_name}, s, {})", - c_args.join(", "), + "{status}let f = rt::call(|| crate::{c_name}({}));{check}\n\t\ts.done();\n\t\trt::start(f.cast(), poll_{c_name}, free_{c_name}, s, {})", + c_args.iter().chain(&status_arg).cloned().collect::>().join(", "), function.eager )); } else { @@ -1790,13 +1784,13 @@ impl<'a> Gen<'a> { } false => self.outs(&outs, &written, &function.ok, err.as_ref(), exception.as_deref(), "ret"), }; - let args: Vec = c_args.iter().cloned().chain(ptrs).collect(); + let args: Vec = c_args.iter().cloned().chain(ptrs).chain(status_arg).collect(); let call = match &handed { Some(h) => h.call(&args), None => format!("crate::{c_name}({})", args.join(", ")), }; stmts.push(format!( - "{decl}\n\t\tlet ret = rt::call(|| {call});\n\t\ts.done();\n\t\t{value}" + "{decl}\n\t\t{status}let ret = rt::call(|| {call});{check}\n\t\ts.done();\n\t\t{value}" )); } // The Python. diff --git a/ffi/src/swift.rs b/ffi/src/swift.rs index 3b189473d..e78153816 100644 --- a/ffi/src/swift.rs +++ b/ffi/src/swift.rs @@ -91,8 +91,12 @@ pub fn generate(abi: &Abi<'_>, packages: &str, contract: u64) -> Result Swift.Bool {{\n\t\t{p}_contract() == 0x{contract:016x} && {upper}_CONTRACT == 0x{contract:016x}\n\t}}\n\n\ \t/// The index of the library's target among those this file serves.\n\ \tstatic let target = Swift.UInt64({p}_target())\n\n\ - \t/// Traps unless the library's target is among those of `on`, as the item `what` is.\n\ - \tstatic func on(_ on: Swift.UInt64, _ what: Swift.String) {{\n\t\tif (on >> target) & 1 == 0 {{\n\t\t\tfatalError(\"\\(what): it is not on this library's target\")\n\t\t}}\n\t}}\n\n\ + \t/// Throws unless the library's target is among those of `on`, as the item `what` is.\n\ + \tstatic func on(_ on: Swift.UInt64, _ what: Swift.String) throws {{\n\t\tif (on >> target) & 1 == 0 {{\n\t\t\tthrow CheckError(function: what, message: \"it is not on this library's target\")\n\t\t}}\n\t}}\n\n\ + \t/// Traps unless the library's target is among those of `on`, where nothing can throw.\n\ + \tstatic func onOrTrap(_ on: Swift.UInt64, _ what: Swift.String) {{\n\t\tif (on >> target) & 1 == 0 {{\n\t\t\tfatalError(\"\\(what): it is not on this library's target\")\n\t\t}}\n\t}}\n\n\ + \t/// Throws what `status`, written by a call of `what`, says it failed, if anything.\n\ + \t@inline(__always)\n\tstatic func status(_ status: Swift.Int32, _ what: Swift.StaticString) throws {{\n\t\tif status != 0 {{\n\t\t\tthrow CheckError(function: \"\\(what)\", message: Swift.String(cString: {p}_status_message(status)))\n\t\t}}\n\t}}\n\n\ \tstatic func free(_ buf: Buf) {{\n\t\t{p}_buf_free(buf)\n\t}}\n\n\ \t/// A buffer Rust owns, holding a copy of what `view` lends.\n\ \tstatic func copy(_ view: Buf) -> Buf {{\n\t\t{p}_buf_copy(view)\n\t}}\n\n\ @@ -146,6 +150,7 @@ const TAKEN: &[&str] = &[ "Bool", "Bytes", "CancellationError", + "CheckError", "Comparable", "CustomDebugStringConvertible", "CustomStringConvertible", @@ -200,6 +205,13 @@ struct Arg { open: Option<(String, String)>, exprs: Vec, after: Vec, + /// Whether an expression gives Rust a retained box, which only the call takes. + gives: bool, + /// Whether its lowering, its one expression, throws. + throws: bool, + /// Whether it opens a buffer Rust takes, a second owner of Rust's own, which only the + /// call frees: so it opens inside every open that throws. + takes: bool, } struct Gen<'a> { @@ -226,13 +238,18 @@ struct Gen<'a> { } impl<'a> Gen<'a> { - /// The statement that traps unless the library's target is among those of `on`, as the - /// item `what` is, then `then`; or `then` alone if it is on every target. - fn on(&self, on: On, what: &str, then: &str) -> String { + /// The statement that throws, if the body around it can, or else traps, unless the + /// library's target is among those of `on`, as the item `what` is, then `then`; or `then` + /// alone if it is on every target. + fn on(&self, on: On, what: &str, then: &str, throws: bool) -> String { let api = self.abi.api; + let check = match throws { + true => "try Native.on", + false => "Native.onOrTrap", + }; match api.everywhere(on) { true => then.to_string(), - false => format!("Native.on({}, \"{what}\"){then}", on.0 & api.all().0), + false => format!("{check}({}, \"{what}\"){then}", on.0 & api.all().0), } } @@ -413,6 +430,27 @@ impl<'a> Gen<'a> { } } + /// Whether lowering a `ty` nested in another value can throw: it holds a class whose + /// value a move takes, closing its handle. + fn throwing(&self, ty: &Type) -> bool { + let mut handles = Vec::new(); + self.abi.api.handles(ty, &mut handles, &mut Vec::new()); + handles.into_iter().any(|i| self.held(i)) + } + + /// Whether object `i` is a class a move may close, whose hold for a call can fail. + fn held(&self, i: usize) -> bool { + self.abi.traits_status(i) && !self.unique(&Type::Object(i)) + } + + /// `lowered`, a lowering in a function that cannot throw, trapping instead. + fn trapping(lowered: String) -> String { + match lowered.contains("try ") { + true => format!("try! {lowered}"), + false => lowered, + } + } + /// The arena argument a value's `_lower` takes, if it needs one. fn arena_arg(&self, ty: &Type) -> &'static str { match self.arena(ty) { @@ -452,8 +490,13 @@ impl<'a> Gen<'a> { true => "unique", false => "list", }; + // A hold that throws makes the list's lowering throw. + let t = match item.contains("try ") { + true => "try ", + false => "", + }; format!( - "Native.vec(a_.{list}({e}) {{ {x} in {item} }}, {}.init(ptr:len:owner:))", + "Native.vec({t}a_.{list}({e}) {{ {x} in {item} }}, {}.init(ptr:len:owner:))", abi.list(inner) ) } @@ -471,26 +514,40 @@ impl<'a> Gen<'a> { ) } }; - format!("Native.option({e}, {{ {v} in {some} }}, {none})") + let t = match some.contains("try ") { + true => "try ", + false => "", + }; + format!("{t}Native.option({e}, {{ {v} in {some} }}, {none})") } Type::Option(inner) => match inner.base() { + Type::Object(i) if self.held(*i) => format!("try a_.held({e}?.handle)"), Type::Object(_) => format!("a_.hold({e}?.handle)"), _ if Abi::is_buf(inner) => format!("a_.buf({e})"), Type::List(_, elem) => { let v = self.fresh("v"); let some = self.lower(inner, &v); - format!("({e}.map {{ {v} in {some} }} ?? {}())", abi.list(elem)) + let t = if some.contains("try ") { "try " } else { "" }; + format!("({t}{e}.map {{ {v} in {some} }} ?? {}())", abi.list(elem)) } _ => { let c = abi.option(inner); let v = self.fresh("v"); let some = self.lower(inner, &v); - format!("({e}.map {{ {v} in {c}(some: true, value: {some}) }} ?? {c}())") + let t = if some.contains("try ") { "try " } else { "" }; + format!("({t}{e}.map {{ {v} in {c}(some: true, value: {some}) }} ?? {c}())") } }, Type::Box(inner) | Type::Ref(_, inner) => self.lower(inner, e), - Type::Record(_) | Type::Enum(_) => format!("{e}._lower({})", self.arena_arg(ty)), + Type::Record(_) | Type::Enum(_) => { + let t = match self.throwing(ty) { + true => "try ", + false => "", + }; + format!("{t}{e}._lower({})", self.arena_arg(ty)) + } Type::Object(_) if self.unique(ty) => format!("{e}.handle"), + Type::Object(i) if self.held(*i) => format!("try a_.held({e}.handle)"), Type::Object(_) => format!("a_.hold({e}.handle)"), Type::Callback(i, _) => format!("Native.lower_{}({e})", abi.callback(*i)), Type::Tuple(elements) => { @@ -768,79 +825,128 @@ impl<'a> Gen<'a> { let name = self.sw(ty); let object = matches!(ty, Type::Object(_)); let unique = self.unique(ty); - // The receiver: a struct's handle, a class's held, or the value lowered. - let (open, this, close) = match object { - true if unique => (String::new(), "self.handle", ""), - true => ("self.handle.with { h_ in ".to_string(), "h_", " }"), - false => { - let arena = match self.arena(ty) { - true => "let a_ = Native.Arena()\n\t\tdefer { a_.finish() }\n\t\t", - false => "", - }; - ( + // One whose checks can fail throws, so conforms to no protocol of these, whose + // requirements cannot throw; any other's checks never fail. + let status = self.abi.traits_status_of(ty); + // The statements lowering a value receiver for the function `what`, which on a target + // the type is not on throws where it can. + let value = |what: &str| { + let arena = match self.arena(ty) { + true => format!("let a_ = Native.Arena(\"{what}\")\n\t\tdefer {{ a_.finish() }}\n\t\t"), + false => String::new(), + }; + format!( + "{}Native.check()\n\t\t{arena}", + self.on(self.on, what, "\n\t\t", status) + ) + }; + let lower = match self.throwing(ty) { + true => format!("try self._lower({})", self.arena_arg(ty)), + false => format!("self._lower({})", self.arena_arg(ty)), + }; + // The statements calling `call`, the C call with its arguments, unclosed, of the + // function `what`, returning `lift` of its result. + let call = |call: &str, what: &str, lift: &dyn Fn(&str) -> String| match status { + true => format!( + "var s_: Swift.Int32 = 0\n\t\tlet r_ = {call}, &s_)\n\t\ttry Native.status(s_, \"{what}\")\n\t\treturn {}", + lift("r_") + ), + false => format!("return {}", lift(&format!("{call})"))), + }; + // The body calling `{c}_` of the receiver, returning `lift` of its result. + let unary = |suffix: &str, lift: &dyn Fn(&str) -> String| { + let what = format!("{c}_{suffix}"); + match (object, unique) { + (true, true) => call(&format!("{what}(self.handle"), &what, lift), + (true, false) => { + let with = match status { + true => format!("try self.handle.with(\"{what}\")"), + false => "self.handle.with".to_string(), + }; format!( - "{}Native.check()\n\t\t{arena}var c_ = self._lower({})\n\t\treturn ", - self.on(self.on, c, "\n\t\t"), - self.arena_arg(ty) - ), - "&c_", - "", - ) + "return {with} {{ h_ in\n\t\t\t{}\n\t\t}}", + call(&format!("{what}(h_"), &what, lift).replace("\n\t\t", "\n\t\t\t") + ) + } + (false, _) => format!( + "{}var c_ = {lower}\n\t\t{}", + value(&what), + call(&format!("{what}(&c_"), &what, lift) + ), } }; - let text = |open: &str, call: &str| match object { - true => format!("Native.string({open}{call}{close})"), - false => format!("{open}Native.string({call})"), + let throws = match status { + true => " throws", + false => "", + }; + // A property's body, in a getter that throws if the call can. + let getter = |body: String| match status { + true => format!("get throws {{\n\t\t\t{}\n\t\t}}", body.replace("\n\t\t", "\n\t\t\t")), + false => body, }; + let text = |r: &str| format!("Native.string({r})"); if t.display { - protocols.push("CustomStringConvertible"); + if !status { + protocols.push("CustomStringConvertible"); + } members.push("description".to_string()); body.push(format!( "\tpublic var description: Swift.String {{\n\t\t{}\n\t}}\n", - text(&open, &format!("{c}_to_string({this})")) + getter(unary("to_string", &text)) )); } if t.debug { - protocols.push("CustomDebugStringConvertible"); + if !status { + protocols.push("CustomDebugStringConvertible"); + } members.push("debugDescription".to_string()); body.push(format!( "\t/// Rust's `Debug` form.\n\tpublic var debugDescription: Swift.String {{\n\t\t{}\n\t}}\n", - text(&open, &format!("{c}_debug({this})")) + getter(unary("debug", &text)) )); } if object && t.clone { members.push("clone".to_string()); - let raw = match unique { - true => format!("{c}_clone(self.handle)"), - false => format!("self.handle.with {{ {c}_clone($0) }}"), - }; + let lift = |r: &str| format!("{name}(raw: {r})"); body.push(format!( - "\t/// A new handle to a clone.\n\tpublic func clone() -> {name} {{\n\t\t{name}(raw: {raw})\n\t}}\n" + "\t/// A new handle to a clone.\n\tpublic func clone(){throws} -> {name} {{\n\t\t{}\n\t}}\n", + unary("clone", &lift) )); } if t.eq { - protocols.push("Equatable"); - let compare = match object { - true if unique => format!("{c}_eq(a.handle, b.handle)"), - true => format!("a.handle.with {{ a_ in b.handle.with {{ b_ in {c}_eq(a_, b_) }} }}"), - false => { - let arena = match self.arena(ty) { - true => "let a_ = Native.Arena()\n\t\tdefer { a_.finish() }\n\t\t", - false => "", + if !status { + protocols.push("Equatable"); + } + let what = format!("{c}_eq"); + let same = |r: &str| r.to_string(); + let compare = match (object, unique) { + (true, true) => call(&format!("{what}(a.handle, b.handle"), &what, &same), + (true, false) => { + let with = |h: &str| match status { + true => format!("try {h}.handle.with(\"{what}\")"), + false => format!("{h}.handle.with"), }; - let arg = self.arena_arg(ty); format!( - "{}Native.check()\n\t\t{arena}var ca_ = a._lower({arg})\n\t\tvar cb_ = b._lower({arg})\n\t\treturn {c}_eq(&ca_, &cb_)", - self.on(self.on, c, "\n\t\t") + "return {} {{ a_ in {} {{ b_ in\n\t\t\t{}\n\t\t}} }}", + with("a"), + with("b"), + call(&format!("{what}(a_, b_"), &what, &same).replace("\n\t\t", "\n\t\t\t") ) } + (false, _) => format!( + "{}var ca_ = {}\n\t\tvar cb_ = {}\n\t\t{}", + value(&what), + lower.replacen("self.", "a.", 1), + lower.replacen("self.", "b.", 1), + call(&format!("{what}(&ca_, &cb_"), &what, &same) + ), }; let borrowing = match unique { true => "borrowing ", false => "", }; body.push(format!( - "\tpublic static func == (a: {borrowing}{name}, b: {borrowing}{name}) -> Swift.Bool {{\n\t\t{compare}\n\t}}\n" + "\tpublic static func == (a: {borrowing}{name}, b: {borrowing}{name}){throws} -> Swift.Bool {{\n\t\t{compare}\n\t}}\n" )); } if t.default { @@ -849,9 +955,15 @@ impl<'a> Gen<'a> { Type::Object(_) => format!("{name}(raw: {c}_default())"), _ => format!("{name}._lift({c}_default())"), }; + // A constructor the target lacks throws, as a function's call does. + let limited = !self.abi.api.everywhere(self.on); + let throws = match limited { + true => " throws", + false => "", + }; body.push(format!( - "\t/// Rust's `Default`.\n\tpublic static func `default`() -> {name} {{\n\t\t{}Native.check()\n\t\treturn {lift}\n\t}}\n", - self.on(self.on, c, "\n\t\t") + "\t/// Rust's `Default`.\n\tpublic static func `default`(){throws} -> {name} {{\n\t\t{}Native.check()\n\t\treturn {lift}\n\t}}\n", + self.on(self.on, &format!("{c}_default"), "\n\t\t", limited) )); } } @@ -929,8 +1041,12 @@ impl<'a> Gen<'a> { "\tstatic func _lift(_ c: {c}) -> {name} {{\n\t\t{name}({})\n\t}}\n", lifted.join(", ") )); + let throws = match self.throwing(&ty) { + true => " throws", + false => "", + }; body.push(format!( - "\tfunc _lower({arena}) -> {c} {{\n\t\t{c}({})\n\t}}\n", + "\tfunc _lower({arena}){throws} -> {c} {{\n\t\t{c}({})\n\t}}\n", lowered.join(", ") )); let text = format!("{rust}\n\n{}", record.docs); @@ -1008,7 +1124,7 @@ impl<'a> Gen<'a> { false => cases.push(format!("\tcase {case}({})\n", fields.join(", "))), } // A value of a variant the library's target lacks is refused as it crosses. - let refused = self.on(variant.on, &format!("{c}.{}", variant.name), "\n\t\t\t"); + let refused = self.on(variant.on, &format!("{c}.{}", variant.name), "\n\t\t\t", false); if e.plain() { lifts.push(format!("\t\tcase {tag}: return .{case}")); lowers.push(format!("\t\tcase .{case}:\n\t\t\t{refused}return {tag}")); @@ -1067,7 +1183,11 @@ impl<'a> Gen<'a> { lowers.join("\n") ), false => format!( - "\tfunc _lower({arena}) -> {c} {{\n\t\tvar c = {c}()\n\t\tswitch self {{\n{}\n\t\t}}\n\t\treturn c\n\t}}\n", + "\tfunc _lower({arena}){} -> {c} {{\n\t\tvar c = {c}()\n\t\tswitch self {{\n{}\n\t\t}}\n\t\treturn c\n\t}}\n", + match self.throwing(&ty) { + true => " throws", + false => "", + }, lowers.join("\n") ), }); @@ -1166,7 +1286,7 @@ impl<'a> Gen<'a> { let call = format!("Box.unbox(data).{member}({})", args.join(", ")); let call = match method.ok { Type::Unit => call, - _ => self.lower(&method.ok, &call), + _ => Self::trapping(self.lower(&method.ok, &call)), }; let mut closure_params = vec!["data".to_string()]; closure_params.extend(ps); @@ -1237,7 +1357,7 @@ impl<'a> Gen<'a> { /// How the call passes `param`, the C parameter `c`, whose Swift value is `e`, the /// `k`th, which `function` takes. - fn arg(&mut self, ty: &Type, c: &Ty, e: &str, k: usize, tr: &str) -> (Arg, bool) { + fn arg(&mut self, ty: &Type, c: &Ty, e: &str, k: usize, tr: &str, what: &str) -> (Arg, bool) { let abi = self.abi; let mut arg = Arg::default(); let mut arena = false; @@ -1250,6 +1370,7 @@ impl<'a> Gen<'a> { let sw = self.sw(ty); arg.exprs.push(self.trampoline(ty, closure_)); arg.exprs.push(format!("Native.Box<{sw}>.give({e})")); + arg.gives = true; arg.exprs .push(format!("{{ data_ in Native.Box<{sw}>.release(data_) }}")); } @@ -1269,13 +1390,23 @@ impl<'a> Gen<'a> { arg.open = Some((format!("{tr}Native.reader(&{e}) {{ {r} in"), "}".to_string())); arg.exprs.push(r); } - (Type::Callback(i, _), _) => arg.exprs.push(format!("Native.lower_{}({e})", abi.callback(*i))), - (Type::Waker, _) => arg.exprs.push(format!("Native.waker({e})")), - (Type::Future, _) => arg.exprs.push(format!("Native.future({e})")), + (Type::Callback(i, _), _) => { + arg.exprs.push(format!("Native.lower_{}({e})", abi.callback(*i))); + arg.gives = true; + } + (Type::Waker, _) => { + arg.exprs.push(format!("Native.waker({e})")); + arg.gives = true; + } + (Type::Future, _) => { + arg.exprs.push(format!("Native.future({e})")); + arg.gives = true; + } (ty, Ty::Buf) if Abi::is_buf(ty) || matches!(ty.base(), Type::Option(inner) if Abi::is_buf(inner)) => { let b = format!("b{k}_"); arg.open = Some((format!("{tr}Native.take({e}) {{ {b} in"), "}".to_string())); arg.exprs.push(b); + arg.takes = true; } // A struct the call borrows or consumes, so it lives until the call returns. (ty, Ty::Handle { .. }) if self.unique(ty) => arg.exprs.push(match ty.base() { @@ -1285,15 +1416,17 @@ impl<'a> Gen<'a> { (ty, Ty::Handle { .. }) => { let h = format!("h{k}_"); let open = match ty.base() { - Type::Option(_) => format!("{tr}Native.with({e}?.handle) {{ {h} in"), - _ => format!("{tr}{e}.handle.with {{ {h} in"), + Type::Option(_) => format!("{tr}Native.with({what}{e}?.handle, {{ {h} in"), + _ => format!("{tr}{e}.handle.with({what}{{ {h} in"), }; - arg.open = Some((open, "}".to_string())); + arg.open = Some((open, "})".to_string())); arg.exprs.push(h); } (ty, _) => { arena = self.arena(ty); - arg.exprs.push(self.lower(ty, e)); + let lowered = self.lower(ty, e); + arg.throws = lowered.contains("try "); + arg.exprs.push(lowered); } } (arg, arena) @@ -1394,7 +1527,7 @@ impl<'a> Gen<'a> { Type::Option(inner) if matches!(**inner, Type::Str | Type::String) => { format!("({e}.map {{ Native.owned($0) }} ?? Native.none)") } - ty => self.lower(ty, e), + ty => Self::trapping(self.lower(ty, e)), } } @@ -1476,19 +1609,29 @@ impl<'a> Gen<'a> { Some(Receiver::Mut) if unique || value => "mutating ", _ => "", }; - // Only a future or a `Result` throws, and a future only once it is awaited. - let throws = function.future || function.err.is_some(); + // A call that can fail a check throws, as do a future and a `Result`. + let status = export.call.status().is_some(); + let everywhere = abi.api.everywhere(function.on); + let throws = function.future || function.err.is_some() || status || !everywhere; // A future's error throws from the call that starts it, as from its polls. - let tr = match function.err.is_some() { + let tr = match function.err.is_some() || status { true => "try ", false => "", }; + // A class's handle closed throws where the call can. + let what = match status { + true => format!("\"{c_name}\", "), + false => String::new(), + }; let mut prelude = Vec::new(); if !object { prelude.push("Native.check()".to_string()); } - if !abi.api.everywhere(function.on) { - prelude.push(self.on(function.on, &c_name, "")); + if !everywhere { + prelude.push(format!( + "try Native.on({}, \"{c_name}\")", + function.on.0 & abi.api.all().0 + )); } let mut arena = false; let mut args: Vec = Vec::new(); @@ -1500,7 +1643,7 @@ impl<'a> Gen<'a> { match c { Ty::Handle { .. } if unique => arg.exprs.push("self.handle".to_string()), Ty::Handle { .. } => { - arg.open = Some((format!("{tr}self.handle.with {{ h_ in"), "}".to_string())); + arg.open = Some((format!("{tr}self.handle.with({what}{{ h_ in"), "})".to_string())); arg.exprs.push("h_".to_string()); let copy = matches!(ty, Type::Object(i) if abi.api.objects[i].copy); if receiver == Receiver::Owned && !copy { @@ -1509,7 +1652,11 @@ impl<'a> Gen<'a> { } _ => { arena |= self.arena(&ty); - prelude.push(format!("var c_self_ = self._lower({})", self.arena_arg(&ty))); + let t = match self.throwing(&ty) { + true => "try ", + false => "", + }; + prelude.push(format!("var c_self_ = {t}self._lower({})", self.arena_arg(&ty))); if value { prelude.push(format!("defer {{ self = {} }}", self.lift(&ty, "c_self_"))); } @@ -1558,13 +1705,25 @@ impl<'a> Gen<'a> { (false, _) => self.ownership(¶m.ty), }; params.push(format!("_ {name}: {ownership}{}", self.sw(¶m.ty))); - let (arg, needs) = self.arg(¶m.ty, c, name, k, tr); + let (arg, needs) = self.arg(¶m.ty, c, name, k, tr, &what); arena |= needs; args.push(arg); } - let outs: Vec<(String, Ty)> = c_params.cloned().collect(); + let outs: Vec<(String, Ty)> = c_params.filter(|(_, ty)| !abi::is_status(ty)).cloned().collect(); + // What runs once the call returns, gated on its checks passing when it can fail one: + // a failed check, or an argument whose lowering throws, leaves every argument as it + // was, as Rust ran nothing. + let ran = + status && (function.params.iter().any(|p| p.ty.written()) || args.iter().any(|a| !a.after.is_empty())); + if ran { + prelude.push("var c_ran_ = false".to_string()); + } + let gate = |stmts: &str| match ran { + true => format!("defer {{ if c_ran_ {{ {stmts} }} }}"), + false => format!("defer {{ {stmts} }}"), + }; // Each value passed for `&mut` takes what Rust wrote back, once the call's own - // access to it ends, on every path, a throw included. + // access to it ends, on every path the call ran, a throw included. let mut written = Vec::new(); for (param, name) in function.params.iter().zip(&names).filter(|(p, _)| p.ty.written()) { let out = Abi::written(function, &abi::escape(¶m.name)); @@ -1574,20 +1733,28 @@ impl<'a> Gen<'a> { .expect("a written parameter has its out"); prelude.push(self.out_decl(&out, ty)); let lifted = self.lift(param.ty.base(), &format!("c_{out}_")); - prelude.push(format!("defer {{ {name} = {lifted} }}")); + prelude.push(gate(&format!("{name} = {lifted}"))); written.push(out); } if arena { prelude.insert( 1.min(prelude.len()), - "let a_ = Native.Arena()\ndefer { a_.finish() }".to_string(), + format!("let a_ = Native.Arena(\"{c_name}\")\ndefer {{ a_.finish() }}"), ); // Past the receiver, which Rust only reads, Rust takes each value passed. prelude.push("a_.take = true".to_string()); } let after: Vec = args.iter().flat_map(|a| a.after.clone()).collect(); if !after.is_empty() { - prelude.push(format!("defer {{ {} }}", after.join("; "))); + prelude.push(gate(&after.join("; "))); + } + // A lowering that throws runs before any box or buffer is given, which the call + // alone would take: a throw between them would leak it. + if args.iter().any(|a| a.gives || a.takes) { + for (k, arg) in args.iter_mut().enumerate().filter(|(_, a)| a.throws) { + prelude.push(format!("let c_arg{k}_ = {}", arg.exprs[0])); + arg.exprs[0] = format!("c_arg{k}_"); + } } let exprs: Vec = args.iter().flat_map(|a| a.exprs.clone()).collect(); @@ -1600,12 +1767,42 @@ impl<'a> Gen<'a> { let thrown = err.as_ref().map(|e| self.thrown(e)); let mut inner = Vec::new(); + // A failed check writes its status, which throws once the call returns. + let (status_arg, check) = match status { + true => { + inner.push("var c_status_: Swift.Int32 = 0".to_string()); + ( + vec!["&c_status_".to_string()], + Some(match ran { + true => format!("try Native.status(c_status_, \"{c_name}\")\nc_ran_ = true"), + false => format!("try Native.status(c_status_, \"{c_name}\")"), + }), + ) + } + false => (Vec::new(), None), + }; let tail; - // What a cancelled task runs instead of the start: the call alone, whose future - // it drops unpolled, as Rust drops one it never awaits. + // The call that makes the future unpolled, as `f_`, its status checked: what an eager + // call starts with, and, freed, what a cancelled task runs instead of the start, as + // Rust drops a future it never awaits. + // The arena frees what it gave Rust unless the call reached C, which then owns it. + let reached = |call: String| match arena { + true => format!("a_.ran({call})"), + false => call, + }; + let call = reached(format!( + "{c_name}({})", + exprs.iter().chain(&status_arg).cloned().collect::>().join(", ") + )); + let made = check + .as_ref() + .map(|check| format!("var c_status_: Swift.Int32 = 0\nlet f_ = {call}\n{check}")); let mut cancelled = None; if let Some(abi::Future { start, poll, free, .. }) = &export.future { - cancelled = Some(format!("{}({c_name}({}))", free.name, exprs.join(", "))); + cancelled = Some(match &made { + Some(made) => format!("{made}\n{}(f_)", free.name), + None => format!("{}({call})", free.name), + }); let poll_outs: Vec<(String, Ty)> = poll.params[2..].to_vec(); let decls: Vec = poll_outs.iter().map(|(n, ty)| self.out_decl(n, ty)).collect(); let ptrs: Vec = poll_outs.iter().map(|(n, _)| format!("&c_{n}_")).collect(); @@ -1629,12 +1826,17 @@ impl<'a> Gen<'a> { let start_args: Vec = (exprs.iter().cloned()) .chain(ptrs.iter().cloned()) .chain(["&f_".to_string()]) + .chain(status_arg.iter().cloned()) .collect(); - inner.push(format!( - "switch {}({}) {{\n{cases}\n}}", - start.name, - start_args.join(", ") - )); + let started = reached(format!("{}({})", start.name, start_args.join(", "))); + match &check { + Some(check) => { + inner.push(format!("let c_poll_ = {started}")); + inner.push(check.clone()); + inner.push(format!("switch c_poll_ {{\n{cases}\n}}")); + } + None => inner.push(format!("switch {started} {{\n{cases}\n}}")), + } let mut body = decls; let call_args: Vec = ["f_".to_string(), "w_".to_string()].into_iter().chain(ptrs).collect(); body.push(format!( @@ -1654,37 +1856,52 @@ impl<'a> Gen<'a> { } let mut call_args = exprs.clone(); call_args.extend(outs.iter().map(|(n, _)| format!("&c_{n}_"))); - let call = format!("{c_name}({})", call_args.join(", ")); + call_args.extend(status_arg); + let call = reached(format!("{c_name}({})", call_args.join(", "))); match (&err, &ok) { (Some(err), ok) => { let ready = match ok { Type::Unit => "return".to_string(), _ => format!("return {}", self.lift_outs(&function.ok, ok)), }; - inner.push(format!("if {call} {{\n\t{ready}\n}}")); + match &check { + Some(check) => { + inner.push(format!("let c_ok_ = {call}")); + inner.push(check.clone()); + inner.push(format!("if c_ok_ {{\n\t{ready}\n}}")); + } + None => inner.push(format!("if {call} {{\n\t{ready}\n}}")), + } let lifted = self.lift(err, "c_err_"); inner.push(format!( "throw {}", thrown_value(thrown.as_ref().expect("an error throws"), &lifted) )); } - (None, Type::Unit) => inner.push(call), + (None, Type::Unit) => { + inner.push(call); + inner.extend(check); + } // A value C returns beside what Rust wrote back is its return, as with none. (None, ok) if outs.iter().any(|(n, _)| !written.contains(n)) => { inner.push(call); + inner.extend(check); inner.push(format!("return {}", self.lift_outs(&function.ok, ok))); } (None, ok) => { inner.push(format!("let c_ret_ = {call}")); + inner.extend(check); inner.push(format!("return {}", self.lift(ok, "c_ret_"))); } } } // Wrap the call in each argument's closure, innermost last, each opened with `try` - // only where the call throws. + // only where the call throws. A buffer Rust takes opens innermost, so nothing throws + // between its take and the call. let wrap = |inner: String, throws: bool| { let mut code = inner; - for arg in args.iter().rev() { + let (takes, rest): (Vec<&Arg>, Vec<&Arg>) = args.iter().partition(|a| a.takes); + for arg in takes.into_iter().rev().chain(rest.into_iter().rev()) { if let Some((open, close)) = &arg.open { // An opener may hold several `try`s, as a lent closure's two do. let open = match throws { @@ -1709,12 +1926,24 @@ impl<'a> Gen<'a> { // and a callback may cancel the task: so it is made, then `Native.made` checks the // task before its first poll, never started. Some(tail) if function.eager => { - let made = wrap(format!("{c_name}({})", exprs.join(", ")), false); - body.push(format!("let f_ = {}", made.strip_prefix("return ").unwrap_or(&made))); + match &made { + // Made by a call of several lines, which binds `f_` itself unwrapped. + Some(made) => { + let wrapped = wrap(format!("{made}\nreturn f_"), true); + match wrapped.strip_prefix("return ") { + Some(wrapped) => body.push(format!("let f_ = {wrapped}")), + None => body.push(made.clone()), + } + } + None => { + let made = wrap(call.clone(), false); + body.push(format!("let f_ = {}", made.strip_prefix("return ").unwrap_or(&made))); + } + } body.push(tail.replacen("Native.run(", "Native.made(", 1)); } Some(tail) => { - let made = wrap(cancelled.expect("a future has its cancel"), false); + let made = wrap(cancelled.expect("a future has its cancel"), status); let made = made.strip_prefix("return ").unwrap_or(&made); body.push(format!( "// A task cancelled already takes nothing, even what is ready.\nif Task.isCancelled {{\n{}\n\tthrow CancellationError()\n}}", @@ -1725,7 +1954,7 @@ impl<'a> Gen<'a> { Type::Unit => "Void".to_string(), _ => self.sw(&ok), }; - let tr = match err.is_some() { + let tr = match err.is_some() || status { true => "try ", false => "", }; @@ -1787,8 +2016,13 @@ impl<'a> Gen<'a> { }; let body = body.join("\n"); let var = function.params.is_empty() && !throws; + // A field read stays a property though a check of its handle may fail, which throws + // from its getter, unless Swift cannot copy its value, whose getter cannot throw. + let getter = throws && !function.future && function.err.is_none() && everywhere && !self.unique(&function.ok); let var = match (function.access, receiver) { - (Access::Field { set: false, .. }, Some(_)) => var && mutation.is_empty(), + (Access::Field { set: false, .. }, Some(_)) => { + function.params.is_empty() && (!throws || getter) && mutation.is_empty() + } (Access::Const, None) => { if var { member = member.trim_matches('`').to_uppercase(); @@ -1825,6 +2059,10 @@ impl<'a> Gen<'a> { params.join(", ") ), }; + let body = match var && throws { + true => format!("get throws {{\n{}\n}}", indent(&body)), + false => body, + }; let code = format!("{}{head} {{\n{}\n}}\n", docs(text.trim(), ""), indent(&body)); (code, full(is_static, &member, labels)) } @@ -2189,7 +2427,8 @@ mod tests { let decl = decl.trim_end_matches('{').trim(); let (name, conforms) = decl.split_once(':').unwrap_or((decl, "")); let names = std::iter::once(name).chain(conforms.split(',')); - for name in names.map(str::trim).filter(|n| !n.is_empty() && !n.starts_with('@')) { + // A qualified name, such as `Swift.Error`, cannot clash. + for name in (names.map(str::trim)).filter(|n| !n.is_empty() && !n.starts_with('@') && !n.contains('.')) { assert!(TAKEN.contains(&name), "the runtime's `{name}` is not in TAKEN"); } } diff --git a/ffi/tests/api.c b/ffi/tests/api.c index 86200ae3d..515885996 100644 --- a/ffi/tests/api.c +++ b/ffi/tests/api.c @@ -290,7 +290,7 @@ static bool drive_poll(void *data, api_Moment now, const api_Waiter *waiter, api } if (d->calls == 2) { printf("drive %llu\n", (unsigned long long)(api_Moment_millis(&now) - api_Moment_millis(&d->start))); - api_waker returned = api_Waiter_waker(waiter); + api_waker returned = api_Waiter_waker(waiter, NULL); returned.wake(returned.data); returned.drop(returned.data); *out = (api_option_Moment){0}; @@ -375,7 +375,7 @@ static bool broken_seek(void *data, int whence, int64_t offset, uint64_t *positi /* The payload sent last, ready at the first poll. */ static api_buf received(api_Channel *channel) { - api_Channel_recv_future *recv = api_Channel_recv(channel); + api_Channel_recv_future *recv = api_Channel_recv(channel, NULL); api_buf payload = {0}; bool ready = api_Channel_recv_poll(recv, (api_waker){0}, &payload) == API_POLL_READY; api_Channel_recv_future_free(recv); @@ -390,13 +390,13 @@ static bool forwarding(void) { api_Channel *channel = api_Channel_new(); api_buf payload = api_token_bytes(3, 64); const uint8_t *at = payload.ptr; - api_Channel_send(channel, payload); + api_Channel_send(channel, payload, NULL); payload = received(channel); bool same = payload.ptr == at && payload.len == 64; - api_Channel_send(channel, (api_buf){.ptr = payload.ptr + 1, .len = 62, .owner = payload.owner}); + api_Channel_send(channel, (api_buf){.ptr = payload.ptr + 1, .len = 62, .owner = payload.owner}, NULL); payload = received(channel); same = same && payload.ptr == at + 1 && payload.len == 62; - api_Channel_send(channel, api_buf_clone(payload)); + api_Channel_send(channel, api_buf_clone(payload), NULL); api_buf shared = received(channel); same = same && shared.ptr == payload.ptr && shared.len == 62; api_buf_free(payload); @@ -431,11 +431,21 @@ static bool closures(void) { api_Ticket held = api_reissue(seven, reissue_kept, &kept); printf("reissued %u %u, cloned %u\n", (unsigned)api_Ticket_number(&given), (unsigned)api_Ticket_number(&held), (unsigned)api_Ticket_clones()); + /* A record's traits borrow the checked handle it holds. */ + api_Mark *nine = api_Mark_of(9); + api_Seat front = api_seat(nine, 1, NULL); + api_Seat back = api_seat(nine, 2, NULL); + print("seat ", api_Seat_debug(&front, NULL), ""); + printf(" %s %s\n", api_Seat_eq(&front, &front, NULL) ? "true" : "false", + api_Seat_eq(&front, &back, NULL) ? "true" : "false"); + api_Seat_free(&back); + api_Seat_free(&front); + api_Mark_free(nine); /* A kept closure, called on another thread after the call that took it returned. */ bool spawned_freed = false; api_Spawned *spawned = api_Spawned_new(spawned_task, &spawned_freed, spawned_free); - printf("spawned %u\n", (unsigned)api_Spawned_join(spawned)); + printf("spawned %u\n", (unsigned)api_Spawned_join(spawned, NULL)); api_Spawned_free(spawned); if (!spawned_freed) { return false; @@ -501,8 +511,8 @@ static void log_free(void *data) { /* A lent closure: adds data's amount to the counter lent for the call. */ static bool edit(void *data, api_Counter *counter) { - api_Counter_add(counter, *(const uint64_t *)data, NULL, NULL); - return api_Counter_edited(counter) && api_Counter_get(counter) == 6; + api_Counter_add(counter, *(const uint64_t *)data, NULL, NULL, NULL); + return api_Counter_edited(counter, NULL) && api_Counter_get(counter, NULL) == 6; } /* Lent closures borrowing their object shared: one `Sync`, one not. */ @@ -519,7 +529,7 @@ static uint32_t triple(void *data, uint32_t side) { static uint64_t visit(void *data, const api_Cursor *cursor) { (void)data; - return api_Cursor_at(cursor) * 2; + return api_Cursor_at(cursor, NULL) * 2; } static void print_visit(void *data, uint32_t at) { @@ -529,7 +539,7 @@ static void print_visit(void *data, uint32_t at) { static uint32_t peek_tally(void *data, const api_Tally *tally) { (void)data; - return api_Tally_bump(tally); + return api_Tally_bump(tally, NULL); } /* A lent closure, which Rust calls on another thread: keeps levels above data's. */ @@ -547,7 +557,7 @@ static bool free_lent(void *data, api_Counter *counter) { static bool move_lent(void *data, api_Counter *counter) { (void)data; - api_Label label = api_Counter_into_label(counter, (api_option_Point){0}); + api_Label label = api_Counter_into_label(counter, (api_option_Point){0}, NULL); api_Label_free(&label); return false; } @@ -622,33 +632,33 @@ static int script(void) { printf("default %d %d\n", d.x, d.y); api_Counter *c = api_Counter_new(str("c"), 5); - printf("get %llu\n", (unsigned long long)api_Counter_get(c)); - print("name ", api_Counter_name(c), "\n"); + printf("get %llu\n", (unsigned long long)api_Counter_get(c, NULL)); + print("name ", api_Counter_name(c, NULL), "\n"); uint64_t v; api_Error err; - if (api_Counter_add(c, 10, &v, &err)) { + if (api_Counter_add(c, 10, &v, &err, NULL)) { printf("add ok %llu\n", (unsigned long long)v); } - if (!api_Counter_add(c, 2000, &v, &err)) { + if (!api_Counter_add(c, 2000, &v, &err, NULL)) { print("add err ", api_Error_to_string(&err), " "); print("", api_Error_debug(&err), "\n"); api_Error_free(&err); } - if (!api_Counter_sub(c, 20, &v, &err)) { + if (!api_Counter_sub(c, 20, &v, &err, NULL)) { printf("sub err underflow by %llu ", (unsigned long long)err.underflow.by); print("", api_Error_debug(&err), "\n"); api_Error_free(&err); } - api_Counter_rename(c, str("d")); - print("name ", api_Counter_name(c), "\n"); - printf("is d %s\n", api_Counter_is(c, str("d")) ? "true" : "false"); - api_Counter *copy = api_Counter_clone(c); - api_Counter_add(copy, 1, NULL, NULL); - printf("clone %llu original %llu\n", (unsigned long long)api_Counter_get(copy), - (unsigned long long)api_Counter_get(c)); + api_Counter_rename(c, str("d"), NULL); + print("name ", api_Counter_name(c, NULL), "\n"); + printf("is d %s\n", api_Counter_is(c, str("d"), NULL) ? "true" : "false"); + api_Counter *copy = api_Counter_clone(c, NULL); + api_Counter_add(copy, 1, NULL, NULL, NULL); + printf("clone %llu original %llu\n", (unsigned long long)api_Counter_get(copy, NULL), + (unsigned long long)api_Counter_get(c, NULL)); api_Counter_free(copy); - print("debug ", api_Counter_debug(c), "\n"); - api_Label label = api_Counter_into_label(c, (api_option_Point){.some = true, .value = p}); + print("debug ", api_Counter_debug(c, NULL), "\n"); + api_Label label = api_Counter_into_label(c, (api_option_Point){.some = true, .value = p}, NULL); api_Counter_free(c); print("label ", api_Label_debug(&label), "\n"); api_Label_free(&label); @@ -659,20 +669,20 @@ static int script(void) { /* A tuple returns through one out pointer per element. */ api_Counter *whole = api_Counter_new(str("s"), 7); api_Counter *below, *above; - api_Counter_split(whole, 5, &below, &above); + api_Counter_split(whole, 5, &below, &above, NULL); api_Counter_free(whole); - print("split ", api_Counter_name(below), " "); - printf("%llu ", (unsigned long long)api_Counter_get(below)); - print("", api_Counter_name(above), " "); - printf("%llu\n", (unsigned long long)api_Counter_get(above)); + print("split ", api_Counter_name(below, NULL), " "); + printf("%llu ", (unsigned long long)api_Counter_get(below, NULL)); + print("", api_Counter_name(above, NULL), " "); + printf("%llu\n", (unsigned long long)api_Counter_get(above, NULL)); api_Counter_free(below); api_Counter_free(above); api_Counter *one = api_Counter_new(str("o"), 3); api_Counter *only; - api_only(one, &only); + api_only(one, &only, NULL); api_Counter_free(one); - print("only ", api_Counter_name(only), " "); - printf("%llu\n", (unsigned long long)api_Counter_get(only)); + print("only ", api_Counter_name(only, NULL), " "); + printf("%llu\n", (unsigned long long)api_Counter_get(only, NULL)); api_Counter_free(only); uint8_t w8; int16_t w16; @@ -726,21 +736,21 @@ static int script(void) { {._0 = str("low"), ._1 = api_Counter_new(str("l"), 1)}, {._0 = str("high"), ._1 = api_Counter_new(str("h"), 9)}, }; - api_Board *board = api_Board_new((api_vec_tuple_string_Counter){.ptr = meters, .len = 2}); + api_Board *board = api_Board_new((api_vec_tuple_string_Counter){.ptr = meters, .len = 2}, NULL); api_Counter_free(meters[0]._1); api_Counter_free(meters[1]._1); - api_vec_tuple_string_Counter ranked = api_Board_ranked(board); + api_vec_tuple_string_Counter ranked = api_Board_ranked(board, NULL); printf("ranked"); for (size_t i = 0; i < ranked.len; i++) { printf("%s%.*s ", i == 0 ? " " : ", ", (int)ranked.ptr[i]._0.len, (const char *)ranked.ptr[i]._0.ptr); - print("", api_Counter_name(ranked.ptr[i]._1), " "); - printf("%llu", (unsigned long long)api_Counter_get(ranked.ptr[i]._1)); + print("", api_Counter_name(ranked.ptr[i]._1, NULL), " "); + printf("%llu", (unsigned long long)api_Counter_get(ranked.ptr[i]._1, NULL)); } printf("\n"); api_vec_tuple_string_Counter_free(ranked); - api_Counter *best = api_Board_best(board); - print("best ", api_Counter_name(best), " "); - printf("%llu\n", (unsigned long long)api_Counter_get(best)); + api_Counter *best = api_Board_best(board, NULL); + print("best ", api_Counter_name(best, NULL), " "); + printf("%llu\n", (unsigned long long)api_Counter_get(best, NULL)); api_Counter_free(best); api_Board_free(board); /* A map crosses as its pairs and a set as its items, which Rust collects in order, each @@ -832,38 +842,38 @@ static int script(void) { /* Each lent counter is a new handle to a clone. */ /* A handle whose value moved in is still the caller's to free. */ api_Counter *moved = api_Counter_new(str("first"), 1); - api_Registry *registry = api_Registry_new(moved); + api_Registry *registry = api_Registry_new(moved, NULL); api_Counter_free(moved); - printf("last %s\n", api_Registry_last(registry) != NULL ? "true" : "false"); + printf("last %s\n", api_Registry_last(registry, NULL) != NULL ? "true" : "false"); moved = api_Counter_new(str("last"), 2); - api_Registry_push(registry, moved); + api_Registry_push(registry, moved, NULL); api_Counter_free(moved); - api_Counter *first = api_Registry_first_get(registry); - api_Counter_add(first, 10, NULL, NULL); - api_Counter *lent = api_Registry_first_get(registry); - printf("first %llu lent %llu\n", (unsigned long long)api_Counter_get(lent), - (unsigned long long)api_Counter_get(first)); + api_Counter *first = api_Registry_first_get(registry, NULL); + api_Counter_add(first, 10, NULL, NULL, NULL); + api_Counter *lent = api_Registry_first_get(registry, NULL); + printf("first %llu lent %llu\n", (unsigned long long)api_Counter_get(lent, NULL), + (unsigned long long)api_Counter_get(first, NULL)); api_Counter_free(lent); api_Counter_free(first); - api_Counter *last = api_Registry_last(registry); - api_Counter_add(last, 10, NULL, NULL); - lent = api_Registry_last(registry); - printf("last %llu lent %llu\n", (unsigned long long)api_Counter_get(lent), - (unsigned long long)api_Counter_get(last)); + api_Counter *last = api_Registry_last(registry, NULL); + api_Counter_add(last, 10, NULL, NULL, NULL); + lent = api_Registry_last(registry, NULL); + printf("last %llu lent %llu\n", (unsigned long long)api_Counter_get(lent, NULL), + (unsigned long long)api_Counter_get(last, NULL)); api_Counter_free(lent); api_Counter_free(last); uint64_t by = 5; - bool edited = api_Registry_edit(registry, edit, &by); - first = api_Registry_first_get(registry); - printf("edit %s %llu\n", edited ? "true" : "false", (unsigned long long)api_Counter_get(first)); + bool edited = api_Registry_edit(registry, edit, &by, NULL); + first = api_Registry_first_get(registry, NULL); + printf("edit %s %llu\n", edited ? "true" : "false", (unsigned long long)api_Counter_get(first, NULL)); api_Counter_free(first); /* A setter moves an object in, as assignment does, leaving the handle to be freed. */ api_Counter *second = api_Counter_new(str("second"), 3); - api_Registry_first_set(registry, second); + api_Registry_first_set(registry, second, NULL); api_Counter_free(second); - first = api_Registry_first_get(registry); - printf("set first %llu ", (unsigned long long)api_Counter_get(first)); - print("", api_Counter_name(first), "\n"); + first = api_Registry_first_get(registry, NULL); + printf("set first %llu ", (unsigned long long)api_Counter_get(first, NULL)); + print("", api_Counter_name(first, NULL), "\n"); api_Counter_free(first); printf("walk %llu\n", (unsigned long long)api_walk(3, visit, NULL)); printf("each %u\n", api_each(2, print_visit, NULL)); @@ -874,33 +884,33 @@ static int script(void) { /* A type generic only over lifetimes is a handle to its 'static form. */ api_Name *name = api_Name_owned(str("a")); api_Name *b = api_Name_owned(str("b")); - api_Name *joined = api_Name_join(name, b); + api_Name *joined = api_Name_join(name, b, NULL); api_Name_free(b); - print("joined ", api_Name_as_str(joined), " "); - print("", api_Name_debug(joined), " "); - printf("%s\n", api_Name_eq(joined, name) ? "true" : "false"); + print("joined ", api_Name_as_str(joined, NULL), " "); + print("", api_Name_debug(joined, NULL), " "); + printf("%s\n", api_Name_eq(joined, name, NULL) ? "true" : "false"); api_Name_free(name); char text[] = "c"; - api_Name_rename(joined, str(text)); + api_Name_rename(joined, str(text), NULL); /* The name took a copy: the caller's buffer is its own again. */ text[0] = 'x'; - print("renamed ", api_Name_as_str(joined), "\n"); + print("renamed ", api_Name_as_str(joined, NULL), "\n"); api_Name *dd = api_Name_owned(str("dd")); - api_Name *longest = api_longer(joined, dd); - print("longer ", api_Name_as_str(longest), "\n"); + api_Name *longest = api_longer(joined, dd, NULL); + print("longer ", api_Name_as_str(longest, NULL), "\n"); api_Name_free(longest); api_Name_free(dd); api_Name_free(joined); api_Name *unnamed = api_unnamed(); - print("unnamed [", api_Name_as_str(unnamed), "]\n"); + print("unnamed [", api_Name_as_str(unnamed, NULL), "]\n"); api_Name_free(unnamed); /* Two buffers of equal text, interned into one string. */ char lit_text[] = "lit", again_text[] = "lit"; api_Name *lit = api_Name_borrowed(str(lit_text)); api_Name *again = api_Name_borrowed(str(again_text)); api_Name *other = api_Name_borrowed(str("other")); - print("borrowed ", api_Name_as_str(lit), " "); - printf("%s %s\n", api_Name_shares(lit, again) ? "true" : "false", api_Name_shares(lit, other) ? "true" : "false"); + print("borrowed ", api_Name_as_str(lit, NULL), " "); + printf("%s %s\n", api_Name_shares(lit, again, NULL) ? "true" : "false", api_Name_shares(lit, other, NULL) ? "true" : "false"); api_Name_free(lit); api_Name_free(again); api_Name_free(other); @@ -927,9 +937,9 @@ static int script(void) { return 1; } api_Digest *digest = api_Digest_new(); - api_Digest_sum_set(digest, sum); - api_array_u8_4 got = api_Digest_sum_get(digest); - api_array_u8_4 masked = api_Digest_flipped(digest, (api_array_u8_4){{0xff, 0, 0xff, 0}}); + api_Digest_sum_set(digest, sum, NULL); + api_array_u8_4 got = api_Digest_sum_get(digest, NULL); + api_array_u8_4 masked = api_Digest_flipped(digest, (api_array_u8_4){{0xff, 0, 0xff, 0}}, NULL); printf("digest %02x%02x%02x%02x %02x%02x%02x%02x", got.items[0], got.items[1], got.items[2], got.items[3], masked.items[0], masked.items[1], masked.items[2], masked.items[3]); print(" ", unparsed_err, "\n"); @@ -937,17 +947,17 @@ static int script(void) { api_Batch *batch = api_Batch_new(); unsigned accepted = 0; for (uint32_t v = 1; v <= 5; v++) { - accepted += api_Batch_push(batch, v); + accepted += api_Batch_push(batch, v, NULL); } api_Batch_2 *batch2 = api_pair(); unsigned paired = 0; for (uint32_t v = 1; v <= 3; v++) { - paired += api_Batch_2_push(batch2, v); + paired += api_Batch_2_push(batch2, v, NULL); } - unsigned long long emptied = api_drain_4(batch); - printf("batch %zu %u %llu %llu %zu %u %llu\n", api_Batch_capacity(batch), accepted, emptied, - (unsigned long long)api_drain_4(batch), api_Batch_2_capacity(batch2), paired, (unsigned long long)api_drain_2(batch2)); - printf("capacities %zu %zu\n", api_capacities_4_2(batch, batch2), api_capacities_2_2(batch2, batch2)); + unsigned long long emptied = api_drain_4(batch, NULL); + printf("batch %zu %u %llu %llu %zu %u %llu\n", api_Batch_capacity(batch, NULL), accepted, emptied, + (unsigned long long)api_drain_4(batch, NULL), api_Batch_2_capacity(batch2, NULL), paired, (unsigned long long)api_drain_2(batch2, NULL)); + printf("capacities %zu %zu\n", api_capacities_4_2(batch, batch2, NULL), api_capacities_2_2(batch2, batch2, NULL)); api_Batch_2_free(batch2); api_Batch_free(batch); api_range_u64 wide = api_widen((api_range_u64){.start = 2, .end = 5}, 1); @@ -1024,20 +1034,20 @@ static int script(void) { api_Stamp rounded = api_Stamp_whole(&stamp); print("whole ", api_Stamp_debug(&rounded), "\n"); api_Message *message = api_Message_new((api_option_Stamp){.some = true, .value = stamp}, str("body")); - print("payload ", api_Message_payload_get(message), "\n"); - api_option_Stamp read = api_Message_stamp_get(message); + print("payload ", api_Message_payload_get(message, NULL), "\n"); + api_option_Stamp read = api_Message_stamp_get(message, NULL); print("stamp ", api_Stamp_debug(&read.value), "\n"); - printf("sent %s\n", api_Message_sent(message) ? "true" : "false"); + printf("sent %s\n", api_Message_sent(message, NULL) ? "true" : "false"); /* Setters assign in place: the bytes are copied in, and a `Copy` value copied. */ api_Stamp reset = api_Stamp_from_millis(4000); - api_Message_stamp_set(message, (api_option_Stamp){.some = true, .value = reset}); - api_Message_payload_set(message, str("edited")); - print("payload ", api_Message_payload_get(message), "\n"); - read = api_Message_stamp_get(message); + api_Message_stamp_set(message, (api_option_Stamp){.some = true, .value = reset}, NULL); + api_Message_payload_set(message, str("edited"), NULL); + print("payload ", api_Message_payload_get(message, NULL), "\n"); + read = api_Message_stamp_get(message, NULL); print("stamp ", api_Stamp_debug(&read.value), "\n"); printf("secs %llu\n", (unsigned long long)api_Stamp_secs(&reset)); - api_Message_stamp_set(message, (api_option_Stamp){0}); - printf("stamp %s\n", api_Message_stamp_get(message).some ? "true" : "false"); + api_Message_stamp_set(message, (api_option_Stamp){0}, NULL); + printf("stamp %s\n", api_Message_stamp_get(message, NULL).some ? "true" : "false"); api_Message_free(message); /* A value past a word, copied across: a closure takes and returns one by value. */ @@ -1106,6 +1116,21 @@ static int script(void) { printf("weighted %llu %llu\n", (unsigned long long)api_Weighted_total(&weighted), (unsigned long long)(api_Score_count(&weighted.score) + api_Score_count(&step))); api_Weighted_free(&weighted); + /* A checked mark beside bytes in a tuple and in a record lent, and bytes beside a move. */ + api_Mark *mark = api_Mark_of(1); + api_Mark_add(mark, 2, NULL); + api_Counter *hefted = api_Counter_new(str("w"), 1); + printf("heft %llu\n", (unsigned long long)api_heft((api_tuple_Mark_bytes){mark, str("ab")}, + (api_Parcel){.mark = mark, .payload = str("abc")}, + (api_tuple_Counter_bytes){hefted, str("abcd")}, NULL)); + api_Counter_free(hefted); + /* A value's `&mut self` beside a checked handle. */ + api_Score noted = api_Score_of(2); + api_Mark *noted_by = api_Mark_of(5); + uint64_t noted_before = api_Score_note(¬ed, noted_by, NULL); + printf("noted %llu %llu\n", (unsigned long long)noted_before, (unsigned long long)api_Score_count(¬ed)); + api_Mark_free(noted_by); + api_Mark_free(mark); static const api_Log_vtable print_log = {.free = log_free, .log = log_line}; uint8_t levels[] = {1, 2, 3, 4}; @@ -1120,17 +1145,17 @@ static int script(void) { api_Channel *channel = api_Channel_new(); api_waker waker = {.wake = wake, .drop = drop, .data = NULL}; - api_Channel_recv_future *recv = api_Channel_recv(channel); + api_Channel_recv_future *recv = api_Channel_recv(channel, NULL); api_buf payload; printf("recv pending %s\n", api_Channel_recv_poll(recv, waker, &payload) == API_POLL_PENDING ? "true" : "false"); - api_Channel_send(channel, str("hi")); + api_Channel_send(channel, str("hi"), NULL); printf("woken %d\n", wakes); if (api_Channel_recv_poll(recv, waker, &payload) == API_POLL_READY) { print("recv ", payload, "\n"); } api_Channel_recv_future_free(recv); - api_Channel_send(channel, str("")); - api_Channel_recv_nonempty_future *nonempty = api_Channel_recv_nonempty(channel); + api_Channel_send(channel, str(""), NULL); + api_Channel_recv_nonempty_future *nonempty = api_Channel_recv_nonempty(channel, NULL); switch (api_Channel_recv_nonempty_poll(nonempty, waker, &payload, &err)) { case API_POLL_READY: printf("nonempty %zu\n", payload.len); @@ -1146,8 +1171,8 @@ static int script(void) { api_Channel_recv_nonempty_future_free(nonempty); api_Batch *fill4 = api_Batch_new(); api_Batch_2 *fill2 = api_pair(); - api_fill_4_future *filling4 = api_fill_4(fill4, 9); - api_fill_2_future *filling2 = api_fill_2(fill2, 1); + api_fill_4_future *filling4 = api_fill_4(fill4, 9, NULL); + api_fill_2_future *filling2 = api_fill_2(fill2, 1, NULL); api_vec_u32 filled4, filled2; if (api_fill_4_poll(filling4, waker, &filled4) == API_POLL_READY && api_fill_2_poll(filling2, waker, &filled2) == API_POLL_READY) { printf("fill"); @@ -1167,14 +1192,14 @@ static int script(void) { api_Batch_free(fill4); /* The send happens at the call: the future is dropped unpolled. */ - api_Channel_send_later_future_free(api_Channel_send_later(channel, str("eager"))); - recv = api_Channel_recv(channel); + api_Channel_send_later_future_free(api_Channel_send_later(channel, str("eager"), NULL)); + recv = api_Channel_recv(channel, NULL); if (api_Channel_recv_poll(recv, waker, &payload) == API_POLL_READY) { print("recv ", payload, "\n"); } api_Channel_recv_future_free(recv); /* The send happens at the call, and the future borrows the channel and the tag. */ - api_Channel_tagged_future *tagging = api_Channel_tagged(channel, str("tag")); + api_Channel_tagged_future *tagging = api_Channel_tagged(channel, str("tag"), NULL); api_buf tagged_text; if (api_Channel_tagged_poll(tagging, waker, &tagged_text) == API_POLL_READY) { print("tagged ", tagged_text, "\n"); @@ -1182,7 +1207,7 @@ static int script(void) { api_Channel_tagged_future_free(tagging); /* So does a future borrowing an optional slice, which the export owns while it is pending. */ const uint32_t items[] = {1, 2, 3}; - api_Channel_itemized_future *itemizing = api_Channel_itemized(channel, (api_vec_u32){.ptr = items, .len = 3}); + api_Channel_itemized_future *itemizing = api_Channel_itemized(channel, (api_vec_u32){.ptr = items, .len = 3}, NULL); size_t itemized; if (api_Channel_itemized_poll(itemizing, waker, &itemized) == API_POLL_READY) { printf("itemized %zu\n", itemized); @@ -1195,8 +1220,8 @@ static int script(void) { printf("byte_len %zu\n", byte_len); } api_byte_len_future_free(measuring); - api_Channel_send(channel, str("sized")); - api_Channel_recv_sized_future *sized = api_Channel_recv_sized(channel); + api_Channel_send(channel, str("sized"), NULL); + api_Channel_recv_sized_future *sized = api_Channel_recv_sized(channel, NULL); size_t len; if (api_Channel_recv_sized_poll(sized, waker, &payload, &len) == API_POLL_READY) { print("sized ", payload, " "); @@ -1204,49 +1229,49 @@ static int script(void) { } api_Channel_recv_sized_future_free(sized); /* The waker moves into Rust, which drops it once it wakes. */ - api_Channel_notify(channel, waker); - api_Channel_send(channel, str("rung")); + api_Channel_notify(channel, waker, NULL); + api_Channel_send(channel, str("rung"), NULL); printf("woken %d\n", wakes); /* A poll with no async twin is a future: it takes what the last send left. */ - api_Channel_poll_take_future *take = api_Channel_poll_take(channel); + api_Channel_poll_take_future *take = api_Channel_poll_take(channel, NULL); if (api_Channel_poll_take_poll(take, waker, &payload) == API_POLL_READY) { print("take ", payload, "\n"); } api_Channel_poll_take_future_free(take); - api_Channel_send(channel, str("measured")); - api_Channel_poll_measure_future *measure = api_Channel_poll_measure(channel, 2); + api_Channel_send(channel, str("measured"), NULL); + api_Channel_poll_measure_future *measure = api_Channel_poll_measure(channel, 2, NULL); if (api_Channel_poll_measure_poll(measure, waker, &len, &err) == API_POLL_READY) { printf("measure %zu\n", len); } api_Channel_poll_measure_future_free(measure); /* A context is made of the waker, which Rust keeps until the send wakes it. */ - api_Channel_listen(channel, waker); - api_Channel_send(channel, str("heard")); + api_Channel_listen(channel, waker, NULL); + api_Channel_send(channel, str("heard"), NULL); printf("woken %d\n", wakes); /* A returned waker is the caller's: it wakes the waiter's task until dropped. */ api_Waiter *waiter = api_Waiter_new(waker); - api_waker returned = api_Waiter_waker(waiter); + api_waker returned = api_Waiter_waker(waiter, NULL); returned.wake(returned.data); returned.drop(returned.data); - returned = api_Waiter_context(waiter); + returned = api_Waiter_context(waiter, NULL); returned.wake(returned.data); printf("woken %d\n", wakes); /* Passed back, it moves into Rust as any waker does. */ int woken = wakes; - api_Channel_notify(channel, returned); + api_Channel_notify(channel, returned, NULL); api_Waiter_free(waiter); - api_Channel_send(channel, str("again")); + api_Channel_send(channel, str("again"), NULL); if (wakes != woken + 1) { return 1; } - api_Channel_clear(channel); + api_Channel_clear(channel, NULL); /* The waiter a pending poll registers lives until the next poll, so the send wakes it. */ - take = api_Channel_poll_take(channel); + take = api_Channel_poll_take(channel, NULL); if (api_Channel_poll_take_poll(take, waker, &payload) != API_POLL_PENDING) { return 1; } woken = wakes; - api_Channel_send(channel, str("later")); + api_Channel_send(channel, str("later"), NULL); if (wakes != woken + 1 || api_Channel_poll_take_poll(take, waker, &payload) != API_POLL_READY) { return 1; } @@ -1255,17 +1280,17 @@ static int script(void) { api_Channel_free(channel); /* A value that holds itself is an object. */ - api_Node *list = api_Node_new(1, NULL); - api_Node_push(list, 2); - printf("node %llu\n", (unsigned long long)api_Node_sum(list)); + api_Node *list = api_Node_new(1, NULL, NULL); + api_Node_push(list, 2, NULL); + printf("node %llu\n", (unsigned long long)api_Node_sum(list, NULL)); api_Node_free(list); api_Tally *tally = api_Tally_new(); - api_Tally_bump(tally); - uint32_t count = api_Tally_bump(tally); - printf("tally %u %s\n", count, api_Tally_eq(tally, tally) ? "true" : "false"); - printf("both %u\n", api_bump_both(tally, tally)); - printf("peek %u\n", api_peek(tally, peek_tally, NULL)); + api_Tally_bump(tally, NULL); + uint32_t count = api_Tally_bump(tally, NULL); + printf("tally %u %s\n", count, api_Tally_eq(tally, tally, NULL) ? "true" : "false"); + printf("both %u\n", api_bump_both(tally, tally, NULL)); + printf("peek %u\n", api_peek(tally, peek_tally, NULL, NULL)); api_Tally_free(tally); /* Another export's types, which this export's function takes and returns. */ @@ -1299,27 +1324,27 @@ static int script(void) { const char *names[] = {"a", "b"}; for (int i = 0; i < 2; i++) { api_Counter *pushed = api_Counter_new(str(names[i]), (uint64_t)i + 1); - api_Stack_push(stack, pushed); + api_Stack_push(stack, pushed, NULL); api_Counter_free(pushed); } - api_Counter *top = api_Stack_pop(stack); - printf("stack %zu Some(%llu)", api_Stack_depth(stack), (unsigned long long)api_Counter_get(top)); + api_Counter *top = api_Stack_pop(stack, NULL); + printf("stack %zu Some(%llu)", api_Stack_depth(stack, NULL), (unsigned long long)api_Counter_get(top, NULL)); api_Counter_free(top); - api_Counter *next = api_Stack_pop(stack); + api_Counter *next = api_Stack_pop(stack, NULL); printf(" %s", next != NULL ? "true" : "false"); api_Counter_free(next); - printf(" %s\n", api_Stack_pop(stack) == NULL ? "true" : "false"); + printf(" %s\n", api_Stack_pop(stack, NULL) == NULL ? "true" : "false"); /* Generic functions and methods, at each implementation of the trait bounding them, by reference, which leaves C its handle. */ api_Counter *from_point = api_Counter_seeded_Point(str("p"), (api_Point){.x = -3, .y = 4}); api_Counter *from_size = api_Counter_seeded_Size(str("s"), (api_Size){.width = 5, .height = 6}); - api_Counter *from_counter = api_Counter_seeded_Counter(str("c"), from_point); + api_Counter *from_counter = api_Counter_seeded_Counter(str("c"), from_point, NULL); /* Two instances whose arguments share a last segment: one takes its module too. */ api_Counter *from_polar = api_Counter_seeded_polar_Point(str("q"), (api_polar_Point){.turns = 1, .distance = 2}); - printf("seeded %llu %llu %llu %llu past %llu seeds %llu\n", (unsigned long long)api_Counter_get(from_point), - (unsigned long long)api_Counter_get(from_size), (unsigned long long)api_Counter_get(from_counter), - (unsigned long long)api_Counter_get(from_polar), - (unsigned long long)api_Stack_depth_past_Point(stack, (api_Point){.x = 1, .y = 2}), + printf("seeded %llu %llu %llu %llu past %llu seeds %llu\n", (unsigned long long)api_Counter_get(from_point, NULL), + (unsigned long long)api_Counter_get(from_size, NULL), (unsigned long long)api_Counter_get(from_counter, NULL), + (unsigned long long)api_Counter_get(from_polar, NULL), + (unsigned long long)api_Stack_depth_past_Point(stack, (api_Point){.x = 1, .y = 2}, NULL), (unsigned long long)api_seeds_Point_Size((api_Size){.width = 2, .height = 3})); api_Counter_free(from_point); api_Counter_free(from_size); @@ -1373,17 +1398,17 @@ static int script(void) { /* Its Drop runs when C frees the handle, not before. */ api_Token *token = api_Token_new(7); - printf("token %u dropped %u\n", api_Token_id_get(token), api_dropped()); + printf("token %u dropped %u\n", api_Token_id_get(token, NULL), api_dropped()); api_Token_free(token); printf("dropped %u\n", api_dropped()); /* A field and the method of its name, each its own function. */ api_Retry *retry = api_Retry_new((api_option_u32){.some = true, .value = 5}); - api_option_u32 limit = api_Retry_limit_get(retry); - printf("retry %d %u %u %u\n", limit.some, limit.value, api_Retry_limit(retry), api_Retry_delay(retry)); + api_option_u32 limit = api_Retry_limit_get(retry, NULL); + printf("retry %d %u %u %u\n", limit.some, limit.value, api_Retry_limit(retry, NULL), api_Retry_delay(retry, NULL)); /* The renamed field's setter, named as its getter. */ - api_Retry_limit_set(retry, (api_option_u32){0}); - printf("retry %u\n", api_Retry_limit(retry)); + api_Retry_limit_set(retry, (api_option_u32){0}, NULL); + printf("retry %u\n", api_Retry_limit(retry, NULL)); api_Retry_free(retry); api_Slot_u8 *slot = api_Slot_u8_new(200); printf("slot %u\n", api_Slot_u8_value(slot)); @@ -1394,8 +1419,8 @@ static int script(void) { if (!api_Url_parse(str("https://example.com/a/b"), &base, &parse_err)) { return 1; } - print("url ", api_Url_to_string(base), " host "); - api_buf host = api_host(base); + print("url ", api_Url_to_string(base, NULL), " host "); + api_buf host = api_host(base, NULL); if (host.ptr != NULL) { print("Some(\"", host, "\")\n"); } else { @@ -1403,10 +1428,10 @@ static int script(void) { } api_Url *resolved; api_buf resolve_err; - api_Url *base_clone = api_Url_clone(base); - if (api_resolve(base_clone, str("c?d"), &resolved, &resolve_err)) { - print("resolve ", api_Url_to_string(resolved), " "); - printf("%s\n", api_Url_eq(resolved, base) ? "true" : "false"); + api_Url *base_clone = api_Url_clone(base, NULL); + if (api_resolve(base_clone, str("c?d"), &resolved, &resolve_err, NULL)) { + print("resolve ", api_Url_to_string(resolved, NULL), " "); + printf("%s\n", api_Url_eq(resolved, base, NULL) ? "true" : "false"); api_Url_free(resolved); } else { print("resolve err ", resolve_err, "\n"); @@ -1415,7 +1440,7 @@ static int script(void) { api_Url_free(base); api_Url *bad; if (api_Url_parse(str("no scheme"), &bad, &parse_err)) { - print("parse ", api_Url_to_string(bad), "\n"); + print("parse ", api_Url_to_string(bad, NULL), "\n"); api_Url_free(bad); } else { print("parse err ", parse_err, "\n"); @@ -1495,12 +1520,12 @@ static int script(void) { api_coordinates((api_option_Point){0})); api_Counter *two = api_Counter_new(str("two"), 2); api_Counter *three = api_Counter_new(str("three"), 3); - printf("plus %llu %llu\n", (unsigned long long)api_Counter_plus(two, three), - (unsigned long long)api_Counter_plus(two, NULL)); - bool added = api_Counter_add_to(two, three); - bool added_none = api_Counter_add_to(two, NULL); + printf("plus %llu %llu\n", (unsigned long long)api_Counter_plus(two, three, NULL), + (unsigned long long)api_Counter_plus(two, NULL, NULL)); + bool added = api_Counter_add_to(two, three, NULL); + bool added_none = api_Counter_add_to(two, NULL, NULL); printf("add_to %s %s %llu\n", added ? "true" : "false", added_none ? "true" : "false", - (unsigned long long)api_Counter_get(three)); + (unsigned long long)api_Counter_get(three, NULL)); api_Counter_free(two); api_Counter_free(three); @@ -1530,44 +1555,44 @@ static int script(void) { /* An Arc of another crate's type: an opaque handle, taken back by value or lent. */ api_tls_Config *config = api_secure(str("h3")); - print("secure ", api_alpn_lent(config), " "); - api_tls_Config *config_copy = api_tls_Config_clone(config); - print("", api_alpn(config_copy), "\n"); + print("secure ", api_alpn_lent(config, NULL), " "); + api_tls_Config *config_copy = api_tls_Config_clone(config, NULL); + print("", api_alpn(config_copy, NULL), "\n"); api_tls_Config_free(config_copy); api_tls_Config_free(config); /* Another crate's value by value: an opaque handle, lent or taken back. */ api_tls_Cert *cert = api_certify(str("der")); - printf("cert %llu ", (unsigned long long)api_cert_len(cert)); - print("", api_cert_der(cert), " "); + printf("cert %llu ", (unsigned long long)api_cert_len(cert, NULL)); + print("", api_cert_der(cert, NULL), " "); api_tls_Cert_free(cert); api_tls_Cert *chain[2] = {api_certify(str("a")), api_certify(str("bc"))}; - printf("%llu\n", (unsigned long long)api_chain_len((api_vec_tls_Cert){.ptr = chain, .len = 2})); + printf("%llu\n", (unsigned long long)api_chain_len((api_vec_tls_Cert){.ptr = chain, .len = 2}, NULL)); api_tls_Cert_free(chain[0]); api_tls_Cert_free(chain[1]); /* Another crate's trait objects: an `Arc`, taken back, and `&'static` ones, held by value. */ api_tls_Resolver *resolver = api_resolver(str("r1")); - print("resolver ", api_resolved(resolver), "\n"); + print("resolver ", api_resolved(resolver, NULL), "\n"); api_tls_Resolver_free(resolver); api_Codecs *codecs = api_Codecs_new(); - printf("codecs %u\n", api_Codecs_ids(codecs)); - api_vec_tls_Codec listed = api_Codecs_list_get(codecs); + printf("codecs %u\n", api_Codecs_ids(codecs, NULL)); + api_vec_tls_Codec listed = api_Codecs_list_get(codecs, NULL); api_tls_Codec *codecs_reversed[2] = {listed.ptr[1], listed.ptr[0]}; - api_Codecs_list_set(codecs, (api_vec_tls_Codec){.ptr = codecs_reversed, .len = 2}); + api_Codecs_list_set(codecs, (api_vec_tls_Codec){.ptr = codecs_reversed, .len = 2}, NULL); /* The setter took each handle's value: the array, and the handles, are still ours to free. */ api_vec_tls_Codec_free(listed); - printf("codecs reversed %u\n", api_Codecs_ids(codecs)); + printf("codecs reversed %u\n", api_Codecs_ids(codecs, NULL)); api_Codecs_free(codecs); /* A standard library value no type maps, in a handle. */ api_std_io_Error *failure = api_failure(str("boom")); - print("failure ", api_failure_message(failure), " "); - print("", api_failure_kind(failure), "\n"); + print("failure ", api_failure_message(failure, NULL), " "); + print("", api_failure_kind(failure, NULL), "\n"); api_std_io_Error_free(failure); /* Iterators, passed as the arrays of their items. */ const uint32_t summed_items[] = {1, 2, 3}; printf("sum_all %u\n", api_sum_all((api_vec_u32){.ptr = summed_items, .len = 3})); /* Each counter moves in, leaving its handle only to free. */ api_Counter *weighed = api_Counter_new(str("w"), 3); - printf("weigh_all %llu\n", (unsigned long long)api_weigh_all_Counter((api_vec_Counter){.ptr = &weighed, .len = 1})); + printf("weigh_all %llu\n", (unsigned long long)api_weigh_all_Counter((api_vec_Counter){.ptr = &weighed, .len = 1}, NULL)); api_Counter_free(weighed); const api_buf alpns[] = {str("h3"), str("moq-00")}; printf("offered_len %zu\n", api_offered_len((api_vec_bytes){.ptr = alpns, .len = 2})); @@ -1596,40 +1621,40 @@ static int script(void) { /* A borrow of a meter that is not Clone, lent from its owner's handle. */ { api_Meters *lender = api_Meters_new(); - api_Meter *first = api_Meters_first(lender); - api_Meter_add(first, 2); - unsigned long long added = api_Meter_add(first, 3); - api_Meter *again = api_Meters_first(lender); - printf("lent %llu %llu\n", added, (unsigned long long)api_Meter_count(again)); + api_Meter *first = api_Meters_first(lender, NULL); + api_Meter_add(first, 2, NULL); + unsigned long long added = api_Meter_add(first, 3, NULL); + api_Meter *again = api_Meters_first(lender, NULL); + printf("lent %llu %llu\n", added, (unsigned long long)api_Meter_count(again, NULL)); api_Meter_free(again); api_Meter_free(first); - api_Meters_clear(lender); - again = api_Meters_first(lender); - printf("cleared %llu\n", (unsigned long long)api_Meter_count(again)); + api_Meters_clear(lender, NULL); + again = api_Meters_first(lender, NULL); + printf("cleared %llu\n", (unsigned long long)api_Meter_count(again, NULL)); api_Meter_free(again); /* A field that is not Clone, lent as a method's borrowed return is. */ - api_Meter *last = api_Meters_last_get(lender); - api_Meter_add(last, 4); - printf("field lent %llu\n", (unsigned long long)api_Meter_add(last, 1)); + api_Meter *last = api_Meters_last_get(lender, NULL); + api_Meter_add(last, 4, NULL); + printf("field lent %llu\n", (unsigned long long)api_Meter_add(last, 1, NULL)); api_Meter_free(last); /* An optional field that is not Clone, lent as `Some` of a handle. */ - api_Meter *spare = api_Meters_spare_get(lender); + api_Meter *spare = api_Meters_spare_get(lender, NULL); printf("spare %s\n", spare != NULL ? "true" : "false"); api_Meter *fresh = api_Meter_default(); - api_Meters_spare_set(lender, fresh); + api_Meters_spare_set(lender, fresh, NULL); api_Meter_free(fresh); - spare = api_Meters_spare_get(lender); - api_Meter_add(spare, 5); - printf("spare lent %llu\n", (unsigned long long)api_Meter_add(spare, 1)); + spare = api_Meters_spare_get(lender, NULL); + api_Meter_add(spare, 5, NULL); + printf("spare lent %llu\n", (unsigned long long)api_Meter_add(spare, 1, NULL)); api_Meter_free(spare); api_Meters_free(lender); /* One lent from a handle that is not Sync, on one thread at a time. */ api_Tallies *tallies = api_Tallies_new(); - api_Tally *tallied = api_Tallies_first(tallies); - api_Tally_bump(tallied); + api_Tally *tallied = api_Tallies_first(tallies, NULL); + api_Tally_bump(tallied, NULL); api_Tally_free(tallied); - tallied = api_Tallies_first(tallies); - printf("tallied %u\n", api_Tally_bump(tallied)); + tallied = api_Tallies_first(tallies, NULL); + printf("tallied %u\n", api_Tally_bump(tallied, NULL)); api_Tally_free(tallied); api_Tallies_free(tallies); } @@ -1650,19 +1675,19 @@ static int script(void) { api_vec_Counter counters = api_counters((api_vec_string){.ptr = counter_names, .len = 2}); printf("counters"); for (size_t i = 0; i < counters.len; i++) { - print(i == 0 ? " " : ", ", api_Counter_name(counters.ptr[i]), " "); - printf("%llu", (unsigned long long)api_Counter_get(counters.ptr[i])); + print(i == 0 ? " " : ", ", api_Counter_name(counters.ptr[i], NULL), " "); + printf("%llu", (unsigned long long)api_Counter_get(counters.ptr[i], NULL)); } printf("\n"); /* Each counter moves in, leaving its handle only to free. */ - uint64_t worth = api_worth((api_vec_Counter){.ptr = counters.ptr, .len = 1}, (api_vec_Counter){.ptr = counters.ptr + 1, .len = 1}); - uint64_t none = api_worth((api_vec_Counter){0}, (api_vec_Counter){.ptr = counters.ptr, .len = 0}); + uint64_t worth = api_worth((api_vec_Counter){.ptr = counters.ptr, .len = 1}, (api_vec_Counter){.ptr = counters.ptr + 1, .len = 1}, NULL); + uint64_t none = api_worth((api_vec_Counter){0}, (api_vec_Counter){.ptr = counters.ptr, .len = 0}, NULL); printf("worth %llu %llu\n", (unsigned long long)worth, (unsigned long long)none); api_vec_Counter_free(counters); api_Counter *held = api_Counter_new(str("h"), 4); api_option_u64 counts[] = { - api_held_count((api_option_tuple_Counter){.some = true, .value = {._0 = held}}), - api_held_count((api_option_tuple_Counter){0}), + api_held_count((api_option_tuple_Counter){.some = true, .value = {._0 = held}}, NULL), + api_held_count((api_option_tuple_Counter){0}, NULL), }; api_Counter_free(held); printf("held"); @@ -1675,9 +1700,9 @@ static int script(void) { } printf("\n"); api_Counter *boxed = api_boxed_counter(str("b")); - print("boxed Some(\"", api_boxed_name(boxed), "\") "); + print("boxed Some(\"", api_boxed_name(boxed, NULL), "\") "); api_Counter_free(boxed); - printf("%s %s\n", api_boxed_name(NULL).ptr ? "Some" : "None", api_boxed_counter((api_buf){0}) ? "false" : "true"); + printf("%s %s\n", api_boxed_name(NULL, NULL).ptr ? "Some" : "None", api_boxed_counter((api_buf){0}) ? "false" : "true"); printf("less %u\n", api_less(5, 2)); uint32_t row0[] = {1, 2}; @@ -1765,11 +1790,11 @@ static int script(void) { if (!api_Json_parse(str("{\"b\": [1, 2.5, null], \"a\": \"\\u00e9\"}"), &doc, &parse_err)) { return 1; } - print("json ", api_encode(doc), "\n"); - print("timed text ", api_timed_text(doc), "\n"); - api_Json *tagged = api_tagged(doc, str("t")); + print("json ", api_encode(doc, NULL), "\n"); + print("timed text ", api_timed_text(doc, NULL), "\n"); + api_Json *tagged = api_tagged(doc, str("t"), NULL); api_Json_free(doc); - print("tagged ", api_Json_to_text(tagged), "\n"); + print("tagged ", api_Json_to_text(tagged, NULL), "\n"); api_Json_free(tagged); /* A JSON object's entries cross as pairs of a key and its document, each moved in. */ api_tuple_string_Json json_pairs[2] = {{._0 = str("b")}, {._0 = str("a")}}; @@ -1777,19 +1802,19 @@ static int script(void) { !api_Json_parse(str("2"), &json_pairs[1]._1, &parse_err)) { return 1; } - api_vec_tuple_string_Json json_entries = api_entries((api_vec_tuple_string_Json){.ptr = json_pairs, .len = 2}, str("t")); + api_vec_tuple_string_Json json_entries = api_entries((api_vec_tuple_string_Json){.ptr = json_pairs, .len = 2}, str("t"), NULL); api_Json_free(json_pairs[0]._1); api_Json_free(json_pairs[1]._1); printf("entries"); for (size_t i = 0; i < json_entries.len; i++) { printf("%s%.*s ", i == 0 ? " " : ", ", (int)json_entries.ptr[i]._0.len, (const char *)json_entries.ptr[i]._0.ptr); - print("", api_Json_to_text(json_entries.ptr[i]._1), ""); + print("", api_Json_to_text(json_entries.ptr[i]._1, NULL), ""); } printf("\n"); api_vec_tuple_string_Json_free(json_entries); api_Json *broken; if (api_Json_parse(str("{\"a\":"), &broken, &parse_err)) { - print("json parse ", api_Json_to_text(broken), "\n"); + print("json parse ", api_Json_to_text(broken, NULL), "\n"); api_Json_free(broken); } else { print("json parse err ", parse_err, "\n"); @@ -1799,11 +1824,11 @@ static int script(void) { if (!api_Url_parse(str("https://example.com/a/b"), &home, &parse_err)) { return 1; } - api_Site *site = api_Site_new(home); - api_Url *home0 = api_Site_home_get(site); - api_Url *home1 = api_Site_home_get(site); - print("site ", api_Url_to_string(home0), " "); - print("", api_Url_to_string(home1), "\n"); + api_Site *site = api_Site_new(home, NULL); + api_Url *home0 = api_Site_home_get(site, NULL); + api_Url *home1 = api_Site_home_get(site, NULL); + print("site ", api_Url_to_string(home0, NULL), " "); + print("", api_Url_to_string(home1, NULL), "\n"); api_Url_free(home0); api_Url_free(home1); api_Site_free(site); @@ -1840,13 +1865,13 @@ static int script(void) { api_buf trail_names[] = {str("a"), str("b")}; api_Trail *trail = api_Trail_new((api_vec_Point){.ptr = trail_points, .len = 2}, (api_vec_string){.ptr = trail_names, .len = 2}); - api_vec_Point points = api_Trail_points(trail); + api_vec_Point points = api_Trail_points(trail, NULL); printf("trail ["); for (size_t i = 0; i < points.len; i++) { print(i == 0 ? "" : ", ", api_Point_debug(&points.ptr[i]), ""); } api_vec_Point_free(points); - api_vec_Label labels = api_Trail_labels(trail); + api_vec_Label labels = api_Trail_labels(trail, NULL); printf("] ["); for (size_t i = 0; i < labels.len; i++) { print(i == 0 ? "" : ", ", api_Label_debug(&labels.ptr[i]), ""); @@ -1854,32 +1879,32 @@ static int script(void) { printf("]\n"); api_vec_Label_free(labels); /* Returned iterators, each collected at the call: drained labels are gone after. */ - api_vec_Point iter_points = api_Trail_iter(trail); + api_vec_Point iter_points = api_Trail_iter(trail, NULL); printf("iterated ["); for (size_t i = 0; i < iter_points.len; i++) { print(i == 0 ? "" : ", ", api_Point_debug(&iter_points.ptr[i]), ""); } api_vec_Point_free(iter_points); - api_vec_string iter_names = api_Trail_names(trail); + api_vec_string iter_names = api_Trail_names(trail, NULL); printf("] ["); for (size_t i = 0; i < iter_names.len; i++) { printf("%s\"%.*s\"", i == 0 ? "" : ", ", (int)iter_names.ptr[i].len, (const char *)iter_names.ptr[i].ptr); } api_vec_string_free(iter_names); - api_buf iter_bytes = api_Trail_bytes(trail); + api_buf iter_bytes = api_Trail_bytes(trail, NULL); printf("] ["); for (size_t i = 0; i < iter_bytes.len; i++) { printf("%s%u", i == 0 ? "" : ", ", iter_bytes.ptr[i]); } printf("]\n"); api_buf_free(iter_bytes); - api_vec_Label drained = api_Trail_drain(trail); + api_vec_Label drained = api_Trail_drain(trail, NULL); printf("drained ["); for (size_t i = 0; i < drained.len; i++) { print(i == 0 ? "" : ", ", api_Label_debug(&drained.ptr[i]), ""); } api_vec_Label_free(drained); - api_vec_Label left = api_Trail_labels(trail); + api_vec_Label left = api_Trail_labels(trail, NULL); printf("] then ["); for (size_t i = 0; i < left.len; i++) { print(i == 0 ? "" : ", ", api_Label_debug(&left.ptr[i]), ""); @@ -1887,7 +1912,7 @@ static int script(void) { printf("]\n"); api_vec_Label_free(left); /* A filter, collected at the call. */ - api_vec_Point beyond = api_Trail_beyond(trail, 1); + api_vec_Point beyond = api_Trail_beyond(trail, 1, NULL); printf("beyond"); for (size_t i = 0; i < beyond.len; i++) { printf("%s%d %d", i == 0 ? " " : ", ", beyond.ptr[i].x, beyond.ptr[i].y); @@ -1899,8 +1924,8 @@ static int script(void) { api_vec_Counter numbered = api_numbered((api_vec_string){.ptr = numbered_names, .len = 2}); printf("numbered"); for (size_t i = 0; i < numbered.len; i++) { - print(i == 0 ? " " : ", ", api_Counter_name(numbered.ptr[i]), " "); - printf("%llu", (unsigned long long)api_Counter_get(numbered.ptr[i])); + print(i == 0 ? " " : ", ", api_Counter_name(numbered.ptr[i], NULL), " "); + printf("%llu", (unsigned long long)api_Counter_get(numbered.ptr[i], NULL)); } printf("\n"); api_vec_Counter_free(numbered); @@ -1917,13 +1942,13 @@ static int script(void) { !api_Json_parse(str("2"), §ion_pairs[1]._1, &parse_err)) { return 1; } - api_vec_tuple_string_Json sections = api_sections((api_vec_tuple_string_Json){.ptr = section_pairs, .len = 2}); + api_vec_tuple_string_Json sections = api_sections((api_vec_tuple_string_Json){.ptr = section_pairs, .len = 2}, NULL); api_Json_free(section_pairs[0]._1); api_Json_free(section_pairs[1]._1); printf("sections"); for (size_t i = 0; i < sections.len; i++) { printf("%s%.*s ", i == 0 ? " " : ", ", (int)sections.ptr[i]._0.len, (const char *)sections.ptr[i]._0.ptr); - print("", api_Json_to_text(sections.ptr[i]._1), ""); + print("", api_Json_to_text(sections.ptr[i]._1, NULL), ""); } printf("\n"); api_vec_tuple_string_Json_free(sections); @@ -1946,20 +1971,20 @@ static int script(void) { /* tracing's level, and its filter as an optional level whose none is `OFF`. */ api_Verbosity *verbosity = api_Verbosity_new(API_LEVEL_WARN); - print("verbosity ", api_spell(api_Verbosity_filter(verbosity)), " "); - print("", api_spell(api_louder(api_Verbosity_filter(verbosity))), "\n"); - api_Verbosity_level_set(verbosity, API_LEVEL_TRACE); - printf("verbosity %s ", api_Verbosity_level_get(verbosity) == API_LEVEL_TRACE ? "true" : "false"); - print("", api_spell(api_Verbosity_filter(verbosity)), " "); - print("", api_spell(api_louder(api_Verbosity_filter(verbosity))), " "); + print("verbosity ", api_spell(api_Verbosity_filter(verbosity, NULL)), " "); + print("", api_spell(api_louder(api_Verbosity_filter(verbosity, NULL))), "\n"); + api_Verbosity_level_set(verbosity, API_LEVEL_TRACE, NULL); + printf("verbosity %s ", api_Verbosity_level_get(verbosity, NULL) == API_LEVEL_TRACE ? "true" : "false"); + print("", api_spell(api_Verbosity_filter(verbosity, NULL)), " "); + print("", api_spell(api_louder(api_Verbosity_filter(verbosity, NULL))), " "); print("", api_spell(api_louder((api_option_Level){.some = false})), "\n"); api_Verbosity_free(verbosity); /* A returned `Vec<&Token>` holds clones, which its free drops. */ api_Token *twice = api_Token_new(9); uint32_t before = api_dropped(); - api_vec_Token tokens = api_Token_twice(twice); - if (tokens.len != 2 || api_Token_id_get(tokens.ptr[1]) != 9) { + api_vec_Token tokens = api_Token_twice(twice, NULL); + if (tokens.len != 2 || api_Token_id_get(tokens.ptr[1], NULL) != 9) { return 1; } api_vec_Token_free(tokens); @@ -1976,6 +2001,192 @@ static int script(void) { } /* Each breaks a contract, which must abort with a message naming the function. */ +/* A counter a closure was lent, which it fails to move out, then reads as it was. */ +static bool move_lent_status(void *data, api_Counter *counter) { + api_status *status = data; + api_Counter_into_label(counter, (api_option_Point){0}, status); + return *status == API_STATUS_LENT && api_Counter_get(counter, NULL) == 0; +} + +/* Each check a call fails writes its status through a non-null one, and changes nothing. */ +static int statuses(void) { + int failed = 0; + api_status status = 0; + api_Channel *channel = api_Channel_new(); + api_Channel_send(channel, str("x"), NULL); + api_Channel_recv_future *recv = api_Channel_recv(channel, NULL); + api_Channel_clear(channel, &status); + if (status != API_STATUS_BORROWED || strcmp(api_status_message(status), "a handle is already borrowed") != 0) { + fprintf(stderr, "a clear during a recv wrote %d\n", status); + failed = 1; + } + /* The recv still has what was sent, and once it ends the clear passes. */ + api_waker waker = {0}; + api_buf payload; + if (api_Channel_recv_poll(recv, waker, &payload) != API_POLL_READY || payload.len != 1) { + fprintf(stderr, "a failed clear changed the channel\n"); + failed = 1; + } + api_buf_free(payload); + api_Channel_recv_future_free(recv); + status = 0; + api_Channel_clear(channel, &status); + if (status != 0) { + fprintf(stderr, "a clear after the recv wrote %d\n", status); + failed = 1; + } + api_Channel_free(channel); + + /* A drop under a running call is refused; once it ends the drop takes the value, which + * a later call then finds moved, and free frees only the handle. */ + channel = api_Channel_new(); + api_Channel_send(channel, str("y"), NULL); + recv = api_Channel_recv(channel, NULL); + status = 0; + api_Channel_drop(channel, &status); + if (status != API_STATUS_BORROWED) { + fprintf(stderr, "a drop during a recv wrote %d\n", status); + failed = 1; + } + if (api_Channel_recv_poll(recv, waker, &payload) != API_POLL_READY || payload.len != 1) { + fprintf(stderr, "a refused drop changed the channel\n"); + failed = 1; + } + api_buf_free(payload); + api_Channel_recv_future_free(recv); + status = 0; + api_Channel_drop(channel, &status); + api_Channel_drop(channel, &status); + if (status != 0) { + fprintf(stderr, "a drop after the recv wrote %d\n", status); + failed = 1; + } + api_Channel_clear(channel, &status); + if (status != API_STATUS_MOVED) { + fprintf(stderr, "a clear after a drop wrote %d\n", status); + failed = 1; + } + api_Channel_free(channel); + + status = 0; + api_Meters *meters = api_Meters_new(); + api_Meter *first = api_Meters_first(meters, NULL); + api_Meters_clear(meters, NULL); + if (api_Meter_count(first, &status) != 0 || status != API_STATUS_OUTLIVED) { + fprintf(stderr, "a meter that outlived its meters wrote %d\n", status); + failed = 1; + } + api_Meter_free(first); + api_Meters_free(meters); + + /* A counter moved in beside a dropped mark, alone or held in a record or an enum, is + * refused before the move, so the counter is still there; once every mark is live, + * the call moves it. */ + api_Counter *admitted = api_Counter_new(str("a"), 5); + api_Mark *mark = api_Mark_of(3); + api_Mark *dropped = api_Mark_of(4); + api_Mark_drop(dropped, NULL); + api_Choice picked = {.tag = API_CHOICE_PICKED, .picked = {._0 = mark}}; + api_Choice empty = {.tag = API_CHOICE_EMPTY}; + api_tuple_Counter_Mark_Seat_Choice entries[] = { + {admitted, dropped, {mark, 1}, empty}, + {admitted, mark, {dropped, 1}, empty}, + {admitted, mark, {mark, 1}, {.tag = API_CHOICE_PICKED, .picked = {._0 = dropped}}}, + }; + for (size_t i = 0; i < sizeof(entries) / sizeof(entries[0]); i++) { + status = 0; + api_admit(entries[i], &status); + uint64_t still = api_Counter_get(admitted, NULL); + if (status != API_STATUS_MOVED || still != 5) { + fprintf(stderr, "admitting beside a dropped mark %zu wrote %d, leaving %llu\n", i, status, (unsigned long long)still); + failed = 1; + } + } + status = 0; + uint64_t sum = api_admit((api_tuple_Counter_Mark_Seat_Choice){admitted, mark, {mark, 1}, picked}, &status); + if (status != 0 || sum != 14) { + fprintf(stderr, "admitting wrote %d, summing %llu\n", status, (unsigned long long)sum); + failed = 1; + } + api_Counter_get(admitted, &status); + if (status != API_STATUS_MOVED) { + fprintf(stderr, "a counter admitted wrote %d\n", status); + failed = 1; + } + /* A record's method lifts the handle its receiver holds, failing as a call does. */ + status = 0; + api_Seat seat = {dropped, 2}; + api_Seat_count(&seat, &status); + if (status != API_STATUS_MOVED) { + fprintf(stderr, "a seat's count with a dropped mark wrote %d\n", status); + failed = 1; + } + /* A value's `&mut self` method refused over a dropped mark leaves the value as it was. */ + status = 0; + api_Score refused = api_Score_of(2); + api_Score_note(&refused, dropped, &status); + if (status != API_STATUS_MOVED || api_Score_count(&refused) != 2) { + fprintf(stderr, "a score noting a dropped mark wrote %d, leaving %llu\n", status, (unsigned long long)api_Score_count(&refused)); + failed = 1; + } + status = 0; + if (api_choose(empty, &status) != 0 || api_choose(picked, &status) != 3 || status != 0) { + fprintf(stderr, "choosing wrote %d\n", status); + failed = 1; + } + api_Counter_free(admitted); + api_Mark_free(mark); + api_Mark_free(dropped); + + /* A refused call frees each buffer it was given: beside the failing handle, in a record + * lent, and beside a move, which it leaves undone. Each case gives three. */ + api_Mark *live = api_Mark_of(2); + api_Mark *gone = api_Mark_of(2); + api_Mark_drop(gone, NULL); + api_Counter *weight = api_Counter_new(str("w"), 1); + api_Counter *dropped_weight = api_Counter_new(str("d"), 1); + api_Counter_drop(dropped_weight, NULL); + struct { + api_Mark *marked, *lent; + api_Counter *moved; + } hefts[] = {{gone, live, weight}, {live, gone, weight}, {live, live, dropped_weight}}; + for (size_t i = 0; i < sizeof(hefts) / sizeof(hefts[0]); i++) { + uint32_t before = api_dropped(); + status = 0; + api_heft((api_tuple_Mark_bytes){hefts[i].marked, api_token_bytes(1, 2)}, + (api_Parcel){.mark = hefts[i].lent, .payload = api_token_bytes(2, 3)}, + (api_tuple_Counter_bytes){hefts[i].moved, api_token_bytes(3, 4)}, &status); + if (status != API_STATUS_MOVED || api_dropped() != before + 3 || api_Counter_get(weight, NULL) != 1) { + fprintf(stderr, "a refused heft %zu wrote %d, freeing %u buffers\n", i, status, api_dropped() - before); + failed = 1; + } + } + uint32_t before_heft = api_dropped(); + status = 0; + uint64_t heft = api_heft((api_tuple_Mark_bytes){live, api_token_bytes(1, 2)}, + (api_Parcel){.mark = live, .payload = api_token_bytes(2, 3)}, + (api_tuple_Counter_bytes){weight, api_token_bytes(3, 4)}, &status); + if (status != 0 || heft != 14 || api_dropped() != before_heft + 3) { + fprintf(stderr, "a heft wrote %d, weighing %llu\n", status, (unsigned long long)heft); + failed = 1; + } + api_Counter_free(dropped_weight); + api_Counter_free(weight); + api_Mark_free(gone); + api_Mark_free(live); + + status = 0; + api_Counter *kept = api_Counter_new(str("c"), 0); + api_Registry *registry = api_Registry_new(kept, NULL); + api_Counter_free(kept); + if (!api_Registry_edit(registry, move_lent_status, &status, NULL)) { + fprintf(stderr, "a lent counter moved out wrote %d\n", status); + failed = 1; + } + api_Registry_free(registry); + return failed; +} + static int broken(const char *which) { #ifdef __linux__ /* Non-dumpable, the expected abort writes no core and never starts the host's crash @@ -1985,23 +2196,26 @@ static int broken(const char *which) { return 1; } #endif + if (strcmp(which, "statuses") == 0) { + return statuses(); + } if (strcmp(which, "moved") == 0) { api_Counter *c = api_Counter_new(str("c"), 0); - api_Label label = api_Counter_into_label(c, (api_option_Point){0}); + api_Label label = api_Counter_into_label(c, (api_option_Point){0}, NULL); api_Label_free(&label); - api_Counter_get(c); + api_Counter_get(c, NULL); } else if (strcmp(which, "borrowed") == 0) { api_Channel *channel = api_Channel_new(); - api_Channel_recv_future *recv = api_Channel_recv(channel); + api_Channel_recv_future *recv = api_Channel_recv(channel, NULL); (void)recv; - api_Channel_clear(channel); + api_Channel_clear(channel, NULL); } else if (strcmp(which, "utf8") == 0) { api_buf bad = {.ptr = (const uint8_t *)"\xff", .len = 1}; api_greet(bad, API_SHAPE_CIRCLE); } else if (strcmp(which, "finished") == 0) { api_Channel *channel = api_Channel_new(); - api_Channel_send(channel, str("x")); - api_Channel_recv_future *recv = api_Channel_recv(channel); + api_Channel_send(channel, str("x"), NULL); + api_Channel_recv_future *recv = api_Channel_recv(channel, NULL); api_waker waker = {0}; api_buf payload; api_Channel_recv_poll(recv, waker, &payload); @@ -2014,19 +2228,19 @@ static int broken(const char *which) { } else if (strcmp(which, "enum") == 0) { api_greet(str("x"), (api_Shape)2); } else if (strcmp(which, "lent") == 0 || strcmp(which, "lent moved") == 0) { - api_Registry *registry = api_Registry_new(api_Counter_new(str("c"), 0)); - api_Registry_edit(registry, strcmp(which, "lent") == 0 ? free_lent : move_lent, NULL); + api_Registry *registry = api_Registry_new(api_Counter_new(str("c"), 0), NULL); + api_Registry_edit(registry, strcmp(which, "lent") == 0 ? free_lent : move_lent, NULL, NULL); } else if (strcmp(which, "outlived") == 0) { api_Meters *meters = api_Meters_new(); - api_Meter *first = api_Meters_first(meters); - api_Meters_clear(meters); - api_Meter_count(first); + api_Meter *first = api_Meters_first(meters, NULL); + api_Meters_clear(meters, NULL); + api_Meter_count(first, NULL); } else if (strcmp(which, "lent shared") == 0) { api_Meters *meters = api_Meters_new(); - api_Meter_reset(api_Meters_first(meters)); + api_Meter_reset(api_Meters_first(meters, NULL), NULL); } else if (strcmp(which, "null") == 0) { - api_Registry *registry = api_Registry_new(api_Counter_new(str("c"), 0)); - api_Registry_edit(registry, NULL, NULL); + api_Registry *registry = api_Registry_new(api_Counter_new(str("c"), 0), NULL); + api_Registry_edit(registry, NULL, NULL, NULL); } else if (strcmp(which, "returned") == 0) { api_made(1, made_view, NULL); } else if (strcmp(which, "reader") == 0) { diff --git a/ffi/tests/api.cpp b/ffi/tests/api.cpp index ea4fe9f91..9de9d5d5f 100644 --- a/ffi/tests/api.cpp +++ b/ffi/tests/api.cpp @@ -638,6 +638,16 @@ int script() { ull(extent_start.start_get()), ull(extent_start.end())); api::Weighted weighted{step, 4}; std::printf("weighted %llu %llu\n", ull(weighted.total()), ull(weighted.score.count() + step.count())); + // A checked mark beside bytes in a tuple and in a record lent, and bytes beside a move. + api::Mark mark = api::Mark::of(1); + mark.add(2); + api::Parcel parcel{mark.clone(), api::buf("abc")}; + std::printf("heft %llu\n", ull(api::heft(std::make_tuple(mark.clone(), api::view("ab")), parcel, + std::make_tuple(api::Counter::new_("w", 1), api::view("abcd"))))); + // A value's `&mut self` beside a checked handle. + api::Score noted = api::Score::of(2); + std::uint64_t noted_before = noted.note(api::Mark::of(5)); + std::printf("noted %llu %llu\n", ull(noted_before), ull(noted.count())); } std::uint8_t levels[] = {1, 2, 3, 4}; @@ -1296,6 +1306,12 @@ int script() { std::uint32_t held = api::reissue(api::Ticket::new_(7), [&kept](const api::Ticket &) { return kept; }).number(); std::printf("reissued %u %u, cloned %u\n", static_cast(given), static_cast(held), static_cast(api::Ticket::clones())); + // A record's traits borrow the checked handle it holds. + api::Mark nine = api::Mark::of(9); + api::Seat front = api::seat(nine, 1); + api::Seat back = api::seat(nine, 2); + print("seat ", front.debug(), ""); + std::printf(" %s %s\n", front == front ? "true" : "false", front == back ? "true" : "false"); } { // `Copy` objects with no C++ copy, beside bytes: a returned vector moves back in, diff --git a/ffi/tests/api.dart b/ffi/tests/api.dart index 2e0bc266a..aa39e9fa2 100644 --- a/ffi/tests/api.dart +++ b/ffi/tests/api.dart @@ -103,12 +103,25 @@ void throws(void Function() f, String what) { check(false, what); } -/// `f` throws the StateError of a closed `type`. +/// `f` throws the StateError of a closed `type`: Dart's own, for a value whose calls only +/// borrow it, or else Rust's check of a moved value, naming the function. void closed(void Function() f, String type, String what) { try { f(); } on StateError catch (e) { - check(e.message == '$type is closed', '$what: $e'); + final moved = e.message.startsWith('api_') && e.message.endsWith(": a handle's value was moved out"); + check(e.message == '$type is closed' || moved, '$what: $e'); + return; + } + check(false, what); +} + +/// `f` throws the StateError `message` of a close under a borrow. +void borrowed(void Function() f, String message, String what) { + try { + f(); + } on StateError catch (e) { + check(e.message == message, '$what: $e'); return; } check(false, what); @@ -394,6 +407,14 @@ Future script() async { print('extent $old ${extent.startGet} ${extent.end()} ${extentStart.startGet} ${extentStart.end()}'); final weighted = api.Weighted(score: increment, weight: 4); print('weighted ${weighted.total()} ${weighted.score.count() + increment.count()}'); + // A checked mark beside bytes in a tuple and in a record lent, and bytes beside a move. + final mark = api.Mark.of(1); + mark.add(2); + final parcel = api.Parcel(mark: mark, payload: bytes([97, 98, 99])); + print('heft ${api.heft((mark, bytes([97, 98])), parcel, (api.Counter('w', 1), bytes([97, 98, 99, 100])))}'); + // A value's `&mut self` beside a checked handle. + final (noted, notedBefore) = api.Score.of(2).note(api.Mark.of(5)); + print('noted $notedBefore ${noted.count()}'); print('kept ${api.emit(PrintLog(), bytes([1, 2, 3, 4]))}'); final logger = api.Logger(PrintLog()); @@ -677,7 +698,7 @@ Future script() async { print('chunks ${api.chunks(utf8.encode('abcde'), 2)}'); final counters = api.counters(['x', 'y']); print('counters ${counters.map((c) => '${c.name()} ${c.get()}').join(', ')}'); - // Each counter moves in, so Dart closes it. + // Each counter moves in, so a later call throws. print('worth ${api.worth([counters[0]], [counters[1]])} ${api.worth(null, [])}'); for (final counter in counters) { closed(counter.get, 'Counter', 'a counter moved in is closed'); @@ -785,6 +806,11 @@ Future script() async { final renewed = api.reissue(api.Ticket(7), (t) => api.Ticket(t.number() + 1)).number(); final copied = api.reissue(api.Ticket(7), (_) => nine).number(); print('reissued $renewed $copied, cloned ${api.Ticket.clones()}'); + // A record's traits borrow the checked handle it holds. + final nines = api.Mark.of(9); + final front = api.seat(nines, 1); + final back = api.seat(nines, 2); + print('seat ${front.debug()} ${front == api.seat(nines, 1)} ${front == back}'); // A kept closure, called on another thread after the call that took it returned. final spawned = api.Spawned(() => 7); print('spawned ${spawned.join()}'); @@ -1017,16 +1043,21 @@ Future futures() async { done.cancel(); check(await done == 2, 'a finished future cancelled keeps its value'); - // A handle closed while a call awaits on it stays alive until the call ends. + // Closing a handle a call awaiting on it borrows throws, as Rust refuses a drop under a + // borrow, and changes nothing: the call ends as it would, and then the close passes. final other = api.Channel(); final held = other.recv(); final sender = other.clone(); - other.close(); + try { + other.close(); + check(false, 'a close under an awaiting call throws'); + } on StateError catch (e) { + check(e.message == 'api_Channel_drop: a handle is already borrowed', 'a close under an awaiting call: $e'); + } sender.send(utf8.encode('held')); - check( - text(await within(held, 'a receive on a closed handle')) == 'held', - 'a closed handle stays alive in the call awaiting on it', - ); + check(text(await within(held, 'a receive the close left')) == 'held', 'the awaiting call ends as it would'); + other.close(); + closed(other.clear, 'Channel', 'a call after the close'); sender.close(); // A future holding an implementation drops it as it finishes, closing it. @@ -1163,6 +1194,12 @@ void targets() { lacks(() => api.wasmOnly(), 'api_wasm_only'); lacks(() => api.pointerBits(const api.PlatformWasm(bits: 32)), 'api_Platform.Wasm'); lacks(() => api.isFamily(api.Family.wasm), 'api_Family.Wasm'); + lacks(() => api.Page.default_(), 'api_Page_default'); + lacks(() => const api.Page(size: 1).debug(), 'api_Page_debug'); + lacks(() => const api.Page(size: 1) == const api.Page(size: 2), 'api_Page_eq'); + final booth = api.Booth(mark: api.Mark.of(1)); + lacks(() => booth.debug(), 'api_Booth_debug'); + lacks(() => booth == api.Booth(mark: booth.mark), 'api_Booth_eq'); } @pragma('vm:never-inline') @@ -1204,8 +1241,9 @@ void closedObjects() { closed(() => api.Board([('open', open), ('closed', c)]), 'Counter', 'a closed object in a tuple'); check(open.get() == 3, 'a call that throws moves none of its other arguments'); - // Nor gives Rust an implementation beside an argument that throws, which would keep it - // unclosed and force each later call through a thread of its own. + // Nor keeps an implementation beside an argument that throws, which would keep it unclosed + // and force each later call through a thread of its own: one Dart refuses Rust never + // takes, and one Rust's check refuses Rust drops, as it takes what it was given first. check(api.onFirstThread(), 'a call while Rust holds no implementation'); final spared = CountLog(); throws(() => api.ArrayU8x4([1, 2, 3]), '3 bytes are no [u8; 4]'); @@ -1213,12 +1251,12 @@ void closedObjects() { throws(() => api.logCounts(spared, 256, open, []), 'an integer out of range after an implementation'); closed(() => api.logCounts(spared, 0, c, []), 'Counter', 'a closed object after an implementation'); closed(() => api.logCounts(spared, 0, open, [c]), 'Counter', 'a closed object in a list after an implementation'); - check(spared.closes == 0, 'a call that throws gives Rust no implementation'); + check(spared.closes == 2, 'a call whose check throws drops the implementation it gave Rust'); check(api.onFirstThread(), "a call after one that threw runs on the isolate's thread"); lacks(() => api.wasmLog(spared), 'api_wasm_log'); check(api.onFirstThread(), "a call after one this target lacks runs on the isolate's thread"); check(api.logCounts(spared, 0, open, []) == 1, 'an implementation a call that threw gave none of'); - check(spared.lines == 1 && spared.closes == 1, 'an implementation given once is closed once'); + check(spared.lines == 1 && spared.closes == 3, 'an implementation given once is closed once'); // An async call. final channel = api.Channel(); @@ -1240,26 +1278,27 @@ void closedObjects() { }); closed(kept.get, 'Counter', 'a lent object kept past its closure'); - // Closed while a call holds it, from the closure the call lends, the object stays alive - // until the call returns, and is freed then. + // Closed from the closure a call lends while the call borrows it, an object throws, as + // Rust refuses a drop under a borrow, and changes nothing; closed after, it closes. final editing = api.Registry(api.Counter('e', 4)); final edited = editing.edit((counter) { - editing.close(); - closed(() => editing.firstGet, 'Registry', 'an object closed in a closure refuses calls'); + borrowed(editing.close, 'api_Registry_drop: a handle is already borrowed', 'a close in a closure the call lends'); return counter.get() == 4; }); - check(edited, 'the call holding a closed object still reaches it'); - closed(() => editing.firstGet, 'Registry', 'an object closed in a closure stays closed'); - // As the drop of what it holds shows: once, after the call. + check(edited, 'the call whose object a close failed on runs on'); + editing.close(); + closed(() => editing.firstGet, 'Registry', 'an object closed after the call'); + // The same for one Dart counts, as the drop of what it holds shows: once, at the close. late api.Logger logger; late CountLog log; log = CountLog(() { - logger.close(); - check(log.closes == 0, 'an object closed during a call it is held by outlives the call'); + borrowed(logger.close, 'Logger: a handle is already borrowed', 'a close during a call it is held by'); }); logger = api.Logger(log); - check(logger.write('x'), 'the call holding a closed object returns'); - check(log.closes == 1, 'an object closed during a call it is held by is freed as it returns'); + check(logger.write('x'), 'the call whose object a close failed on returns'); + check(log.closes == 0, 'a close that failed frees nothing'); + logger.close(); + check(log.closes == 1, 'a close after the call frees it'); logger.close(); check(log.closes == 1, 'closing it again frees nothing'); print('closed ok'); diff --git a/ffi/tests/api.go b/ffi/tests/api.go index edb792607..847e32f34 100644 --- a/ffi/tests/api.go +++ b/ffi/tests/api.go @@ -17,6 +17,7 @@ import ( "net/netip" "os" "runtime" + "slices" "strings" "sync" "sync/atomic" @@ -491,6 +492,23 @@ func script() { fmt.Printf("extent %d %d %d %d %d\n", old, extent.StartGet(), extent.End(), extentStart.StartGet(), extentStart.End()) weighted := api.Weighted{Score: step, Weight: 4} fmt.Printf("weighted %d %d\n", weighted.Total(), weighted.Score.Count()+step.Count()) + // A checked mark beside bytes in a tuple and in a record lent, and bytes beside a move. + mark := api.MarkOf(1) + ok(mark.Add(2)) + parcel := api.Parcel{Mark: mark, Payload: rt.NewBytes([]byte("abc"))} + hefted := api.NewCounter("w", 1) + fmt.Printf("heft %d\n", must(api.Heft(rt.Tuple2[*api.Mark, rt.Bytes]{V0: mark, V1: rt.NewBytes([]byte("ab"))}, parcel, rt.Tuple2[*api.Counter, rt.Bytes]{V0: hefted, V1: rt.NewBytes([]byte("abcd"))}))) + hefted.Close() + // A value's `&mut self` beside a checked handle. + noted := api.ScoreOf(2) + notedBy := api.MarkOf(5) + notedBefore := must(noted.Note(notedBy)) + fmt.Printf("noted %d %d\n", notedBefore, noted.Count()) + notedBy.Close() + // Refused over a closed mark, the call writes nothing back. + _, refused := noted.Note(notedBy) + check(refused != nil && noted.Count() == 7, "a refused note leaves the score as it was") + mark.Close() // Rust frees the implementation once it drops it, releasing Go's. fmt.Printf("kept %d\n", api.Emit(printLog{}, rt.NewBytes([]byte{1, 2, 3, 4}))) @@ -688,9 +706,9 @@ func script() { return escaped(*p) }), ",", ""), "[]")) // A path that is no UTF-8 is a Go string all the same. - check(api.Stray() == "\xff", "a path that is not UTF-8 crosses as its bytes") + check(must(api.Stray()) == "\xff", "a path that is not UTF-8 crosses as its bytes") var lent string - api.VisitStray(func(path string) { lent = path }) + ok(api.VisitStray(func(path string) { lent = path })) check(lent == "\xff", "a path Rust lends that is not UTF-8 crosses as its bytes") // An error Rust returns only boxed is its type. _, err = api.Refuse("why") @@ -918,6 +936,26 @@ func script() { renewed := api.Reissue(seven, func(t api.Ticket) api.Ticket { return api.NewTicket(t.Number() + 1) }) copied := api.Reissue(seven, func(api.Ticket) api.Ticket { return nine }) fmt.Printf("reissued %d %d, cloned %d\n", renewed.Number(), copied.Number(), api.TicketClones()) + // A record's traits borrow the checked handle it holds. + nines := api.MarkOf(9) + front, back := must(api.NewSeat(nines, 1)), must(api.NewSeat(nines, 2)) + fmt.Printf("seat %s %t %t\n", front.GoString(), must(front.Equal(front)), must(front.Equal(back))) + // The packages serve wasm32 too: the traits of a type only wasm32 has fail on the host's + // library as its functions do. + { + _, err := api.PageDefault() + check(errors.Is(err, errors.ErrUnsupported), "a Default off the target is unsupported") + _, err = api.Page{Size: 1}.Equal(api.Page{Size: 1}) + check(errors.Is(err, errors.ErrUnsupported), "an Equal off the target is unsupported") + booth := api.Booth{Mark: nines} + _, err = booth.Equal(booth) + check(errors.Is(err, errors.ErrUnsupported), "a checked Equal off the target is unsupported") + check( + booth.GoString() == "api_Booth_debug: it is not on this library's target", + "a GoString off the target reads as its error", + ) + } + check(nines.Close() == nil, "a mark closes") // A kept closure, called on another thread after the call that took it returned. spawned := api.NewSpawned(func() uint32 { return 7 }) fmt.Printf("spawned %d\n", must(spawned.Join())) @@ -1177,7 +1215,8 @@ func futures() { check(errors.Is(err, context.DeadlineExceeded), "a receive past its deadline returns its error") } - // A handle closed while a call waits on it stays alive until the call ends. + // Closing a handle a call waiting on it borrows fails, as Rust refuses a drop under a + // borrow, and changes nothing: the call ends as it would, and then Close passes. { other := api.NewChannel() watching := watch(ctx) @@ -1185,9 +1224,13 @@ func futures() { go func() { got <- string(must(other.Recv(watching)).Copy()) }() <-watching.waiting sender := must(other.Clone()) - other.Close() + err := other.Close() + check(errors.Is(err, rt.ErrBorrowed) && err.Error() == "api_Channel_drop: a handle is already borrowed", "Close under a waiting call fails") sender.Send(rt.NewBytes([]byte("held"))) - check(<-got == "held", "a closed handle stays alive in the call waiting on it") + check(<-got == "held", "the waiting call ends as it would") + check(other.Close() == nil, "Close passes once no call borrows it") + _, err = other.Recv(ctx) + check(errors.Is(err, rt.ErrClosed) && err.Error() == "Channel is closed", "a call after Close fails") sender.Close() } @@ -1407,30 +1450,28 @@ func closed() { f() return nil } - isClosed := func(err error, name, what string) { - check(errors.Is(err, rt.ErrClosed) && err.Error() == name+" is closed", what) + isClosed := func(err error, want, what string) { + check(errors.Is(err, rt.ErrClosed) && err.Error() == want, fmt.Sprintf("%s: %v", what, err)) } - // A method, a trait's, and a field's accessors. c := api.NewCounter("c", 1) - c.Close() - c.Close() + check(c.Close() == nil && c.Close() == nil, "Close, then again, passes") _, err := c.Get() - isClosed(err, "Counter", "a method of a closed object") + isClosed(err, "Counter is closed", "a method of a closed object") _, err = c.Clone() - isClosed(err, "Counter", "Clone of a closed object") + isClosed(err, "Counter is closed", "Clone of a closed object") check(c.GoString() == "Counter is closed", "GoString of a closed object is the error") tally := api.NewTally() tally.Close() _, err = api.NewTally().Equal(tally) - isClosed(err, "Tally", "Equal with a closed object") + isClosed(err, "Tally is closed", "Equal with a closed object") registry := must(api.NewRegistry(api.NewCounter("first", 1))) registry.Close() _, err = registry.FirstGet() - isClosed(err, "Registry", "a field's getter of a closed object") + isClosed(err, "Registry is closed", "a field's getter of a closed object") // A call that fails moves nothing in. second := api.NewCounter("second", 2) - isClosed(registry.FirstSet(second), "Registry", "a field's setter of a closed object") + isClosed(registry.FirstSet(second), "Registry is closed", "a field's setter of a closed object") check(must(second.Get()) == 2, "a failed call leaves what it would move") // A record's method, whose receiver holds a value only Rust makes: Go's zero value of @@ -1441,40 +1482,58 @@ func closed() { // An argument, alone, or nested where the call holds it through its arena. _, err = api.BumpBoth(api.NewTally(), tally) - isClosed(err, "Tally", "a closed argument") + isClosed(err, "Tally is closed", "a closed argument") open := api.NewCounter("open", 3) _, err = api.Worth(&[]*api.Counter{open, c}, nil) - isClosed(err, "Counter", "a closed object in a list") + isClosed(err, "Counter is closed", "a closed object in a list") check(must(open.Get()) == 3, "a failed call leaves the rest of a list") // An async call. channel := api.NewChannel() channel.Close() _, err = channel.Recv(ctx) - isClosed(err, "Channel", "an async call of a closed object") - - // Closed by a call that moved it, plain or boxed, or once the callback it was lent to - // returned. - moved := api.NewCounter("m", 0) - must(moved.IntoLabel(nil)) - _, err = moved.Get() - isClosed(err, "Counter", "a method of a moved object") + isClosed(err, "Channel is closed", "an async call of a closed object") + + // Moved by a call, plain or boxed, or closed once the callback it was lent to returned. + taken := api.NewCounter("m", 0) + must(taken.IntoLabel(nil)) + _, err = taken.Get() + isClosed(err, "Counter is closed", "a method of a moved object") tail := must(api.NewNode(1, nil)) must(api.NewNode(2, tail)) _, err = tail.Sum() - isClosed(err, "Node", "a method of a boxed moved object") + isClosed(err, "Node is closed", "a method of a boxed moved object") var kept *api.Counter must(must(api.NewRegistry(api.NewCounter("c", 0))).Edit(func(counter *api.Counter) bool { kept = counter return false })) _, err = kept.Get() - isClosed(err, "Counter", "a method of a lent object kept past its callback") - - // Close racing calls on other goroutines: each call either runs, holding the handle - // so Rust's value outlives it, or returns ErrClosed; Rust's is dropped once. + isClosed(err, "Counter is closed", "a method of a lent object kept past its callback") + + // Close racing calls on other goroutines, of a value Go counts and of one Rust checks. + latch := api.LatchDefault() + must(latch.Set(5)) + race(latch, func() (bool, error) { + v, err := latch.Get() + return err == nil && *v == 5, err + }, []string{"Latch is closed"}, "Latch: a handle is already borrowed") before := api.Dropped() token := api.NewToken(5) + race(token, func() (bool, error) { + id, err := token.IdGet() + return id == 5, err + }, []string{"Token is closed", "api_Token_id_get: a handle's value was moved out"}, "api_Token_drop: a handle is already borrowed") + check(api.Dropped() == before+1, "Close racing calls drops Rust's value once") + fmt.Println("closed ok") +} + +// race closes c while 8 goroutines call call on it: each call runs on the live value or, +// once c is closed, fails with one of closed, which for a value Rust checks includes its +// moved check, met by a call that held the handle before Close; a Close a running call +// beats fails with borrowed, as Rust refuses a drop under a borrow, and changes nothing, so +// the next tries again. +func race(c interface{ Close() error }, call func() (bool, error), closed []string, borrowed string) { var wg sync.WaitGroup var ran, failed atomic.Int64 for range 8 { @@ -1482,13 +1541,13 @@ func closed() { go func() { defer wg.Done() for { - id, err := token.IdGet() + live, err := call() if err != nil { - isClosed(err, "Token", "a call racing Close") + check(errors.Is(err, rt.ErrClosed) && slices.Contains(closed, err.Error()), fmt.Sprintf("a call racing Close: %v", err)) failed.Add(1) return } - check(id == 5, "a call racing Close reads the live value") + check(live, "a call racing Close reads the live value") ran.Add(1) } }() @@ -1496,10 +1555,16 @@ func closed() { for ran.Load() < 100 { runtime.Gosched() } - token.Close() + for { + err := c.Close() + if err == nil { + break + } + check(errors.Is(err, rt.ErrBorrowed) && err.Error() == borrowed, fmt.Sprintf("Close racing a call: %v", err)) + } wg.Wait() - check(failed.Load() == 8 && api.Dropped() == before+1, "Close racing calls drops Rust's value once") - fmt.Println("closed ok") + check(failed.Load() == 8, "every call after Close fails") + check(c.Close() == nil, "Close again passes") } // bytes checks that Bytes Rust returns read Rust's buffer in place, kept alive by any view @@ -1628,15 +1693,19 @@ func batched() []rt.Bytes { // broken breaks a contract, which must fail with a message naming it. func broken(which string) { switch which { + // A failed check returns an error, which the case raises once it checked its kind. case "outlived": meters := api.NewMeters() first := must(meters.First()) ok(meters.Clear()) - _ = must(first.Count()) + _, err := first.Count() + check(errors.Is(err, rt.ErrClosed), "a handle that outlived its lender is closed") + panic(err) case "lent moved": must(api.NewRegistry(api.NewCounter("c", 0))).Edit(func(counter *api.Counter) bool { - counter.IntoLabel(nil) - return false + _, err := counter.IntoLabel(nil) + check(errors.Is(err, rt.ErrBorrowed), "a lent handle moved out is borrowed") + panic(err) }) // An IPv6 address where Rust takes `SocketAddrV4`. case "family": @@ -1652,7 +1721,9 @@ func broken(which string) { api.Extended(math.MaxInt64, 1) // The packages serve wasm32 too, whose items the host's library lacks. case "target": - api.WasmOnly() + _, err := api.WasmOnly() + check(errors.Is(err, errors.ErrUnsupported), "an item off the target is unsupported") + panic(err) case "variant": api.PointerBits(api.PlatformWasm{Bits: 32}) case "plain variant": diff --git a/ffi/tests/api.kt b/ffi/tests/api.kt index 1f17ee99b..52e89859e 100644 --- a/ffi/tests/api.kt +++ b/ffi/tests/api.kt @@ -319,6 +319,14 @@ fun script() { println("extent $old ${extent.startGet} ${extent.end()} ${extentStart.startGet} ${extentStart.end()}") val weighted = Weighted(step, 4u) println("weighted ${weighted.total()} ${weighted.score.count() + step.count()}") + // A checked mark beside bytes in a tuple and in a record lent, and bytes beside a move. + val mark = Mark.of(1uL) + mark.add(2uL) + val parcel = Parcel(mark, Bytes("abc".encodeToByteArray())) + println("heft ${heft(Pair(mark, Bytes("ab".encodeToByteArray())), parcel, Pair(Counter.new("w", 1u), Bytes("abcd".encodeToByteArray())))}") + // A value's `&mut self` beside a checked handle. + val (noted, notedBefore) = Score.of(2uL).note(Mark.of(5uL)) + println("noted $notedBefore ${noted.count()}") println("kept ${emit(PrintLog(), Bytes(byteArrayOf(1, 2, 3, 4)))}") Logger.new(PrintLog()).use { logger -> println("write ${logger.write("hi")}") } @@ -690,6 +698,11 @@ fun script() { val given = reissue(Ticket.new(7u)) { t -> Ticket.new(t.number() + 1u) }.number() val held = reissue(Ticket.new(7u)) { nine }.number() println("reissued $given $held, cloned ${Ticket.clones()}") + // A record's traits borrow the checked handle it holds. + val nines = Mark.of(9uL) + val front = seat(nines, 1u) + val rear = seat(nines, 2u) + println("seat ${front.debug()} ${front == front.copy()} ${front == rear}") // A kept closure, called on another thread after the call that took it returned. println("spawned ${Spawned.new { 7u }.join()}") // A kept closure: called again at the instant it asks for, then once its waiter wakes. @@ -890,17 +903,22 @@ fun futures() { check(payload.text() == "kept", "a cancelled receive loses nothing") } - // A handle closed while a call awaits on it is freed once the call ends. + // Closing a handle a call awaiting on it borrows throws, as Rust refuses a drop under a + // borrow, and changes nothing: the call ends as it would, and then the close passes. run { val other = Channel.new() var got: String? = null scope.launch { got = other.recv().text() } q.drain() val sender = other.clone() - other.close() + val refused = runCatching { other.close() }.exceptionOrNull() + check(refused is IllegalStateException && refused.message == "api_Channel_drop: a handle is already borrowed", "a close under an awaiting call throws: $refused") sender.send(Bytes("held".encodeToByteArray())) q.drain() - check(got == "held", "a closed handle stays held by the call awaiting on it") + check(got == "held", "the awaiting call ends as it would") + other.close() + val moved = runCatching { other.clear() }.exceptionOrNull() + check(moved is IllegalStateException && moved.message == "Channel is closed", "a call after close throws: $moved") sender.close() } @@ -1175,6 +1193,9 @@ fun owned() { "api_wasm_only" to { wasmOnly() }, "api_Platform.Wasm" to { pointerBits(Platform.Wasm(32u)) }, "api_Family.Wasm" to { isFamily(Family.WASM) }, + "api_Page_default" to { Page.default() }, + "api_Page_debug" to { Page(1u).debug() }, + "api_Booth_debug" to { Booth(Mark.of(1uL)).debug() }, )) { val thrown = runCatching(call).exceptionOrNull() check(thrown is UnsupportedOperationException && thrown.message == "$what: it is not on this library's target", "$what throws: $thrown") @@ -1183,6 +1204,13 @@ fun owned() { check(runCatching { wasmTake(kept) }.exceptionOrNull() is UnsupportedOperationException, "api_wasm_take throws") check(kept.get() == 7uL, "a refused call leaves its moved argument open") } + // A failed check refuses the call before it moves anything, so the lent counter stays open. + Registry.new(Counter.new("r", 3u)).edit { lent -> + val refused = runCatching { returnMoved(lent, false) }.exceptionOrNull() + check(refused is IllegalStateException && refused.message == "api_return_moved: a handle a call lent is borrowed exclusively or moved out", "a lent counter refuses the move: $refused") + check(lent.get() == 3uL, "a call a check refused leaves its moved argument open") + false + } // Bytes outlive the object that returned them and the record that held them. val body = ByteArray(2048) { 'b'.code.toByte() } @@ -1281,30 +1309,18 @@ fun sliced(id: UInt): Bytes = tokenBytes(id, 1025u).slice(1015) // Frees racing calls: a close racing calls on other threads, and the GC freeing what a // call reads while it runs. fun raced() { - // Each call either runs, holding the handle so Rust's value outlives it, or throws. + // A latch's calls only borrow it, so Kotlin counts them; a token's Rust checks. var before = dropped() - val token = Token.new(5u) - val ran = java.util.concurrent.atomic.AtomicInteger() - val failed = java.util.concurrent.atomic.AtomicInteger() - val callers = List(8) { - thread { - while (true) { - val id = try { - token.idGet - } catch (e: IllegalStateException) { - check(e.message == "Token is closed", "a call racing close throws") - failed.incrementAndGet() - return@thread - } - check(id == 5u, "a call racing close reads the live value") - ran.incrementAndGet() - } - } - } - while (ran.get() < 100) Thread.yield() - token.close() - callers.forEach { it.join() } - check(failed.get() == 8 && dropped() == before + 1u, "close racing calls drops Rust's value once") + val latch = Latch.default() + latch.set(5u) + race(latch, { it.get() == 5u }, setOf("Latch is closed"), "Latch: a handle is already borrowed") + race( + Token.new(5u), + { it.idGet == 5u }, + setOf("Token is closed", "api_Token_id_get: a handle's value was moved out"), + "api_Token_drop: a handle is already borrowed", + ) + check(dropped() == before + 1u, "close racing calls drops Rust's value once") // Calls on values nothing else reaches, while another thread collects: each reads what // it was given, never a buffer freed under it and reused by the next, which holds @@ -1328,6 +1344,43 @@ fun raced() { collected(before + 3u * rounds, "the GC frees what each call raced") } +// Close [made] while 8 threads call [call] on it: each call runs on the live value or, once +// it is closed, throws one of [closed], which for a value Rust checks includes its moved +// check, met by a call that held the handle before close; a close a running call beats +// throws [borrowed], as Rust refuses a drop under a borrow, and changes nothing, so the next +// tries again. +fun race(made: T, call: (T) -> Boolean, closed: Set, borrowed: String) { + val ran = java.util.concurrent.atomic.AtomicInteger() + val failed = java.util.concurrent.atomic.AtomicInteger() + val callers = List(8) { + thread { + while (true) { + val live = try { + call(made) + } catch (e: IllegalStateException) { + check(e.message in closed, "a call racing close throws: ${e.message}") + failed.incrementAndGet() + return@thread + } + check(live, "a call racing close reads the live value") + ran.incrementAndGet() + } + } + } + while (ran.get() < 100) Thread.yield() + while (true) { + try { + made.close() + break + } catch (e: IllegalStateException) { + check(e.message == borrowed, "a close racing a call throws: ${e.message}") + } + } + callers.forEach { it.join() } + check(failed.get() == 8, "every call after close throws") + made.close() +} + // Calls on values nothing else reaches once it returns, holding [id], racing the GC. @OwnFrame fun racedRound(id: UInt) { diff --git a/ffi/tests/api.py b/ffi/tests/api.py index 480b97f3d..4b8cf167b 100644 --- a/ffi/tests/api.py +++ b/ffi/tests/api.py @@ -384,6 +384,13 @@ def edit(counter): print(f"extent {old} {extent.start_get} {extent.end()} {extent_start.start_get} {extent_start.end()}") weighted = api.Weighted(increment, 4) print(f"weighted {weighted.total()} {weighted.score.count() + increment.count()}") + # A checked mark beside bytes in a tuple and in a record lent, and bytes beside a move. + mark = api.Mark.of(1) + mark.add(2) + print(f"heft {api.heft((mark, b'ab'), api.Parcel(mark, b'abc'), (api.Counter.new('w', 1), b'abcd'))}") + # A value's `&mut self` beside a checked handle. + noted, noted_before = api.Score.of(2).note(api.Mark.of(5)) + print(f"noted {noted_before} {noted.count()}") print(f"kept {api.emit(PrintLog(), bytes([1, 2, 3, 4]))}") logger = api.Logger.new(PrintLog()) @@ -668,7 +675,7 @@ async def polls(): print(f"chunks {[list(data(chunk)) for chunk in api.chunks(b'abcde', 2)]}") counters = api.counters(["x", "y"]) print("counters " + ", ".join(f"{c.name()} {c.get()}" for c in counters)) - # Each counter moves in, so Python closes it. + # Each counter moves in, so a later call raises. print(f"worth {api.worth([counters[0]], [counters[1]])} {api.worth(None, [])}") for counter in counters: try: @@ -762,6 +769,10 @@ async def polls(): given = api.reissue(api.Ticket.new(7), lambda t: api.Ticket.new(t.number() + 1)).number() held = api.reissue(api.Ticket.new(7), lambda _: kept).number() print(f"reissued {given} {held}, cloned {api.Ticket.clones()}") + # A record's traits borrow the checked handle it holds. + nines = api.Mark.of(9) + front, back = api.seat(nines, 1), api.seat(nines, 2) + print(f"seat {front!r} {str(front == api.seat(nines, 1)).lower()} {str(front == back).lower()}") # A kept closure, called on another thread after the call that took it returned. print(f"spawned {api.Spawned.new(lambda: 7).join()}") # A kept closure: called again at the instant it asks for, then once its waiter wakes. @@ -1418,6 +1429,9 @@ def raises(error, f, what): (api.wasm_only, "api_wasm_only"), (lambda: api.pointer_bits(api.Platform.Wasm(32)), "api_Platform.Wasm"), (lambda: api.is_family(api.Family.WASM), "api_Family.Wasm"), + (api.Page.default, "api_Page_default"), + (lambda: repr(api.Page(size=1)), "api_Page_debug"), + (lambda: repr(api.Booth(mark=api.Mark.of(1))), "api_Booth_debug"), ]: try: f() diff --git a/ffi/tests/api.swift b/ffi/tests/api.swift index 3f85f3a50..40ba489d0 100644 --- a/ffi/tests/api.swift +++ b/ffi/tests/api.swift @@ -24,6 +24,23 @@ final class PrintLog: Log { } } +/// A Log that counts its frees. +final class DropLog: Log { + let drops: Cell + + init(_ drops: Cell) { + self.drops = drops + } + + func log(_: UInt8, _: String) -> Bool { + true + } + + deinit { + drops.value += 1 + } +} + /// A Log that cancels a task at each line, and counts them. final class CancelLog: Log, @unchecked Sendable { let task: Cell?> @@ -56,6 +73,43 @@ func text(_ data: Bytes) -> String { String(decoding: data, as: UTF8.self) } +/// Check that `body` throws for the call `function` of a Refusal a move closed. +func closed(_ function: String, _ body: () throws -> Void) { + do { + try body() + check(false, "\(function) of a closed Refusal throws") + } catch let e as CheckError { + check(e.function == function && e.message == "Refusal is closed", "\(function) threw \(e)") + } catch { + check(false, "\(function) threw \(error)") + } +} + +/// Check that `body` throws the CheckError of `function`, which the library's target lacks. +func lacks(_ function: String, _ body: () throws -> Void) { + do { + try body() + check(false, "\(function) throws off its target") + } catch let e as CheckError { + check( + e.function == function && e.message == "it is not on this library's target", + "\(function) threw \(e)" + ) + } catch { + check(false, "\(function) threw \(error)") + } +} + +/// The file serves wasm32 too: what only wasm32 has throws on the host's library, wherever +/// Swift lets it. +func targets() { + lacks("api_wasm_only") { _ = try wasmOnly() } + lacks("api_Page_default") { _ = try Page.default() } + let booth = Booth(mark: Mark.of(1)) + lacks("api_Booth_debug") { _ = try booth.debugDescription } + lacks("api_Booth_eq") { _ = try booth == booth } +} + /// The refusal `refuse` throws for `reason`: a class, since Swift throws it. func refusal(_ reason: String) -> Refusal { do { @@ -192,8 +246,8 @@ func script() { print("default \(d.x) \(d.y)") var c = Counter.new("c", 5) - print("get \(c.get())") - print("name \(c.name())") + print("get \(try! c.get())") + print("name \(try! c.name())") print("add ok \(try! c.add(10))") do { _ = try c.add(2000) @@ -209,22 +263,22 @@ func script() { } catch { check(false, "sub throws an Error") } - c.rename("d") - print("name \(c.name())") - print("is d \(c.`is`("d"))") - var copy = c.clone() + try! c.rename("d") + print("name \(try! c.name())") + print("is d \(try! c.`is`("d"))") + var copy = try! c.clone() _ = try! copy.add(1) - print("clone \(copy.get()) original \(c.get())") - print("debug \(c.debugDescription)") - let label = c.intoLabel(p) + print("clone \(try! copy.get()) original \(try! c.get())") + print("debug \(try! c.debugDescription)") + let label = try! c.intoLabel(p) print("label \(debug(label))") print("note \(hasText(Note(text: ""))) \(hasText(Note(text: nil)))") // A tuple holding a handle is a struct of its elements. - let split = Counter.new("s", 7).split(5) - print("split \(split._0.name()) \(split._0.get()) \(split._1.name()) \(split._1.get())") - let one = only(Counter.new("o", 3)) - print("only \(one.name()) \(one.get())") + let split = try! Counter.new("s", 7).split(5) + print("split \(try! split._0.name()) \(try! split._0.get()) \(try! split._1.name()) \(try! split._1.get())") + let one = try! only(Counter.new("o", 3)) + print("only \(try! one.name()) \(try! one.get())") let (w8, w16, w32, w64) = widths(98305) print("widths \(w8) \(w16) \(w32) \(w64)") for b: UInt64 in [2, 0] { @@ -256,11 +310,11 @@ func script() { var ranks = UniqueArray() ranks.append(TupleStringCounter("low", Counter.new("l", 1))) ranks.append(TupleStringCounter("high", Counter.new("h", 9))) - let board = Board.new(ranks) - let ranked = board.ranked() - print("ranked \(listed(ranked) { "\($0._0) \($0._1.name()) \($0._1.get())" })") - let best = board.best() - print("best \(best.name()) \(best.get())") + let board = try! Board.new(ranks) + let ranked = try! board.ranked() + print("ranked \(listed(ranked) { "\($0._0) \(try! $0._1.name()) \(try! $0._1.get())" })") + let best = try! board.best() + print("best \(try! best.name()) \(try! best.get())") // A map crosses as its pairs and a set as its items, which Rust collects in order, each // key once, the last kept. let atlas = Atlas(points: [("b", Point(x: 1, y: 1)), ("ab", Point(x: 2, y: 3)), ("b", Point(x: 4, y: 4))], ids: [3, 1, 3]) @@ -294,40 +348,40 @@ func script() { print("title Some((\"\(title(true)!)\",)) \(title(false) == nil ? "None" : "Some") named Some((\"\(named("x")!)\",))") // Each lent counter is a new handle to a clone. - var registry = Registry.new(Counter.new("first", 1)) - print("last \(registry.last() != nil)") - registry.push(Counter.new("last", 2)) - var first = registry.firstGet + var registry = try! Registry.new(Counter.new("first", 1)) + print("last \(try! registry.last() != nil)") + try! registry.push(Counter.new("last", 2)) + var first = try! registry.firstGet() _ = try! first.add(10) - print("first \(registry.firstGet.get()) lent \(first.get())") - var last = registry.last()! + print("first \(try! registry.firstGet().get()) lent \(try! first.get())") + var last = try! registry.last()! _ = try! last.add(10) - print("last \(registry.last()!.get()) lent \(last.get())") + print("last \(try! registry.last()!.get()) lent \(try! last.get())") // A closure lent for the call edits the first counter through a handle. let by: UInt64 = 5 - let edited = registry.edit { counter in + let edited = try! registry.edit { counter in _ = try! counter.add(by) - return counter.edited() && counter.get() == 6 + return try! counter.edited() && counter.get() == 6 } - print("edit \(edited) \(registry.firstGet.get())") + print("edit \(edited) \(try! registry.firstGet().get())") // A setter moves an object in, as assignment does, closing its handle. - registry.firstSet(Counter.new("second", 3)) - print("set first \(registry.firstGet.get()) \(registry.firstGet.name())") - print("walk \(walk(3) { cursor in cursor.at() * 2 })") + try! registry.firstSet(Counter.new("second", 3)) + print("set first \(try! registry.firstGet().get()) \(try! registry.firstGet().name())") + print("walk \(walk(3) { cursor in try! cursor.at() * 2 })") print("each \(each(2) { at in print("visit \(at)") })") print("offset \(offset({ 7 }, 3))") print("shift \(shift({ 7 }, 4))") let name = Name.owned("a") - var joined = name.join(Name.owned("b")) - print("joined \(joined.asStr()) \(joined.debugDescription) \(joined == name)") - joined.rename("c") - print("renamed \(joined.asStr())") - let long = longer(joined, Name.owned("dd")) - print("longer \(long.asStr())") - print("unnamed [\(unnamed().asStr())]") + var joined = try! name.join(Name.owned("b")) + print("joined \(try! joined.asStr()) \(try! joined.debugDescription) \(try! joined == name)") + try! joined.rename("c") + print("renamed \(try! joined.asStr())") + let long = try! longer(joined, Name.owned("dd")) + print("longer \(try! long.asStr())") + print("unnamed [\(try! unnamed().asStr())]") let lit = Name.borrowed("lit") - print("borrowed \(lit.asStr()) \(lit.shares(Name.borrowed("lit"))) \(lit.shares(Name.borrowed("other")))") + print("borrowed \(try! lit.asStr()) \(try! lit.shares(Name.borrowed("lit"))) \(try! lit.shares(Name.borrowed("other")))") print("total \(total(RangeU64(start: 2, end: 5)))") print("reach \(reach(2, 5))") @@ -340,7 +394,7 @@ func script() { check(U128(high: .max, low: .max).description == "340282366920938463463374607431768211455", "a u128's decimal digits") let sumHex = { (sum: ArrayU8x4) in sum.map { Swift.String(format: "%02x", $0) }.joined() } var digest = Digest.new() - digest.sumSet(try! fingerprint("deadbeef")) + try! digest.sumSet(try! fingerprint("deadbeef")) var unparsed = "" do { _ = try fingerprint("zz") @@ -349,14 +403,14 @@ func script() { } catch { check(false, "fingerprint throws its message") } - print("digest \(sumHex(digest.sumGet)) \(sumHex(digest.flipped(ArrayU8x4([0xff, 0, 0xff, 0])!))) \(unparsed)") + print("digest \(sumHex(try! digest.sumGet)) \(sumHex(try! digest.flipped(ArrayU8x4([0xff, 0, 0xff, 0])!))) \(unparsed)") check(ArrayU8x4([1, 2, 3]) == nil, "3 bytes are no [u8; 4]") var batch = Batch.new(), batch2 = pair() - let accepted = (1...5).filter { batch.push($0) }.count - let paired = (1...3).filter { batch2.push($0) }.count - let emptied = drain4(&batch) - print("batch \(batch.capacity()) \(accepted) \(emptied) \(drain4(&batch)) \(batch2.capacity()) \(paired) \(drain2(&batch2))") - print("capacities \(capacities42(batch, batch2)) \(capacities22(batch2, batch2))") + let accepted = (1...5).filter { try! batch.push($0) }.count + let paired = (1...3).filter { try! batch2.push($0) }.count + let emptied = try! drain4(&batch) + print("batch \(try! batch.capacity()) \(accepted) \(emptied) \(try! drain4(&batch)) \(try! batch2.capacity()) \(paired) \(try! drain2(&batch2))") + print("capacities \(try! capacities42(batch, batch2)) \(try! capacities22(batch2, batch2))") let wide = widen(RangeU64(start: 2, end: 5), 1) print("widen \(wide.start) \(wide.end)") let counts = [ @@ -411,18 +465,18 @@ func script() { print("secs \(stamp.secs())") print("whole \(debug(stamp.whole()))") var message = Message.new(stamp, Bytes("body".utf8)) - print("payload \(text(message.payloadGet))") - print("stamp \(debug(message.stampGet!))") - print("sent \(message.sent())") + print("payload \(text(try! message.payloadGet))") + print("stamp \(debug(try! message.stampGet!))") + print("sent \(try! message.sent())") // Setters assign in place: the bytes are copied in, and a `Copy` handle copied. let reset = Stamp.fromMillis(4000) - message.stampSet(reset) - message.payloadSet(Bytes("edited".utf8)) - print("payload \(text(message.payloadGet))") - print("stamp \(debug(message.stampGet!))") + try! message.stampSet(reset) + try! message.payloadSet(Bytes("edited".utf8)) + print("payload \(text(try! message.payloadGet))") + print("stamp \(debug(try! message.stampGet!))") print("secs \(reset.secs())") - message.stampSet(nil) - print("stamp \(message.stampGet != nil)") + try! message.stampSet(nil) + print("stamp \(try! message.stampGet != nil)") // A value past a word, copied across: a closure takes and returns one by value. let moment = Moment.new(1500, 1000)! @@ -464,6 +518,15 @@ func script() { print("extent \(old) \(extent.startGet) \(extent.end()) \(extentStart.startGet) \(extentStart.end())") let weighted = Weighted(score: step, weight: 4) print("weighted \(weighted.total()) \(weighted.score.count() + step.count())") + // A checked mark beside bytes in a tuple and in a record lent, and bytes beside a move. + let mark = Mark.of(1) + try! mark.add(2) + let parcel = Parcel(mark: mark, payload: Bytes("abc".utf8)) + print("heft \(try! heft((mark, Bytes("ab".utf8)), parcel, TupleCounterBytes(Counter.new("w", 1), Bytes("abcd".utf8))))") + // A value's `&mut self` beside a checked handle. + var noted = Score.of(2) + let notedBefore = try! noted.note(Mark.of(5)) + print("noted \(notedBefore) \(noted.count())") // Bound first, so Swift's reference is gone before the print. let kept = emit(PrintLog(), Bytes([1, 2, 3, 4])) @@ -490,13 +553,13 @@ func script() { q.launch { got.value = try await channel.recv() } q.drain() print("recv pending \(got.value == nil)") - channel.send(Bytes("hi".utf8)) + try! channel.send(Bytes("hi".utf8)) wakes.value += q.size print("woken \(wakes.value)") q.drain() print("recv \(text(got.value!))") block { - channel.send(Bytes()) + try! channel.send(Bytes()) do { print("nonempty \(try await channel.recvNonempty().count)") } catch { @@ -512,43 +575,43 @@ func script() { print("tagged \(try await channel.tagged("tag"))") print("itemized \(try await channel.itemized([1, 2, 3]))") print("byte_len \(try await byteLen(Bytes("abc".utf8)))") - channel.send(Bytes("sized".utf8)) + try! channel.send(Bytes("sized".utf8)) let (payload, size) = try await channel.recvSized() print("sized \(text(payload)) \(size)") } // The waker moves into Rust, which keeps it until the send wakes it. - channel.notify { wakes.value += 1 } - channel.send(Bytes("rung".utf8)) + try! channel.notify { wakes.value += 1 } + try! channel.send(Bytes("rung".utf8)) print("woken \(wakes.value)") block { // A poll with no async twin is a future. print("take \(text(try await channel.pollTake()))") // A `&mut self` poll needs a binding of its own, which a closure cannot capture. var measured = Channel.new() - measured.send(Bytes("measured".utf8)) + try! measured.send(Bytes("measured".utf8)) print("measure \(try await measured.pollMeasure(2))") } // A context is made of the waker, which Rust keeps until the send wakes it. - channel.listen { wakes.value += 1 } - channel.send(Bytes("heard".utf8)) + try! channel.listen { wakes.value += 1 } + try! channel.send(Bytes("heard".utf8)) print("woken \(wakes.value)") // A returned waker wakes the waiter's task through Rust until released. do { let waiter = Waiter.new { wakes.value += 1 } - waiter.waker()() - waiter.context()() + try! waiter.waker()() + try! waiter.context()() } print("woken \(wakes.value)") - var list = Node.new(1, nil) - list.push(2) - print("node \(list.sum())") + var list = try! Node.new(1, nil) + try! list.push(2) + print("node \(try! list.sum())") let tally = Tally.new() - _ = tally.bump() - print("tally \(tally.bump()) \(tally == tally)") - print("both \(bumpBoth(tally, tally))") - print("peek \(peek(tally) { lent in lent.bump() })") + _ = try! tally.bump() + print("tally \(try! tally.bump()) \(try! tally == tally)") + print("both \(try! bumpBoth(tally, tally))") + print("peek \(try! peek(tally) { lent in try! lent.bump() })") let grown = grow(Area.new(Size(width: 2, height: 3)), 1) let size = grown.size() @@ -568,19 +631,19 @@ func script() { // At its default, named as the type itself. var stack = Stack.new() for (i, n) in ["a", "b"].enumerated() { - stack.push(Counter.new(n, UInt64(i) + 1)) + try! stack.push(Counter.new(n, UInt64(i) + 1)) } - let top = stack.pop()!.get() - print("stack \(stack.depth()) Some(\(top)) \(stack.pop() != nil) \(stack.pop() == nil)") + let top = try! stack.pop()!.get() + print("stack \(try! stack.depth()) Some(\(top)) \(try! stack.pop() != nil) \(try! stack.pop() == nil)") // Generic functions and methods, at each implementation of the trait bounding them, by // reference, which leaves Swift its handle. let fromPoint = Counter.seededPoint("p", Point(x: -3, y: 4)) let fromSize = Counter.seededSize("s", Size(width: 5, height: 6)) - let fromCounter = Counter.seededCounter("c", fromPoint) + let fromCounter = try! Counter.seededCounter("c", fromPoint) let fromPolar = Counter.seededPolarPoint("q", PolarPoint(turns: 1, distance: 2)) print( - "seeded \(fromPoint.get()) \(fromSize.get()) \(fromCounter.get()) \(fromPolar.get()) " - + "past \(stack.depthPastPoint(Point(x: 1, y: 2))) seeds \(seedsPointSize(Size(width: 2, height: 3)))") + "seeded \(try! fromPoint.get()) \(try! fromSize.get()) \(try! fromCounter.get()) \(try! fromPolar.get()) " + + "past \(try! stack.depthPastPoint(Point(x: 1, y: 2))) seeds \(seedsPointSize(Size(width: 2, height: 3)))") // A trait implemented for `Point` and `&Point` apart: one instance. print("weigh \(weighPoint(Point(x: -1, y: 2)))") @@ -619,24 +682,24 @@ func script() { // Its Drop runs when Swift releases the last reference, not before. do { let token = Token.new(7) - withExtendedLifetime(token) { print("token \(token.idGet) dropped \(dropped())") } + withExtendedLifetime(token) { print("token \(try! token.idGet) dropped \(dropped())") } } print("dropped \(dropped())") // A field and the method of its name, each its own member. var retry = Retry.new(5) - let limit = retry.limitGet - print("retry \(limit != nil ? 1 : 0) \(limit ?? 0) \(retry.limit()) \(retry.delay())") + let limit = try! retry.limitGet + print("retry \(limit != nil ? 1 : 0) \(limit ?? 0) \(try! retry.limit()) \(try! retry.delay())") // The renamed field's setter, named as its getter. - retry.limitSet(nil) - print("retry \(retry.limit())") + try! retry.limitSet(nil) + print("retry \(try! retry.limit())") print("slot \(SlotU8.new(200).value())") // Foreign types: a URL parsed from text, socket addresses, OS paths, and `Vec`s. let base = try! Url.parse("https://example.com/a/b") - print("url \(base.description) host \(host(base).map { "Some(\"\($0)\")" } ?? "None")") - let url = try! resolve(base.clone(), "c?d") - print("resolve \(url.description) \(url == base)") + print("url \(try! base.description) host \(try! host(base).map { "Some(\"\($0)\")" } ?? "None")") + let url = try! resolve(try! base.clone(), "c?d") + print("resolve \(try! url.description) \(try! url == base)") do { _ = try Url.parse("no scheme") } catch let e as StringError { @@ -666,23 +729,65 @@ func script() { let two = Counter.new("two", 2) var three: Counter? = Counter.new("three", 3) var none: Counter? = nil - print("plus \(two.plus(other: three!)) \(two.plus())") + print("plus \(try! two.plus(other: three!)) \(try! two.plus())") // A list and an option holding a struct beside a class, whose handle each moves. var pairs = UniqueArray() pairs.append(TupleCounterRefusal(Counter.new("a", 1), refusal("x"))) check( - reasons(pairs, TupleCounterRefusal(Counter.new("b", 2), refusal("y"))) + try! reasons(pairs, TupleCounterRefusal(Counter.new("b", 2), refusal("y"))) == "a refused: x, b refused: y", "reasons moves a struct and a class out of a list and an option" ) + // A class a move closed throws for each later call, as a receiver or nested. + let kept = refusal("z") + var moved = UniqueArray() + moved.append(TupleCounterRefusal(Counter.new("c", 3), kept)) + _ = try! reasons(moved, nil) + closed("api_Refusal_to_string") { _ = try kept.description } + var again = UniqueArray() + again.append(TupleCounterRefusal(Counter.new("d", 4), kept)) + do { + _ = try reasons(again, nil) + check(false, "api_reasons of a closed Refusal throws") + } catch let e as CheckError { + check(e.function == "api_reasons" && e.message == "Refusal is closed", "api_reasons threw \(e)") + } catch { + check(false, "api_reasons threw \(error)") + } + // A call that throws before Rust runs changes none of its arguments: it closes no + // class it would move, and writes back no value Rust would. + let live = refusal("live") + var mixed = UniqueArray() + mixed.append(TupleCounterRefusal(Counter.new("e", 5), live)) + mixed.append(TupleCounterRefusal(Counter.new("f", 6), kept)) + check((try? reasons(mixed, nil)) == nil, "api_reasons of a closed Refusal beside a live one throws") + check(try! live.description == "refused: live", "a refused call leaves the class it would move open") + // Nor does it keep a callback it gave before the throw. + let drops = Cell(0) + check((try? relay(DropLog(drops), [kept])) == nil, "api_relay of a closed Refusal throws") + check(drops.value == 1, "a call refused before Rust runs frees the callback it lowered") + // Nor the buffers it gave Rust before the throw, alone or beside the closed class. + check(try! excuse(tokenBytes(1, 1025), (tokenBytes(2, 1025), refusal("x"))) == 2051, "api_excuse sums its lengths") + let given = dropped() + do { + let note = tokenBytes(1, 1025) + let nested = tokenBytes(2, 1025) + check((try? excuse(note, (nested, kept))) == nil, "api_excuse of a closed Refusal throws") + } + check(dropped() == given + 2, "a call refused before Rust runs frees the buffers it gave") + var at = Point(x: 1, y: 2) + check((try? nudgeBy(&at, kept)) == nil, "api_nudge_by of a closed Refusal throws") + check(at == Point(x: 1, y: 2), "a refused call writes nothing back") + try! nudgeBy(&at, live) + check(at == Point(x: 5, y: 6), "a call writes back what Rust left") // Each optional borrowed parameter is an overload with and without its label. let one = Counter.new("one", 1) check( - two.clamped() == 2 && two.clamped(low: three!) == 3 && two.clamped(high: one) == 1 + try! two.clamped() == 2 && two.clamped(low: three!) == 3 && two.clamped(high: one) == 1 && two.clamped(low: one, high: three!) == 2, "clamped overloads each optional borrow" ) - print("add_to \(two.addTo(&three)) \(two.addTo(&none)) \(three!.get())") + print("add_to \(try! two.addTo(&three)) \(try! two.addTo(&none)) \(try! three!.get())") } do { var encoded = Bytes("0,".utf8) @@ -703,33 +808,33 @@ func script() { } do { let config = secure("h3") - print("secure \(alpnLent(config)) \(alpn(config.clone()))") + print("secure \(try! alpnLent(config)) \(try! alpn(config.clone()))") let cert = certify(Bytes("der".utf8)) - let certLength = certLen(cert) - let der = text(certDer(cert)) + let certLength = try! certLen(cert) + let der = text(try! certDer(cert)) var chain = UniqueArray() chain.append(certify(Bytes("a".utf8))) chain.append(certify(Bytes("bc".utf8))) - print("cert \(certLength) \(der) \(chainLen(chain))") + print("cert \(certLength) \(der) \(try! chainLen(chain))") // Another crate's trait objects: an `Arc`, taken back, and `'static` ones, held by value. - print("resolver \(resolved(resolver("r1")))") + print("resolver \(try! resolved(resolver("r1")))") var codecs = Codecs.new() - print("codecs \(codecs.ids())") - var listed = codecs.listGet + print("codecs \(try! codecs.ids())") + var listed = try! codecs.listGet() var reversed = UniqueArray() reversed.append(listed.remove(at: 1)) reversed.append(listed.remove(at: 0)) - codecs.listSet(reversed) - print("codecs reversed \(codecs.ids())") + try! codecs.listSet(reversed) + print("codecs reversed \(try! codecs.ids())") // A standard library value no type maps, in a handle. let failed = failure("boom") - let message = failureMessage(failed) - print("failure \(message) \(failureKind(failed))") + let message = try! failureMessage(failed) + print("failure \(message) \(try! failureKind(failed))") // Iterators, passed as the arrays of their items. print("sum_all \(sumAll([1, 2, 3]))") var weighed = UniqueArray() weighed.append(Counter.new("w", 3)) - print("weigh_all \(weighAllCounter(weighed))") + print("weigh_all \(try! weighAllCounter(weighed))") print("offered_len \(offeredLen([Bytes("h3".utf8), Bytes("moq-00".utf8)]))") print("join_words \(joinWords(["a", "b", "c"]))") // Bytes that must be `'static`, interned. @@ -745,40 +850,40 @@ func script() { check(false, "resume throws its message") } var meters = Meters.new() - let first = meters.first() - _ = first.add(2) - print("lent \(first.add(3)) \(meters.first().count())") - meters.clear() - print("cleared \(meters.first().count())") - let last = meters.lastGet - _ = last.add(4) - print("field lent \(last.add(1))") + let first = try! meters.first() + _ = try! first.add(2) + print("lent \(try! first.add(3)) \(try! meters.first().count())") + try! meters.clear() + print("cleared \(try! meters.first().count())") + let last = try! meters.lastGet() + _ = try! last.add(4) + print("field lent \(try! last.add(1))") // An optional field that is not Clone, lent as `Some` of a handle. var hasSpare = false - if let _ = meters.spareGet { + if let _ = try! meters.spareGet() { hasSpare = true } print("spare \(hasSpare)") - meters.spareSet(Meter.default()) - if let spare = meters.spareGet { - _ = spare.add(5) - print("spare lent \(spare.add(1))") + try! meters.spareSet(Meter.default()) + if let spare = try! meters.spareGet() { + _ = try! spare.add(5) + print("spare lent \(try! spare.add(1))") } let tallies = Tallies.new() - _ = tallies.first().bump() - print("tallied \(tallies.first().bump())") + _ = try! tallies.first().bump() + print("tallied \(try! tallies.first().bump())") } print("chunks \(chunks(Bytes("abcde".utf8), 2).map { Array($0) })") var counters = counters(["x", "y"]) - print("counters \(listed(counters) { "\($0.name()) \($0.get())" })") + print("counters \(listed(counters) { "\(try! $0.name()) \(try! $0.get())" })") // Each counter moves in. var some = UniqueArray() some.append(counters.remove(at: 0)) var boxed = UniqueArray() boxed.append(counters.remove(at: 0)) - print("worth \(worth(some, boxed)) \(worth(nil, UniqueArray()))") - print("held \([heldCount(Counter.new("h", 4)), heldCount(nil)].map { $0.map { "Some(\($0))" } ?? "None" }.joined(separator: " "))") - print("boxed \(boxedName(boxedCounter("b")).map { "Some(\"\($0)\")" } ?? "None") \(boxedName(nil) ?? "None") \(boxedCounter(nil) == nil)") + print("worth \(try! worth(some, boxed)) \(try! worth(nil, UniqueArray()))") + print("held \([try! heldCount(Counter.new("h", 4)), try! heldCount(nil)].map { $0.map { "Some(\($0))" } ?? "None" }.joined(separator: " "))") + print("boxed \(try! boxedName(boxedCounter("b")).map { "Some(\"\($0)\")" } ?? "None") \(try! boxedName(nil) ?? "None") \(boxedCounter(nil) == nil)") print("less \(less(5, 2))") print("sums \(sums([[1, 2], [], [3]]))") print("least \(least([[3, 1], [], [2]]).map { "\($0)" }.joined(separator: " "))") @@ -803,16 +908,16 @@ func script() { let parts = String(decoding: Api.joined([Bytes("ab".utf8), Bytes(), Bytes("c".utf8)]), as: UTF8.self) print("joined \(parts) \(Api.joined([]).count) read \(readLen(Bytes("hello".utf8)))") let doc = try! Json.parse(#"{"b": [1, 2.5, null], "a": "é"}"#) - print("json \(encode(doc))") - print("timed text \(timedText(doc))") - let tagged = tagged(doc, "t") - print("tagged \(tagged.toText())") + print("json \(try! encode(doc))") + print("timed text \(try! timedText(doc))") + let tagged = try! tagged(doc, "t") + print("tagged \(try! tagged.toText())") // A JSON object's entries cross as pairs of a key and its document, each moved in. var pairs = UniqueArray() pairs.append(TupleStringJson("b", try! Json.parse("[1]"))) pairs.append(TupleStringJson("a", try! Json.parse("2"))) - let jsonEntries = entries(pairs, "t") - print("entries \(listed(jsonEntries) { "\($0._0) \($0._1.toText())" })") + let jsonEntries = try! entries(pairs, "t") + print("entries \(listed(jsonEntries) { "\($0._0) \(try! $0._1.toText())" })") do { _ = try Json.parse("{\"a\":") } catch let e as StringError { @@ -820,21 +925,21 @@ func script() { } catch { check(false, "parse throws its message") } - let site = Site.new(base) - print("site \(site.homeGet.description) \(site.homeGet.description)") + let site = try! Site.new(base) + print("site \(try! site.homeGet().description) \(try! site.homeGet().description)") // Slices, passed in and returned as clones, and the socket address of each family. print("shapes \(Shape.ALL) greetings \(GREETINGS)") print("both \(Shape.BOTH) primes \(PRIMES)") var trail = Trail.new([Point(x: 1, y: 2), Point(x: 3, y: 4)], ["a", "b"]) - print("trail \(trail.points()) \(trail.labels())") + print("trail \(try! trail.points()) \(try! trail.labels())") // Returned iterators, each collected at the call: drained labels are gone after. - print("iterated \(trail.iter()) \(trail.names()) \(Array(trail.bytes()))") - print("drained \(trail.drain()) then \(trail.labels())") + print("iterated \(try! trail.iter()) \(try! trail.names()) \(Array(try! trail.bytes()))") + print("drained \(try! trail.drain()) then \(try! trail.labels())") // A filter, collected at the call. - print("beyond \(trail.beyond(1).map { "\($0.x) \($0.y)" }.joined(separator: ", "))") + print("beyond \(try! trail.beyond(1).map { "\($0.x) \($0.y)" }.joined(separator: ", "))") let numbered = numbered(["p", "q"]) - print("numbered \(listed(numbered) { "\($0.name()) \($0.get())" })") + print("numbered \(listed(numbered) { "\(try! $0.name()) \(try! $0.get())" })") let (next4, next6) = nexts(SocketAddr(ip: [127, 0, 0, 1], port: 8080), v6) print("nexts \(next4.ip.map(String.init).joined(separator: ".")) \(next4.port) \(next6.port) \(next6.scopeId)") @@ -842,8 +947,8 @@ func script() { var sectionPairs = UniqueArray() sectionPairs.append(TupleStringJson("b", try! Json.parse("[1]"))) sectionPairs.append(TupleStringJson("a", try! Json.parse("2"))) - let jsonSections = sections(sectionPairs) - print("sections \(listed(jsonSections) { "\($0._0) \($0._1.toText())" })") + let jsonSections = try! sections(sectionPairs) + print("sections \(listed(jsonSections) { "\($0._0) \(try! $0._1.toText())" })") // An instant only Rust makes, which each call copies out of its handle, and compares. let millis = { (d: Swift.Duration) in d.components.seconds * 1000 + d.components.attoseconds / 1_000_000_000_000_000 } let began = now() @@ -852,9 +957,9 @@ func script() { print("has_instant \(hasInstant(began)) \(hasInstant(nil))") // tracing's level, and its filter as an optional level whose nil is `OFF`. var verbosity = Verbosity.new(.warn) - print("verbosity \(spell(verbosity.filter())) \(spell(louder(verbosity.filter())))") - verbosity.levelSet(.trace) - print("verbosity \(verbosity.levelGet == .trace) \(spell(verbosity.filter())) \(spell(louder(verbosity.filter()))) \(spell(louder(nil)))") + print("verbosity \(spell(try! verbosity.filter())) \(spell(louder(try! verbosity.filter())))") + try! verbosity.levelSet(.trace) + print("verbosity \(try! verbosity.levelGet == .trace) \(spell(try! verbosity.filter())) \(spell(louder(try! verbosity.filter()))) \(spell(louder(nil)))") print("made \(made(3) { "\($0) times" })") // A closure taking and returning `Copy` objects copies them, never running their `Clone`. @@ -862,8 +967,13 @@ func script() { let given = reissue(Ticket.new(7)) { Ticket.new($0.number() + 1) }.number() let held = reissue(Ticket.new(7)) { _ in nine }.number() print("reissued \(given) \(held), cloned \(Ticket.clones())") + // A record's traits borrow the checked handle it holds. + let nines = Mark.of(9) + let front = try! seat(nines, 1) + let back = try! seat(nines, 2) + print("seat \(try! front.debugDescription) \(try! front == front) \(try! front == back)") // A kept closure, called on another thread after the call that took it returned. - print("spawned \(Spawned.new { 7 }.join())") + print("spawned \(try! Spawned.new { 7 }.join())") // A kept closure: called again at the instant it asks for, then once its waiter wakes. let start = Moment.new(0, 1000)! let calls = Cell(0) @@ -874,7 +984,7 @@ func script() { case 1: return now.after(5) case 2: print("drive \(now.millis() - start.millis())") - waiter.waker()() + try! waiter.waker()() return nil default: throw StringError(error: "stopped after \(calls.value)") } @@ -900,6 +1010,7 @@ func script() { print("hop \(nextHop(hop).map { "Some(\($0))" } ?? "None")") } print("farther hop \(fartherHop(PairNonZeroU16(left: 3, right: .max)))") + targets() } /// Print each of `n`'s lists, its floats as their bits, as transcript.rs does. @@ -1004,7 +1115,7 @@ func futures() { // The send lands from another thread while the receive waits. let payload = block { - Thread.detachNewThread { channel.send(Bytes("blocked".utf8)) } + Thread.detachNewThread { try! channel.send(Bytes("blocked".utf8)) } return try await channel.recv() } check(text(payload) == "blocked", "recv returns the payload another thread sent") @@ -1017,7 +1128,7 @@ func futures() { q.launch { got.value = text(try await channel.recv()) } q.drain() check(got.value == nil, "recv waits for a send") - channel.send(Bytes("then".utf8)) + try! channel.send(Bytes("then".utf8)) check(got.value == nil, "recv never resumes inside the wake") q.drain() check(got.value == "then", "recv resumes with the payload") @@ -1055,7 +1166,7 @@ func futures() { } } q.drain() - channel.send(Bytes("kept".utf8)) + try! channel.send(Bytes("kept".utf8)) task.cancel() q.drain() check(!called.value && threw.value, "a cancelled receive throws CancellationError") @@ -1066,7 +1177,7 @@ func futures() { // A task cancelled before its call takes nothing, even a payload ready at the first poll. do { let threw = Cell(false) - channel.send(Bytes("ready".utf8)) + try! channel.send(Bytes("ready".utf8)) let task = q.launch { do { _ = try await channel.recv() @@ -1123,11 +1234,11 @@ func futures() { let sender: Channel do { let other = Channel.new() - sender = other.clone() + sender = try! other.clone() q.launch { got.value = text(try await other.recv()) } q.drain() } - sender.send(Bytes("held".utf8)) + try! sender.send(Bytes("held".utf8)) q.drain() check(got.value == "held", "the call awaiting on an object holds it") } @@ -1224,7 +1335,7 @@ func futures() { _ = held return 1 } - check(spawned.join() == 1, "a spawned closure runs") + check(try! spawned.join() == 1, "a spawned closure runs") until { freed.value } check(freed.value, "Rust releases a kept closure once it drops it") } @@ -1269,7 +1380,7 @@ func owned() { check(dropped() == before + 1, "releasing returned bytes frees Rust's") do { let token = Token.new(2) - check(token.idGet == 2, "a token reads back") + check(try! token.idGet == 2, "a token reads back") } check(dropped() == before + 2, "releasing an object frees its handle") do { @@ -1294,18 +1405,18 @@ func forward() { let channel = Channel.new() let payload = tokenBytes(4, 64) let at = address(payload) - channel.send(payload) + try! channel.send(payload) let got = block { try await channel.recv() } check(address(got) == at && got.count == 64, "a payload sent back is Rust's own") let packet = nextPacket(Packet(seq: 1, payload: got)) check(address(packet.payload) == at && packet.payload.count == 64, "a record carries a payload back as Rust's own") let part = packet.payload.dropFirst(8) - channel.send(part) + try! channel.send(part) let sent = block { try await channel.recv() } check(address(sent) == at.map { $0 + 8 } && sent.count == 56, "a slice sent back is Rust's own") check(Bytes(sent.data) == sent && sent.data.count == 56, "Rust's bytes copy into a Data") // A few bytes `Data` holds inline, which pass back as Swift's. - channel.send(tokenBytes(6, 3)) + try! channel.send(tokenBytes(6, 3)) check(block { try await channel.recv() } == Bytes([6, 6, 6]), "a short payload sent back reads back") } @@ -1314,24 +1425,24 @@ func forward() { #if MOVED func moved() { let c = Counter.new("c", 0) - _ = c.intoLabel(nil) - _ = c.get() + _ = try! c.intoLabel(nil) + _ = try! c.get() } #endif #if BOXED_MOVED func boxedMoved() { - let tail = Node.new(1, nil) - _ = Node.new(2, tail) - _ = tail.sum() + let tail = try! Node.new(1, nil) + _ = try! Node.new(2, tail) + _ = try! tail.sum() } #endif #if LENT_MOVED func lentMoved() { - var registry = Registry.new(Counter.new("c", 0)) - _ = registry.edit { counter in - _ = counter.intoLabel(nil) + var registry = try! Registry.new(Counter.new("c", 0)) + _ = try! registry.edit { counter in + _ = try! counter.intoLabel(nil) return false } } @@ -1345,7 +1456,7 @@ final class Kept: @unchecked Sendable { func lentKept() { let kept = Kept() - _ = peek(Tally.new()) { lent in + _ = try! peek(Tally.new()) { lent in kept.tally = lent return 0 } @@ -1364,28 +1475,29 @@ func broken(_ which: String) { // A lent return outliving its owner's change, which Rust checks. case "outlived": var meters = Meters.new() - let first = meters.first() - meters.clear() - _ = first.count() + let first = try! meters.first() + try! meters.clear() + do { + _ = try first.count() + } catch let e as CheckError { + // Rust ran nothing, so its meters still clear. + check(e.function == "api_Meter_count", "a check names its function") + try! meters.clear() + fatalError(e.description) + } catch { + check(false, "a failed check throws a CheckError") + } // An IPv6 address where Rust takes `SocketAddrV4`. case "family": let any = SocketAddr(ip: Array(repeating: 0, count: 16), port: 1) _ = nexts(any, any) // No String holds a path that is not UTF-8. case "stray": - _ = stray() + _ = try! stray() case "lent stray": - visitStray { _ in } - // A class moved into Rust, which Swift still holds: the call closes its handle. - case "refusal moved": - let kept = refusal("z") - var pairs = UniqueArray() - pairs.append(TupleCounterRefusal(Counter.new("a", 1), kept)) - _ = reasons(pairs, nil) - _ = kept.description - // The file serves wasm32 too, whose items the host's library lacks. - case "target": - _ = wasmOnly() + try! visitStray { _ in } + // The file serves wasm32 too, whose items the host's library lacks, where Swift cannot + // throw. case "variant": _ = pointerBits(.wasm(bits: 32)) case "plain variant": diff --git a/ffi/tests/fixtures.rs b/ffi/tests/fixtures.rs index a7d9eeb2b..b58cd01f5 100644 --- a/ffi/tests/fixtures.rs +++ b/ffi/tests/fixtures.rs @@ -217,6 +217,8 @@ fn api_c() { api.transcript, "the C transcript differs from the native one" ); + // A non-null status takes each failed check, which changed nothing. + run(Command::new(&exe).arg("statuses")); // Each broken contract aborts, naming the function and what broke. for (case, message) in [ @@ -238,7 +240,7 @@ fn api_c() { ("lent", "api_Registry_edit: a lent handle was freed"), ( "lent moved", - "api_Counter_into_label: a lent handle's value cannot move out", + "api_Counter_into_label: a handle a call lent is borrowed exclusively or moved out", ), ("null", "api_Registry_edit: a function is null"), ("returned", "api_made: a returned buffer is not one buf_copy made"), @@ -246,7 +248,7 @@ fn api_c() { ("outlived", OUTLIVED), ( "lent shared", - "api_Meter_reset: a handle a call lent shared is borrowed exclusively", + "api_Meter_reset: a handle a call lent is borrowed exclusively or moved out", ), ("target", "api_wasm_only: it is not on this library's target"), ( @@ -404,7 +406,7 @@ fn api_cpp() { ), ( "lent moved", - "api_Counter_into_label: a lent handle's value cannot move out", + "api_Counter_into_label: a handle a call lent is borrowed exclusively or moved out", ), ("lent kept", "a lent object was moved out of"), ("overread", "api_tail: a reader read more than it was asked"), @@ -845,18 +847,21 @@ fn kotlin(native: &str, vm: &dyn Fn(&[&str]) -> Command) { run(&mut vm(&["owned"])); run(&mut vm(&["clash"])); for (case, message) in [ - ("moved", "Counter is closed"), - ("boxed moved", "Node is closed"), + ("moved", "IllegalStateException: Counter is closed"), + ("boxed moved", "IllegalStateException: Node is closed"), ("lent kept", "Counter is closed"), ( "lent moved", - "api_Counter_into_label: a lent handle's value cannot move out", + "Native$__Refused: api_Counter_into_label: a handle a call lent is borrowed exclusively or moved out", ), ("callback threw", "api::Log::log: a call into Kotlin threw"), ("closure threw", "api_made: a call into Kotlin threw"), ("closure failed", "api_drive: a call into Kotlin threw"), ("reader threw", "SeekableByteChannel.read: a call into Kotlin threw"), - ("outlived", OUTLIVED), + ( + "outlived", + "Native$__Refused: api_Meter_count: a handle lent from another outlived it", + ), ("stray", "MalformedInputException"), ("family", "an IPv6 address has no Rust SocketAddrV4"), ] { @@ -947,17 +952,23 @@ fn api_python() { run(&mut python(&dir, &[case])); } for (case, message) in [ - ("moved", "ValueError: Counter is closed"), - ("boxed moved", "ValueError: Node is closed"), + ("moved", "ValueError: api_Counter_get: a handle's value was moved out"), + ( + "boxed moved", + "ValueError: api_Node_sum: a handle's value was moved out", + ), ("lent kept", "ValueError: Counter is closed"), ("lent held", "Counter: a call still holds it when its lend ends"), ( "lent moved", - "api_Counter_into_label: a lent handle's value cannot move out", + "RuntimeError: api_Counter_into_label: a handle a call lent is borrowed exclusively or moved out", ), ("callback threw", "api::Log::log: a call into Python raised"), ("foreign cancelled", "a future Rust polls raised"), - ("outlived", OUTLIVED), + ( + "outlived", + "ValueError: api_Meter_count: a handle lent from another outlived it", + ), ("family", "ValueError: an IPv6 address has no Rust SocketAddrV4"), ("kept threw", "api_Spawned_new: a call into Python raised"), ("returned", "api_made: a call into Python raised"), @@ -1151,8 +1162,6 @@ fn api_swift() { ("outlived", OUTLIVED), ("stray", "a path from Rust is not UTF-8"), ("lent stray", "a path from Rust is not UTF-8"), - ("refusal moved", "Refusal is closed"), - ("target", "api_wasm_only: it is not on this library's target"), ("variant", "api_Platform.Wasm: it is not on this library's target"), ("plain variant", "api_Family.Wasm: it is not on this library's target"), ] { @@ -1238,12 +1247,12 @@ fn api_dart() { run(&mut dart_run(&exe, &[case])); } for (case, message) in [ - ("moved", "Bad state: Counter is closed"), - ("boxed moved", "Bad state: Node is closed"), + ("moved", "Bad state: api_Counter_get: a handle's value was moved out"), + ("boxed moved", "Bad state: api_Node_sum: a handle's value was moved out"), ("lent kept", "Bad state: Counter is closed"), ( "lent moved", - "api_Counter_into_label: a lent handle's value cannot move out", + "Bad state: api_Counter_into_label: a handle a call lent is borrowed exclusively or moved out", ), ("callback threw", "api::Log::log: a call into Dart threw"), ( @@ -1254,7 +1263,10 @@ fn api_dart() { "reader threw", "a reader's seek: a call into Dart threw Bad state: a seek failed", ), - ("outlived", OUTLIVED), + ( + "outlived", + "Bad state: api_Meter_count: a handle lent from another outlived it", + ), ("family", "an IPv6 address has no Rust SocketAddrV4"), ("stray", "a path from Rust is not UTF-8"), ("lent stray", "a path from Rust is not UTF-8"), @@ -1455,7 +1467,7 @@ fn api_go() { for (case, message) in [ ( "lent moved", - "api_Counter_into_label: a lent handle's value cannot move out", + "api_Counter_into_label: a handle a call lent is borrowed exclusively or moved out", ), ("nil reader", "a nil io.ReadSeeker"), ("returned zero", "a zero Moment, which only Rust makes"), @@ -1743,7 +1755,7 @@ fn unreached_types() { assert!(glue.contains("fn pkg_copied_again("), "{glue}"); // A conversion target only an `impl Into` names is reached, and taken. assert!( - glue.contains("rt::Object::<::conv_reached::Value>::take(p0, w)"), + glue.contains("rt::Object::<::conv_reached::Value>::reserve(p0, takes, w)"), "{glue}" ); assert!(glue.contains("::kio_reached::Pending"), "{glue}"); diff --git a/ffi/tests/fixtures/shapes.rs b/ffi/tests/fixtures/shapes.rs index 28b9359b9..08ed0890c 100644 --- a/ffi/tests/fixtures/shapes.rs +++ b/ffi/tests/fixtures/shapes.rs @@ -979,9 +979,10 @@ impl Proto { /// `ty`. A poll's call must return `ready`. fn call(&self, function: &str, args: &[String], ty: &str, var: &str, ready: Option<&str>) -> String { let args = args.join(", "); - let outs = &self.params[self.params.len() - self.outs()..]; + let end = self.params.len() - usize::from(self.status()); + let outs = &self.params[end - self.outs()..end]; if outs.is_empty() { - return format!("\t\t{ty} {var} = {function}({args});\n"); + return format!("\t\t{ty} {var} = {function}({args}{});\n", self.tail()); } let mut out = String::new(); let mut pointers = Vec::new(); @@ -991,7 +992,7 @@ impl Proto { pointers.push(format!("&{var}_{k}")); values.push(format!("{var}_{k}")); } - let call = format!("{function}({args}, {})", pointers.join(", ")); + let call = format!("{function}({args}, {}{})", pointers.join(", "), self.tail()); let _ = match ready { Some(ready) => writeln!(out, "\t\tif ({call} != {ready}) abort();"), None => writeln!(out, "\t\t{call};"), @@ -1004,6 +1005,19 @@ impl Proto { out } + /// Whether it takes a trailing status, for a check one of its handles can fail. + fn status(&self) -> bool { + self.params.last().is_some_and(|(_, name)| name == "status") + } + + /// What follows its other arguments: NULL for a status, aborting on a failed check. + fn tail(&self) -> &'static str { + match self.status() { + true => ", NULL", + false => "", + } + } + /// How many out pointers it writes its output through: `out`, or `out0` on for a tuple. fn outs(&self) -> usize { let out = |name: &str| name == "out" || name.strip_prefix("out").is_some_and(|k| k.parse::().is_ok()); @@ -1046,8 +1060,9 @@ fn c(built: &Built, calls: &[Call]) -> String { block += &echo.call(&fname(Item::Echo), &["a".into()], &digested, "b", None); let _ = writeln!( block, - "\t\tprintf(\"{label} %\" PRIu64 \"\\n\", {}(b));", - fname(Item::Digest) + "\t\tprintf(\"{label} %\" PRIu64 \"\\n\", {}(b{}));", + fname(Item::Digest), + digest.tail() ); } Call::Lent(..) | Call::Peek(..) => { @@ -1060,9 +1075,10 @@ fn c(built: &Built, calls: &[Call]) -> String { let (_, param) = proto(item, n).pointer("f"); let _ = writeln!( closures, - "static uint64_t {}(void *data, {param} v) {{\n\t(void)data;\n\treturn {}(v);\n}}\n", + "static uint64_t {}(void *data, {param} v) {{\n\t(void)data;\n\treturn {}(v{});\n}}\n", item.name(n), - fname(Item::Digest) + fname(Item::Digest), + digest.tail() ); } let _ = writeln!( @@ -1099,7 +1115,7 @@ fn c(built: &Built, calls: &[Call]) -> String { let future = format!("{}_future", fname(Item::Later)); let poll = prototype(&header, &format!("{}_poll", fname(Item::Later))); block += &make.call(&fname(Item::Make), &[format!("{seed}u")], later.param("v"), "a", None); - let _ = writeln!(block, "\t\t{future} *f = {}(a);", fname(Item::Later)); + let _ = writeln!(block, "\t\t{future} *f = {}(a{});", fname(Item::Later), later.tail()); // Ready at once: an async fn that awaits nothing. let ready = format!("{}_POLL_READY", name.to_uppercase()); let polled = poll.call( @@ -1113,8 +1129,9 @@ fn c(built: &Built, calls: &[Call]) -> String { let _ = writeln!(block, "\t\t{future}_free(f);"); let _ = writeln!( block, - "\t\tprintf(\"{label} %\" PRIu64 \"\\n\", {}(b));", - fname(Item::Digest) + "\t\tprintf(\"{label} %\" PRIu64 \"\\n\", {}(b{}));", + fname(Item::Digest), + digest.tail() ); } } @@ -1212,14 +1229,52 @@ fn python(built: &Built, calls: &[Call]) -> String { /// The Swift driver, in the generated module's `main.swift`: each shape's calls in a /// function of its own, since type-checking them as one body of top-level code takes /// tens of gigabytes. -fn swift(calls: &[Call]) -> String { +fn swift(calls: &[Call], source: &str) -> String { + // Whether the generated `func` throws, as one taking a checked handle does. + let throws = |function: &str| { + let at = source + .find(&format!("public func {function}(")) + .unwrap_or_else(|| panic!("the Swift declares {function}")); + source[at..].lines().next().unwrap().contains(" throws ") + }; + // `try` where an expression's call throws: a closure, which cannot, aborts instead. + let tr = |functions: &[String]| match functions.iter().any(|f| throws(f)) { + true => "try ", + false => "", + }; + let closed = |function: String| match throws(&function) { + true => "try! ", + false => "", + }; let shapes = by_shape(calls, |call| { let value = match call { - Call::Echo(n, seed) => format!("digest{n}(echo{n}(make{n}({seed})))"), - Call::Lent(n, seed) => format!("lent{n}({seed}) {{ digest{n}($0) }}"), - Call::Peek(n, seed) => format!("peek{n}({seed}) {{ digest{n}($0) }}"), - Call::Made(n, seed) => format!("made{n}({seed}) {{ make{n}($0) }}"), - Call::Later(n, seed) => format!("digest{n}(try await later{n}(make{n}({seed})))"), + Call::Echo(n, seed) => format!( + "{}digest{n}(echo{n}(make{n}({seed})))", + tr(&[format!("digest{n}"), format!("echo{n}"), format!("make{n}")]) + ), + Call::Lent(n, seed) => { + format!( + "{}lent{n}({seed}) {{ {}digest{n}($0) }}", + tr(&[format!("lent{n}")]), + closed(format!("digest{n}")) + ) + } + Call::Peek(n, seed) => { + format!( + "{}peek{n}({seed}) {{ {}digest{n}($0) }}", + tr(&[format!("peek{n}")]), + closed(format!("digest{n}")) + ) + } + Call::Made(n, seed) => { + format!( + "{}made{n}({seed}) {{ {}make{n}($0) }}", + tr(&[format!("made{n}")]), + closed(format!("make{n}")) + ) + } + // The future throws, so `try` always marks the call. + Call::Later(n, seed) => format!("try await digest{n}(later{n}(make{n}({seed})))"), }; format!("\tprint(\"{} \\({value})\")", call.label()) }); @@ -1466,7 +1521,8 @@ fn round_trip(built: &Built, language: &str) { let module = built.out.join("swift"); let (generated, main) = (dir.join(format!("{name}.swift")), dir.join("main.swift")); step::copy(&module.join(format!("{name}.swift")), &generated); - step::write(&main, swift(calls)); + let source = std::fs::read_to_string(&generated).unwrap(); + step::write(&main, swift(calls, &source)); let collections = super::swift_collections(); let include = built.out.join("include"); let step = Step { diff --git a/ffi/tests/miri/api.rs b/ffi/tests/miri/api.rs index a9a8a7ed1..9449d5de6 100644 --- a/ffi/tests/miri/api.rs +++ b/ffi/tests/miri/api.rs @@ -102,16 +102,35 @@ unsafe extern "C" fn foreign_free(data: *mut c_void) { fn handles() { unsafe { let counter = api_Counter_new(view("c"), 5); - assert_eq!(api_Counter_get(counter), 5); + assert_eq!(api_Counter_get(counter, std::ptr::null_mut()), 5); let mut out = 0; - assert!(api_Counter_add(counter, 2, &mut out, std::ptr::null_mut())); - assert_eq!(api_Counter_get(counter), 7); - assert_eq!(take(api_Counter_name(counter)), "c"); + assert!(api_Counter_add(counter, 2, &mut out, std::ptr::null_mut(), std::ptr::null_mut())); + assert_eq!(api_Counter_get(counter, std::ptr::null_mut()), 7); + assert_eq!(take(api_Counter_name(counter, std::ptr::null_mut())), "c"); let other = api_Counter_new(view("d"), 3); - assert_eq!(api_Counter_plus(counter, other), 10); + assert_eq!(api_Counter_plus(counter, other, std::ptr::null_mut()), 10); api_Counter_free(other); // Moving the value out leaves the handle for the caller to free. - assert_eq!(api_Counter_into_sum(counter, 1), 8); + assert_eq!(api_Counter_into_sum(counter, 1, std::ptr::null_mut()), 8); + api_Counter_free(counter); + } +} + +/// A close while a call reserved the value to move out fails as borrowed, so the call's +/// rollback, once a later check of it fails, revives no value the close reported dropped. +#[test] +fn close_while_reserved() { + type Counter = rt::Object<::api_export::Counter>; + unsafe { + let counter = api_Counter_new(view("c"), 5); + let mut takes = rt::Takes::default(); + Counter::reserve(counter, &mut takes, "reserve").unwrap(); + assert_eq!(Counter::close(counter, "close"), Err(rt::Status::Borrowed)); + assert!(Counter::borrow(counter, "borrow").is_err()); + drop(takes); + let mut status = 0; + assert_eq!(api_Counter_get(counter, &mut status), 5); + assert_eq!(status, 0); api_Counter_free(counter); } } @@ -135,26 +154,26 @@ fn channel() { let w = waker(&counts); unsafe { let channel = api_Channel_new(); - let recv = api_Channel_recv(channel); + let recv = api_Channel_recv(channel, std::ptr::null_mut()); let mut payload = rt::Buf::NONE; assert_eq!(api_Channel_recv_poll(recv, w, &mut payload), rt::PENDING); - api_Channel_send(channel, view("hi")); + api_Channel_send(channel, view("hi"), std::ptr::null_mut()); assert_eq!(counts.wakes.load(Ordering::SeqCst), 1); assert_eq!(api_Channel_recv_poll(recv, w, &mut payload), rt::READY); assert_eq!(take(payload), "hi"); api_Channel_recv_future_free(recv); // A send from another thread wakes the poll's waker there. - let recv = api_Channel_recv(channel); + let recv = api_Channel_recv(channel, std::ptr::null_mut()); assert_eq!(api_Channel_recv_poll(recv, w, &mut payload), rt::PENDING); - api_Channel_send_from_thread(channel, view("far")); + api_Channel_send_from_thread(channel, view("far"), std::ptr::null_mut()); assert_eq!(counts.wakes.load(Ordering::SeqCst), 2); assert_eq!(api_Channel_recv_poll(recv, w, &mut payload), rt::READY); assert_eq!(take(payload), "far"); api_Channel_recv_future_free(recv); // Cancelled while pending, it frees what it held, its waker included. - let recv = api_Channel_recv(channel); + let recv = api_Channel_recv(channel, std::ptr::null_mut()); assert_eq!(api_Channel_recv_poll(recv, w, &mut payload), rt::PENDING); api_Channel_recv_future_free(recv); api_Channel_free(channel); diff --git a/quest/m1/ffi-export/README.md b/quest/m1/ffi-export/README.md index 747c5f307..ed2be7b06 100644 --- a/quest/m1/ffi-export/README.md +++ b/quest/m1/ffi-export/README.md @@ -32,7 +32,7 @@ Decided with the maintainer on 2026-11-09, in the quest audit: ones ([lossless](/quest/m1/ffi-export/lossless.md)). Rejected: idiomatic types whose truncations and aborts become errors; today's mappings. - ✅ A failed run-time check raises the language's own error, and a Rust panic aborts - ([checks-raise](/quest/m1/ffi-export/checks-raise.md)). Rejected: catching panics at the + ([#730](https://github.com/kixel-ai/rust2/pull/730)). Rejected: catching panics at the boundary; aborting on every failed check. - ✅ A type or future that is not `Send` exports, checked by thread ([not-send](/quest/m1/ffi-export/not-send.md)). Rejected: a Rust-owned thread for every diff --git a/quest/m1/ffi-export/checks-raise.md b/quest/m1/ffi-export/checks-raise.md deleted file mode 100644 index c71fb7144..000000000 --- a/quest/m1/ffi-export/checks-raise.md +++ /dev/null @@ -1,61 +0,0 @@ -# [L] Failed checks raise - -## Goal - -A run-time check a [rust2-ffi](/ffi/README.md) binding fails, for a rule its language -cannot express, raises the language's own error and leaves the process running: a borrow -Rust would refuse (a `&mut self` call while another call holds the handle), a call on a -moved-out or closed handle, an object lent for a call used past it, or an item called on a -target that lacks it. Kotlin, Python, Dart, and Swift throw, and Go returns an `error`. C -and C++ abort with the message, and a Rust panic aborts everywhere. Each handle's borrows -are counted once per call, in Rust, and a type no export can misuse pays for no check. - -## Plan - -Decided with the maintainer on 2026-11-09, in the quest audit: ✅ checks raise and panics -abort. Rejected: raising panics too, which costs unwind tables and landing pads in every -export and a poisoned state for an object that panicked mid-call; aborting on every failed -check, as today. The check runs before any Rust code does, so nothing is corrupted when it -fails. AGENTS.md carries the rule. - -Decided with the maintainer on 2026-11-12, in the quest audit: - -- ✅ The one accounting is the runtime's, in `ffi/runtime/rt.rs`: a failed check returns a - status and the binding raises from it. Rejected: the accounting in each binding, as this - quest first planned, which ports the runtime's state machine (shared, exclusive, moved, - other thread, lent) to five runtimes and m3's three, and needs unchecked C entry points, - since Swift and Go call the C header directly; building both in Go first to measure. -- ✅ The exporter decides at export time which types need a check. A type no export - borrows exclusively or moves skips the accounting on its owned handles; lent handles - keep it. `ffi/src/ir.rs` already computes most of this for an object's `shared` flag. - -What the code does today: - -- `rt.rs` aborts through `fail` at 34 sites, reachable from safe multithreaded Kotlin, Go, - and Python. Every handle argument pays a compare-and-swap in and a `fetch_sub` out, which - is the whole of C's 10 ns u64 getter against Rust's 0.4 - ([the binding benchmark](/bench/ffi/README.md)). -- Kotlin and Go keep a hold count of their own beside the runtime's, so a call costs four - atomic operations per handle. Swift keeps one only for its classes since - [#697](https://github.com/kixel-ai/rust2/pull/697), and traps on a closed handle - (`rt.swift`'s `hold`). The bindings' counts go; the runtime's status replaces them. -- [#700](https://github.com/kixel-ai/rust2/pull/700) added a fourth check, an item called - on a target the library is not built for, which traps in Swift (`ffi/src/swift.rs`) and - panics in Go (`ffi/src/go.rs`). AGENTS.md lists three; add it there with this change. -- Swift's `async throws` and throwing functions already exist for `Result`; a call that - can fail only a check becomes throwing too, which changes its signature. Go already - returns `rt.ErrClosed` from every call that takes a handle. -- A foreign exception inside a callback or lent closure still aborts, since Rust has - nowhere to unwind it to. -- `ffi/README.md` says the C++ header throws nothing, so C++ aborts as C does. - -Measure a call before and after on the benchmark's handle call and u64 getter rows. A -checked call may keep the runtime's two atomics; a type that needs no check should reach -Rust's own cost plus the crossing. - -Public API: which generated functions throw or return an error (breaking in Swift), the -error types the runtimes add, and the status the C functions return. - -## Related - -- [Cheaper returned-object ownership](/quest/m1/ffi-speed/object-ownership.md) - rewrites the same handle word, so its baselines move with this diff --git a/quest/m1/ffi-export/kotlin-compile-heap.md b/quest/m1/ffi-export/kotlin-compile-heap.md new file mode 100644 index 000000000..8bdab9bbd --- /dev/null +++ b/quest/m1/ffi-export/kotlin-compile-heap.md @@ -0,0 +1,12 @@ +# [S] moq's Kotlin compiles in kotlinc's default heap + +## Goal + +The Kotlin the exporter writes for moq compiles under kotlinc's and Gradle's default heaps, so no consumer needs a JVM flag, and `ffi/justfile` drops the `JAVA_OPTS="-Xmx2g -Xms128M"` its moq recipes set. + +## Plan + +- Kept by the maintainer on 2026-11-14 ([kixel-ai/rust2#730](https://github.com/kixel-ai/rust2/pull/730#issuecomment-6881950120)), rather than dropped. +- `moq.kt` is one 3.5 MB file, and kotlinc 2.4.21 needs 512 to 640 MB of heap to compile it with `moq/moq.kt`, just past the wrapper's default `-Xmx512M`. Main also outgrew that default by kixel-ai/rust2#805, which set the flag for both moq recipes. +- Measure first: the heap the Android Gradle plugin's Kotlin daemon gets by default, and what compiling `moq.kt` takes there (`just ffi android` builds the api fixture the same way). +- Recommended fix: write one Kotlin file per exported type, or per Rust module, beside `_Native`'s, so peak heap tracks the largest file rather than the whole export. Measure kotlinc's peak on moq before and after. diff --git a/quest/m1/ffi-export/lossless.md b/quest/m1/ffi-export/lossless.md index 826f9cfa9..16a6f4664 100644 --- a/quest/m1/ffi-export/lossless.md +++ b/quest/m1/ffi-export/lossless.md @@ -52,7 +52,3 @@ is the limit and no exact type exists at a call's speed, such as a Python `int` Public API: every generated API's types for durations, addresses, paths, and text (breaking), and the runtimes' conversion helpers. - -## Required - -- [Failed checks raise](/quest/m1/ffi-export/checks-raise.md) - how a binding raises, which the checked conversions and invalid text use diff --git a/quest/m1/ffi-export/not-send.md b/quest/m1/ffi-export/not-send.md index c6153843c..9c1a337b4 100644 --- a/quest/m1/ffi-export/not-send.md +++ b/quest/m1/ffi-export/not-send.md @@ -25,7 +25,7 @@ On 2026-11-14, at [#802](https://github.com/kixel-ai/rust2/pull/802)'s pick, the - The handle records its owner thread, and each call compares it: no atomics, so it costs less than a `Sync` type's borrow. The wrong thread - [raises](/quest/m1/ffi-export/checks-raise.md). + raises, as each failed check does. - A free from another thread never drops the value there. GC finalizers run elsewhere: Kotlin's phantom-reference daemon thread, Go's `runtime.AddCleanup` goroutine, Python's collector, Swift's `deinit` on the releasing thread, and Dart's finalizer with no @@ -54,7 +54,3 @@ On 2026-11-14, at [#802](https://github.com/kixel-ai/rust2/pull/802)'s pick, the check. Public API: the items that now export, and Swift's `Sendable` conformances (breaking). - -## Required - -- [Failed checks raise](/quest/m1/ffi-export/checks-raise.md) - the wrong-thread check raises through the same accounting diff --git a/quest/m1/ffi-export/ship.md b/quest/m1/ffi-export/ship.md index 351bfe110..ac3abac36 100644 --- a/quest/m1/ffi-export/ship.md +++ b/quest/m1/ffi-export/ship.md @@ -20,6 +20,5 @@ then see change. A sibling planned later adds itself to `Required` here. ## Required - [Refusals](/quest/m1/ffi-refusals/README.md) - the census lists no refusals at the moq pin, and `ffi.toml` is gone -- [Failed checks raise](/quest/m1/ffi-export/checks-raise.md) - which generated functions throw - [Lossless binding types](/quest/m1/ffi-export/lossless.md) - the types of durations, addresses, paths, and text - [Not Send](/quest/m1/ffi-export/not-send.md) - the types and futures that export checked by thread diff --git a/quest/m1/ffi-speed/go-status.md b/quest/m1/ffi-speed/go-status.md new file mode 100644 index 000000000..8698e1288 --- /dev/null +++ b/quest/m1/ffi-speed/go-status.md @@ -0,0 +1,25 @@ +# [S] Go's checked calls allocate no status + +## Goal + +A [rust2-ffi](/ffi/README.md) Go call that can fail a check passes its +`_status` without a heap allocation. Today each declares `var c_status_` and +passes `&c_status_` to C, which cgo moves to the heap: `go build -gcflags=-m` reports it +for every checked function (756 in moq's `net` package), and a bare cgo call measured +6 ns more with the pointer than without, about what Go's checked getter lost against +main in [the bench](/bench/ffi/README.md#per-handle-baseline). + +## Plan + +- ✅ Pass the stack cell's address hidden from escape analysis, as a `uintptr` turned back + into a pointer at the call (rejected: keep the heap allocation; decide later). Settled by + the maintainer 2026-11-13. A goroutine inside a cgo call is never shrunk (the runtime's + `isShrinkStackSafe` refuses one in a syscall), and the glue writes the status only when a + check fails, before any Rust code runs, so before any callback into Go could grow and + move the stack. The generated glue checks that ordering at run time: a status written + after a callback into Go began fails loudly, never silently. The fixtures run `go vet`, + which flags a `uintptr` made back into a pointer. +- Rejected: a thread-local last error, refused on 2026-11-12 as hidden state a caller can + forget to read; a `sync.Pool` of C cells, which costs more than the allocation; one + shared cell, since goroutines share threads. +- Re-measure the Go getter and handle call against main, CPU first. diff --git a/quest/m1/ffi-speed/object-ownership.md b/quest/m1/ffi-speed/object-ownership.md index 57b4b17fe..ebe67c0e5 100644 --- a/quest/m1/ffi-speed/object-ownership.md +++ b/quest/m1/ffi-speed/object-ownership.md @@ -19,7 +19,3 @@ Ownership stays automatic: a dropped object is still freed without a call from t - Explicit `Close` stays, and the cleanup stays as the backstop; dropping it is refused, since a missed `Close` would leak. - Re-measure the bench's object return and stamped write. - -## Related - -- [Failed checks raise](/quest/m1/ffi-export/checks-raise.md) - rewrites the same handle word; profile after it lands, or the baselines move diff --git a/quest/m1/ffi-speed/shared-borrow.md b/quest/m1/ffi-speed/shared-borrow.md index 516967a6d..dc0e1e5e3 100644 --- a/quest/m1/ffi-speed/shared-borrow.md +++ b/quest/m1/ffi-speed/shared-borrow.md @@ -21,8 +21,9 @@ flag; an exclusive borrow, a move, or a free clears the bias and waits for the t slots naming the handle to empty, then falls back to the count. A slot collision falls back to the count too. The uncontended cost stays one atomic store and one load, so the single-thread rows must not rise. -The language hold counts then need the same treatment, or folding into the C ABI's check, -which [checks-raise](/quest/m1/ffi-export/checks-raise.md) may already do. +The language hold counts then need the same treatment: Go's and Kotlin's still count each +call beside the C ABI's check, while Python's and Dart's borrow through it alone for a +checked type since [kixel-ai/rust2#730](https://github.com/kixel-ai/rust2/pull/730). Rejected for now: per-handle sharded counters, which cost each handle a cache line per shard. diff --git a/quest/m2/wasm/backend.md b/quest/m2/wasm/backend.md index f3b06c418..a02e43158 100644 --- a/quest/m2/wasm/backend.md +++ b/quest/m2/wasm/backend.md @@ -106,4 +106,3 @@ Public API: the TypeScript binding's shape, and the name of the web target. ## Required - [Lossless binding types](/quest/m1/ffi-export/lossless.md) - the types a new binding copies are the exact ones -- [Failed checks raise](/quest/m1/ffi-export/checks-raise.md) - the one accounting a new binding copies raises