Skip to content

Commit

Permalink
JACOBIN-299 Further standardization of codebase to use types.* values…
Browse files Browse the repository at this point in the history
… rather than raw strings
  • Loading branch information
platypusguy committed Aug 9, 2023
1 parent 96ca47c commit fd78ed0
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 53 deletions.
7 changes: 4 additions & 3 deletions src/classloader/classloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"jacobin/globals"
"jacobin/log"
"jacobin/shutdown"
"jacobin/types"
"jacobin/util"
"os"
"path/filepath"
Expand Down Expand Up @@ -721,12 +722,12 @@ func (cl *Classloader) GetCountOfLoadedClasses() int {
// and skips all array classes. For these latter cases or any errors, it returns ""
func normalizeClassReference(ref string) string {
refClassName := ref
if strings.HasPrefix(refClassName, "[L") {
refClassName = strings.TrimPrefix(refClassName, "[L")
if strings.HasPrefix(refClassName, types.RefArray) {
refClassName = strings.TrimPrefix(refClassName, types.RefArray)
if strings.HasSuffix(refClassName, ";") {
refClassName = strings.TrimSuffix(refClassName, ";")
}
} else if strings.HasPrefix(refClassName, "[") {
} else if strings.HasPrefix(refClassName, types.Array) {
refClassName = ""
}
return refClassName
Expand Down
3 changes: 2 additions & 1 deletion src/classloader/classloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"io"
"jacobin/globals"
"jacobin/log"
"jacobin/types"
"os"
"strings"
"sync"
Expand Down Expand Up @@ -120,7 +121,7 @@ func TestNormalizingClassReference(t *testing.T) {
t.Error("Unexpected normalized class reference: " + s)
}

s = normalizeClassReference("[B")
s = normalizeClassReference(types.ByteArray)
if s != "" {
t.Error("Unexpected normalized class reference: " + s)
}
Expand Down
3 changes: 2 additions & 1 deletion src/jvm/instantiate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"jacobin/classloader"
"jacobin/globals"
"jacobin/log"
"jacobin/types"
"testing"
)

Expand All @@ -21,7 +22,7 @@ func TestInstantiateArray(t *testing.T) {
_ = log.SetLogLevel(log.WARNING)
classloader.InitMethodArea()

obj, err := instantiateClass("[B")
obj, err := instantiateClass(types.ByteArray)
if err != nil {
t.Errorf("Got unexpected error from instantiating array: %s", err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion src/jvm/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func runFrame(fs *list.List) error {
} else {
obj := *(f.OpStack[f.TOS].(*object.Object))
if obj.Fields != nil && len(obj.Fields) > 0 {
if obj.Fields != nil && obj.Fields[0].Ftype == "[B" { // if it's a string, just show the string
if obj.Fields != nil && obj.Fields[0].Ftype == types.ByteArray { // if it's a string, just show the string
if obj.Fields[0].Fvalue == nil {
stackTop = fmt.Sprintf("[]byte: <nil>")
} else {
Expand Down
22 changes: 11 additions & 11 deletions src/object/String.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,38 +35,38 @@ func NewString() *Object {
// equivalent in go: runes.
array := make([]byte, 10)
s.Fields = append(s.Fields,
Field{Ftype: "[B", Fvalue: &array})
Field{Ftype: types.ByteArray, Fvalue: &array})

// field 01 -- coder LATIN(=bytes, for compact strings) is 0; UTF16 is 1
s.Fields = append(s.Fields, Field{Ftype: "B", Fvalue: int64(1)})
s.Fields = append(s.Fields, Field{Ftype: types.Byte, Fvalue: int64(1)})

// field 02 -- string hash
s.Fields = append(s.Fields, Field{Ftype: "I", Fvalue: int64(0)})
s.Fields = append(s.Fields, Field{Ftype: types.Int, Fvalue: int64(0)})

// field 03 -- COMPACT_STRINGS (always true for JDK >= 9)
s.Fields = append(s.Fields, Field{Ftype: "XZ", Fvalue: types.JavaBoolTrue})

// field 04 -- UTF_8.INSTANCE ptr to encoder
s.Fields = append(s.Fields, Field{Ftype: "L", Fvalue: nil})
s.Fields = append(s.Fields, Field{Ftype: types.Ref, Fvalue: nil})

// field 05 -- ISO_8859_1.INSTANCE ptr to encoder
s.Fields = append(s.Fields, Field{Ftype: "L", Fvalue: nil})
s.Fields = append(s.Fields, Field{Ftype: types.Ref, Fvalue: nil})

// field 06 -- sun/nio/cs/US_ASCII.INSTANCE
s.Fields = append(s.Fields, Field{Ftype: "L", Fvalue: nil})
s.Fields = append(s.Fields, Field{Ftype: types.Ref, Fvalue: nil})

// field 07 -- java/nio/charset/CodingErrorAction.REPLACE
s.Fields = append(s.Fields, Field{Ftype: "L", Fvalue: nil})
s.Fields = append(s.Fields, Field{Ftype: types.Ref, Fvalue: nil})

// field 08 -- java/lang/String.CASE_INSENSITIVE_ORDER
// points to a comparator. Will be useful to fill in later
s.Fields = append(s.Fields, Field{Ftype: "L", Fvalue: nil})
s.Fields = append(s.Fields, Field{Ftype: types.Ref, Fvalue: nil})

// field 09 -- hashIsZero (only true in rare case where hash is 0
s.Fields = append(s.Fields, Field{Ftype: "Z", Fvalue: types.JavaBoolFalse})
// field 09 -- hashIsZero (only true in rare case where hash is 0)
s.Fields = append(s.Fields, Field{Ftype: types.Bool, Fvalue: types.JavaBoolFalse})

// field 10 -- serialPersistentFields
s.Fields = append(s.Fields, Field{Ftype: "L", Fvalue: nil})
s.Fields = append(s.Fields, Field{Ftype: types.Ref, Fvalue: nil})

return s
}
Expand Down
2 changes: 1 addition & 1 deletion src/object/arrays.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func MakeArrayFromRawArray(rawArray interface{}) *Object {
raw := rawArray.(*[]uint8)
o := MakeEmptyObject()
o.Klass = nil
of := Field{Ftype: "[B", Fvalue: raw}
of := Field{Ftype: types.ByteArray, Fvalue: raw}
o.Fields = append(o.Fields, of)
return o
}
Expand Down
2 changes: 1 addition & 1 deletion src/types/javaTypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const Double = "D"
const Float = "F"
const Int = "I" // can be either 32- or 64-bit int
const Long = "J"
const Ref = "L"
const Short = "S"

const Array = "["
Expand All @@ -23,7 +24,6 @@ const IntArray = "[I"
const FloatArray = "[F"
const RefArray = "[L"
const RuneArray = "[R" // used only in strings that are not compact
const Ref = "Z"

// Jacobin-specific types
const String = "T"
Expand Down
69 changes: 35 additions & 34 deletions src/util/execUtilities_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,49 @@
package util

import (
"testing"
"jacobin/types"
"testing"
)

// verify that a trailing slash in JAVA_HOME is removed
func TestParseIncomingParamsFromMethType(t *testing.T) {
res := ParseIncomingParamsFromMethTypeString("(SBI)")
if len(res) != 3 { // short, byte and int all become 'I'
t.Errorf("Expected 3 parsed parameters, got %d", len(res))
}

if res[0] != "I" || res[1] != "I" || res[2] != "I" {
t.Errorf("Expected parse would return 3 values of 'I', got: %s%s%s",
res[0], res[1], res[2])
}

res = ParseIncomingParamsFromMethTypeString("(S[BI)I")
if len(res) != 3 { // short, byte and int all become 'I'
t.Errorf("Expected 3 parsed parameters, got %d", len(res))
}

if res[0] != "I" || res[1] != "[B" || res[2] != "I" {
t.Errorf("Expected parse would return S [B I, got: %s %s %s",
res[0], res[1], res[2])
}

res = ParseIncomingParamsFromMethTypeString("")
if len(res) != 0 {
t.Errorf("Expected parse would return value an empty string array, got: %s", res)
}
res := ParseIncomingParamsFromMethTypeString("(SBI)")
if len(res) != 3 { // short, byte and int all become 'I'
t.Errorf("Expected 3 parsed parameters, got %d", len(res))
}

if res[0] != types.Int || res[1] != types.Int || res[2] != types.Int {
t.Errorf("Expected parse would return 3 values of 'I', got: %s%s%s",
res[0], res[1], res[2])
}

res = ParseIncomingParamsFromMethTypeString("(S[BI)I")
if len(res) != 3 { // short, byte and int all become 'I' aka types.Int
t.Errorf("Expected 3 parsed parameters, got %d", len(res))
}

if res[0] != types.Int || res[1] != types.ByteArray || res[2] != types.Int {
t.Errorf("Expected parse would return S [B I, got: %s %s %s",
res[0], res[1], res[2])
}

res = ParseIncomingParamsFromMethTypeString("")
if len(res) != 0 {
t.Errorf("Expected parse would return value an empty string array, got: %s", res)
}
}

// test that pointer/refernce in the params is handled correctly
// especially, that references (start with L and with ;) are correctly
// parsed and represeneted in the output
func TestParseIncomingReferenceParamsFromMethType(t *testing.T) {
res := ParseIncomingParamsFromMethTypeString("(LString;Ljava/lang/Integer;JJ)")
if len(res) != 4 { // short, byte and int all become 'I'
t.Errorf("Expected 4 parsed parameters, got %d", len(res))
}

var params string = res[0] + res[1] + res[2] + res[3]
if params != "LLJJ" {
t.Errorf("Expected param string of 'LLJJ', got: %s", params)
}
res := ParseIncomingParamsFromMethTypeString("(LString;Ljava/lang/Integer;JJ)")
if len(res) != 4 { // short, byte and int all become 'I'
t.Errorf("Expected 4 parsed parameters, got %d", len(res))
}

var params string = res[0] + res[1] + res[2] + res[3]
if params != "LLJJ" {
t.Errorf("Expected param string of 'LLJJ', got: %s", params)
}
}

0 comments on commit fd78ed0

Please sign in to comment.