Граф коммитов

1230 Коммитов

Автор SHA1 Сообщение Дата
Ben Dean-Kawamura e7a4130d2d Updating the foreign executor API
Added parameters/return values to signal failure.  In particular, we
need a way to signal that the foreign executor has been cancelled/closed
and therefore any futures that depend on it should be halted.  This is
needed to fix #1669.

Updated the Rust code to check for executor failures.  We need to
release any references passed to the waker callback since that callback
will not run in that case.

Updated the foreign code to handle cancelled futures.

Reworked and updated unit tests to test the new system.

Bumped `UNIFFI_CONTRACT_VERSION` since this requires changing the
scaffolding API.  I think this is correct, although you could make the
argument that it's not needed since the async code is still fairly
experimental.

Updated the `uniffi-versioning.md` doc now that
`UNIFFI_CONTRACT_VERSION` lives in `uniffi_meta`.
2023-08-16 15:12:17 -04:00
Ben Dean-Kawamura 689f099a2b Futures shared resource test updates
Set the timeout in Rust rather than the foreign language.  I'm not sure
exactly why, but on Kotlin if we see the issue the test simply hangs
rather than exiting with an exception.  By setting the timeout in Rust,
we can at least print out an error before the hangs.

Added Python and Swift tests.
2023-08-15 12:54:00 -04:00
Ben Dean-Kawamura 5b330c110e Copying code from hywan/feat-cancellable-async-func
This is mainly for the updated futures fixture tests, but I also copied
some other changes.
2023-08-15 12:54:00 -04:00
bendk 9d9db6dc84
Merge pull request #1709 from anonyome/gm/1708-kotlin-callback-method-name
Issue 1708: jna callback method "invoke" -> "callback"
2023-08-15 11:51:01 -04:00
bendk 7b133f5b7e
Merge pull request #1703 from ijc/swift-unused-field-on-flat-error-enum
swift: Remove unused value warning
2023-08-15 11:08:28 -04:00
George Mulhearn 29c5c97291 try with executor callback object as internal visibility
Signed-off-by: George Mulhearn <gmulhearn@anonyome.com>
2023-08-15 08:13:30 +10:00
George Mulhearn ac7f9536f7 make UniFfiForeignExecutorCallback.callback public
Signed-off-by: George Mulhearn <gmulhearn@anonyome.com>
2023-08-14 18:47:22 +10:00
Antonius Naumann 0dffde3ac5 Add cargo swift to "External resources" section 2023-08-14 10:24:01 +02:00
George Mulhearn 5039e0a9c6 try change callback -> invoke
Signed-off-by: George Mulhearn <gmulhearn@anonyome.com>
2023-08-14 18:04:20 +10:00
Ian Campbell 5598e0b607 swift: Avoid generating `try` on function calls which do not throw
```
uniffi-fixture-callbacks-129de7ccace1172c/fixture_callbacks.swift:1067:26: warning: no calls to throwing functions occur within 'try' expression
            let result = try swiftCallbackInterface.fromSimpleType(
                         ^
```

NB: the `swift::try` macro is concerned with the containing function calling
subfunctions (it expands to `try` or `try!`) which is not what is needed here,
we care about whether the subfunction being called throws.
2023-08-11 12:19:33 +02:00
Ian Campbell 4232270526 swift: Only quote keywords in contexts where that is required
Determined from https://docs.swift.org/swift-book/documentation/the-swift-programming-language/lexicalstructure/#Keywords-and-Punctuation

Previously lots of warning were generated of the form:
```
/tmp/target/tmp/uniffi-example-custom-types-2b18f735d09116c0/custom_types.swift:427:13: warning: keyword 'url' does not need to be escaped in argument list
            `url`: FfiConverterTypeUrl.read(from: &buf),
            ^   ~
```

When compiling generated Swift code.

Swift appears to be fairly tollerant of keyswords in some contexts, in
particular:
* only a small subset of keywords need to be quoted when a name is
  used as an argument label when calling a function.
