Skip to content

Commit

Permalink
Change to use int32 internally to represent Java int
Browse files Browse the repository at this point in the history
* Allow *int32, *[]int32 destination values for return/get
  • Loading branch information
timob committed Nov 23, 2023
1 parent a2cc97c commit 62f4e26
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 18 deletions.
28 changes: 24 additions & 4 deletions helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func AttemptToFindJVMLibPath() string {
return libPath
}

// there should be no cases where val underlying type is an int or []int
func assignDest(val interface{}, dest interface{}) error {
if dest == nil {
return nil
Expand Down Expand Up @@ -69,10 +70,13 @@ func assignDest(val interface{}, dest interface{}) error {
*dv = v
assigned = true
}
case int:
if dv, ok := dest.(*int); ok {
case int32:
if dv, ok := dest.(*int32); ok {
*dv = v
assigned = true
} else if dv, ok := dest.(*int); ok {
*dv = int(v)
assigned = true
}
case int64:
if dv, ok := dest.(*int64); ok {
Expand Down Expand Up @@ -109,10 +113,17 @@ func assignDest(val interface{}, dest interface{}) error {
*dv = v
assigned = true
}
case []int:
if dv, ok := dest.(*[]int); ok {
case []int32:
if dv, ok := dest.(*[]int32); ok {
*dv = v
assigned = true
} else if dv, ok := dest.(*[]int); ok {
sliceAssigned := make([]int, len(v))
for i, e := range v {
sliceAssigned[i] = int(e)
}
*dv = sliceAssigned
assigned = true
}
case []int64:
if dv, ok := dest.(*[]int64); ok {
Expand Down Expand Up @@ -141,3 +152,12 @@ func assignDest(val interface{}, dest interface{}) error {
}
return nil
}

// returned int32 can be converted to jint
func assignJavaIntFromInt(v int) int32 {
v2 := int32(v)
if int(v2) != v {
fmt.Fprintf(os.Stderr, "JNIGI WARNING int -> Int conversion changed value")
}
return v2
}
25 changes: 11 additions & 14 deletions jnigi.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,17 +469,14 @@ func (j *Env) toGoArray(array jobject, aType Type) (interface{}, error) {
}
return v, nil
case Int:
v := make([]int, len)
v := make([]int32, len)
if len >= 0 {
ptr := getIntArrayElements(j.jniEnv, jintArray(array), nil)
if j.exceptionCheck() {
return nil, j.handleException()
}
elems := (*(*[big]int32)(ptr))[0:len]
//copy(v, elems)
for i := 0; i < len; i++ {
v[i] = int(elems[i])
}
copy(v, elems)
releaseIntArrayElements(j.jniEnv, jintArray(array), ptr, jint(jni_abort))
}
return v, nil
Expand Down Expand Up @@ -769,7 +766,7 @@ func (j *Env) toJavaArray(src interface{}) (jobject, error) {
} else {
data := make([]int32, len(v))
for i := 0; i < len(v); i++ {
data[i] = int32(v[i])
data[i] = assignJavaIntFromInt(v[i])
}
ptr = unsafe.Pointer(&data[0])
}
Expand Down Expand Up @@ -881,7 +878,7 @@ func (j *Env) createArgs(args []interface{}) (ptr unsafe.Pointer, refs []jobject
case int32:
argList[i] = uint64(jint(v))
case int:
argList[i] = uint64(jint(int32(v)))
argList[i] = uint64(jint(assignJavaIntFromInt(v)))
case int64:
argList[i] = uint64(jlong(v))
case float32:
Expand Down Expand Up @@ -1256,7 +1253,7 @@ func (o *ObjectRef) genericCallMethod(env *Env, methodName string, rType Type, r
case rType == Short:
retVal = int16(callShortMethodA(env.jniEnv, o.jobject, mid, jniArgs))
case rType == Int:
retVal = int(callIntMethodA(env.jniEnv, o.jobject, mid, jniArgs))
retVal = int32(callIntMethodA(env.jniEnv, o.jobject, mid, jniArgs))
case rType == Long:
retVal = int64(callLongMethodA(env.jniEnv, o.jobject, mid, jniArgs))
case rType == Float:
Expand Down Expand Up @@ -1357,7 +1354,7 @@ func (o *ObjectRef) genericCallNonvirtualMethod(env *Env, className string, meth
case rType == Short:
retVal = int16(callNonvirtualShortMethodA(env.jniEnv, o.jobject, class, mid, jniArgs))
case rType == Int:
retVal = int(callNonvirtualIntMethodA(env.jniEnv, o.jobject, class, mid, jniArgs))
retVal = int32(callNonvirtualIntMethodA(env.jniEnv, o.jobject, class, mid, jniArgs))
case rType == Long:
retVal = int64(callNonvirtualLongMethodA(env.jniEnv, o.jobject, class, mid, jniArgs))
case rType == Float:
Expand Down Expand Up @@ -1458,7 +1455,7 @@ func (j *Env) genericCallStaticMethod(className string, methodName string, rType
case rType == Short:
retVal = int16(callStaticShortMethodA(j.jniEnv, class, mid, jniArgs))
case rType == Int:
retVal = int(callStaticIntMethodA(j.jniEnv, class, mid, jniArgs))
retVal = int32(callStaticIntMethodA(j.jniEnv, class, mid, jniArgs))
case rType == Long:
retVal = int64(callStaticLongMethodA(j.jniEnv, class, mid, jniArgs))
case rType == Float:
Expand Down Expand Up @@ -1558,7 +1555,7 @@ func (o *ObjectRef) genericGetField(env *Env, fieldName string, fType Type, fCla
case fType == Short:
retVal = int16(getShortField(env.jniEnv, o.jobject, fid))
case fType == Int:
retVal = int(getIntField(env.jniEnv, o.jobject, fid))
retVal = int32(getIntField(env.jniEnv, o.jobject, fid))
case fType == Long:
retVal = int64(getLongField(env.jniEnv, o.jobject, fid))
case fType == Float:
Expand Down Expand Up @@ -1616,7 +1613,7 @@ func (o *ObjectRef) SetField(env *Env, fieldName string, value interface{}) erro
case int32:
setIntField(env.jniEnv, o.jobject, fid, jint(v))
case int:
setIntField(env.jniEnv, o.jobject, fid, jint(int32(v)))
setIntField(env.jniEnv, o.jobject, fid, jint(assignJavaIntFromInt(v)))
case int64:
setLongField(env.jniEnv, o.jobject, fid, jlong(v))
case float32:
Expand Down Expand Up @@ -1702,7 +1699,7 @@ func (j *Env) genericGetStaticField(className string, fieldName string, fType Ty
case fType == Short:
retVal = int16(getStaticShortField(j.jniEnv, class, fid))
case fType == Int:
retVal = int(getStaticIntField(j.jniEnv, class, fid))
retVal = int32(getStaticIntField(j.jniEnv, class, fid))
case fType == Long:
retVal = int64(getStaticLongField(j.jniEnv, class, fid))
case fType == Float:
Expand Down Expand Up @@ -1760,7 +1757,7 @@ func (j *Env) SetStaticField(className string, fieldName string, value interface
case int32:
setStaticIntField(j.jniEnv, class, fid, jint(v))
case int:
setStaticIntField(j.jniEnv, class, fid, jint(int32(v)))
setStaticIntField(j.jniEnv, class, fid, jint(assignJavaIntFromInt(v)))
case int64:
setStaticLongField(j.jniEnv, class, fid, jlong(v))
case float32:
Expand Down

0 comments on commit 62f4e26

Please sign in to comment.