Skip to content

Commit

Permalink
Add more overloads
Browse files Browse the repository at this point in the history
  • Loading branch information
pomadchin committed Feb 23, 2023
1 parent 00605c1 commit 7038a1a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ object Library {
@native def say(message: String): Int
@native def say(message: String, i: Int): Int
@native def say(message: String, i: Int, l: Long): Int
@native def say(messages: Array[String]): Int

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ object Main {
val result1 = Library.say("hello world")
val result2 = Library.say("hello world", 1)
val result3 = Library.say("hello world", 1, 2L)
val result4 = Library.say(Array("hello world1", "hello world2"))

assert(result1 == 42)
assert(result2 == 43)
assert(result3 == 44)
assert(result4 == 45)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class SimpleSpec extends AnyFlatSpec {
assert(Library.say("hello") == 42)
assert(Library.say("hello", 1) == 43)
assert(Library.say("hello", 1, 2L) == 44)
assert(Library.say(Array("hello world1", "hello world2")) == 45)
}

}
25 changes: 25 additions & 0 deletions plugin/src/sbt-test/sbt-jni/overloads/native/src/library.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ JNIEXPORT jint JNICALL Java_simple_Library_00024_say__Ljava_lang_String_2
return 42;
}

/*
* Class: simple_Library_00024
* Method: say
* Signature: (Ljava/lang/String;I)I
*/
JNIEXPORT jint JNICALL Java_simple_Library_00024_say__Ljava_lang_String_2I
(JNIEnv *env, jobject clazz, jstring message, jint i) {
const char* msg = (*env)->GetStringUTFChars(env, message, 0);
Expand All @@ -24,6 +29,11 @@ JNIEXPORT jint JNICALL Java_simple_Library_00024_say__Ljava_lang_String_2I
return 43;
}

/*
* Class: simple_Library_00024
* Method: say
* Signature: (Ljava/lang/String;IJ)I
*/
JNIEXPORT jint JNICALL Java_simple_Library_00024_say__Ljava_lang_String_2IJ
(JNIEnv *env, jobject clazz, jstring message, jint i, jlong l) {
const char* msg = (*env)->GetStringUTFChars(env, message, 0);
Expand All @@ -32,3 +42,18 @@ JNIEXPORT jint JNICALL Java_simple_Library_00024_say__Ljava_lang_String_2IJ
(*env)->ReleaseStringUTFChars(env, message, msg);
return 44;
}

/*
* Class: simple_Library_00024
* Method: say
* Signature: ([Ljava/lang/String;)I
*/
JNIEXPORT jint JNICALL Java_simple_Library_00024_say___3Ljava_lang_String_2
(JNIEnv *env, jobject clazz, jobjectArray messages) {
for (jint i = 0; i < (*env)->GetArrayLength(env, messages); i++) {
jstring message = (jstring) ((*env)->GetObjectArrayElement(env, messages, i));
const char *msg = (*env)->GetStringUTFChars(env, message, 0);
fprintf(stdout, "Printing from native library: %s\n", msg);
}
return 45;
}

0 comments on commit 7038a1a

Please sign in to comment.