| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2009 Google Inc. All rights reserved. | 2 * Copyright (C) 2009 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ | 29 */ |
| 30 | 30 |
| 31 #include "config.h" | 31 #include "config.h" |
| 32 #include "bindings/v8/custom/V8ArrayBufferCustom.h" | 32 #include "bindings/v8/custom/V8ArrayBufferCustom.h" |
| 33 | 33 |
| 34 #include "bindings/v8/V8Binding.h" | |
| 35 #include "core/dom/ExceptionCode.h" | |
| 36 | |
| 37 #include "wtf/ArrayBuffer.h" | 34 #include "wtf/ArrayBuffer.h" |
| 38 #include "wtf/StdLibExtras.h" | 35 #include "wtf/StdLibExtras.h" |
| 39 | 36 |
| 37 #include "V8ArrayBuffer.h" |
| 38 #include "bindings/v8/V8Binding.h" |
| 39 #include "core/dom/ExceptionCode.h" |
| 40 |
| 40 namespace WebCore { | 41 namespace WebCore { |
| 41 | 42 |
| 42 using namespace WTF; | |
| 43 | |
| 44 V8ArrayBufferDeallocationObserver* V8ArrayBufferDeallocationObserver::instance() | 43 V8ArrayBufferDeallocationObserver* V8ArrayBufferDeallocationObserver::instance() |
| 45 { | 44 { |
| 46 DEFINE_STATIC_LOCAL(V8ArrayBufferDeallocationObserver, deallocationObserver,
()); | 45 DEFINE_STATIC_LOCAL(V8ArrayBufferDeallocationObserver, deallocationObserver,
()); |
| 47 return &deallocationObserver; | 46 return &deallocationObserver; |
| 48 } | 47 } |
| 49 | 48 |
| 50 WrapperTypeInfo V8ArrayBuffer::info = { | |
| 51 0, V8ArrayBuffer::derefObject, | |
| 52 0, 0, 0, 0, 0, WrapperTypeObjectPrototype | |
| 53 }; | |
| 54 | 49 |
| 55 bool V8ArrayBuffer::HasInstance(v8::Handle<v8::Value> value, v8::Isolate*, Wrapp
erWorldType) | 50 void V8ArrayBuffer::constructorCustom(const v8::FunctionCallbackInfo<v8::Value>&
args) |
| 56 { | 51 { |
| 57 return value->IsArrayBuffer(); | 52 // If we return a previously constructed ArrayBuffer, |
| 53 // e.g. from the call to ArrayBufferView.buffer, this code is called |
| 54 // with a zero-length argument list. The V8DOMWrapper will then |
| 55 // set the internal pointer in the newly-created object. |
| 56 // Unfortunately it doesn't look like it's possible to distinguish |
| 57 // between this case and that where the user calls "new |
| 58 // ArrayBuffer()" from JavaScript. To guard against problems, |
| 59 // we always create at least a zero-length ArrayBuffer, even |
| 60 // if it is immediately overwritten by the V8DOMWrapper. |
| 61 |
| 62 // Supported constructors: |
| 63 // ArrayBuffer(n) where n is an integer: |
| 64 // -- create an empty buffer of n bytes |
| 65 |
| 66 int argLength = args.Length(); |
| 67 int length = 0; |
| 68 if (argLength > 0) |
| 69 length = toInt32(args[0]); // NaN/+inf/-inf returns 0, this is intended
by WebIDL |
| 70 RefPtr<ArrayBuffer> buffer; |
| 71 if (length >= 0) |
| 72 buffer = ArrayBuffer::create(static_cast<unsigned>(length), 1); |
| 73 if (!buffer.get()) { |
| 74 throwError(v8RangeError, "ArrayBuffer size is not a small enough positiv
e integer.", args.GetIsolate()); |
| 75 return; |
| 76 } |
| 77 buffer->setDeallocationObserver(V8ArrayBufferDeallocationObserver::instance(
)); |
| 78 v8::V8::AdjustAmountOfExternalAllocatedMemory(buffer->byteLength()); |
| 79 // Transform the holder into a wrapper object for the array. |
| 80 v8::Handle<v8::Object> wrapper = args.Holder(); |
| 81 V8DOMWrapper::associateObjectWithWrapper(buffer.release(), &info, wrapper, a
rgs.GetIsolate(), WrapperConfiguration::Dependent); |
| 82 args.GetReturnValue().Set(wrapper); |
| 58 } | 83 } |
| 59 | 84 |
| 60 bool V8ArrayBuffer::HasInstanceInAnyWorld(v8::Handle<v8::Value> value, v8::Isola
te*) | |
| 61 { | |
| 62 return value->IsArrayBuffer(); | |
| 63 } | |
| 64 | |
| 65 void V8ArrayBuffer::derefObject(void* object) | |
| 66 { | |
| 67 static_cast<ArrayBuffer*>(object)->deref(); | |
| 68 } | |
| 69 | |
| 70 | |
| 71 v8::Handle<v8::Object> V8ArrayBuffer::createWrapper(PassRefPtr<ArrayBuffer> impl
, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate) | |
| 72 { | |
| 73 ASSERT(impl.get()); | |
| 74 ASSERT(DOMDataStore::getWrapper(impl.get(), isolate).IsEmpty()); | |
| 75 | |
| 76 v8::Handle<v8::Object> wrapper = v8::ArrayBuffer::New(impl->data(), impl->by
teLength()); | |
| 77 v8::V8::AdjustAmountOfExternalAllocatedMemory(impl->byteLength()); | |
| 78 impl->setDeallocationObserver(V8ArrayBufferDeallocationObserver::instance())
; | |
| 79 | |
| 80 V8DOMWrapper::associateObjectWithWrapper(impl, &info, wrapper, isolate, Wrap
perConfiguration::Independent); | |
| 81 return wrapper; | |
| 82 } | |
| 83 | |
| 84 ArrayBuffer* V8ArrayBuffer::toNative(v8::Handle<v8::Object> object) | |
| 85 { | |
| 86 ASSERT(object->IsArrayBuffer()); | |
| 87 void* arraybufferPtr = object->GetAlignedPointerFromInternalField(v8DOMWrapp
erObjectIndex); | |
| 88 if (arraybufferPtr) | |
| 89 return reinterpret_cast<ArrayBuffer*>(arraybufferPtr); | |
| 90 | |
| 91 v8::Local<v8::ArrayBuffer> v8buffer = object.As<v8::ArrayBuffer>(); | |
| 92 ASSERT(!v8buffer->IsExternal()); | |
| 93 | |
| 94 v8::ArrayBuffer::Contents v8Contents = v8buffer->Externalize(); | |
| 95 ArrayBufferContents contents(v8Contents.Data(), v8Contents.ByteLength()); | |
| 96 RefPtr<ArrayBuffer> buffer = ArrayBuffer::create(contents); | |
| 97 // V8 accounts for external memory even after externalizing the buffer. | |
| 98 buffer->setDeallocationObserver(V8ArrayBufferDeallocationObserver::instance(
)); | |
| 99 V8DOMWrapper::associateObjectWithWrapper(buffer.release(), &info, object, v8
::Isolate::GetCurrent(), WrapperConfiguration::Dependent); | |
| 100 | |
| 101 arraybufferPtr = object->GetAlignedPointerFromInternalField(v8DOMWrapperObje
ctIndex); | |
| 102 ASSERT(arraybufferPtr); | |
| 103 return reinterpret_cast<ArrayBuffer*>(arraybufferPtr); | |
| 104 } | |
| 105 | |
| 106 | |
| 107 } // namespace WebCore | 85 } // namespace WebCore |
| OLD | NEW |