use of com.github.anba.es6draft.runtime.objects.binary.ArrayBufferConstructor.CloneArrayBuffer in project es6draft by anba.
the class TypedArrayConstructor method constructWithTypedArray.
/**
* 22.2.4.3 TypedArray ( typedArray )
*
* @param cx
* the execution context
* @param newTarget
* the NewTarget constructor object
* @param typedArray
* the source typed array object
* @return the typed array object
*/
private TypedArrayObject constructWithTypedArray(ExecutionContext cx, Constructor newTarget, TypedArrayObject typedArray) {
/* step 1 (implicit) */
/* step 2 (not applicable) */
/* steps 3-4 (TypedArray allocation deferred) */
ScriptObject proto = GetPrototypeFromConstructor(cx, newTarget, prototypeForType(elementType));
/* step 5 */
TypedArrayObject srcArray = typedArray;
/* step 6 */
ArrayBuffer srcData = srcArray.getBuffer();
/* step 7 */
if (IsDetachedBuffer(srcData)) {
throw newTypeError(cx, Messages.Key.BufferDetached);
}
/* step 8 */
ElementType elementType = this.elementType;
/* step 9 */
long elementLength = srcArray.getArrayLength();
/* steps 10-11 */
ElementType srcType = srcArray.getElementType();
/* step 12 (omitted) */
/* step 13 */
long srcByteOffset = srcArray.getByteOffset();
/* steps 14-15 */
long byteLength = elementType.toBytes(elementLength);
/* steps 16-17 */
Constructor bufferConstructor;
if (!IsSharedArrayBuffer(srcData)) {
bufferConstructor = SpeciesConstructor(cx, srcData, Intrinsics.ArrayBuffer);
} else {
bufferConstructor = (Constructor) cx.getIntrinsic(Intrinsics.ArrayBuffer);
}
/* steps 18-19 */
ArrayBufferObject data;
if (elementType == srcType) {
/* step 17.a */
data = CloneArrayBuffer(cx, srcData, srcByteOffset, byteLength, bufferConstructor);
} else {
/* step 18 */
/* step 18.a */
data = AllocateArrayBuffer(cx, bufferConstructor, byteLength);
/* step 18.b */
if (IsDetachedBuffer(srcData)) {
throw newTypeError(cx, Messages.Key.BufferDetached);
}
// FIXME: spec issue - move before array buffer allocation?
if (!elementType.isCompatibleNumericType(srcType)) {
throw newTypeError(cx, Messages.Key.IncompatibleElementTypes);
}
/* steps 18.c-f */
srcArray.functions().construct(cx, srcArray, data, elementType);
}
/* steps 4, 20-24 */
return new TypedArrayObject(cx.getRealm(), elementType, data, byteLength, 0, elementLength, proto);
}
use of com.github.anba.es6draft.runtime.objects.binary.ArrayBufferConstructor.CloneArrayBuffer in project es6draft by anba.
the class TypedArrayFunctions method cloneArrayBuffer.
private static ArrayBuffer cloneArrayBuffer(ExecutionContext cx, TypedArrayObject typedArray, TypedArrayObject target, long targetOffset) {
ArrayBuffer targetBuffer = target.getBuffer();
ArrayBuffer srcBuffer = typedArray.getBuffer();
if (srcBuffer.sameData(targetBuffer) && isOverlappingSet(typedArray, target, targetOffset)) {
return CloneArrayBuffer(cx, srcBuffer, typedArray.getByteOffset(), typedArray.getByteLength(), (Constructor) cx.getIntrinsic(Intrinsics.ArrayBuffer));
}
return srcBuffer;
}
use of com.github.anba.es6draft.runtime.objects.binary.ArrayBufferConstructor.CloneArrayBuffer in project es6draft by anba.
the class TypedArrayFunctions method set.
final void set(ExecutionContext cx, TypedArrayObject typedArray, TypedArrayObject target, long targetOffset) {
/* step 8 */
ArrayBuffer targetBuffer = target.getBuffer();
/* step 11 */
ArrayBuffer srcBuffer = typedArray.getBuffer();
/* steps 13-19 (omitted) */
/* step 20 */
int srcLength = length(typedArray);
/* step 21 */
long srcByteOffset = typedArray.getByteOffset();
/* steps 23-24 */
ArrayBuffer clonedSrcBuffer = cloneArrayBuffer(cx, typedArray, target, targetOffset);
assert !IsDetachedBuffer(targetBuffer);
int srcByteIndex;
if (clonedSrcBuffer != srcBuffer) {
srcBuffer = clonedSrcBuffer;
srcByteIndex = 0;
} else {
srcByteIndex = index(srcByteOffset);
}
int targetIndex = index(targetOffset);
/* steps 26-28 */
assert typedArray.getElementType().isCompatibleNumericType(target.getElementType());
switch(target.getElementType()) {
case Int8:
setInt8(srcBuffer, srcByteIndex, target, targetIndex, srcLength);
break;
case Uint8:
setUint8(srcBuffer, srcByteIndex, target, targetIndex, srcLength);
break;
case Uint8C:
setUint8C(srcBuffer, srcByteIndex, target, targetIndex, srcLength);
break;
case Int16:
setInt16(srcBuffer, srcByteIndex, target, targetIndex, srcLength);
break;
case Uint16:
setUint16(srcBuffer, srcByteIndex, target, targetIndex, srcLength);
break;
case Int32:
setInt32(srcBuffer, srcByteIndex, target, targetIndex, srcLength);
break;
case Uint32:
setUint32(srcBuffer, srcByteIndex, target, targetIndex, srcLength);
break;
case BigInt64:
setInt64(srcBuffer, srcByteIndex, target, targetIndex, srcLength);
break;
case BigUint64:
setUint64(srcBuffer, srcByteIndex, target, targetIndex, srcLength);
break;
case Float32:
setFloat32(srcBuffer, srcByteIndex, target, targetIndex, srcLength);
break;
case Float64:
setFloat64(srcBuffer, srcByteIndex, target, targetIndex, srcLength);
break;
default:
throw new AssertionError();
}
}
Aggregations