nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeUint8ClampedArray.java
author attila
Wed, 15 Oct 2014 15:57:46 +0200
changeset 27104 bc8ce3f84b84
parent 26768 751b0f427090
child 27209 30d8609b9561
permissions -rw-r--r--
8060242: Compile-time expression evaluator was not seeing into ArrayBufferViews Reviewed-by: hannesw, lagergren, sundar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     1
/*
16151
97c1e756ae1e 8005663: Update copyright year to 2013
jlaskey
parents: 16147
diff changeset
     2
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     4
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    10
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    15
 * accompanied this code).
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    16
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    20
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    23
 * questions.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    24
 */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    25
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    26
package jdk.nashorn.internal.objects;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    27
24769
attila
parents: 24720 23947
diff changeset
    28
import static jdk.nashorn.internal.codegen.CompilerConstants.specialCall;
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
    29
import static jdk.nashorn.internal.codegen.CompilerConstants.staticCall;
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
    30
import static jdk.nashorn.internal.lookup.Lookup.MH;
27104
bc8ce3f84b84 8060242: Compile-time expression evaluator was not seeing into ArrayBufferViews
attila
parents: 26768
diff changeset
    31
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
    32
import java.lang.invoke.MethodHandle;
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
    33
import java.lang.invoke.MethodHandles;
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
    34
import java.nio.ByteBuffer;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    35
import jdk.nashorn.internal.objects.annotations.Attribute;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    36
import jdk.nashorn.internal.objects.annotations.Constructor;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    37
import jdk.nashorn.internal.objects.annotations.Function;
17763
f30b7aa0e736 8014735: Typed Array, BYTES_PER_ELEMENT should be a class property
sundar
parents: 16226
diff changeset
    38
import jdk.nashorn.internal.objects.annotations.Property;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    39
import jdk.nashorn.internal.objects.annotations.ScriptClass;
17763
f30b7aa0e736 8014735: Typed Array, BYTES_PER_ELEMENT should be a class property
sundar
parents: 16226
diff changeset
    40
import jdk.nashorn.internal.objects.annotations.Where;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    41
import jdk.nashorn.internal.runtime.JSType;
18618
136279c4cbe6 8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents: 17771
diff changeset
    42
import jdk.nashorn.internal.runtime.PropertyMap;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    43
import jdk.nashorn.internal.runtime.ScriptObject;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    44
import jdk.nashorn.internal.runtime.arrays.ArrayData;
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
    45
import jdk.nashorn.internal.runtime.arrays.TypedArrayData;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    46
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    47
/**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    48
 * Uint8 clamped array for TypedArray extension
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    49
 */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    50
@ScriptClass("Uint8ClampedArray")
16226
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16151
diff changeset
    51
