3
3
4
4
from config import Config
5
5
6
+ # Tuples have only new, optionally clone, and free methods
6
7
class TupleGenerator :
7
8
8
9
def __init__ (self ) -> None :
@@ -12,35 +13,13 @@ def __init__(self) -> None:
12
13
template = template_handle .read ()
13
14
self .template = template
14
15
15
- def generate_tuple (self , tuple_name , tuple_details ):
16
+ def generate_tuple (self , struct_name , struct_details , all_type_details = {} ):
16
17
# method_names = ['openChannel', 'closeChannel']
17
18
# native_method_names = ['ChannelHandler_openChannel', 'ChannelHandler_closeChannel']
18
- print (tuple_name )
19
- swift_tuple_name = tuple_name [3 :]
20
19
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 :]
36
21
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
44
23
45
24
46
25
# REGULAR METHODS START
@@ -51,16 +30,31 @@ def generate_tuple(self, tuple_name, tuple_details):
51
30
method_template = method_template_regex .search (mutating_output_file_contents ).group (2 )
52
31
53
32
method_prefix = swift_tuple_name + '_'
54
- tuple_methods = ''
33
+ struct_methods = ''
55
34
56
35
# fill templates
57
- for current_method_details in tuple_details .methods :
36
+ for current_method_details in struct_details .methods :
58
37
current_native_method_name = current_method_details ['name' ]['native' ]
59
38
current_method_name = current_method_details ['name' ]['swift' ]
60
39
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 ):]
62
46
63
47
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
+
64
58
current_replacement = current_replacement .replace ('func methodName(' , f'func { current_method_name } (' )
65
59
66
60
is_clone_method = current_method_details ['is_clone' ]
@@ -97,15 +91,22 @@ def generate_tuple(self, tuple_name, tuple_details):
97
91
# }
98
92
# the \n\t will add a bunch of extra lines, but this file will be easier to read
99
93
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
101
95
\n \t \t pointer
102
96
\n \t }}
103
97
'''
104
98
native_call_prep += current_prep
105
99
106
100
if not pass_instance :
107
101
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 } ' )
109
110
110
111
current_replacement = current_replacement .replace ('swift_arguments' , ', ' .join (swift_arguments ))
111
112
if is_clone_method :
@@ -116,13 +117,13 @@ def generate_tuple(self, tuple_name, tuple_details):
116
117
current_replacement = current_replacement .replace ('/* NATIVE_CALL_PREP */' , native_call_prep )
117
118
current_replacement = current_replacement .replace ('-> Void {' , f'-> { current_return_type } {{' )
118
119
119
- tuple_methods += '\n ' + current_replacement + '\n '
120
+ struct_methods += '\n ' + current_replacement + '\n '
120
121
121
122
122
123
# DESTRUCTOR START
123
- if tuple_details .free_method is not None :
124
+ if struct_details .free_method is not None :
124
125
# fill constructor details
125
- free_method_details = tuple_details .free_method
126
+ free_method_details = struct_details .free_method
126
127
free_native_name = free_method_details ['name' ]['native' ]
127
128
native_call_prep = ''
128
129
native_arguments = []
@@ -146,17 +147,20 @@ def generate_tuple(self, tuple_name, tuple_details):
146
147
mutability_infix = 'Mutable'
147
148
148
149
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
150
151
\n \t \t pointer
151
152
\n \t }}
152
153
'''
153
154
native_call_prep += current_prep
154
155
elif pass_instance :
155
- passed_argument_name = 'self.cTuple !'
156
+ passed_argument_name = 'self.cOpaqueStruct !'
156
157
native_arguments .append (f'{ passed_argument_name } ' )
157
158
159
+ # always overwrite the weird _res variable
160
+ native_arguments = ['self.cOpaqueStruct!' ]
161
+
158
162
159
- tuple_methods += f'''
163
+ struct_methods += f'''
160
164
\n \t deinit {{
161
165
{ native_call_prep }
162
166
\n \t { free_native_name } ({ ', ' .join (native_arguments )} )
@@ -167,10 +171,10 @@ def generate_tuple(self, tuple_name, tuple_details):
167
171
mutating_output_file_contents = mutating_output_file_contents .replace ('class TupleName {' ,
168
172
f'class { swift_tuple_name } {{' )
169
173
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>' ,
174
178
mutating_output_file_contents )
175
179
176
180
@@ -181,4 +185,3 @@ def generate_tuple(self, tuple_name, tuple_details):
181
185
os .makedirs (output_directory )
182
186
with open (output_path , "w" ) as f :
183
187
f .write (mutating_output_file_contents )
184
- pass
0 commit comments