Search in sources :

Example 1 with AllocateArrayBuffer

use of com.github.anba.es6draft.runtime.objects.binary.ArrayBufferConstructor.AllocateArrayBuffer 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);
}
Also used : ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) Constructor(com.github.anba.es6draft.runtime.types.Constructor) BuiltinConstructor(com.github.anba.es6draft.runtime.types.builtins.BuiltinConstructor) CloneArrayBuffer(com.github.anba.es6draft.runtime.objects.binary.ArrayBufferConstructor.CloneArrayBuffer) AllocateArrayBuffer(com.github.anba.es6draft.runtime.objects.binary.ArrayBufferConstructor.AllocateArrayBuffer) IsSharedArrayBuffer(com.github.anba.es6draft.runtime.objects.atomics.SharedArrayBufferConstructor.IsSharedArrayBuffer)

Aggregations

IsSharedArrayBuffer (com.github.anba.es6draft.runtime.objects.atomics.SharedArrayBufferConstructor.IsSharedArrayBuffer)1 AllocateArrayBuffer (com.github.anba.es6draft.runtime.objects.binary.ArrayBufferConstructor.AllocateArrayBuffer)1 CloneArrayBuffer (com.github.anba.es6draft.runtime.objects.binary.ArrayBufferConstructor.CloneArrayBuffer)1 Constructor (com.github.anba.es6draft.runtime.types.Constructor)1 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)1 BuiltinConstructor (com.github.anba.es6draft.runtime.types.builtins.BuiltinConstructor)1