Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ValueTask and ValueTask<T> to Promise conversion #1744

Merged
merged 2 commits into from Jan 17, 2024

Conversation

wazzamatazz
Copy link
Contributor

@wazzamatazz wazzamatazz commented Jan 17, 2024

This PR extends the work done in #1567 to add support for converting ValueTask and ValueTask<T> to promises.

This support is only available for .NET Standard 2.1 and .NET (non-Framework) targets. Support for .NET Framework and .NET Standard 2.0 targets could be enabled by adding a reference to System.Threading.Tasks.Extensions.

Adds new `IsAwaitable` and `ConvertAwaitableToPromise` methods.

`IsAwaitable` checks if a result is a `Task`. For .NET Standard 2.1/.NET targets, the method also checks if a result is `ValueTask` or `ValueTask<T>`.

If a result is awaitable, `Call` now calls `ConvertAwaitableToPromise`. This method makes use of the existing `ConvertTaskToPromise` method for `Task`-based results. For .NET Standard 2.1/.NET targets, it also handles conversion of `ValueTask`/`ValueTask<T>` results into `Task`/`Task<T>` before calling `ConvertTaskToPromise`.
Copy link
Collaborator

@lahma lahma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the clean and concise PR!

@lahma lahma merged commit 45fca04 into sebastienros:main Jan 17, 2024
3 checks passed
@wazzamatazz
Copy link
Contributor Author

wazzamatazz commented Jan 17, 2024

Would you be adverse to adding the System.Threading.Tasks.Extensions reference for .NET Framework and .NET Standard targets? Otherwise I can see the potential for confusion if someone is targeting .NET Framework and using ValueTask but finds that it doesn't work with Jint.

I'm happy to work up another PR to do this btw.

@lahma
Copy link
Collaborator

lahma commented Jan 17, 2024

Not sure if ValueTask is commonly used under .NET Framework, it's more targeted to modern .NET runtime. Maybe we should wait and see if there's some feedback suggesting that it is needed.

@wazzamatazz wazzamatazz deleted the valuetask-interop branch January 17, 2024 17:09
lahma pushed a commit to lahma/jint that referenced this pull request Jan 19, 2024
* Support ValueTask/ValueTask<T> in modern targets

Adds new `IsAwaitable` and `ConvertAwaitableToPromise` methods.

`IsAwaitable` checks if a result is a `Task`. For .NET Standard 2.1/.NET targets, the method also checks if a result is `ValueTask` or `ValueTask<T>`.

If a result is awaitable, `Call` now calls `ConvertAwaitableToPromise`. This method makes use of the existing `ConvertTaskToPromise` method for `Task`-based results. For .NET Standard 2.1/.NET targets, it also handles conversion of `ValueTask`/`ValueTask<T>` results into `Task`/`Task<T>` before calling `ConvertTaskToPromise`.

* Add ValueTask/ValueTask<T> tests
scgm0 pushed a commit to scgm0/jint that referenced this pull request Jan 25, 2024
* Support ValueTask/ValueTask<T> in modern targets

Adds new `IsAwaitable` and `ConvertAwaitableToPromise` methods.

`IsAwaitable` checks if a result is a `Task`. For .NET Standard 2.1/.NET targets, the method also checks if a result is `ValueTask` or `ValueTask<T>`.

If a result is awaitable, `Call` now calls `ConvertAwaitableToPromise`. This method makes use of the existing `ConvertTaskToPromise` method for `Task`-based results. For .NET Standard 2.1/.NET targets, it also handles conversion of `ValueTask`/`ValueTask<T>` results into `Task`/`Task<T>` before calling `ConvertTaskToPromise`.

* Add ValueTask/ValueTask<T> tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants