@@ -99,7 +99,7 @@ internal class LuaOptionalAttribute : Attribute;
9999 IsSpanByte = isSpanByte ,
100100 IsDelegate = param . Type . TypeKind == TypeKind . Delegate
101101 } ;
102- } ) . ToList ( ) ;
102+ } ) . ToArray ( ) ;
103103
104104 var returnIsDelegate = method . ReturnType . TypeKind == TypeKind . Delegate ;
105105
@@ -248,14 +248,15 @@ private static void EmitMethodExport(CodeWriter code, MethodExport method, List<
248248
249249 if ( method . IsOptional )
250250 {
251- code . Line ( $ "if ({ fieldName } == null)") ;
251+ code . Line ( $ "var { fieldName } _local = { fieldName } ;") ;
252+ code . Line ( $ "if ({ fieldName } _local == null)") ;
252253 code . OpenBrace ( ) ;
253254 code . Line ( $ "throw new InvalidOperationException(\" { method . MethodName } is not available.\" );") ;
254255 code . CloseBrace ( ) ;
255256 code . Line ( ) ;
256257 }
257258
258- EmitMethodBody ( code , method , fieldName ) ;
259+ EmitMethodBody ( code , method , method . IsOptional ? $ " { fieldName } _local" : fieldName ) ;
259260
260261 code . CloseBrace ( ) ;
261262 code . Line ( ) ;
@@ -578,25 +579,67 @@ private static string AccessibilityToString(Accessibility accessibility)
578579 } ;
579580 }
580581
581- private struct MethodExport
582+ private struct MethodExport : System . IEquatable < MethodExport >
582583 {
583584 public string Namespace ;
584585 public string TypeName ;
585586 public string MethodName ;
586587 public string ExportName ;
587588 public string ReturnType ;
588589 public bool ReturnsVoid ;
589- public List < ParamInfo > Parameters ;
590+ public ParamInfo [ ] Parameters ;
590591 public bool IsOptional ;
591592 public bool Throws ;
592593 public bool PCall ;
593594 public bool ReturnsLuaString ;
594595 public bool ReturnIsDelegate ;
595596 public string ? ReturnDelegateType ;
596597 public Accessibility Accessibility ;
598+
599+ public bool Equals ( MethodExport other )
600+ {
601+ return Namespace == other . Namespace
602+ && TypeName == other . TypeName
603+ && MethodName == other . MethodName
604+ && ExportName == other . ExportName
605+ && ReturnType == other . ReturnType
606+ && ReturnsVoid == other . ReturnsVoid
607+ && ParametersEqual ( Parameters , other . Parameters )
608+ && IsOptional == other . IsOptional
609+ && Throws == other . Throws
610+ && PCall == other . PCall
611+ && ReturnsLuaString == other . ReturnsLuaString
612+ && ReturnIsDelegate == other . ReturnIsDelegate
613+ && ReturnDelegateType == other . ReturnDelegateType
614+ && Accessibility == other . Accessibility ;
615+ }
616+
617+ public override bool Equals ( object ? obj )
618+ {
619+ return obj is MethodExport other && Equals ( other ) ;
620+ }
621+
622+ public override int GetHashCode ( )
623+ {
624+ var hash = StringHash ( Namespace ) ;
625+ hash = CombineHash ( hash , StringHash ( TypeName ) ) ;
626+ hash = CombineHash ( hash , StringHash ( MethodName ) ) ;
627+ hash = CombineHash ( hash , StringHash ( ExportName ) ) ;
628+ hash = CombineHash ( hash , StringHash ( ReturnType ) ) ;
629+ hash = CombineHash ( hash , ReturnsVoid . GetHashCode ( ) ) ;
630+ hash = CombineHash ( hash , ParametersHash ( Parameters ) ) ;
631+ hash = CombineHash ( hash , IsOptional . GetHashCode ( ) ) ;
632+ hash = CombineHash ( hash , Throws . GetHashCode ( ) ) ;
633+ hash = CombineHash ( hash , PCall . GetHashCode ( ) ) ;
634+ hash = CombineHash ( hash , ReturnsLuaString . GetHashCode ( ) ) ;
635+ hash = CombineHash ( hash , ReturnIsDelegate . GetHashCode ( ) ) ;
636+ hash = CombineHash ( hash , StringHash ( ReturnDelegateType ) ) ;
637+ hash = CombineHash ( hash , Accessibility . GetHashCode ( ) ) ;
638+ return hash ;
639+ }
597640 }
598641
599- private struct FieldExport
642+ private struct FieldExport : System . IEquatable < FieldExport >
600643 {
601644 public string Namespace ;
602645 public string TypeName ;
@@ -605,9 +648,37 @@ private struct FieldExport
605648 public string DelegateName ;
606649 public bool IsOptional ;
607650 public bool Throws ;
651+
652+ public bool Equals ( FieldExport other )
653+ {
654+ return Namespace == other . Namespace
655+ && TypeName == other . TypeName
656+ && FieldName == other . FieldName
657+ && ExportName == other . ExportName
658+ && DelegateName == other . DelegateName
659+ && IsOptional == other . IsOptional
660+ && Throws == other . Throws ;
661+ }
662+
663+ public override bool Equals ( object ? obj )
664+ {
665+ return obj is FieldExport other && Equals ( other ) ;
666+ }
667+
668+ public override int GetHashCode ( )
669+ {
670+ var hash = StringHash ( Namespace ) ;
671+ hash = CombineHash ( hash , StringHash ( TypeName ) ) ;
672+ hash = CombineHash ( hash , StringHash ( FieldName ) ) ;
673+ hash = CombineHash ( hash , StringHash ( ExportName ) ) ;
674+ hash = CombineHash ( hash , StringHash ( DelegateName ) ) ;
675+ hash = CombineHash ( hash , IsOptional . GetHashCode ( ) ) ;
676+ hash = CombineHash ( hash , Throws . GetHashCode ( ) ) ;
677+ return hash ;
678+ }
608679 }
609680
610- private struct ParamInfo
681+ private struct ParamInfo : System . IEquatable < ParamInfo >
611682 {
612683 public string Type ;
613684 public string Name ;
@@ -616,6 +687,86 @@ private struct ParamInfo
616687 public bool IsSpanChar ;
617688 public bool IsSpanByte ;
618689 public bool IsDelegate ;
690+
691+ public bool Equals ( ParamInfo other )
692+ {
693+ return Type == other . Type
694+ && Name == other . Name
695+ && RefKind == other . RefKind
696+ && MarshalAs == other . MarshalAs
697+ && IsSpanChar == other . IsSpanChar
698+ && IsSpanByte == other . IsSpanByte
699+ && IsDelegate == other . IsDelegate ;
700+ }
701+
702+ public override bool Equals ( object ? obj )
703+ {
704+ return obj is ParamInfo other && Equals ( other ) ;
705+ }
706+
707+ public override int GetHashCode ( )
708+ {
709+ var hash = StringHash ( Type ) ;
710+ hash = CombineHash ( hash , StringHash ( Name ) ) ;
711+ hash = CombineHash ( hash , RefKind . GetHashCode ( ) ) ;
712+ hash = CombineHash ( hash , StringHash ( MarshalAs ) ) ;
713+ hash = CombineHash ( hash , IsSpanChar . GetHashCode ( ) ) ;
714+ hash = CombineHash ( hash , IsSpanByte . GetHashCode ( ) ) ;
715+ hash = CombineHash ( hash , IsDelegate . GetHashCode ( ) ) ;
716+ return hash ;
717+ }
718+ }
719+
720+ private static bool ParametersEqual ( ParamInfo [ ] ? left , ParamInfo [ ] ? right )
721+ {
722+ if ( ReferenceEquals ( left , right ) )
723+ {
724+ return true ;
725+ }
726+
727+ if ( left == null || right == null || left . Length != right . Length )
728+ {
729+ return false ;
730+ }
731+
732+ for ( var i = 0 ; i < left . Length ; i ++ )
733+ {
734+ if ( ! left [ i ] . Equals ( right [ i ] ) )
735+ {
736+ return false ;
737+ }
738+ }
739+
740+ return true ;
741+ }
742+
743+ private static int ParametersHash ( ParamInfo [ ] ? parameters )
744+ {
745+ if ( parameters == null )
746+ {
747+ return 0 ;
748+ }
749+
750+ var hash = 17 ;
751+ foreach ( var parameter in parameters )
752+ {
753+ hash = CombineHash ( hash , parameter . GetHashCode ( ) ) ;
754+ }
755+
756+ return hash ;
757+ }
758+
759+ private static int StringHash ( string ? value )
760+ {
761+ return value ? . GetHashCode ( ) ?? 0 ;
762+ }
763+
764+ private static int CombineHash ( int left , int right )
765+ {
766+ unchecked
767+ {
768+ return ( left * 397 ) ^ right ;
769+ }
619770 }
620771
621772 private readonly struct InitEntry ( string fieldName , string delegateName , string exportName , bool isOptional , bool throws )
0 commit comments