This repository has been archived by the owner on Mar 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Custom marshal attribute #25
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,9 @@ | ||
{ | ||
// Don't show the "Required assets are missing..." dialog on startup. We'll manage tasks.json / launch.json manually. | ||
"csharp.suppressBuildAssetsNotification": true | ||
"csharp.suppressBuildAssetsNotification": true, | ||
|
||
// We try to limit the length of code lines to 100 columns. | ||
"editor.rulers": [ | ||
100, | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
|
||
namespace NodeApi; | ||
|
||
/// <summary> | ||
/// Interface for a custom implementation of marshaling .NET types to and from JavaScript values. | ||
/// A type that implements this interface can be assigned to | ||
/// <see cref="JSMarshalAsAttribute.CustomMarshalType" />. | ||
/// </summary> | ||
public interface IJSMarshaler<T> | ||
{ | ||
JSValue MarshalToJS(T value); | ||
|
||
T MarshalFromJS(JSValue value); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
|
||
namespace NodeApi; | ||
|
||
/// <summary> | ||
/// Specifies marshalling behavior for <see cref="JSMarshalAsAttribute" />. | ||
/// </summary> | ||
public enum JSMarshal | ||
{ | ||
/// <summary> | ||
/// Default marshalling behavior. | ||
/// </summary> | ||
Default, | ||
|
||
/// <summary> | ||
/// Marshalling is handled by a custom marshaler type. When this is specified, | ||
/// <see cref="JSMarshalAsAttribute.CustomMarshalType" /> must also be set. | ||
/// </summary> | ||
CustomMarshaler, | ||
|
||
/// <summary> | ||
/// A .NET object is marshalled as a JS external value. External values may not | ||
/// be accessed by JS, but may be held and passed back to .NET code intact. | ||
/// </summary> | ||
ExternalObject, | ||
|
||
/// <summary> | ||
/// The object is marshalled by value, meaning all public properties are shallow-copied. | ||
/// This is the default marshalling behavior for .NET structs. | ||
/// </summary> | ||
ByValObject, | ||
|
||
/// <summary> | ||
/// The object is marshalled by reference, meaning only a proxy to the object is passed. | ||
/// This is the default marshalling behavior for .NET classes and interfaces. | ||
/// </summary> | ||
ByRefObject, | ||
|
||
/// <summary> | ||
/// Collection items are copied into a new collection in the target environment. | ||
/// This is the default marshalling behavior for .NET arrays. | ||
/// </summary> | ||
ByValCollection = ByValObject, | ||
|
||
/// <summary> | ||
/// The collection is marshalled by reference, meaning only a proxy to the collection is passed. | ||
/// This is the default marshalling behavior for .NET generic collections. | ||
/// </summary> | ||
ByRefCollection = ByRefObject, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
|
||
using System; | ||
|
||
namespace NodeApi; | ||
|
||
/// <summary> | ||
/// Specifies how .NET types are marshalled to and from JavaScript values. | ||
/// </summary> | ||
[AttributeUsage( | ||
AttributeTargets.Property | | ||
AttributeTargets.Parameter | | ||
AttributeTargets.ReturnValue)] | ||
public sealed class JSMarshalAsAttribute : Attribute | ||
{ | ||
/// <summary> | ||
/// Specifies how a .NET property, parameter, or return value is marshalled to | ||
/// and from a JavaScript value. | ||
/// </summary> | ||
/// <param name="marshal">One of the available marshaling options.</param> | ||
public JSMarshalAsAttribute(JSMarshal marshal) | ||
{ | ||
Value = marshal; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the marshaling behavior indicated by the attribute. | ||
/// </summary> | ||
public JSMarshal Value { get; } | ||
|
||
/// <summary> | ||
/// When <see cref="Value" /> is <see cref="JSMarshal.CustomMarshaler" />, gets the type | ||
/// that handles marshalling. The type must implement <see cref="IJSMarshaler{T}" />, | ||
/// where the type argument matches the type being of the property, parameter, or | ||
/// return value the <see cref="JSMarshalAsAttribute" /> is applied to. | ||
/// </summary> | ||
public Type? CustomMarshalType { get; init; } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought about that, but we'd still need a non-generic |
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we also add fields?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Later, if and when we support marshalling fields.
Fields are not supported now, but there's no reason they can't be marshalled in almost the same way as properties, along with an optimization for
const
fields. I don't think it's a priority now though.