Example #1
0
func mainThreadNotReady(thread *rtda.Thread) bool {
	stack := thread.CurrentFrame().OperandStack()
	if _mainThreadGroup == nil {
		undoExec(thread)
		threadGroupClass := _classLoader.LoadClass("java/lang/ThreadGroup")
		_mainThreadGroup = threadGroupClass.NewObj()
		initMethod := threadGroupClass.GetConstructor("()V")
		stack.PushRef(_mainThreadGroup) // this
		thread.InvokeMethod(initMethod)
		return true
	}
	if thread.JThread() == nil {
		undoExec(thread)
		threadClass := _classLoader.LoadClass("java/lang/Thread")
		mainThreadObj := threadClass.NewObjWithExtra(thread)
		mainThreadObj.SetFieldValue("priority", "I", int32(1))
		thread.HackSetJThread(mainThreadObj)

		initMethod := threadClass.GetConstructor("(Ljava/lang/ThreadGroup;Ljava/lang/String;)V")
		stack.PushRef(mainThreadObj)        // this
		stack.PushRef(_mainThreadGroup)     // group
		stack.PushRef(rtda.JString("main")) // name
		thread.InvokeMethod(initMethod)
		return true
	}
	return false
}
Example #2
0
func mainClassNotReady(thread *rtda.Thread) bool {
	mainClass := _classLoader.LoadClass(_mainClassName)
	if mainClass.InitializationNotStarted() {
		undoExec(thread)
		thread.InitClass(mainClass)
		return true
	}
	return false
}
Example #3
0
func _logFrames(thread *rtda.Thread) {
	for !thread.IsStackEmpty() {
		frame := thread.PopFrame()
		method := frame.Method()
		className := method.Class().Name()
		lineNum := method.GetLineNumber(frame.NextPC())
		fmt.Printf(">> line:%4d pc:%4d %v.%v%v \n",
			lineNum, frame.NextPC(), className, method.Name(), method.Descriptor())
	}
}
Example #4
0
func bootClassesNotReady(thread *rtda.Thread) bool {
	for _, className := range _bootClasses {
		class := _classLoader.LoadClass(className)
		if class.InitializationNotStarted() {
			undoExec(thread)
			thread.InitClass(class)
			return true
		}
	}
	return false
}
Example #5
0
func jlSystemNotReady(thread *rtda.Thread) bool {
	sysClass := _classLoader.LoadClass("java/lang/System")
	propsField := sysClass.GetStaticField("props", "Ljava/util/Properties;")
	props := propsField.GetStaticValue()
	if props == nil {
		undoExec(thread)
		initSys := sysClass.GetStaticMethod("initializeSystemClass", "()V")
		thread.InvokeMethod(initSys)
		return true
	}
	return false
}
Example #6
0
// todo
func Loop(thread *rtda.Thread) {
	threadObj := thread.JThread()
	isDaemon := threadObj != nil && threadObj.GetFieldValue("daemon", "Z").(int32) == 1
	if !isDaemon {
		nonDaemonThreadStart()
	}

	_loop(thread)

	// terminate thread
	threadObj = thread.JThread()
	threadObj.Monitor().NotifyAll()
	if !isDaemon {
		nonDaemonThreadStop()
	}
}
Example #7
0
// todo
func _catchErr(thread *rtda.Thread) {
	if r := recover(); r != nil {
		if err, ok := r.(jerrors.ClassNotFoundError); ok {
			thread.ThrowClassNotFoundException(err.Error())
			_loop(thread)
			return
		}

		_logFrames(thread)

		err, ok := r.(error)
		if !ok {
			err = fmt.Errorf("%v", r)
			panic(err.Error())
		} else {
			panic(err.Error())
		}
	}
}
Example #8
0
func _loop(thread *rtda.Thread) {
	defer _catchErr(thread) // todo

	for {
		frame := thread.CurrentFrame()
		pc := frame.NextPC()
		thread.SetPC(pc)

		// fetch instruction
		method := frame.Method()
		if method.Instructions == nil {
			method.Instructions = decodeMethod(method.Code())
		}
		insts := method.Instructions.([]base.Instruction)
		inst := insts[pc]
		instCount := len(insts)

		// update nextPC
		for {
			pc++
			if pc >= instCount || insts[pc] != nil {
				break
			}
		}
		frame.SetNextPC(pc)

		// execute instruction
		//_logInstruction(frame, inst)
		inst.Execute(frame)
		if thread.IsStackEmpty() {
			break
		}
	}
}
Example #9
0
func execMain(thread *rtda.Thread) {
	thread.PopFrame()
	mainClass := _classLoader.LoadClass(_mainClassName)
	mainMethod := mainClass.GetMainMethod()
	if mainMethod != nil {
		newFrame := thread.NewFrame(mainMethod)
		thread.PushFrame(newFrame)
		args := createArgs()
		newFrame.LocalVars().SetRef(0, args)
	} else {
		panic("no main method!") // todo
	}
}
Example #10
0
// prepare to reexec this instruction
func undoExec(thread *rtda.Thread) {
	thread.CurrentFrame().RevertNextPC()
}