@@ -90,6 +90,10 @@ private static void GenerateSingleFieldAccessors(CppGenerationContext context, T
90
90
91
91
bool hasStructRewrite = Interop . RewriteStructReturn ( ref getParameters , ref getType , ref getInteropType ) ;
92
92
93
+ // Add a parameter in which to return the exception, if there is one.
94
+ getParameters = getParameters . Concat ( new [ ] { ( ParameterName : "reinteropException" , CallSiteName : "&reinteropException" , Type : CppType . VoidPointerPointer , InteropType : CppType . VoidPointerPointer ) } ) ;
95
+ interopSetParameters = interopSetParameters + ", void** reinteropException" ;
96
+
93
97
var interopGetParameters = getParameters . Select ( parameter => $ "{ parameter . InteropType . GetFullyQualifiedName ( ) } { parameter . ParameterName } ") ;
94
98
var interopGetParametersCall = getParameters . Select ( parameter => parameter . Type . GetConversionToInteropType ( context , parameter . CallSiteName ) ) ;
95
99
@@ -145,7 +149,11 @@ private static void GenerateSingleFieldAccessors(CppGenerationContext context, T
145
149
146
150
string [ ] invocation = new [ ]
147
151
{
152
+ $ "void* reinteropException = nullptr;",
148
153
$ "auto result = Field_get_{ field . Name } ({ string . Join ( ", " , interopGetParametersCall ) } );",
154
+ $ "if (reinteropException != nullptr) {{",
155
+ $ " throw Reinterop::ReinteropNativeException(::DotNet::System::Exception(::DotNet::Reinterop::ObjectHandle(reinteropException)));",
156
+ $ "}}",
149
157
$ "return { getType . GetConversionFromInteropType ( context , "result" ) } ;"
150
158
} ;
151
159
if ( hasStructRewrite )
@@ -154,17 +162,25 @@ private static void GenerateSingleFieldAccessors(CppGenerationContext context, T
154
162
{
155
163
invocation = new [ ]
156
164
{
165
+ $ "void* reinteropException = nullptr;",
157
166
$ "{ getType . GenericArguments . FirstOrDefault ( ) . GetFullyQualifiedName ( ) } result;",
158
167
$ "std::uint8_t resultIsValid = Field_get_{ field . Name } ({ string . Join ( ", " , interopGetParametersCall ) } );",
168
+ $ "if (reinteropException != nullptr) {{",
169
+ $ " throw Reinterop::ReinteropNativeException(::DotNet::System::Exception(::DotNet::Reinterop::ObjectHandle(reinteropException)));",
170
+ $ "}}",
159
171
$ "return resultIsValid ? std::make_optional(std::move({ getType . GetConversionFromInteropType ( context , "result" ) } )) : std::nullopt;"
160
172
} ;
161
173
}
162
174
else
163
175
{
164
176
invocation = new [ ]
165
177
{
178
+ $ "void* reinteropException = nullptr;",
166
179
$ "{ getType . GetFullyQualifiedName ( ) } result;",
167
180
$ "Field_get_{ field . Name } ({ string . Join ( ", " , interopGetParametersCall ) } );",
181
+ $ "if (reinteropException != nullptr) {{",
182
+ $ " throw Reinterop::ReinteropNativeException(::DotNet::System::Exception(::DotNet::Reinterop::ObjectHandle(reinteropException)));",
183
+ $ "}}",
168
184
$ "return { getType . GetConversionFromInteropType ( context , "result" ) } ;"
169
185
} ;
170
186
}
@@ -181,22 +197,28 @@ private static void GenerateSingleFieldAccessors(CppGenerationContext context, T
181
197
{
182
198
definition . Type ,
183
199
getType ,
184
- CppObjectHandle . GetCppType ( context )
200
+ CppObjectHandle . GetCppType ( context ) ,
201
+ CppReinteropException . GetCppType ( context )
185
202
}
186
203
) ) ;
187
204
188
205
definition . Elements . Add ( new (
189
206
Content :
190
207
$$ """
191
208
void {{ definition . Type . Name }} ::{{ field . Name }} ({{ setType . GetFullyQualifiedName ( ) }} value){{ ( field . IsStatic ? "" : " const" ) }} {
192
- Field_set_{{ field . Name }} ({{ interopSetParametersCall }} );
209
+ void* reinteropException = nullptr;
210
+ Field_set_{{ field . Name }} ({{ interopSetParametersCall }} , &reinteropException);
211
+ if (reinteropException != nullptr) {
212
+ throw Reinterop::ReinteropNativeException(::DotNet::System::Exception(::DotNet::Reinterop::ObjectHandle(reinteropException)));
213
+ }
193
214
}
194
215
""" ,
195
216
TypeDefinitionsReferenced : new [ ]
196
217
{
197
218
definition . Type ,
198
219
setType ,
199
- CppObjectHandle . GetCppType ( context )
220
+ CppObjectHandle . GetCppType ( context ) ,
221
+ CppReinteropException . GetCppType ( context )
200
222
}
201
223
) ) ;
202
224
}
0 commit comments