Skip to content

Commit a615096

Browse files
committed
Ensure that conversions are defined for lambdas defined in struct
In the case of Swift language, when lambda was defined in a structure, then the code could not compile because of missing conversion functions. Interestingly, that was not the case for classes. After investigation it turned out, that the root cause is related to missing inclusion of SwiftLambdaConversion template in SwiftStructConversion. This change: - adds missing inclusion of lambda conversions in the mentioned template (SwiftStructConversion.mustache) - implements functional tests for Swift to check that the fix works as expected - implements functional tests for Dart and Android to also cover the tested case in these technologies - updates the reference files for smoke tests related to definition of nested fields in structs Signed-off-by: Patryk Wrobel <[email protected]>
1 parent 57ec8d8 commit a615096

File tree

8 files changed

+202
-7
lines changed

8 files changed

+202
-7
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Gluecodium project Release Notes
22

3+
## Unreleased
4+
### Bug fixes:
5+
* Added missing generation of conversion functions for lambdas defined in structs for Swift.
6+
37
## 13.9.5
48
Release date: 2024-10-22
59
### Features:

functional-tests/functional/android/src/test/java/com/here/android/test/LambdasTest.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2016-2019 HERE Europe B.V.
2+
* Copyright (C) 2016-2024 HERE Europe B.V.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -160,4 +160,12 @@ public void callJavaOverloadedLambdaInCpp() {
160160

161161
assertEquals("42", result);
162162
}
163+
164+
@Test
165+
public void callJavaLambdaFromCppForLambdaDefinedInStruct() {
166+
StructWithLambda.LambdaCallback callback = (String arg) -> arg;
167+
String result = StructWithLambda.invokeCallback(callback);
168+
169+
assertEquals("some callback argument", result);
170+
}
163171
}

functional-tests/functional/dart/test/Lambdas_test.dart

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// -------------------------------------------------------------------------------------------------
2-
// Copyright (C) 2016-2020 HERE Europe B.V.
2+
// Copyright (C) 2016-2024 HERE Europe B.V.
33
//
44
// Licensed under the Apache License, Version 2.0 (the "License");
55
// you may not use this file except in compliance with the License.
@@ -130,4 +130,11 @@ void main() {
130130

131131
expect(result, isNull);
132132
});
133+
_testSuite.test("Call Dart lambda in C++ for lambda defined in struct", () {
134+
final callback = (String? arg) => arg;
135+
136+
final result = StructWithLambda.invokeCallback(callback);
137+
138+
expect(result!, "some callback argument");
139+
});
133140
}

functional-tests/functional/input/lime/Lambdas.lime

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (C) 2016-2019 HERE Europe B.V.
1+
# Copyright (C) 2016-2024 HERE Europe B.V.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -61,6 +61,12 @@ class LambdasDeclarationOrder {
6161
}
6262
}
6363

64+
struct StructWithLambda {
65+
lambda LambdaCallback = (String?) -> String?
66+
67+
static fun invoke_callback(callback: LambdaCallback?): String?
68+
}
69+
6470
interface LambdasInterface {
6571
lambda TakeScreenshotCallback = (Blob?) -> Void
6672

functional-tests/functional/input/src/cpp/Lambdas.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// -------------------------------------------------------------------------------------------------
2-
// Copyright (C) 2016-2019 HERE Europe B.V.
2+
// Copyright (C) 2016-2024 HERE Europe B.V.
33
//
44
// Licensed under the Apache License, Version 2.0 (the "License");
55
// you may not use this file except in compliance with the License.
@@ -20,8 +20,11 @@
2020

2121
#include "test/CallOverloadedLambda.h"
2222
#include "test/ClassWithInternalLambda.h"
23+
#include "test/StructWithLambda.h"
2324
#include "test/Lambdas.h"
2425

26+
#include <functional>
27+
2528
namespace test
2629
{
2730

@@ -152,4 +155,13 @@ CallOverloadedLambda::invoke_overloaded_lambda(const OverloadedLambda& lambda,
152155
return lambda(value);
153156
}
154157

158+
std::optional<std::string>
159+
StructWithLambda::invoke_callback(const std::optional<LambdaCallback>& callback) {
160+
if (callback) {
161+
return std::invoke(*callback, "some callback argument");
162+
}
163+
164+
return {};
165+
}
166+
155167
}

functional-tests/functional/swift/Tests/LambdasTests.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// -------------------------------------------------------------------------------------------------
2-
// Copyright (C) 2016-2019 HERE Europe B.V.
2+
// Copyright (C) 2016-2024 HERE Europe B.V.
33
//
44
// Licensed under the Apache License, Version 2.0 (the "License");
55
// you may not use this file except in compliance with the License.
@@ -128,6 +128,13 @@ class LambdasTests: XCTestCase {
128128
XCTAssertNil(result)
129129
}
130130

131+
func testCallingSwiftLambdaFromCppForLambdaDefinedInStruct() {
132+
let callback = { (arg: String?) in return arg }
133+
let result = StructWithLambda.invokeCallback(callback: callback)
134+
135+
XCTAssertEqual(result, "some callback argument");
136+
}
137+
131138
static var allTests = [
132139
("testCppLambdaInSwift", testCppLambdaInSwift),
133140
("testSwiftLambdaInCpp", testSwiftLambdaInCpp),
@@ -143,6 +150,7 @@ class LambdasTests: XCTestCase {
143150
("testCppNullableLambdaInSwiftWithValue", testCppNullableLambdaInSwiftWithValue),
144151
("testCppNullableLambdaInSwiftWithNil", testCppNullableLambdaInSwiftWithNil),
145152
("testSwiftNullableLambdaInCppWithValue", testSwiftNullableLambdaInCppWithValue),
146-
("testSwiftNullableLambdaInCppWithNil", testSwiftNullableLambdaInCppWithNil)
153+
("testSwiftNullableLambdaInCppWithNil", testSwiftNullableLambdaInCppWithNil),
154+
("testCallingSwiftLambdaFromCppForLambdaDefinedInStruct", testCallingSwiftLambdaFromCppForLambdaDefinedInStruct)
147155
]
148156
}

gluecodium/src/main/resources/templates/swift/SwiftStructConversion.mustache

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{{!!
22
!
3-
! Copyright (C) 2016-2019 HERE Europe B.V.
3+
! Copyright (C) 2016-2024 HERE Europe B.V.
44
!
55
! Licensed under the Apache License, Version 2.0 (the "License");
66
! you may not use this file except in compliance with the License.
@@ -95,6 +95,9 @@ Optionals
9595
{{#structs}}
9696
{{>swift/SwiftStructConversion}}{{!!
9797
}}{{/structs}}
98+
{{#lambdas}}
99+
{{>swift/SwiftLambdaConversion}}{{!!
100+
}}{{/lambdas}}
98101
{{#enumerations}}
99102
{{>swift/SwiftEnumConversion}}{{!!
100103
}}{{/enumerations}}

0 commit comments

Comments
 (0)