Skip to content

Commit d7d2c23

Browse files
committed
fix UInt/usize issues
1 parent c2c6f0d commit d7d2c23

File tree

6 files changed

+82
-166
lines changed

6 files changed

+82
-166
lines changed

src/generators/tuple_generator.py

Lines changed: 44 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from config import Config
55

6+
# Tuples have only new, optionally clone, and free methods
67
class TupleGenerator:
78

89
def __init__(self) -> None:
@@ -12,35 +13,13 @@ def __init__(self) -> None:
1213
template = template_handle.read()
1314
self.template = template
1415

15-
def generate_tuple(self, tuple_name, tuple_details):
16+
def generate_tuple(self, struct_name, struct_details, all_type_details = {}):
1617
# method_names = ['openChannel', 'closeChannel']
1718
# native_method_names = ['ChannelHandler_openChannel', 'ChannelHandler_closeChannel']
18-
print(tuple_name)
19-
swift_tuple_name = tuple_name[3:]
2019

21-
mutating_output_file_contents = self.template
22-
23-
# CONSTRUCTOR START
24-
if tuple_details.constructor_method is not None:
25-
# fill constructor details
26-
constructor_details = tuple_details.constructor_method
27-
constructor_native_name = constructor_details['name']['native']
28-
swift_arguments = []
29-
native_arguments = []
30-
for current_argument_details in constructor_details['argument_types']:
31-
argument_name = current_argument_details.var_name
32-
passed_argument_name = argument_name
33-
34-
swift_arguments.append(f'{argument_name}: {current_argument_details.swift_type}')
35-
native_arguments.append(f'{passed_argument_name}')
20+
swift_tuple_name = struct_name[3:]
3621

37-
mutating_output_file_contents = mutating_output_file_contents.replace('swift_constructor_arguments',
38-
', '.join(swift_arguments))
39-
mutating_output_file_contents = mutating_output_file_contents.replace('native_constructor_arguments',
40-
', '.join(native_arguments))
41-
mutating_output_file_contents = mutating_output_file_contents.replace(
42-
'self.cTuple = TupleType(',
43-
f'self.cTuple = {constructor_native_name}(')
22+
mutating_output_file_contents = self.template
4423

4524

4625
# REGULAR METHODS START
@@ -51,16 +30,31 @@ def generate_tuple(self, tuple_name, tuple_details):
5130
method_template = method_template_regex.search(mutating_output_file_contents).group(2)
5231

5332
method_prefix = swift_tuple_name + '_'
54-
tuple_methods = ''
33+
struct_methods = ''
5534

5635
# fill templates
57-
for current_method_details in tuple_details.methods:
36+
for current_method_details in struct_details.methods:
5837
current_native_method_name = current_method_details['name']['native']
5938
current_method_name = current_method_details['name']['swift']
6039
current_return_type = current_method_details['return_type'].swift_type
61-
# current_method_name = current_native_method_name[len(method_prefix):]
40+
current_return_type = swift_tuple_name
41+
# current_rust_return_type = current_method_details['return_type'].rust_obj
42+
43+
# if current_rust_return_type in all_type_details and all_type_details[current_rust_return_type].type.name == 'UNITARY_ENUM':
44+
# current_return_type = current_rust_return_type
45+
current_method_name = current_native_method_name[len(method_prefix):]
6246

6347
current_replacement = method_template
48+
49+
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: '
51+
return_type_wrapper_suffix = ')'
52+
current_replacement = current_replacement.replace('return TupleType_methodName(native_arguments)', f'return {return_type_wrapper_prefix}TupleType_methodName(native_arguments){return_type_wrapper_suffix}')
53+
elif current_method_details['return_type'].rust_obj == 'LDK' + current_method_details['return_type'].swift_type:
54+
return_type_wrapper_prefix = f'{current_method_details["return_type"].swift_type}(pointer: '
55+
return_type_wrapper_suffix = ')'
56+
current_replacement = current_replacement.replace('return TupleType_methodName(native_arguments)', f'return {return_type_wrapper_prefix}TupleType_methodName(native_arguments){return_type_wrapper_suffix}')
57+
6458
current_replacement = current_replacement.replace('func methodName(', f'func {current_method_name}(')
6559

