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

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

author
hannesw
date
Wed, 26 Jun 2013 15:40:52 +0200
changeset 380
80c66d3fd872
parent 82
abea4ba28901
child 414
ec84ba68ad39
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 java.util.Arrays;
    29 import jdk.nashorn.internal.objects.annotations.Attribute;
    30 import jdk.nashorn.internal.objects.annotations.Constructor;
    31 import jdk.nashorn.internal.objects.annotations.Function;
    32 import jdk.nashorn.internal.objects.annotations.Getter;
    33 import jdk.nashorn.internal.objects.annotations.ScriptClass;
    34 import jdk.nashorn.internal.runtime.JSType;
    35 import jdk.nashorn.internal.runtime.PropertyMap;
    36 import jdk.nashorn.internal.runtime.ScriptObject;
    37 import jdk.nashorn.internal.runtime.ScriptRuntime;
    39 @ScriptClass("ArrayBuffer")
    40 final class NativeArrayBuffer extends ScriptObject {
    41     private final byte[] buffer;
    43     // initialized by nasgen
    44     private static PropertyMap $nasgenmap$;
    46     @Constructor(arity = 1)
    47     public static Object constructor(final boolean newObj, final Object self, final Object... args) {
    48         if (args.length == 0) {
    49             throw new RuntimeException("missing length argument");
    50         }
    52         return new NativeArrayBuffer(JSType.toInt32(args[0]));
    53     }
    55     protected NativeArrayBuffer(final byte[] byteArray) {
    56         super(Global.instance().getArrayBufferPrototype(), $nasgenmap$);
    57         this.buffer = byteArray;
    58     }
    60     protected NativeArrayBuffer(final int byteLength) {
    61         this(new byte[byteLength]);
    62     }
    64     protected NativeArrayBuffer(final NativeArrayBuffer other, final int begin, final int end) {
    65         this(Arrays.copyOfRange(other.buffer, begin, end));
    66     }
    68     @Getter(attributes = Attribute.NOT_ENUMERABLE | Attribute.NOT_WRITABLE | Attribute.NOT_CONFIGURABLE)
    69     public static Object byteLength(final Object self) {
    70         return ((NativeArrayBuffer)self).buffer.length;
    71     }
    73     @Function(attributes = Attribute.NOT_ENUMERABLE)
    74     public static Object slice(final Object self, final Object begin0, final Object end0) {
    75         final NativeArrayBuffer arrayBuffer = (NativeArrayBuffer)self;
    76         int begin = JSType.toInt32(begin0);
    77         int end = end0 != ScriptRuntime.UNDEFINED ? JSType.toInt32(end0) : arrayBuffer.getByteLength();
    78         begin = adjustIndex(begin, arrayBuffer.getByteLength());
    79         end = adjustIndex(end, arrayBuffer.getByteLength());
    80         return new NativeArrayBuffer((NativeArrayBuffer) self, begin, Math.max(end, begin));
    81     }
    83     /**
    84      * If index is negative, it refers to an index from the end of the array, as
    85      * opposed to from the beginning. The index is clamped to the valid index
    86      * range for the array.
    87      *
    88      * @param index  The index.
    89      * @param length The length of the array.
    90      * @return valid index index in the range [0, length).
    91      */
    92     static int adjustIndex(final int index, final int length) {
    93         if (index < 0) {
    94             return clamp(index + length, length);
    95         }
    96         return clamp(index, length);
    97     }
    99     /**
   100      * Clamp index into the range [0, length).
   101      */
   102     private static int clamp(final int index, final int length) {
   103         if (index < 0) {
   104             return 0;
   105         } else if (index > length) {
   106             return length;
   107         }
   108         return index;
   109     }
   111     public byte[] getByteArray() {
   112         return buffer;
   113     }
   115     public int getByteLength() {
   116         return buffer.length;
   117     }
   118 }

mercurial