Skip to content

Commit 04066e7

Browse files
committed
generate pointers correctly and pass them as arguments
1 parent 8c2fe89 commit 04066e7

File tree

2 files changed

+69
-35
lines changed

2 files changed

+69
-35
lines changed
Lines changed: 67 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,84 @@
11
import re
22
import os
33

4+
45
class OpaqueStructGenerator:
56

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']
1217

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:]
1619

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)
1824

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 = ''
2127

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):]
2432

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}(')
2737

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
3048

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
3560

61+
swift_arguments.append(f'{current_argument_details.java_hu_ty} {argument_name}')
62+
native_arguments.append(f'{passed_argument_name}')
3663

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)
4167

68+
struct_methods += '\n' + current_replacement + '\n'
4269

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)
4376

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

templates/OpaqueStructTemplate.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ class OpaqueStructName {
88

99
/* STRUCT_METHODS_START */
1010
func methodName(swift_arguments) -> Void {
11-
OpaqueStructType_methodName(self.cOpaqueStruct, native_arguments);
11+
/* NATIVE_CALL_PREP */
12+
OpaqueStructType_methodName(native_arguments);
1213
}
1314
/* STRUCT_METHODS_END */
1415

0 commit comments

Comments
 (0)