22//
33// This source file is part of the Swift.org open source project
44//
5- // Copyright (c) 2024 Apple Inc. and the Swift.org project authors
5+ // Copyright (c) 2026 Apple Inc. and the Swift.org project authors
66// Licensed under Apache License v2.0
77//
88// See LICENSE.txt for license information
@@ -17,60 +17,65 @@ import Testing
1717
1818final class DataImportTests {
1919 private static let ifConfigImport = """
20- #if canImport(FoundationEssentials)
21- import FoundationEssentials
22- #else
23- import Foundation
24- #endif
25- """
20+ #if canImport(FoundationEssentials)
21+ import FoundationEssentials
22+ #else
23+ import Foundation
24+ #endif
25+ """
2626 private static let foundationData_interfaceFile =
2727 """
2828 import Foundation
29-
29+
3030 public func receiveData(dat: Data)
3131 public func returnData() -> Data
3232 """
3333
3434 private static let foundationDataProtocol_interfaceFile =
3535 """
3636 import Foundation
37-
37+
3838 public func receiveDataProtocol<T: DataProtocol>(dat: some DataProtocol, dat2: T?)
3939 """
4040
4141 private static let essentialsData_interfaceFile =
4242 """
4343 import FoundationEssentials
44-
44+
4545 public func receiveData(dat: Data)
4646 public func returnData() -> Data
4747 """
4848
4949 private static let essentialsDataProtocol_interfaceFile =
5050 """
5151 import FoundationEssentials
52-
52+
5353 public func receiveDataProtocol<T: DataProtocol>(dat: some DataProtocol, dat2: T?)
5454 """
5555 private static let ifConfigData_interfaceFile =
5656 """
5757 \( ifConfigImport)
58-
58+
5959 public func receiveData(dat: Data)
6060 public func returnData() -> Data
6161 """
6262
6363 private static let ifConfigDataProtocol_interfaceFile =
6464 """
6565 \( ifConfigImport)
66-
66+
6767 public func receiveDataProtocol<T: DataProtocol>(dat: some DataProtocol, dat2: T?)
6868 """
6969
70- @Test ( " Import Data: Swift thunks " , arguments: zip (
71- [ Self . foundationData_interfaceFile, Self . essentialsData_interfaceFile, Self . ifConfigData_interfaceFile] ,
72- [ " import Foundation " , " import FoundationEssentials " , Self . ifConfigImport]
73- ) )
70+ @Test (
71+ " Import Data: Swift thunks " ,
72+ arguments: zip (
73+ [
74+ Self . foundationData_interfaceFile, Self . essentialsData_interfaceFile,
75+ Self . ifConfigData_interfaceFile,
76+ ] ,
77+ [ " import Foundation " , " import FoundationEssentials " , Self . ifConfigImport]
78+ ) )
7479 func data_swiftThunk( fileContent: String , expectedImportChunk: String ) throws {
7580
7681 try assertOutput (
@@ -123,10 +128,13 @@ final class DataImportTests {
123128 ]
124129 )
125130 }
126-
127- @Test ( " Import Data: JavaBindings " , arguments: [
128- Self . foundationData_interfaceFile, Self . essentialsData_interfaceFile, Self . ifConfigData_interfaceFile
129- ] )
131+
132+ @Test (
133+ " Import Data: JavaBindings " ,
134+ arguments: [
135+ Self . foundationData_interfaceFile, Self . essentialsData_interfaceFile,
136+ Self . ifConfigData_interfaceFile,
137+ ] )
130138 func data_javaBindings( fileContent: String ) throws {
131139 try assertOutput (
132140 input: fileContent, . ffm, . java,
@@ -209,7 +217,6 @@ final class DataImportTests {
209217 }
210218 """ ,
211219
212-
213220 """
214221 /**
215222 * {@snippet lang=c :
@@ -245,8 +252,13 @@ final class DataImportTests {
245252 * public init(bytes: UnsafeRawPointer, count: Int)
246253 * }
247254 */
248- public static Data init(java.lang.foreign.MemorySegment bytes, long count, AllocatingSwiftArena swiftArena$) {
255+ public static Data init(java.lang.foreign.MemorySegment bytes, long count, AllocatingSwiftArena swiftArena$) throws SwiftIntegerOverflowException {
249256 MemorySegment _result = swiftArena$.allocate(Data.$LAYOUT);
257+ if (SwiftValueLayout.has32bitSwiftInt) {
258+ if (count < Integer.MIN_VALUE || count > Integer.MAX_VALUE) {
259+ throw new SwiftIntegerOverflowException( " Parameter 'count' overflow: " + count);
260+ }
261+ }
250262 swiftjava_SwiftModule_Data_init_bytes_count.call(bytes, count, _result);
251263 return Data.wrapMemoryAddressUnsafe(_result, swiftArena$);
252264 }
@@ -286,9 +298,15 @@ final class DataImportTests {
286298 * public var count: Int
287299 * }
288300 */
289- public long getCount() {
301+ public long getCount() throws SwiftIntegerOverflowException {
290302 $ensureAlive();
291- return swiftjava_SwiftModule_Data_count$get.call(this.$memorySegment());
303+ long _result$checked = swiftjava_SwiftModule_Data_count$get.call(this.$memorySegment());
304+ if (SwiftValueLayout.has32bitSwiftInt) {
305+ if (_result$checked < Integer.MIN_VALUE || _result$checked > Integer.MAX_VALUE) {
306+ throw new SwiftIntegerOverflowException( " Return value overflow: " + _result$checked);
307+ }
308+ }
309+ return _result$checked;
292310 }
293311 """ ,
294312
@@ -352,7 +370,6 @@ final class DataImportTests {
352370 }
353371 """ ,
354372
355-
356373 """
357374 /**
358375 * Downcall to Swift:
@@ -366,15 +383,20 @@ final class DataImportTests {
366383 swiftjava_SwiftModule_Data_withUnsafeBytes__.call(withUnsafeBytes.$toUpcallStub(body, arena$), this.$memorySegment());
367384 }
368385 }
369- """
386+ """ ,
370387 ]
371388 )
372389 }
373390
374- @Test ( " Import DataProtocol: Swift thunks " , arguments: zip (
375- [ Self . foundationDataProtocol_interfaceFile, Self . essentialsDataProtocol_interfaceFile, Self . ifConfigDataProtocol_interfaceFile] ,
376- [ " import Foundation " , " import FoundationEssentials " , Self . ifConfigImport]
377- ) )
391+ @Test (
392+ " Import DataProtocol: Swift thunks " ,
393+ arguments: zip (
394+ [
395+ Self . foundationDataProtocol_interfaceFile, Self . essentialsDataProtocol_interfaceFile,
396+ Self . ifConfigDataProtocol_interfaceFile,
397+ ] ,
398+ [ " import Foundation " , " import FoundationEssentials " , Self . ifConfigImport]
399+ ) )
378400 func dataProtocol_swiftThunk( fileContent: String , expectedImportChunk: String ) throws {
379401 try assertOutput (
380402 input: fileContent, . ffm, . swift,
@@ -390,14 +412,17 @@ final class DataImportTests {
390412 // Just to make sure 'Data' is imported.
391413 """
392414 @_cdecl( " swiftjava_getType_SwiftModule_Data " )
393- """
415+ """ ,
394416 ]
395417 )
396418 }
397419
398- @Test ( " Import DataProtocol: JavaBindings " , arguments: [
399- Self . foundationDataProtocol_interfaceFile, Self . essentialsDataProtocol_interfaceFile, Self . ifConfigDataProtocol_interfaceFile
400- ] )
420+ @Test (
421+ " Import DataProtocol: JavaBindings " ,
422+ arguments: [
423+ Self . foundationDataProtocol_interfaceFile, Self . essentialsDataProtocol_interfaceFile,
424+ Self . ifConfigDataProtocol_interfaceFile,
425+ ] )
401426 func dataProtocol_javaBindings( fileContent: String ) throws {
402427
403428 try assertOutput (
@@ -445,9 +470,9 @@ final class DataImportTests {
445470 // Just to make sure 'Data' is imported.
446471 """
447472 public final class Data extends FFMSwiftInstance implements SwiftValue {
448- """
473+ """ ,
449474 ]
450475 )
451476 }
452-
477+
453478}
0 commit comments