1- using System . Runtime . InteropServices ;
1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Linq ;
4+ using System . Runtime . InteropServices ;
25
36namespace Binaryen . NET ;
47
@@ -28,102 +31,78 @@ public class BinaryenModule : IDisposable
2831
2932 [ DllImport ( Interop . CPPDLL , CallingConvention = CallingConvention . Cdecl ) ]
3033 private extern static void BinaryenAddFunctionImport (
31- IntPtr module ,
32- string internalName ,
33- string externalModuleName ,
34- string externalBaseName ,
35- IntPtr paramTypes ,
36- IntPtr resultTypes ) ;
34+ IntPtr module ,
35+ string internalName ,
36+ string externalModuleName ,
37+ string externalBaseName ,
38+ UIntPtr paramTypes ,
39+ UIntPtr resultTypes ) ;
3740
3841 [ DllImport ( Interop . CPPDLL , CallingConvention = CallingConvention . Cdecl ) ]
3942 private extern static IntPtr BinaryenAddFunction (
40- IntPtr module ,
41- string name ,
42- IntPtr paramTypes ,
43- IntPtr resultTypes ,
44- IntPtr [ ] varTypes ,
45- uint numVarTypes ,
46- IntPtr body ) ;
43+ IntPtr module ,
44+ string name ,
45+ UIntPtr paramTypes ,
46+ UIntPtr resultTypes ,
47+ UIntPtr [ ] varTypes ,
48+ uint numVarTypes ,
49+ IntPtr body ) ;
4750 #endregion
4851
49- /// <summary> Pointer to the native class. </summary>
50- internal IntPtr Handle { get ; set ; }
52+ internal IntPtr Handle { get ; private set ; }
5153 private bool _disposed ;
5254
53- /// <summary> Creates a new instance of <see cref="BinaryenModule"/>. </summary>
5455 public BinaryenModule ( ) => Handle = BinaryenModuleCreate ( ) ;
55-
56- /// <summary> Creates a new instance of <see cref="BinaryenModule"/> from the given pointer. </summary>
5756 internal BinaryenModule ( IntPtr handle ) => Handle = handle ;
58-
59- /// <summary> Finalizer. </summary>
6057 ~ BinaryenModule ( ) => Dispose ( ) ;
6158
62- /// <summary>
63- /// Creates a new instance of <see cref="BinaryenModule"/> from the given WAT string.
64- /// </summary>
65- /// <param name="wat"> The WAT string. </param>
6659 public static BinaryenModule Parse ( string wat ) => new BinaryenModule ( BinaryenModuleParse ( wat ) ) ;
6760
68- /// <summary> Adds a function export to the module. </summary >
69- public void AddFunctionExport ( string internalName , string externalName ) => BinaryenAddFunctionExport ( Handle , internalName , externalName ) ;
61+ public void AddFunctionExport ( string internalName , string externalName ) = >
62+ BinaryenAddFunctionExport ( Handle , internalName , externalName ) ;
7063
71- /// <summary> Adds a function import to the module. </summary>
7264 public void AddFunctionImport (
7365 string internalName ,
7466 string externalModuleName ,
7567 string externalBaseName ,
76- IEnumerable < BinaryenType > paramTypes ,
77- BinaryenType resultType )
68+ IEnumerable < BinaryenType > ? paramTypes = null ,
69+ BinaryenType ? resultType = null )
7870 {
79- // No types specified, use default types
80- if ( paramTypes . Count ( ) == 0 )
81- paramTypes = new BinaryenType [ ] { BinaryenType . None } ;
71+ paramTypes ??= new [ ] { BinaryenType . None } ;
72+ resultType ??= BinaryenType . None ;
8273
83- IntPtr paramTypesHandle = BinaryenType . Combine ( paramTypes . ToArray ( ) ) . Handle ;
74+ // Use BinaryenType.Create to combine multiple param types
75+ UIntPtr combinedParams = BinaryenType . Create ( paramTypes . ToArray ( ) ) . Handle ;
76+ UIntPtr combinedResult = resultType . Handle ;
8477
8578 BinaryenAddFunctionImport (
8679 Handle ,
8780 internalName ,
8881 externalModuleName ,
8982 externalBaseName ,
90- paramTypesHandle ,
91- resultType . Handle ) ;
83+ combinedParams ,
84+ combinedResult ) ;
9285 }
9386
94- /// <summary> Adds a function to the module. </summary>
9587 public IntPtr AddFunction (
9688 string name ,
97- IEnumerable < BinaryenType > paramTypes ,
98- BinaryenType resultType ,
99- IEnumerable < BinaryenType > localTypes ,
100- BinaryenExpression body )
89+ BinaryenExpression body ,
90+ IEnumerable < BinaryenType > ? paramTypes = null ,
91+ BinaryenType ? resultType = null ,
92+ IEnumerable < BinaryenType > ? localTypes = null )
10193 {
102- if ( paramTypes . Count ( ) == 0 )
103- paramTypes = new [ ] { BinaryenType . None } ;
104- if ( resultType == null )
105- resultType = BinaryenType . None ;
106-
107- IntPtr paramHandle = BinaryenType . Combine ( paramTypes . ToArray ( ) ) . Handle ;
108- IntPtr resultHandle = resultType . Handle ;
94+ paramTypes ??= Array . Empty < BinaryenType > ( ) ;
95+ resultType ??= BinaryenType . None ;
96+ localTypes ??= Array . Empty < BinaryenType > ( ) ;
10997
110- // locals must be passed as raw array of IntPtr
111- IntPtr [ ] localHandles = localTypes . Select ( t => t . Handle ) . ToArray ( ) ;
112- uint numLocals = ( uint ) localHandles . Length ;
98+ UIntPtr combinedParams = BinaryenType . Create ( paramTypes . ToArray ( ) ) . Handle ;
99+ UIntPtr combinedResult = resultType . Handle ;
100+ UIntPtr [ ] locals = localTypes . Select ( t => t . Handle ) . ToArray ( ) ;
101+ uint numLocals = ( uint ) locals . Length ;
113102
114- return BinaryenAddFunction (
115- Handle ,
116- name ,
117- paramHandle ,
118- resultHandle ,
119- localHandles ,
120- numLocals ,
121- body . Handle ) ;
103+ return BinaryenAddFunction ( Handle , name , combinedParams , combinedResult , locals , numLocals , body . Handle ) ;
122104 }
123105
124-
125-
126- /// <summary> Converts the module to a Web Assembly Text (WAT) string. </summary>
127106 public string ToText ( )
128107 {
129108 if ( Handle == IntPtr . Zero )
@@ -135,21 +114,15 @@ public string ToText()
135114 byte [ ] buffer = new byte [ bufferSize ] ;
136115 UIntPtr written = BinaryenModuleWriteText ( Handle , buffer , ( UIntPtr ) buffer . Length ) ;
137116
138- // If the module fits in the buffer, return the string
139117 if ( ( int ) written < bufferSize )
140118 return System . Text . Encoding . UTF8 . GetString ( buffer , 0 , ( int ) written ) ;
141119
142- // Otherwise, double the buffer size and try again
143120 bufferSize *= 2 ;
144-
145- // Prevent excessively large allocations
146- if ( bufferSize > 16 * 1024 * 1024 ) // 16 MB
121+ if ( bufferSize > 16 * 1024 * 1024 )
147122 throw new InvalidOperationException ( "Module is too large to serialize to WAT." ) ;
148123 }
149124 }
150125
151-
152- /// <summary> Converts the module to a Web Assembly (WASM) byte array. </summary>
153126 public byte [ ] ToBinary ( )
154127 {
155128 if ( Handle == IntPtr . Zero )
@@ -161,19 +134,15 @@ public byte[] ToBinary()
161134 byte [ ] buffer = new byte [ bufferSize ] ;
162135 UIntPtr written = BinaryenModuleWrite ( Handle , buffer , ( UIntPtr ) buffer . Length ) ;
163136
164- // If the module fits in the buffer, return the bytes
165137 if ( ( int ) written < bufferSize )
166138 {
167139 byte [ ] result = new byte [ ( int ) written ] ;
168140 Array . Copy ( buffer , result , ( int ) written ) ;
169141 return result ;
170142 }
171143
172- // Otherwise, double the buffer size and try again
173144 bufferSize *= 2 ;
174-
175- // Prevent excessively large allocations
176- if ( bufferSize > 16 * 1024 * 1024 ) // 16 MB
145+ if ( bufferSize > 16 * 1024 * 1024 )
177146 throw new InvalidOperationException ( "Module is too large to serialize to binary." ) ;
178147 }
179148 }
0 commit comments