Ejemplo n.º 1
0
// private static native void setIn0(InputStream in);
// (Ljava/io/InputStream;)V
func setIn0(frame *rtda.Frame) {
	vars := frame.LocalVars()
	in := vars.GetRef(0)

	sysClass := frame.Method().Class()
	sysClass.SetStaticValue("in", "Ljava/io/InputStream;", in)
}
Ejemplo n.º 2
0
// private static native void setOut0(PrintStream out);
// (Ljava/io/PrintStream;)V
func setOut0(frame *rtda.Frame) {
	vars := frame.LocalVars()
	out := vars.GetRef(0)

	sysClass := frame.Method().Class()
	sysClass.SetStaticValue("out", "Ljava/io/PrintStream;", out)
}
Ejemplo n.º 3
0
// private static native void setErr0(PrintStream err);
// (Ljava/io/PrintStream;)V
func setErr0(frame *rtda.Frame) {
	vars := frame.LocalVars()
	err := vars.GetRef(0)

	sysClass := frame.Method().Class()
	sysClass.SetStaticValue("err", "Ljava/io/PrintStream;", err)
}
Ejemplo n.º 4
0
//  native void socketConnect(InetAddress address, int port, int timeout)
//  throws IOException;
func psi_socketConnect(frame *rtda.Frame) {
	vars := frame.LocalVars()
	this := vars.GetThis()
	address := vars.GetRef(1)
	port := vars.Get(2)

	holder := address.GetFieldValue("holder", "Ljava/net/InetAddress$InetAddressHolder;").(*rtc.Obj)

	//fmt.Println(address.Class().GetInstanceMethod("getHostAddress", "()Ljava/lang/String;").NativeMethod())
	add := holder.GetFieldValue("address", "I").(int32)
	b := make([]byte, 4)
	binary.BigEndian.PutUint32(b, uint32(add))
	laddr := fmt.Sprintf("%d.%d.%d.%d:%d", b[0], b[1], b[2], b[3], port)

	conn, err := net.Dial("tcp", laddr)

	if err != nil {
		frame.Thread().ThrowIOException(err.Error())
	}
	//TODO what ? timeout how to implement ?
	_timeout := vars.GetInt(3)
	if _timeout > 0 {
		conn.SetDeadline(time.Now().Add(time.Duration(_timeout) * time.Millisecond))
	}

	fdObj := this.GetFieldValue("fd", "Ljava/io/FileDescriptor;").(*rtc.Obj)
	fdObj.SetExtra(conn)
}
Ejemplo n.º 5
0
//private native int socketRead0(FileDescriptor fd, byte b[], int off, int len, int timeout)
// java/net/SocketInputStream~socketRead0~(Ljava/io/FileDescriptor;[BIII)I
func sis_socketRead0(frame *rtda.Frame) {
	vars := frame.LocalVars()
	//this := vars.GetThis()
	fd := vars.GetRef(1)
	buf := vars.GetRef(2)
	off := vars.GetInt(3)
	_len := vars.GetInt(4)

	conn := fd.Extra().(net.Conn)

	_timeout := vars.GetInt(5)
	if _timeout > 0 {
		conn.SetDeadline(time.Now().Add(time.Duration(_timeout) * time.Millisecond))
	}

	goBuf := buf.GoBytes()
	goBuf = goBuf[off : off+_len]

	n, err := conn.Read(goBuf)
	if err == nil || n > 0 || err == io.EOF {
		frame.OperandStack().PushInt(int32(n))
	} else {
		// todo
		panic(err.Error())
		//frame.Thread().ThrowIOException(err.Error())
	}

}
Ejemplo n.º 6
0
// public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
// (Ljava/lang/Object;ILjava/lang/Object;II)V
func arraycopy(frame *rtda.Frame) {
	vars := frame.LocalVars()
	src := vars.GetRef(0)
	srcPos := vars.GetInt(1)
	dest := vars.GetRef(2)
	destPos := vars.GetInt(3)
	length := vars.GetInt(4)

	// NullPointerException
	if src == nil || dest == nil {
		panic("NPE") // todo
	}
	// ArrayStoreException
	if !checkArrayCopy(src, dest) {
		panic("ArrayStoreException")
	}
	// IndexOutOfBoundsException
	if srcPos < 0 || destPos < 0 || length < 0 ||
		srcPos+length > rtc.ArrayLength(src) ||
		destPos+length > rtc.ArrayLength(dest) {

		panic("IndexOutOfBoundsException") // todo
	}

	rtc.ArrayCopy(src, dest, srcPos, destPos, length)
}
Ejemplo n.º 7
0
// public static native void set(Object array, int index, Object value)
//        throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
// (Ljava/lang/Object;ILjava/lang/Object;)V
func set(frame *rtda.Frame) {
	vars := frame.LocalVars()
	arr := vars.GetRef(0)
	index := vars.GetInt(1)
	value := vars.GetRef(2)

	if arr == nil {
		frame.Thread().ThrowNPE()
		return
	}
	if !arr.IsArray() {
		frame.Thread().ThrowIllegalArgumentException("Argument is not an array")
		return
	}

	if index < 0 || index >= rtc.ArrayLength(arr) {
		frame.Thread().ThrowArrayIndexOutOfBoundsExceptionNoMsg()
		return
	}

	if !arr.IsPrimitiveArray() {
		arr.Refs()[index] = value
		return
	}

	//TODO Consistent with the current need to determine whether the type and source type
	// Such as:
	// [I
	// java/lang/Integer
	// frame.Thread().ThrowIllegalArgumentException("argument type mismatch")

	// primitive array
	primitiveDescriptorStr := arr.Class().Name()[1:]
	if primitiveDescriptorStr != value.GetPrimitiveDescriptor() {
		frame.Thread().ThrowIllegalArgumentException("argument type mismatch")
		return
	}

	unboxed := box.Unbox(value, primitiveDescriptorStr)

	primitiveDescriptor := arr.Class().Name()[1]
	switch primitiveDescriptor {
	case 'Z':
		arr.Booleans()[index] = int8(unboxed.(int32))
	case 'B':
		arr.Bytes()[index] = int8(unboxed.(int32))
	case 'C':
		arr.Chars()[index] = uint16(unboxed.(int32))
	case 'S':
		arr.Shorts()[index] = int16(unboxed.(int32))
	case 'I':
		arr.Ints()[index] = unboxed.(int32)
	case 'J':
		arr.Longs()[index] = unboxed.(int64)
	case 'F':
		arr.Floats()[index] = unboxed.(float32)
	case 'D':
		arr.Doubles()[index] = unboxed.(float64)
	}
}
Ejemplo n.º 8
0
// public native void setLength(long newLength) throws IOException;
// (J)V
func raf_setLength(frame *rtda.Frame) {
	vars := frame.LocalVars()
	this := vars.GetThis()
	//length := vars.GetLong(1)

	goFile := this.Extra().(*os.File)

	cur, _ := goFile.Seek(0, os.SEEK_CUR)

	//TODO
	//How do set file length in Go ?
	panic("native method not implement! RandomAccessFile.setLength")
	var newLength int64
	newLength = 0
	if cur > newLength {
		if _, err := goFile.Seek(0, os.SEEK_END); err != nil {
			frame.Thread().ThrowIOException("setLength failed")
		}

	} else {
		if _, err := goFile.Seek(cur, os.SEEK_SET); err != nil {
			frame.Thread().ThrowIOException("setLength failed")
		}
	}
}
Ejemplo n.º 9
0
// private native void open(String name, int mode) throws FileNotFoundException;
// (Ljava/lang/String;)V
func raf_open(frame *rtda.Frame) {
	vars := frame.LocalVars()
	this := vars.GetThis()
	name := vars.GetRef(1)
	mode := vars.GetInt(2) //flag
	flag := 0

	if mode&1 > 0 {
		flag |= os.O_RDONLY
	}

	//write
	if mode&2 > 0 {
		flag |= os.O_RDWR | os.O_CREATE
	}

	if mode&4 > 0 {
		flag |= os.O_SYNC | os.O_CREATE
	}

	if mode&8 > 0 {
		//TODO:O_DSYNC
		flag |= os.O_SYNC | os.O_CREATE
	}

	goName := rtda.GoString(name)
	if goFile, err := os.OpenFile(goName, flag, 0660); err != nil {
		frame.Thread().ThrowFileNotFoundException(goName)
	} else {
		this.SetExtra(goFile)
	}
}
Ejemplo n.º 10
0
// private static native int findSignal(String string);
// (Ljava/lang/String;)I
func findSignal(frame *rtda.Frame) {
	vars := frame.LocalVars()
	vars.GetRef(0) // name

	stack := frame.OperandStack()
	stack.PushInt(0) // todo
}
Ejemplo n.º 11
0
// private native void writeBytes(byte b[], int off, int len, boolean append) throws IOException;
// ([BIIZ)V
func writeBytes(frame *rtda.Frame) {
	vars := frame.LocalVars()
	fosObj := vars.GetRef(0)     // this
	byteArrObj := vars.GetRef(1) // b
	offset := vars.GetInt(2)     // off
	length := vars.GetInt(3)     // len
	//vars.GetBoolean(4) // append

	fdObj := fosObj.GetFieldValue("fd", "Ljava/io/FileDescriptor;").(*rtc.Obj)
	if fdObj.Extra() == nil {
		goFd := fdObj.GetFieldValue("fd", "I").(int32)
		switch goFd {
		case 0:
			fdObj.SetExtra(os.Stdin)
		case 1:
			fdObj.SetExtra(os.Stdout)
		case 2:
			fdObj.SetExtra(os.Stderr)
		}
	}
	goFile := fdObj.Extra().(*os.File)

	goBytes := byteArrObj.GoBytes()
	goBytes = goBytes[offset : offset+length]
	goFile.Write(goBytes)
}
Ejemplo n.º 12
0
func _getEntryPop(frame *rtda.Frame) *gozip.File {
	vars := frame.LocalVars()
	jzentry := vars.GetLong(0)

	entry := getEntryFile(jzentry)
	return entry
}
Ejemplo n.º 13
0
//  public native void unpark(Object thread);
//  (Ljava/lang/Object;)V
func unpark(frame *rtda.Frame) {
	vars := frame.LocalVars()
	threadObj := vars.GetRef(1)

	thread := threadObj.Extra().(*rtda.Thread)
	thread.Unpark()
}
Ejemplo n.º 14
0
// private native void interrupt0();
// ()V
func interrupt0(frame *rtda.Frame) {
	vars := frame.LocalVars()
	this := vars.GetThis()

	thread := _extraThread(this)
	thread.Interrupt()
}
Ejemplo n.º 15
0
//(Ljava/lang/String;)[Ljava/net/InetAddress;
func i6di_lookupAllHostAddr(frame *rtda.Frame) {
	vars := frame.LocalVars()
	host := rtda.GoString(vars.GetRef(1))
	address, _ := net.LookupHost(host)
	constructorCount := uint(len(address))

	inetAddress := rtc.BootLoader().LoadClass("java/net/InetAddress")
	inetAddressArr := inetAddress.NewArray(constructorCount)

	stack := frame.OperandStack()
	stack.PushRef(inetAddressArr)

	//TODO
	//getByName descriptor:(Ljava/lang/String;)Ljava/net/InetAddress;
	//if constructorCount > 0 {
	//	thread := frame.Thread()
	//	constructorObjs := inetAddressArr.Refs()
	//	inetAddressGetByNameMethod := inetAddress.GetStaticMethod("getByName", "(Ljava/lang/String;)Ljava/net/InetAddress;")

	//	fmt.Println(constructorObjs[0])
	//	fmt.Println(inetAddressGetByNameMethod)
	//	fmt.Println(thread)
	//	thread.InvokeMethodWithShim(inetAddressGetByNameMethod, []Any{
	//		constructorObjs[0],
	//		rtda.JString(host),
	//	})
	//}
}
Ejemplo n.º 16
0
// protected native Object clone() throws CloneNotSupportedException;
// ()Ljava/lang/Object;
func clone(frame *rtda.Frame) {
	vars := frame.LocalVars()
	this := vars.GetThis()

	// todo
	stack := frame.OperandStack()
	stack.PushRef(this.Clone())
}
Ejemplo n.º 17
0
// public native int hashCode();
// ()I
func hashCode(frame *rtda.Frame) {
	vars := frame.LocalVars()
	this := vars.GetThis()

	hash := int32(uintptr(unsafe.Pointer(this)))
	stack := frame.OperandStack()
	stack.PushInt(hash)
}
Ejemplo n.º 18
0
// public final native Class<?> getClass();
// ()Ljava/lang/Class;
func getClass(frame *rtda.Frame) {
	vars := frame.LocalVars()
	this := vars.GetThis()

	class := this.Class().JClass()
	stack := frame.OperandStack()
	stack.PushRef(class)
}
Ejemplo n.º 19
0
func _get(frame *rtda.Frame) (*rtda.OperandStack, []byte) {
	vars := frame.LocalVars()
	// vars.GetRef(0) // this
	address := vars.GetLong(1)

	stack := frame.OperandStack()
	mem := memoryAt(address)
	return stack, mem
}
Ejemplo n.º 20
0
// static native MemberName resolve(MemberName self, Class<?> caller) throws LinkageError;
// (Ljava/lang/invoke/MemberName;Ljava/lang/Class;)Ljava/lang/invoke/MemberName;
func resolve(frame *rtda.Frame) {
	vars := frame.LocalVars()
	mn := vars.GetRef(0)
	// caller := vars.GetRef(1)

	// panic("todo resolve")
	stack := frame.OperandStack()
	stack.PushRef(mn)
}
Ejemplo n.º 21
0
// public static native double longBitsToDouble(long bits);
// (J)D
func longBitsToDouble(frame *rtda.Frame) {
	vars := frame.LocalVars()
	bits := vars.GetLong(0)

	// todo
	value := math.Float64frombits(uint64(bits))
	stack := frame.OperandStack()
	stack.PushDouble(value)
}
Ejemplo n.º 22
0
// public native long staticFieldOffset(Field f);
// (Ljava/lang/reflect/Field;)J
func staticFieldOffset(frame *rtda.Frame) {
	vars := frame.LocalVars()
	// vars.GetRef(0) // this
	fieldObj := vars.GetRef(1)

	offset := fieldObj.GetFieldValue("slot", "I").(int32)
	stack := frame.OperandStack()
	stack.PushLong(int64(offset))
}
Ejemplo n.º 23
0
// private static native long handle0(int i, long l);
// (IJ)J
func handle0(frame *rtda.Frame) {
	// todo
	vars := frame.LocalVars()
	vars.GetInt(0)
	vars.GetLong(1)

	stack := frame.OperandStack()
	stack.PushLong(0)
}
Ejemplo n.º 24
0
// private static native int getTotal(long jzfile);
// (J)I
func getTotal(frame *rtda.Frame) {
	vars := frame.LocalVars()
	jzfile := vars.GetLong(0)

	total := getEntryCount(jzfile)

	stack := frame.OperandStack()
	stack.PushInt(total)
}
Ejemplo n.º 25
0
// public native long objectFieldOffset(Field field);
// (Ljava/lang/reflect/Field;)J
func objectFieldOffset(frame *rtda.Frame) {
	vars := frame.LocalVars()
	jField := vars.GetRef(1)

	offset := jField.GetFieldValue("slot", "I").(int32)

	stack := frame.OperandStack()
	stack.PushLong(int64(offset))
}
Ejemplo n.º 26
0
// public static native int getLength(Object array) throws IllegalArgumentException;
// (Ljava/lang/Object;)I
func getLength(frame *rtda.Frame) {
	vars := frame.LocalVars()
	arr := vars.GetRef(0)

	// todo IllegalArgumentException
	_len := rtc.ArrayLength(arr)
	stack := frame.OperandStack()
	stack.PushInt(_len)
}
Ejemplo n.º 27
0
// public static native long doubleToRawLongBits(double value);
// (D)J
func doubleToRawLongBits(frame *rtda.Frame) {
	vars := frame.LocalVars()
	value := vars.GetDouble(0)

	// todo
	bits := math.Float64bits(value)
	stack := frame.OperandStack()
	stack.PushLong(int64(bits))
}
Ejemplo n.º 28
0
func _put(frame *rtda.Frame) ([]byte, Any) {
	vars := frame.LocalVars()
	// vars.GetRef(0) // this
	address := vars.GetLong(1)
	value := vars.Get(3)

	mem := memoryAt(address)
	return mem, value
}
Ejemplo n.º 29
0
// public native long allocateMemory(long bytes);
// (J)J
func allocateMemory(frame *rtda.Frame) {
	vars := frame.LocalVars()
	// vars.GetRef(0) // this
	bytes := vars.GetLong(1)

	address := allocate(bytes)
	stack := frame.OperandStack()
	stack.PushLong(address)
}
Ejemplo n.º 30
0
// public static native int identityHashCode(Object x);
// (Ljava/lang/Object;)I
func identityHashCode(frame *rtda.Frame) {
	vars := frame.LocalVars()
	ref := vars.GetRef(0)

	// todo
	hashCode := int32(uintptr(unsafe.Pointer(ref)))
	stack := frame.OperandStack()
	stack.PushInt(hashCode)
}