Skip to content

Commit 5672f90

Browse files
committed
fix result names and add manual u8slice support
1 parent 37a20ed commit 5672f90

File tree

9 files changed

+153
-46
lines changed

9 files changed

+153
-46
lines changed

src/generators/opaque_struct_generator.py

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def generate_opaque_struct(self, struct_name, struct_details, all_type_details={
4040
if current_argument_details.rust_obj is not None and current_argument_details.rust_obj.startswith(
4141
'LDK') and current_argument_details.swift_type.startswith('['):
4242
constructor_argument_conversion_method = f'let converted_{argument_name} = Bindings.new_{current_argument_details.rust_obj}(array: {argument_name})'
43-
constructor_argument_prep += constructor_argument_conversion_method
43+
constructor_argument_prep += '\n\t\t'+constructor_argument_conversion_method
4444
passed_argument_name = f'converted_{argument_name}'
4545
elif current_argument_details.rust_obj == 'LDK' + current_argument_details.swift_type:
4646
passed_argument_name += '.cOpaqueStruct!'
@@ -78,7 +78,8 @@ def generate_opaque_struct(self, struct_name, struct_details, all_type_details={
7878
for current_method_details in struct_details.methods:
7979
current_native_method_name = current_method_details['name']['native']
8080
current_method_name = current_method_details['name']['swift']
81-
current_return_type = current_method_details['return_type'].swift_type
81+
current_return_type = current_method_details['return_type']
82+
current_swift_return_type = current_return_type.swift_type
8283
# current_rust_return_type = current_method_details['return_type'].rust_obj
8384

8485
# if current_rust_return_type in all_type_details and all_type_details[current_rust_return_type].type.name == 'UNITARY_ENUM':
@@ -88,22 +89,35 @@ def generate_opaque_struct(self, struct_name, struct_details, all_type_details={
8889
current_replacement = method_template
8990
is_clone_method = current_method_details['is_clone']
9091

91-
if current_method_details['return_type'].rust_obj is not None and current_method_details[
92-
'return_type'].rust_obj.startswith('LDK') and current_method_details[
93-
'return_type'].swift_type.startswith('['):
94-
return_type_wrapper_prefix = f'Bindings.{current_method_details["return_type"].rust_obj}_to_array(byteType: '
92+
if current_return_type.rust_obj is not None and current_return_type.rust_obj.startswith('LDK') and current_return_type.swift_type.startswith('['):
93+
return_type_wrapper_prefix = f'Bindings.{current_method_details["return_type"].rust_obj}_to_array(nativeType: '
9594
return_type_wrapper_suffix = ')'
9695
current_replacement = current_replacement.replace(
9796
'return OpaqueStructType_methodName(native_arguments)',
9897
f'return {return_type_wrapper_prefix}OpaqueStructType_methodName(native_arguments){return_type_wrapper_suffix}')
99-
elif current_method_details['return_type'].rust_obj == 'LDK' + current_method_details[
100-
'return_type'].swift_type and not is_clone_method:
98+
elif current_return_type.rust_obj == 'LDK' + current_return_type.swift_type and not is_clone_method:
10199
return_type_wrapper_prefix = f'{current_method_details["return_type"].swift_type}(pointer: '
100+
# return_type_wrapper_suffix = '.pointee)'
101+
return_type_wrapper_suffix = ')'
102+
current_replacement = current_replacement.replace(
103+
'return OpaqueStructType_methodName(native_arguments)',
104+
f'return {return_type_wrapper_prefix}OpaqueStructType_methodName(native_arguments){return_type_wrapper_suffix}')
105+
elif current_return_type.rust_obj == 'LDKC' + current_return_type.swift_type and not is_clone_method:
106+
return_type_wrapper_prefix = f'{current_method_details["return_type"].swift_type}(pointer: '
107+
# return_type_wrapper_suffix = '.pointee)'
102108
return_type_wrapper_suffix = ')'
103109
current_replacement = current_replacement.replace(
104110
'return OpaqueStructType_methodName(native_arguments)',
105111
f'return {return_type_wrapper_prefix}OpaqueStructType_methodName(native_arguments){return_type_wrapper_suffix}')
106112

113+
if current_return_type.rust_obj is None and current_return_type.swift_type.startswith('['):
114+
current_replacement = current_replacement.replace(
115+
'OpaqueStructType_methodName(native_arguments)',
116+
'OpaqueStructType_methodName(native_arguments).pointee')
117+
118+
if current_return_type.rust_obj is None and current_return_type.swift_type.startswith('['):
119+
current_swift_return_type = current_return_type.swift_raw_type
120+
107121
current_replacement = current_replacement.replace('func methodName(', f'func {current_method_name}(')
108122

109123
if is_clone_method:
@@ -151,17 +165,22 @@ def generate_opaque_struct(self, struct_name, struct_details, all_type_details={
151165
# native_arguments.append(f'{passed_argument_name}')
152166
if current_argument_details.rust_obj == 'LDK' + current_argument_details.swift_type and not current_argument_details.is_ptr:
153167
native_arguments.append(f'{passed_argument_name}.cOpaqueStruct!')
168+
elif current_argument_details.rust_obj is not None and current_argument_details.rust_obj.startswith(
169+
'LDK') and current_argument_details.swift_type.startswith('['):
170+
native_arguments.append(f'Bindings.new_{current_argument_details.rust_obj}(array: {passed_argument_name})')
154171
else:
155172
native_arguments.append(f'{passed_argument_name}')
156173

174+
175+
157176
current_replacement = current_replacement.replace('swift_arguments', ', '.join(swift_arguments))
158177
if is_clone_method:
159178
# add closing parenthesis that could not be added further up in the clone initializer
160179
current_replacement = current_replacement.replace('native_arguments', ', '.join(native_arguments) + ')')
161180
else:
162181
current_replacement = current_replacement.replace('native_arguments', ', '.join(native_arguments))
163182
current_replacement = current_replacement.replace('/* NATIVE_CALL_PREP */', native_call_prep)
164-
current_replacement = current_replacement.replace('-> Void {', f'-> {current_return_type} {{')
183+
current_replacement = current_replacement.replace('-> Void {', f'-> {current_swift_return_type} {{')
165184

166185
struct_methods += '\n' + current_replacement + '\n'
167186

src/generators/result_generator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def generate_result(self, struct_name, struct_details, all_type_details={}):
1717
# method_names = ['openChannel', 'closeChannel']
1818
# native_method_names = ['ChannelHandler_openChannel', 'ChannelHandler_closeChannel']
1919

20-
swift_struct_name = struct_name[3:]
20+
swift_struct_name = struct_name[4:]
2121

2222
mutating_output_file_contents = self.template
2323

@@ -88,7 +88,7 @@ def generate_result(self, struct_name, struct_details, all_type_details={}):
8888
if current_method_details['return_type'].rust_obj is not None and current_method_details[
8989
'return_type'].rust_obj.startswith('LDK') and current_method_details[
9090
'return_type'].swift_type.startswith('['):
91-
return_type_wrapper_prefix = f'Bindings.{current_method_details["return_type"].rust_obj}_to_array(byteType: '
91+
return_type_wrapper_prefix = f'Bindings.{current_method_details["return_type"].rust_obj}_to_array(nativeType: '
9292
return_type_wrapper_suffix = ')'
9393
current_replacement = current_replacement.replace(
9494
'return ResultType_methodName(native_arguments)',

src/generators/trait_generator.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ def generate_trait(self, struct_name, struct_details):
3939
for current_lambda in struct_details.lambdas:
4040
current_lambda_name = current_lambda['name']
4141

42+
if not current_lambda['is_lambda']:
43+
current_field_details = current_lambda['field_details']
44+
current_rust_type = current_field_details.rust_obj
45+
instantiation_arguments.append(f'{current_lambda_name}: {current_rust_type}()')
46+
continue
47+
4248
current_native_callback_replacement = native_callback_template
4349
current_native_callback_replacement = current_native_callback_replacement.replace('func methodNameCallback(', f'func {current_lambda_name}Callback(')
4450
current_native_callback_replacement = current_native_callback_replacement.replace('instance: TraitName', f'instance: {swift_struct_name}')
@@ -51,14 +57,26 @@ def generate_trait(self, struct_name, struct_details):
5157

5258
# let's specify the correct return type
5359
swift_raw_return_type = current_lambda['return_type'].swift_raw_type
60+
if current_lambda['return_type'].rust_obj is not None and current_lambda['return_type'].rust_obj.startswith('LDK'):
61+
swift_raw_return_type = current_lambda['return_type'].rust_obj
62+
5463
current_native_callback_replacement = current_native_callback_replacement.replace(') -> Void {', f') -> {swift_raw_return_type} {{')
5564

5665
# let's get the current native arguments, i. e. the arguments we get from C into the native callback
5766
native_arguments = []
5867
swift_callback_arguments = []
5968
swift_argument_string = ''
6069
for current_argument in current_lambda['argument_types']:
61-
native_arguments.append(f'{current_argument.var_name}: {current_argument.swift_raw_type}?')
70+
passed_raw_type = current_argument.swift_raw_type
71+
if current_argument.rust_obj is not None and current_argument.rust_obj.startswith('LDK'):
72+
passed_raw_type = current_argument.rust_obj
73+
if current_argument.is_const:
74+
passed_raw_type = f'UnsafePointer<{passed_raw_type}>'
75+
if(current_argument.swift_type.startswith('[')):
76+
passed_raw_type += '?' # TODO: figure out when tf it actually becomes nullable!
77+
# if current_argument.passed_as_ptr:
78+
# passed_raw_type += '?'
79+
native_arguments.append(f'{current_argument.var_name}: {passed_raw_type}')
6280
swift_callback_arguments.append(f'{current_argument.var_name}: {current_argument.var_name}')
6381
if len(native_arguments) > 0:
6482
# add leading comma
@@ -82,7 +100,7 @@ def generate_trait(self, struct_name, struct_details):
82100
f'var cOpaqueStruct: {struct_name}?')
83101
trait_file = trait_file.replace('self.cOpaqueStruct = TraitType(',
84102
f'self.cOpaqueStruct = {struct_name}(')
85-
trait_file = trait_file.replace('native_callback_instantiation_arguments', ', '.join(instantiation_arguments))
103+
trait_file = trait_file.replace('native_callback_instantiation_arguments', ',\n\t\t\t'.join(instantiation_arguments))
86104
trait_file = native_callback_template_regex.sub(f'\g<1>{native_callbacks}\g<3>', trait_file)
87105
trait_file = swift_callback_template_regex.sub(f'\g<1>{swift_callbacks}\g<3>', trait_file)
88106

src/generators/tuple_generator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def generate_tuple(self, struct_name, struct_details, all_type_details = {}):
4747
current_replacement = method_template
4848

4949
if current_method_details['return_type'].rust_obj is not None and current_method_details['return_type'].rust_obj.startswith('LDK') and current_method_details['return_type'].swift_type.startswith('['):
50-
return_type_wrapper_prefix = f'Bindings.{current_method_details["return_type"].rust_obj}_to_array(byteType: '
50+
return_type_wrapper_prefix = f'Bindings.{current_method_details["return_type"].rust_obj}_to_array(nativeType: '
5151
return_type_wrapper_suffix = ')'
5252
current_replacement = current_replacement.replace('return TupleType_methodName(native_arguments)', f'return {return_type_wrapper_prefix}TupleType_methodName(native_arguments){return_type_wrapper_suffix}')
5353
elif current_method_details['return_type'].rust_obj == 'LDK' + current_method_details['return_type'].swift_type:

src/generators/util_generators/byte_array_generator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ def generate_byte_array(self, byte_array_type_name, byte_array_type_details):
2222
mutating_current_byte_array_methods = mutating_current_byte_array_methods.replace('fieldName:',
2323
f'{byte_array_field.var_name}:')
2424
tupleArguments = 'array[0]'
25-
tupleReads = f'byteType.{byte_array_field.var_name}.0'
25+
tupleReads = f'nativeType.{byte_array_field.var_name}.0'
2626
for i in range(1, array_length):
2727
tupleArguments += f', array[{i}]'
28-
tupleReads += f', byteType.{byte_array_field.var_name}.{i}'
28+
tupleReads += f', nativeType.{byte_array_field.var_name}.{i}'
2929
mutating_current_byte_array_methods = mutating_current_byte_array_methods.replace('tupleArguments',
3030
tupleArguments)
3131
mutating_current_byte_array_methods = mutating_current_byte_array_methods.replace('tupleReads',

src/generators/util_generators/vector_generator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def generate_vector(self, vector_name, vector_type_details):
3030
dimensions -= 1
3131
swift_primitive = deepest_iteratee.name
3232
if dimensions > 1:
33-
conversion_call = f'let convertedEntry = {shallowmost_iteratee.name}_to_array(vector: currentEntry)'
33+
conversion_call = f'let convertedEntry = {shallowmost_iteratee.name}_to_array(nativeType: currentEntry)'
3434

3535
mutating_current_vector_methods = self.template
3636
for dim_delta in range(1, dimensions):

src/lightning_header_parser.py

Lines changed: 57 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ def populate_type_details(self):
170170
trait_fn_lines = []
171171
field_var_lines = []
172172

173+
ordered_interpreted_lines = []
174+
173175
for idx, struct_line in enumerate(obj_lines):
174176
struct_name_match = struct_name_regex.match(struct_line)
175177
if struct_name_match is not None:
@@ -196,9 +198,11 @@ def populate_type_details(self):
196198
trait_fn_match = line_indicates_trait_regex.match(struct_line)
197199
if trait_fn_match is not None:
198200
trait_fn_lines.append(trait_fn_match)
201+
ordered_interpreted_lines.append({"type": "lambda", "value": trait_fn_match})
199202
field_var_match = line_field_var_regex.match(struct_line)
200203
if field_var_match is not None:
201204
field_var_lines.append(field_var_match)
205+
ordered_interpreted_lines.append({"type": "field", "value": field_var_match})
202206
field_lines.append(struct_line)
203207

204208
assert (struct_name is not None)
@@ -259,7 +263,7 @@ def populate_type_details(self):
259263
# TODO: vector type (each one needs to be mapped)
260264
self.vec_types.add(struct_name)
261265
# vector_type_details = None
262-
vector_type_details = TypeDetails() # iterator type
266+
vector_type_details = TypeDetails() # iterator type
263267
vector_type_details.type = CTypes.VECTOR
264268
vector_type_details.name = struct_name
265269

@@ -273,7 +277,7 @@ def populate_type_details(self):
273277
vector_type_details.is_primitive = True
274278
vector_type_details.primitive_swift_counterpart = self.language_constants.c_type_map[vec_ty]
275279
self.type_details[struct_name] = vector_type_details
276-
# pass
280+
# pass
277281
elif is_union_enum:
278282
assert (struct_name.endswith("_Tag"))
279283
struct_name = struct_name[:-4]
@@ -291,7 +295,7 @@ def populate_type_details(self):
291295
pass
292296
elif len(trait_fn_lines) > 0:
293297
self.trait_structs.add(struct_name)
294-
lambdas = self.parse_lambda_details(trait_fn_lines)
298+
lambdas = self.parse_lambda_details(ordered_interpreted_lines)
295299
current_type_detail.lambdas = lambdas
296300
elif struct_name == "LDKTxOut":
297301
# TODO: why is this even a special case? It's Swift, we dgaf
@@ -359,28 +363,57 @@ def populate_type_details(self):
359363
# self.global_methods.add(method_details)
360364
pass
361365

362-
def parse_lambda_details(self, trait_fn_lines):
366+
def parse_lambda_details(self, ordered_interpreted_lines):
367+
field_var_convs = []
368+
flattened_field_var_convs = []
369+
363370
lambdas = []
364-
for fn_line in trait_fn_lines:
365-
ret_ty_info = swift_type_mapper.map_types_to_swift(fn_line.group(2).strip() + " ret", None, False,
366-
self.tuple_types, self.unitary_enums,
367-
self.language_constants)
368-
is_const = fn_line.group(4) is not None
369-
370-
arg_tys = []
371-
for idx, arg in enumerate(fn_line.group(5).split(',')):
372-
if arg == "":
373-
continue
374-
arg_conv_info = swift_type_mapper.map_types_to_swift(arg, None, False, self.tuple_types,
375-
self.unitary_enums,
376-
self.language_constants)
377-
arg_tys.append(arg_conv_info)
378-
lambdas.append({
379-
'name': fn_line.group(3),
380-
'is_constant': is_const,
381-
'return_type': ret_ty_info,
382-
'argument_types': arg_tys
383-
})
371+
372+
for current_interpreted_line in ordered_interpreted_lines:
373+
if current_interpreted_line['type'] == 'field':
374+
var_line = current_interpreted_line['value']
375+
current_field_type = var_line.group(1)
376+
current_field_name = var_line.group(2)
377+
if False and current_field_type in self.trait_structs:
378+
lambdas.append({
379+
'name': current_field_name,
380+
'field_details': self.type_details[current_field_type],
381+
'is_lambda': False
382+
})
383+
# flattened_field_var_convs.extend(self.type_details[current_field_type])
384+
else:
385+
mapped = swift_type_mapper.map_types_to_swift(current_field_type + " " + current_field_name, None, False,
386+
self.tuple_types, self.unitary_enums,
387+
self.language_constants)
388+
lambdas.append({
389+
'name': current_field_name,
390+
'field_details': mapped,
391+
'is_lambda': False
392+
})
393+
elif current_interpreted_line['type'] == 'lambda':
394+
fn_line = current_interpreted_line['value']
395+
ret_ty_info = swift_type_mapper.map_types_to_swift(fn_line.group(2).strip() + " ret", None, False,
396+
self.tuple_types, self.unitary_enums,
397+
self.language_constants)
398+
is_const = fn_line.group(4) is not None
399+
400+
arg_tys = []
401+
for idx, arg in enumerate(fn_line.group(5).split(',')):
402+
if arg == "":
403+
continue
404+
arg_conv_info = swift_type_mapper.map_types_to_swift(arg, None, False, self.tuple_types,
405+
self.unitary_enums,
406+
self.language_constants)
407+
arg_tys.append(arg_conv_info)
408+
409+
lambdas.append({
410+
'name': fn_line.group(3),
411+
'is_lambda': True,
412+
'is_constant': is_const,
413+
'return_type': ret_ty_info,
414+
'argument_types': arg_tys
415+
})
416+
384417
return lambdas
385418

386419
def parse_function_details(self, line, re_match, ret_arr_len, c_call_string):

0 commit comments

Comments
 (0)