-
Notifications
You must be signed in to change notification settings - Fork 92
Cleanup local refs #616
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
Open
madsodgaard
wants to merge
7
commits into
swiftlang:main
Choose a base branch
from
madsodgaard:localref-cleanups
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.
+61
−4
Open
Cleanup local refs #616
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e9f4382
clear local ref in JavaObjectHolder
madsodgaard e5733df
cleanup localref in JNI cache
madsodgaard d995929
delete local refs when doing method lookups
madsodgaard a386a15
make frame count based on args
madsodgaard 8e491cd
more small cleanups
madsodgaard ce785da
fix tests
madsodgaard c702ebd
protocol wrappers emit a frame
madsodgaard 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -108,6 +108,7 @@ extension AnyJavaObject { | |
| let thisClass = try environment.translatingJNIExceptions { | ||
| environment.interface.GetObjectClass(environment, javaThis) | ||
| }! | ||
| defer { environment.interface.DeleteLocalRef(environment, thisClass) } | ||
|
|
||
| return try environment.translatingJNIExceptions { | ||
| try Self.javaMethodLookup( | ||
|
|
@@ -131,6 +132,7 @@ extension AnyJavaObject { | |
| let thisClass = try environment.translatingJNIExceptions { | ||
| environment.interface.GetObjectClass(environment, javaThis) | ||
| }! | ||
| defer { environment.interface.DeleteLocalRef(environment, thisClass) } | ||
|
|
||
| return try environment.translatingJNIExceptions { | ||
| try Self.javaMethodLookup( | ||
|
|
@@ -153,7 +155,11 @@ extension AnyJavaObject { | |
| ) throws -> Result { | ||
| // Retrieve the method that performs this call, then package the values and | ||
| // call it. | ||
| // Size the frame to 1 ref per argument (strings/objects each need 1; | ||
| // primitives need 0, so this slightly over-estimates) plus 1 for the result ref. | ||
| let jniMethod = Result.jniMethodCall(in: environment) | ||
| environment.interface.PushLocalFrame(environment, Int32(countArgs(repeat each args) + 2)) | ||
| defer { environment.interface.PopLocalFrame(environment, nil) } | ||
ktoso marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| let jniArgs = getJValues(repeat each args, in: environment) | ||
| let jniResult = try environment.translatingJNIExceptions { | ||
| jniMethod(environment, this, method, jniArgs) | ||
|
|
@@ -219,7 +225,10 @@ extension AnyJavaObject { | |
| ) throws { | ||
| // Retrieve the method that performs this call, then package the arguments | ||
| // and call it. | ||
| // Size the frame to 1 ref per argument; no result ref needed for void calls. | ||
| let jniMethod = environment.interface.CallVoidMethodA! | ||
| environment.interface.PushLocalFrame(environment, Int32(countArgs(repeat each args) + 1)) | ||
|
Collaborator
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. Again we need to check the result type, so I think we need a with... pattern here - proposing one here swiftlang/swift-java-jni-core#11 |
||
| defer { environment.interface.PopLocalFrame(environment, nil) } | ||
| let jniArgs = getJValues(repeat each args, in: environment) | ||
| try environment.translatingJNIExceptions { | ||
| jniMethod(environment, this, method, jniArgs) | ||
|
|
@@ -269,11 +278,16 @@ extension AnyJavaObject { | |
| in: environment | ||
| ) | ||
|
|
||
| // Retrieve the constructor, then map the arguments and call it. | ||
| // Retrieve the constructor, then map the arguments and call it. Use a local | ||
| // frame so args are freed; promote the new object to the outer frame | ||
| // via PopLocalFrame(result). | ||
| // Size the frame to 1 ref per argument; result is promoted via PopLocalFrame(newObj). | ||
| environment.interface.PushLocalFrame(environment, Int32(countArgs(repeat each arguments) + 1)) | ||
| let jniArgs = getJValues(repeat each arguments, in: environment) | ||
| return try environment.translatingJNIExceptions { | ||
| let newObj = try environment.translatingJNIExceptions { | ||
| environment.interface.NewObjectA!(environment, thisClass, methodID, jniArgs) | ||
| }! | ||
| return environment.interface.PopLocalFrame(environment, newObj)! | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -285,6 +299,7 @@ extension AnyJavaObject { | |
|
|
||
| // Retrieve the Java class instance from the object. | ||
| let thisClass = environment.interface.GetObjectClass(environment, this)! | ||
| defer { environment.interface.DeleteLocalRef(environment, thisClass) } | ||
|
|
||
| return environment.interface.GetFieldID(environment, thisClass, fieldName, FieldType.jniMangling) | ||
| } | ||
|
|
@@ -306,6 +321,9 @@ extension AnyJavaObject { | |
|
|
||
| let fieldID = getJNIFieldID(fieldName, fieldType: fieldType)! | ||
| let jniMethod = FieldType.jniFieldSet(in: environment) | ||
| // Frame of 2: 1 for the field value's local ref, 1 buffer. | ||
| environment.interface.PushLocalFrame(environment, 2) | ||
| defer { environment.interface.PopLocalFrame(environment, nil) } | ||
| jniMethod(environment, javaThis, fieldID, newValue.getJNIValue(in: environment)) | ||
| } | ||
| } | ||
|
|
@@ -337,8 +355,12 @@ extension JavaClass { | |
| ) | ||
| }! | ||
|
|
||
| // Retrieve the method that performs this call, then | ||
| // Retrieve the method that performs this call, then package the values and | ||
| // call it. | ||
| // Size the frame to 1 ref per argument plus 1 for the result ref. | ||
| let jniMethod = Result.jniStaticMethodCall(in: environment) | ||
| environment.interface.PushLocalFrame(environment, Int32(countArgs(repeat each arguments) + 2)) | ||
| defer { environment.interface.PopLocalFrame(environment, nil) } | ||
| let jniArgs = getJValues(repeat each arguments, in: environment) | ||
| let jniResult = try environment.translatingJNIExceptions { | ||
| jniMethod(environment, thisClass, methodID, jniArgs) | ||
|
|
@@ -371,8 +393,11 @@ extension JavaClass { | |
| ) | ||
| }! | ||
|
|
||
| // Retrieve the method that performs this call, then | ||
| // Retrieve the method that performs this call | ||
| // Size the frame to 1 ref per argument; no result ref needed for void calls. | ||
| let jniMethod = environment.interface.CallStaticVoidMethodA | ||
| environment.interface.PushLocalFrame(environment, Int32(countArgs(repeat each arguments) + 1)) | ||
| defer { environment.interface.PopLocalFrame(environment, nil) } | ||
| let jniArgs = getJValues(repeat each arguments, in: environment) | ||
| try environment.translatingJNIExceptions { | ||
| jniMethod!(environment, thisClass, methodID, jniArgs) | ||
|
|
||
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
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
we should be checking the result of the push, we may be OOMing the JVM here, and we'd pop the wrong frame because we didn't push one (!)
if pushed != JNI_OKand need to guard the pop the same...
We may suggest doing a withFrame pattern tbh: swiftlang/swift-java-jni-core#11