Skip to content

Commit

Permalink
Apply feedbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
buyaa-n committed Apr 27, 2023
1 parent e8414ba commit 6eb9a3e
Show file tree
Hide file tree
Showing 7 changed files with 512 additions and 501 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<Compile Include="System\Reflection\Emit\MethodBuilderImpl.cs" />
<Compile Include="System\Reflection\Emit\ModuleBuilderImpl.cs" />
<Compile Include="System\Reflection\Emit\ParameterBuilderImpl.cs" />
<Compile Include="System\Reflection\Emit\PseudoCustomAttributesData.cs" />
<Compile Include="System\Reflection\Emit\TypeBuilderImpl.cs" />
<Compile Include="System\Reflection\Emit\SignatureHelper.cs" />
</ItemGroup>
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal sealed class FieldBuilderImpl : FieldBuilder
private readonly Type _fieldType;
private FieldAttributes _attributes;

internal MarshallingInfo? _marshallingInfo;
internal MarshallingData? _marshallingData;
internal int _offset;
internal List<CustomAttributeWrapper>? _customAttributes;

Expand Down Expand Up @@ -52,7 +52,7 @@ protected override void SetCustomAttributeCore(ConstructorInfo con, ReadOnlySpan
return;
case "System.Runtime.InteropServices.MarshalAsAttribute":
_attributes |= FieldAttributes.HasFieldMarshal;
_marshallingInfo = MarshallingInfo.ParseMarshallingInfo(con, binaryAttribute, isField : true);
_marshallingData = MarshallingData.CreateMarshallingData(con, binaryAttribute, isField : true);
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ protected override ParameterBuilder DefineParameterCore(int position, ParameterA
if (position > 0 && (_parameterTypes == null || position > _parameterTypes.Length))
throw new ArgumentOutOfRangeException(SR.ArgumentOutOfRange_ParamSequence);

if (position == 0 && _parameters == null)
{
_parameters = new ParameterBuilderImpl[1];
}

Debug.Assert(_parameters != null);
attributes &= ~ParameterAttributes.ReservedMask;
ParameterBuilderImpl parameter = new ParameterBuilderImpl(this, position, attributes, strParamName);
Expand Down Expand Up @@ -155,103 +160,4 @@ public override object Invoke(object? obj, BindingFlags invokeAttr, Binder? bind
public override MethodInfo MakeGenericMethod(params System.Type[] typeArguments)
=> throw new NotImplementedException();
}

internal sealed class DllImportData
{
private readonly string _moduleName;
private readonly string? _entryPoint;
private readonly MethodImportAttributes _flags;
internal DllImportData(string moduleName, string? entryPoint, MethodImportAttributes flags)
{
_moduleName = moduleName;
_entryPoint = entryPoint;
_flags = flags;
}

public string ModuleName => _moduleName;

public string? EntryPoint => _entryPoint;

public MethodImportAttributes Flags => _flags;

internal static DllImportData CreateDllImportData(CustomAttributeInfo attr, out bool preserveSig)
{
string? moduleName = (string?)attr._ctorArgs[0];
if (moduleName == null || moduleName.Length == 0)
{
throw new ArgumentException(SR.Argument_DllNameCannotBeEmpty);
}

MethodImportAttributes importAttributes = MethodImportAttributes.None;
string? entryPoint = null;
preserveSig = true;
for (int i = 0; i < attr._namedParamNames.Length; ++i)
{
string name = attr._namedParamNames[i];
object value = attr._namedParamValues[i]!;
switch (name)
{
case "PreserveSig":
preserveSig = (bool)value;
break;
case "CallingConvention":
importAttributes |= (CallingConvention)value switch
{
CallingConvention.Cdecl => MethodImportAttributes.CallingConventionCDecl,
CallingConvention.FastCall => MethodImportAttributes.CallingConventionFastCall,
CallingConvention.StdCall => MethodImportAttributes.CallingConventionStdCall,
CallingConvention.ThisCall => MethodImportAttributes.CallingConventionThisCall,
_=> MethodImportAttributes.CallingConventionWinApi // Roslyn defaults with this
};
break;
case "CharSet":
importAttributes |= (CharSet)value switch
{
CharSet.Ansi => MethodImportAttributes.CharSetAnsi,
CharSet.Auto => MethodImportAttributes.CharSetAuto,
CharSet.Unicode => MethodImportAttributes.CharSetUnicode,
_ => MethodImportAttributes.CharSetAuto
};
break;
case "EntryPoint":
entryPoint = (string?)value;
break;
case "ExactSpelling":
if ((bool)value)
{
importAttributes |= MethodImportAttributes.ExactSpelling;
}
break;
case "SetLastError":
if ((bool)value)
{
importAttributes |= MethodImportAttributes.SetLastError;
}
break;
case "BestFitMapping":
if ((bool)value)
{
importAttributes |= MethodImportAttributes.BestFitMappingEnable;
}
else
{
importAttributes |= MethodImportAttributes.BestFitMappingDisable;
}
break;
case "ThrowOnUnmappableChar":
if ((bool)value)
{
importAttributes |= MethodImportAttributes.ThrowOnUnmappableCharEnable;
}
else
{
importAttributes |= MethodImportAttributes.ThrowOnUnmappableCharDisable;
}
break;
}
}

return new DllImportData(moduleName, entryPoint, importAttributes);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,9 @@ internal void AppendMetadata()
WriteCustomAttributes(parameter._customAttributes, parameterHandle);
_nextParameterRowId++;

if (parameter._marshallingInfo != null)
if (parameter._marshallingData != null)
{
AddMarshalling(parameterHandle, parameter._marshallingInfo.PopulateMarshallingBlob(_metadataBuilder));
AddMarshalling(parameterHandle, parameter._marshallingData.SerializeMarshallingData());
}

if (parameter._defaultValue != DBNull.Value)
Expand Down Expand Up @@ -179,9 +179,9 @@ internal void AppendMetadata()
AddFieldLayout(fieldHandle, field._offset);
}

if (field._marshallingInfo != null)
if (field._marshallingData != null)
{
AddMarshalling(fieldHandle, field._marshallingInfo.PopulateMarshallingBlob(_metadataBuilder));
AddMarshalling(fieldHandle, field._marshallingData.SerializeMarshallingData());
}
}
}
Expand Down Expand Up @@ -303,8 +303,8 @@ private MemberReferenceHandle AddConstructorReference(TypeReferenceHandle parent
private void AddFieldLayout(FieldDefinitionHandle fieldHandle, int offset) =>
_metadataBuilder.AddFieldLayout(field: fieldHandle, offset: offset);

private void AddMarshalling(EntityHandle fieldHandle, BlobHandle descriptor) =>
_metadataBuilder.AddMarshallingDescriptor(fieldHandle, descriptor);
private void AddMarshalling(EntityHandle fieldHandle, BlobBuilder builder) =>
_metadataBuilder.AddMarshallingDescriptor(fieldHandle, _metadataBuilder.GetOrAddBlob(builder));

private ParameterHandle AddParameter(ParameterBuilderImpl parameter) =>
_metadataBuilder.AddParameter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ internal sealed class ParameterBuilderImpl : ParameterBuilder
private readonly MethodBuilderImpl _methodBuilder;
private ParameterAttributes _attributes;

internal List<CustomAttributeWrapper>? _customAttributes = new();
internal MarshallingInfo? _marshallingInfo;
internal List<CustomAttributeWrapper>? _customAttributes;
internal MarshallingData? _marshallingData;
internal object? _defaultValue = DBNull.Value;

public ParameterBuilderImpl(MethodBuilderImpl methodBuilder, int sequence, ParameterAttributes attributes, string? paramName)
Expand Down Expand Up @@ -47,7 +47,7 @@ protected override void SetCustomAttributeCore(ConstructorInfo con, ReadOnlySpan
return;
case "System.Runtime.InteropServices.MarshalAsAttribute":
_attributes |= ParameterAttributes.HasFieldMarshal;
_marshallingInfo = MarshallingInfo.ParseMarshallingInfo(con, binaryAttribute, isField: false);
_marshallingData = MarshallingData.CreateMarshallingData(con, binaryAttribute, isField: false);
return;
case "System.Runtime.InteropServices.DefaultParameterValueAttribute":
// MS.NET doesn't handle this attribute but we handle it for consistency TODO: not sure if we need to handle this
Expand Down

0 comments on commit 6eb9a3e

Please sign in to comment.