src/jdk/nashorn/internal/objects/NativeUint8Array.java

Wed, 26 Jun 2013 15:40:52 +0200

author
hannesw
date
Wed, 26 Jun 2013 15:40:52 +0200
changeset 380
80c66d3fd872
parent 292
4d2eca4d4d66
child 405
b1980b5f00a1
permissions
-rw-r--r--

8019157: Avoid calling ScriptObject.setProto() if possible
Reviewed-by: jlaskey, sundar

     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  */
    26 package jdk.nashorn.internal.objects;
    28 import jdk.nashorn.internal.objects.annotations.Attribute;
    29 import jdk.nashorn.internal.objects.annotations.Constructor;
    30 import jdk.nashorn.internal.objects.annotations.Function;
    31 import jdk.nashorn.internal.objects.annotations.Property;
    32 import jdk.nashorn.internal.objects.annotations.ScriptClass;
    33 import jdk.nashorn.internal.objects.annotations.Where;
    34 import jdk.nashorn.internal.runtime.PropertyMap;
    35 import jdk.nashorn.internal.runtime.ScriptObject;
    36 import jdk.nashorn.internal.runtime.arrays.ArrayData;
    38 /**
    39  * Uint8 array for TypedArray extension
    40  */
    41 @ScriptClass("Uint8Array")
    42 public final class NativeUint8Array extends ArrayBufferView {
    43     /**
    44      * The size in bytes of each element in the array.
    45      */
    46     @Property(attributes = Attribute.NOT_ENUMERABLE | Attribute.NOT_WRITABLE | Attribute.NOT_CONFIGURABLE, where = Where.CONSTRUCTOR)
    47     public static final int BYTES_PER_ELEMENT = 1;
    49     // initialized by nasgen
    50     private static PropertyMap $nasgenmap$;
    52     private static final Factory FACTORY = new Factory(BYTES_PER_ELEMENT) {
    53         @Override
    54         public ArrayBufferView construct(final NativeArrayBuffer buffer, final int byteOffset, final int length) {
    55             return new NativeUint8Array(buffer, byteOffset, length);
    56         }
    57         @Override
    58         public ArrayData createArrayData(final NativeArrayBuffer buffer, final int byteOffset, final int length) {
    59             return new Uint8ArrayData(buffer, byteOffset, length);
    60         }
    61     };
    63     private static final class Uint8ArrayData extends ArrayDataImpl {
    64         private Uint8ArrayData(final NativeArrayBuffer buffer, final int byteOffset, final int elementLength) {
    65             super(buffer, byteOffset, elementLength);
    66         }
    68         @Override
    69         protected int byteIndex(final int index) {
    70             return index * BYTES_PER_ELEMENT + byteOffset;
    71         }
    73         @Override
    74         protected int getIntImpl(final int index) {
    75             return buffer.getByteArray()[byteIndex(index)] & 0xff;
    76         }
    78         @Override
    79         protected void setImpl(final int index, final int value) {
    80             buffer.getByteArray()[byteIndex(index)] = (byte)value;
    81         }
    82     }
    84     /**
    85      * Constructor
    86      *
    87      * @param newObj is this typed array instantiated with the new operator
    88      * @param self   self reference
    89      * @param args   args
    90      *
    91      * @return new typed array
    92      */
    93     @Constructor(arity = 1)
    94     public static Object constructor(final boolean newObj, final Object self, final Object... args) {
    95         return constructorImpl(args, FACTORY);
    96     }
    98     NativeUint8Array(final NativeArrayBuffer buffer, final int byteOffset, final int length) {
    99         super(buffer, byteOffset, length);
   100     }
   102     @Override
   103     protected Factory factory() {
   104         return FACTORY;
   105     }
   107     /**
   108      * Set values
   109      * @param self   self reference
   110      * @param array  multiple values of array's type to set
   111      * @param offset optional start index, interpreted  0 if undefined
   112      * @return undefined
   113      */
   114     @Function(attributes = Attribute.NOT_ENUMERABLE)
   115     protected static Object set(final Object self, final Object array, final Object offset) {
   116         return ArrayBufferView.setImpl(self, array, offset);
   117     }
   119     /**
   120      * Returns a new TypedArray view of the ArrayBuffer store for this TypedArray,
   121      * referencing the elements at begin, inclusive, up to end, exclusive. If either
   122      * begin or end is negative, it refers to an index from the end of the array,
   123      * as opposed to from the beginning.
   124      * <p>
   125      * If end is unspecified, the subarray contains all elements from begin to the end
   126      * of the TypedArray. The range specified by the begin and end values is clamped to
   127      * the valid index range for the current array. If the computed length of the new
   128      * TypedArray would be negative, it is clamped to zero.
   129      * <p>
   130      * The returned TypedArray will be of the same type as the array on which this
   131      * method is invoked.
   132      *
   133      * @param self self reference
   134      * @param begin begin position
   135      * @param end end position
   136      *
   137      * @return sub array
   138      */
   139     @Function(attributes = Attribute.NOT_ENUMERABLE)
   140     protected static Object subarray(final Object self, final Object begin, final Object end) {
   141         return ArrayBufferView.subarrayImpl(self, begin, end);
   142     }
   144     @Override
   145     protected ScriptObject getPrototype() {
   146         return Global.instance().getUint8ArrayPrototype();
   147     }
   148 }

mercurial