Skip to content

Commit

Permalink
all: fix Java types
Browse files Browse the repository at this point in the history
  • Loading branch information
larpon committed Jan 17, 2024
1 parent b62d36e commit 23b0c93
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 109 deletions.
2 changes: 1 addition & 1 deletion android/android.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
4 changes: 2 additions & 2 deletions android/keyboard/keyboard.v
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions examples/android/keyboard/keyboard.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
}

Expand Down Expand Up @@ -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(
Expand Down
26 changes: 2 additions & 24 deletions examples/android/toast/toast.v
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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(
Expand Down
146 changes: 83 additions & 63 deletions jni.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
Expand Down Expand Up @@ -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 +
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
7 changes: 4 additions & 3 deletions jni.v
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
}
Loading

0 comments on commit 23b0c93

Please sign in to comment.