Skip to content

Commit

Permalink
Fixes #2 : Support passing the JNIEnv pointer to native methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
chirino committed Feb 24, 2012
1 parent 369b4d3 commit f0e3ace
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 23 deletions.
3 changes: 2 additions & 1 deletion hawtjni-example/src/main/java/test/Example.java
Original file line number Diff line number Diff line change
Expand Up @@ -324,5 +324,6 @@ static class Range {

}


public static final native void passingtheenv (String msg, JNIEnv env);

}
3 changes: 3 additions & 0 deletions hawtjni-example/src/main/native-package/src/foo.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,6 @@ char * char_add(char *arg, int count) {
return arg+count;
}

void passingtheenv (const char *who, JNIEnv *env) {
printf("%s, the JNIEnv is at: %x\n", who, env);
}
2 changes: 2 additions & 0 deletions hawtjni-example/src/main/native-package/src/foo.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define INCLUDED_FOO_H

#include <stdlib.h>
#include "jni.h"

#ifdef __cplusplus
extern "C" {
Expand All @@ -35,6 +36,7 @@ long foowork(struct foo **arg, int count);

void callmeback(void (*thecallback)(int number));

void passingtheenv (const char *who, JNIEnv *env);

#ifdef __cplusplus
} /* extern "C" */
Expand Down
3 changes: 3 additions & 0 deletions hawtjni-example/src/test/java/test/ExampleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static test.Example.*;

import org.fusesource.hawtjni.runtime.Callback;
import org.fusesource.hawtjni.runtime.JNIEnv;
import org.junit.Test;
import static org.fusesource.hawtjni.runtime.PointerMath.*;

Expand Down Expand Up @@ -70,6 +71,8 @@ public void test() {

// Heap memory is not GCed, we must manually free it.
free(ptr);

passingtheenv("Hiram", null);
}

public long instanceCallback(long value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,8 @@ boolean generateLocalVars(JNIMethod method, List<JNIParameter> params, JNIType r
} else {
throw new Error("not done");
}
} else if (paramType.isType("org.fusesource.hawtjni.runtime.JNIEnv")) {
// no need to generate a local for this one..
} else if (paramType.isType("java.lang.String")) {
if (param.getFlag(ArgFlag.UNICODE)) {
output("const jchar *lparg" + i);
Expand Down Expand Up @@ -631,26 +633,32 @@ boolean generateGetters(JNIMethod method, List<JNIParameter> params) {
boolean genFailTag = false;
int criticalCount = 0;
for (JNIParameter param : params) {
if (!isCritical(param)) {
genFailTag |= generateGetParameter(method, param, false, 1);
} else {
criticalCount++;
if( !"org.fusesource.hawtjni.runtime.JNIEnv".equals(param.getTypeClass().getName()) ) {
if (!isCritical(param)) {
genFailTag |= generateGetParameter(method, param, false, 1);
} else {
criticalCount++;
}
}
}
if (criticalCount != 0) {
outputln("#ifdef JNI_VERSION_1_2");
outputln("\tif (IS_JNI_1_2) {");
for (JNIParameter param : params) {
if (isCritical(param)) {
genFailTag |= generateGetParameter(method, param, true, 2);
if( !"org.fusesource.hawtjni.runtime.JNIEnv".equals(param.getTypeClass().getName()) ) {
if (isCritical(param)) {
genFailTag |= generateGetParameter(method, param, true, 2);
}
}
}
outputln("\t} else");
outputln("#endif");
outputln("\t{");
for (JNIParameter param : params) {
if (isCritical(param)) {
genFailTag |= generateGetParameter(method, param, false, 2);
if( !"org.fusesource.hawtjni.runtime.JNIEnv".equals(param.getTypeClass().getName()) ) {
if (isCritical(param)) {
genFailTag |= generateGetParameter(method, param, false, 2);
}
}
}
outputln("\t}");
Expand All @@ -662,36 +670,44 @@ void generateSetters(JNIMethod method, List<JNIParameter> params) {
int criticalCount = 0;
for (int i = params.size() - 1; i >= 0; i--) {
JNIParameter param = params.get(i);
if (isCritical(param)) {
criticalCount++;
if( !"org.fusesource.hawtjni.runtime.JNIEnv".equals(param.getTypeClass().getName()) ) {
if (isCritical(param)) {
criticalCount++;
}
}
}
if (criticalCount != 0) {
outputln("#ifdef JNI_VERSION_1_2");
outputln("\tif (IS_JNI_1_2) {");
for (int i = params.size() - 1; i >= 0; i--) {
JNIParameter param = params.get(i);
if (isCritical(param)) {
output("\t");
generateSetParameter(param, true);
if( !"org.fusesource.hawtjni.runtime.JNIEnv".equals(param.getTypeClass().getName()) ) {
if (isCritical(param)) {
output("\t");
generateSetParameter(param, true);
}
}
}
outputln("\t} else");
outputln("#endif");
outputln("\t{");
for (int i = params.size() - 1; i >= 0; i--) {
JNIParameter param = params.get(i);
if (isCritical(param)) {
output("\t");
generateSetParameter(param, false);
if( !"org.fusesource.hawtjni.runtime.JNIEnv".equals(param.getTypeClass().getName()) ) {
if (isCritical(param)) {
output("\t");
generateSetParameter(param, false);
}
}
}
outputln("\t}");
}
for (int i = params.size() - 1; i >= 0; i--) {
JNIParameter param = params.get(i);
if (!isCritical(param)) {
generateSetParameter(param, false);
if( !"org.fusesource.hawtjni.runtime.JNIEnv".equals(param.getTypeClass().getName()) ) {
if (!isCritical(param)) {
generateSetParameter(param, false);
}
}
}
}
Expand Down Expand Up @@ -803,10 +819,14 @@ void generateFunctionCallRightSide(JNIMethod method, List<JNIParameter> params,
if (i == params.size() - 1 && param.getFlag(ArgFlag.SENTINEL)) {
output("NULL");
} else {
JNIType paramType = param.getType32();
if (!paramType.isPrimitive() && !isSystemClass(paramType))
output("lp");
output("arg" + i);
if( "org.fusesource.hawtjni.runtime.JNIEnv".equals(param.getTypeClass().getName()) ) {
output("env");
} else {
JNIType paramType = param.getType32();
if (!paramType.isPrimitive() && !isSystemClass(paramType))
output("lp");
output("arg" + i);
}
}
if (param.getFlag(ArgFlag.CS_OBJECT))
output(")");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Copyright (C) 2010, FuseSource Corp. All rights reserved.
*/
package org.fusesource.hawtjni.runtime;

/**
* <p>
* This is a marker class. Methods that take this as an argument
* will receive that actual native 'JNIEnv *' value. Since this
* class cannot be instantiated, Java callers must pass null
* for the value.
* </p>
*
* @author <a href="http://hiramchirino.com">Hiram Chirino</a>
*/
public class JNIEnv {
private JNIEnv() {}
}

0 comments on commit f0e3ace

Please sign in to comment.