Skip to content

Commit

Permalink
JACOBIN-340 Finalizing logic for static initializer blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
platypusguy committed Aug 26, 2023
1 parent 8692c0f commit 45ab815
Showing 1 changed file with 43 additions and 36 deletions.
79 changes: 43 additions & 36 deletions src/jvm/instantiate.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ import (
"errors"
"fmt"
"jacobin/classloader"
"jacobin/frames"
"jacobin/globals"
"jacobin/thread"
"strconv"

// "jacobin/frames"
// "jacobin/globals"
"jacobin/log"
Expand Down Expand Up @@ -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 = "<clinit>"
// 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 = "<clinit>"
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
}

Expand Down

0 comments on commit 45ab815

Please sign in to comment.