* enum variant names do not need to be quoted when they are used, only when
  they are defined.

Fixes: #1460
2023-08-11 11:48:04 +02:00
Ian Campbell e10fcb2fa8 swift: Move away from deprecated `Data` constructor
```
uniffi-fixture-coverall-af68e6307d32e36f/coverall.swift:490:16: warning: 'init(bytes:)' is deprecated: replaced by 'init(_:)'
        return Data(bytes: try readBytes(&buf, count: Int(len)))
               ^
uniffi-fixture-coverall-af68e6307d32e36f/coverall.swift:490:16: note: use 'init(_:)' instead
        return Data(bytes: try readBytes(&buf, count: Int(len)))
               ^    ~~~~~~~
```

According to the docs for the [deprecated][] method and the [replacement][]
method the replacement is available in all the same versions as the deprecated
function, so there should be no risk in removing.

AFAICT the deprecation was in 2018/19 timeframe.

[deprecated]: https://developer.apple.com/documentation/foundation/data/3020550-init
[replacement]: https://developer.apple.com/documentation/foundation/data/2852972-init
2023-08-11 11:18:17 +02:00
Ian Campbell 2b52865e36 swift: Remove unused value warning
The generated code was producing a warning (seen a couple of dozen times in the
fixtures):

```
unary_result_alias.swift:373:32: warning: immutable value 'message' was never used; consider replacing with '_' or removing it
        case let .AllGoneWrong(message):
                               ^~~~~~~
                               _
```

The call of `write_fn` on `message` was removed back in 734050dbf1
("Refactoring the callback interface return code").

Comparing with the Kotlin and Python equivalents the pattern is that the
foreign enum/error has the message field but it is not propagated to Rust for
flat enums. So replace with `_` as suggested.

Doing so then results in:

```
unary_result_alias.swift:373:14: warning: 'let' pattern has no effect; sub-pattern didn't bind any variables
        case let .AllGoneWrong(_ /* message is ignored*/):
             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
```

