Skip to content

Update 'code_writers' to pass marshaling info#2318

Merged
Sergio0694 merged 4 commits intouser/sergiopedri/optimize-agile-refs2from
user/sergiopedri/cswinrt-marshaling-type
Mar 8, 2026
Merged

Update 'code_writers' to pass marshaling info#2318
Sergio0694 merged 4 commits intouser/sergiopedri/optimize-agile-refs2from
user/sergiopedri/cswinrt-marshaling-type

Conversation

@Sergio0694
Copy link
Member

Follow up to #2255.

Sergio0694 and others added 4 commits March 7, 2026 22:44
Add get_marshaling_type_name() that reads the MarshalingBehaviorAttribute
from a TypeDef and returns the corresponding C# CreateObjectReferenceMarshalingType
enum value string for code generation. Maps MarshalingType.Agile (2) and
MarshalingType.Standard (3) to their respective enum members, defaulting to
Unknown for all other cases.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Pass CreateObjectReferenceMarshalingType to base constructor calls in
write_factory_constructors for both parameterized (callback-based) and
parameterless (DerivedSealed) factory activation patterns.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Pass CreateObjectReferenceMarshalingType to base constructor calls in
write_composable_constructors for both parameterless (DerivedComposed)
and parametered (callback-based) patterns. Also update all four protected
pass-through constructors to accept and forward the marshalingType parameter.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Pass CreateObjectReferenceMarshalingType to CreateObjectReference and
CreateObjectReferenceUnsafe calls in write_class_comwrappers_marshaller_attribute
and write_class_comwrappers_callback (sealed and unsealed paths, 4 call sites).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@Sergio0694 Sergio0694 merged commit 6aef353 into user/sergiopedri/optimize-agile-refs2 Mar 8, 2026
15 checks passed
@Sergio0694 Sergio0694 deleted the user/sergiopedri/cswinrt-marshaling-type branch March 8, 2026 19:55
Sergio0694 added a commit that referenced this pull request Mar 8, 2026
* Add helper to read MarshalingBehaviorAttribute from .winmd metadata

Add get_marshaling_type_name() that reads the MarshalingBehaviorAttribute
from a TypeDef and returns the corresponding C# CreateObjectReferenceMarshalingType
enum value string for code generation. Maps MarshalingType.Agile (2) and
MarshalingType.Standard (3) to their respective enum members, defaulting to
Unknown for all other cases.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Update factory constructor generation with marshalingType parameter

Pass CreateObjectReferenceMarshalingType to base constructor calls in
write_factory_constructors for both parameterized (callback-based) and
parameterless (DerivedSealed) factory activation patterns.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Update composable constructor generation with marshalingType parameter

Pass CreateObjectReferenceMarshalingType to base constructor calls in
write_composable_constructors for both parameterless (DerivedComposed)
and parametered (callback-based) patterns. Also update all four protected
pass-through constructors to accept and forward the marshalingType parameter.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Update ComWrappers marshaller/callback with marshalingType parameter

Pass CreateObjectReferenceMarshalingType to CreateObjectReference and
CreateObjectReferenceUnsafe calls in write_class_comwrappers_marshaller_attribute
and write_class_comwrappers_callback (sealed and unsealed paths, 4 call sites).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sergio0694 added a commit that referenced this pull request Mar 11, 2026
* Add helper to read MarshalingBehaviorAttribute from .winmd metadata

Add get_marshaling_type_name() that reads the MarshalingBehaviorAttribute
from a TypeDef and returns the corresponding C# CreateObjectReferenceMarshalingType
enum value string for code generation. Maps MarshalingType.Agile (2) and
MarshalingType.Standard (3) to their respective enum members, defaulting to
Unknown for all other cases.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Update factory constructor generation with marshalingType parameter

Pass CreateObjectReferenceMarshalingType to base constructor calls in
write_factory_constructors for both parameterized (callback-based) and
parameterless (DerivedSealed) factory activation patterns.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Update composable constructor generation with marshalingType parameter

Pass CreateObjectReferenceMarshalingType to base constructor calls in
write_composable_constructors for both parameterless (DerivedComposed)
and parametered (callback-based) patterns. Also update all four protected
pass-through constructors to accept and forward the marshalingType parameter.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Update ComWrappers marshaller/callback with marshalingType parameter

