Remove Assembly.
[SquirrelJME.git] / emulators / springcoat-vm / src / main / java / cc / squirreljme / vm / springcoat / SpringSimpleObject.java
blobf655862da74d9e415240ca4f8d8ebabed53d07fb
1 // -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // SquirrelJME
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the GNU General Public License v3+, or later.
7 // See license.mkd for licensing and copyright information.
8 // ---------------------------------------------------------------------------
10 package cc.squirreljme.vm.springcoat;
12 import cc.squirreljme.vm.springcoat.brackets.RefLinkHolder;
13 import cc.squirreljme.vm.springcoat.exceptions.SpringVirtualMachineException;
14 import java.lang.ref.Reference;
15 import java.lang.ref.WeakReference;
17 /**
18 * This is a representation of an object within the virtual machine.
20 * @since 2018/09/08
22 public final class SpringSimpleObject
23 implements SpringObject
25 /** The type of object this is. */
26 protected final SpringClass type;
28 /** The monitor for this object, every object has one. */
29 protected final SpringMonitor monitor =
30 new SpringMonitor();
32 /** The reference link holder. */
33 protected final RefLinkHolder refLink =
34 new RefLinkHolder();
36 /** Field storage in the class. */
37 final SpringFieldStorage[] _fields;
39 /** String representation. */
40 private Reference<String> _string;
42 /**
43 * Initializes the object.
45 * @param __cl The class of the object.
46 * @throws NullPointerException On null arguments.
47 * @since 2018/09/08
49 public SpringSimpleObject(SpringClass __cl)
50 throws NullPointerException
52 if (__cl == null)
53 throw new NullPointerException("NARG");
55 this.type = __cl;
57 // Setup field array
58 int n;
59 SpringFieldStorage[] fields;
60 this._fields = (fields =
61 new SpringFieldStorage[(n = __cl.instanceFieldCount())]);
63 // Initialize variable for all fields
64 int i = 0;
65 for (SpringField f : __cl.fieldTable())
67 int thisDx = i++;
68 fields[thisDx] = new SpringFieldStorage(f, f.index);
72 /**
73 * Returns the field by the given field.
75 * @param __f The field to get.
76 * @return The storage for the field.
77 * @throws NullPointerException On null arguments.
78 * @since 2018/11/19
80 public final SpringFieldStorage fieldByField(SpringField __f)
81 throws NullPointerException
83 if (__f == null)
84 throw new NullPointerException("NARG");
86 return this.fieldByIndex(__f.index());
89 /**
90 * Returns the field by the given index.
92 * @param __dx The field to get.
93 * @return The storage for the field by the specified index.
94 * @since 2018/09/16
96 public final SpringFieldStorage fieldByIndex(int __dx)
98 try
100 return this._fields[__dx];
102 catch (IndexOutOfBoundsException e)
104 throw new SpringVirtualMachineException(
105 "Invalid field index: " + __dx, e);
110 * Returns the field by the name and type.
112 * @param __static Is this field static?
113 * @param __name The name of the field.
114 * @param __type The type of the field.
115 * @return The storage for the field.
116 * @since 2020/06/17
118 public final SpringFieldStorage fieldByNameAndType(boolean __static,
119 String __name, String __type)
121 return this.fieldByField(
122 this.type().lookupField(__static, __name, __type));
126 * {@inheritDoc}
127 * @since 2018/09/15
129 @Override
130 public final SpringMonitor monitor()
132 return this.monitor;
136 * {@inheritDoc}
137 * @since 2020/05/31
139 @Override
140 public RefLinkHolder refLink()
142 return this.refLink;
146 * {@inheritDoc}
147 * @since 2018/09/15
149 @Override
150 public final String toString()
152 Reference<String> ref = this._string;
153 String rv;
155 if (ref == null || null == (rv = ref.get()))
156 this._string = new WeakReference<>((rv = String.format(
157 "%s@%08x", this.type.name(), System.identityHashCode(this))));
159 return rv;
163 * {@inheritDoc}
164 * @since 2018/09/09
166 @Override
167 public final SpringClass type()
169 return this.type;