Skip to content

Commit 0622afa

Browse files
committed
update generator to clean up LDKStr and to not dangle NetGraphMsgHandler in BGProcessor initialization
1 parent 2d6e992 commit 0622afa

File tree

7 files changed

+37
-11
lines changed

7 files changed

+37
-11
lines changed

.editorconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
indent_style = tab
33
indent_size = 4
44
insert_final_newline = true
5+
trim_trailing_whitespace = true
56

67
[Makefile]
78

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ folder due to the previous `genbindings.sh` step. As Docker won't have access to
6767
replace those lines with the following:
6868

6969
```yaml
70-
lightning = { git = "https://github.com/thebluematt/rust-lightning", rev = "xxx", features = ["std"] }
71-
lightning-persister = { git = "https://github.com/thebluematt/rust-lightning", rev = "xxx" }
72-
lightning-invoice = { git = "https://github.com/thebluematt/rust-lightning", rev = "xxx" }
73-
lightning-background-processor = { git = "https://github.com/thebluematt/rust-lightning", rev = "xxx" }
70+
lightning = { git = "https://github.com/thebluematt/rust-lightning", branch = "2021-03-java-bindings-base", default-features = false }
71+
lightning-persister = { git = "https://github.com/thebluematt/rust-lightning", branch = "2021-03-java-bindings-base", default-features = false }
72+
lightning-invoice = { git = "https://github.com/thebluematt/rust-lightning", branch = "2021-03-java-bindings-base", default-features = false }
73+
lightning-background-processor = { git = "https://github.com/thebluematt/rust-lightning", branch = "2021-03-java-bindings-base", default-features = false }
7474
```
7575

7676
You will note that the revision is unspecified and is currently just placeholder `xxx`s. To obtain the revision,

ci/docker_shell.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ LDK_SUBDIRECTORY="$LDK_DIRECTORY/lightning-c-bindings"
1111
if [[ "${MOUNT_CI_FOLDER}" = "true" ]]; then
1212
# use for direct ci folder access
1313
cp -R bindings/batteries ci/LDKSwift/Sources/LDKSwift
14-
docker run --name "swift-generator-shell" -e MOUNTED_CI_FOLDER=true -i -v $PROJECT_DIRECTORY/ci:/ci -v $LDK_DIRECTORY:/ldk-c-bindings --rm -t swift-generation-ci /bin/bash
14+
# docker run --name "swift-generator-shell" -e MOUNTED_CI_FOLDER=true -i -v $PROJECT_DIRECTORY/ci:/ci -v $LDK_DIRECTORY:/ldk-c-bindings --rm -t swift-generation-ci /bin/bash
15+
docker run --name "swift-generator-shell" -e MOUNTED_CI_FOLDER=true -i -v $PROJECT_DIRECTORY/ci:/ci -v $LDK_DIRECTORY:/ldk-c-bindings -v $PROJECT_DIRECTORY/src:/src --rm -t swift-generation-ci /bin/bash
1516
elif [ "${MOUNT_CI_FOLDER}" = "false" ]; then
1617
docker run --name "swift-generator-shell" -e MOUNTED_CI_FOLDER=false -i -v $LDK_DIRECTORY:/ldk-c-bindings --rm -t swift-generation-ci /bin/bash
1718
else

ci/run_tests.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,6 @@ python3 ./fix_header_includes.py
3030
python3 ./fix_swift_imports.py
3131

3232
pushd LDKSwift/
33-
swift test # -Xswiftc -suppress-warnings
34-
#swift test -Xswiftc -suppress-warnings
33+
#swift test
34+
swift test -Xswiftc -suppress-warnings
35+
#swift test --filter 'testInvoiceSerialization' -Xswiftc -suppress-warnings