Pass CreateObjectReferenceMarshalingType to CreateObjectReference and
CreateObjectReferenceUnsafe calls in write_class_comwrappers_marshaller_attribute
and write_class_comwrappers_callback (sealed and unsealed paths, 4 call sites).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sergio0694 added a commit that referenced this pull request Mar 13, 2026
* Add helper to read MarshalingBehaviorAttribute from .winmd metadata

Add get_marshaling_type_name() that reads the MarshalingBehaviorAttribute
from a TypeDef and returns the corresponding C# CreateObjectReferenceMarshalingType
enum value string for code generation. Maps MarshalingType.Agile (2) and
MarshalingType.Standard (3) to their respective enum members, defaulting to
Unknown for all other cases.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Update factory constructor generation with marshalingType parameter

Pass CreateObjectReferenceMarshalingType to base constructor calls in
write_factory_constructors for both parameterized (callback-based) and
parameterless (DerivedSealed) factory activation patterns.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Update composable constructor generation with marshalingType parameter

Pass CreateObjectReferenceMarshalingType to base constructor calls in
write_composable_constructors for both parameterless (DerivedComposed)
and parametered (callback-based) patterns. Also update all four protected
pass-through constructors to accept and forward the marshalingType parameter.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Update ComWrappers marshaller/callback with marshalingType parameter

Pass CreateObjectReferenceMarshalingType to CreateObjectReference and
CreateObjectReferenceUnsafe calls in write_class_comwrappers_marshaller_attribute
and write_class_comwrappers_callback (sealed and unsealed paths, 4 call sites).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sergio0694 added a commit that referenced this pull request Mar 14, 2026
…2255)

* Add CreateObjectReferenceMarshalingType enum

Introduce an internal enum CreateObjectReferenceMarshalingType to specify the marshaling behavior when creating WindowsRuntimeObjectReference instances. Defines Unknown, Agile, and Standard values and includes XML doc remarks linking to Windows.Foundation.Metadata.MarshalingType for clarity.

* Add marshaling type support to object reference creation

Add a marshalingType parameter to WindowsRuntimeObjectReference.InitializeObjectReference and InitializeObjectReferenceUnsafe to enable fast-path decisions (Agile/Standard) and avoid expensive runtime checks when metadata is available. Update free-threaded checks to use a boolean result derived from HRESULTs and adjust error handling and ownership semantics accordingly. Add overloads to WindowsRuntimeComWrappersMarshal that accept MarshalingType (mapped via CreateObjectReferenceMarshalingType.FromMetadata) and annotate platform support; update wrapperFlags assignment and XML docs. Also add required usings and pragmas to silence analyzer warnings.

* Use marshalingType to optimize threading check

Add a CreateObjectReferenceMarshalingType parameter to creation helpers and branch on it to avoid expensive runtime queries when possible. Fast-paths for Agile and Standard are used to set free-threaded semantics, while the default path calls ComObjectHelpers.IsFreeThreadedUnsafe and preserves previous failure handling (including releasing the inner unknown and throwing). Refactor uses a bool isFreeThreaded instead of HRESULT comparisons and clean up related comments and XML docs (including a small typo fix). This reduces runtime overhead for known marshaling annotations and centralizes the free-threaded decision logic.

* Introduce CreateObjectReferenceMarshalingType enum

Add a local CreateObjectReferenceMarshalingType enum (public but marked Obsolete and EditorBrowsable(Never)) to represent marshaling kinds and replace usages of Windows.Foundation.Metadata.MarshalingType. Update WindowsRuntimeComWrappersMarshal.CreateObjectReference/CreateObjectReferenceUnsafe to accept the new enum and pass it through to WindowsRuntimeObjectReference initialization. Propagate the marshalingType parameter through WindowsRuntimeObject constructors and activation paths so the value is forwarded when creating object references. Remove the direct dependency on Windows.Foundation.Metadata and adjust XML docs/links accordingly.

* Add marshaling type validation and feature switch

