libraries/v8/runtime.cc
// Initializes a typed array from an array-like object.
// If an array-like object happens to be a typed array of the same type,
// initializes backing store using memove.
//
// Returns true if backing store was initialized or false otherwise.
Initializes a typed array from an array-like object.
If an array-like object happens to be a typed array of the same type,
initializes backing store using memove.

Returns true if backing store was initialized or false otherwise.
Initializes a typed array from an array - like object . If an array - like object happens to be a typed array of the same type initializes backing store using memove . Returns true if backing store was initialized or false otherwise .
RUNTIME_FUNCTION(MaybeObject*, Runtime_TypedArrayInitializeFromArrayLike) {
  HandleScope scope(isolate);
  ASSERT(args.length() == 4);
  CONVERT_ARG_HANDLE_CHECKED(JSTypedArray, holder, 0);
  CONVERT_SMI_ARG_CHECKED(arrayId, 1);
  CONVERT_ARG_HANDLE_CHECKED(Object, source, 2);
  CONVERT_ARG_HANDLE_CHECKED(Object, length_obj, 3);
  ASSERT(holder->GetInternalFieldCount() ==
      v8::ArrayBufferView::kInternalFieldCount);
  for (int i = 0; i < v8::ArrayBufferView::kInternalFieldCount; i++) {
    holder->SetInternalField(i, Smi::FromInt(0));
  }
  ExternalArrayType array_type = kExternalByteArray;  
  size_t element_size = 1;  
  ArrayIdToTypeAndSize(arrayId, &array_type, &element_size);
  Handle<JSArrayBuffer> buffer = isolate->factory()->NewJSArrayBuffer();
  size_t length = NumberToSize(isolate, *length_obj);
  if ((length > static_cast<unsigned>(Smi::kMaxValue)) ||
      (length > (kMaxInt / element_size))) {
    return isolate->Throw(*isolate->factory()->
          NewRangeError("invalid_typed_array_length",
            HandleVector<Object>(NULL, 0)));
  }
  size_t byte_length = length * element_size;
   TODO(dslomov): revise this once we support subclassing.
  if (!Runtime::SetupArrayBufferAllocatingData(
        isolate, buffer, byte_length, false)) {
    return isolate->Throw(*isolate->factory()->
          NewRangeError("invalid_array_buffer_length",
            HandleVector<Object>(NULL, 0)));
  }
  holder->set_buffer(*buffer);
  holder->set_byte_offset(Smi::FromInt(0));
  Handle<Object> byte_length_obj(
      isolate->factory()->NewNumberFromSize(byte_length));
  holder->set_byte_length(*byte_length_obj);
  holder->set_length(*length_obj);
  holder->set_weak_next(buffer->weak_first_view());
  buffer->set_weak_first_view(*holder);
  Handle<ExternalArray> elements =
      isolate->factory()->NewExternalArray(
          static_cast<int>(length), array_type,
          static_cast<uint8_t*>(buffer->backing_store()));
  holder->set_elements(*elements);
  if (source->IsJSTypedArray()) {
    Handle<JSTypedArray> typed_array(JSTypedArray::cast(*source));
    if (typed_array->type() == holder->type()) {
      uint8_t* backing_store =
        static_cast<uint8_t*>(
          JSArrayBuffer::cast(typed_array->buffer())->backing_store());
      size_t source_byte_offset =
          NumberToSize(isolate, typed_array->byte_offset());
      memcpy(
          buffer->backing_store(),
          backing_store + source_byte_offset,
          byte_length);
      return *isolate->factory()->true_value();
    } else {
      return *isolate->factory()->false_value();
    }
  }
  return *isolate->factory()->false_value();
}
RUNTIME_FUNCTION ( MaybeObject * , Runtime_TypedArrayInitializeFromArrayLike ) { HandleScope scope ( isolate ) ; ASSERT ( args . length ( ) == 4 ) ; CONVERT_ARG_HANDLE_CHECKED ( JSTypedArray , holder , 0 ) ; CONVERT_SMI_ARG_CHECKED ( arrayId , 1 ) ; CONVERT_ARG_HANDLE_CHECKED ( Object , source , 2 ) ; CONVERT_ARG_HANDLE_CHECKED ( Object , length_obj , 3 ) ; ASSERT ( holder -> GetInternalFieldCount ( ) == v8 :: ArrayBufferView :: kInternalFieldCount ) ; for ( int i = 0 ; i < v8 :: ArrayBufferView :: kInternalFieldCount ; i ++ ) { holder -> SetInternalField ( i , Smi :: FromInt ( 0 ) ) ; } ExternalArrayType array_type = kExternalByteArray ; size_t element_size = 1 ; ArrayIdToTypeAndSize ( arrayId , & array_type , & element_size ) ; Handle < JSArrayBuffer > buffer = isolate -> factory ( ) -> NewJSArrayBuffer ( ) ; size_t length = NumberToSize ( isolate , * length_obj ) ; if ( ( length > static_cast < unsigned > ( Smi :: kMaxValue ) ) || ( length > ( kMaxInt / element_size ) ) ) { return isolate -> Throw ( * isolate -> factory ( ) -> NewRangeError ( " " , HandleVector < Object > ( NULL , 0 ) ) ) ; } size_t byte_length = length * element_size ; if ( ! Runtime :: SetupArrayBufferAllocatingData ( isolate , buffer , byte_length , false ) ) { return isolate -> Throw ( * isolate -> factory ( ) -> NewRangeError ( " " , HandleVector < Object > ( NULL , 0 ) ) ) ; } holder -> set_buffer ( * buffer ) ; holder -> set_byte_offset ( Smi :: FromInt ( 0 ) ) ; Handle < Object > byte_length_obj ( isolate -> factory ( ) -> NewNumberFromSize ( byte_length ) ) ; holder -> set_byte_length ( * byte_length_obj ) ; holder -> set_length ( * length_obj ) ; holder -> set_weak_next ( buffer -> weak_first_view ( ) ) ; buffer -> set_weak_first_view ( * holder ) ; Handle < ExternalArray > elements = isolate -> factory ( ) -> NewExternalArray ( static_cast < int > ( length ) , array_type , static_cast < uint8_t * > ( buffer -> backing_store ( ) ) ) ; holder -> set_elements ( * elements ) ; if ( source -> IsJSTypedArray ( ) ) { Handle < JSTypedArray > typed_array ( JSTypedArray :: cast ( * source ) ) ; if ( typed_array -> type ( ) == holder -> type ( ) ) { uint8_t * backing_store = static_cast < uint8_t * > ( JSArrayBuffer :: cast ( typed_array -> buffer ( ) ) -> backing_store ( ) ) ; size_t source_byte_offset = NumberToSize ( isolate , typed_array -> byte_offset ( ) ) ; memcpy ( buffer -> backing_store ( ) , backing_store + source_byte_offset , byte_length ) ; return * isolate -> factory ( ) -> true_value ( ) ; } else { return * isolate -> factory ( ) -> false_value ( ) ; } } return * isolate -> factory ( ) -> false_value ( ) ; }
Initializes a typed array from an array-like object.
Initializes a typed array from an array - like object .
// Bogus initialization. // Bogus initialization. // NOTE: not initializing backing store. // We assume that the caller of this function will initialize holder // with the loop //      for(i = 0; i < length; i++) { holder[i] = source[i]; } // We assume that the caller of this function is always a typed array // constructor. // If source is a typed array, this loop will always run to completion, // so we are sure that the backing store will be initialized. // Otherwise, the indexing operation might throw, so the loop will not // run to completion and the typed array might remain partly initialized. // However we further assume that the caller of this function is a typed array // constructor, and the exception will propagate out of the constructor, // therefore uninitialized memory will not be accessible by a user program. // // TODO(dslomov): revise this once we support subclassing.