Skip to content

Conversation

@Sergio0694
Copy link
Member

Title. Also fixes some small bugs around the vtables for IDictionary<,> types.

Examples of codegen:

image

Note

Draft because there's some commits to move and this branch also needs to be rebased.

Replaced hardcoded HSTRING types in delegate comments with <ELEMENT_TYPE>, <KEY_TYPE>, and <VALUE_TYPE> placeholders to generalize the function pointer signatures for broader applicability. This improves clarity and maintainability when generating code for different types.
Changed a comment to indicate that arguments are loaded inside an outer 'try/finally' block instead of 'try/catch', improving code clarity.
Inserted the Conv_U opcode in both IAsyncInfoMethods and IVectorViewMethods to ensure correct type conversion during interop method generation. This change improves type safety and correctness in the generated interop code.
Introduces a local variable 'elementAbiType' to store the ABI type of the element and uses it consistently when defining locals and method signatures for the 'GetAt' method. This improves code clarity and ensures the correct ABI type is used throughout the method generation process.
Moved the construction of the HasKey method for IMapView<K,V> from InteropTypeDefinitionBuilder to a new InteropMethodDefinitionFactory.IMapViewMethods class. This centralizes method creation logic and enables more maintainable and reusable code for interop method definitions.
Refactored Lookup method creation in InteropTypeDefinitionBuilder to use InteropMethodDefinitionFactory.IMapViewMethods.Lookup. Added full implementation of the Lookup method in InteropMethodDefinitionFactory, including method body, local variables, and parameter/return value rewriting logic for IMapView<K, V> interop.
Replaced manual construction of HasKey and Lookup methods with calls to InteropMethodDefinitionFactory.IMapViewMethods. This improves maintainability and centralizes method creation logic.
Moved the Insert method definition for IDictionary2 from InteropTypeDefinitionBuilder to a new InteropMethodDefinitionFactory.IMapMethods helper. This centralizes Insert method creation logic and improves maintainability by encapsulating method body generation and parameter marshaling.
Refactored the Remove method definition for IMap<TKey, TValue> interop types to use a new factory method. The new implementation generates a complete method body with proper marshaling, exception handling, and resource cleanup, improving maintainability and correctness.
Replaces valueType with valueAbiType in the call to WellKnownTypeSignatureFactory.IReadOnlyDictionary2LookupImpl to ensure the correct ABI type is used for the value parameter.
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.

Pull request overview

This PR implements full code generation for IMapViewMethods and IMapMethods types for dictionaries in the WinRT interop generator. The changes enable proper vtable handling and method emission for IDictionary<K,V> and IReadOnlyDictionary<K,V> interfaces.

Key Changes:

  • Added complete implementations for IMapViewMethods.HasKey and IMapViewMethods.Lookup methods
  • Added complete implementations for IMapMethods.Insert and IMapMethods.Remove methods
  • Fixed vtable type selection bugs for IDictionary<K,V> interfaces
  • Improved ABI type handling in various interop method factories

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
InteropGenerator.Emit.cs Added missing emitState parameter when defining IReadOnlyDictionary types
WellKnownTypeDefinitionFactory.cs Updated comments to use generic type placeholders instead of HSTRING
InteropMethodDefinitionFactory.IVectorViewMethods.cs Fixed ABI type handling by extracting elementAbiType once and using consistently
InteropMethodDefinitionFactory.IMapViewMethods.cs New file implementing HasKey and Lookup methods for IMapView interfaces
InteropMethodDefinitionFactory.IMapMethods.cs New file implementing Insert and Remove methods for IMap interfaces
InteropMethodDefinitionFactory.IAsyncInfoMethods.cs Added missing Conv_U instruction before vtable method call
InteropTypeDefinitionBuilder.IReadOnlyDictionary2.cs Replaced placeholder implementations with actual method factory calls
InteropTypeDefinitionBuilder.IDictionary2.cs Fixed vtable type references and replaced placeholder implementations
InteropTypeDefinitionBuilder.Delegate.cs Corrected comment from 'try/catch' to 'try/finally'

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

}

/// <summary>
/// Creates a <see cref="MethodDefinition"/> for the <c>Remove</c> method for some <c>IMaplt;K, V&gt;</c> interface.
Copy link

Copilot AI Dec 22, 2025

Choose a reason for hiding this comment

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

Corrected 'IMaplt;K' to 'IMap<K' in XML documentation.

Suggested change
/// Creates a <see cref="MethodDefinition"/> for the <c>Remove</c> method for some <c>IMaplt;K, V&gt;</c> interface.
/// Creates a <see cref="MethodDefinition"/> for the <c>Remove</c> method for some <c>IMap&lt;K, V&gt;</c> interface.

Copilot uses AI. Check for mistakes.

// Track rewriting the return value for this method
emitState.TrackNativeParameterMethodRewrite(
paraneterType: keyType,
Copy link

Copilot AI Dec 22, 2025

Choose a reason for hiding this comment

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

Corrected spelling of 'paraneterType' to 'parameterType'.

Suggested change
paraneterType: keyType,
parameterType: keyType,

Copilot uses AI. Check for mistakes.

// Track rewriting the 'key' parameter for this method
emitState.TrackNativeParameterMethodRewrite(
paraneterType: keyType,
Copy link

Copilot AI Dec 22, 2025

Choose a reason for hiding this comment

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

Corrected spelling of 'paraneterType' to 'parameterType'.

Suggested change
paraneterType: keyType,
parameterType: keyType,

Copilot uses AI. Check for mistakes.

// Track rewriting the two parameters for this method
emitState.TrackNativeParameterMethodRewrite(
paraneterType: keyType,
Copy link

Copilot AI Dec 22, 2025

Choose a reason for hiding this comment

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

Corrected spelling of 'paraneterType' to 'parameterType'.

Copilot uses AI. Check for mistakes.
parameterIndex: 1);

emitState.TrackNativeParameterMethodRewrite(
paraneterType: valueType,
Copy link

Copilot AI Dec 22, 2025

Choose a reason for hiding this comment

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

Corrected spelling of 'paraneterType' to 'parameterType'.

Copilot uses AI. Check for mistakes.

// Track rewriting the return value for this method
emitState.TrackNativeParameterMethodRewrite(
paraneterType: keyType,
Copy link

Copilot AI Dec 22, 2025

Choose a reason for hiding this comment

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

Corrected spelling of 'paraneterType' to 'parameterType'.

Suggested change
paraneterType: keyType,
parameterType: keyType,

Copilot uses AI. Check for mistakes.

// Add and implement the 'Lookup' method
mapViewMethodsType.AddMethodImplementation(
declaration: interopReferences.IMapViewMethodsImpl2HasKey(keyType, valueType).Import(module),
Copy link

Copilot AI Dec 22, 2025

Choose a reason for hiding this comment

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

The 'Lookup' method is incorrectly implemented against the 'HasKey' interface declaration. This should use 'IMapViewMethodsImpl2Lookup' instead of 'IMapViewMethodsImpl2HasKey'.

Suggested change
declaration: interopReferences.IMapViewMethodsImpl2HasKey(keyType, valueType).Import(module),
declaration: interopReferences.IMapViewMethodsImpl2Lookup(keyType, valueType).Import(module),

Copilot uses AI. Check for mistakes.
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