src/conversion_helper.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,17 @@ def is_instance_type(cls, swift_type: str, raw_rust_type: str, include_options_a
7474
def is_array_type(cls, swift_type: str):
7575
return swift_type.startswith('[')
7676

77+
@classmethod
78+
def is_type_cloneable(cls, raw_rust_type: str):
79+
cloneability_lookup = 'x-uncloneable'
80+
individual_cloneability_lookup = None
81+
if raw_rust_type is not None:
82+
cloneability_lookup = raw_rust_type
83+
if cloneability_lookup.startswith('LDK'):
84+
cloneability_lookup = cloneability_lookup[len('LDK'):]
85+
is_cloneable = cloneability_lookup in cloneable_types
86+
return is_cloneable
87+
7788
@classmethod
7889
def prepare_swift_to_native_arguments(cls, argument_types, is_trait_callback=False, force_pass_instance=False, is_free_method=False, is_returned_value_freeable=False, unwrap_complex_arrays = True, array_unwrapping_preparation_only = False, is_trait_default_redirect = False):
7990
swift_arguments = []
@@ -309,7 +320,7 @@ def prepare_swift_to_native_arguments(cls, argument_types, is_trait_callback=Fal
309320
force_unwrap_suffix = '!'
310321
native_arguments.append(f'Bindings.string_to_unsafe_int8_pointer(string: {passed_argument_name}{force_unwrap_suffix})')
311322
else:
312-
native_arguments.append(f'Bindings.new_LDKStr(string: {passed_argument_name})')
323+
native_arguments.append(f'Bindings.new_LDKStr(string: {passed_argument_name}, chars_is_owned: true)')
313324
elif current_argument_details.rust_obj is None and current_argument_details.arr_len is not None and current_argument_details.arr_len.isnumeric():
314325
if current_argument_details.is_const:
315326
force_unwrap_suffix = ''
@@ -372,7 +383,16 @@ def prepare_native_to_swift_callback_arguments(cls, argument_types, array_unwrap
372383
swift_local_conversion_suffix = ')'
373384
# TODO: see if `current_argument_details.pass_by_ref` condition is necessary
374385
if current_argument_details.passed_as_ptr and current_argument_details.pass_by_ref:
386+
# this is useful for traits, but some types may not have cloneability support
375387
swift_local_conversion_suffix = ').dangle()'
388+
is_cloneable = ConversionHelper.is_type_cloneable(received_raw_type)
389+
if is_cloneable:
390+
swift_local_conversion_suffix += '.clone()'
391+
else:
392+
print(f"trait callback uncloneable type danger: {received_raw_type} / {published_swift_type}")
393+
elif current_argument_details.passed_as_ptr or current_argument_details.pass_by_ref:
394+
descriptor = 'passed_as_ptr' if current_argument_details.passed_as_ptr else 'pass_by_ref'
395+
print(f'{descriptor}: {received_raw_type} / {published_swift_type}')
376396
elif received_raw_type.startswith('LDKCOption_') and received_raw_type == 'LDKC' + published_swift_type:
377397
swift_local_conversion_prefix = f'{published_swift_type}(pointer: '
378398
swift_local_conversion_suffix = ')'

src/generators/opaque_struct_generator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def generate_opaque_struct(self, struct_name, struct_details, all_type_details={
6969
''')
7070

7171
constructor_swift_arguments[4] = new_swift_argument
72-
constructor_native_call_prep += '\nlet graphMessageHandler = net_graph_msg_handler?.dangle().cOpaqueStruct! ?? LDKNetGraphMsgHandler(inner: nil, is_owned: true)\n'
72+
constructor_native_call_prep += '\nlet graphMessageHandler = net_graph_msg_handler?.cOpaqueStruct! ?? LDKNetGraphMsgHandler(inner: nil, is_owned: true)\n'
7373
constructor_post_init_anchoring = constructor_post_init_anchoring.replace('try? self.addAnchor(anchor: net_graph_msg_handler)', '''
7474
if let handler = net_graph_msg_handler {
7575
try? self.addAnchor(anchor: handler)

templates/BindingsTemplate.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ public class Bindings {
338338
}
339339
/* RUST_TO_SWIFT_END */
340340

341-
public class func LDKStr_to_string(nativeType: LDKStr) -> String {
341+
public class func LDKStr_to_string(nativeType: LDKStr, deallocate: Bool = true) -> String {
342342
var array = [UInt8]()
343343
for index in 0..<Int(nativeType.len) {
344344
let currentEntry = nativeType.chars[index]
@@ -347,6 +347,9 @@ public class Bindings {
347347
}
348348
let data = Data(bytes: array)
349349
let string = String(data: data, encoding: .utf8)!
350+
if deallocate && nativeType.len > 0{
351+
Str_free(nativeType)
352+
}
350353
return string
351354
}
352355

@@ -380,7 +383,7 @@ public class Bindings {
380383
let nativeKeysManager = keysManager.cOpaqueStruct!
381384
let amount = Option_u64Z(value: amountMsat)
382385
let nativeAmount = amount.cOpaqueStruct!
383-
let nativeDescription = Self.new_LDKStr(string: description)
386+
let nativeDescription = Self.new_LDKStr(string: description, chars_is_owned: true)
384387
return withUnsafePointer(to: channelManager.cOpaqueStruct!) { (pointer: UnsafePointer<LDKChannelManager>) -> Result_InvoiceSignOrCreationErrorZ in
385388
let nativeResult = create_invoice_from_channelmanager(pointer, nativeKeysManager, network, nativeAmount, nativeDescription)
386389
return Result_InvoiceSignOrCreationErrorZ(pointer: nativeResult)

0 commit comments

Comments
 (0)