Introduce runtime validation for declared CreateObjectReferenceMarshalingType values. Adds ComObjectHelpers.ValidateMarshalingType (with aggressive inlining and a NoInlining helper) to check IsFreeThreadedUnsafe and FailFast on mismatches, and wire calls to this validator in WindowsRuntimeObjectReference initialization paths. Add a new feature switch (CSWINRT_ENABLE_MARSHALING_TYPE_VALIDATION / EnableMarshalingTypeValidation) to toggle the behavior (defaults to false) and add the required using for System.Runtime.CompilerServices.

* Fix typo

* Use hresult.Failed property

Replace calls to hresult.Failed() with the hresult.Failed property in WindowsRuntimeObjectReference.Initialization.cs. Updated the failure checks around COM aggregation handling and the free-threaded interface release to match the HRESULT API change.

* Release acquired COM refs on failure

On initialization failure, explicitly release any acquired COM interface pointers to avoid leaking references: call ReleaseUnsafe on acquiredNewInstanceUnknown (if non-null) and on acquiredInnerInstanceUnknown. The previous long AddRef/Release commentary was moved and condensed below to clarify ownership semantics and that no AddRef is required in the common optimized paths. This change ensures proper cleanup on early returns and reduces lifetime-management complexity.

* Update 'code_writers' to pass marshaling info (#2318)

* Add helper to read MarshalingBehaviorAttribute from .winmd metadata

Add get_marshaling_type_name() that reads the MarshalingBehaviorAttribute
from a TypeDef and returns the corresponding C# CreateObjectReferenceMarshalingType
enum value string for code generation. Maps MarshalingType.Agile (2) and
MarshalingType.Standard (3) to their respective enum members, defaulting to
Unknown for all other cases.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Update factory constructor generation with marshalingType parameter

Pass CreateObjectReferenceMarshalingType to base constructor calls in
write_factory_constructors for both parameterized (callback-based) and
parameterless (DerivedSealed) factory activation patterns.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Update composable constructor generation with marshalingType parameter

Pass CreateObjectReferenceMarshalingType to base constructor calls in
write_composable_constructors for both parameterless (DerivedComposed)
and parametered (callback-based) patterns. Also update all four protected
pass-through constructors to accept and forward the marshalingType parameter.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Update ComWrappers marshaller/callback with marshalingType parameter

Pass CreateObjectReferenceMarshalingType to CreateObjectReference and
CreateObjectReferenceUnsafe calls in write_class_comwrappers_marshaller_attribute
and write_class_comwrappers_callback (sealed and unsealed paths, 4 call sites).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Use implicit enum values in marshaling enum

Remove explicit numeric assignments for Agile and Standard in CreateObjectReferenceMarshalingType so the enum uses default sequential values. This is a syntactic cleanup with no behavioral change; file also ends without a trailing newline.

* Add 'EnableMarshalingTypeMetadataSupport' feature switch

Add a new 'CSWINRT_ENABLE_MARSHALING_TYPE_METADATA_SUPPORT' feature switch
(defaults to 'true') that controls whether marshaling type metadata from WinMD
files is used to optimize object reference creation. When disabled, the runtime
will always query the marshaling type at runtime instead of trusting metadata.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Override marshaling type when metadata support is disabled

When 'EnableMarshalingTypeMetadataSupport' is set to false, override the
input marshaling type to 'Unknown' in all three initialization methods.
This forces the runtime to always query the marshaling type at runtime,
rather than trusting the metadata annotation from the WinMD file.

The override is placed after the validation call, so that marshaling type
validation (if enabled) still sees the original metadata value.

Co-Authored-By: Copilot <223556219+Copilot@users.noreply.github.com>

* Add .targets entries for marshaling type feature switches

Add MSBuild property defaults and RuntimeHostConfigurationOption entries
for both new feature switches introduced in this PR:

- 'CsWinRTEnableMarshalingTypeValidation' (default: false) maps to the
  'CSWINRT_ENABLE_MARSHALING_TYPE_VALIDATION' runtime switch
- 'CsWinRTEnableMarshalingTypeMetadataSupport' (default: true) maps to
  the 'CSWINRT_ENABLE_MARSHALING_TYPE_METADATA_SUPPORT' runtime switch

This follows the same pattern as all other CsWinRT feature switches,
with both a PropertyGroup default and a RuntimeHostConfigurationOption
for IL trimming support.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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