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

Implement strongly typed cast from/to JSValue #196

Merged
merged 13 commits into from
Aug 8, 2024

Conversation

vmoroz
Copy link
Member

@vmoroz vmoroz commented Feb 2, 2024

This PR implements checked and unchecked casting between JSValue and other types that implement the new IJSValue interface.

  • Added new IJSValue interface which is implemented by JSValue and types that wrap up JSValue such as JSArray, JSDate, etc.
  • The IJSValue interface new generic methods: Is<T>, As<T>, AsUnchecked<T>, and CastTo<T>.
  • These methods are implemented in derived structs with help of IJSValue static methods CanCreateFrom and CreateUnchecked. In case if we targeted .Net version is below version 7.0, it uses IJSValueShim helper class that binds to static methods using reflection.
  • All IJSValue derived types implement these casting instance methods and the helper static methods.
  • Explicit casting operators are changed to use the new JSValue.As<T> method.
  • New unit tests are added to test the new casting operations.

Please note that I could not find a good way to differentiate Proxy from Object and currently any JSValue that is an object can be casted to JSProxy.

@vmoroz vmoroz requested a review from jasongin February 2, 2024 22:51
@vmoroz vmoroz marked this pull request as draft February 2, 2024 22:52
src/NodeApi/JSObject.cs Outdated Show resolved Hide resolved
src/NodeApi/JSObject.cs Outdated Show resolved Hide resolved
src/NodeApi/IJSValue.cs Outdated Show resolved Hide resolved
src/NodeApi/IJSValue.cs Outdated Show resolved Hide resolved
src/NodeApi/IJSValue.cs Outdated Show resolved Hide resolved
src/NodeApi/IJSValue.cs Outdated Show resolved Hide resolved
src/NodeApi/JSObject.cs Outdated Show resolved Hide resolved
@vmoroz vmoroz force-pushed the PR/object_type_cast branch from ee71872 to 5c4e176 Compare August 2, 2024 18:53
@vmoroz vmoroz changed the title [DRAFT] Implement strongly typed case from/to JSValue Implement strongly typed case from/to JSValue Aug 5, 2024
@vmoroz vmoroz marked this pull request as ready for review August 5, 2024 02:20
@vmoroz vmoroz changed the title Implement strongly typed case from/to JSValue Implement strongly typed cast from/to JSValue Aug 5, 2024
src/NodeApi/IJSValue.cs Outdated Show resolved Hide resolved
src/NodeApi/JSValue.cs Show resolved Hide resolved
src/NodeApi/JSValue.cs Show resolved Hide resolved
src/NodeApi/JSValue.cs Outdated Show resolved Hide resolved
test/TestCases/napi-dotnet/jsvalue_cast.js Outdated Show resolved Hide resolved
src/NodeApi/IJSValue.cs Outdated Show resolved Hide resolved
src/NodeApi/IJSValue.cs Outdated Show resolved Hide resolved
src/NodeApi/IJSValue.cs Outdated Show resolved Hide resolved
@vmoroz vmoroz merged commit d52ad7c into microsoft:main Aug 8, 2024
14 checks passed
@vmoroz vmoroz deleted the PR/object_type_cast branch August 8, 2024 01:00
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.

2 participants