Skip to content

Commit

Permalink
JACOBIN-340 Filled in logic for native method for Class.getPrimitiveC…
Browse files Browse the repository at this point in the history
…lass()
  • Loading branch information
platypusguy committed Aug 23, 2023
1 parent b08ee79 commit 62b6c47
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
57 changes: 52 additions & 5 deletions src/classloader/javalangClass.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,59 @@

package classloader

// Implementation of some of the functions in in Java/lang/Class. Starting with getPrimitiveInsance()
import (
"errors"
"jacobin/log"
"jacobin/shutdown"
)

func getPrimitiveClass(primitive string) Klass {
// Implementation of some of the functions in in Java/lang/Class.

func Load_Lang_Class() map[string]GMeth {

MethodSignatures["java/lang/Class.getPrimitiveClass(Ljava/lang/String;)Ljava/lang/Class;"] =
GMeth{
ParamSlots: 1,
GFunction: getPrimitiveClass,
}

return MethodSignatures
}

// getPrimitiveClass() takes a one-word descriptor of a primitive and
// returns the native primitive class that corresponds to it. Not
// quite sure how this class is used, but it needs to implemented.
func getPrimitiveClass(params []interface{}) interface{} {
primitive := params[0].(string)
if primitive == "int" {
// do somethng
return Klass{}
k, err := simpleClassLoadByName("java/lang/Integer")
if err != nil {
return errors.New("getPrimitiveClass() could not load java/lang/Integer")
} else {
return k
}
}
return errors.New("getPrimitiveClass() reached unreadhable code")
}

// simpleClassLoadByName() just checks the MethodArea cache for the loaded
// class, and if it's not there, it loads it and returns a pointer to it.
// Logic basically duplicates similar functionality in instantiate.go
func simpleClassLoadByName(className string) (*Klass, error) {
alreadyLoaded := MethAreaFetch(className)
if alreadyLoaded != nil { // if the class is already loaded, skip the rest of this
return alreadyLoaded, nil
}

// If not, try to load class by name
err := LoadClassFromNameOnly(className)
if err != nil {
errMsg := "instantiateClass()-getPrimitivelass(): Failed to load class " + className
_ = log.Log(errMsg, log.SEVERE)
_ = log.Log(err.Error(), log.SEVERE)
shutdown.Exit(shutdown.APP_EXCEPTION)
return nil, errors.New(errMsg) // needed for testing, which does not shutdown on failure
} else {
return MethAreaFetch(className), nil
}
return Klass{} // should be a void class or the like.
}
1 change: 1 addition & 0 deletions src/classloader/mTable.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ var MTmutex sync.Mutex
// they make available.
func MTableLoadNatives() {
loadlib(&MTable, Load_Io_PrintStream()) // load the java.io.prinstream golang functions
loadlib(&MTable, Load_Lang_Class()) // load the java.lang.Class golang functions
loadlib(&MTable, Load_Lang_System()) // load the java.lang.system golang functions
loadlib(&MTable, Load_Lang_Math()) // load the java.lang.system golang functions
}
Expand Down

0 comments on commit 62b6c47

Please sign in to comment.