-
Notifications
You must be signed in to change notification settings - Fork 49
[SwiftKit] Move 'selfPointer' to 'JNISwiftInstance' & downcall trace logs #322
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
Conversation
7c96b3b
to
e7612f0
Compare
`FFMSwiftInstance` doesn't need it.
2d2318a
to
49e95c6
Compare
|
||
let returnKeyword = translatedDecl.translatedFunctionSignature.resultType.isVoid ? "" : "return " | ||
|
||
printer.print( | ||
""" | ||
long selfPointer = this.pointer(); | ||
long \(selfVarName) = this.$memoryAddress(); |
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.
These are basically unsafe, so I figured we shoudl hide them using our $ pattern.
@@ -206,13 +206,15 @@ extension JNISwift2JavaGenerator { | |||
printDeclDocumentation(&printer, decl) | |||
printer.printBraceBlock("public \(renderFunctionSignature(decl))") { printer in | |||
var arguments = translatedDecl.translatedFunctionSignature.parameters.map(\.name) | |||
arguments.append("selfPointer") | |||
|
|||
let selfVarName = "self$" |
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.
The reason we're doing $
in names in bodies is so they don't clash with any parameter name that someone can come up with, so I moved to self$ here -- FYI @madsodgaard
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.
good idea!
CallTraces.traceDowncall("\(type.swiftNominal.name).\(funcName)", | ||
"this", this, | ||
"self", self$); | ||
} |
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.
a bit of downtracing helped diagnose issues
fatalError("Missing self pointer in call to \\(#function)!") | ||
} | ||
""" | ||
) |
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.
This code was repeated 2 times, so now it's in 1 place.
The reason to split it up into more lines and not just the single line with multiple !
is so we have better messages when things fail; It took me a while to diagnose an issue without this so I'd suggest to keep this in.
|
||
// TODO: make this a flagset integer and/or use a field updater | ||
/** Used to track additional state of the underlying object, e.g. if it was explicitly destroyed. */ | ||
private final AtomicBoolean $state$destroyed = new AtomicBoolean(false); |
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.
fields should be before methods; we should install a formatting tool tbh
@@ -42,7 +55,7 @@ protected JNISwiftInstance(long pointer, SwiftArena arena) { | |||
protected abstract Runnable $createDestroyFunction(); | |||
|
|||
@Override | |||
public SwiftInstanceCleanup createCleanupAction() { | |||
public SwiftInstanceCleanup $createCleanup() { |
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.
Also not a method end users should be invoking explicitly, so changed it while at it.
@@ -17,49 +17,38 @@ | |||
import java.util.concurrent.atomic.AtomicBoolean; | |||
|
|||
public abstract class SwiftInstance { | |||
/// Pointer to the "self". | |||
private final long selfPointer; |
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.
"The change" the SwiftInstance
does not need storage, we can just have FFM or JNI one implement the abstract method instead
*/ | ||
protected SwiftInstance(long pointer, SwiftArena arena) { | ||
this.selfPointer = pointer; | ||
arena.register(this); |
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.
Necessary to remove this since otherwise we'd race the register with a non-initialized memory address value in the class calling super()
/** | ||
* Utility functions, similar to @{link java.util.Objects} | ||
*/ | ||
public class SwiftObjects { |
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.
Following java.util.Objects
but with more helpers for us ;)
@Override | ||
public long $memoryAddress() { | ||
return $memorySegment().address(); | ||
} |
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.
:-)
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.
Very nice 👍
let selfBits$ = Int(Int64(fromJNI: \(selfPointerParam.name), in: env$)) | ||
assert(selfBits$ != 0, "$self memory address was null: \(selfPointerParam.name) = \\(\(selfPointerParam.name))" ) | ||
guard let \(newSelfParamName) = UnsafeMutablePointer<\(swiftParentName)>(bitPattern: selfBits$) else { | ||
fatalError("Missing self pointer in call to \\(#function)!") |
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.
I think we can move the above assert message to this fatalError instead. The UnsafeMutablePointer
initializer will fail if the bitPattern is zero.
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.
Good point, done
Continued spm instability on linux :( |
Seems I could not push "to" the PR here #321
Minor refactor, we can easily keep the pointer in the base type