Skip to content

Commit ec4d7db

Browse files
committed
add support for tuples and traits
1 parent 04066e7 commit ec4d7db

File tree

8 files changed

+559
-378
lines changed

8 files changed

+559
-378
lines changed
File renamed without changes.

input/minimal_tuple.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
typedef struct LDKThirtyTwoBytes {
2+
uint8_t data[32];
3+
} LDKThirtyTwoBytes;
4+
5+
typedef struct LDKC2Tuple_u64u64Z {
6+
uint64_t a;
7+
uint64_t b;
8+
} LDKC2Tuple_u64u64Z;
9+
10+
struct LDKC2Tuple_u64u64Z C2Tuple_u64u64Z_clone(const struct LDKC2Tuple_u64u64Z *NONNULL_PTR orig);
11+
12+
void C2Tuple_u64u64Z_free(struct LDKC2Tuple_u64u64Z _res);
13+
14+
struct LDKC2Tuple_u64u64Z C2Tuple_u64u64Z_new(uint64_t a, uint64_t b);

src/generators/opaque_struct_generator.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ def generate_opaque_struct(self, struct_name, struct_details):
2727

2828
# fill templates
2929
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):]
30+
current_native_method_name = current_method_details['name']['native']
31+
current_method_name = current_method_details['name']['swift']
32+
# current_method_name = current_native_method_name[len(method_prefix):]
3233

3334
current_replacement = method_template
3435
current_replacement = current_replacement.replace('func methodName(', f'func {current_method_name}(')
@@ -51,10 +52,11 @@ def generate_opaque_struct(self, struct_name, struct_details):
5152
# let managerPointer = withUnsafePointer(to: self.cChannelManager!) { (pointer: UnsafePointer<LDKChannelManager>) in
5253
# pointer
5354
# }
55+
# the \n\t will add a bunch of extra lines, but this file will be easier to read
5456
current_prep = f'''
55-
let {passed_argument_name} = withUnsafePointer(to: {argument_name}.cOpaqueStruct!) {{ (pointer: UnsafePointer<{current_argument_details.rust_obj}>) in
56-
pointer
57-
}}
57+
\n\t let {passed_argument_name} = withUnsafePointer(to: {argument_name}.cOpaqueStruct!) {{ (pointer: UnsafePointer<{current_argument_details.rust_obj}>) in
58+
\n\t\t pointer
59+
\n\t }}
5860
'''
5961
native_call_prep += current_prep
6062

@@ -75,7 +77,7 @@ def generate_opaque_struct(self, struct_name, struct_details):
7577
opaque_struct_file = method_template_regex.sub(f'\g<1>{struct_methods}\g<3>', opaque_struct_file)
7678

7779
# store the output
78-
output_path = f'{os.path.dirname(__file__)}/../../output/LDK/{swift_struct_name}.swift'
80+
output_path = f'{os.path.dirname(__file__)}/../../output/LDK/structs/{swift_struct_name}.swift'
7981
output_directory = os.path.dirname(output_path)
8082
if not os.path.exists(output_directory):
8183
os.makedirs(output_directory)

src/generators/tuple_generator.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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

Comments
 (0)