Skip to content

Commit 722d6c5

Browse files
committed
move array types known a priori to static specification
1 parent fb1e7b4 commit 722d6c5

File tree

3 files changed

+51
-110
lines changed

3 files changed

+51
-110
lines changed

bindings/LDK/results/Result_RecoverableSignatureNoneZ.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ public class Result_RecoverableSignatureNoneZ: NativeTypeWrapper {
4545
return nil
4646
}
4747

48-
public class func ok(serialized_form: [UInt8]) -> Result_RecoverableSignatureNoneZ {
48+
public class func ok(o: [UInt8]) -> Result_RecoverableSignatureNoneZ {
4949

50-
return Result_RecoverableSignatureNoneZ(pointer: CResult_RecoverableSignatureNoneZ_ok(Bindings.new_LDKRecoverableSignature(array: serialized_form)));
50+
return Result_RecoverableSignatureNoneZ(pointer: CResult_RecoverableSignatureNoneZ_ok(Bindings.new_LDKRecoverableSignature(array: o)));
5151
}
5252

5353
public class func err() -> Result_RecoverableSignatureNoneZ {

src/conversion_helper.py

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,31 @@
44
cloneable_types = set()
55
detected_cloneable_types = set()
66

7+
class ArrayAccessorType:
8+
def __init__(self, key: str, size: int = -1, length_key = None):
9+
self.size: int = size
10+
self.key: str = key
11+
self.length_key: str = length_key
12+
13+
14+
array_accessor_types: dict[str, ArrayAccessorType] = {
15+
"LDKThirtyTwoBytes": ArrayAccessorType(size=32, key='data'),
16+
"LDKPaymentPreimage": ArrayAccessorType(size=32, key='data'),
17+
"LDKPublicKey": ArrayAccessorType(size=33, key='compressed_form'),
18+
"LDKSecretKey": ArrayAccessorType(size=32, key='bytes'),
19+
"LDKSignature": ArrayAccessorType(size=64, key='compact_form'),
20+
"LDKThreeBytes": ArrayAccessorType(size=3, key='data'),
21+
"LDKFourBytes": ArrayAccessorType(size=4, key='data'),
22+
"LDKTenBytes": ArrayAccessorType(size=10, key='data'),
23+
"LDKTwelveBytes": ArrayAccessorType(size=12, key='data'),
24+
"LDKSixteenBytes": ArrayAccessorType(size=16, key='data'),
25+
"LDKTwentyBytes": ArrayAccessorType(size=20, key='data'),
26+
"LDKRecoverableSignature": ArrayAccessorType(size=68, key='serialized_form'),
27+
"LDKu8slice": ArrayAccessorType(length_key='datalen', key='data'),
28+
"LDKCVec_u8Z": ArrayAccessorType(length_key='datalen', key='data'),
29+
"LDKCVec_u5Z": ArrayAccessorType(length_key='datalen', key='data'),
30+
"LDKTransaction": ArrayAccessorType(length_key='datalen', key='data'),
31+
}
732

833
class ConversionHelper:
934
trait_structs = set()
@@ -77,7 +102,7 @@ def prepare_swift_to_native_arguments(cls, argument_types, is_trait_callback=Fal
77102
pass_instance = True
78103
passed_argument_name = 'self'
79104
static_eligible = False
80-
105+
81106
# if caller_is_nullable_inner_type:
82107
# native_call_prep = f'''
83108
# if self.cOpaqueStruct!.inner == nil {{
@@ -313,7 +338,7 @@ def prepare_native_to_swift_callback_arguments(cls, argument_types, array_unwrap
313338
passed_var_name = received_var_name
314339
swift_callback_argument_value = received_var_name
315340

316-
if published_swift_type == '[UInt8]':
341+
if '[UInt8]' in published_swift_type:
317342
if inferred_raw_swift_type.startswith('(UInt8'):
318343
array_length = current_argument_details.arr_len
319344
swift_local_conversion_prefix = f'Bindings.tuple{array_length}_to_array(nativeType: '
@@ -331,8 +356,7 @@ def prepare_native_to_swift_callback_arguments(cls, argument_types, array_unwrap
331356
swift_local_conversion_prefix = f'Bindings.LDKu8slice_to_array(nativeType: '
332357
swift_local_conversion_suffix = ')'
333358
published_swift_type = '[UInt8]'
334-
elif published_swift_type == '[[UInt8]]':
335-
if received_raw_type == 'LDKCVec_PaymentPreimageZ':
359+
elif received_raw_type == 'LDKCVec_PaymentPreimageZ':
336360
swift_local_conversion_prefix = f'Bindings.LDKCVec_PaymentPreimageZ_to_array(nativeType: '
337361
swift_local_conversion_suffix = ')'
338362
elif received_raw_type is not None and received_raw_type.startswith('LDK'):
@@ -437,7 +461,7 @@ def prepare_return_value(cls, return_type, is_clone_method=False, is_trait_insta
437461
if (rust_return_type.startswith('LDKCVec_') or rust_return_type == 'LDKTransaction') and is_raw_property_getter:
438462
return_suffix = ', deallocate: false)'
439463
individual_map_suffix = '.dangle()'
440-
464+
441465
if TypeParsingRegeces.WRAPPER_TYPE_ARRAY_BRACKET_REGEX.search(return_type_string) and not is_trait_callback:
442466
# replace the last [ with [LDK (in case
443467
constructor_type = return_type_string.lstrip('[').rstrip(']')
@@ -456,18 +480,18 @@ def prepare_return_value(cls, return_type, is_clone_method=False, is_trait_insta
456480

457481
map_prefix = f'''.map {{ (cOpaqueStruct) in
458482
cOpaqueStruct''' * (wrapper_depth-1)
459-
483+
460484
map_suffix = '}' * wrapper_depth
461-
485+
462486
return_suffix += f'''
463487
{map_prefix}
464488
.map {{ (cOpaqueStruct) in
465489
{constructor_type}(pointer: cOpaqueStruct){individual_map_suffix}
466490
{map_suffix}
467491
'''
468-
469-
470-
492+
493+
494+
471495
elif return_type.swift_raw_type.startswith('(UInt8'):
472496
# TODO: get array length
473497
array_length = return_type.arr_len

src/swift_type_mapper.py

Lines changed: 15 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from src.binding_types import TypeInfo
22
from src.type_parsing_regeces import TypeParsingRegeces
33
from src.conversion_helper import ConversionHelper
4+
from src.conversion_helper import array_accessor_types
45

56
var_is_arr_regex = TypeParsingRegeces.IS_VARIABLE_AN_ARRAY_REGEX
67
var_ty_regex = TypeParsingRegeces.VARIABLE_TYPE_REGEX
@@ -37,104 +38,20 @@ def map_types_to_swift(fn_arg, ret_arr_len, java_c_types_none_allowed, tuple_typ
3738
# the way that Swift automatically maps C types when divergent from what people use, like const char * not being a String
3839
swift_raw_type = None
3940

40-
if fn_arg.startswith("LDKThirtyTwoBytes"):
41-
fn_arg = "uint8_t (*" + fn_arg[18:] + ")[32]"
42-
assert var_is_arr_regex.match(fn_arg[8:])
43-
rust_obj = "LDKThirtyTwoBytes"
44-
swift_raw_type = rust_obj
45-
arr_access = "data"
46-
elif fn_arg.startswith("LDKPaymentPreimage"): # disable this conversion using x prefix
47-
prefix_length = len('LDKPaymentPreimage') + 1
48-
fn_arg = "uint8_t (*" + fn_arg[prefix_length:] + ")[32]"
49-
assert var_is_arr_regex.match(fn_arg[8:])
50-
rust_obj = "LDKPaymentPreimage"
51-
swift_raw_type = rust_obj
52-
arr_access = "data"
53-
elif fn_arg.startswith("LDKPublicKey"):
54-
fn_arg = "uint8_t (*" + fn_arg[13:] + ")[33]"
55-
assert var_is_arr_regex.match(fn_arg[8:])
56-
rust_obj = "LDKPublicKey"
57-
swift_raw_type = rust_obj
58-
arr_access = "compressed_form"
59-
elif fn_arg.startswith("LDKSecretKey"):
60-
fn_arg = "uint8_t (*" + fn_arg[13:] + ")[32]"
61-
assert var_is_arr_regex.match(fn_arg[8:])
62-
rust_obj = "LDKSecretKey"
63-
swift_raw_type = rust_obj
64-
arr_access = "bytes"
65-
elif fn_arg.startswith("LDKSignature"):
66-
fn_arg = "uint8_t (*" + fn_arg[13:] + ")[64]"
67-
assert var_is_arr_regex.match(fn_arg[8:])
68-
rust_obj = "LDKSignature"
69-
swift_raw_type = rust_obj
70-
arr_access = "compact_form"
71-
elif fn_arg.startswith("LDKThreeBytes"):
72-
fn_arg = "uint8_t (*" + fn_arg[14:] + ")[3]"
73-
assert var_is_arr_regex.match(fn_arg[8:])
74-
rust_obj = "LDKThreeBytes"
75-
swift_raw_type = rust_obj
76-
arr_access = "data"
77-
elif fn_arg.startswith("LDKFourBytes"):
78-
fn_arg = "uint8_t (*" + fn_arg[13:] + ")[4]"
79-
assert var_is_arr_regex.match(fn_arg[8:])
80-
rust_obj = "LDKFourBytes"
81-
swift_raw_type = rust_obj
82-
arr_access = "data"
83-
elif fn_arg.startswith("LDKTenBytes"):
84-
fn_arg = "uint8_t (*" + fn_arg[12:] + ")[10]"
85-
assert var_is_arr_regex.match(fn_arg[8:])
86-
rust_obj = "LDKTenBytes"
87-
swift_raw_type = rust_obj
88-
arr_access = "data"
89-
elif fn_arg.startswith("LDKTwelveBytes"):
90-
fn_arg = "uint8_t (*" + fn_arg[len('LDKTwelveBytes '):] + ")[12]"
91-
assert var_is_arr_regex.match(fn_arg[8:])
92-
rust_obj = "LDKTwelveBytes"
93-
swift_raw_type = rust_obj
94-
arr_access = "data"
95-
elif fn_arg.startswith("LDKSixteenBytes"):
96-
fn_arg = "uint8_t (*" + fn_arg[16:] + ")[16]"
97-
assert var_is_arr_regex.match(fn_arg[8:])
98-
rust_obj = "LDKSixteenBytes"
99-
swift_raw_type = rust_obj
100-
arr_access = "data"
101-
elif fn_arg.startswith("LDKTwentyBytes"):
102-
fn_arg = "uint8_t (*" + fn_arg[15:] + ")[20]"
103-
assert var_is_arr_regex.match(fn_arg[8:])
104-
rust_obj = "LDKTwentyBytes"
105-
swift_raw_type = rust_obj
106-
arr_access = "data"
107-
elif fn_arg.startswith("LDKRecoverableSignature"):
108-
fn_arg = "uint8_t (*serialized_form)[68]"
109-
assert var_is_arr_regex.match(fn_arg[8:])
110-
rust_obj = "LDKRecoverableSignature"
111-
swift_raw_type = rust_obj
112-
arr_access = "serialized_form"
113-
elif fn_arg.startswith("LDKu8slice"):
114-
fn_arg = "uint8_t (*" + fn_arg[11:] + ")[datalen]"
115-
assert var_is_arr_regex.match(fn_arg[8:])
116-
rust_obj = "LDKu8slice"
117-
arr_access = "data"
118-
elif fn_arg.startswith("LDKCVec_u8Z"):
119-
fn_arg = "uint8_t (*" + fn_arg[12:] + ")[datalen]"
120-
rust_obj = "LDKCVec_u8Z"
121-
swift_raw_type = rust_obj
122-
assert var_is_arr_regex.match(fn_arg[8:])
123-
arr_access = "data"
124-
elif fn_arg.startswith("LDKCVec_u5Z"):
125-
prefix_length = len('LDKCVec_u5Z') + 1
126-
fn_arg = "uint8_t (*" + fn_arg[prefix_length:] + ")[data]"
127-
rust_obj = "LDKCVec_u5Z"
128-
swift_raw_type = rust_obj
129-
assert var_is_arr_regex.match(fn_arg[8:])
130-
arr_access = "data"
131-
elif fn_arg.startswith("LDKTransaction ") or fn_arg == "LDKTransaction":
132-
fn_arg = "uint8_t (*" + fn_arg[15:] + ")[datalen]"
133-
rust_obj = "LDKTransaction"
134-
swift_raw_type = rust_obj
135-
assert var_is_arr_regex.match(fn_arg[8:])
136-
arr_access = "data"
137-
elif fn_arg.startswith("LDKCVec_"):
41+
for type_name, access_details in array_accessor_types.items():
42+
if fn_arg.startswith(f'{type_name} ') or fn_arg == type_name: # include space
43+
rust_obj = type_name
44+
arr_access = access_details.key
45+
prefix_length = len(type_name) + 1
46+
if access_details.size > 0:
47+
fn_arg = "uint8_t (*" + fn_arg[prefix_length:] + f")[{access_details.size}]"
48+
assert var_is_arr_regex.match(fn_arg[8:])
49+
swift_raw_type = rust_obj
50+
elif access_details.size == -1 and access_details.length_key is not None:
51+
fn_arg = "uint8_t (*" + fn_arg[prefix_length:] + f")[{access_details.length_key}]"
52+
assert var_is_arr_regex.match(fn_arg[8:])
53+
break
54+
if fn_arg.startswith("LDKCVec_"):
13855
is_ptr = False
13956
if "*" in fn_arg:
14057
fn_arg = fn_arg.replace("*", "")

0 commit comments

Comments
 (0)