6660
is_clone_method = current_method_details['is_clone']
@@ -97,15 +91,22 @@ def generate_tuple(self, tuple_name, tuple_details):
9791
# }
9892
# the \n\t will add a bunch of extra lines, but this file will be easier to read
9993
current_prep = f'''
100-
\n\t let {passed_argument_name} = withUnsafe{mutability_infix}Pointer(to: {argument_name}.cTuple!) {{ (pointer: Unsafe{mutability_infix}Pointer<{current_argument_details.rust_obj}>) in
94+
\n\t let {passed_argument_name} = withUnsafe{mutability_infix}Pointer(to: {argument_name}.cOpaqueStruct!) {{ (pointer: Unsafe{mutability_infix}Pointer<{current_argument_details.rust_obj}>) in
10195
\n\t\t pointer
10296
\n\t }}
10397
'''
10498
native_call_prep += current_prep
10599

106100
if not pass_instance:
107101
swift_arguments.append(f'{argument_name}: {current_argument_details.swift_type}')
108-
native_arguments.append(f'{passed_argument_name}')
102+
103+
# native_arguments.append(f'{passed_argument_name}')
104+
if current_argument_details.rust_obj == 'LDK' + current_argument_details.swift_type and not current_argument_details.is_ptr:
105+
native_arguments.append(f'{passed_argument_name}.cOpaqueStruct!')
106+
elif current_argument_details.rust_obj is not None and current_argument_details.rust_obj.startswith('LDK') and current_argument_details.swift_type.startswith('['):
107+
native_arguments.append(f'Bindings.new_{current_argument_details.rust_obj}(array: {passed_argument_name})')
108+
else:
109+
native_arguments.append(f'{passed_argument_name}')
109110

110111
current_replacement = current_replacement.replace('swift_arguments', ', '.join(swift_arguments))
111112
if is_clone_method:
@@ -116,13 +117,13 @@ def generate_tuple(self, tuple_name, tuple_details):
116117
current_replacement = current_replacement.replace('/* NATIVE_CALL_PREP */', native_call_prep)
117118
current_replacement = current_replacement.replace('-> Void {', f'-> {current_return_type} {{')
118119

119-
tuple_methods += '\n' + current_replacement + '\n'
120+
struct_methods += '\n' + current_replacement + '\n'
120121

121122

122123
# DESTRUCTOR START
123-
if tuple_details.free_method is not None:
124+
if struct_details.free_method is not None:
124125
# fill constructor details
125-
free_method_details = tuple_details.free_method
126+
free_method_details = struct_details.free_method
126127
free_native_name = free_method_details['name']['native']
127128
native_call_prep = ''
128129
native_arguments = []
@@ -146,17 +147,20 @@ def generate_tuple(self, tuple_name, tuple_details):
146147
mutability_infix = 'Mutable'
147148

148149
current_prep = f'''
149-
\n\t let {passed_argument_name} = withUnsafe{mutability_infix}Pointer(to: {argument_name}.cTuple!) {{ (pointer: Unsafe{mutability_infix}Pointer<{current_argument_details.rust_obj}>) in
150+
\n\t let {passed_argument_name} = withUnsafe{mutability_infix}Pointer(to: {argument_name}.cOpaqueStruct!) {{ (pointer: Unsafe{mutability_infix}Pointer<{current_argument_details.rust_obj}>) in
150151
\n\t\t pointer
151152
\n\t }}
152153
'''
153154
native_call_prep += current_prep
154155
elif pass_instance:
155-
passed_argument_name = 'self.cTuple!'
156+
passed_argument_name = 'self.cOpaqueStruct!'
156157
native_arguments.append(f'{passed_argument_name}')
157158

