diff --git a/android/android.c.v b/android/android.c.v index 0f702e2..e7850a2 100644 --- a/android/android.c.v +++ b/android/android.c.v @@ -11,7 +11,7 @@ pub fn env() &jni.Env { } */ -pub fn activity() ?&os.NativeActivity { +pub fn activity() !&os.NativeActivity { actvty := &os.NativeActivity(C.sapp_android_get_native_activity()) if isnil(actvty) { return error(@MOD + '.' + @FN + ': could not get reference to Android native activity') diff --git a/android/keyboard/keyboard.v b/android/keyboard/keyboard.v index 67daede..475d0f8 100644 --- a/android/keyboard/keyboard.v +++ b/android/keyboard/keyboard.v @@ -191,9 +191,9 @@ pub fn is_visible() bool { // Release references to objects jni.delete_local_ref(env, view_visible_rect) - jni.delete_local_ref(env, &jni.JavaObject(voidptr(&rect_class))) + jni.delete_local_ref(env, jni.JavaObject(rect_class)) jni.delete_local_ref(env, display_dimension) - jni.delete_local_ref(env, &jni.JavaObject(voidptr(&view_class))) + jni.delete_local_ref(env, jni.JavaObject(view_class)) // hack := display_height - status_bar_height != view_visible_height // ??? Original code had this // but it doesn't work on devices with where the primary keys are software keys places in a bar in the bottom. diff --git a/examples/android/keyboard/keyboard.v b/examples/android/keyboard/keyboard.v index ad734f2..aabbdb3 100644 --- a/examples/android/keyboard/keyboard.v +++ b/examples/android/keyboard/keyboard.v @@ -39,7 +39,7 @@ fn on_soft_keyboard_input(env &jni.Env, thiz jni.JavaObject, app_ptr i64, jstr j buffer := jni.j2v_string(env, jstr) println(@MOD + '.' + @FN + ': "${buffer}" (${start},${before},${count})') - mut char_code := byte(0) + mut char_code := u8(0) mut char_literal := '' mut pos := start + before @@ -49,7 +49,7 @@ fn on_soft_keyboard_input(env &jni.Env, thiz jni.JavaObject, app_ptr i64, jstr j // Backspace println(@MOD + '.' + @FN + ': ${start} > ${buffer.len}') } else { - char_code = byte(buffer[pos]) + char_code = u8(buffer[pos]) char_literal = char_code.ascii_str() } @@ -173,7 +173,7 @@ fn event(ev &gg.Event, mut app App) { fn main() { mut app := &App{ - gg: 0 + gg: unsafe { nil } } app.gg = gg.new_context( diff --git a/examples/android/toast/toast.v b/examples/android/toast/toast.v index 5e29e4a..c8a9864 100644 --- a/examples/android/toast/toast.v +++ b/examples/android/toast/toast.v @@ -76,30 +76,8 @@ fn draw_triangle() { fn init(mut app App) { desc := sapp.create_desc() gfx.setup(&desc) - sgl_desc := sgl.Desc{ - max_vertices: 50 * 65536 - } + sgl_desc := sgl.Desc{} sgl.setup(&sgl_desc) - - // 3d pipeline - mut pipdesc := gfx.PipelineDesc{} - unsafe { vmemset(&pipdesc, 0, int(sizeof(pipdesc))) } - - color_state := gfx.ColorState{ - blend: gfx.BlendState{ - enabled: true - src_factor_rgb: .src_alpha - dst_factor_rgb: .one_minus_src_alpha - } - } - pipdesc.colors[0] = color_state - - pipdesc.depth = gfx.DepthState{ - write_enabled: true - compare: .less_equal - } - pipdesc.cull_mode = .back - app.pip_3d = sgl.make_pipeline(&pipdesc) } fn cleanup(mut app App) { @@ -130,7 +108,7 @@ fn event(ev &gg.Event, mut app App) { fn main() { // App init mut app := &App{ - gg: 0 + gg: unsafe { nil } } app.gg = gg.new_context( diff --git a/jni.c.v b/jni.c.v index bf18e0a..874140c 100644 --- a/jni.c.v +++ b/jni.c.v @@ -12,98 +12,110 @@ pub enum Version { v10 = 0x000a0000 // C.JNI_VERSION_10 } -type JavaVM = C.JavaVM -type Env = C.JNIEnv +pub type JavaVM = C.JavaVM +pub type Env = C.JNIEnv // type JavaByte = C.jbyte -type JavaObject = C.jobject -type JavaString = C.jstring -type JavaClass = C.jclass +pub type JavaObject = voidptr // C.jobject +pub type JavaString = voidptr // C.jstring +pub type JavaClass = voidptr // C.jclass // type JavaSize = C.jsize -type JavaMethodID = C.jmethodID -type JavaFieldID = C.jfieldID -type JavaThrowable = C.jthrowable +pub type JavaMethodID = C.jmethodID +pub type JavaFieldID = C.jfieldID +pub type JavaThrowable = voidptr // C.jthrowable = C.jobject = void* // -type JavaArray = C.jarray -type JavaByteArray = C.jbyteArray -type JavaCharArray = C.jcharArray -type JavaShortArray = C.jshortArray -type JavaIntArray = C.jintArray -type JavaLongArray = C.jlongArray -type JavaFloatArray = C.jfloatArray -type JavaDoubleArray = C.jdoubleArray -type JavaObjectArray = C.jobjectArray +pub type JavaArray = voidptr // C.jarray +pub type JavaByteArray = voidptr // C.jbyteArray +pub type JavaCharArray = voidptr // C.jcharArray +pub type JavaShortArray = voidptr // C.jshortArray +pub type JavaIntArray = voidptr // C.jintArray +pub type JavaLongArray = voidptr // C.jlongArray +pub type JavaFloatArray = voidptr // C.jfloatArray +pub type JavaDoubleArray = voidptr // C.jdoubleArray +pub type JavaObjectArray = voidptr // C.jobjectArray pub type JavaValue = C.jvalue -// jni.h -@[typedef] -struct C.jstring {} +// For more info on the C types see https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/types.html -@[typedef] -struct C.JNIEnv {} +pub type C.jboolean = u8 -@[typedef] -struct C.JavaVM {} +pub type C.jbyte = i8 -@[typedef] -struct C.jobject {} +pub type C.jchar = u16 -@[typedef] -struct C.jclass {} +pub type C.jshort = i16 -@[typedef] -struct C.jmethodID {} +pub type C.jint = i32 -@[typedef] -struct C.jfieldID {} +pub type C.jlong = i64 -@[typedef] -struct C.jthrowable {} +pub type C.jfloat = f32 + +pub type C.jdouble = f64 + +pub type C.jsize = i32 // C.jint + +// jni.h @[typedef] -struct C.jweak {} +pub struct C.JNIEnv {} -// @[typedef] -struct C.JNINativeMethod { - name &char - signature &char - fn_ptr voidptr -} +pub struct C.JavaVM {} + +pub type C.jobject = voidptr + +pub type C.jclass = voidptr // C.jobject = void* + +pub type C.jstring = voidptr // Arrays -@[typedef] -struct C.jarray {} +pub type C.jarray = voidptr -@[typedef] -struct C.jbyteArray {} +pub type C.jobjectArray = voidptr // C.jarray = C.jobject = void* -@[typedef] -struct C.jcharArray {} +pub type C.jbooleanArray = voidptr // C.jarray = C.jobject = void* -@[typedef] -struct C.jshortArray {} +pub type C.jbyteArray = voidptr // C.jarray = C.jobject = void* -@[typedef] -struct C.jintArray {} +pub type C.jcharArray = voidptr // C.jarray = C.jobject = void* + +pub type C.jshortArray = voidptr // C.jarray = C.jobject = void* + +pub type C.jintArray = voidptr // C.jarray = C.jobject = void* + +pub type C.jlongArray = voidptr // C.jarray = C.jobject = void* + +pub type C.jfloatArray = voidptr // C.jarray = C.jobject = void* + +pub type C.jdoubleArray = voidptr // C.jarray = C.jobject = void* + +// Misc +pub type C.jthrowable = voidptr // C.jobject = void* +pub type C.jweak = voidptr // C.jobject = void* @[typedef] -struct C.jlongArray {} +pub struct C.jmethodID {} @[typedef] -struct C.jfloatArray {} +pub struct C.jfieldID {} @[typedef] -struct C.jdoubleArray {} +pub struct C.jthrowable {} +// @[typedef] -struct C.jobjectArray {} +pub struct C.JNINativeMethod { + name &char + signature &char + fn_ptr voidptr +} @[typedef] -union C.jvalue { +pub union C.jvalue { z C.jboolean b C.jbyte c C.jchar @@ -167,7 +179,7 @@ pub fn find_class(env &Env, name string) JavaClass { } n := name.replace('.', '/') $if debug { - mut cls := C.jclass(0) //{} + mut cls := JavaClass(unsafe { nil }) // C.jclass(0) $if android { cls = C.gFindClass(n.str) } $else { @@ -176,7 +188,7 @@ pub fn find_class(env &Env, name string) JavaClass { if exception_check(env) { exception_describe(env) if !isnil(cls) { - o := &JavaObject(voidptr(&cls)) // o := C.ClassToObject(cls) + o := JavaObject(cls) // o := C.ClassToObject(cls) // o := C.ClassToObject(cls) delete_local_ref(env, o) } @@ -338,7 +350,7 @@ pub fn get_method_id(env &Env, clazz JavaClass, name string, sig string) JavaMet if exception_check(env) { exception_describe(env) if !isnil(mid) { - o := &JavaObject(voidptr(&mid)) // o := C.MethodIDToObject(mid) + o := JavaObject(mid) // o := C.MethodIDToObject(mid) delete_local_ref(env, o) } panic(@MOD + '.' + @FN + @@ -691,8 +703,8 @@ pub fn set_object_field(env &Env, obj JavaObject, field_id JavaFieldID, val Java pub fn set_string_field(env &Env, obj JavaObject, field_id JavaFieldID, val string) { jstr := jstring(env, val) - jobj := &JavaObject(voidptr(&jstr)) - set_object_field(env, obj, field_id, jobj) + // jobj := &JavaObject(voidptr(&jstr)) + set_object_field(env, obj, field_id, jstr) } fn C.SetBooleanField(env &C.JNIEnv, obj C.jobject, fieldID C.jfieldID, val C.jboolean) @@ -743,7 +755,7 @@ pub fn get_static_method_id(env &Env, clazz JavaClass, name string, sig string) if exception_check(env) { exception_describe(env) if !isnil(mid) { - o := &JavaObject(voidptr(&mid)) // o := C.MethodIDToObject(mid) + o := JavaObject(mid) // TODO? o := C.MethodIDToObject(mid) delete_local_ref(env, o) } // clsn := get_class_name(env, clazz) @@ -1168,4 +1180,12 @@ fn C.GetDirectBufferCapacity(env &C.JNIEnv, buf C.jobject) C.jlong // New JNI 1.6 Features -fn C.GetObjectRefType(env &C.JNIEnv, obj C.jobject) C.jobjectRefType +// JObjectRefType is C.jobjectRefType +pub enum JObjectRefType { + invald = C.JNIInvalidRefType // = 0, + local = C.JNILocalRefType // = 1, + global = C.JNIGlobalRefType // = 2, + weak_global = C.JNIWeakGlobalRefType // = 3 +} + +fn C.GetObjectRefType(env &C.JNIEnv, obj C.jobject) JObjectRefType diff --git a/jni.v b/jni.v index b56526e..1db05e6 100644 --- a/jni.v +++ b/jni.v @@ -292,8 +292,8 @@ pub fn call_object_method(env &Env, obj JavaObject, signature string, args ...Ty fn get_class_static_method_id(env &Env, fqn_sig string) (JavaClass, JavaMethodID) { clazz, fn_name, fn_sig := v2j_signature(fqn_sig) - // mut jclazz := JavaClass{} - mut jclazz := C.jclass(0) + + mut jclazz := JavaClass(unsafe { nil }) // Find the Java class $if android { jclazz = find_class(default_env(), clazz) @@ -340,7 +340,8 @@ pub fn (jo JavaObject) call(env &Env, typ MethodType, signature string, args ... @[inline] pub fn (jc JavaClass) get_name(env &Env) string { unsafe { - o := &JavaObject(voidptr(&jc)) + // o := &JavaObject(voidptr(&jc)) + o := JavaObject(jc) return o.class_name(env) } } diff --git a/types.v b/types.v index de5f8e2..088c9e3 100644 --- a/types.v +++ b/types.v @@ -180,7 +180,8 @@ pub fn v2j_value(env &Env, vt Type) JavaValue { } string { jstr := jstring(env, vt) - jobj := &JavaObject(voidptr(&jstr)) + // jobj := &JavaObject(voidptr(&jstr)) + jobj := JavaObject(jstr) JavaValue{ l: jobj } @@ -231,7 +232,7 @@ pub fn j2v_byte(jbyte C.jbyte) u8 { @[inline] pub fn j2v_boolean(jbool C.jboolean) bool { - return jbool == C.jboolean(C.JNI_TRUE) + return jbool == C.JNI_TRUE // C.jboolean(C.JNI_TRUE) } @[inline] @@ -267,49 +268,49 @@ pub fn j2v_double(jdouble C.jdouble) f64 { @[inline] pub fn jboolean(val bool) C.jboolean { if val { - return C.jboolean(C.JNI_TRUE) + return u8(C.JNI_TRUE) // C.jboolean(C.JNI_TRUE) } - return C.jboolean(C.JNI_FALSE) + return u8(C.JNI_FALSE) // C.jboolean(C.JNI_FALSE) } @[inline] pub fn jbyte(val u8) C.jbyte { - return C.jbyte(val) + return val // C.jbyte(val) } @[inline] pub fn jfloat(val f32) C.jfloat { - return C.jfloat(val) + return val // C.jfloat(val) } @[inline] pub fn jdouble(val f64) C.jdouble { - return C.jdouble(val) + return val // C.jdouble(val) } @[inline] pub fn jint(val int) C.jint { - return C.jint(val) + return i32(val) // C.jint(val) } @[inline] pub fn jsize(val int) C.jsize { - return C.jsize(val) + return i32(val) // C.jsize(val) = C.jint = i32 } @[inline] pub fn jshort(val i16) C.jshort { - return C.jshort(val) + return val // C.jshort(val) } @[inline] pub fn jlong(val i64) C.jlong { - return C.jlong(val) + return val // C.jlong(val) } @[inline] pub fn jchar(val rune) C.jchar { - return C.jchar(val) + return u16(val) // C.jchar(val) } @[inline] diff --git a/util.c.v b/util.c.v index a2fdb12..56b9d7b 100644 --- a/util.c.v +++ b/util.c.v @@ -15,7 +15,7 @@ pub fn set_java_vm(vm &JavaVM) { } pub fn env_detach() (&Env, bool) { - env := &C.JNIEnv(0) + env := &C.JNIEnv(unsafe { nil }) need_detach := C.gEnvNeedDetach(&env) if isnil(env) {