Mercurial > hg > truffle
changeset 22435:dc2bfc816011
Documenting and testing behavior of FrameDescriptor#shallowCopy
author | Jaroslav Tulach <jaroslav.tulach@oracle.com> |
---|---|
date | Thu, 03 Dec 2015 19:48:03 +0100 |
parents | c11ce7d2e2c3 |
children | 0ca502f898ec |
files | truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/FrameDescriptorTest.java truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/frame/FrameDescriptor.java |
diffstat | 2 files changed, 34 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/FrameDescriptorTest.java Thu Dec 03 19:37:45 2015 +0100 +++ b/truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/FrameDescriptorTest.java Thu Dec 03 19:48:03 2015 +0100 @@ -88,4 +88,30 @@ assertNull("Info isn't copied!", copy.getSlots().get(1).getInfo()); assertEquals("Kind isn't copied!", copy.getSlots().get(1).getKind(), FrameSlotKind.Illegal); } + + @Test + public void shallowCopy() { + Object defaultValue = "default"; + FrameDescriptor d = new FrameDescriptor(defaultValue); + s1 = d.addFrameSlot("v1", "i1", FrameSlotKind.Boolean); + s2 = d.addFrameSlot("v2", "i2", FrameSlotKind.Float); + + assertEquals(2, d.getSize()); + final FrameSlot first = d.getSlots().get(1); + assertEquals(first.getInfo(), "i2"); + assertEquals(first.getKind(), FrameSlotKind.Float); + assertEquals(first.getIndex(), 1); + + FrameDescriptor copy = d.shallowCopy(); + + assertEquals(2, copy.getSize()); + final FrameSlot firstCopy = copy.getSlots().get(1); + assertEquals("Info is copied", firstCopy.getInfo(), "i2"); + assertEquals("Kind is copied", firstCopy.getKind(), FrameSlotKind.Float); + assertEquals(firstCopy.getIndex(), 1); + + firstCopy.setKind(FrameSlotKind.Int); + assertEquals("Kind is changed", firstCopy.getKind(), FrameSlotKind.Int); + assertEquals("Kind is changed in original too!", first.getKind(), FrameSlotKind.Int); + } }
--- a/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/frame/FrameDescriptor.java Thu Dec 03 19:37:45 2015 +0100 +++ b/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/frame/FrameDescriptor.java Thu Dec 03 19:48:03 2015 +0100 @@ -237,7 +237,7 @@ * Deeper copy of the descriptor. Copies all slots in the descriptor, but only their identifiers * - not their {@link FrameSlot#getInfo()} neither their {@link FrameSlot#getKind()}! * - * @return new instance of a descritor with copies of values from this one + * @return new instance of a descriptor with copies of values from this one */ public FrameDescriptor copy() { FrameDescriptor clonedFrameDescriptor = new FrameDescriptor(this.defaultValue); @@ -248,6 +248,13 @@ return clonedFrameDescriptor; } + /** + * Shallow copy of the descriptor. Re-uses the existing slots in new descriptor. As a result, if + * you {@link FrameSlot#setKind(com.oracle.truffle.api.frame.FrameSlotKind) change kind} of one + * of the slots it is changed in the original as well as in the shallow copy. + * + * @return new instance of a descriptor with copies of values from this one + */ public FrameDescriptor shallowCopy() { FrameDescriptor clonedFrameDescriptor = new FrameDescriptor(this.defaultValue); clonedFrameDescriptor.slots.addAll(slots);