|
1 | 1 | import re
|
2 | 2 | import os
|
3 | 3 |
|
| 4 | + |
4 | 5 | class OpaqueStructGenerator:
|
5 | 6 |
|
6 |
| - def __init__(self) -> None: |
7 |
| - super().__init__() |
8 |
| - template_path = f'{os.path.dirname(__file__)}/../../templates/OpaqueStructTemplate.swift' |
9 |
| - with open(template_path, 'r') as template_handle: |
10 |
| - template = template_handle.read() |
11 |
| - self.template = template |
| 7 | + def __init__(self) -> None: |
| 8 | + super().__init__() |
| 9 | + template_path = f'{os.path.dirname(__file__)}/../../templates/OpaqueStructTemplate.swift' |
| 10 | + with open(template_path, 'r') as template_handle: |
| 11 | + template = template_handle.read() |
| 12 | + self.template = template |
| 13 | + |
| 14 | + def generate_opaque_struct(self, struct_name, struct_details): |
| 15 | + # method_names = ['openChannel', 'closeChannel'] |
| 16 | + # native_method_names = ['ChannelHandler_openChannel', 'ChannelHandler_closeChannel'] |
12 | 17 |
|
13 |
| - def generate_opaque_struct(self, struct_name, struct_details): |
14 |
| - # method_names = ['openChannel', 'closeChannel'] |
15 |
| - # native_method_names = ['ChannelHandler_openChannel', 'ChannelHandler_closeChannel'] |
| 18 | + swift_struct_name = struct_name[3:] |
16 | 19 |
|
17 |
| - swift_struct_name = struct_name[3:] |
| 20 | + method_template_regex = re.compile( |
| 21 | + "(\/\* STRUCT_METHODS_START \*\/\n)(.*)(\n[\t ]*\/\* STRUCT_METHODS_END \*\/)", |
| 22 | + flags=re.MULTILINE | re.DOTALL) |
| 23 | + method_template = method_template_regex.search(self.template).group(2) |
18 | 24 |
|
19 |
| - method_template_regex = re.compile("(\/\* STRUCT_METHODS_START \*\/\n)(.*)(\n[\t ]*\/\* STRUCT_METHODS_END \*\/)", flags = re.MULTILINE | re.DOTALL) |
20 |
| - method_template = method_template_regex.search(self.template).group(2) |
| 25 | + method_prefix = swift_struct_name + '_' |
| 26 | + struct_methods = '' |
21 | 27 |
|
22 |
| - method_prefix = swift_struct_name+'_' |
23 |
| - struct_methods = '' |
| 28 | + # fill templates |
| 29 | + for current_method_details in struct_details.methods: |
| 30 | + current_native_method_name = current_method_details['name'] |
| 31 | + current_method_name = current_native_method_name[len(method_prefix):] |
24 | 32 |
|
25 |
| - # fill templates |
26 |
| - for current_method_details in struct_details.methods: |
| 33 | + current_replacement = method_template |
| 34 | + current_replacement = current_replacement.replace('func methodName(', f'func {current_method_name}(') |
| 35 | + current_replacement = current_replacement.replace('OpaqueStructType_methodName(', |
| 36 | + f'{current_native_method_name}(') |
27 | 37 |
|
28 |
| - current_native_method_name = current_method_details['name'] |
29 |
| - current_method_name = current_native_method_name[len(method_prefix):] |
| 38 | + # replace arguments |
| 39 | + swift_arguments = [] |
| 40 | + native_arguments = ['self.cOpaqueStruct'] |
| 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 |
30 | 48 |
|
31 |
| - current_replacement = method_template |
32 |
| - current_replacement = current_replacement.replace('func methodName(', f'func {current_method_name}(') |
33 |
| - current_replacement = current_replacement.replace('OpaqueStructType_methodName(', f'{current_native_method_name}(') |
34 |
| - struct_methods += '\n'+current_replacement+'\n' |
| 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 | + current_prep = f''' |
| 55 | + let {passed_argument_name} = withUnsafePointer(to: {argument_name}.cOpaqueStruct!) {{ (pointer: UnsafePointer<{current_argument_details.rust_obj}>) in |
| 56 | + pointer |
| 57 | + }} |
| 58 | + ''' |
| 59 | + native_call_prep += current_prep |
35 | 60 |
|
| 61 | + swift_arguments.append(f'{current_argument_details.java_hu_ty} {argument_name}') |
| 62 | + native_arguments.append(f'{passed_argument_name}') |
36 | 63 |
|
37 |
| - opaque_struct_file = self.template.replace('class OpaqueStructName {', f'class {swift_struct_name} {{') |
38 |
| - opaque_struct_file = opaque_struct_file.replace('var cOpaqueStruct: OpaqueStructType?', f'var cOpaqueStruct: {struct_name}?') |
39 |
| - opaque_struct_file = opaque_struct_file.replace('self.cOpaqueStruct = OpaqueStructType()', f'self.cOpaqueStruct = {struct_name}_new()') |
40 |
| - opaque_struct_file = method_template_regex.sub(f'\g<1>{struct_methods}\g<3>', opaque_struct_file) |
| 64 | + current_replacement = current_replacement.replace('swift_arguments', ', '.join(swift_arguments)) |
| 65 | + current_replacement = current_replacement.replace('native_arguments', ', '.join(native_arguments)) |
| 66 | + current_replacement = current_replacement.replace('/* NATIVE_CALL_PREP */', native_call_prep) |
41 | 67 |
|
| 68 | + struct_methods += '\n' + current_replacement + '\n' |
42 | 69 |
|
| 70 | + opaque_struct_file = self.template.replace('class OpaqueStructName {', f'class {swift_struct_name} {{') |
| 71 | + opaque_struct_file = opaque_struct_file.replace('var cOpaqueStruct: OpaqueStructType?', |
| 72 | + f'var cOpaqueStruct: {struct_name}?') |
| 73 | + opaque_struct_file = opaque_struct_file.replace('self.cOpaqueStruct = OpaqueStructType()', |
| 74 | + f'self.cOpaqueStruct = {struct_name}_new()') |
| 75 | + opaque_struct_file = method_template_regex.sub(f'\g<1>{struct_methods}\g<3>', opaque_struct_file) |
43 | 76 |
|
44 |
| - # store the output |
45 |
| - output_path = f'{os.path.dirname(__file__)}/../../output/LDK/{swift_struct_name}.swift' |
46 |
| - output_directory = os.path.dirname(output_path) |
47 |
| - if not os.path.exists(output_directory): |
48 |
| - os.makedirs(output_directory) |
49 |
| - with open(output_path, "w") as f: |
50 |
| - f.write(opaque_struct_file) |
51 |
| - pass |
| 77 | + # store the output |
| 78 | + output_path = f'{os.path.dirname(__file__)}/../../output/LDK/{swift_struct_name}.swift' |
| 79 | + output_directory = os.path.dirname(output_path) |
| 80 | + if not os.path.exists(output_directory): |
| 81 | + os.makedirs(output_directory) |
| 82 | + with open(output_path, "w") as f: |
| 83 | + f.write(opaque_struct_file) |
| 84 | + pass |
0 commit comments