UndefinedArrayFilter.java revision 1844:9c7526916609
1261287Sdes/*
2261287Sdes * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
3261287Sdes * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4261287Sdes *
5261287Sdes * This code is free software; you can redistribute it and/or modify it
6261287Sdes * under the terms of the GNU General Public License version 2 only, as
7261287Sdes * published by the Free Software Foundation.  Oracle designates this
8261287Sdes * particular file as subject to the "Classpath" exception as provided
9261287Sdes * by Oracle in the LICENSE file that accompanied this code.
10261287Sdes *
11261287Sdes * This code is distributed in the hope that it will be useful, but WITHOUT
12261287Sdes * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13261287Sdes * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14261287Sdes * version 2 for more details (a copy is included in the LICENSE file that
15261287Sdes * accompanied this code).
16261287Sdes *
17261287Sdes * You should have received a copy of the GNU General Public License version
18261287Sdes * 2 along with this work; if not, write to the Free Software Foundation,
19261287Sdes * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20261287Sdes *
21261287Sdes * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22261287Sdes * or visit www.oracle.com if you need additional information or have any
23261287Sdes * questions.
24261287Sdes */
25261287Sdes
26261287Sdespackage jdk.nashorn.internal.runtime.arrays;
27261287Sdes
28261287Sdesimport static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
29261287Sdesimport java.lang.reflect.Array;
30261287Sdesimport jdk.nashorn.internal.runtime.BitVector;
31261287Sdesimport jdk.nashorn.internal.runtime.UnwarrantedOptimismException;
32261287Sdes
33261287Sdes/**
34261287Sdes * This filter handles the presence of undefined array elements.
35261287Sdes */
36261287Sdesfinal class UndefinedArrayFilter extends ArrayFilter {
37261287Sdes    /** Bit vector tracking undefined slots. */
38261287Sdes    private final BitVector undefined;
39261287Sdes
40261287Sdes    UndefinedArrayFilter(final ArrayData underlying) {
41261287Sdes        super(underlying);
42261287Sdes        this.undefined = new BitVector(underlying.length());
43261287Sdes    }
44261287Sdes
45261287Sdes    @Override
46261287Sdes    public ArrayData copy() {
47261287Sdes        final UndefinedArrayFilter copy = new UndefinedArrayFilter(underlying.copy());
48261287Sdes        copy.getUndefined().copy(undefined);
49261287Sdes        return copy;
50261287Sdes    }
51261287Sdes
52261287Sdes    @Override
53261287Sdes    public Object[] asObjectArray() {
54261287Sdes        final Object[] value = super.asObjectArray();
55261287Sdes
56261287Sdes        for (int i = 0; i < value.length; i++) {
57261287Sdes            if (undefined.isSet(i)) {
58261287Sdes                value[i] = UNDEFINED;
59261287Sdes            }
60261287Sdes        }
61261287Sdes
62261287Sdes        return value;
63261287Sdes    }
64261287Sdes
65261287Sdes    @Override
66261287Sdes    public Object asArrayOfType(final Class<?> componentType) {
67261287Sdes        final Object value = super.asArrayOfType(componentType);
68261287Sdes        final Object undefValue = convertUndefinedValue(componentType);
69261287Sdes        final int l = Array.getLength(value);
70261287Sdes        for (int i = 0; i < l; i++) {
71261287Sdes            if (undefined.isSet(i)) {
72261287Sdes                Array.set(value, i,undefValue);
73261287Sdes            }
74261287Sdes        }
75261287Sdes
76261287Sdes        return value;
77261287Sdes    }
78261287Sdes
79261287Sdes    @Override
80261287Sdes    public ArrayData shiftLeft(final int by) {
81261287Sdes        super.shiftLeft(by);
82261287Sdes        undefined.shiftLeft(by, length());
83261287Sdes        return this;
84261287Sdes    }
85261287Sdes
86261287Sdes    @Override
87261287Sdes    public ArrayData shiftRight(final int by) {
88261287Sdes        super.shiftRight(by);
89261287Sdes        undefined.shiftRight(by, length());
90261287Sdes        return this;
91261287Sdes    }
92261287Sdes
93261287Sdes    @Override
94261287Sdes    public ArrayData ensure(final long safeIndex) {
95261287Sdes        if (safeIndex >= SparseArrayData.MAX_DENSE_LENGTH && safeIndex >= length()) {
96261287Sdes            return new SparseArrayData(this, safeIndex + 1);
97261287Sdes        }
98261287Sdes
99261287Sdes        super.ensure(safeIndex);
100261287Sdes        undefined.resize(length());
101261287Sdes
102261287Sdes        return this;
103261287Sdes    }
104261287Sdes
105261287Sdes    @Override
106261287Sdes    public ArrayData shrink(final long newLength) {
107261287Sdes        super.shrink(newLength);
108261287Sdes        undefined.resize(length());
109261287Sdes        return this;
110261287Sdes    }
111261287Sdes
112261287Sdes    @Override
113261287Sdes    public ArrayData set(final int index, final Object value, final boolean strict) {
114261287Sdes        undefined.clear(index);
115261287Sdes
116261287Sdes        if (value == UNDEFINED) {
117261287Sdes            undefined.set(index);
118261287Sdes            return this;
119261287Sdes        }
120261287Sdes
121261287Sdes        return super.set(index, value, strict);
122261287Sdes    }
123261287Sdes
124261287Sdes    @Override
125261287Sdes    public ArrayData set(final int index, final int value, final boolean strict) {
126261287Sdes        undefined.clear(index);
127261287Sdes
128261287Sdes        return super.set(index, value, strict);
129261287Sdes    }
130261287Sdes
131261287Sdes    @Override
132261287Sdes    public ArrayData set(final int index, final double value, final boolean strict) {
133261287Sdes        undefined.clear(index);
134261287Sdes
135261287Sdes        return super.set(index, value, strict);
136261287Sdes    }
137261287Sdes
138261287Sdes    @Override
139261287Sdes    public int getInt(final int index) {
140261287Sdes        if (undefined.isSet(index)) {
141261287Sdes            return 0;
142261287Sdes        }
143261287Sdes
144261287Sdes        return super.getInt(index);
145261287Sdes    }
146261287Sdes
147261287Sdes    @Override
148261287Sdes    public int getIntOptimistic(final int index, final int programPoint) {
149261287Sdes        if (undefined.isSet(index)) {
150261287Sdes            throw new UnwarrantedOptimismException(UNDEFINED, programPoint);
151261287Sdes        }
152261287Sdes
153261287Sdes        return super.getIntOptimistic(index, programPoint);
154261287Sdes    }
155261287Sdes
156261287Sdes    @Override
157261287Sdes    public double getDouble(final int index) {
158261287Sdes        if (undefined.isSet(index)) {
159261287Sdes            return Double.NaN;
160261287Sdes        }
161261287Sdes
162261287Sdes        return super.getDouble(index);
163261287Sdes    }
164261287Sdes
165261287Sdes    @Override
166261287Sdes    public double getDoubleOptimistic(final int index, final int programPoint) {
167261287Sdes        if (undefined.isSet(index)) {
168261287Sdes            throw new UnwarrantedOptimismException(UNDEFINED, programPoint);
169261287Sdes        }
170261287Sdes
171261287Sdes        return super.getDoubleOptimistic(index, programPoint);
172261287Sdes    }
173261287Sdes
174261287Sdes    @Override
175261287Sdes    public Object getObject(final int index) {
176261287Sdes        if (undefined.isSet(index)) {
177261287Sdes            return UNDEFINED;
178261287Sdes        }
179261287Sdes
180261287Sdes        return super.getObject(index);
181261287Sdes    }
182261287Sdes
183261287Sdes    @Override
184261287Sdes    public ArrayData delete(final int index) {
185261287Sdes        undefined.clear(index);
186261287Sdes
187261287Sdes        return super.delete(index);
188261287Sdes    }
189261287Sdes
190261287Sdes    @Override
191261287Sdes    public Object pop() {
192261287Sdes        final long index = length() - 1;
193261287Sdes
194261287Sdes        if (super.has((int)index)) {
195261287Sdes            final boolean isUndefined = undefined.isSet(index);
196261287Sdes            final Object value = super.pop();
197261287Sdes
198261287Sdes            return isUndefined ? UNDEFINED : value;
199261287Sdes        }
200261287Sdes
201261287Sdes        return super.pop();
202261287Sdes    }
203261287Sdes
204261287Sdes    @Override
205261287Sdes    public ArrayData slice(final long from, final long to) {
206261287Sdes        final ArrayData newArray = underlying.slice(from, to);
207261287Sdes        final UndefinedArrayFilter newFilter = new UndefinedArrayFilter(newArray);
208261287Sdes        newFilter.getUndefined().copy(undefined);
209261287Sdes        newFilter.getUndefined().shiftLeft(from, newFilter.length());
210261287Sdes
211261287Sdes        return newFilter;
212261287Sdes    }
213261287Sdes
214261287Sdes    private BitVector getUndefined() {
215261287Sdes        return undefined;
216261287Sdes    }
217261287Sdes}
218261287Sdes