diff --git a/src/jvm/instantiate.go b/src/jvm/instantiate.go index c6bfc1ff..d0bb5b3a 100644 --- a/src/jvm/instantiate.go +++ b/src/jvm/instantiate.go @@ -10,6 +10,11 @@ import ( "errors" "fmt" "jacobin/classloader" + "jacobin/frames" + "jacobin/globals" + "jacobin/thread" + "strconv" + // "jacobin/frames" // "jacobin/globals" "jacobin/log" @@ -232,44 +237,46 @@ func runInitializationBlock(k *classloader.Klass, idx int) error { return nil } -func runJavaInitializer(meth classloader.MData, k *classloader.Klass) error { - // f := frames.CreateFrame(meth.MaxStack) // create a new frame - // f.MethName = "" - // f.ClName = className - // f.CP = m.Cp // add its pointer to the class CP - // for i := 0; i < len(m.Code); i++ { // copy the bytecodes over - // f.Meth = append(f.Meth, m.Code[i]) - // } - // - // // allocate the local variables - // for j := 0; j < m.MaxLocals; j++ { - // f.Locals = append(f.Locals, 0) - // } - // - // // create the first thread and place its first frame on it - // glob := globals.GetGlobalRef() - // clInitThread := thread.CreateThread() - // clInitThread.Stack = frames.CreateFrameStack() - // clInitThread.ID = thread.AddThreadToTable(&clInitThread, &glob.Threads) - // - // clInitThread.Trace = MainThread.Trace - // f.Thread = clInitThread.ID - // - // if frames.PushFrame(clInitThread.Stack, f) != nil { - // _ = log.Log("Memory exceptions allocating frame on thread: "+strconv.Itoa(clInitThread.ID), - // log.SEVERE) - // return errors.New("outOfMemory Exception") - // } - // - // if clInitThread.Trace { - // traceInfo := fmt.Sprintf("StartExec: f.MethName=%s, m.MaxStack=%d, m.MaxLocals=%d, len(m.Code)=%d", - // f.MethName, m.MaxStack, m.MaxLocals, len(m.Code)) - // _ = log.Log(traceInfo, log.TRACE_INST) - // } - // - // err = runThread(&clInitThread) +func runJavaInitializer(m classloader.MData, k *classloader.Klass) error { + meth := m.(classloader.JmEntry) + f := frames.CreateFrame(meth.MaxStack) // create a new frame + f.MethName = "" + f.ClName = k.Data.Name + f.CP = meth.Cp // add its pointer to the class CP + for i := 0; i < len(meth.Code); i++ { // copy the bytecodes over + f.Meth = append(f.Meth, meth.Code[i]) + } + + // allocate the local variables + for j := 0; j < meth.MaxLocals; j++ { + f.Locals = append(f.Locals, 0) + } + + // create the first thread and place its first frame on it + glob := globals.GetGlobalRef() + clInitThread := thread.CreateThread() + clInitThread.Stack = frames.CreateFrameStack() + clInitThread.ID = thread.AddThreadToTable(&clInitThread, &glob.Threads) + + clInitThread.Trace = MainThread.Trace + f.Thread = clInitThread.ID + + if frames.PushFrame(clInitThread.Stack, f) != nil { + _ = log.Log("Memory exceptions allocating frame on thread: "+strconv.Itoa(clInitThread.ID), + log.SEVERE) + return errors.New("outOfMemory Exception") + } + + if clInitThread.Trace { + traceInfo := fmt.Sprintf("StartExec: f.MethName=%s, m.MaxStack=%d, m.MaxLocals=%d, len(m.Code)=%d", + f.MethName, meth.MaxStack, meth.MaxLocals, len(meth.Code)) + _ = log.Log(traceInfo, log.TRACE_INST) + } + + // err := runThread(&clInitThread) // if err != nil { // return err + // } return nil }