forked from platypusguy/jacobin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavaLangBoolean.go
More file actions
422 lines (355 loc) · 12.3 KB
/
javaLangBoolean.go
File metadata and controls
422 lines (355 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
* Jacobin VM - A Java virtual machine
* Copyright (c) 2023 by the Jacobin authors. Consult jacobin.org.
* Licensed under Mozilla Public License 2.0 (MPL 2.0) All rights reserved.
*/
package javaLang
import (
"fmt"
"jacobin/src/classloader"
"jacobin/src/excNames"
"jacobin/src/gfunction/ghelpers"
"jacobin/src/globals"
"jacobin/src/object"
"jacobin/src/statics"
"jacobin/src/trace"
"jacobin/src/types"
"strings"
)
func Load_Lang_Boolean() {
ghelpers.MethodSignatures["java/lang/Boolean.<clinit>()V"] =
ghelpers.GMeth{
ParamSlots: 0,
GFunction: booleanClinit,
}
ghelpers.MethodSignatures["java/lang/Boolean.<init>(Z)V"] =
ghelpers.GMeth{
ParamSlots: 1,
GFunction: ghelpers.TrapDeprecated,
}
ghelpers.MethodSignatures["java/lang/Boolean.<init>(Ljava/lang/String;)V"] =
ghelpers.GMeth{
ParamSlots: 1,
GFunction: ghelpers.TrapDeprecated,
}
ghelpers.MethodSignatures["java/lang/Boolean.booleanValue()Z"] =
ghelpers.GMeth{
ParamSlots: 0,
GFunction: booleanBooleanValue,
}
ghelpers.MethodSignatures["java/lang/Boolean.compare(ZZ)I"] =
ghelpers.GMeth{
ParamSlots: 2,
GFunction: booleanCompare,
}
ghelpers.MethodSignatures["java/lang/Boolean.compareTo(Ljava/lang/Boolean;)I"] =
ghelpers.GMeth{
ParamSlots: 1,
GFunction: booleanCompareTo,
}
ghelpers.MethodSignatures["java/lang/Boolean.describeConstable()Ljava.util.Optional;"] =
ghelpers.GMeth{
ParamSlots: 0,
GFunction: ghelpers.TrapFunction,
}
ghelpers.MethodSignatures["java/lang/Boolean.equals(Ljava/lang/Object;)Z"] =
ghelpers.GMeth{
ParamSlots: 1,
GFunction: booleanEquals,
}
ghelpers.MethodSignatures["java/lang/Boolean.getBoolean(Ljava/lang/String;)Z"] =
ghelpers.GMeth{
ParamSlots: 1,
GFunction: booleanGetBoolean,
}
ghelpers.MethodSignatures["java/lang/Boolean.hashCode()I"] =
ghelpers.GMeth{
ParamSlots: 0,
GFunction: booleanHashCode,
}
ghelpers.MethodSignatures["java/lang/Boolean.hashCode(Z)I"] =
ghelpers.GMeth{
ParamSlots: 1,
GFunction: booleanHashCodeStatic,
}
ghelpers.MethodSignatures["java/lang/Boolean.logicalAnd(ZZ)Z"] =
ghelpers.GMeth{
ParamSlots: 2,
GFunction: booleanLogicalAnd,
}
ghelpers.MethodSignatures["java/lang/Boolean.logicalOr(ZZ)Z"] =
ghelpers.GMeth{
ParamSlots: 2,
GFunction: booleanLogicalOr,
}
ghelpers.MethodSignatures["java/lang/Boolean.logicalXor(ZZ)Z"] =
ghelpers.GMeth{
ParamSlots: 2,
GFunction: booleanLogicalXor,
}
ghelpers.MethodSignatures["java/lang/Boolean.parseBoolean(Ljava/lang/String;)Z"] =
ghelpers.GMeth{
ParamSlots: 1,
GFunction: booleanParseBoolean,
}
ghelpers.MethodSignatures["java/lang/Boolean.toString()Ljava/lang/String;"] =
ghelpers.GMeth{
ParamSlots: 0,
GFunction: booleanToString,
}
ghelpers.MethodSignatures["java/lang/Boolean.toString(Z)Ljava/lang/String;"] =
ghelpers.GMeth{
ParamSlots: 1,
GFunction: booleanToStringStatic,
}
ghelpers.MethodSignatures["java/lang/Boolean.valueOf(Z)Ljava/lang/Boolean;"] =
ghelpers.GMeth{
ParamSlots: 1,
GFunction: booleanValueOf,
}
ghelpers.MethodSignatures["java/lang/Boolean.valueOf(Ljava/lang/String;)Ljava/lang/Boolean;"] =
ghelpers.GMeth{
ParamSlots: 1,
GFunction: booleanValueOfString,
}
}
func booleanClinit(_ []interface{}) interface{} {
// Fetch the dummy "boolean" class from the Method Area
k := classloader.MethAreaFetch("boolean")
if k == nil || k.Data.ClassObject == nil {
// Fatal error: boot sequence failed
trace.Error("integerClinit: primitive 'boolean' class not found in MethArea")
return nil
}
// Set the static field Boolean.TYPE to this object
statics.AddStatic("java/lang/Boolean.TYPE", statics.Static{
Type: types.Ref,
Value: k.Data.ClassObject,
})
// Initialize static references for Boolean.TRUE and Boolean.FALSE
obj := object.MakeOneFieldObject(types.ClassNameBoolean, "value", types.Bool, types.JavaBoolFalse)
_ = statics.AddStatic("java/lang/Boolean.FALSE", statics.Static{Type: types.Ref, Value: obj})
obj = object.MakeOneFieldObject(types.ClassNameBoolean, "value", types.Bool, types.JavaBoolTrue)
_ = statics.AddStatic("java/lang/Boolean.TRUE", statics.Static{Type: types.Ref, Value: obj})
return nil
}
/*
// Create the primitive java/lang/Class instance for "boolean"
primClassJlc := classloader.MakeJlcEntry("boolean", true)
// Register it in the JLCmap so it can be found by name "boolean"
classloader.JlcMapLock.Lock()
classloader.JLCmap["boolean"] = primClassJlc
classloader.JlcMapLock.Unlock()
// Set the static field Boolean.TYPE to this object
_ = statics.AddStatic("java/lang/Boolean.TYPE", statics.Static{
Type: types.Ref,
Value: object.MakePrimitiveObjectFromJlcInstance("boolean"),
})
// Also update the Jlc entry for Boolean to include this static field in its Statics list
classloader.JlcMapLock.RLock()
booleanJlc, ok := classloader.JLCmap[className]
classloader.JlcMapLock.RUnlock()
if ok {
fieldName := "TYPE"
fieldDesc := types.Jlc
entry := fieldName + fieldDesc
booleanJlc.Lock.Lock()
if !slices.Contains(booleanJlc.Statics, entry) {
booleanJlc.Statics = append(booleanJlc.Statics, entry)
}
booleanJlc.Lock.Unlock()
} else {
if globals.TraceClass {
trace.Warning("booleanClinit: java/lang/Boolean not found in JLCmap")
}
}
return nil
*/
// Return the value of this Boolean object as a boolean primitive.
func booleanBooleanValue(params []interface{}) interface{} {
// Try for a Boolean object.
obj, ok := params[0].(*object.Object)
if !ok || obj == nil {
errMsg := fmt.Sprintf("booleanBooleanValue: Boolean parameter is neither nil or an invalid object: {%T, %v}",
params[0], params[0])
return ghelpers.GetGErrBlk(excNames.IllegalArgumentException, errMsg)
}
// Get the value field.
fld, ok := obj.FieldTable["value"]
if !ok {
errMsg := "booleanBooleanValue: Missing the \"value\" field"
return ghelpers.GetGErrBlk(excNames.IllegalArgumentException, errMsg)
}
var zz int64
switch fld.Fvalue.(type) {
case int64:
zz = fld.Fvalue.(int64)
default:
errMsg := fmt.Sprintf("booleanBooleanValue: The \"value\" field has the wrong type: %T", fld.Fvalue)
return ghelpers.GetGErrBlk(excNames.IllegalArgumentException, errMsg)
}
// inObj should be a String object.
switch zz {
case types.JavaBoolTrue, types.JavaBoolFalse:
return zz
}
// Return exception.
errMsg := fmt.Sprintf("booleanBooleanValue: The \"value\" field value is neither true nor false: %d", zz)
return ghelpers.GetGErrBlk(excNames.IllegalArgumentException, errMsg)
}
// Returns true if and only if the system property named by the argument exists
// and is equal to, ignoring case, the string "true".
// Else, return false.
func booleanGetBoolean(params []interface{}) interface{} {
// Get the property name and validate it.
obj, ok := params[0].(*object.Object)
if !ok || obj == nil || !object.IsStringObject(obj) {
return types.JavaBoolFalse
}
propName := object.GoStringFromStringObject(obj)
if propName == "" {
return types.JavaBoolFalse
}
// Call SystemGetProperty() to get the property value.
propValue := globals.GetSystemProperty(propName)
if propValue == "" {
return types.JavaBoolFalse
}
if strings.EqualFold(propValue, "true") {
return types.JavaBoolTrue
}
return types.JavaBoolFalse
}
// Returns a Boolean object instance, based on the parameter: boolean.
func booleanValueOf(params []interface{}) interface{} {
inBool, ok := params[0].(int64)
if !ok {
return ghelpers.GetGErrBlk(excNames.IllegalArgumentException, fmt.Sprintf("booleanValueOf: expected int64 (boolean), got %T", params[0]))
}
return object.MakePrimitiveObject("java/lang/Boolean", types.Bool, inBool)
}
// booleanValueOfString returns a Boolean object instance, based on the parameter: String.
func booleanValueOfString(params []interface{}) interface{} {
strObj, ok := params[0].(*object.Object)
if !ok || !object.IsStringObject(strObj) {
return ghelpers.GetGErrBlk(excNames.IllegalArgumentException, "booleanValueOfString: expected String object")
}
strValue := object.GoStringFromStringObject(strObj)
if strings.EqualFold(strValue, "true") {
return object.MakePrimitiveObject("java/lang/Boolean", types.Bool, types.JavaBoolTrue)
}
return object.MakePrimitiveObject("java/lang/Boolean", types.Bool, types.JavaBoolFalse)
}
// booleanParseBoolean parses the string argument as a boolean.
func booleanParseBoolean(params []interface{}) interface{} {
strObj, ok := params[0].(*object.Object)
if !ok || !object.IsStringObject(strObj) {
return types.JavaBoolFalse
}
strValue := object.GoStringFromStringObject(strObj)
if strings.EqualFold(strValue, "true") {
return types.JavaBoolTrue
}
return types.JavaBoolFalse
}
// booleanCompare compares two boolean values.
func booleanCompare(params []interface{}) interface{} {
x := params[0].(types.JavaBool)
y := params[1].(types.JavaBool)
if x == y {
return int64(0)
}
if x == types.JavaBoolTrue {
return int64(1)
}
return int64(-1)
}
// booleanCompareTo compares this Boolean instance with another.
func booleanCompareTo(params []interface{}) interface{} {
thisObj, ok1 := params[0].(*object.Object)
if !ok1 || object.IsNull(thisObj) {
return ghelpers.GetGErrBlk(excNames.NullPointerException, "booleanCompareTo: expected non-nil this-object")
}
otherObj, ok2 := params[1].(*object.Object)
if !ok2 || object.IsNull(otherObj) {
return ghelpers.GetGErrBlk(excNames.NullPointerException, "booleanCompareTo: expected non-nil that-object")
}
thisVal := thisObj.FieldTable["value"].Fvalue.(int64)
otherVal := otherObj.FieldTable["value"].Fvalue.(int64)
return booleanCompare([]interface{}{thisVal, otherVal})
}
// booleanEquals returns true if the specified object is a Boolean and has the same value.
func booleanEquals(params []interface{}) interface{} {
thisObj, ok1 := params[0].(*object.Object)
if !ok1 || object.IsNull(thisObj) {
return ghelpers.GetGErrBlk(excNames.NullPointerException, "booleanEquals: expected non-nil this-object")
}
otherObj, ok := params[1].(*object.Object)
if !ok || object.IsNull(otherObj) {
return types.JavaBoolFalse
}
if object.GoStringFromStringPoolIndex(otherObj.KlassName) != "java/lang/Boolean" {
return types.JavaBoolFalse
}
thisVal := thisObj.FieldTable["value"].Fvalue.(int64)
otherVal := otherObj.FieldTable["value"].Fvalue.(int64)
if thisVal == otherVal {
return types.JavaBoolTrue
}
return types.JavaBoolFalse
}
// booleanHashCode returns a hash code for this Boolean object.
func booleanHashCode(params []interface{}) interface{} {
obj := params[0].(*object.Object)
val := obj.FieldTable["value"].Fvalue.(int64)
return booleanHashCodeStatic([]interface{}{val})
}
// booleanHashCodeStatic returns a hash code for a boolean value.
func booleanHashCodeStatic(params []interface{}) interface{} {
val := params[0].(int64)
if val == types.JavaBoolTrue {
return int64(1231)
}
return int64(1237)
}
// booleanLogicalAnd returns the result of applying the logical AND operator to the specified boolean operands.
func booleanLogicalAnd(params []interface{}) interface{} {
a := params[0].(int64)
b := params[1].(int64)
if a == types.JavaBoolTrue && b == types.JavaBoolTrue {
return types.JavaBoolTrue
}
return types.JavaBoolFalse
}
// booleanLogicalOr returns the result of applying the logical OR operator to the specified boolean operands.
func booleanLogicalOr(params []interface{}) interface{} {
a := params[0].(int64)
b := params[1].(int64)
if a == types.JavaBoolTrue || b == types.JavaBoolTrue {
return types.JavaBoolTrue
}
return types.JavaBoolFalse
}
// booleanLogicalXor returns the result of applying the logical XOR operator to the specified boolean operands.
func booleanLogicalXor(params []interface{}) interface{} {
a := params[0].(int64)
b := params[1].(int64)
if a != b {
return types.JavaBoolTrue
}
return types.JavaBoolFalse
}
// booleanToString returns a String object representing this Boolean's value.
func booleanToString(params []interface{}) interface{} {
obj := params[0].(*object.Object)
val := obj.FieldTable["value"].Fvalue.(int64)
return booleanToStringStatic([]interface{}{val})
}
// booleanToStringStatic returns a String object representing the specified boolean.
func booleanToStringStatic(params []interface{}) interface{} {
val := params[0].(int64)
if val == types.JavaBoolTrue {
return object.StringObjectFromGoString("true")
}
return object.StringObjectFromGoString("false")
}