|
| 1 | +import re |
| 2 | +import os |
| 3 | + |
| 4 | + |
| 5 | +class TupleGenerator: |
| 6 | + |
| 7 | + def __init__(self) -> None: |
| 8 | + super().__init__() |
| 9 | + template_path = f'{os.path.dirname(__file__)}/../../templates/TupleTemplate.swift' |
| 10 | + with open(template_path, 'r') as template_handle: |
| 11 | + template = template_handle.read() |
| 12 | + self.template = template |
| 13 | + |
| 14 | + def generate_tuple(self, tuple_name, tuple_details): |
| 15 | + # method_names = ['openChannel', 'closeChannel'] |
| 16 | + # native_method_names = ['ChannelHandler_openChannel', 'ChannelHandler_closeChannel'] |
| 17 | + |
| 18 | + swift_tuple_name = tuple_name[3:] |
| 19 | + |
| 20 | + method_template_regex = re.compile( |
| 21 | + "(\/\* TUPLE_METHODS_START \*\/\n)(.*)(\n[\t ]*\/\* TUPLE_METHODS_END \*\/)", |
| 22 | + flags=re.MULTILINE | re.DOTALL) |
| 23 | + method_template = method_template_regex.search(self.template).group(2) |
| 24 | + |
| 25 | + method_prefix = swift_tuple_name + '_' |
| 26 | + tuple_methods = '' |
| 27 | + |
| 28 | + # fill templates |
| 29 | + for current_method_details in tuple_details.methods: |
| 30 | + current_native_method_name = current_method_details['name']['native'] |
| 31 | + current_method_name = current_method_details['name']['swift'] |
| 32 | + |
| 33 | + current_replacement = method_template |
| 34 | + current_replacement = current_replacement.replace('func methodName(', f'func {current_method_name}(') |
| 35 | + current_replacement = current_replacement.replace('TupleType_methodName(', |
| 36 | + f'{current_native_method_name}(') |
| 37 | + |
| 38 | + # replace arguments |
| 39 | + swift_arguments = [] |
| 40 | + native_arguments = ['self.cTuple'] |
| 41 | + native_call_prep = '' |
| 42 | + for current_argument_details in current_method_details['argument_types']: |
| 43 | + argument_name = current_argument_details.var_name |
| 44 | + passed_argument_name = argument_name |
| 45 | + if argument_name == 'this_ptr': |
| 46 | + # we already pass this much more elegantly |
| 47 | + continue |
| 48 | + |
| 49 | + if current_argument_details.passed_as_ptr: |
| 50 | + passed_argument_name = argument_name+'Pointer' |
| 51 | + # let managerPointer = withUnsafePointer(to: self.cChannelManager!) { (pointer: UnsafePointer<LDKChannelManager>) in |
| 52 | + # pointer |
| 53 | + # } |
| 54 | + # the \n\t will add a bunch of extra lines, but this file will be easier to read |
| 55 | + current_prep = f''' |
| 56 | + \n\t let {passed_argument_name} = withUnsafePointer(to: {argument_name}.cTuple!) {{ (pointer: UnsafePointer<{current_argument_details.rust_obj}>) in |
| 57 | + \n\t\t pointer |
| 58 | + \n\t }} |
| 59 | + ''' |
| 60 | + native_call_prep += current_prep |
| 61 | + |
| 62 | + swift_arguments.append(f'{current_argument_details.java_hu_ty} {argument_name}') |
| 63 | + native_arguments.append(f'{passed_argument_name}') |
| 64 | + |
| 65 | + current_replacement = current_replacement.replace('swift_arguments', ', '.join(swift_arguments)) |
| 66 | + current_replacement = current_replacement.replace('native_arguments', ', '.join(native_arguments)) |
| 67 | + current_replacement = current_replacement.replace('/* NATIVE_CALL_PREP */', native_call_prep) |
| 68 | + |
| 69 | + tuple_methods += '\n' + current_replacement + '\n' |
| 70 | + |
| 71 | + tuple_file = self.template.replace('class TupleName {', f'class {swift_tuple_name} {{') |
| 72 | + tuple_file = tuple_file.replace('var cTuple: TupleType?', |
| 73 | + f'var cTuple: {tuple_name}?') |
| 74 | + tuple_file = tuple_file.replace('self.cTuple = TupleType()', |
| 75 | + f'self.cTuple = {tuple_name}_new()') |
| 76 | + tuple_file = method_template_regex.sub(f'\g<1>{tuple_methods}\g<3>', tuple_file) |
| 77 | + |
| 78 | + # store the output |
| 79 | + output_path = f'{os.path.dirname(__file__)}/../../output/LDK/tuples/{swift_tuple_name}.swift' |
| 80 | + output_directory = os.path.dirname(output_path) |
| 81 | + if not os.path.exists(output_directory): |
| 82 | + os.makedirs(output_directory) |
| 83 | + with open(output_path, "w") as f: |
| 84 | + f.write(tuple_file) |
| 85 | + pass |
0 commit comments