Skip to content
This repository was archived by the owner on Apr 27, 2026. It is now read-only.

JavaScript usage documentation improvements #3025

Merged
jonsequitur merged 3 commits into
dotnet:mainfrom
jonsequitur:docs-improvements-2023-06-08
Jun 9, 2023
Merged

JavaScript usage documentation improvements #3025
jonsequitur merged 3 commits into
dotnet:mainfrom
jonsequitur:docs-improvements-2023-06-08

Conversation

@jonsequitur

Copy link
Copy Markdown
Contributor

This addresses #2928.

@jonsequitur
jonsequitur force-pushed the docs-improvements-2023-06-08 branch from aec8316 to 8047288 Compare June 8, 2023 22:58
<img width="503" alt="image" src="https://user-images.githubusercontent.com/547415/211661641-057e29fc-8048-4910-983f-14d8380dca8b.png">

One notable detail is that the type of the new C# variable has changed from the original declaration. It was originally declared as `int[]`, but after sharing the array back from JavaScript, the C# kernel has a variable called `array` which is of type `JsonDocument`. Because the JavaScript kernel runs in a different process from the .NET subkernels, sharing happens via JSON serialization. Complex types shared from JavaScript to .NET are sent as JSON and deserialized using `System.Text.Json`. There are a few simple types that will be shared as their intuitive .NET counterparts.
One notable detail here is that the type of the new C# variable has changed from the original declaration. It was originally declared as `int[]`, but after sharing the array back from JavaScript, the C# kernel has a variable called `array` which is of type `JsonDocument`. Because the JavaScript kernel runs in a different process from the .NET subkernels, sharing happens via JSON serialization. Complex types shared from JavaScript to .NET are sent as JSON and deserialized using `System.Text.Json`. There are a few simple types that will be shared as their intuitive .NET counterparts.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should we change the screenshot for the above example to use #!set instead of #!share? That may work better since the sharing is by value by default with #!set (and since there will be a nice error if someone tries to share by reference for JavaScript).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good call.


```javascript
const doSomething = async () => {
// Your code here

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Just curious: Does some alternate approach exist that would allow us to execute some arbitrary piece of supplied JavaScript within a top-level scope? I am sure there is good reason to select the above approach - but was just curious if there are alternatives and what downsides these alternatives might have.

@shyamnamboodiripad shyamnamboodiripad left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks great 👍🏾

@jonsequitur
jonsequitur merged commit 3be49ef into dotnet:main Jun 9, 2023
@colombod colombod added the Area-Documentation Improvements or additions to documentation label Jun 13, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Area-Documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants