Ejemplo n.º 1
0
func jBoolValue(v interface{}) (C.jvalue, error) {
	val, ok := v.(bool)
	if !ok {
		return errJValue, fmt.Errorf("%#v isn't bool", v)
	}
	jBool := C.jboolean(C.JNI_FALSE)
	if val {
		jBool = C.jboolean(C.JNI_TRUE)
	}
	return C.jBoolValue(jBool), nil
}
Ejemplo n.º 2
0
func (self *Environment) ToIntArray(arrayObj *Object) (array []int) {
	alen := C.envGetArrayLength(self.env, arrayObj.object)
	_false := C.jboolean(C.JNI_FALSE)
	ptr := C.envGetIntArrayElements(self.env, arrayObj.object, &_false)
	defer C.envReleaseIntArrayElements(self.env, arrayObj.object, ptr, 0)
	bytes := C.GoBytes(unsafe.Pointer(ptr), C.int(alen)*4)
	array = (*(*[1024 * 1024]int)(unsafe.Pointer(&bytes)))[0:int(alen)]
	return
}
Ejemplo n.º 3
0
func (self *Environment) ToString(strobj *Object) (str string, isNull bool, err error) {
	var bytesObj *Object
	bytesObj, err = self.CallObjectObj(strobj, false, "getBytes", types.Array{types.Basic(types.ByteKind)}, self.utf8())
	if err == nil && bytesObj == nil {
		isNull = true
		return
	}
	if err == nil {
		defer self.DeleteLocalRef(bytesObj)
		alen := C.envGetArrayLength(self.env, bytesObj.object)
		_false := C.jboolean(C.JNI_FALSE)
		ptr := C.envGetByteArrayElements(self.env, bytesObj.object, &_false)
		defer C.envReleaseByteArrayElements(self.env, bytesObj.object, ptr, 0)
		str = string(C.GoBytes(unsafe.Pointer(ptr), C.int(alen)))
	}
	return
}
Ejemplo n.º 4
0
func (self *Environment) setBoolField(z interface{}, static bool, name string, gval bool) (err error) {
	jval, field, err := self.getField(z, static, name, types.Basic(types.BoolKind))
	if err != nil {
		return
	}
	val := C.jboolean(C.JNI_FALSE)
	if gval {
		val = C.JNI_TRUE
	}
	if static {
		C.envSetStaticBooleanField(self.env, C.valObject(jval), field.field, val)
	} else {
		C.envSetBooleanField(self.env, C.valObject(jval), field.field, val)
	}
	if self.ExceptionCheck() {
		err = self.ExceptionOccurred()
	}
	return
}
Ejemplo n.º 5
0
// C callbacks actually start in the 'generifiedX' calls (see the .c files)
// Next, goCallbackNArgs is used to determine the number of variadic paramers to expect (
// java doesn't inform us of this, and we can't force any of the callback parameters.
//
// Finally, goCallback looks up the 'fId' - our internal function reference ID (the X in generified),
// un(re) marshalls all the parameters appropriately, calls our function, and returns
// any underlying value back to generified who will return it to the JVM.
// The initial jbool indicates success, and any failure should check for exceptions.
//
//export goCallback
func goCallback(envp, obj uintptr, fId int, nargs int, argp uintptr) (ok C.jboolean, val interface{}) {
	args := C.ArgListPtr(unsafe.Pointer(argp))
	env := AllEnvs.Find(envp)
	if env == nil {
		panic("Got a nil environment")
	}
	if env.jvm == nil {
		panic("Environment pointer has no JVM")
	}
	var cd callbackDescriptor
	var _ok bool
	if cd, _ok = env.jvm.registered[fId]; !_ok {
		print("Unknown callbackId: \t", fId, "\n")
		return
	}
	// TODO: pack argp somehow...
	if nargs != len(cd.Signature.Params) {
		panic("callback/signature length mismatch")
	}
	inCall := []reflect.Value{reflect.ValueOf(env), reflect.ValueOf(newObject(C.jobject(unsafe.Pointer(obj))))}
	var err error
	for i := 0; i < nargs; i++ {
		switch cd.Signature.Params[i].Kind() {
		case types.BoolKind:
			inCall = append(inCall, reflect.ValueOf(bool(0 != C.valBool(C.getArg(args, C.int(i))))))
		case types.LongKind:
			inCall = append(inCall, reflect.ValueOf(int64(C.valLong(C.getArg(args, C.int(i))))))
		case types.IntKind:
			inCall = append(inCall, reflect.ValueOf(int(C.valInt(C.getArg(args, C.int(i))))))
		case types.ShortKind:
			inCall = append(inCall, reflect.ValueOf(int16(C.valShort(C.getArg(args, C.int(i))))))
		case types.FloatKind:
			inCall = append(inCall, reflect.ValueOf(float32(C.valFloat(C.getArg(args, C.int(i))))))
		case types.DoubleKind:
			inCall = append(inCall, reflect.ValueOf(float64(C.valDouble(C.getArg(args, C.int(i))))))
		case types.ClassKind:
			inCall = append(inCall, reflect.ValueOf(newObject(C.valObject(C.getArg(args, C.int(i))))))
		default:
			err = errors.New("Couldn't reflect kind " + cd.Signature.Params[i].Kind().TypeString())
		}
		if err != nil {
			break
		}
	}
	if err != nil {
		return
	}
	outCall := reflect.ValueOf(cd.F).Call(inCall)
	switch cd.Signature.Return.Kind() {
	case types.VoidKind:
		return 1, nil
	}
	switch cd.Signature.Return.Kind() {
	case types.BoolKind:
		if outCall[0].Interface().(bool) {
			return 1, C.jboolean(C.JNI_TRUE)
		} else {
			return 1, C.jboolean(C.JNI_FALSE)
		}
	case types.ByteKind:
		return 1, C.jbyte(outCall[0].Interface().(byte))
	case types.CharKind:
		return 1, C.jchar(outCall[0].Interface().(int))
	case types.IntKind:
		return 1, C.jint(outCall[0].Interface().(int))
	case types.ShortKind:
		return 1, C.jshort(outCall[0].Interface().(int16))
	case types.LongKind:
		return 1, C.jint(outCall[0].Interface().(int64))
	case types.FloatKind:
		return 1, C.jfloat(outCall[0].Interface().(float32))
	case types.DoubleKind:
		return 1, C.jdouble(outCall[0].Interface().(float64))
	case types.ClassKind:
		klass := cd.Signature.Return.(types.Class).Klass
		if klass.Cmp(types.JavaLangString) == 0 {
			var obj *Object
			str := outCall[0].Interface().(string)
			obj, err = env.NewStringObject(str)
			if err == nil {
				return 1, C.jstring(obj.object)
			} // else, exception occurred
			// not needed as callbacks will reap their own refs.
			// env.DeleteLocalRef(obj)
			print("String Error\t", err.Error())
			return 0, nil
		}
		return 1, C.jobject(outCall[0].Interface().(*Object).object)
	default:
		panic("array return type not yet supported")
	}

	panic("not reached")
}
Ejemplo n.º 6
0
//	TODO(refcounting): any constructed objects will be leaked on call return,
//	as nothing cleans up proxy objects.  I'm also torn on how to differentiate
//	the objects made here  and those coming in from other references.
//
//	Refcounting attempt 1, objects _we_ construct will be returned in the objStack,
//	otherwise refcounts of 'pass-through' java natives are untouched by the call to newArgList.
//
//	On error, the stack has already been blown (and will be empty).
func newArgList(ctx *Environment, params ...interface{}) (alp argList, objStack []*Object, err error) {
	alp = make(argList, 0)
	defer func() {
		if err != nil {
			blowStack(ctx, objStack)
			objStack = []*Object{}
		}
	}()
	for i, param := range params {
		var ok C.int
		switch v := param.(type) {
		case int:
			alp = append(alp, C.intValue(C.jint(v)))
		case int64:
			alp = append(alp, C.longValue(C.jlong(v)))
		case C.jstring:
			alp = append(alp, C.objValue(v))
		case C.jboolean:
			alp = append(alp, C.boolValue(v))
		case C.jint:
			alp = append(alp, C.intValue(v))
		case C.jobject:
			alp = append(alp, C.objValue(v))
		case *Object:
			alp = append(alp, C.objValue(v.object))
		case *Class:
			alp = append(alp, C.objValue(v.class))
		case C.jvalue:
			alp = append(alp, v)
		case string:
			var str *Object
			str, err = ctx.NewStringObject(v)
			if err == nil {
				objStack = append(objStack, str)
				alp = append(alp, C.objValue(str.object))
			}
		case []string:
			var klass *Class
			var obj *Object
			klass, err = ctx.GetClassStr("java/lang/String")
			// classes via this channel are cached and globally referenced by gojvm, not stacked.
			if err == nil {
				obj, err = ctx.newObjectArray(len(v), klass, nil)
			}
			if err == nil {
				objStack = append(objStack, obj)
				for i, s := range v {
					var str *Object
					str, err = ctx.NewStringObject(s)
					if err == nil {
						// I'm assuming stuffing the array adds a reference inside the JVM.
						defer ctx.DeleteLocalRef(str)
						ctx.setObjectArrayElement(obj, i, str)
						if ctx.ExceptionCheck() {
							err = ctx.ExceptionOccurred()
						}

					}
					if err != nil {
						break
					}
				}
			}
			if err == nil {
				alp = append(alp, C.objValue(obj.object))
			}
		case []byte:
			var obj *Object
			obj, err = ctx.newByteObject(v)
			if err == nil {
				alp = append(alp, C.objValue(obj.object))
			}
			objStack = append(objStack, obj)
		case bool:
			val := C.jboolean(C.JNI_FALSE)
			if v {
				val = C.JNI_TRUE
			}
			alp = append(alp, C.boolValue(val))
		default:
			err = errors.New(fmt.Sprintf("Unknown type: %T/%s", v, v))
		}
		if ok != 0 {
			err = errors.New("Couldn't parse arg #" + strconv.Itoa(i+1))
		}
		if err != nil {
			break
		}
	}
	return
}