-
Notifications
You must be signed in to change notification settings - Fork 46
[JExtract/FFM] Translate 'some DataProtocol' parameters to 'Data' #303
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ | |
set -x | ||
set -e | ||
|
||
./gradlew run | ||
./gradlew run | ||
./gradlew test |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
Samples/SwiftKitSampleApp/src/test/java/com/example/swift/DataImportTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2025 Apple Inc. and the Swift.org project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of Swift.org project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
package com.example.swift; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.swift.swiftkit.ffm.AllocatingSwiftArena; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
public class DataImportTest { | ||
@Test | ||
void test_Data_receiveAndReturn() { | ||
try (var arena = AllocatingSwiftArena.ofConfined()) { | ||
var origBytes = arena.allocateFrom("foobar"); | ||
var origDat = Data.init(origBytes, origBytes.byteSize(), arena); | ||
assertEquals(7, origDat.getCount()); | ||
|
||
var retDat = MySwiftLibrary.globalReceiveReturnData(origDat, arena); | ||
assertEquals(7, retDat.getCount()); | ||
retDat.withUnsafeBytes((retBytes) -> { | ||
assertEquals(7, retBytes.byteSize()); | ||
var str = retBytes.getString(0); | ||
assertEquals("foobar", str); | ||
}); | ||
} | ||
} | ||
|
||
@Test | ||
void test_DataProtocol_receive() { | ||
try (var arena = AllocatingSwiftArena.ofConfined()) { | ||
var bytes = arena.allocateFrom("hello"); | ||
var dat = Data.init(bytes, bytes.byteSize(), arena); | ||
var result = MySwiftLibrary.globalReceiveSomeDataProtocol(dat); | ||
assertEquals(6, result); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,6 +77,10 @@ extension FFMSwift2JavaGenerator { | |
struct CdeclLowering { | ||
var knownTypes: SwiftKnownTypes | ||
|
||
init(knownTypes: SwiftKnownTypes) { | ||
self.knownTypes = knownTypes | ||
} | ||
|
||
init(symbolTable: SwiftSymbolTable) { | ||
self.knownTypes = SwiftKnownTypes(symbolTable: symbolTable) | ||
} | ||
|
@@ -165,7 +169,7 @@ struct CdeclLowering { | |
) | ||
|
||
case .nominal(let nominal): | ||
if let knownType = nominal.nominalTypeDecl.knownStandardLibraryType { | ||
if let knownType = nominal.nominalTypeDecl.knownTypeKind { | ||
if convention == .inout { | ||
// FIXME: Support non-trivial 'inout' for builtin types. | ||
throw LoweringError.inoutNotSupported(type) | ||
|
@@ -320,6 +324,18 @@ struct CdeclLowering { | |
conversion: conversion | ||
) | ||
|
||
case .opaque(let proto), .existential(let proto): | ||
// If the protocol has a known representative implementation, e.g. `String` for `StringProtocol` | ||
// Translate it as the concrete type. | ||
// NOTE: This is a temporary workaround until we add support for generics. | ||
if | ||
let knownProtocol = proto.asNominalTypeDeclaration?.knownTypeKind, | ||
let concreteTy = knownTypes.representativeType(of: knownProtocol) | ||
{ | ||
return try lowerParameter(concreteTy, convention: convention, parameterName: parameterName) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
throw LoweringError.unhandledType(type) | ||
|
||
case .optional: | ||
throw LoweringError.unhandledType(type) | ||
} | ||
|
@@ -386,7 +402,7 @@ struct CdeclLowering { | |
|
||
switch type { | ||
case .nominal(let nominal): | ||
if let knownType = nominal.nominalTypeDecl.knownStandardLibraryType { | ||
if let knownType = nominal.nominalTypeDecl.knownTypeKind { | ||
switch knownType { | ||
case .unsafeRawBufferPointer, .unsafeMutableRawBufferPointer: | ||
// pointer buffers are lowered to (raw-pointer, count) pair. | ||
|
@@ -421,7 +437,7 @@ struct CdeclLowering { | |
// Custom types are not supported yet. | ||
throw LoweringError.unhandledType(type) | ||
|
||
case .function, .metatype, .optional, .tuple: | ||
case .function, .metatype, .optional, .tuple, .existential, .opaque: | ||
// TODO: Implement | ||
throw LoweringError.unhandledType(type) | ||
} | ||
|
@@ -454,7 +470,7 @@ struct CdeclLowering { | |
|
||
case .nominal(let nominal): | ||
// Types from the Swift standard library that we know about. | ||
if let knownType = nominal.nominalTypeDecl.knownStandardLibraryType { | ||
if let knownType = nominal.nominalTypeDecl.knownTypeKind { | ||
switch knownType { | ||
case .unsafePointer, .unsafeMutablePointer: | ||
// Typed pointers are lowered to corresponding raw forms. | ||
|
@@ -575,7 +591,7 @@ struct CdeclLowering { | |
conversion: .tupleExplode(conversions, name: outParameterName) | ||
) | ||
|
||
case .function(_), .optional(_): | ||
case .function, .optional, .existential, .opaque: | ||
throw LoweringError.unhandledType(type) | ||
} | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ktoso I'd be happy to make this and
Data
thing test cases, but where exactly I should write? Any PR testing runsSamples:SwiftKitSampleApp:test
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So every example has a
ci-validate.sh
and you can add a./gradle test
in there inSamples/SwiftKitSampleApp/ci-validate.sh
and add a test inSamples/SwiftKitSampleApp/src/test/java/com/example/swift
:)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh boy I just realized we have tests there but we've not been running them huh. so yeah please add a
gradle test
there :)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!