Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions compile-java8-code.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash -e

# This script must be run on a Mac due to its reliance on /usr/libexec/java_home
# to find a JDK 1.8 installation. Note that this script will work correctly on
# a mac with JDK 1.8 installed, even if JAVA_HOME currently points to a 1.7
# or earlier JDK.
# This script is run manually by maintainers of this project, who add the
# the generated .class files to source control.

JAVA_VERSION=1.8
JDK8_HOME=$(/usr/libexec/java_home -v ${JAVA_VERSION})

cd test8
${JDK8_HOME}/bin/javac -source ${JAVA_VERSION} *.java
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"nodeunit": "0.9.0"
},
"scripts": {
"test": "nodeunit test",
"test": "nodeunit test test8",
"postinstall": "node postInstall.js"
},
"main": "./index.js"
Expand Down
12 changes: 9 additions & 3 deletions src/java.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ v8::Local<v8::Value> Java::createJVM(JavaVM** jvm, JNIEnv** env) {
}

JNI_GetDefaultJavaVMInitArgs(&args);
args.version = JNI_VERSION_1_6;
args.version = JNI_BEST_VERSION;
args.ignoreUnrecognized = false;
args.options = vmOptions;
args.nOptions = vmOptionsCount;
Expand Down Expand Up @@ -1050,6 +1050,12 @@ NAN_METHOD(Java::instanceOf) {
void EIO_CallJs(uv_work_t* req) {
}

static std::string int_to_string(int i) {
char buf[32];
snprintf(buf, sizeof(buf), "%d", i);
return std::string(buf);
}

#if NODE_MINOR_VERSION >= 10
void EIO_AfterCallJs(uv_work_t* req, int status) {
#else
Expand All @@ -1062,10 +1068,10 @@ void EIO_AfterCallJs(uv_work_t* req) {
dynamicProxyData->result = NULL;

JNIEnv* env;
int ret = dynamicProxyData->java->getJvm()->GetEnv((void**)&env, JNI_VERSION_1_6);
int ret = dynamicProxyData->java->getJvm()->GetEnv((void**)&env, JNI_BEST_VERSION);
if (ret != JNI_OK) {
dynamicProxyData->throwableClass = "java/lang/IllegalStateException";
dynamicProxyData->throwableMessage = "Could not retrieve JNIEnv: jvm->GetEnv returned " + ret;
dynamicProxyData->throwableMessage = "Could not retrieve JNIEnv: jvm->GetEnv returned " + int_to_string(ret);
dynamicProxyData->done = DYNAMIC_PROXY_JS_ERROR;
return;
}
Expand Down
6 changes: 6 additions & 0 deletions src/java.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
#include <string>
#include <nan.h>

#ifdef JNI_VERSION_1_8
#define JNI_BEST_VERSION JNI_VERSION_1_8
#else
#define JNI_BEST_VERSION JNI_VERSION_1_6
#endif

class Java : public node::ObjectWrap {
public:
static void Init(v8::Handle<v8::Object> target);
Expand Down
4 changes: 2 additions & 2 deletions src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,11 @@ std::string javaMethodCallToString(JNIEnv *env, jobject obj, jmethodID methodId,

JNIEnv* javaGetEnv(JavaVM* jvm, jobject classLoader) {
JNIEnv *env = NULL;
int ret = jvm->GetEnv((void**)&env, JNI_VERSION_1_6);
int ret = jvm->GetEnv((void**)&env, JNI_BEST_VERSION);

if (ret == JNI_EDETACHED) {
JavaVMAttachArgs attachArgs;
attachArgs.version = JNI_VERSION_1_6;
attachArgs.version = JNI_BEST_VERSION;
attachArgs.name = NULL;
attachArgs.group = NULL;
jvm->AttachCurrentThread((void**)&env, &attachArgs);
Expand Down
Binary file added test8/TestLambda$IntegerMath.class
Binary file not shown.
Binary file added test8/TestLambda.class
Binary file not shown.
18 changes: 18 additions & 0 deletions test8/TestLambda.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class TestLambda
{
public TestLambda() {}

interface IntegerMath {
int op(int a, int b);
}

public int testLambdaAddition(Integer x, Integer y) {
IntegerMath addition = (a, b) -> a + b;
return addition.op(x, y);
}

public int testLambdaSubtraction(Integer x, Integer y) {
IntegerMath subtraction = (a, b) -> a - b;
return subtraction.op(x, y);
}
}
27 changes: 27 additions & 0 deletions test8/testLambda.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
var java = require("../testHelpers").java;

var nodeunit = require("nodeunit");
var util = require("util");

exports['Java8'] = nodeunit.testCase({
"call methods of a class that uses lambda expressions": function(test) {
try {
var TestLambda = java.import('TestLambda');
var lambda = new TestLambda();
var sum = lambda.testLambdaAdditionSync(23, 42);
test.equal(sum, 65);
var diff = lambda.testLambdaSubtractionSync(23, 42);
test.equal(diff, -19);
}
catch (err) {
var unsupportedVersion = err.toString().match(/Unsupported major.minor version 52.0/)
test.ok(unsupportedVersion);
if (unsupportedVersion)
console.log('JRE 1.8 not available');
else
console.error('Java8 test failed with unknown error:', err);
}
test.done();
}
});

1 change: 1 addition & 0 deletions testHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ java.options.push("-Djava.awt.headless=true");

java.classpath.push("test/");
java.classpath.push("test/commons-lang3-3.1.jar");
java.classpath.push("test8/");

module.exports.java = java;