Rename the reject() helper function to something more meaningful.
[WebKit-https.git] / Source / JavaScriptCore / runtime / JSGenericTypedArrayViewInlines.h
1 /*
2  * Copyright (C) 2013, 2016 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
24  */
25
26 #pragma once
27
28 #include "ArrayBufferView.h"
29 #include "DeferGC.h"
30 #include "Error.h"
31 #include "ExceptionHelpers.h"
32 #include "JSArrayBuffer.h"
33 #include "JSGenericTypedArrayView.h"
34 #include "TypeError.h"
35 #include "TypedArrays.h"
36
37 namespace JSC {
38
39 template<typename Adaptor>
40 JSGenericTypedArrayView<Adaptor>::JSGenericTypedArrayView(
41     VM& vm, ConstructionContext& context)
42     : Base(vm, context)
43 {
44 }
45
46 template<typename Adaptor>
47 JSGenericTypedArrayView<Adaptor>* JSGenericTypedArrayView<Adaptor>::create(
48     ExecState* exec, Structure* structure, unsigned length)
49 {
50     VM& vm = exec->vm();
51     auto scope = DECLARE_THROW_SCOPE(vm);
52     ConstructionContext context(vm, structure, length, sizeof(typename Adaptor::Type));
53     if (!context) {
54         throwOutOfMemoryError(exec, scope);
55         return nullptr;
56     }
57     JSGenericTypedArrayView* result =
58         new (NotNull, allocateCell<JSGenericTypedArrayView>(vm.heap))
59         JSGenericTypedArrayView(vm, context);
60     result->finishCreation(vm);
61     return result;
62 }
63
64 template<typename Adaptor>
65 JSGenericTypedArrayView<Adaptor>* JSGenericTypedArrayView<Adaptor>::createWithFastVector(
66     ExecState* exec, Structure* structure, unsigned length, void* vector)
67 {
68     VM& vm = exec->vm();
69     ConstructionContext context(structure, length, vector);
70     RELEASE_ASSERT(context);
71     JSGenericTypedArrayView* result =
72         new (NotNull, allocateCell<JSGenericTypedArrayView>(vm.heap))
73         JSGenericTypedArrayView(vm, context);
74     result->finishCreation(vm);
75     return result;
76 }
77
78 template<typename Adaptor>
79 JSGenericTypedArrayView<Adaptor>* JSGenericTypedArrayView<Adaptor>::createUninitialized(
80     ExecState* exec, Structure* structure, unsigned length)
81 {
82     VM& vm = exec->vm();
83     auto scope = DECLARE_THROW_SCOPE(vm);
84     ConstructionContext context(
85         vm, structure, length, sizeof(typename Adaptor::Type),
86         ConstructionContext::DontInitialize);
87     if (!context) {
88         throwOutOfMemoryError(exec, scope);
89         return nullptr;
90     }
91     JSGenericTypedArrayView* result =
92         new (NotNull, allocateCell<JSGenericTypedArrayView>(vm.heap))
93         JSGenericTypedArrayView(vm, context);
94     result->finishCreation(vm);
95     return result;
96 }
97
98 template<typename Adaptor>
99 JSGenericTypedArrayView<Adaptor>* JSGenericTypedArrayView<Adaptor>::create(
100     ExecState* exec, Structure* structure, PassRefPtr<ArrayBuffer> passedBuffer,
101     unsigned byteOffset, unsigned length)
102 {
103     VM& vm = exec->vm();
104     auto scope = DECLARE_THROW_SCOPE(vm);
105     RefPtr<ArrayBuffer> buffer = passedBuffer;
106     size_t size = sizeof(typename Adaptor::Type);
107     if (!ArrayBufferView::verifySubRangeLength(buffer, byteOffset, length, size)) {
108         throwException(exec, scope, createRangeError(exec, "Length out of range of buffer"));
109         return nullptr;
110     }
111     if (!ArrayBufferView::verifyByteOffsetAlignment(byteOffset, size)) {
112         throwException(exec, scope, createRangeError(exec, "Byte offset is not aligned"));
113         return nullptr;
114     }
115     ConstructionContext context(vm, structure, buffer, byteOffset, length);
116     ASSERT(context);
117     JSGenericTypedArrayView* result =
118         new (NotNull, allocateCell<JSGenericTypedArrayView>(vm.heap))
119         JSGenericTypedArrayView(vm, context);
120     result->finishCreation(vm);
121     return result;
122 }
123
124 template<typename Adaptor>
125 JSGenericTypedArrayView<Adaptor>* JSGenericTypedArrayView<Adaptor>::create(
126     VM& vm, Structure* structure, PassRefPtr<typename Adaptor::ViewType> impl)
127 {
128     RefPtr<ArrayBuffer> buffer = impl->buffer();
129     ConstructionContext context(vm, structure, buffer, impl->byteOffset(), impl->length());
130     ASSERT(context);
131     JSGenericTypedArrayView* result =
132         new (NotNull, allocateCell<JSGenericTypedArrayView>(vm.heap))
133         JSGenericTypedArrayView(vm, context);
134     result->finishCreation(vm);
135     return result;
136 }
137
138 template<typename Adaptor>
139 JSGenericTypedArrayView<Adaptor>* JSGenericTypedArrayView<Adaptor>::create(
140     Structure* structure, JSGlobalObject* globalObject,
141     PassRefPtr<typename Adaptor::ViewType> impl)
142 {
143     return create(globalObject->vm(), structure, impl);
144 }
145
146 template<typename Adaptor>
147 bool JSGenericTypedArrayView<Adaptor>::validateRange(
148     ExecState* exec, unsigned offset, unsigned length)
149 {
150     VM& vm = exec->vm();
151     auto scope = DECLARE_THROW_SCOPE(vm);
152     if (canAccessRangeQuickly(offset, length))
153         return true;
154     
155     throwException(exec, scope, createRangeError(exec, "Range consisting of offset and length are out of bounds"));
156     return false;
157 }
158
159 template<typename Adaptor>
160 template<typename OtherAdaptor>
161 bool JSGenericTypedArrayView<Adaptor>::setWithSpecificType(
162     ExecState* exec, unsigned offset, JSGenericTypedArrayView<OtherAdaptor>* other,
163     unsigned otherOffset, unsigned length, CopyType type)
164 {
165     // Handle the hilarious case: the act of getting the length could have resulted
166     // in neutering. Well, no. That'll never happen because there cannot be
167     // side-effects on getting the length of a typed array. But predicting where there
168     // are, or aren't, side-effects is a fool's game so we resort to this cheap
169     // check. Worst case, if we're wrong, people start seeing less things get copied
170     // but we won't have a security vulnerability.
171     length = std::min(length, other->length());
172
173     RELEASE_ASSERT(other->canAccessRangeQuickly(otherOffset, length));
174     if (!validateRange(exec, offset, length))
175         return false;
176     
177     // This method doesn't support copying between the same array. Note that
178     // set() will only call this if the types differ, which implicitly guarantees
179     // that we can't be the same array. This is relevant because the way we detect
180     // non-overlapping is by checking if either (a) either array doesn't have a
181     // backing buffer or (b) the backing buffers are different, but that doesn't
182     // catch the case where it's the *same* array - fortunately though, this code
183     // path never needs to worry about that case.
184     ASSERT(static_cast<JSCell*>(this) != static_cast<JSCell*>(other));
185     
186     // 1) If the two arrays are non-overlapping, we can copy in any order we like
187     //    and we don't need an intermediate buffer. Arrays are definitely
188     //    non-overlapping if either one of them has no backing buffer (that means
189     //    that it *owns* its philosophical backing buffer) or if they have
190     //    different backing buffers.
191     // 2) If the two arrays overlap but have the same element size, we can do a
192     //    memmove-like copy where we flip-flop direction based on which vector
193     //    starts before the other:
194     //    A) If the destination vector is before the source vector, then a forward
195     //       copy is in order.
196     //    B) If the destination vector is after the source vector, then a backward
197     //       copy is in order.
198     // 3) If we have different element sizes and there is a chance of overlap then
199     //    we need an intermediate vector.
200     
201     // NB. Comparisons involving elementSize will be constant-folded by template
202     // specialization.
203
204     unsigned otherElementSize = sizeof(typename OtherAdaptor::Type);
205
206     // Handle cases (1) and (2A).
207     if (!hasArrayBuffer() || !other->hasArrayBuffer()
208         || existingBuffer() != other->existingBuffer()
209         || (elementSize == otherElementSize && vector() <= other->vector())
210         || type == CopyType::LeftToRight) {
211         for (unsigned i = 0; i < length; ++i) {
212             setIndexQuicklyToNativeValue(
213                 offset + i, OtherAdaptor::template convertTo<Adaptor>(
214                     other->getIndexQuicklyAsNativeValue(i + otherOffset)));
215         }
216         return true;
217     }
218
219     // Now we either have (2B) or (3) - so first we try to cover (2B).
220     if (elementSize == otherElementSize) {
221         for (unsigned i = length; i--;) {
222             setIndexQuicklyToNativeValue(
223                 offset + i, OtherAdaptor::template convertTo<Adaptor>(
224                     other->getIndexQuicklyAsNativeValue(i + otherOffset)));
225         }
226         return true;
227     }
228     
229     // Fail: we need an intermediate transfer buffer (i.e. case (3)).
230     Vector<typename Adaptor::Type, 32> transferBuffer(length);
231     for (unsigned i = length; i--;) {
232         transferBuffer[i] = OtherAdaptor::template convertTo<Adaptor>(
233             other->getIndexQuicklyAsNativeValue(i + otherOffset));
234     }
235     for (unsigned i = length; i--;)
236         setIndexQuicklyToNativeValue(offset + i, transferBuffer[i]);
237     
238     return true;
239 }
240
241 template<typename Adaptor>
242 bool JSGenericTypedArrayView<Adaptor>::set(
243     ExecState* exec, unsigned offset, JSObject* object, unsigned objectOffset, unsigned length, CopyType type)
244 {
245     const ClassInfo* ci = object->classInfo();
246     if (ci->typedArrayStorageType == Adaptor::typeValue) {
247         // The super fast case: we can just memcpy since we're the same type.
248         JSGenericTypedArrayView* other = jsCast<JSGenericTypedArrayView*>(object);
249         length = std::min(length, other->length());
250         
251         RELEASE_ASSERT(other->canAccessRangeQuickly(objectOffset, length));
252         if (!validateRange(exec, offset, length))
253             return false;
254
255         memmove(typedVector() + offset, other->typedVector() + objectOffset, length * elementSize);
256         return true;
257     }
258     
259     switch (ci->typedArrayStorageType) {
260     case TypeInt8:
261         return setWithSpecificType<Int8Adaptor>(
262             exec, offset, jsCast<JSInt8Array*>(object), objectOffset, length, type);
263     case TypeInt16:
264         return setWithSpecificType<Int16Adaptor>(
265             exec, offset, jsCast<JSInt16Array*>(object), objectOffset, length, type);
266     case TypeInt32:
267         return setWithSpecificType<Int32Adaptor>(
268             exec, offset, jsCast<JSInt32Array*>(object), objectOffset, length, type);
269     case TypeUint8:
270         return setWithSpecificType<Uint8Adaptor>(
271             exec, offset, jsCast<JSUint8Array*>(object), objectOffset, length, type);
272     case TypeUint8Clamped:
273         return setWithSpecificType<Uint8ClampedAdaptor>(
274             exec, offset, jsCast<JSUint8ClampedArray*>(object), objectOffset, length, type);
275     case TypeUint16:
276         return setWithSpecificType<Uint16Adaptor>(
277             exec, offset, jsCast<JSUint16Array*>(object), objectOffset, length, type);
278     case TypeUint32:
279         return setWithSpecificType<Uint32Adaptor>(
280             exec, offset, jsCast<JSUint32Array*>(object), objectOffset, length, type);
281     case TypeFloat32:
282         return setWithSpecificType<Float32Adaptor>(
283             exec, offset, jsCast<JSFloat32Array*>(object), objectOffset, length, type);
284     case TypeFloat64:
285         return setWithSpecificType<Float64Adaptor>(
286             exec, offset, jsCast<JSFloat64Array*>(object), objectOffset, length, type);
287     case NotTypedArray:
288     case TypeDataView: {
289         if (!validateRange(exec, offset, length))
290             return false;
291
292         // We could optimize this case. But right now, we don't.
293         for (unsigned i = 0; i < length; ++i) {
294             JSValue value = object->get(exec, i + objectOffset);
295             if (!setIndex(exec, offset + i, value))
296                 return false;
297         }
298         return true;
299     } }
300     
301     RELEASE_ASSERT_NOT_REACHED();
302     return false;
303 }
304
305 template<typename Adaptor>
306 ArrayBuffer* JSGenericTypedArrayView<Adaptor>::existingBuffer()
307 {
308     return existingBufferInButterfly();
309 }
310
311 template<typename Adaptor>
312 EncodedJSValue JSGenericTypedArrayView<Adaptor>::throwNeuteredTypedArrayTypeError(ExecState* exec, EncodedJSValue object, PropertyName)
313 {
314     VM& vm = exec->vm();
315     auto scope = DECLARE_THROW_SCOPE(vm);
316     ASSERT_UNUSED(object, jsCast<JSGenericTypedArrayView*>(JSValue::decode(object))->isNeutered());
317     return throwVMTypeError(exec, scope, typedArrayBufferHasBeenDetachedErrorMessage);
318 }
319
320 template<typename Adaptor>
321 bool JSGenericTypedArrayView<Adaptor>::getOwnPropertySlot(
322     JSObject* object, ExecState* exec, PropertyName propertyName, PropertySlot& slot)
323 {
324     JSGenericTypedArrayView* thisObject = jsCast<JSGenericTypedArrayView*>(object);
325
326     if (Optional<uint32_t> index = parseIndex(propertyName)) {
327         if (thisObject->isNeutered()) {
328             slot.setCustom(thisObject, None, throwNeuteredTypedArrayTypeError);
329             return true;
330         }
331
332         if (thisObject->canGetIndexQuickly(index.value()))
333             slot.setValue(thisObject, DontDelete | ReadOnly, thisObject->getIndexQuickly(index.value()));
334         else
335             slot.setValue(thisObject, DontDelete | ReadOnly, jsUndefined());
336         return true;
337     }
338     
339     return Base::getOwnPropertySlot(thisObject, exec, propertyName, slot);
340 }
341
342 template<typename Adaptor>
343 bool JSGenericTypedArrayView<Adaptor>::put(
344     JSCell* cell, ExecState* exec, PropertyName propertyName, JSValue value,
345     PutPropertySlot& slot)
346 {
347     JSGenericTypedArrayView* thisObject = jsCast<JSGenericTypedArrayView*>(cell);
348
349     // https://tc39.github.io/ecma262/#sec-integer-indexed-exotic-objects-set-p-v-receiver
350     // Ignore the receiver even if the receiver is altered to non base value.
351     // 9.4.5.5-2-b-i Return ? IntegerIndexedElementSet(O, numericIndex, V).
352     if (Optional<uint32_t> index = parseIndex(propertyName))
353         return putByIndex(thisObject, exec, index.value(), value, slot.isStrictMode());
354     
355     return Base::put(thisObject, exec, propertyName, value, slot);
356 }
357
358 template<typename Adaptor>
359 bool JSGenericTypedArrayView<Adaptor>::defineOwnProperty(
360     JSObject* object, ExecState* exec, PropertyName propertyName,
361     const PropertyDescriptor& descriptor, bool shouldThrow)
362 {
363     VM& vm = exec->vm();
364     auto scope = DECLARE_THROW_SCOPE(vm);
365     JSGenericTypedArrayView* thisObject = jsCast<JSGenericTypedArrayView*>(object);
366
367     if (parseIndex(propertyName)) {
368         if (descriptor.isAccessorDescriptor())
369             return typeError(exec, scope, shouldThrow, ASCIILiteral("Attempting to store accessor indexed property on a typed array."));
370
371         if (descriptor.configurable())
372             return typeError(exec, scope, shouldThrow, ASCIILiteral("Attempting to configure non-configurable property."));
373
374         if (!descriptor.enumerable() || !descriptor.writable())
375             return typeError(exec, scope, shouldThrow, ASCIILiteral("Attempting to store non-enumerable or non-writable indexed property on a typed array."));
376
377         if (descriptor.value()) {
378             PutPropertySlot unused(JSValue(thisObject), shouldThrow);
379             return thisObject->put(thisObject, exec, propertyName, descriptor.value(), unused);
380         }
381         return true;
382     }
383     
384     return Base::defineOwnProperty(thisObject, exec, propertyName, descriptor, shouldThrow);
385 }
386
387 template<typename Adaptor>
388 bool JSGenericTypedArrayView<Adaptor>::deleteProperty(
389     JSCell* cell, ExecState* exec, PropertyName propertyName)
390 {
391     VM& vm = exec->vm();
392     auto scope = DECLARE_THROW_SCOPE(vm);
393     JSGenericTypedArrayView* thisObject = jsCast<JSGenericTypedArrayView*>(cell);
394
395     if (thisObject->isNeutered())
396         return typeError(exec, scope, true, ASCIILiteral(typedArrayBufferHasBeenDetachedErrorMessage));
397
398     if (parseIndex(propertyName))
399         return false;
400     
401     return Base::deleteProperty(thisObject, exec, propertyName);
402 }
403
404 template<typename Adaptor>
405 bool JSGenericTypedArrayView<Adaptor>::getOwnPropertySlotByIndex(
406     JSObject* object, ExecState* exec, unsigned propertyName, PropertySlot& slot)
407 {
408     JSGenericTypedArrayView* thisObject = jsCast<JSGenericTypedArrayView*>(object);
409
410     if (thisObject->isNeutered()) {
411         slot.setCustom(thisObject, None, throwNeuteredTypedArrayTypeError);
412         return true;
413     }
414
415     if (propertyName > MAX_ARRAY_INDEX) {
416         return thisObject->methodTable()->getOwnPropertySlot(
417             thisObject, exec, Identifier::from(exec, propertyName), slot);
418     }
419     
420     if (!thisObject->canGetIndexQuickly(propertyName))
421         return false;
422     
423     slot.setValue(thisObject, None, thisObject->getIndexQuickly(propertyName));
424     return true;
425 }
426
427 template<typename Adaptor>
428 bool JSGenericTypedArrayView<Adaptor>::putByIndex(
429     JSCell* cell, ExecState* exec, unsigned propertyName, JSValue value, bool shouldThrow)
430 {
431     JSGenericTypedArrayView* thisObject = jsCast<JSGenericTypedArrayView*>(cell);
432
433     if (propertyName > MAX_ARRAY_INDEX) {
434         PutPropertySlot slot(JSValue(thisObject), shouldThrow);
435         return thisObject->methodTable()->put(thisObject, exec, Identifier::from(exec, propertyName), value, slot);
436     }
437     
438     return thisObject->setIndex(exec, propertyName, value);
439 }
440
441 template<typename Adaptor>
442 bool JSGenericTypedArrayView<Adaptor>::deletePropertyByIndex(
443     JSCell* cell, ExecState* exec, unsigned propertyName)
444 {
445     return cell->methodTable()->deleteProperty(cell, exec, Identifier::from(exec, propertyName));
446 }
447
448 template<typename Adaptor>
449 void JSGenericTypedArrayView<Adaptor>::getOwnPropertyNames(
450     JSObject* object, ExecState* exec, PropertyNameArray& array, EnumerationMode mode)
451 {
452     JSGenericTypedArrayView* thisObject = jsCast<JSGenericTypedArrayView*>(object);
453
454     if (array.includeStringProperties()) {
455         for (unsigned i = 0; i < thisObject->m_length; ++i)
456             array.add(Identifier::from(exec, i));
457     }
458     
459     return Base::getOwnPropertyNames(object, exec, array, mode);
460 }
461
462 template<typename Adaptor>
463 size_t JSGenericTypedArrayView<Adaptor>::estimatedSize(JSCell* cell)
464 {
465     JSGenericTypedArrayView* thisObject = jsCast<JSGenericTypedArrayView*>(cell);
466
467     if (thisObject->m_mode == OversizeTypedArray)
468         return Base::estimatedSize(thisObject) + thisObject->byteSize();
469     if (thisObject->m_mode == FastTypedArray && thisObject->m_vector)
470         return Base::estimatedSize(thisObject) + thisObject->byteSize();
471
472     return Base::estimatedSize(thisObject);
473 }
474
475 template<typename Adaptor>
476 void JSGenericTypedArrayView<Adaptor>::visitChildren(JSCell* cell, SlotVisitor& visitor)
477 {
478     JSGenericTypedArrayView* thisObject = jsCast<JSGenericTypedArrayView*>(cell);
479     
480     switch (thisObject->m_mode) {
481     case FastTypedArray: {
482         if (thisObject->m_vector)
483             visitor.markAuxiliary(thisObject->m_vector.get());
484         break;
485     }
486         
487     case OversizeTypedArray: {
488         visitor.reportExtraMemoryVisited(thisObject->byteSize());
489         break;
490     }
491         
492     case WastefulTypedArray:
493         break;
494         
495     case DataViewMode:
496         RELEASE_ASSERT_NOT_REACHED();
497         break;
498     }
499     
500     Base::visitChildren(thisObject, visitor);
501 }
502
503 template<typename Adaptor>
504 ArrayBuffer* JSGenericTypedArrayView<Adaptor>::slowDownAndWasteMemory(JSArrayBufferView* object)
505 {
506     JSGenericTypedArrayView* thisObject = jsCast<JSGenericTypedArrayView*>(object);
507     
508     // We play this game because we want this to be callable even from places that
509     // don't have access to ExecState* or the VM, and we only allocate so little
510     // memory here that it's not necessary to trigger a GC - just accounting what
511     // we have done is good enough. The sort of bizarro exception to the "allocating
512     // little memory" is when we transfer a backing buffer into the C heap; this
513     // will temporarily get counted towards heap footprint (incorrectly, in the case
514     // of adopting an oversize typed array) but we don't GC here anyway. That's
515     // almost certainly fine. The worst case is if you created a ton of fast typed
516     // arrays, and did nothing but caused all of them to slow down and waste memory.
517     // In that case, your memory footprint will double before the GC realizes what's
518     // up. But if you do *anything* to trigger a GC watermark check, it will know
519     // that you *had* done those allocations and it will GC appropriately.
520     Heap* heap = Heap::heap(thisObject);
521     VM& vm = *heap->vm();
522     DeferGCForAWhile deferGC(*heap);
523     
524     ASSERT(!thisObject->hasIndexingHeader());
525
526     RELEASE_ASSERT(!thisObject->hasIndexingHeader());
527     thisObject->m_butterfly.set(vm, thisObject, Butterfly::createOrGrowArrayRight(
528         thisObject->butterfly(), vm, thisObject, thisObject->structure(),
529         thisObject->structure()->outOfLineCapacity(), false, 0, 0));
530
531     RefPtr<ArrayBuffer> buffer;
532     
533     switch (thisObject->m_mode) {
534     case FastTypedArray:
535         buffer = ArrayBuffer::create(thisObject->vector(), thisObject->byteLength());
536         break;
537         
538     case OversizeTypedArray:
539         // FIXME: consider doing something like "subtracting" from extra memory
540         // cost, since right now this case will cause the GC to think that we reallocated
541         // the whole buffer.
542         buffer = ArrayBuffer::createAdopted(thisObject->vector(), thisObject->byteLength());
543         break;
544         
545     default:
546         RELEASE_ASSERT_NOT_REACHED();
547         break;
548     }
549
550     thisObject->butterfly()->indexingHeader()->setArrayBuffer(buffer.get());
551     thisObject->m_vector.setWithoutBarrier(buffer->data());
552     thisObject->m_mode = WastefulTypedArray;
553     heap->addReference(thisObject, buffer.get());
554     
555     return buffer.get();
556 }
557
558 template<typename Adaptor>
559 PassRefPtr<ArrayBufferView>
560 JSGenericTypedArrayView<Adaptor>::getTypedArrayImpl(JSArrayBufferView* object)
561 {
562     JSGenericTypedArrayView* thisObject = jsCast<JSGenericTypedArrayView*>(object);
563     return thisObject->typedImpl();
564 }
565
566 } // namespace JSC