2 * Copyright (C) 2009 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "bindings/v8/custom/V8ArrayBufferCustom.h"
34 #include "bindings/v8/V8Binding.h"
35 #include "wtf/ArrayBuffer.h"
36 #include "wtf/StdLibExtras.h"
42 V8ArrayBufferDeallocationObserver* V8ArrayBufferDeallocationObserver::instanceTemplate()
44 DEFINE_STATIC_LOCAL(V8ArrayBufferDeallocationObserver, deallocationObserver, ());
45 return &deallocationObserver;
48 const WrapperTypeInfo V8ArrayBuffer::wrapperTypeInfo = {
50 0, V8ArrayBuffer::derefObject,
51 0, 0, 0, 0, 0, WrapperTypeObjectPrototype, false
54 bool V8ArrayBuffer::hasInstance(v8::Handle<v8::Value> value, v8::Isolate*)
56 return value->IsArrayBuffer();
59 void V8ArrayBuffer::derefObject(void* object)
61 static_cast<ArrayBuffer*>(object)->deref();
64 v8::Handle<v8::Object> V8ArrayBuffer::createWrapper(PassRefPtr<ArrayBuffer> impl, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
67 ASSERT(!DOMDataStore::containsWrapper<V8ArrayBuffer>(impl.get(), isolate));
69 v8::Handle<v8::Object> wrapper = v8::ArrayBuffer::New(isolate, impl->data(), impl->byteLength());
70 impl->setDeallocationObserver(V8ArrayBufferDeallocationObserver::instanceTemplate());
72 V8DOMWrapper::associateObjectWithWrapper<V8ArrayBuffer>(impl, &wrapperTypeInfo, wrapper, isolate, WrapperConfiguration::Independent);
76 ArrayBuffer* V8ArrayBuffer::toNative(v8::Handle<v8::Object> object)
78 ASSERT(object->IsArrayBuffer());
79 void* arraybufferPtr = object->GetAlignedPointerFromInternalField(v8DOMWrapperObjectIndex);
81 return reinterpret_cast<ArrayBuffer*>(arraybufferPtr);
83 v8::Local<v8::ArrayBuffer> v8buffer = object.As<v8::ArrayBuffer>();
84 ASSERT(!v8buffer->IsExternal());
86 v8::ArrayBuffer::Contents v8Contents = v8buffer->Externalize();
87 ArrayBufferContents contents(v8Contents.Data(), v8Contents.ByteLength(),
88 V8ArrayBufferDeallocationObserver::instanceTemplate());
89 RefPtr<ArrayBuffer> buffer = ArrayBuffer::create(contents);
90 V8DOMWrapper::associateObjectWithWrapper<V8ArrayBuffer>(buffer.release(), &wrapperTypeInfo, object, v8::Isolate::GetCurrent(), WrapperConfiguration::Dependent);
92 arraybufferPtr = object->GetAlignedPointerFromInternalField(v8DOMWrapperObjectIndex);
93 ASSERT(arraybufferPtr);
94 return reinterpret_cast<ArrayBuffer*>(arraybufferPtr);
98 v8::Handle<v8::Value> toV8NoInline(ArrayBuffer* impl, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
100 return toV8(impl, creationContext, isolate);
103 } // namespace WebCore