@@ -19,70 +19,77 @@ def generate_trait(self, struct_name, struct_details):
19
19
20
20
swift_struct_name = struct_name [3 :]
21
21
22
- method_template_regex = re .compile (
23
- "(\/\* STRUCT_METHODS_START \*\/\n )(.*)(\n [\t ]*\/\* STRUCT_METHODS_END \*\/)" ,
22
+ native_callback_template_regex = re .compile (
23
+ "(\/\* NATIVE_CALLBACKS_START \*\/\n )(.*)(\n [\t ]*\/\* NATIVE_CALLBACKS_END \*\/)" ,
24
24
flags = re .MULTILINE | re .DOTALL )
25
- method_template = method_template_regex .search (self .template ).group (2 )
25
+ native_callback_template = native_callback_template_regex .search (self .template ).group (2 )
26
+
27
+ swift_callback_template_regex = re .compile (
28
+ "(\/\* SWIFT_CALLBACKS_START \*\/\n )(.*)(\n [\t ]*\/\* SWIFT_CALLBACKS_END \*\/)" ,
29
+ flags = re .MULTILINE | re .DOTALL )
30
+ swift_callback_template = swift_callback_template_regex .search (self .template ).group (2 )
26
31
27
32
method_prefix = swift_struct_name + '_'
28
- struct_methods = ''
33
+ native_callbacks = ''
34
+ swift_callbacks = ''
35
+
36
+ instantiation_arguments = []
29
37
30
38
# fill templates
31
- for current_method_details in struct_details .methods :
32
- current_native_method_name = current_method_details ['name' ]['native' ]
33
- current_method_name = current_method_details ['name' ]['swift' ]
34
- # current_method_name = current_native_method_name[len(method_prefix):]
35
-
36
- current_replacement = method_template
37
- current_replacement = current_replacement .replace ('func methodName(' , f'func { current_method_name } (' )
38
- current_replacement = current_replacement .replace ('OpaqueStructType_methodName(' ,
39
- f'{ current_native_method_name } (' )
40
-
41
- # replace arguments
42
- swift_arguments = []
43
- native_arguments = ['self.cOpaqueStruct' ]
44
- native_call_prep = ''
45
- for current_argument_details in current_method_details ['argument_types' ]:
46
- argument_name = current_argument_details .var_name
47
- passed_argument_name = argument_name
48
- if argument_name == 'this_ptr' :
49
- # we already pass this much more elegantly
50
- continue
51
-
52
- if current_argument_details .passed_as_ptr :
53
- passed_argument_name = argument_name + 'Pointer'
54
- # let managerPointer = withUnsafePointer(to: self.cChannelManager!) { (pointer: UnsafePointer<LDKChannelManager>) in
55
- # pointer
56
- # }
57
- # the \n\t will add a bunch of extra lines, but this file will be easier to read
58
- current_prep = f'''
59
- \n \t let { passed_argument_name } = withUnsafePointer(to: { argument_name } .cOpaqueStruct!) {{ (pointer: UnsafePointer<{ current_argument_details .rust_obj } >) in
60
- \n \t \t pointer
61
- \n \t }}
62
- '''
63
- native_call_prep += current_prep
64
-
65
- swift_arguments .append (f'{ current_argument_details .java_hu_ty } { argument_name } ' )
66
- native_arguments .append (f'{ passed_argument_name } ' )
67
-
68
- current_replacement = current_replacement .replace ('swift_arguments' , ', ' .join (swift_arguments ))
69
- current_replacement = current_replacement .replace ('native_arguments' , ', ' .join (native_arguments ))
70
- current_replacement = current_replacement .replace ('/* NATIVE_CALL_PREP */' , native_call_prep )
71
-
72
- struct_methods += '\n ' + current_replacement + '\n '
73
-
74
- opaque_struct_file = self .template .replace ('class OpaqueStructName {' , f'class { swift_struct_name } {{' )
75
- opaque_struct_file = opaque_struct_file .replace ('var cOpaqueStruct: OpaqueStructType?' ,
76
- f'var cOpaqueStruct: { struct_name } ?' )
77
- opaque_struct_file = opaque_struct_file .replace ('self.cOpaqueStruct = OpaqueStructType()' ,
78
- f'self.cOpaqueStruct = { struct_name } _new()' )
79
- opaque_struct_file = method_template_regex .sub (f'\g<1>{ struct_methods } \g<3>' , opaque_struct_file )
39
+ for current_lambda in struct_details .lambdas :
40
+ current_lambda_name = current_lambda ['name' ]
41
+
42
+ current_native_callback_replacement = native_callback_template
43
+ current_native_callback_replacement = current_native_callback_replacement .replace ('func methodNameCallback(' , f'func { current_lambda_name } Callback(' )
44
+ current_native_callback_replacement = current_native_callback_replacement .replace ('instance: TraitName' , f'instance: { swift_struct_name } ' )
45
+ current_native_callback_replacement = current_native_callback_replacement .replace ('instance.callbackName(' , f'instance.{ current_lambda_name } (' )
46
+
47
+ current_swift_callback_replacement = swift_callback_template
48
+ current_swift_callback_replacement = current_swift_callback_replacement .replace ('func methodName(' , f'func { current_lambda_name } (' )
49
+
50
+ instantiation_arguments .append (f'{ current_lambda_name } : { current_lambda_name } Callback' )
51
+
52
+ # let's specify the correct return type
53
+ swift_raw_return_type = current_lambda ['return_type' ].swift_raw_type
54
+ current_native_callback_replacement = current_native_callback_replacement .replace (') -> Void {' , f') -> { swift_raw_return_type } {{' )
55
+
56
+ # let's get the current native arguments, i. e. the arguments we get from C into the native callback
57
+ native_arguments = []
58
+ swift_callback_arguments = []
59
+ swift_argument_string = ''
60
+ for current_argument in current_lambda ['argument_types' ]:
61
+ native_arguments .append (f'{ current_argument .var_name } : { current_argument .swift_raw_type } ?' )
62
+ swift_callback_arguments .append (f'{ current_argument .var_name } : { current_argument .var_name } ' )
63
+ if len (native_arguments ) > 0 :
64
+ # add leading comma
65
+ swift_argument_string = ', ' .join (native_arguments )
66
+ native_arguments .insert (0 , '' )
67
+
68
+ native_argument_string = ', ' .join (native_arguments )
69
+ current_native_callback_replacement = current_native_callback_replacement .replace (', native_arguments' , native_argument_string )
70
+ current_native_callback_replacement = current_native_callback_replacement .replace ('swift_callback_arguments' , ', ' .join (swift_callback_arguments ))
71
+ current_swift_callback_replacement = current_swift_callback_replacement .replace ('swift_arguments' , swift_argument_string )
72
+
73
+ if not current_lambda ['is_constant' ]:
74
+ current_native_callback_replacement = current_native_callback_replacement .replace ('(pointer: UnsafeRawPointer?' , '(pointer: UnsafeMutableRawPointer?' )
75
+
76
+ native_callbacks += '\n ' + current_native_callback_replacement + '\n '
77
+ swift_callbacks += '\n ' + current_swift_callback_replacement + '\n '
78
+
79
+ trait_file = self .template .replace ('class TraitName {' , f'class { swift_struct_name } {{' )
80
+ trait_file = trait_file .replace ('var cTrait: TraitType?' ,
81
+ f'var cTrait: { struct_name } ?' )
82
+ trait_file = trait_file .replace ('self.cTrait = TraitType(' ,
83
+ f'self.cTrait = { struct_name } (' )
84
+ trait_file = trait_file .replace ('native_callback_instantiation_arguments' , ', ' .join (instantiation_arguments ))
85
+ trait_file = native_callback_template_regex .sub (f'\g<1>{ native_callbacks } \g<3>' , trait_file )
86
+ trait_file = swift_callback_template_regex .sub (f'\g<1>{ swift_callbacks } \g<3>' , trait_file )
80
87
81
88
# store the output
82
89
output_path = f'{ Config .OUTPUT_DIRECTORY_PATH } /traits/{ swift_struct_name } .swift'
83
90
output_directory = os .path .dirname (output_path )
84
91
if not os .path .exists (output_directory ):
85
92
os .makedirs (output_directory )
86
93
with open (output_path , "w" ) as f :
87
- f .write (opaque_struct_file )
94
+ f .write (trait_file )
88
95
pass
0 commit comments