Skip to content

Commit c30a2ee

Browse files
committed
support creating int and double arrays
1 parent 95f3074 commit c30a2ee

7 files changed

Lines changed: 58 additions & 0 deletions

File tree

src/java.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,34 @@ NAN_METHOD(Java::newArray) {
619619
}
620620
}
621621

622+
else if(strcmp(className.c_str(), "double") == 0) {
623+
results = env->NewDoubleArray(arrayObj->Length());
624+
for(uint32_t i=0; i<arrayObj->Length(); i++) {
625+
v8::Local<v8::Value> item = arrayObj->Get(i);
626+
jobject val = v8ToJava(env, item);
627+
jclass doubleClazz = env->FindClass("java/lang/Double");
628+
jmethodID double_doubleValue = env->GetMethodID(doubleClazz, "doubleValue", "()D");
629+
jdouble doubleValues[1];
630+
doubleValues[0] = env->CallDoubleMethod(val, double_doubleValue);
631+
assert(!env->ExceptionCheck());
632+
env->SetDoubleArrayRegion((jdoubleArray)results, i, 1, doubleValues);
633+
}
634+
}
635+
636+
else if(strcmp(className.c_str(), "int") == 0) {
637+
results = env->NewIntArray(arrayObj->Length());
638+
for(uint32_t i=0; i<arrayObj->Length(); i++) {
639+
v8::Local<v8::Value> item = arrayObj->Get(i);
640+
jobject val = v8ToJava(env, item);
641+
jclass integerClazz = env->FindClass("java/lang/Integer");
642+
jmethodID integer_intValue = env->GetMethodID(integerClazz, "intValue", "()I");
643+
jint intValues[1];
644+
intValues[0] = env->CallIntMethod(val, integer_intValue);
645+
assert(!env->ExceptionCheck());
646+
env->SetIntArrayRegion((jintArray)results, i, 1, intValues);
647+
}
648+
}
649+
622650
else if(strcmp(className.c_str(), "boolean") == 0) {
623651
results = env->NewBooleanArray(arrayObj->Length());
624652
for(uint32_t i=0; i<arrayObj->Length(); i++) {

test/Test$StaticEnum.class

0 Bytes
Binary file not shown.

test/Test$SubClass.class

0 Bytes
Binary file not shown.

test/Test$SuperClass.class

0 Bytes
Binary file not shown.

test/Test.class

253 Bytes
Binary file not shown.

test/Test.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,18 @@ public static boolean[] staticBooleanArray(Boolean[] arg) {
6868
return b;
6969
}
7070

71+
public static double[] staticDoubleArray(double[] arg) {
72+
double[] b = new double[arg.length];
73+
for(int i=0; i<arg.length; i++) { b[i] = arg[i]; }
74+
return b;
75+
}
76+
77+
public static int[] staticIntArray(int[] arg) {
78+
int[] b = new int[arg.length];
79+
for(int i=0; i<arg.length; i++) { b[i] = arg[i]; }
80+
return b;
81+
}
82+
7183
public static class SuperClass {
7284
public int getVal() { return 3; }
7385
}

test/simple-test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,24 @@ exports['Simple'] = nodeunit.testCase({
282282
test.done();
283283
},
284284

285+
"new int array": function(test) {
286+
var intArray = java.newArray("int", [1, 2]);
287+
var r = java.callStaticMethodSync("Test", "staticIntArray", intArray);
288+
test.equal(r.length, 2);
289+
test.equal(r[0], 1);
290+
test.equal(r[1], 2);
291+
test.done();
292+
},
293+
294+
"new double array": function(test) {
295+
var doubleArray = java.newArray("double", [1.2, 4]);
296+
var r = java.callStaticMethodSync("Test", "staticDoubleArray", doubleArray);
297+
test.equal(r.length, 2);
298+
test.equal(r[0], 1.2);
299+
test.equal(r[1], 4);
300+
test.done();
301+
},
302+
285303
"new short array objects": function(test) {
286304
var shortArray = java.newArray("java.lang.Short", [1, 2].map(function(c) { return java.newShort(c); }));
287305
var r = java.callStaticMethodSync("Test", "staticShortArray", shortArray);

0 commit comments

Comments
 (0)