Merge branch 'master' of whereswaldon/jni into master

This commit is contained in:
gmp 2020-06-26 21:27:01 +00:00 committed by Gogs
commit b33150047f
6 changed files with 268 additions and 3 deletions

View File

@ -29,6 +29,13 @@ __attribute__ ((visibility ("hidden"))) void _jni_CallStaticVoidMethodA(JNIEnv *
__attribute__ ((visibility ("hidden"))) jobject _jni_CallObjectMethodA(JNIEnv *env, jobject obj, jmethodID method, jvalue *args); __attribute__ ((visibility ("hidden"))) jobject _jni_CallObjectMethodA(JNIEnv *env, jobject obj, jmethodID method, jvalue *args);
__attribute__ ((visibility ("hidden"))) jint _jni_CallIntMethodA(JNIEnv *env, jobject obj, jmethodID method, jvalue *args); __attribute__ ((visibility ("hidden"))) jint _jni_CallIntMethodA(JNIEnv *env, jobject obj, jmethodID method, jvalue *args);
__attribute__ ((visibility ("hidden"))) void _jni_CallVoidMethodA(JNIEnv *env, jobject obj, jmethodID method, jvalue *args); __attribute__ ((visibility ("hidden"))) void _jni_CallVoidMethodA(JNIEnv *env, jobject obj, jmethodID method, jvalue *args);
__attribute__ ((visibility ("hidden"))) jboolean _jni_CallBooleanMethodA(JNIEnv *env, jobject obj, jmethodID method, jvalue *args);
__attribute__ ((visibility ("hidden"))) jbyte _jni_CallByteMethodA(JNIEnv *env, jobject obj, jmethodID method, jvalue *args);
__attribute__ ((visibility ("hidden"))) jchar _jni_CallCharMethodA(JNIEnv *env, jobject obj, jmethodID method, jvalue *args);
__attribute__ ((visibility ("hidden"))) jshort _jni_CallShortMethodA(JNIEnv *env, jobject obj, jmethodID method, jvalue *args);
__attribute__ ((visibility ("hidden"))) jlong _jni_CallLongMethodA(JNIEnv *env, jobject obj, jmethodID method, jvalue *args);
__attribute__ ((visibility ("hidden"))) jfloat _jni_CallFloatMethodA(JNIEnv *env, jobject obj, jmethodID method, jvalue *args);
__attribute__ ((visibility ("hidden"))) jdouble _jni_CallDoubleMethodA(JNIEnv *env, jobject obj, jmethodID method, jvalue *args);
__attribute__ ((visibility ("hidden"))) jbyteArray _jni_NewByteArray(JNIEnv *env, jsize length); __attribute__ ((visibility ("hidden"))) jbyteArray _jni_NewByteArray(JNIEnv *env, jsize length);
__attribute__ ((visibility ("hidden"))) jbyte *_jni_GetByteArrayElements(JNIEnv *env, jbyteArray arr); __attribute__ ((visibility ("hidden"))) jbyte *_jni_GetByteArrayElements(JNIEnv *env, jbyteArray arr);
__attribute__ ((visibility ("hidden"))) void _jni_ReleaseByteArrayElements(JNIEnv *env, jbyteArray arr, jbyte *elems, jint mode); __attribute__ ((visibility ("hidden"))) void _jni_ReleaseByteArrayElements(JNIEnv *env, jbyteArray arr, jbyte *elems, jint mode);

28
jni.c
View File

@ -134,6 +134,34 @@ void _jni_CallVoidMethodA(JNIEnv *env, jobject obj, jmethodID method, jvalue *ar
(*env)->CallVoidMethodA(env, obj, method, args); (*env)->CallVoidMethodA(env, obj, method, args);
} }
jboolean _jni_CallBooleanMethodA(JNIEnv *env, jobject obj, jmethodID method, jvalue *args) {
return (*env)->CallBooleanMethodA(env, obj, method, args);
}
jbyte _jni_CallByteMethodA(JNIEnv *env, jobject obj, jmethodID method, jvalue *args) {
return (*env)->CallByteMethodA(env, obj, method, args);
}
jchar _jni_CallCharMethodA(JNIEnv *env, jobject obj, jmethodID method, jvalue *args) {
return (*env)->CallCharMethodA(env, obj, method, args);
}
jshort _jni_CallShortMethodA(JNIEnv *env, jobject obj, jmethodID method, jvalue *args) {
return (*env)->CallShortMethodA(env, obj, method, args);
}
jlong _jni_CallLongMethodA(JNIEnv *env, jobject obj, jmethodID method, jvalue *args) {
return (*env)->CallLongMethodA(env, obj, method, args);
}
jfloat _jni_CallFloatMethodA(JNIEnv *env, jobject obj, jmethodID method, jvalue *args) {
return (*env)->CallFloatMethodA(env, obj, method, args);
}
jdouble _jni_CallDoubleMethodA(JNIEnv *env, jobject obj, jmethodID method, jvalue *args) {
return (*env)->CallDoubleMethodA(env, obj, method, args);
}
jbyteArray _jni_NewByteArray(JNIEnv *env, jsize length) { jbyteArray _jni_NewByteArray(JNIEnv *env, jsize length) {
return (*env)->NewByteArray(env, length); return (*env)->NewByteArray(env, length);
} }