159+
# always overwrite the weird _res variable
160+
native_arguments = ['self.cOpaqueStruct!']
161+
158162

159-
tuple_methods += f'''
163+
struct_methods += f'''
160164
\n\tdeinit {{
161165
{native_call_prep}
162166
\n\t {free_native_name}({', '.join(native_arguments)})
@@ -167,10 +171,10 @@ def generate_tuple(self, tuple_name, tuple_details):
167171
mutating_output_file_contents = mutating_output_file_contents.replace('class TupleName {',
168172
f'class {swift_tuple_name} {{')
169173
mutating_output_file_contents = mutating_output_file_contents.replace('init(pointer: TupleType',
170-
f'init(pointer: {tuple_name}')
171-
mutating_output_file_contents = mutating_output_file_contents.replace('var cTuple: TupleType?',
172-
f'var cTuple: {tuple_name}?')
173-
mutating_output_file_contents = method_template_regex.sub(f'\g<1>{tuple_methods}\g<3>',
174+
f'init(pointer: {struct_name}')
175+
mutating_output_file_contents = mutating_output_file_contents.replace('var cOpaqueStruct: TupleType?',
176+
f'var cOpaqueStruct: {struct_name}?')
177+
mutating_output_file_contents = method_template_regex.sub(f'\g<1>{struct_methods}\g<3>',
174178
mutating_output_file_contents)
175179

176180

@@ -181,4 +185,3 @@ def generate_tuple(self, tuple_name, tuple_details):
181185
os.makedirs(output_directory)
182186
with open(output_path, "w") as f:
183187
f.write(mutating_output_file_contents)
184-
pass

src/generators/tuple_generator_old.py

Lines changed: 0 additions & 85 deletions
This file was deleted.

src/sdk_generator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def generate_tuple_wrappers(parser: LightningHeaderParser):
4747
tuples = parser.tuple_types
4848
for current_tuple in tuples:
4949
current_tuple_details = parser.type_details[current_tuple]
50-
tuple_generator.generate_tuple(current_tuple, current_tuple_details)
50+
tuple_generator.generate_tuple(current_tuple, current_tuple_details, all_type_details=parser.type_details)
5151

5252

5353
def generate_trait_placeholders(parser: LightningHeaderParser):

src/swift_constants.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def __init__(self, DEBUG: bool, target: Target, **kwargs):
2626
self.file_ext = ".java"
2727
self.ptr_c_ty = "int64_t"
2828
# self.ptr_native_ty = "long"
29-
self.ptr_native_ty = "UInt64" # TODO: verify that all target architectures are 64 bit
29+
self.ptr_native_ty = "UInt" # TODO: verify that all target architectures are 64 bit
3030
self.result_c_ty = "jclass"
3131
self.ptr_arr = "jobjectArray"
3232
self.get_native_arr_len_call = ("(*env)->GetArrayLength(env, ", ")")

src/swift_type_mapper.py

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ def map_types_to_swift(fn_arg, ret_arr_len, java_c_types_none_allowed, tuple_typ
175175
else:
176176
c_ty = "int64_t"
177177
rust_obj = "uintptr_t"
178+
swift_type = 'UInt'
178179
fn_arg = fn_arg[9:].strip()
179180
is_primitive = True
180181
elif is_const and fn_arg.startswith("char *"):
@@ -202,42 +203,44 @@ def map_types_to_swift(fn_arg, ret_arr_len, java_c_types_none_allowed, tuple_typ
202203
elif type_match.startswith("LDKC2Tuple"):
203204
c_ty = language_constants.ptr_c_ty
204205
java_ty = language_constants.ptr_native_ty
205-
swift_type = "TwoTuple<"
206-
if not type_match in tuple_types:
207-
assert java_c_types_none_allowed
208-
return None
209-
for idx, ty_info in enumerate(tuple_types[type_match][0]):
210-
if idx != 0:
211-
swift_type = swift_type + ", "
212-
if ty_info.is_native_primitive:
213-
if ty_info.swift_type == "int":
214-
swift_type = swift_type + "Integer" # Java concrete integer type is Integer, not Int
215-
else:
216-
swift_type = swift_type + ty_info.swift_type.title() # If we're a primitive, capitalize the first letter
217-
else:
218-
swift_type = swift_type + ty_info.swift_type
219-
swift_type = swift_type + ">"
206+
swift_type = type_match[3:]
207+
# swift_type = "TwoTuple<"
208+
# if not type_match in tuple_types:
209+
# assert java_c_types_none_allowed
210+
# return None
211+
# for idx, ty_info in enumerate(tuple_types[type_match][0]):
212+
# if idx != 0:
213+
# swift_type = swift_type + ", "
214+
# if ty_info.is_native_primitive:
215+
# if ty_info.swift_type == "int":
216+
# swift_type = swift_type + "Integer" # Java concrete integer type is Integer, not Int
217+
# else:
218+
# swift_type = swift_type + ty_info.swift_type.title() # If we're a primitive, capitalize the first letter
219+
# else:
220+
# swift_type = swift_type + ty_info.swift_type
221+
# swift_type = swift_type + ">"
220222
fn_arg = name_match
221223
rust_obj = type_match
222224
take_by_ptr = True
223225
elif type_match.startswith("LDKC3Tuple"):
224226
c_ty = language_constants.ptr_c_ty
225227
java_ty = language_constants.ptr_native_ty
226-
swift_type = "ThreeTuple<"
227-
if not type_match in tuple_types:
228-
assert java_c_types_none_allowed
229-
return None
230-
for idx, ty_info in enumerate(tuple_types[type_match][0]):
231-
if idx != 0:
232-
swift_type = swift_type + ", "
233-
if ty_info.is_native_primitive:
234-
if ty_info.java_hu_ty == "int":
235-
swift_type = swift_type + "Integer" # Java concrete integer type is Integer, not Int
236-
else:
237-
swift_type = swift_type + ty_info.java_hu_ty.title() # If we're a primitive, capitalize the first letter
238-
else:
239-
swift_type = swift_type + ty_info.swift_type
240-
swift_type = swift_type + ">"
228+
swift_type = type_match[3:]
229+
# swift_type = "ThreeTuple<"
230+
# if not type_match in tuple_types:
231+
# assert java_c_types_none_allowed
232+
# return None
233+
# for idx, ty_info in enumerate(tuple_types[type_match][0]):
234+
# if idx != 0:
235+
# swift_type = swift_type + ", "
236+
# if ty_info.is_native_primitive:
237+
# if ty_info.java_hu_ty == "int":
238+
# swift_type = swift_type + "Integer" # Java concrete integer type is Integer, not Int
239+
# else:
240+
# swift_type = swift_type + ty_info.java_hu_ty.title() # If we're a primitive, capitalize the first letter
241+
# else:
242+
# swift_type = swift_type + ty_info.swift_type
243+
# swift_type = swift_type + ">"
241244
fn_arg = name_match
242245
rust_obj = type_match
243246
take_by_ptr = True

templates/TupleTemplate.swift

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
class TupleName {
22

3-
var cTuple: TupleType?;
3+
var cOpaqueStruct: TupleType?;
44

5-
init(swift_constructor_arguments) {
6-
/* NATIVE_CONSTRUCTOR_PREP */
7-
self.cTuple = TupleType(native_constructor_arguments)
8-
}
9-
10-
private init(pointer: TupleType){
11-
self.cTuple = pointer
5+
init(pointer: TupleType){
6+
self.cOpaqueStruct = pointer
127
}
138

149
/* TUPLE_METHODS_START */

0 commit comments

Comments
 (0)