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 
  26 package jdk.nashorn.internal.objects;
  27 
  28 import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
  29 import java.nio.ByteBuffer;
  30 import jdk.nashorn.internal.objects.annotations.Attribute;
  31 import jdk.nashorn.internal.objects.annotations.Constructor;
  32 import jdk.nashorn.internal.objects.annotations.Function;
  33 import jdk.nashorn.internal.objects.annotations.Getter;
  34 import jdk.nashorn.internal.objects.annotations.ScriptClass;
  35 import jdk.nashorn.internal.objects.annotations.SpecializedFunction;
  36 import jdk.nashorn.internal.objects.annotations.Where;
  37 import jdk.nashorn.internal.runtime.JSType;
  38 import jdk.nashorn.internal.runtime.PropertyMap;
  39 import jdk.nashorn.internal.runtime.ScriptObject;
  40 import jdk.nashorn.internal.runtime.ScriptRuntime;
  41 
  42 /**
  43  * NativeArrayBuffer - ArrayBuffer as described in the JS typed
  44  * array spec
  45  */
  46 @ScriptClass("ArrayBuffer")
  47 public final class NativeArrayBuffer extends ScriptObject {
  48     private final ByteBuffer nb;
  49 
  50     // initialized by nasgen
  51     private static PropertyMap $nasgenmap$;
  52 
  53     /**
  54      * Constructor
  55      * @param nb native byte buffer to wrap
  56      * @param global global instance
  57      */
  58     protected NativeArrayBuffer(final ByteBuffer nb, final Global global) {
  59         super(global.getArrayBufferPrototype(), $nasgenmap$);
  60         this.nb = nb;
  61     }
  62 
  63     /**
  64      * Constructor
  65      * @param nb native byte buffer to wrap
  66      */
  67     protected NativeArrayBuffer(final ByteBuffer nb) {
  68         this(nb, Global.instance());
  69     }
  70 
  71     /**
  72      * Constructor
  73      * @param byteLength byteLength for buffer
  74      */
  75     protected NativeArrayBuffer(final int byteLength) {
  76         this(ByteBuffer.allocateDirect(byteLength));
  77     }
  78 
  79     /**
  80      * Clone constructor
  81      * Used only for slice
  82      * @param other original buffer
  83      * @param begin begin byte index
  84      * @param end   end byte index
  85      */
  86     protected NativeArrayBuffer(final NativeArrayBuffer other, final int begin, final int end) {
  87         this(cloneBuffer(other.getNioBuffer(), begin, end));
  88     }
  89 
  90     /**
  91      * Constructor
  92      * @param newObj is this invoked with new
  93      * @param self   self reference
  94      * @param args   arguments to constructor
  95      * @return new NativeArrayBuffer
  96      */
  97     @Constructor(arity = 1)
  98     public static NativeArrayBuffer constructor(final boolean newObj, final Object self, final Object... args) {
  99         if (!newObj) {
 100             throw typeError("constructor.requires.new", "ArrayBuffer");
 101         }
 102 
 103         if (args.length == 0) {
 104             throw new RuntimeException("missing length argument");
 105         }
 106 
 107         return new NativeArrayBuffer(JSType.toInt32(args[0]));
 108     }
 109 
 110     private static ByteBuffer cloneBuffer(final ByteBuffer original, final int begin, final int end) {
 111         final ByteBuffer clone = ByteBuffer.allocateDirect(original.capacity());
 112         original.rewind();//copy from the beginning
 113         clone.put(original);
 114         original.rewind();
 115         clone.flip();
 116         clone.position(begin);
 117         clone.limit(end);
 118         return clone.slice();
 119     }
 120 
 121     ByteBuffer getNioBuffer() {
 122         return nb;
 123     }
 124 
 125     @Override
 126     public String getClassName() {
 127         return "ArrayBuffer";
 128     }
 129 
 130     /**
 131      * Byte length for native array buffer
 132      * @param self native array buffer
 133      * @return byte length
 134      */
 135     @Getter(attributes = Attribute.NOT_ENUMERABLE | Attribute.NOT_WRITABLE | Attribute.NOT_CONFIGURABLE)
 136     public static int byteLength(final Object self) {
 137         return ((NativeArrayBuffer)self).getByteLength();
 138     }
 139 
 140     /**
 141      * Returns true if an object is an ArrayBufferView
 142      *
 143      * @param self self
 144      * @param obj  object to check
 145      *
 146      * @return true if obj is an ArrayBufferView
 147      */
 148     @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
 149     public static boolean isView(final Object self, final Object obj) {
 150         return obj instanceof ArrayBufferView;
 151     }
 152 
 153     /**
 154      * Slice function
 155      * @param self   native array buffer
 156      * @param begin0 start byte index
 157      * @param end0   end byte index
 158      * @return new array buffer, sliced
 159      */
 160     @Function(attributes = Attribute.NOT_ENUMERABLE)
 161     public static NativeArrayBuffer slice(final Object self, final Object begin0, final Object end0) {
 162         final NativeArrayBuffer arrayBuffer = (NativeArrayBuffer)self;
 163         final int               byteLength  = arrayBuffer.getByteLength();
 164         final int               begin       = adjustIndex(JSType.toInt32(begin0), byteLength);
 165         final int               end         = adjustIndex(end0 != ScriptRuntime.UNDEFINED ? JSType.toInt32(end0) : byteLength, byteLength);
 166         return new NativeArrayBuffer(arrayBuffer, begin, Math.max(end, begin));
 167     }
 168 
 169     /**
 170      * Specialized slice function
 171      * @param self   native array buffer
 172      * @param begin  start byte index
 173      * @param end    end byte index
 174      * @return new array buffer, sliced
 175      */
 176     @SpecializedFunction
 177     public static Object slice(final Object self, final int begin, final int end) {
 178         final NativeArrayBuffer arrayBuffer = (NativeArrayBuffer)self;
 179         final int byteLength  = arrayBuffer.getByteLength();
 180         return new NativeArrayBuffer(arrayBuffer, adjustIndex(begin, byteLength), Math.max(adjustIndex(end, byteLength), begin));
 181     }
 182 
 183     /**
 184      * Specialized slice function
 185      * @param self   native array buffer
 186      * @param begin  start byte index
 187      * @return new array buffer, sliced
 188      */
 189     @SpecializedFunction
 190     public static Object slice(final Object self, final int begin) {
 191         return slice(self, begin, ((NativeArrayBuffer)self).getByteLength());
 192     }
 193 
 194     /**
 195      * If index is negative, it refers to an index from the end of the array, as
 196      * opposed to from the beginning. The index is clamped to the valid index
 197      * range for the array.
 198      *
 199      * @param index  The index.
 200      * @param length The length of the array.
 201      * @return valid index index in the range [0, length).
 202      */
 203     static int adjustIndex(final int index, final int length) {
 204         return index < 0 ? clamp(index + length, length) : clamp(index, length);
 205     }
 206 
 207     /**
 208      * Clamp index into the range [0, length).
 209      */
 210     private static int clamp(final int index, final int length) {
 211         if (index < 0) {
 212             return 0;
 213         } else if (index > length) {
 214             return length;
 215         }
 216         return index;
 217     }
 218 
 219     int getByteLength() {
 220         return nb.limit();
 221     }
 222 
 223     ByteBuffer getBuffer() {
 224        return nb;
 225     }
 226 
 227     ByteBuffer getBuffer(final int offset) {
 228         return (ByteBuffer)nb.duplicate().position(offset);
 229     }
 230 
 231     ByteBuffer getBuffer(final int offset, final int length) {
 232         return (ByteBuffer)getBuffer(offset).limit(length);
 233     }
 234 }