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

Print this page




  25 
  26 package jdk.nashorn.internal.objects;
  27 
  28 import static jdk.nashorn.internal.runtime.ECMAErrors.rangeError;
  29 
  30 import jdk.nashorn.internal.objects.annotations.Attribute;
  31 import jdk.nashorn.internal.objects.annotations.Getter;
  32 import jdk.nashorn.internal.objects.annotations.ScriptClass;
  33 import jdk.nashorn.internal.runtime.JSType;
  34 import jdk.nashorn.internal.runtime.PropertyMap;
  35 import jdk.nashorn.internal.runtime.ScriptObject;
  36 import jdk.nashorn.internal.runtime.ScriptRuntime;
  37 import jdk.nashorn.internal.runtime.arrays.ArrayData;
  38 
  39 @ScriptClass("ArrayBufferView")
  40 abstract class ArrayBufferView extends ScriptObject {
  41 
  42     // initialized by nasgen
  43     private static PropertyMap $nasgenmap$;
  44 
  45     static PropertyMap getInitialMap() {
  46         return $nasgenmap$;
  47     }
  48 
  49     private ArrayBufferView(final NativeArrayBuffer buffer, final int byteOffset, final int elementLength, final Global global) {
  50         super(getInitialMap());
  51         checkConstructorArgs(buffer, byteOffset, elementLength);
  52         this.setProto(getPrototype(global));
  53         this.setArray(factory().createArrayData(buffer, byteOffset, elementLength));
  54     }
  55 
  56     ArrayBufferView(final NativeArrayBuffer buffer, final int byteOffset, final int elementLength) {
  57         this(buffer, byteOffset, elementLength, Global.instance());
  58     }
  59 
  60     private void checkConstructorArgs(final NativeArrayBuffer buffer, final int byteOffset, final int elementLength) {
  61         if (byteOffset < 0 || elementLength < 0) {
  62             throw new RuntimeException("byteOffset or length must not be negative");
  63         }
  64         if (byteOffset + elementLength * bytesPerElement() > buffer.getByteLength()) {
  65             throw new RuntimeException("byteOffset + byteLength out of range");
  66         }
  67         if (byteOffset % bytesPerElement() != 0) {
  68             throw new RuntimeException("byteOffset must be a multiple of the element size");
  69         }
  70     }




  25 
  26 package jdk.nashorn.internal.objects;
  27 
  28 import static jdk.nashorn.internal.runtime.ECMAErrors.rangeError;
  29 
  30 import jdk.nashorn.internal.objects.annotations.Attribute;
  31 import jdk.nashorn.internal.objects.annotations.Getter;
  32 import jdk.nashorn.internal.objects.annotations.ScriptClass;
  33 import jdk.nashorn.internal.runtime.JSType;
  34 import jdk.nashorn.internal.runtime.PropertyMap;
  35 import jdk.nashorn.internal.runtime.ScriptObject;
  36 import jdk.nashorn.internal.runtime.ScriptRuntime;
  37 import jdk.nashorn.internal.runtime.arrays.ArrayData;
  38 
  39 @ScriptClass("ArrayBufferView")
  40 abstract class ArrayBufferView extends ScriptObject {
  41 
  42     // initialized by nasgen
  43     private static PropertyMap $nasgenmap$;
  44 




  45     private ArrayBufferView(final NativeArrayBuffer buffer, final int byteOffset, final int elementLength, final Global global) {
  46         super($nasgenmap$);
  47         checkConstructorArgs(buffer, byteOffset, elementLength);
  48         this.setProto(getPrototype(global));
  49         this.setArray(factory().createArrayData(buffer, byteOffset, elementLength));
  50     }
  51 
  52     ArrayBufferView(final NativeArrayBuffer buffer, final int byteOffset, final int elementLength) {
  53         this(buffer, byteOffset, elementLength, Global.instance());
  54     }
  55 
  56     private void checkConstructorArgs(final NativeArrayBuffer buffer, final int byteOffset, final int elementLength) {
  57         if (byteOffset < 0 || elementLength < 0) {
  58             throw new RuntimeException("byteOffset or length must not be negative");
  59         }
  60         if (byteOffset + elementLength * bytesPerElement() > buffer.getByteLength()) {
  61             throw new RuntimeException("byteOffset + byteLength out of range");
  62         }
  63         if (byteOffset % bytesPerElement() != 0) {
  64             throw new RuntimeException("byteOffset must be a multiple of the element size");
  65         }
  66     }