UndefinedArrayFilter.java revision 1040:cc3000241e57
1/*
2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package jdk.nashorn.internal.runtime.arrays;
27
28import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
29import java.lang.reflect.Array;
30import jdk.nashorn.internal.runtime.BitVector;
31import jdk.nashorn.internal.runtime.UnwarrantedOptimismException;
32
33/**
34 * This filter handles the presence of undefined array elements.
35 */
36final class UndefinedArrayFilter extends ArrayFilter {
37    /** Bit vector tracking undefines. */
38    private final BitVector undefined;
39
40    UndefinedArrayFilter(final ArrayData underlying) {
41        super(underlying);
42
43        this.undefined = new BitVector(underlying.length);
44    }
45
46    @Override
47    public ArrayData copy() {
48        final UndefinedArrayFilter copy = new UndefinedArrayFilter(underlying.copy());
49        copy.getUndefined().copy(undefined);
50        return copy;
51    }
52
53    @Override
54    public Object[] asObjectArray() {
55        final Object[] value = super.asObjectArray();
56
57        for (int i = 0; i < value.length; i++) {
58            if (undefined.isSet(i)) {
59                value[i] = UNDEFINED;
60            }
61        }
62
63        return value;
64    }
65
66    @Override
67    public Object asArrayOfType(final Class<?> componentType) {
68        final Object value = super.asArrayOfType(componentType);
69        final Object undefValue = convertUndefinedValue(componentType);
70        final int l = Array.getLength(value);
71        for (int i = 0; i < l; i++) {
72            if (undefined.isSet(i)) {
73                Array.set(value, i,undefValue);
74            }
75        }
76
77        return value;
78    }
79
80    @Override
81    public void shiftLeft(final int by) {
82        super.shiftLeft(by);
83        undefined.shiftLeft(by, length);
84    }
85
86    @Override
87    public ArrayData shiftRight(final int by) {
88        super.shiftRight(by);
89        undefined.shiftRight(by, length);
90
91        return this;
92    }
93
94    @Override
95    public ArrayData ensure(final long safeIndex) {
96        if (safeIndex >= SparseArrayData.MAX_DENSE_LENGTH && safeIndex >= length) {
97            return new SparseArrayData(this, safeIndex + 1);
98        }
99
100        super.ensure(safeIndex);
101        undefined.resize(length);
102
103        return this;
104    }
105
106    @Override
107    public ArrayData shrink(final long newLength) {
108        super.shrink(newLength);
109        undefined.resize(length);
110
111        return this;
112    }
113
114    @Override
115    public ArrayData set(final int index, final Object value, final boolean strict) {
116        undefined.clear(index);
117
118        if (value == UNDEFINED) {
119            undefined.set(index);
120            return this;
121        }
122
123        return super.set(index, value, strict);
124    }
125
126    @Override
127    public ArrayData set(final int index, final int value, final boolean strict) {
128        undefined.clear(index);
129
130        return super.set(index, value, strict);
131    }
132
133    @Override
134    public ArrayData set(final int index, final long value, final boolean strict) {
135        undefined.clear(index);
136
137        return super.set(index, value, strict);
138    }
139
140    @Override
141    public ArrayData set(final int index, final double value, final boolean strict) {
142        undefined.clear(index);
143
144        return super.set(index, value, strict);
145    }
146
147    @Override
148    public int getInt(final int index) {
149        if (undefined.isSet(index)) {
150            return 0;
151        }
152
153        return super.getInt(index);
154    }
155
156    @Override
157    public int getIntOptimistic(final int index, final int programPoint) {
158        if (undefined.isSet(index)) {
159            throw new UnwarrantedOptimismException(UNDEFINED, programPoint);
160        }
161
162        return super.getIntOptimistic(index, programPoint);
163    }
164
165    @Override
166    public long getLong(final int index) {
167        if (undefined.isSet(index)) {
168            return 0L;
169        }
170
171        return super.getLong(index);
172    }
173
174    @Override
175    public long getLongOptimistic(final int index, final int programPoint) {
176        if (undefined.isSet(index)) {
177            throw new UnwarrantedOptimismException(UNDEFINED, programPoint);
178        }
179
180        return super.getLongOptimistic(index, programPoint);
181    }
182
183    @Override
184    public double getDouble(final int index) {
185        if (undefined.isSet(index)) {
186            return Double.NaN;
187        }
188
189        return super.getDouble(index);
190    }
191
192    @Override
193    public double getDoubleOptimistic(final int index, final int programPoint) {
194        if (undefined.isSet(index)) {
195            throw new UnwarrantedOptimismException(UNDEFINED, programPoint);
196        }
197
198        return super.getDoubleOptimistic(index, programPoint);
199    }
200
201    @Override
202    public Object getObject(final int index) {
203        if (undefined.isSet(index)) {
204            return UNDEFINED;
205        }
206
207        return super.getObject(index);
208    }
209
210    @Override
211    public ArrayData delete(final int index) {
212        undefined.clear(index);
213
214        return super.delete(index);
215    }
216
217    @Override
218    public Object pop() {
219        final long index = length - 1;
220
221        if (super.has((int)index)) {
222            final boolean isUndefined = undefined.isSet(index);
223            final Object value = super.pop();
224
225            return isUndefined ? UNDEFINED : value;
226        }
227
228        return super.pop();
229    }
230
231    @Override
232    public ArrayData slice(final long from, final long to) {
233        final ArrayData newArray = underlying.slice(from, to);
234        final UndefinedArrayFilter newFilter = new UndefinedArrayFilter(newArray);
235        newFilter.getUndefined().copy(undefined);
236        newFilter.getUndefined().shiftLeft(from, newFilter.length);
237
238        return newFilter;
239    }
240
241    private BitVector getUndefined() {
242        return undefined;
243    }
244}
245