Skip to content

Commit

Permalink
JACOBIN-340 Added experimental 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 46560dd commit 33541fe
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
11 changes: 11 additions & 0 deletions src/classloader/javaLangSystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ func Load_Lang_System() map[string]GMeth {
GFunction: getProperty,
}

// MethodSignatures["java/lang/System.<clinit>()V"] = // TODO: need to replace eventually
// GMeth{
// ParamSlots: 0,
// GFunction: justReturn,
// }

return MethodSignatures
}

Expand Down Expand Up @@ -172,3 +178,8 @@ func getProperty(params []interface{}) interface{} {
obj := object.CreateCompactStringFromGoString(&value)
return obj
}

// do-nothing function
func justReturn([]interface{}) interface{} {
return nil
}
27 changes: 15 additions & 12 deletions src/jvm/instantiate.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ func instantiateClass(classname string) (*object.Object, error) {
obj.Fields = append(obj.Fields, *fieldToAdd)
} // loop through the fields if any
obj.FieldTable = nil
return &obj, nil
goto runInitializer
// return &obj, nil
} // end of handling fields for objects w/ no superclasses

obj.FieldTable = make(map[string]object.Field)
Expand Down Expand Up @@ -145,14 +146,15 @@ func instantiateClass(classname string) (*object.Object, error) {
} // end of handling fields for one class or superclass
} // end of handling fields for classes with superclasses other than Object

// run intialization blocks
for i := 0; i < len(k.Data.Methods); i++ {
meth := k.Data.Methods[i]
methName := k.Data.CP.Utf8Refs[meth.Name]
if strings.HasPrefix(methName, "<clinit>") {
runInitializationBlock(k, i)
}
}
runInitializer:
// // run intialization blocks
// for i := 0; i < len(k.Data.Methods); i++ {
// meth := k.Data.Methods[i]
// methName := k.Data.CP.Utf8Refs[meth.Name]
// if strings.HasPrefix(methName, "<clinit>") {
// runInitializationBlock(k, i)
// }
// }
return &obj, nil
}

Expand All @@ -166,16 +168,17 @@ func instantiateClass(classname string) (*object.Object, error) {
// CURR: Implement the above logic here.
func runInitializationBlock(k *classloader.Klass, idx int) error {

msg := fmt.Sprintf("<clinit> found in %s, method #%d\n", k.Data.Name, idx)
_ = log.Log(msg, log.FINE)
fmt.Print(msg)
// msg := fmt.Sprintf("<clinit> found in %s, method #%d\n", k.Data.Name, idx)
// _ = log.Log(msg, log.FINE)
// fmt.Print(msg)

className := k.Data.Name
me, err := classloader.FetchMethodAndCP(className, "<clinit>", "()V")
if err != nil {
return errors.New("Class not found: " + className + "<clinit>()")
}

// CURR: Don't assume it's a Java rather than native routine
m := me.Meth.(classloader.JmEntry)
f := frames.CreateFrame(m.MaxStack) // create a new frame
f.MethName = "<clinit>"
Expand Down

0 comments on commit 33541fe

Please sign in to comment.