use of com.github.anba.es6draft.runtime.AbstractOperations.SpeciesConstructor in project es6draft by anba.
the class ArrayBufferConstructor method CloneArrayBuffer.
/**
* 24.1.1.4 CloneArrayBuffer (srcBuffer, srcByteOffset)
*
* @param cx
* the execution context
* @param srcBuffer
* the source buffer
* @param srcByteOffset
* the source offset
* @param cloneConstructor
* the intrinsic constructor function
* @return the new array buffer object
*/
public static ArrayBufferObject CloneArrayBuffer(ExecutionContext cx, ArrayBuffer srcBuffer, long srcByteOffset, Intrinsics cloneConstructor) {
/* step 1 (implicit) */
/* steps 2-3 */
Constructor bufferConstructor;
if (cloneConstructor == null) {
/* steps 2.a-b */
bufferConstructor = SpeciesConstructor(cx, srcBuffer, Intrinsics.ArrayBuffer);
/* step 2.c */
if (IsDetachedBuffer(srcBuffer)) {
throw newTypeError(cx, Messages.Key.BufferDetached);
}
} else {
/* step 3 */
assert IsConstructor(cx.getIntrinsic(cloneConstructor));
bufferConstructor = (Constructor) cx.getIntrinsic(cloneConstructor);
}
/* step 4 */
ByteBuffer srcBlock = srcBuffer.getData();
/* step 5 */
long srcLength = srcBuffer.getByteLength();
/* step 6 */
assert srcByteOffset <= srcLength;
/* step 7 */
long cloneLength = srcLength - srcByteOffset;
/* steps 8-9 */
ArrayBufferObject targetBuffer = AllocateArrayBuffer(cx, bufferConstructor, cloneLength);
/* step 10 */
if (IsDetachedBuffer(srcBuffer)) {
throw newTypeError(cx, Messages.Key.BufferDetached);
}
/* step 11 */
ByteBuffer targetBlock = targetBuffer.getData();
/* step 12 */
CopyDataBlockBytes(targetBlock, 0, srcBlock, srcByteOffset, cloneLength);
/* step 13 */
return targetBuffer;
}
use of com.github.anba.es6draft.runtime.AbstractOperations.SpeciesConstructor in project es6draft by anba.
the class TypedArrayConstructor method TypedArraySpeciesCreate.
/**
* TypedArraySpeciesCreate( exemplar, argumentList )
*
* @param cx
* the execution context
* @param exemplar
* the typed array object
* @param length
* the new typed array length
* @return the new typed array object
*/
public static TypedArrayObject TypedArraySpeciesCreate(ExecutionContext cx, TypedArrayObject exemplar, long length) {
/* step 1 (implicit) */
/* step 2 */
Intrinsics defaultConstructor = exemplar.getElementType().getConstructor();
/* step 3 */
Constructor constructor = SpeciesConstructor(cx, exemplar, defaultConstructor);
/* step 4 */
return TypedArrayCreate(cx, constructor, length);
}
use of com.github.anba.es6draft.runtime.AbstractOperations.SpeciesConstructor in project es6draft by anba.
the class TypedArrayConstructor method TypedArraySpeciesCreate.
/**
* TypedArraySpeciesCreate( exemplar, argumentList )
*
* @param cx
* the execution context
* @param exemplar
* the typed array object
* @param args
* the constructor function arguments
* @return the new typed array object
*/
public static TypedArrayObject TypedArraySpeciesCreate(ExecutionContext cx, TypedArrayObject exemplar, Object... args) {
/* step 1 (implicit) */
/* step 2 */
Intrinsics defaultConstructor = exemplar.getElementType().getConstructor();
/* step 3 */
Constructor constructor = SpeciesConstructor(cx, exemplar, defaultConstructor);
/* step 4 */
return TypedArrayCreate(cx, constructor, args);
}
use of com.github.anba.es6draft.runtime.AbstractOperations.SpeciesConstructor 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);
}
/* steps 8-9 */
ElementType elementType = this.elementType;
/* step 10 */
long elementLength = srcArray.getArrayLength();
/* steps 11-12 */
ElementType srcType = srcArray.getElementType();
/* step 13 */
int srcElementSize = srcType.size();
/* step 14 */
long srcByteOffset = srcArray.getByteOffset();
/* step 15 */
int elementSize = elementType.size();
/* step 16 */
long byteLength = elementSize * elementLength;
/* steps 17-18 */
ArrayBufferObject data;
if (elementType == srcType) {
/* step 17 */
data = CloneArrayBuffer(cx, srcData, srcByteOffset);
} else {
/* step 18 */
/* step 18.a */
// FIXME: spec bug - SharedArrayBuffers not handled correctly!
Constructor bufferConstructor = SpeciesConstructor(cx, srcData, Intrinsics.ArrayBuffer);
/* step 18.b */
data = AllocateArrayBuffer(cx, bufferConstructor, byteLength);
/* step 18.c */
if (IsDetachedBuffer(srcData)) {
throw newTypeError(cx, Messages.Key.BufferDetached);
}
/* step 18.d */
long srcByteIndex = srcByteOffset;
/* step 18.e */
long targetByteIndex = 0;
/* steps 18.f-g */
for (long count = elementLength; count > 0; --count) {
double value = GetValueFromBuffer(srcData, srcByteIndex, srcType);
SetValueInBuffer(data, targetByteIndex, elementType, value);
srcByteIndex += srcElementSize;
targetByteIndex += elementSize;
}
}
/* steps 4, 19-23 */
return new TypedArrayObject(cx.getRealm(), elementType, data, byteLength, 0, elementLength, proto);
}
Aggregations