2 * Copyright (C) 2009 Google Inc. All rights reserved.
3 * Copyright (C) 2012 Ericsson AB. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include "bindings/v8/DOMWrapperWorld.h"
36 #include "bindings/v8/ExceptionMessages.h"
37 #include "bindings/v8/V8BindingMacros.h"
38 #include "bindings/v8/V8PerIsolateData.h"
39 #include "bindings/v8/V8StringResource.h"
40 #include "bindings/v8/V8ThrowException.h"
41 #include "bindings/v8/V8ValueCache.h"
42 #include "heap/Heap.h"
43 #include "wtf/MathExtras.h"
44 #include "wtf/text/AtomicString.h"
55 class ExecutionContext;
56 class ScriptWrappable;
57 class XPathNSResolver;
59 const int kMaxRecursionDepth = 22;
61 // Schedule a DOM exception to be thrown, if the exception code is different
63 v8::Handle<v8::Value> setDOMException(int, v8::Isolate*);
64 v8::Handle<v8::Value> setDOMException(int, const String&, v8::Isolate*);
66 // Schedule a JavaScript error to be thrown.
67 v8::Handle<v8::Value> throwError(V8ErrorType, const String&, v8::Isolate*);
69 // Schedule a JavaScript error to be thrown.
70 v8::Handle<v8::Value> throwError(v8::Handle<v8::Value>, v8::Isolate*);
72 // A helper for throwing JavaScript TypeError.
73 v8::Handle<v8::Value> throwTypeError(const String&, v8::Isolate*);
75 v8::ArrayBuffer::Allocator* v8ArrayBufferAllocator();
77 inline v8::Handle<v8::Value> argumentOrNull(const v8::FunctionCallbackInfo<v8::Value>& info, int index)
79 return index >= info.Length() ? v8::Local<v8::Value>() : info[index];
82 template<typename CallbackInfo, typename V>
83 inline void v8SetReturnValue(const CallbackInfo& info, V v)
85 info.GetReturnValue().Set(v);
88 template<typename CallbackInfo>
89 inline void v8SetReturnValueBool(const CallbackInfo& info, bool v)
91 info.GetReturnValue().Set(v);
94 template<typename CallbackInfo>
95 inline void v8SetReturnValueInt(const CallbackInfo& info, int v)
97 info.GetReturnValue().Set(v);
100 template<typename CallbackInfo>
101 inline void v8SetReturnValueUnsigned(const CallbackInfo& info, unsigned v)
103 info.GetReturnValue().Set(v);
106 template<typename CallbackInfo>
107 inline void v8SetReturnValueNull(const CallbackInfo& info)
109 info.GetReturnValue().SetNull();
112 template<typename CallbackInfo>
113 inline void v8SetReturnValueUndefined(const CallbackInfo& info)
115 info.GetReturnValue().SetUndefined();
118 template<typename CallbackInfo>
119 inline void v8SetReturnValueEmptyString(const CallbackInfo& info)
121 info.GetReturnValue().SetEmptyString();
124 template <class CallbackInfo>
125 inline void v8SetReturnValueString(const CallbackInfo& info, const String& string, v8::Isolate* isolate)
127 if (string.isNull()) {
128 v8SetReturnValueEmptyString(info);
131 V8PerIsolateData::from(isolate)->stringCache()->setReturnValueFromString(info.GetReturnValue(), string.impl());
134 template <class CallbackInfo>
135 inline void v8SetReturnValueStringOrNull(const CallbackInfo& info, const String& string, v8::Isolate* isolate)
137 if (string.isNull()) {
138 v8SetReturnValueNull(info);
141 V8PerIsolateData::from(isolate)->stringCache()->setReturnValueFromString(info.GetReturnValue(), string.impl());
144 template <class CallbackInfo>
145 inline void v8SetReturnValueStringOrUndefined(const CallbackInfo& info, const String& string, v8::Isolate* isolate)
147 if (string.isNull()) {
148 v8SetReturnValueUndefined(info);
151 V8PerIsolateData::from(isolate)->stringCache()->setReturnValueFromString(info.GetReturnValue(), string.impl());
154 // Convert v8::String to a WTF::String. If the V8 string is not already
155 // an external string then it is transformed into an external string at this
156 // point to avoid repeated conversions.
157 inline String toCoreString(v8::Handle<v8::String> value)
159 return v8StringToWebCoreString<String>(value, Externalize);
162 inline String toCoreStringWithNullCheck(v8::Handle<v8::String> value)
164 if (value.IsEmpty() || value->IsNull())
166 return toCoreString(value);
169 inline String toCoreStringWithUndefinedOrNullCheck(v8::Handle<v8::String> value)
171 if (value.IsEmpty() || value->IsNull() || value->IsUndefined())
173 return toCoreString(value);
176 inline AtomicString toCoreAtomicString(v8::Handle<v8::String> value)
178 return v8StringToWebCoreString<AtomicString>(value, Externalize);
181 // This method will return a null String if the v8::Value does not contain a v8::String.
182 // It will not call ToString() on the v8::Value. If you want ToString() to be called,
183 // please use the V8TRYCATCH_FOR_V8STRINGRESOURCE_*() macros instead.
184 inline String toCoreStringWithUndefinedOrNullCheck(v8::Handle<v8::Value> value)
186 if (value.IsEmpty() || !value->IsString())
189 return toCoreString(value.As<v8::String>());
192 // Convert a string to a V8 string.
193 // Return a V8 external string that shares the underlying buffer with the given
194 // WebCore string. The reference counting mechanism is used to keep the
195 // underlying buffer alive while the string is still live in the V8 engine.
196 inline v8::Handle<v8::String> v8String(v8::Isolate* isolate, const String& string)
199 return v8::String::Empty(isolate);
200 return V8PerIsolateData::from(isolate)->stringCache()->v8ExternalString(string.impl(), isolate);
203 inline v8::Handle<v8::String> v8AtomicString(v8::Isolate* isolate, const char* str)
206 return v8::String::NewFromUtf8(isolate, str, v8::String::kInternalizedString, strlen(str));
209 inline v8::Handle<v8::String> v8AtomicString(v8::Isolate* isolate, const char* str, size_t length)
212 return v8::String::NewFromUtf8(isolate, str, v8::String::kInternalizedString, length);
215 inline v8::Handle<v8::Value> v8Undefined()
217 return v8::Handle<v8::Value>();
221 struct V8ValueTraits {
222 static inline v8::Handle<v8::Value> arrayV8Value(const T& value, v8::Isolate* isolate)
224 return toV8(WTF::getPtr(value), v8::Handle<v8::Object>(), isolate);
229 struct V8ValueTraits<String> {
230 static inline v8::Handle<v8::Value> arrayV8Value(const String& value, v8::Isolate* isolate)
232 return v8String(isolate, value);
237 struct V8ValueTraits<AtomicString> {
238 static inline v8::Handle<v8::Value> arrayV8Value(const AtomicString& value, v8::Isolate* isolate)
240 return v8String(isolate, value);
245 struct V8ValueTraits<unsigned> {
246 static inline v8::Handle<v8::Value> arrayV8Value(const unsigned& value, v8::Isolate* isolate)
248 return v8::Integer::NewFromUnsigned(isolate, value);
253 struct V8ValueTraits<unsigned long> {
254 static inline v8::Handle<v8::Value> arrayV8Value(const unsigned long& value, v8::Isolate* isolate)
256 return v8::Integer::NewFromUnsigned(isolate, value);
261 struct V8ValueTraits<float> {
262 static inline v8::Handle<v8::Value> arrayV8Value(const float& value, v8::Isolate* isolate)
264 return v8::Number::New(isolate, value);
269 struct V8ValueTraits<double> {
270 static inline v8::Handle<v8::Value> arrayV8Value(const double& value, v8::Isolate* isolate)
272 return v8::Number::New(isolate, value);
276 template<typename T, size_t inlineCapacity>
277 v8::Handle<v8::Value> v8Array(const Vector<T, inlineCapacity>& iterator, v8::Isolate* isolate)
279 v8::Local<v8::Array> result = v8::Array::New(isolate, iterator.size());
281 typename Vector<T, inlineCapacity>::const_iterator end = iterator.end();
282 typedef V8ValueTraits<T> TraitsType;
283 for (typename Vector<T, inlineCapacity>::const_iterator iter = iterator.begin(); iter != end; ++iter)
284 result->Set(v8::Integer::New(isolate, index++), TraitsType::arrayV8Value(*iter, isolate));
288 template<typename T, size_t inlineCapacity>
289 v8::Handle<v8::Value> v8Array(const HeapVector<T, inlineCapacity>& iterator, v8::Isolate* isolate)
291 v8::Local<v8::Array> result = v8::Array::New(isolate, iterator.size());
293 typename HeapVector<T, inlineCapacity>::const_iterator end = iterator.end();
294 typedef V8ValueTraits<T> TraitsType;
295 for (typename HeapVector<T, inlineCapacity>::const_iterator iter = iterator.begin(); iter != end; ++iter)
296 result->Set(v8::Integer::New(isolate, index++), TraitsType::arrayV8Value(*iter, isolate));
300 // Conversion flags, used in toIntXX/toUIntXX.
301 enum IntegerConversionConfiguration {
307 // Convert a value to a 8-bit signed integer. The conversion fails if the
308 // value cannot be converted to a number or the range violated per WebIDL:
309 // http://www.w3.org/TR/WebIDL/#es-byte
310 int8_t toInt8(v8::Handle<v8::Value>, IntegerConversionConfiguration, ExceptionState&);
311 inline int8_t toInt8(v8::Handle<v8::Value> value, ExceptionState& exceptionState)
313 return toInt8(value, NormalConversion, exceptionState);
316 // Convert a value to a 8-bit integer assuming the conversion cannot fail.
317 int8_t toInt8(v8::Handle<v8::Value>);
319 // Convert a value to a 8-bit unsigned integer. The conversion fails if the
320 // value cannot be converted to a number or the range violated per WebIDL:
321 // http://www.w3.org/TR/WebIDL/#es-octet
322 uint8_t toUInt8(v8::Handle<v8::Value>, IntegerConversionConfiguration, ExceptionState&);
323 inline uint8_t toUInt8(v8::Handle<v8::Value> value, ExceptionState& exceptionState)
325 return toUInt8(value, NormalConversion, exceptionState);
328 // Convert a value to a 8-bit unsigned integer assuming the conversion cannot fail.
329 uint8_t toUInt8(v8::Handle<v8::Value>);
331 // Convert a value to a 16-bit signed integer. The conversion fails if the
332 // value cannot be converted to a number or the range violated per WebIDL:
333 // http://www.w3.org/TR/WebIDL/#es-short
334 int16_t toInt16(v8::Handle<v8::Value>, IntegerConversionConfiguration, ExceptionState&);
335 inline int16_t toInt16(v8::Handle<v8::Value> value, ExceptionState& exceptionState)
337 return toInt16(value, NormalConversion, exceptionState);
340 // Convert a value to a 16-bit integer assuming the conversion cannot fail.
341 int16_t toInt16(v8::Handle<v8::Value>);
343 // Convert a value to a 16-bit unsigned integer. The conversion fails if the
344 // value cannot be converted to a number or the range violated per WebIDL:
345 // http://www.w3.org/TR/WebIDL/#es-unsigned-short
346 uint16_t toUInt16(v8::Handle<v8::Value>, IntegerConversionConfiguration, ExceptionState&);
347 inline uint16_t toUInt16(v8::Handle<v8::Value> value, ExceptionState& exceptionState)
349 return toUInt16(value, NormalConversion, exceptionState);
352 // Convert a value to a 16-bit unsigned integer assuming the conversion cannot fail.
353 uint16_t toUInt16(v8::Handle<v8::Value>);
355 // Convert a value to a 32-bit signed integer. The conversion fails if the
356 // value cannot be converted to a number or the range violated per WebIDL:
357 // http://www.w3.org/TR/WebIDL/#es-long
358 int32_t toInt32(v8::Handle<v8::Value>, IntegerConversionConfiguration, ExceptionState&);
359 inline int32_t toInt32(v8::Handle<v8::Value> value, ExceptionState& exceptionState)
361 return toInt32(value, NormalConversion, exceptionState);
364 // Convert a value to a 32-bit integer assuming the conversion cannot fail.
365 int32_t toInt32(v8::Handle<v8::Value>);
367 // Convert a value to a 32-bit unsigned integer. The conversion fails if the
368 // value cannot be converted to a number or the range violated per WebIDL:
369 // http://www.w3.org/TR/WebIDL/#es-unsigned-long
370 uint32_t toUInt32(v8::Handle<v8::Value>, IntegerConversionConfiguration, ExceptionState&);
371 inline uint32_t toUInt32(v8::Handle<v8::Value> value, ExceptionState& exceptionState)
373 return toUInt32(value, NormalConversion, exceptionState);
376 // Convert a value to a 32-bit unsigned integer assuming the conversion cannot fail.
377 uint32_t toUInt32(v8::Handle<v8::Value>);
379 // Convert a value to a 64-bit signed integer. The conversion fails if the
380 // value cannot be converted to a number or the range violated per WebIDL:
381 // http://www.w3.org/TR/WebIDL/#es-long-long
382 int64_t toInt64(v8::Handle<v8::Value>, IntegerConversionConfiguration, ExceptionState&);
383 inline int64_t toInt64(v8::Handle<v8::Value> value, ExceptionState& exceptionState)
385 return toInt64(value, NormalConversion, exceptionState);
388 // Convert a value to a 64-bit integer assuming the conversion cannot fail.
389 int64_t toInt64(v8::Handle<v8::Value>);
391 // Convert a value to a 64-bit unsigned integer. The conversion fails if the
392 // value cannot be converted to a number or the range violated per WebIDL:
393 // http://www.w3.org/TR/WebIDL/#es-unsigned-long-long
394 uint64_t toUInt64(v8::Handle<v8::Value>, IntegerConversionConfiguration, ExceptionState&);
395 inline uint64_t toUInt64(v8::Handle<v8::Value> value, ExceptionState& exceptionState)
397 return toUInt64(value, NormalConversion, exceptionState);
400 // Convert a value to a 64-bit unsigned integer assuming the conversion cannot fail.
401 uint64_t toUInt64(v8::Handle<v8::Value>);
403 // Convert a value to a single precision float, which might fail.
404 float toFloat(v8::Handle<v8::Value>, ExceptionState&);
406 // Convert a value to a single precision float assuming the conversion cannot fail.
407 inline float toFloat(v8::Local<v8::Value> value)
409 return static_cast<float>(value->NumberValue());
412 inline v8::Handle<v8::Boolean> v8Boolean(bool value, v8::Isolate* isolate)
414 return value ? v8::True(isolate) : v8::False(isolate);
417 inline double toCoreDate(v8::Handle<v8::Value> object)
419 if (object->IsDate())
420 return v8::Handle<v8::Date>::Cast(object)->ValueOf();
421 if (object->IsNumber())
422 return object->NumberValue();
423 return std::numeric_limits<double>::quiet_NaN();
426 inline v8::Handle<v8::Value> v8DateOrNull(double value, v8::Isolate* isolate)
429 return std::isfinite(value) ? v8::Date::New(isolate, value) : v8::Handle<v8::Value>::Cast(v8::Null(isolate));
432 // FIXME: Remove the special casing for NodeFilter and XPathNSResolver.
433 PassRefPtr<NodeFilter> toNodeFilter(v8::Handle<v8::Value>, v8::Isolate*);
434 PassRefPtr<XPathNSResolver> toXPathNSResolver(v8::Handle<v8::Value>, v8::Isolate*);
436 template<class T> struct NativeValueTraits;
439 struct NativeValueTraits<String> {
440 static inline String nativeValue(const v8::Handle<v8::Value>& value, v8::Isolate* isolate)
442 V8TRYCATCH_FOR_V8STRINGRESOURCE_RETURN(V8StringResource<>, stringValue, value, String());
448 struct NativeValueTraits<unsigned> {
449 static inline unsigned nativeValue(const v8::Handle<v8::Value>& value, v8::Isolate* isolate)
451 return toUInt32(value);
456 struct NativeValueTraits<float> {
457 static inline float nativeValue(const v8::Handle<v8::Value>& value, v8::Isolate* isolate)
459 return static_cast<float>(value->NumberValue());
464 struct NativeValueTraits<double> {
465 static inline double nativeValue(const v8::Handle<v8::Value>& value, v8::Isolate* isolate)
467 return static_cast<double>(value->NumberValue());
472 struct NativeValueTraits<v8::Handle<v8::Value> > {
473 static inline v8::Handle<v8::Value> nativeValue(const v8::Handle<v8::Value>& value, v8::Isolate* isolate)
479 // Converts a JavaScript value to an array as per the Web IDL specification:
480 // http://www.w3.org/TR/2012/CR-WebIDL-20120419/#es-array
481 template <class T, class V8T>
482 Vector<RefPtr<T> > toRefPtrNativeArrayUnchecked(v8::Local<v8::Value> v8Value, uint32_t length, v8::Isolate* isolate, bool* success = 0)
484 Vector<RefPtr<T> > result;
485 result.reserveInitialCapacity(length);
486 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(v8Value);
487 for (uint32_t i = 0; i < length; ++i) {
488 v8::Handle<v8::Value> element = object->Get(i);
490 if (V8T::hasInstance(element, isolate)) {
491 v8::Handle<v8::Object> elementObject = v8::Handle<v8::Object>::Cast(element);
492 result.uncheckedAppend(V8T::toNative(elementObject));
496 throwTypeError("Invalid Array element type", isolate);
497 return Vector<RefPtr<T> >();
503 v8::Handle<v8::Value> toV8Sequence(v8::Handle<v8::Value>, uint32_t& length, v8::Isolate*);
505 template <class T, class V8T>
506 Vector<RefPtr<T> > toRefPtrNativeArray(v8::Handle<v8::Value> value, int argumentIndex, v8::Isolate* isolate, bool* success = 0)
511 v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(isolate, value));
513 if (value->IsArray()) {
514 length = v8::Local<v8::Array>::Cast(v8Value)->Length();
515 } else if (toV8Sequence(value, length, isolate).IsEmpty()) {
516 throwTypeError(ExceptionMessages::notAnArrayTypeArgumentOrValue(argumentIndex), isolate);
517 return Vector<RefPtr<T> >();
520 return toRefPtrNativeArrayUnchecked<T, V8T>(v8Value, length, isolate, success);
523 template <class T, class V8T>
524 Vector<RefPtr<T> > toRefPtrNativeArray(v8::Handle<v8::Value> value, const String& propertyName, v8::Isolate* isolate, bool* success = 0)
529 v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(isolate, value));
531 if (value->IsArray()) {
532 length = v8::Local<v8::Array>::Cast(v8Value)->Length();
533 } else if (toV8Sequence(value, length, isolate).IsEmpty()) {
534 throwTypeError(ExceptionMessages::notASequenceTypeProperty(propertyName), isolate);
535 return Vector<RefPtr<T> >();
538 return toRefPtrNativeArrayUnchecked<T, V8T>(v8Value, length, isolate, success);
541 // Converts a JavaScript value to an array as per the Web IDL specification:
542 // http://www.w3.org/TR/2012/CR-WebIDL-20120419/#es-array
544 Vector<T> toNativeArray(v8::Handle<v8::Value> value, int argumentIndex, v8::Isolate* isolate)
546 v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(isolate, value));
548 if (value->IsArray()) {
549 length = v8::Local<v8::Array>::Cast(v8Value)->Length();
550 } else if (toV8Sequence(value, length, isolate).IsEmpty()) {
551 throwTypeError(ExceptionMessages::notAnArrayTypeArgumentOrValue(argumentIndex), isolate);
556 result.reserveInitialCapacity(length);
557 typedef NativeValueTraits<T> TraitsType;
558 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(v8Value);
559 for (uint32_t i = 0; i < length; ++i)
560 result.uncheckedAppend(TraitsType::nativeValue(object->Get(i), isolate));
565 Vector<T> toNativeArguments(const v8::FunctionCallbackInfo<v8::Value>& info, int startIndex)
567 ASSERT(startIndex <= info.Length());
569 typedef NativeValueTraits<T> TraitsType;
570 int length = info.Length();
571 result.reserveInitialCapacity(length);
572 for (int i = startIndex; i < length; ++i)
573 result.uncheckedAppend(TraitsType::nativeValue(info[i], info.GetIsolate()));
577 // Validates that the passed object is a sequence type per WebIDL spec
578 // http://www.w3.org/TR/2012/CR-WebIDL-20120419/#es-sequence
579 inline v8::Handle<v8::Value> toV8Sequence(v8::Handle<v8::Value> value, uint32_t& length, v8::Isolate* isolate)
581 // Attempt converting to a sequence if the value is not already an array but is
582 // any kind of object except for a native Date object or a native RegExp object.
583 ASSERT(!value->IsArray());
584 // FIXME: Do we really need to special case Date and RegExp object?
585 // https://www.w3.org/Bugs/Public/show_bug.cgi?id=22806
586 if (!value->IsObject() || value->IsDate() || value->IsRegExp()) {
587 // The caller is responsible for reporting a TypeError.
588 return v8Undefined();
591 v8::Local<v8::Value> v8Value(v8::Local<v8::Value>::New(isolate, value));
592 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(v8Value);
593 v8::Local<v8::String> lengthSymbol = v8AtomicString(isolate, "length");
595 // FIXME: The specification states that the length property should be used as fallback, if value
596 // is not a platform object that supports indexed properties. If it supports indexed properties,
597 // length should actually be one greater than value’s maximum indexed property index.
598 V8TRYCATCH(v8::Local<v8::Value>, lengthValue, object->Get(lengthSymbol));
600 if (lengthValue->IsUndefined() || lengthValue->IsNull()) {
601 // The caller is responsible for reporting a TypeError.
602 return v8Undefined();
605 V8TRYCATCH(uint32_t, sequenceLength, lengthValue->Int32Value());
606 length = sequenceLength;
611 v8::Isolate* toIsolate(ExecutionContext*);
612 v8::Isolate* toIsolate(Frame*);
614 WrapperWorldType worldType(v8::Isolate*);
615 WrapperWorldType worldTypeInMainThread(v8::Isolate*);
617 DOMWindow* toDOMWindow(v8::Handle<v8::Value>, v8::Isolate*);
618 DOMWindow* toDOMWindow(v8::Handle<v8::Context>);
619 ExecutionContext* toExecutionContext(v8::Handle<v8::Context>);
621 DOMWindow* activeDOMWindow(v8::Isolate*);
622 ExecutionContext* activeExecutionContext(v8::Isolate*);
623 DOMWindow* firstDOMWindow(v8::Isolate*);
624 Document* currentDocument(v8::Isolate*);
625 ExecutionContext* currentExecutionContext(v8::Isolate*);
627 // Returns a V8 context associated with a ExecutionContext and a DOMWrapperWorld.
628 // This method returns an empty context if there is no frame or the frame is already detached.
629 v8::Local<v8::Context> toV8Context(ExecutionContext*, DOMWrapperWorld*);
630 // Returns a V8 context associated with a Frame and a DOMWrapperWorld.
631 // This method returns an empty context if the frame is already detached.
632 v8::Local<v8::Context> toV8Context(v8::Isolate*, Frame*, DOMWrapperWorld*);
634 // Returns the frame object of the window object associated with
635 // a context, if the window is currently being displayed in the Frame.
636 Frame* toFrameIfNotDetached(v8::Handle<v8::Context>);
638 // If the current context causes out of memory, JavaScript setting
639 // is disabled and it returns true.
640 bool handleOutOfMemory();
641 v8::Local<v8::Value> handleMaxRecursionDepthExceeded(v8::Isolate*);
642 void crashIfV8IsDead();
644 inline bool isUndefinedOrNull(v8::Handle<v8::Value> value)
646 return value->IsNull() || value->IsUndefined();
648 v8::Handle<v8::Function> getBoundFunction(v8::Handle<v8::Function>);
650 // Attaches |environment| to |function| and returns it.
651 inline v8::Local<v8::Function> createClosure(v8::FunctionCallback function, v8::Handle<v8::Value> environment, v8::Isolate* isolate)
653 return v8::Function::New(isolate, function, environment);
656 // FIXME: This will be soon embedded in the generated code.
657 template<class Collection> static void indexedPropertyEnumerator(const v8::PropertyCallbackInfo<v8::Array>& info)
659 Collection* collection = reinterpret_cast<Collection*>(info.Holder()->GetAlignedPointerFromInternalField(v8DOMWrapperObjectIndex));
660 int length = collection->length();
661 v8::Handle<v8::Array> properties = v8::Array::New(info.GetIsolate(), length);
662 for (int i = 0; i < length; ++i) {
663 // FIXME: Do we need to check that the item function returns a non-null value for this index?
664 v8::Handle<v8::Integer> integer = v8::Integer::New(info.GetIsolate(), i);
665 properties->Set(integer, integer);
667 v8SetReturnValue(info, properties);
670 v8::Local<v8::Value> getHiddenValue(v8::Isolate*, v8::Handle<v8::Object>, const char*);
671 v8::Local<v8::Value> getHiddenValue(v8::Isolate*, v8::Handle<v8::Object>, v8::Handle<v8::String>);
672 bool setHiddenValue(v8::Isolate*, v8::Handle<v8::Object>, const char*, v8::Handle<v8::Value>);
673 bool setHiddenValue(v8::Isolate*, v8::Handle<v8::Object>, v8::Handle<v8::String>, v8::Handle<v8::Value>);
674 bool deleteHiddenValue(v8::Isolate*, v8::Handle<v8::Object>, const char*);
675 bool deleteHiddenValue(v8::Isolate*, v8::Handle<v8::Object>, v8::Handle<v8::String>);
676 v8::Local<v8::Value> getHiddenValueFromMainWorldWrapper(v8::Isolate*, ScriptWrappable*, const char*);
677 v8::Local<v8::Value> getHiddenValueFromMainWorldWrapper(v8::Isolate*, ScriptWrappable*, v8::Handle<v8::String>);
679 // These methods store hidden values into an array that is stored in the internal field of a DOM wrapper.
680 void addHiddenValueToArray(v8::Handle<v8::Object>, v8::Local<v8::Value>, int cacheIndex, v8::Isolate*);
681 void removeHiddenValueFromArray(v8::Handle<v8::Object>, v8::Local<v8::Value>, int cacheIndex, v8::Isolate*);
682 void moveEventListenerToNewWrapper(v8::Handle<v8::Object>, EventListener* oldValue, v8::Local<v8::Value> newValue, int cacheIndex, v8::Isolate*);
684 // Converts a DOM object to a v8 value.
685 // This is a no-inline version of toV8(). If you want to call toV8()
686 // without creating #include cycles, you can use this function instead.
687 // Each specialized implementation will be generated.
689 v8::Handle<v8::Value> toV8NoInline(T* impl, v8::Handle<v8::Object> creationContext, v8::Isolate*);
691 // Result values for platform object 'deleter' methods,
692 // http://www.w3.org/TR/WebIDL/#delete
696 DeleteUnknownProperty
699 class V8IsolateInterruptor : public ThreadState::Interruptor {
701 explicit V8IsolateInterruptor(v8::Isolate* isolate) : m_isolate(isolate) { }
703 static void onInterruptCallback(v8::Isolate* isolate, void* data)
705 reinterpret_cast<V8IsolateInterruptor*>(data)->onInterrupted();
708 virtual void requestInterrupt() OVERRIDE
710 m_isolate->RequestInterrupt(&onInterruptCallback, this);
713 virtual void clearInterrupt() OVERRIDE
715 m_isolate->ClearInterrupt();
719 v8::Isolate* m_isolate;
722 } // namespace WebCore
724 #endif // V8Binding_h