So drop the `let` too.
2023-08-11 09:39:15 +01:00
Ian Campbell 53d5ac7274
Allow use of `uniffi::custom_newtype!` from non-root modules (#1691)
After moving `NewtypeHandle` in the fixture to a submodule I got:

```
error[E0433]: failed to resolve: use of undeclared type `UniffiCustomTypeConverter`
   --> fixtures/ext-types/proc-macro-lib/src/lib.rs:168:5
    |
168 |     uniffi::custom_newtype!(NewtypeHandle, i64);
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ use of undeclared type `UniffiCustomTypeConverter`
    |
    = note: this error originates in the macro `uniffi::custom_newtype` (in Nightly builds, run with -Z macro-backtrace for more info)
```

Giving the full path to the type defined in the scaffolding resolves this.
2023-08-10 10:36:11 -04:00
Jan-Erik Rediger 7f58021c08 Remove outdated comment in example script 2023-08-10 16:21:21 +02:00
bendk fde6d8842b
Merge pull request #1692 from bendk/kotlin-tochar-deprecation
Fix for Kotlin toChar deprecation (#1685)
2023-08-09 15:36:32 -04:00
Ian Campbell fc8a722c36
Re-allow more complex custom types in UDL (#1690)
* Add http-headermap custom type fixture

`http::HeaderMap` is a multi-map so for simplicity we expose as a `sequence` of
headers.

This was broken by https://github.com/mozilla/uniffi-rs/pull/1661 which caused:

```
error: unexpected token
   --> /tmp/target/debug/build/uniffi-fixture-ext-types-http-headermap-2750e41fdae052ad/out/http_headermap.uniffi.rs:132:40
    |
132 | ::uniffi::custom_type!(r#HeaderMap, std::vec::Vec<r#HttpHeader>);
    |                                        ^
```

* Use builtin's `FfiConverter::FfiType` when implementing custom type `FfiConverter`

This avoids needing to match on stringified Rust types.

This is the first part of the fix for the `http-headmap` fixture.

* Allow custom type's builtin to be a `Path` not just an `Ident`

This allows the `http-headermap` test to pass again.
2023-08-09 09:57:42 -04:00
Mark Hammond 1696344a8f
Followup to #1680, update macro names for custom type macros. (#1694) 2023-08-08 15:59:49 -04:00
Mark Hammond 81fd29d83c
Avoid using a derive for custom types to support types defined elsewhere. (#1680) 2023-08-08 14:23:49 -04:00
Ben Dean-Kawamura 153a09b2fd Fix for Kotlin toChar deprecation (#1685)
Copied from #1684 (thanks Hywan!)
2023-08-08 12:15:32 -04:00
soupslurpr c3c230574d fix typo detils -> details 2023-08-08 10:27:13 +02:00
bendk ea99c0f2ac
Merge pull request #1687 from bendk/kotlin-async-test-timeouts
Fix Kotlin callback handler reference handling
2023-08-07 15:28:27 -04:00
Ben Dean-Kawamura 65de282a03 Fix Kotlin callback handler reference handling
When we execute a future on Kotlin, we create a `jni.Callback` instance
that Rust calls when the Future is ready.  We need to hold a reference
to that object or else the JVM will finalize it and when Rust tries to
invoke the callback bad things will happen. In the unit tests this
causes them to stall because the Kotlin coroutine continutation is never
completed.

The `callbackHolder` variable was intended to hold a reference, but it
doesn't seem to be working (see [arg0d's analysis](https://github.com/mozilla/uniffi-rs/pull/1677#issuecomment-1660663156)).
I belive the issue is that variable isn't actually used anywhere so
Kotlin frees it before the coroutine has completed and the reason #1684
fixes the issue is that it uses the variable in the completion handler
(https://github.com/mozilla/uniffi-rs/pull/1684/files#diff-f564b605a6ba32d19bcb47128dc9c5c2f01169e2a41fa60ffaaf0b6eea13845cR34)

This commit changes things so instead of using a local variable, there's
a global set that stores all active callbacks.  It seems to fix the
issue for me when using arg0d's `repeat(1000) { ... }` test.
2023-08-07 12:01:06 -04:00
bendk 02ada515ef
Merge pull request #1671 from bendk/library-mode-crate-types
Allow different crate types when using library mode
2023-08-07 10:57:23 -04:00
Mark Hammond 5497ae8118
Combine the 'external-types' and 'ext-types' fixtures. (#1676)
The 'external-types' fixture was testing that a type defined in
another crate could be used in UDL in this crate. It was a little
confusing to have two fixtures with such similar names, so the
two fixtures are now one.
2023-08-02 14:58:15 -04:00
Omar 7b18fa7873 python: Decorate the `__init__` methods with `@typing.no_type_check` to get
around some of the limitations with default values in python.
2023-08-01 14:18:23 +02:00
Omar e7a6808398 python: add type hints for enum sub-classes and sub-class `__init__`
methods.
2023-08-01 14:18:23 +02:00
Omar 2e5021df23 python: Add type hints to `__init__` methods on records 2023-08-01 14:18:23 +02:00
Omar 1e02615518 python: Add type hints to records 2023-08-01 14:18:23 +02:00
Omar 7e461c74e2 python: Add return types to top-level functions for better IDE
support
2023-08-01 14:18:23 +02:00
Jan-Erik Rediger 8b0563c9c9 Add changelog entry 2023-08-01 13:20:38 +02:00
Jan-Erik Rediger c580d2edbe uniffi_macros: Force-include the `Cargo.toml` to read 2023-08-01 13:20:38 +02:00
Mark Hammond b0f85597ec
Include why we paniced in our panic message. (#1681) 2023-07-31 11:36:34 -04:00
Praveen Perera b227d3966d
Update docs for proc-macro constructors (#1679) 2023-07-27 13:58:57 -04:00
Mark Hammond 93634167f2
Allow procmacros to define custom types (#1661) 2023-07-27 10:47:42 -04:00
Jan-Erik Rediger e78018b464 Hack: Copy metadata module to avoid uniffi_core dependency
In m-c we're running into linking issues in `uniffi_macros`,
where it's missing symbols coming from `uniffi_core`:

    /bin/ld: uniffi_foreign_executor_callback_set: undefined version:
    /bin/ld: failed to set dynamic section sizes: bad value

That's most likely this issue on Rust:
https://github.com/rust-lang/rust/issues/111888
It's called out that this likely actually broke because of another PR:
https://github.com/rust-lang/rust/pull/99944

Despite this bug there's a bit of an issue in UniFFI to begin with:
We're exporting `extern "C"` functions from a crate that is a dependency
of some other crates, including `uniffi_macros`, and thus the symbols
land in a part where they are not needed.

However for `uniffi_meta` in particular we don't need much from
`uniffi_core`, so for now we just copy the necessary bits to get it all
working.
2023-07-26 10:30:07 +02:00
Ben Dean-Kawamura 088e5658c9 Allow different crate types when using library mode
This makes library mode work when the dependent crate doesn't include
"lib" in their `crate-type` list.  I think that check was leftover from
older code and isn't needed anymore.
2023-07-25 19:34:58 -04:00
Mark Hammond 997691ce4b
Fix export of Swift custom types which don't have a foreign type (#1660) 2023-07-25 10:23:47 -04:00
bendk ff6c9c4dbd
Merge pull request #1663 from bendk/crate-mode-typo-fix
Fixed a typo from when library mode was named crate mode
2023-07-25 10:04:10 -04:00
heinrich5991 c62d34a225
Remove wrong, copy-pasted comment (#1664) 2023-07-25 09:38:55 -04:00
Ben Dean-Kawamura fd6d702153 Fixed a typo from when library mode was named crate mode 2023-07-24 15:00:15 -04:00
heinrich5991 d146cf6fc4
Test string edge cases for Kotlin and Swift (#1602)
* Test string edge cases for Kotlin and Swift

* Kotlin: Throw `CharacterCodingException` on invalid UTF-16 `String`

This exception is produced using
`CharsetEncoder.onUnmappableCharacter(CodingErrorAction.REPORT)`.
2023-07-24 14:51:13 -04:00
Mark Hammond 826ea99596
Remove almost all references to `DOMString` (#1659) 2023-07-24 10:05:38 -04:00
Mark Hammond 9c4405ec4a
Fix documentation so it doesn't use an invalid `&mut self` (#1655) 2023-07-20 15:24:52 -04:00
Mark Hammond b9b3c290a4
New example and docs for Futures (#1634)
Co-authored-by: Ivan Enderlin <ivan@mnt.io>
Co-authored-by: Jan-Erik Rediger <janerik@fnordig.de>
2023-07-19 11:30:19 -04:00
Jonas Platte 8565b7f941 proc-macro: Support default of None for struct fields 2023-07-13 22:49:18 +02:00
Jonas Platte b7d7bbd4a2 Relax async_runtime check on impl blocks
It used to error out if there were any non-async functions, now it only
errors if there are no async functions in the impl block.
2023-07-11 16:13:13 +02:00
Jonas Platte b18d6b7201 Disallow async for traits
It won't work anyways, checking it in scaffolding generation means that
users will discover this earlier.
2023-07-11 16:13:13 +02:00
Jonas Platte c9b9ae00b3 Abort earlier on invalid / unsupported ImplItem
Dealing with lists of results wasn't actually used for more fine-grained
error handling, and was being somewhat annoying.
2023-07-11 16:13:13 +02:00
Jonas Platte f7de79b4a9 Enforce that constructors aren't async in scaffolding generation 2023-07-11 16:13:13 +02:00