Example #1
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),
	//	})
	//}
}
Example #2
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)
	}
}
Example #3
0
// public native String intern();
// ()Ljava/lang/String;
func intern(frame *rtda.Frame) {
	vars := frame.LocalVars()
	jStr := vars.GetThis()

	goStr := rtda.GoString(jStr)
	internedStr := rtda.InternString(goStr, jStr)

	stack := frame.OperandStack()
	stack.PushRef(internedStr)
}
Example #4
0
// static native Class<?> getPrimitiveClass(String name);
// (Ljava/lang/String;)Ljava/lang/Class;
func getPrimitiveClass(frame *rtda.Frame) {
	vars := frame.LocalVars()
	nameObj := vars.GetRef(0)

	name := rtda.GoString(nameObj)
	classLoader := frame.ClassLoader()
	class := classLoader.GetPrimitiveClass(name)
	classObj := class.JClass()

	stack := frame.OperandStack()
	stack.PushRef(classObj)
}
Example #5
0
// private native String canonicalize0(String path) throws IOException;
// (Ljava/lang/String;)Ljava/lang/String;
func canonicalize0(frame *rtda.Frame) {
	vars := frame.LocalVars()
	pathStr := vars.GetRef(1)

	// todo
	path := rtda.GoString(pathStr)
	path2 := filepath.Clean(path)
	if path2 != path {
		pathStr = rtda.JString(path2)
	}

	stack := frame.OperandStack()
	stack.PushRef(pathStr)
}
Example #6
0
// private static native long open(String name, int mode, long lastModified,
//                                 boolean usemmap) throws IOException;
// (Ljava/lang/String;IJZ)J
func open(frame *rtda.Frame) {
	vars := frame.LocalVars()
	nameObj := vars.GetRef(0)

	name := rtda.GoString(nameObj)
	jzfile, err := openZip(name)
	if err != nil {
		// todo
		panic("IOException!" + err.Error())
	}

	stack := frame.OperandStack()
	stack.PushLong(jzfile)
}
Example #7
0
File: Net.go Project: cretz/jvm.go
// private static native void bind0(FileDescriptor fd, boolean preferIPv6, boolean useExclBind, InetAddress addr,int port)
func net_bind0(frame *rtda.Frame) {
	vars := frame.LocalVars()
	this := vars.GetThis()
	address := vars.GetRef(3)
	port := vars.Get(4)

	holder := address.GetFieldValue("holder", "Ljava/net/InetAddress$InetAddressHolder;").(*rtc.Obj)
	hostNameObj := holder.GetFieldValue("hostName", "Ljava/lang/String;").(*rtc.Obj)
	hostName := rtda.GoString(hostNameObj)
	laddr := fmt.Sprintf("%s:%d", hostName, port)
	listen, err := net.Listen("tcp", laddr)
	if err != nil {
		frame.Thread().ThrowIOException(err.Error())
	}
	this.SetExtra(listen)
}
Example #8
0
// private native Class<?> findBootstrapClass(String name);
// (Ljava/lang/String;)Ljava/lang/Class;
func findBootstrapClass(frame *rtda.Frame) {
	vars := frame.LocalVars()
	//this := vars.GetThis()
	name := vars.GetRef(1)

	className := rtc.DotToSlash(rtda.GoString(name))
	class := rtc.BootLoader().LoadClass(className)

	// todo: init class?
	stack := frame.OperandStack()
	stack.PushRef(class.JClass())

	// todo
	if r := recover(); r != nil {
		frame.OperandStack().PushRef(nil)
	}
}
Example #9
0
// private native Class<?> defineClass1(String name, byte[] b, int off, int len,
//                                      ProtectionDomain pd, String source);
// (Ljava/lang/String;[BIILjava/security/ProtectionDomain;Ljava/lang/String;)Ljava/lang/Class;
func defineClass1(frame *rtda.Frame) {
	vars := frame.LocalVars()
	this := vars.GetThis()
	name := vars.GetRef(1)
	byteArr := vars.GetRef(2)
	off := vars.GetInt(3)
	_len := vars.GetInt(4)
	// pd := vars.GetRef(5)
	// source := vars.GetRef(6)

	goBytes := byteArr.GoBytes()
	goBytes = goBytes[off : off+_len]

	println(this.Extra())
	panic(rtda.GoString(name))

}
Example #10
0
func _loadClass(frame *rtda.Frame) {
	vars := frame.LocalVars()
	//loader := vars.GetRef(0)
	nameObj := vars.GetRef(1)
	byteArr := vars.GetRef(2)
	off := vars.GetInt(3)
	_len := vars.GetInt(4)

	name := rtda.GoString(nameObj)
	name = rtc.DotToSlash(name)
	data := byteArr.GoBytes()
	data = data[off : off+_len]

	// todo
	class := frame.ClassLoader().DefineClass(name, data)
	stack := frame.OperandStack()
	stack.PushRef(class.JClass())
}
Example #11
0
// private native final Class<?> findLoadedClass0(String name);
// (Ljava/lang/String;)Ljava/lang/Class;
func findLoadedClass0(frame *rtda.Frame) {
	vars := frame.LocalVars()
	this := vars.GetThis()
	name := vars.GetRef(1)

	className := rtc.DotToSlash(rtda.GoString(name))

	if isAppClassLoader(this) {
		class := rtc.BootLoader().FindLoadedClass(className)
		if class != nil {
			frame.OperandStack().PushRef(class.JClass())
		} else {
			frame.OperandStack().PushRef(nil)
		}
		return
	}

	// todo
	frame.OperandStack().PushRef(nil)
}
Example #12
0
// private static native Class<?> forName0(String name, boolean initialize,
//                                         ClassLoader loader,
//                                         Class<?> caller)
//     throws ClassNotFoundException;
// (Ljava/lang/String;ZLjava/lang/ClassLoader;Ljava/lang/Class;)Ljava/lang/Class;
func forName0(frame *rtda.Frame) {
	vars := frame.LocalVars()
	jName := vars.GetRef(0)
	initialize := vars.GetBoolean(1)
	//jLoader := vars.GetRef(2)

	goName := rtda.GoString(jName)
	goName = jutil.ReplaceAll(goName, ".", "/")
	goClass := frame.ClassLoader().LoadClass(goName)
	jClass := goClass.JClass()

	if initialize && goClass.InitializationNotStarted() {
		// undo forName0
		thread := frame.Thread()
		frame.SetNextPC(thread.PC())
		// init class
		thread.InitClass(goClass)
	} else {
		stack := frame.OperandStack()
		stack.PushRef(jClass)
	}
}
Example #13
0
func _getPath(fileObj *rtc.Obj) string {
	pathStr := fileObj.GetFieldValue("path", "Ljava/lang/String;").(*rtc.Obj)
	return rtda.GoString(pathStr)
}