-
Notifications
You must be signed in to change notification settings - Fork 35
Prototyping Swift Array Accessor #207
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
Draft
ktoso
wants to merge
5
commits into
swiftlang:main
Choose a base branch
from
ktoso:wip-swift-array-accessor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
49d8ffa
Prototyping Swift Array Accessor
ktoso 8071555
jextract visitor: allow importing struct decl
ktoso e54d00e
Import and allow init, returning, destroying Swift `struct`s
ktoso c0dac2c
wip
ktoso 2afc1d9
correct how we get the unsafe self pointer of a struct
ktoso 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
68 changes: 68 additions & 0 deletions
68
Samples/SwiftKitSampleApp/Sources/MySwiftLibrary/MySwiftLibrary+ManualThunks.swift
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,68 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2024 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import SwiftKitSwift | ||
|
||
@_cdecl("swiftjava_manual_getArrayMySwiftClass") | ||
public func swiftjava_manual_getArrayMySwiftClass() -> UnsafeMutableRawPointer /* [MySwiftClass] */ { | ||
p("[thunk] swiftjava_manual_getArrayMySwiftClass") | ||
var array: [MySwiftClass] = getArrayMySwiftClass() | ||
p("[thunk] swiftjava_manual_getArrayMySwiftClass -> \(array)") | ||
// TODO: we need to retain it I guess as we escape it into Java | ||
var ptr: UnsafeRawBufferPointer! | ||
array.withUnsafeBytes { | ||
ptr = $0 | ||
} | ||
p("[thunk] swiftjava_manual_getArrayMySwiftClass -> \(ptr)") | ||
|
||
return UnsafeMutableRawPointer(mutating: ptr!.baseAddress)! | ||
} | ||
|
||
@_cdecl("swiftjava_SwiftKitSwift_Array_count") // FIXME: hardcoded for MySwiftClass | ||
public func swiftjava_SwiftKitSwift_Array_count( | ||
rawPointer: UnsafeMutableRawPointer, // Array<T> | ||
elementType: UnsafeMutableRawPointer // Metadata of T | ||
) -> Int { | ||
print("[swift][\(#fileID):\(#line)](\(#function) passed in rawPointer = \(rawPointer)") | ||
print("[swift][\(#fileID):\(#line)](\(#function) passed in metadata = \(elementType)") | ||
|
||
let array = rawPointer.assumingMemoryBound(to: [MySwiftClass].self) | ||
.pointee | ||
|
||
print("[swift][\(#fileID):\(#line)](\(#function) ARRAY count = \(array.count)") | ||
print("[swift][\(#fileID):\(#line)](\(#function) ARRAY[0] = \(unsafeBitCast(array[0], to: UInt64.self))") | ||
return array.count | ||
} | ||
|
||
@_cdecl("swiftjava_SwiftKitSwift_Array_get") // FIXME: hardcoded for MySwiftClass | ||
public func swiftjava_SwiftKitSwift_Array_get( | ||
rawPointer: UnsafeMutableRawPointer, // Array<T> | ||
index: Int, | ||
elementType: UnsafeMutableRawPointer // Metadata of T | ||
) -> UnsafeMutableRawPointer { | ||
print("[swift][\(#fileID):\(#line)](\(#function) passed in rawPointer = \(rawPointer)") | ||
print("[swift][\(#fileID):\(#line)](\(#function) passed in index = \(index)") | ||
print("[swift][\(#fileID):\(#line)](\(#function) passed in metadata = \(elementType)") | ||
|
||
let array: UnsafeMutableBufferPointer<MySwiftClass> = UnsafeMutableBufferPointer( | ||
start: rawPointer.assumingMemoryBound(to: MySwiftClass.self), | ||
count: 999 // FIXME: we need this to be passed in | ||
) | ||
|
||
print("[swift][\(#fileID):\(#line)](\(#function) ARRAY[\(index)] = \(unsafeBitCast(array[index], to: UInt64.self))") | ||
let object = array[index] | ||
|
||
let objectPointer = unsafeBitCast(object, to: UnsafeMutableRawPointer.self) | ||
return _swiftjava_swift_retain(object: objectPointer) | ||
} |
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 |
---|---|---|
|
@@ -35,6 +35,10 @@ public func globalMakeInt() -> Int { | |
return 42 | ||
} | ||
|
||
public func getMySwiftClassUntyped<T>(as: T.Type) -> Any { | ||
return MySwiftClass(len: 1, cap: 2) | ||
} | ||
|
||
public func globalWriteString(string: String) -> Int { | ||
return string.count | ||
} | ||
|
@@ -47,6 +51,30 @@ public func globalCallMeRunnable(run: () -> ()) { | |
run() | ||
} | ||
|
||
public func getArrayInt() -> [Int] { | ||
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | ||
} | ||
|
||
let DATA = [ | ||
MySwiftClass(len: 1, cap: 11), | ||
MySwiftClass(len: 2, cap: 22), | ||
MySwiftClass(len: 3, cap: 33), | ||
] | ||
|
||
public func getArrayMySwiftClass() -> [MySwiftClass] { | ||
DATA | ||
} | ||
|
||
|
||
let BYTES_DATA: [UInt8] = [ | ||
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, | ||
] | ||
|
||
|
||
public func getByteArray() -> [UInt8] { | ||
BYTES_DATA | ||
} | ||
|
||
public class MySwiftClass { | ||
|
||
public var len: Int | ||
|
@@ -64,7 +92,7 @@ public class MySwiftClass { | |
|
||
deinit { | ||
let addr = unsafeBitCast(self, to: UInt64.self) | ||
p("Deinit, self = 0x\(String(addr, radix: 16, uppercase: true))") | ||
p("MySwiftClass.deinit, self = 0x\(String(addr, radix: 16, uppercase: true))") | ||
} | ||
|
||
public var counter: Int32 = 0 | ||
|
@@ -77,6 +105,15 @@ public class MySwiftClass { | |
p("i:\(i)") | ||
} | ||
|
||
// TODO: workaround until we expose properties again | ||
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. TODO: Seems we didn't yet finish moving to the "thunk way" the properties, gotta do that. |
||
public func getterForLen() -> Int { | ||
len | ||
} | ||
// TODO: workaround until we expose properties again | ||
public func getterForCap() -> Int { | ||
cap | ||
} | ||
|
||
public func echoIntMethod(i: Int) -> Int { | ||
p("i:\(i)") | ||
return i | ||
|
@@ -99,9 +136,9 @@ public class MySwiftClass { | |
|
||
// ==== Internal helpers | ||
|
||
private func p(_ msg: String, file: String = #fileID, line: UInt = #line, function: String = #function) { | ||
// print("[swift][\(file):\(line)](\(function)) \(msg)") | ||
// fflush(stdout) | ||
package func p(_ msg: String, file: String = #fileID, line: UInt = #line, function: String = #function) { | ||
print("[swift][\(file):\(line)](\(function)) \(msg)") | ||
fflush(stdout) | ||
} | ||
|
||
#if os(Linux) | ||
|
60 changes: 60 additions & 0 deletions
60
Samples/SwiftKitSampleApp/Sources/MySwiftLibrary/MySwiftStruct.swift
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,60 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2024 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#if os(Linux) | ||
import Glibc | ||
#else | ||
import Darwin.C | ||
#endif | ||
|
||
public struct MySwiftStruct { | ||
private var numberA: Int | ||
private var numberB: Int | ||
private var numberC: Int | ||
private var numberD: Int | ||
|
||
public init(number: Int) { | ||
self.numberA = number | ||
self.numberB = number | ||
self.numberC = number | ||
self.numberD = number | ||
} | ||
|
||
public func getTheNumber() -> Int { | ||
numberA | ||
} | ||
} | ||
|
||
public struct MyHugeSwiftStruct { | ||
private var numberA: Int | ||
private var numberB: Int | ||
private var numberC: Int | ||
private var numberD: Int | ||
|
||
private var numberA2: Int = 0 | ||
private var numberB2: Int = 0 | ||
private var numberC2: Int = 0 | ||
private var numberD2: Int = 0 | ||
|
||
public init(number: Int) { | ||
self.numberA = number | ||
self.numberB = number | ||
self.numberC = number | ||
self.numberD = number | ||
} | ||
|
||
public func getTheNumber() -> Int { | ||
numberA | ||
} | ||
} |
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.
TODO: make it not global and deal with lifetime