43
jni.go
View File

@ -2,7 +2,6 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
// Package jni implements various helper functions for communicating with the // Package jni implements various helper functions for communicating with the
// Android JVM though JNI. // Android JVM though JNI.
package jni package jni
@ -155,6 +154,48 @@ func CallIntMethod(e Env, obj Object, method MethodID, args ...Value) (int32, er
return int32(res), exception(e) return int32(res), exception(e)
} }
// CallBooleanMethod calls a method on an object, returning a bool.
func CallBooleanMethod(e Env, obj Object, method MethodID, args ...Value) (bool, error) {
res := C._jni_CallBooleanMethodA(e.env, C.jobject(obj), C.jmethodID(method), varArgs(args))
return res == TRUE, exception(e)
}
// CallByteMethod calls a method on an object, returning a byte.
func CallByteMethod(e Env, obj Object, method MethodID, args ...Value) (byte, error) {
res := C._jni_CallByteMethodA(e.env, C.jobject(obj), C.jmethodID(method), varArgs(args))
return byte(res), exception(e)
}
// CallCharMethod calls a method on an object, returning a rune.
func CallCharMethod(e Env, obj Object, method MethodID, args ...Value) (rune, error) {
res := C._jni_CallCharMethodA(e.env, C.jobject(obj), C.jmethodID(method), varArgs(args))
return rune(res), exception(e)
}
// CallShortMethod calls a method on an object, returning an int32.
func CallShortMethod(e Env, obj Object, method MethodID, args ...Value) (int16, error) {
res := C._jni_CallShortMethodA(e.env, C.jobject(obj), C.jmethodID(method), varArgs(args))
return int16(res), exception(e)
}
// CallLongMethod calls a method on an object, returning an int64.
func CallLongMethod(e Env, obj Object, method MethodID, args ...Value) (int64, error) {
res := C._jni_CallLongMethodA(e.env, C.jobject(obj), C.jmethodID(method), varArgs(args))
return int64(res), exception(e)
}
// CallFloatMethod calls a method on an object, returning a float32.
func CallFloatMethod(e Env, obj Object, method MethodID, args ...Value) (float32, error) {
res := C._jni_CallFloatMethodA(e.env, C.jobject(obj), C.jmethodID(method), varArgs(args))
return float32(res), exception(e)
}
// CallDoubleMethod calls a method on an object, returning a float64.
func CallDoubleMethod(e Env, obj Object, method MethodID, args ...Value) (float64, error) {
res := C._jni_CallDoubleMethodA(e.env, C.jobject(obj), C.jmethodID(method), varArgs(args))
return float64(res), exception(e)
}
// GetByteArrayElements returns the contents of the array. // GetByteArrayElements returns the contents of the array.
func GetByteArrayElements(e Env, jarr ByteArray) []byte { func GetByteArrayElements(e Env, jarr ByteArray) []byte {
size := C._jni_GetArrayLength(e.env, C.jarray(jarr)) size := C._jni_GetArrayLength(e.env, C.jarray(jarr))

View File

@ -145,6 +145,167 @@ func TestIntMethod(t *testing.T) {
} }
} }
func TestBooleanMethod(t *testing.T) {
err := Do(vm, func(env Env) error {
cls := FindClass(env, "test/AClass")
mid := GetMethodID(env, cls, "<init>", "()V")
inst, err := NewObject(env, cls, mid)
mid = GetMethodID(env, cls, "GetBoolean", "()Z")
if mid == nil {
t.Errorf("MethodID is nil")
}
res, err := CallBooleanMethod(env, inst, mid)
if err != nil {
t.Errorf("Method invocation failed")
}
if !res {
t.Errorf("Method returned %v, not expected value of %v.", res, true)
}
return nil
})
if err != nil {
t.Errorf("Error: %s", err)
}
}
func TestByteMethod(t *testing.T) {
err := Do(vm, func(env Env) error {
cls := FindClass(env, "test/AClass")
mid := GetMethodID(env, cls, "<init>", "()V")
inst, err := NewObject(env, cls, mid)
mid = GetMethodID(env, cls, "GetByte", "()B")
if mid == nil {
t.Errorf("MethodID is nil")
}
res, err := CallByteMethod(env, inst, mid)
if err != nil {
t.Errorf("Method invocation failed")
}
if res != 127 {
t.Errorf("Method returned %d, not expected value of %d.", res, 127)
}
return nil
})
if err != nil {
t.Errorf("Error: %s", err)
}
}
func TestCharMethod(t *testing.T) {
err := Do(vm, func(env Env) error {
cls := FindClass(env, "test/AClass")
mid := GetMethodID(env, cls, "<init>", "()V")
inst, err := NewObject(env, cls, mid)
mid = GetMethodID(env, cls, "GetChar", "()C")
if mid == nil {
t.Errorf("MethodID is nil")
}
res, err := CallCharMethod(env, inst, mid)
if err != nil {
t.Errorf("Method invocation failed")
}
if res != 65432 {
t.Errorf("Method returned %d, not expected value of %d.", res, 65432)
}
return nil
})
if err != nil {
t.Errorf("Error: %s", err)
}
}
func TestShortMethod(t *testing.T) {
err := Do(vm, func(env Env) error {
cls := FindClass(env, "test/AClass")
mid := GetMethodID(env, cls, "<init>", "()V")
inst, err := NewObject(env, cls, mid)
mid = GetMethodID(env, cls, "GetShort", "()S")
if mid == nil {
t.Errorf("MethodID is nil")
}
res, err := CallShortMethod(env, inst, mid)
if err != nil {
t.Errorf("Method invocation failed")
}
if res != 512 {
t.Errorf("Method returned %d, not expected value of %d.", res, 512)
}
return nil
})
if err != nil {
t.Errorf("Error: %s", err)
}
}
func TestLongMethod(t *testing.T) {
err := Do(vm, func(env Env) error {
cls := FindClass(env, "test/AClass")
mid := GetMethodID(env, cls, "<init>", "()V")
inst, err := NewObject(env, cls, mid)
mid = GetMethodID(env, cls, "GetLong", "()J")
if mid == nil {
t.Errorf("MethodID is nil")
}
res, err := CallLongMethod(env, inst, mid)
if err != nil {
t.Errorf("Method invocation failed")
}
if res != 1<<33 {
t.Errorf("Method returned %d, not expected value of %d.", res, 1<<33)
}
return nil
})
if err != nil {
t.Errorf("Error: %s", err)
}
}
func TestFloatMethod(t *testing.T) {
err := Do(vm, func(env Env) error {
cls := FindClass(env, "test/AClass")
mid := GetMethodID(env, cls, "<init>", "()V")
inst, err := NewObject(env, cls, mid)
mid = GetMethodID(env, cls, "GetFloat", "()F")
if mid == nil {
t.Errorf("MethodID is nil")
}
res, err := CallFloatMethod(env, inst, mid)
if err != nil {
t.Errorf("Method invocation failed")
}
if res != 4.321 {
t.Errorf("Method returned %f, not expected value of %f.", res, 4.321)
}
return nil
})
if err != nil {
t.Errorf("Error: %s", err)
}
}
func TestDoubleMethod(t *testing.T) {
err := Do(vm, func(env Env) error {
cls := FindClass(env, "test/AClass")
mid := GetMethodID(env, cls, "<init>", "()V")
inst, err := NewObject(env, cls, mid)
mid = GetMethodID(env, cls, "GetDouble", "()D")
if mid == nil {
t.Errorf("MethodID is nil")
}
res, err := CallDoubleMethod(env, inst, mid)
if err != nil {
t.Errorf("Method invocation failed")
}
if res != 5.4321 {
t.Errorf("Method returned %f, not expected value of %f.", res, 5.4321)
}
return nil
})
if err != nil {
t.Errorf("Error: %s", err)
}
}
func TestVoidMethod(t *testing.T) { func TestVoidMethod(t *testing.T) {
err := Do(vm, func(env Env) error { err := Do(vm, func(env Env) error {
cls := FindClass(env, "test/AClass") cls := FindClass(env, "test/AClass")

Binary file not shown.

View File

@ -21,6 +21,34 @@ public class AClass {
return intval; return intval;
} }
public boolean GetBoolean() {
return true;
}
public byte GetByte() {
return 127;
}
public char GetChar() {
return 65432;
}
public short GetShort() {
return 512;
}
public long GetLong() {
return 1L<<33;
}
public float GetFloat() {
return 4.321f;
}
public double GetDouble() {
return 5.4321;
}
public void SetInt(int val) { public void SetInt(int val) {
intval = val; intval = val;
} }