public final class NativeUint8ClampedArray extends ArrayBufferView {
17771
9fedae4933e4 8015354: JSON.parse should not use [[Put]] but use [[DefineOwnProperty]] instead
sundar
parents: 17763
diff changeset
    52
    /**
9fedae4933e4 8015354: JSON.parse should not use [[Put]] but use [[DefineOwnProperty]] instead
sundar
parents: 17763
diff changeset
    53
     * The size in bytes of each element in the array.
9fedae4933e4 8015354: JSON.parse should not use [[Put]] but use [[DefineOwnProperty]] instead
sundar
parents: 17763
diff changeset
    54
     */
17763
f30b7aa0e736 8014735: Typed Array, BYTES_PER_ELEMENT should be a class property
sundar
parents: 16226
diff changeset
    55
    @Property(attributes = Attribute.NOT_ENUMERABLE | Attribute.NOT_WRITABLE | Attribute.NOT_CONFIGURABLE, where = Where.CONSTRUCTOR)
f30b7aa0e736 8014735: Typed Array, BYTES_PER_ELEMENT should be a class property
sundar
parents: 16226
diff changeset
    56
    public static final int BYTES_PER_ELEMENT = 1;
f30b7aa0e736 8014735: Typed Array, BYTES_PER_ELEMENT should be a class property
sundar
parents: 16226
diff changeset
    57
18618
136279c4cbe6 8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents: 17771
diff changeset
    58
    // initialized by nasgen
18842
3c3be808b593 8019585: Sometimes a var declaration using itself in its init wasn't declared as canBeUndefined, causing erroneous bytecode
lagergren
parents: 18618
diff changeset
    59
    @SuppressWarnings("unused")
18618
136279c4cbe6 8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents: 17771
diff changeset
    60
    private static PropertyMap $nasgenmap$;
136279c4cbe6 8019157: Avoid calling ScriptObject.setProto() if possible
hannesw
parents: 17771
diff changeset
    61
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    62
    private static final Factory FACTORY = new Factory(BYTES_PER_ELEMENT) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    63
        @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    64
        public ArrayBufferView construct(final NativeArrayBuffer buffer, final int byteOffset, final int length) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    65
            return new NativeUint8ClampedArray(buffer, byteOffset, length);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    66
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    67
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    68
        @Override
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
    69
        public Uint8ClampedArrayData createArrayData(final ByteBuffer nb, final int start, final int end) {
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
    70
            return new Uint8ClampedArrayData(nb, start, end);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    71
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    72
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    73
        @Override
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
    74
        public String getClassName() {
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
    75
            return "Uint8ClampedArray";
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    76
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    77
    };
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    78
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
    79
    private static final class Uint8ClampedArrayData extends TypedArrayData<ByteBuffer> {
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
    80
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
    81
        private static final MethodHandle GET_ELEM = specialCall(MethodHandles.lookup(), Uint8ClampedArrayData.class, "getElem", int.class, int.class).methodHandle();
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
    82
        private static final MethodHandle SET_ELEM = specialCall(MethodHandles.lookup(), Uint8ClampedArrayData.class, "setElem", void.class, int.class, int.class).methodHandle();
25828
077046a5d726 8044786: Some tests fail with non-optimistic compilation
attila
parents: 24778
diff changeset
    83
        private static final MethodHandle RINT_D   = staticCall(MethodHandles.lookup(), Uint8ClampedArrayData.class, "rint", double.class, double.class).methodHandle();
077046a5d726 8044786: Some tests fail with non-optimistic compilation
attila
parents: 24778
diff changeset
    84
        private static final MethodHandle RINT_O   = staticCall(MethodHandles.lookup(), Uint8ClampedArrayData.class, "rint", Object.class, Object.class).methodHandle();
24769
attila
parents: 24720 23947
diff changeset
    85
        private static final MethodHandle CLAMP_LONG = staticCall(MethodHandles.lookup(), Uint8ClampedArrayData.class, "clampLong", long.class, long.class).methodHandle();
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
    86
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
    87
        private Uint8ClampedArrayData(final ByteBuffer nb, final int start, final int end) {
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
    88
            super(((ByteBuffer)nb.position(start).limit(end)).slice(), end - start);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    89
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    90
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    91
        @Override
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
    92
        protected MethodHandle getGetElem() {
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
    93
            return GET_ELEM;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    94
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    95
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    96
        @Override
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
    97
        protected MethodHandle getSetElem() {
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
    98
            return SET_ELEM;
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
    99
        }
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
   100
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   101
        @Override
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   102
        public Class<?> getElementType() {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   103
            return int.class;
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   104
        }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 25865
diff changeset
   105
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
   106
        private int getElem(final int index) {
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
   107
            try {
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
   108
                return nb.get(index) & 0xff;
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
   109
            } catch (final IndexOutOfBoundsException e) {
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
   110
                throw new ClassCastException(); //force relink - this works for unoptimistic too
23947
36ce4a262b4a 8030199: Nashorn: Uint8ClampedArray - Incorrect ToUint8Clamp implementation
hannesw
parents: 23763
diff changeset
   111
            }
36ce4a262b4a 8030199: Nashorn: Uint8ClampedArray - Incorrect ToUint8Clamp implementation
hannesw
parents: 23763
diff changeset
   112
        }
36ce4a262b4a 8030199: Nashorn: Uint8ClampedArray - Incorrect ToUint8Clamp implementation
hannesw
parents: 23763
diff changeset
   113
36ce4a262b4a 8030199: Nashorn: Uint8ClampedArray - Incorrect ToUint8Clamp implementation
hannesw
parents: 23763
diff changeset
   114
        @Override
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
   115
        public MethodHandle getElementSetter(final Class<?> elementType) {
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
   116
            final MethodHandle setter = super.getElementSetter(elementType); //getContinuousElementSetter(getClass(), setElem(), elementType);
24769
attila
parents: 24720 23947
diff changeset
   117
            if (setter != null) {
25828
077046a5d726 8044786: Some tests fail with non-optimistic compilation
attila
parents: 24778
diff changeset
   118
                if (elementType == Object.class) {
077046a5d726 8044786: Some tests fail with non-optimistic compilation
attila
parents: 24778
diff changeset
   119
                    return MH.filterArguments(setter, 2, RINT_O);
077046a5d726 8044786: Some tests fail with non-optimistic compilation
attila
parents: 24778
diff changeset
   120
                } else if (elementType == double.class) {
077046a5d726 8044786: Some tests fail with non-optimistic compilation
attila
parents: 24778
diff changeset
   121
                    return MH.filterArguments(setter, 2, RINT_D);
24769
attila
parents: 24720 23947
diff changeset
   122
                } else if (elementType == long.class) {
attila
parents: 24720 23947
diff changeset
   123
                    return MH.filterArguments(setter, 2, CLAMP_LONG);
attila
parents: 24720 23947
diff changeset
   124
                }
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
   125
            }
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
   126
            return setter;
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
   127
        }
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
   128
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
   129
        private void setElem(final int index, final int elem) {
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
   130
            try {
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
   131
                final byte clamped;
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
   132
                if ((elem & 0xffff_ff00) == 0) {
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
   133
                    clamped = (byte)elem;
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
   134
                } else {
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
   135
                    clamped = elem < 0 ? 0 : (byte)0xff;
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
   136
                }
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
   137
                nb.put(index, clamped);
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
   138
            } catch (final IndexOutOfBoundsException e) {
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
   139
                //swallow valid array indexes. it's ok.
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
   140
                if (index < 0) {
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
   141
                    throw new ClassCastException();
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
   142
                }
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
   143
            }
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
   144
        }
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
   145
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
   146
        @Override
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
   147
        public boolean isClamped() {
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
   148
            return true;
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
   149
        }
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
   150
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
   151
        @Override
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
   152
        public boolean isUnsigned() {
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
   153
            return true;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   154
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   155
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   156
        @Override
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24769
diff changeset
   157
        public int getInt(final int index) {
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
   158
            return getElem(index);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   159
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   160
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   161
        @Override
27104
bc8ce3f84b84 8060242: Compile-time expression evaluator was not seeing into ArrayBufferViews
attila
parents: 26768
diff changeset
   162
        public int getIntOptimistic(final int index, final int programPoint) {
bc8ce3f84b84 8060242: Compile-time expression evaluator was not seeing into ArrayBufferViews
attila
parents: 26768
diff changeset
   163
            return getElem(index);
bc8ce3f84b84 8060242: Compile-time expression evaluator was not seeing into ArrayBufferViews
attila
parents: 26768
diff changeset
   164
        }
bc8ce3f84b84 8060242: Compile-time expression evaluator was not seeing into ArrayBufferViews
attila
parents: 26768
diff changeset
   165
bc8ce3f84b84 8060242: Compile-time expression evaluator was not seeing into ArrayBufferViews
attila
parents: 26768
diff changeset
   166
        @Override
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24769
diff changeset
   167
        public long getLong(final int index) {
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
   168
            return getInt(index);
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
   169
        }
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
   170
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
   171
        @Override
27104
bc8ce3f84b84 8060242: Compile-time expression evaluator was not seeing into ArrayBufferViews
attila
parents: 26768
diff changeset
   172
        public long getLongOptimistic(final int index, final int programPoint) {
bc8ce3f84b84 8060242: Compile-time expression evaluator was not seeing into ArrayBufferViews
attila
parents: 26768
diff changeset
   173
            return getElem(index);
bc8ce3f84b84 8060242: Compile-time expression evaluator was not seeing into ArrayBufferViews
attila
parents: 26768
diff changeset
   174
        }
bc8ce3f84b84 8060242: Compile-time expression evaluator was not seeing into ArrayBufferViews
attila
parents: 26768
diff changeset
   175
bc8ce3f84b84 8060242: Compile-time expression evaluator was not seeing into ArrayBufferViews
attila
parents: 26768
diff changeset
   176
        @Override
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24769
diff changeset
   177
        public double getDouble(final int index) {
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
   178
            return getInt(index);
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
   179
        }
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
   180
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
   181
        @Override
27104
bc8ce3f84b84 8060242: Compile-time expression evaluator was not seeing into ArrayBufferViews
attila
parents: 26768
diff changeset
   182
        public double getDoubleOptimistic(final int index, final int programPoint) {
bc8ce3f84b84 8060242: Compile-time expression evaluator was not seeing into ArrayBufferViews
attila
parents: 26768
diff changeset
   183
            return getElem(index);
bc8ce3f84b84 8060242: Compile-time expression evaluator was not seeing into ArrayBufferViews
attila
parents: 26768
diff changeset
   184
        }
bc8ce3f84b84 8060242: Compile-time expression evaluator was not seeing into ArrayBufferViews
attila
parents: 26768
diff changeset
   185
bc8ce3f84b84 8060242: Compile-time expression evaluator was not seeing into ArrayBufferViews
attila
parents: 26768
diff changeset
   186
        @Override
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24769
diff changeset
   187
        public Object getObject(final int index) {
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
   188
            return getInt(index);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   189
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   190
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   191
        @Override
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24769
diff changeset
   192
        public ArrayData set(final int index, final Object value, final boolean strict) {
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
   193
            return set(index, JSType.toNumber(value), strict);
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
   194
        }
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
   195
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
   196
        @Override
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24769
diff changeset
   197
        public ArrayData set(final int index, final int value, final boolean strict) {
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
   198
            setElem(index, value);
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
   199
            return this;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   200
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   201
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   202
        @Override
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24769
diff changeset
   203
        public ArrayData set(final int index, final long value, final boolean strict) {
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
   204
            return set(index, (int)value, strict);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   205
        }
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
   206
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
   207
        @Override
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24769
diff changeset
   208
        public ArrayData set(final int index, final double value, final boolean strict) {
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
   209
            return set(index, rint(value), strict);
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
   210
        }
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
   211
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24769
diff changeset
   212
        private static double rint(final double rint) {
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
   213
            return (int)Math.rint(rint);
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
   214
        }
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 19638
diff changeset
   215
24769
attila
parents: 24720 23947
diff changeset
   216
        @SuppressWarnings("unused")
25828
077046a5d726 8044786: Some tests fail with non-optimistic compilation
attila
parents: 24778
diff changeset
   217
        private static Object rint(final Object rint) {
077046a5d726 8044786: Some tests fail with non-optimistic compilation
attila
parents: 24778
diff changeset
   218
            return rint(JSType.toNumber(rint));
077046a5d726 8044786: Some tests fail with non-optimistic compilation
attila
parents: 24778
diff changeset
   219
        }
077046a5d726 8044786: Some tests fail with non-optimistic compilation
attila
parents: 24778
diff changeset
   220
077046a5d726 8044786: Some tests fail with non-optimistic compilation
attila
parents: 24778
diff changeset
   221
        @SuppressWarnings("unused")
24769
attila
parents: 24720 23947
diff changeset
   222
        private static long clampLong(final long l) {
attila
parents: 24720 23947
diff changeset
   223
            if(l < 0L) {
attila
parents: 24720 23947
diff changeset
   224
                return 0L;
attila
parents: 24720 23947
diff changeset
   225
            } else if(l > 0xffL) {
attila
parents: 24720 23947
diff changeset
   226
                return 0xffL;
attila
parents: 24720 23947
diff changeset
   227
            }
attila
parents: 24720 23947
diff changeset
   228
            return l;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   229
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   230
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   231
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   232
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   233
     * Constructor
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   234
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   235
     * @param newObj is this typed array instantiated with the new operator
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   236
     * @param self   self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   237
     * @param args   args
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   238
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   239
     * @return new typed array
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   240
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   241
    @Constructor(arity = 1)
23763
950d8bc0554f 8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents: 19638
diff changeset
   242
    public static NativeUint8ClampedArray constructor(final boolean newObj, final Object self, final Object... args) {
24769
attila
parents: 24720 23947
diff changeset
   243
        return (NativeUint8ClampedArray)constructorImpl(newObj, args, FACTORY);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   244
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   245
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   246
    NativeUint8ClampedArray(final NativeArrayBuffer buffer, final int byteOffset, final int length) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   247
        super(buffer, byteOffset, length);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   248
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   249
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   250
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   251
    protected Factory factory() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   252
        return FACTORY;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   253
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   254
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   255
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   256
     * Set values
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   257
     * @param self   self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   258
     * @param array  multiple values of array's type to set
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   259
     * @param offset optional start index, interpreted  0 if undefined
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   260
     * @return undefined
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   261
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   262
    @Function(attributes = Attribute.NOT_ENUMERABLE)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   263
    protected static Object set(final Object self, final Object array, final Object offset) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   264
        return ArrayBufferView.setImpl(self, array, offset);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   265
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   266
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   267
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   268
     * Returns a new TypedArray view of the ArrayBuffer store for this TypedArray,
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   269
     * referencing the elements at begin, inclusive, up to end, exclusive. If either
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   270
     * begin or end is negative, it refers to an index from the end of the array,
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   271
     * as opposed to from the beginning.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   272
     * <p>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   273
     * If end is unspecified, the subarray contains all elements from begin to the end
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   274
     * of the TypedArray. The range specified by the begin and end values is clamped to
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   275
     * the valid index range for the current array. If the computed length of the new
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   276
     * TypedArray would be negative, it is clamped to zero.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   277
     * <p>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   278
     * The returned TypedArray will be of the same type as the array on which this
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   279
     * method is invoked.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   280
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   281
     * @param self self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   282
     * @param begin begin position
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   283
     * @param end end position
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   284
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   285
     * @return sub array
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   286
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   287
    @Function(attributes = Attribute.NOT_ENUMERABLE)
23763
950d8bc0554f 8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents: 19638
diff changeset
   288
    protected static NativeUint8ClampedArray subarray(final Object self, final Object begin, final Object end) {
950d8bc0554f 8038456: improve nasgen type checks and use specific return type for @Function, @SpecializedFunctio methods
sundar
parents: 19638
diff changeset
   289
        return (NativeUint8ClampedArray)ArrayBufferView.subarrayImpl(self, begin, end);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   290
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   291
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   292
    @Override
18851
bdb92c95f886 8019947: inherited property invalidation does not work with two globals in same context
sundar
parents: 18842
diff changeset
   293
    protected ScriptObject getPrototype(final Global global) {
bdb92c95f886 8019947: inherited property invalidation does not work with two globals in same context
sundar
parents: 18842
diff changeset
   294
        return global.getUint8ClampedArrayPrototype();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   295
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   296
}