4
4
cloneable_types = set ()
5
5
detected_cloneable_types = set ()
6
6
7
+
7
8
class ConversionHelper :
8
9
trait_structs = set ()
9
10
10
11
@classmethod
11
- def prepare_swift_to_native_arguments (cls , argument_types , is_trait_callback = False , force_pass_instance = False , is_free_method = False ):
12
+ def prepare_swift_to_native_arguments (cls , argument_types , is_trait_callback = False , force_pass_instance = False , is_free_method = False ):
12
13
swift_arguments = []
13
14
native_arguments = []
14
15
pointer_wrapping_prefix = ''
@@ -43,8 +44,6 @@ def prepare_swift_to_native_arguments(cls, argument_types, is_trait_callback = F
43
44
passed_argument_name = 'self'
44
45
static_eligible = False
45
46
46
-
47
-
48
47
if current_argument_details .is_ptr :
49
48
passed_argument_name = argument_name + 'Pointer'
50
49
if argument_name == 'init' :
@@ -87,25 +86,23 @@ def prepare_swift_to_native_arguments(cls, argument_types, is_trait_callback = F
87
86
88
87
initialization_target = f'{ argument_name } Unwrapped.cOpaqueStruct!'
89
88
90
- if current_argument_details .rust_obj is not None and current_argument_details .rust_obj .startswith (
91
- 'LDK' ) and current_argument_details .swift_type .startswith ('[' ):
92
- # TODO: figure out why this never gets hit, or rather why I originally wrote this
93
- initialization_target = f'Bindings.new_{ current_argument_details .rust_obj } Wrapper(array: { argument_name } Unwrapped).cOpaqueStruct!'
94
- is_pointer_to_array = True
89
+ if current_argument_details .rust_obj is not None and current_argument_details .rust_obj .startswith ('LDK' ) and current_argument_details .swift_type .startswith ('[' ):
90
+ # TODO: figure out why this never gets hit, or rather why I originally wrote this
91
+ initialization_target = f'Bindings.new_{ current_argument_details .rust_obj } Wrapper(array: { argument_name } Unwrapped).cOpaqueStruct!'
92
+ is_pointer_to_array = True
95
93
96
94
native_call_prep += f'''
97
95
var { passed_argument_name } : UnsafeMutablePointer<{ current_argument_details .rust_obj } >? = nil
98
96
if let { argument_name } Unwrapped = { argument_name } {{
99
97
{ passed_argument_name } = UnsafeMutablePointer<{ current_argument_details .rust_obj } >.allocate(capacity: 1)
100
98
{ passed_argument_name } !.initialize(to: { initialization_target } )
101
99
}}
102
- '''
103
- # print('optional argument:', argument_name, passed_argument_name)
100
+ ''' # print('optional argument:', argument_name, passed_argument_name)
104
101
else :
105
102
wrapper_return_prefix = '' if pointer_wrapping_depth == 0 else 'return '
106
103
pointer_wrapping_prefix += f'{ wrapper_return_prefix } withUnsafe{ mutability_infix } Pointer(to: { reference_prefix } { argument_name } .cOpaqueStruct!) {{ ({ passed_argument_name } : Unsafe{ mutability_infix } Pointer<{ current_argument_details .rust_obj } >) in\n '
107
104
pointer_wrapping_suffix += '\n }'
108
- # native_call_prep += current_prep
105
+ # native_call_prep += current_prep
109
106
elif is_cloneable :
110
107
if not is_free_method :
111
108
# clone_infix = '.clone()'
@@ -119,12 +116,13 @@ def prepare_swift_to_native_arguments(cls, argument_types, is_trait_callback = F
119
116
pass
120
117
elif current_argument_details .rust_obj .startswith ('LDK' ) and not current_argument_details .swift_type .startswith ('[' ) and not is_free_method :
121
118
non_cloneable_argument_indices_passed_by_ownership .append (argument_index )
122
- if current_argument_details .rust_obj is not None and ('Option' in current_argument_details .rust_obj or 'Tuple' in current_argument_details .rust_obj or 'Result' in current_argument_details .rust_obj ) and not current_argument_details .rust_obj .startswith ('[' ):
119
+ if current_argument_details .rust_obj is not None and (
120
+ 'Option' in current_argument_details .rust_obj or 'Tuple' in current_argument_details .rust_obj or 'Result' in current_argument_details .rust_obj ) and not current_argument_details .rust_obj .startswith (
121
+ '[' ):
123
122
# TODO: figure out potentially undetected cloneable types
124
123
# print('Potentially undetected cloneable type?', current_argument_details.rust_obj)
125
124
pass
126
125
127
-
128
126
if is_trait_callback and current_argument_details .is_const :
129
127
if current_argument_details .swift_type .startswith ('[' ) or current_argument_details .swift_type == 'String' :
130
128
mutabilityIndicatorSuffix = '?'
@@ -139,15 +137,12 @@ def prepare_swift_to_native_arguments(cls, argument_types, is_trait_callback = F
139
137
swift_argument_type = swift_argument_type .replace ('[LDKResult_' , '[LDKCResult_' ).replace ('[LDKTuple_' , '[LDKCTuple_' )
140
138
swift_arguments .append (f'{ argument_name } : { swift_argument_type } { mutabilityIndicatorSuffix } ' )
141
139
142
-
143
-
144
140
# native_arguments.append(f'{passed_argument_name}')
145
141
if current_argument_details .rust_obj == 'LDK' + swift_argument_type and not current_argument_details .is_ptr :
146
142
native_arguments .append (f'{ passed_argument_name } { clone_infix } .cOpaqueStruct!' )
147
143
elif current_argument_details .rust_obj == 'LDKC' + swift_argument_type and not current_argument_details .is_ptr :
148
144
native_arguments .append (f'{ passed_argument_name } { clone_infix } .cOpaqueStruct!' )
149
- elif current_argument_details .rust_obj is not None and current_argument_details .rust_obj .startswith (
150
- 'LDK' ) and swift_argument_type .startswith ('[' ) and not is_pointer_to_array :
145
+ elif current_argument_details .rust_obj is not None and current_argument_details .rust_obj .startswith ('LDK' ) and swift_argument_type .startswith ('[' ) and not is_pointer_to_array :
151
146
# if current_argument_details.swift_type == '[UInt8]' and not current_argument_details.swift_raw_type.startswith('LDKC'):
152
147
if current_argument_details .rust_obj in pointer_iterating_vector_types :
153
148
# TODO: expand beyond 1-dimensional array support (as of writing only affects Route.swift)
@@ -172,8 +167,7 @@ def prepare_swift_to_native_arguments(cls, argument_types, is_trait_callback = F
172
167
native_arguments .append (f'{ passed_argument_name } Wrapper.cOpaqueStruct!' )
173
168
else :
174
169
native_arguments .append (f'Bindings.new_{ current_argument_details .rust_obj } (array: { passed_argument_name } )' )
175
- elif current_argument_details .rust_obj is not None and current_argument_details .rust_obj .startswith (
176
- 'LDK' ) and current_argument_details .is_unary_tuple :
170
+ elif current_argument_details .rust_obj is not None and current_argument_details .rust_obj .startswith ('LDK' ) and current_argument_details .is_unary_tuple :
177
171
native_arguments .append (f'Bindings.new_{ current_argument_details .rust_obj } (array: { passed_argument_name } )' )
178
172
elif swift_argument_type == 'String' :
179
173
if is_trait_callback and current_argument_details .swift_raw_type == 'UnsafePointer<Int8>' :
@@ -200,15 +194,8 @@ def prepare_swift_to_native_arguments(cls, argument_types, is_trait_callback = F
200
194
201
195
full_syntax = pointer_wrapping_prefix + ', ' .join (native_arguments ) + pointer_wrapping_suffix
202
196
# print(full_syntax)
203
- return {
204
- "swift_arguments" : swift_arguments ,
205
- "native_arguments" : native_arguments ,
206
- "native_call_prefix" : pointer_wrapping_prefix ,
207
- "native_call_suffix" : pointer_wrapping_suffix ,
208
- "native_call_prep" : native_call_prep ,
209
- "static_eligible" : static_eligible ,
210
- "non_cloneable_argument_indices_passed_by_ownership" : non_cloneable_argument_indices_passed_by_ownership
211
- }
197
+ return {"swift_arguments" : swift_arguments , "native_arguments" : native_arguments , "native_call_prefix" : pointer_wrapping_prefix , "native_call_suffix" : pointer_wrapping_suffix ,
198
+ "native_call_prep" : native_call_prep , "static_eligible" : static_eligible , "non_cloneable_argument_indices_passed_by_ownership" : non_cloneable_argument_indices_passed_by_ownership }
212
199
213
200
# the arguments that we receive from a native lambda, before they get passed on to a human consumer
214
201
# Traits -> NATIVE_CALLBACKS -> SWIFT_CALLBACK_PREP, basically
@@ -230,7 +217,7 @@ def prepare_native_to_swift_callback_arguments(cls, argument_types):
230
217
231
218
received_var_name = current_argument_details .var_name
232
219
233
- if received_var_name == 'init' : # illegal in swift
220
+ if received_var_name == 'init' : # illegal in swift
234
221
received_var_name = 'initValue'
235
222
passed_var_name = received_var_name
236
223
swift_callback_argument_value = received_var_name
@@ -264,7 +251,7 @@ def prepare_native_to_swift_callback_arguments(cls, argument_types):
264
251
# Bindings array converters cannot yet convert LDK types to wrapped types
265
252
published_swift_type = TypeParsingRegeces .WRAPPER_TYPE_ARRAY_BRACKET_REGEX .sub ('[LDK' , published_swift_type )
266
253
received_raw_type = current_argument_details .rust_obj
267
- # swift_callback_argument_value = f'{received_raw_type}(pointer: {received_var_name})'
254
+ # swift_callback_argument_value = f'{received_raw_type}(pointer: {received_var_name})'
268
255
269
256
if received_raw_type is None :
270
257
received_raw_type = inferred_raw_swift_type
@@ -278,9 +265,9 @@ def prepare_native_to_swift_callback_arguments(cls, argument_types):
278
265
if current_argument_details .swift_type .startswith ('[' ) or current_argument_details .swift_type == 'String' :
279
266
is_optional = True
280
267
received_raw_type += '?' # TODO: figure out when tf it actually becomes nullable!
281
- published_swift_type += '?' # the swift thing is obviously nullible, too
268
+ published_swift_type += '?' # the swift thing is obviously nullible, too
282
269
283
- if is_unsafe_pointer : # always true
270
+ if is_unsafe_pointer : # always true
284
271
received_var_name += 'Pointer'
285
272
if is_optional :
286
273
if inferred_raw_swift_type == 'UnsafePointer<Int8>' :
@@ -314,15 +301,11 @@ def prepare_native_to_swift_callback_arguments(cls, argument_types):
314
301
# native_arguments.insert(0, '')
315
302
pass
316
303
317
- return {
318
- 'raw_native_argument_list' : native_arguments ,
319
- 'swift_callback_arguments' : swift_callback_arguments ,
320
- 'public_swift_argument_list' : public_swift_argument_list ,
321
- 'swift_callback_prep' : swift_callback_prep
322
- }
304
+ return {'raw_native_argument_list' : native_arguments , 'swift_callback_arguments' : swift_callback_arguments , 'public_swift_argument_list' : public_swift_argument_list ,
305
+ 'swift_callback_prep' : swift_callback_prep }
323
306
324
307
@classmethod
325
- def prepare_return_value (cls , return_type , is_clone_method = False , is_trait_instantiator = False , is_raw_property_getter = False ):
308
+ def prepare_return_value (cls , return_type , is_clone_method = False , is_trait_instantiator = False , is_raw_property_getter = False ):
326
309
rust_return_type = return_type .rust_obj
327
310
return_prefix = ''
328
311
return_suffix = ''
@@ -352,9 +335,9 @@ def prepare_return_value(cls, return_type, is_clone_method = False, is_trait_ins
352
335
elif rust_return_type == 'LDKC' + return_type_string and not is_clone_method :
353
336
return_prefix = f'{ swift_return_instantiation_type } (pointer: '
354
337
return_suffix = ')'
355
- # if is_trait_instantiator:
356
- # only applies to tuples, but is never hit
357
- # return_suffix = ', anchor: self)'
338
+ # if is_trait_instantiator:
339
+ # only applies to tuples, but is never hit
340
+ # return_suffix = ', anchor: self)'
358
341
elif return_type_string == 'String' :
359
342
return_prefix = 'Bindings.LDKStr_to_string(nativeType: '
360
343
return_suffix = ')'
@@ -366,10 +349,8 @@ def prepare_return_value(cls, return_type, is_clone_method = False, is_trait_ins
366
349
pass
367
350
368
351
if TypeParsingRegeces .WRAPPER_TYPE_ARRAY_BRACKET_REGEX .search (return_type_string ):
352
+ # replace the last [ with [LDK (in case
369
353
return_type_string = TypeParsingRegeces .WRAPPER_TYPE_ARRAY_BRACKET_REGEX .sub ('[LDK' , return_type_string )
354
+ return_type_string = return_type_string .replace ('LDKResult_' , 'LDKCResult_' ).replace ('LDKTuple_' , 'LDKCTuple_' ).replace ('LDKVec_' , 'LDKCVec_' )
370
355
371
- return {
372
- 'prefix' : return_prefix ,
373
- 'suffix' : return_suffix ,
374
- 'swift_type' : return_type_string
375
- }
356
+ return {'prefix' : return_prefix , 'suffix' : return_suffix , 'swift_type' : return_type_string }
0 commit comments