-
Notifications
You must be signed in to change notification settings - Fork 46
[JExtract] Unify mechanisms between value types and reference types #240
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -290,11 +290,8 @@ extension Swift2JavaTranslator { | |
parentProtocol = "SwiftValue" | ||
} | ||
|
||
printer.printTypeDecl("public final class \(decl.javaClassName) implements \(parentProtocol)") { | ||
printer.printTypeDecl("public final class \(decl.javaClassName) extends SwiftInstance implements \(parentProtocol)") { | ||
printer in | ||
// ==== Storage of the class | ||
printClassSelfProperty(&printer, decl) | ||
printStatusFlagsField(&printer, decl) | ||
|
||
// Constants | ||
printClassConstants(printer: &printer) | ||
|
@@ -400,35 +397,6 @@ extension Swift2JavaTranslator { | |
) | ||
} | ||
|
||
/// Print a property where we can store the "self" pointer of a class. | ||
private func printClassSelfProperty(_ printer: inout CodePrinter, _ decl: ImportedNominalType) { | ||
printer.print( | ||
""" | ||
// Pointer to the referred to class instance's "self". | ||
private final MemorySegment selfMemorySegment; | ||
|
||
public final MemorySegment $memorySegment() { | ||
return this.selfMemorySegment; | ||
} | ||
""" | ||
) | ||
} | ||
|
||
private func printStatusFlagsField(_ printer: inout CodePrinter, _ decl: ImportedNominalType) { | ||
printer.print( | ||
""" | ||
// 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); | ||
|
||
@Override | ||
public final AtomicBoolean $statusDestroyedFlag() { | ||
return this.$state$destroyed; | ||
} | ||
""" | ||
) | ||
} | ||
|
||
private func printClassMemoryLayout(_ printer: inout CodePrinter, _ decl: ImportedNominalType) { | ||
printer.print( | ||
""" | ||
|
@@ -472,30 +440,11 @@ extension Swift2JavaTranslator { | |
\(decl.renderCommentSnippet ?? " *") | ||
*/ | ||
public \(parentName.unqualifiedJavaTypeName)(\(renderJavaParamDecls(decl, paramPassingStyle: .wrapper))) { | ||
this(/*arena=*/null, \(renderForwardJavaParams(decl, paramPassingStyle: .wrapper))); | ||
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. I wonder if we should remove the constructors without an arena passed in actually… fine to keep in this pr, wdyt tho? 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. I'm fine with removing this constructor as we don't want users to use try(var arena = SwiftArena.ofConfined()) {
var obj = new MyClass(arena);
var value = new MyStruct(); // Accidental use of "auto" arena.
obj.process(value); // Probably fine, but...
} This is actually probably fine, but still it's not ideal. 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. Yeah, it’ll be more consistent to just always pass arenas, might save us weird headaches down the line. you can remove here or we’ll do later, your call 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. Let's do it in this PR 👍 |
||
this(SwiftArena.ofAuto(), \(renderForwardJavaParams(decl, paramPassingStyle: .wrapper))); | ||
} | ||
""" | ||
) | ||
|
||
let initializeMemorySegment = | ||
if let parent = decl.parent, | ||
parent.isReferenceType | ||
{ | ||
""" | ||
this.selfMemorySegment = (MemorySegment) mh$.invokeExact( | ||
\(renderForwardJavaParams(decl, paramPassingStyle: nil)) | ||
); | ||
""" | ||
} else { | ||
""" | ||
this.selfMemorySegment = arena.allocate($layout()); | ||
mh$.invokeExact( | ||
\(renderForwardJavaParams(decl, paramPassingStyle: nil)), | ||
/* indirect return buffer */this.selfMemorySegment | ||
); | ||
""" | ||
} | ||
|
||
printer.print( | ||
""" | ||
/** | ||
|
@@ -505,20 +454,23 @@ extension Swift2JavaTranslator { | |
\(decl.renderCommentSnippet ?? " *") | ||
*/ | ||
public \(parentName.unqualifiedJavaTypeName)(SwiftArena arena, \(renderJavaParamDecls(decl, paramPassingStyle: .wrapper))) { | ||
var mh$ = \(descClassIdentifier).HANDLE; | ||
try { | ||
super(() -> { | ||
var mh$ = \(descClassIdentifier).HANDLE; | ||
try { | ||
MemorySegment _result = arena.allocate($LAYOUT); | ||
|
||
if (SwiftKit.TRACE_DOWNCALLS) { | ||
SwiftKit.traceDowncall(\(renderForwardJavaParams(decl, paramPassingStyle: nil))); | ||
} | ||
|
||
\(initializeMemorySegment) | ||
|
||
if (arena != null) { | ||
arena.register(this); | ||
} | ||
} catch (Throwable ex$) { | ||
throw new AssertionError("should not reach here", ex$); | ||
} | ||
mh$.invokeExact( | ||
\(renderForwardJavaParams(decl, paramPassingStyle: nil)), | ||
/* indirect return buffer */_result | ||
); | ||
return _result; | ||
} catch (Throwable ex$) { | ||
throw new AssertionError("should not reach here", ex$); | ||
} | ||
}, arena); | ||
} | ||
""" | ||
) | ||
|
@@ -714,9 +666,7 @@ extension Swift2JavaTranslator { | |
let guardFromDestroyedObjectCalls: String = | ||
if decl.hasParent { | ||
""" | ||
if (this.$state$destroyed.get()) { | ||
throw new IllegalStateException("Attempted to call method on already destroyed instance of " + getClass().getSimpleName() + "!"); | ||
} | ||
$ensureAlive(); | ||
""" | ||
} else { "" } | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,18 +34,18 @@ public SwiftAnyType(MemorySegment memorySegment) { | |
this.memorySegment = memorySegment.asReadOnly(); | ||
} | ||
|
||
public SwiftAnyType(SwiftHeapObject object) { | ||
if (object.$layout().name().isEmpty()) { | ||
throw new IllegalArgumentException("SwiftHeapObject must have a mangled name in order to obtain its SwiftType."); | ||
} | ||
|
||
String mangledName = object.$layout().name().get(); | ||
var type = SwiftKit.getTypeByMangledNameInEnvironment(mangledName); | ||
if (type.isEmpty()) { | ||
throw new IllegalArgumentException("A Swift Any.Type cannot be null!"); | ||
} | ||
this.memorySegment = type.get().memorySegment; | ||
} | ||
// public SwiftAnyType(SwiftHeapObject object) { | ||
// if (object.$layout().name().isEmpty()) { | ||
// throw new IllegalArgumentException("SwiftHeapObject must have a mangled name in order to obtain its SwiftType."); | ||
// } | ||
// | ||
// String mangledName = object.$layout().name().get(); | ||
// var type = SwiftKit.getTypeByMangledNameInEnvironment(mangledName); | ||
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. I don't feel we need this function anymore. Every instance has 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. Removed 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. Yeah let's remove, it is from the time when we were still calling directly into initializers from the java side I think |
||
// if (type.isEmpty()) { | ||
// throw new IllegalArgumentException("A Swift Any.Type cannot be null!"); | ||
// } | ||
// this.memorySegment = type.get().memorySegment; | ||
// } | ||
|
||
|
||
public MemorySegment $memorySegment() { | ||
|
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.
Ok 👍