Skip to content

Commit a599b85

Browse files
committed
GlueGen JavaCallback: Revised: Static Java callback dispatcher, dropping native heap, support Struct UserParam ...
Implementation now generates a static Java callback dispatcher for each defined SetCallbackFunction, which gets invoked by the generated native static counterpart with all arguments required. The static callback utilizes its own synchronization for thread-safety and fetches the required data set stored at SetCallbackFunction to dispatch the call to the users' CallbackFunction. In case the callback has been removed already, the static callback simply bails out quietly. The native code does not create, release or manage heap memory and therefore is considered safe. +++ Further Struct Type UserParam are now supported including Heterogeneous UserParam mapping (read GlueGen_Mapping.*). +++ Cleaned up code by extracting all JavaCallback emitter code into JavaCallbackEmitter class in one place, leaving JavaMethodbindingEmitter and CMethodbindingEmitter mostly in their original stage (non-convoluted). In this regard, I had to refactor a few function, i.e. moving CMethodbindingEmitter.getJNIMangledArg(..) into JavaType.appendDescriptor(..) and JavaType.appendJNIDescriptor(..) while reusing the toJNIMethodDescriptor(..) conversion. Test4JavaCallback covers and passes all cases.
1 parent 6d53b4b commit a599b85

18 files changed

+1999
-862
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ GlueGen can produce native foreign function bindings to Java™ as well as
3131
[map native data structures](doc/GlueGen_Mapping.md#struct-mapping) to be fully accessible from Java™ including
3232
potential calls to [embedded function pointer](doc/GlueGen_Mapping.md#struct-function-pointer-support).
3333

34-
GlueGen supports [registering Java™ callback methods](doc/GlueGen_Mapping.md#java-callback-from-native-c-api-support)
34+
GlueGen supports [registering Java™ callback methods](doc/GlueGen_Mapping.md#java-callback)
3535
to receive asynchronous and off-thread native toolkit events,
3636
where a generated native callback function dispatches the events to Java™.
3737

doc/GlueGen_Mapping.html

Lines changed: 260 additions & 42 deletions
Large diffs are not rendered by default.

doc/GlueGen_Mapping.md

Lines changed: 204 additions & 23 deletions
Large diffs are not rendered by default.

src/java/com/jogamp/gluegen/CCodeUnit.java

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,6 @@ public void emitJNIOnLoadJNIEnvCode(final String libraryBasename) {
8484
emitln( getJNIOnLoadJNIEnvCode(libraryBasename) );
8585
}
8686

87-
/** Emits {@link #JavaCallbackGlueDataDecl}. */
88-
public void emitJavaCallbackGlueDataDecl() {
89-
emitln( JavaCallbackGlueDataDecl );
90-
}
91-
9287
@Override
9388
public String toString() { return "CCodeUnit[unit "+cUnitName+", file "+filename+"]"; }
9489

@@ -113,16 +108,6 @@ public void emitJavaCallbackGlueDataDecl() {
113108
" return jbyteBuffer;\n"+
114109
"}\n";
115110

116-
/** JavaCallback Glue Data typedef struct */
117-
public static final String JavaCallbackGlueDataDecl =
118-
"typedef struct {\n"+
119-
" jobject lockObj;\n"+
120-
" jobject cbFunc;\n"+
121-
" jmethodID cbMethodID;\n"+
122-
" jobject userParam;\n"+
123-
"} T_JavaCallbackGlueData;\n"+
124-
"\n";
125-
126111
/**
127112
* Returns native JNI declarations for `JavaVM* {libraryBasename}_jvmHandle`
128113
* and `JVMUtil_GetJNIEnv(..)`.

src/java/com/jogamp/gluegen/CMethodBindingEmitter.java

Lines changed: 59 additions & 296 deletions
Large diffs are not rendered by default.

src/java/com/jogamp/gluegen/FunctionEmitter.java

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ public void addModifiers(final Iterator<EmissionModifier> mi) {
114114
* Emit the function to the {@link #getUnit()}
115115
*/
116116
public final void emit() {
117+
emitAdditionalCode();
117118
emitDocComment();
118119
//output.println(" // Emitter: " + getClass().getName());
119120
emitSignature();
@@ -139,6 +140,7 @@ public void setCommentEmitter(final CommentEmitter cEmitter) {
139140
*/
140141
public CommentEmitter getCommentEmitter() { return commentEmitter; }
141142

143+
protected void emitAdditionalCode() { }
142144
protected void emitDocComment() {
143145

144146
if (commentEmitter != null) {
@@ -154,32 +156,42 @@ protected void emitDocComment() {
154156
}
155157
}
156158

157-
protected void emitSignature() {
159+
protected final void emitSignature() {
160+
unit.emit(appendSignature(new StringBuilder()).toString());
161+
}
158162

159-
unit.emit(getBaseIndentString()); // indent method
163+
protected StringBuilder appendSignature(final StringBuilder buf) {
164+
buf.append(getBaseIndentString()); // indent method
160165

161-
final int numEmitted = emitModifiers();
166+
final int numEmitted = appendModifiers(buf);
162167
if (numEmitted > 0) {
163-
unit.emit(" ");
168+
buf.append(" ");
164169
}
165170

166-
emitReturnType();
167-
unit.emit(" ");
171+
appendReturnType(buf);
172+
buf.append(" ");
168173

169-
emitName();
170-
unit.emit("(");
174+
appendName(buf);
175+
buf.append("(");
171176

172-
emitArguments();
173-
unit.emit(")");
177+
appendArguments(buf);
178+
buf.append(")");
179+
return buf;
174180
}
175181

176-
protected int emitModifiers() {
182+
protected final int emitModifiers() {
183+
final StringBuilder buf = new StringBuilder();
184+
final int n = appendModifiers(buf);
185+
unit.emit(buf.toString());
186+
return n;
187+
}
188+
protected int appendModifiers(final StringBuilder buf) {
177189
int numEmitted = 0;
178190
for (final Iterator<EmissionModifier> it = getModifiers(); it.hasNext(); ) {
179-
unit.emit(it.next().toString());
191+
buf.append(it.next().toString());
180192
++numEmitted;
181193
if (it.hasNext()) {
182-
unit.emit(" ");
194+
buf.append(" ");
183195
}
184196
}
185197
return numEmitted;
@@ -190,10 +202,23 @@ protected int emitModifiers() {
190202
protected String getCommentStartString() { return "/* "; }
191203
protected String getCommentEndString() { return " */"; }
192204

193-
protected abstract void emitReturnType();
194-
protected abstract void emitName();
205+
protected final void emitReturnType() {
206+
unit.emit(appendReturnType(new StringBuilder()).toString());
207+
}
208+
protected abstract StringBuilder appendReturnType(StringBuilder buf);
209+
protected final void emitName() {
210+
unit.emit(appendName(new StringBuilder()).toString());
211+
}
212+
protected abstract StringBuilder appendName(StringBuilder buf);
213+
/** Returns the number of arguments emitted. */
214+
protected final int emitArguments() {
215+
final StringBuilder buf = new StringBuilder();
216+
final int n = appendArguments(buf);
217+
unit.emit(buf.toString());
218+
return n;
219+
}
195220
/** Returns the number of arguments emitted. */
196-
protected abstract int emitArguments();
221+
protected abstract int appendArguments(StringBuilder buf);
197222
protected abstract void emitBody();
198223

199224
public static class EmissionModifier {

0 commit comments

Comments
 (0)