| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef V8_BINDING_H__ | |
| 6 #define V8_BINDING_H__ | |
| 7 | |
| 8 #include "config.h" | |
| 9 | |
| 10 #include "MathExtras.h" | |
| 11 #include "PlatformString.h" | |
| 12 | |
| 13 #include <v8.h> | |
| 14 | |
| 15 namespace WebCore { | |
| 16 | |
| 17 // The string returned by this function is still owned by the argument | |
| 18 // and will be deallocated when the argument is deallocated. | |
| 19 inline const uint16_t* FromWebCoreString(const String& str) { | |
| 20 return reinterpret_cast<const uint16_t*>(str.characters()); | |
| 21 } | |
| 22 | |
| 23 // Convert v8 types to a WebCore::String. If the V8 string is not already | |
| 24 // an external string then it is transformed into an external string at this | |
| 25 // point to avoid repeated conversions. | |
| 26 String v8StringToWebCoreString( | |
| 27 v8::Handle<v8::String> obj, bool externalize); | |
| 28 String v8ValueToWebCoreString(v8::Handle<v8::Value> obj); | |
| 29 | |
| 30 // TODO(mbelshe): drop this in favor of the type specific | |
| 31 // v8ValueToWebCoreString when we rework the code generation. | |
| 32 inline String ToWebCoreString(v8::Handle<v8::Value> obj) { | |
| 33 return v8ValueToWebCoreString(obj); | |
| 34 } | |
| 35 | |
| 36 inline String ToWebCoreString(v8::Handle<v8::String> string) { | |
| 37 return v8StringToWebCoreString(string, true); | |
| 38 } | |
| 39 | |
| 40 // Convert v8 types to a WebCore::AtomicString. | |
| 41 AtomicString v8StringToAtomicWebCoreString(v8::Handle<v8::String> obj); | |
| 42 AtomicString v8ValueToAtomicWebCoreString(v8::Handle<v8::Value> obj); | |
| 43 | |
| 44 inline String valueToStringWithNullCheck(v8::Handle<v8::Value> value) { | |
| 45 if (value->IsNull()) return String(); | |
| 46 return ToWebCoreString(value); | |
| 47 } | |
| 48 | |
| 49 inline String valueToStringWithNullOrUndefinedCheck( | |
| 50 v8::Handle<v8::Value> value) { | |
| 51 if (value->IsNull() || value->IsUndefined()) return String(); | |
| 52 return ToWebCoreString(value); | |
| 53 } | |
| 54 | |
| 55 // Convert a value to a 32-bit integer. The conversion fails if the | |
| 56 // value cannot be converted to an integer or converts to nan or to an | |
| 57 // infinity. | |
| 58 // FIXME: Rename to toInt32() once V8 bindings migration is complete. | |
| 59 inline int ToInt32(v8::Handle<v8::Value> value, bool& ok) { | |
| 60 ok = true; | |
| 61 | |
| 62 // Fast case. The value is already a 32-bit integer. | |
| 63 if (value->IsInt32()) { | |
| 64 return value->Int32Value(); | |
| 65 } | |
| 66 | |
| 67 // Can the value be converted to a number? | |
| 68 v8::Local<v8::Number> number_object = value->ToNumber(); | |
| 69 if (number_object.IsEmpty()) { | |
| 70 ok = false; | |
| 71 return 0; | |
| 72 } | |
| 73 | |
| 74 // Does the value convert to nan or to an infinity? | |
| 75 double number_value = number_object->Value(); | |
| 76 if (isnan(number_value) || isinf(number_value)) { | |
| 77 ok = false; | |
| 78 return 0; | |
| 79 } | |
| 80 | |
| 81 // Can the value be converted to a 32-bit integer? | |
| 82 v8::Local<v8::Int32> int_value = value->ToInt32(); | |
| 83 if (int_value.IsEmpty()) { | |
| 84 ok = false; | |
| 85 return 0; | |
| 86 } | |
| 87 | |
| 88 // Return the result of the int32 conversion. | |
| 89 return int_value->Value(); | |
| 90 } | |
| 91 | |
| 92 // Convert a value to a 32-bit integer assuming the conversion cannot fail. | |
| 93 // FIXME: Rename to toInt32() once V8 bindings migration is complete. | |
| 94 inline int ToInt32(v8::Handle<v8::Value> value) { | |
| 95 bool ok; | |
| 96 return ToInt32(value, ok); | |
| 97 } | |
| 98 | |
| 99 inline String ToString(const String& string) { | |
| 100 return string; | |
| 101 } | |
| 102 | |
| 103 // Convert a string to a V8 string. | |
| 104 v8::Handle<v8::String> v8String(const String& str); | |
| 105 | |
| 106 inline v8::Handle<v8::String> v8UndetectableString(const String& str) { | |
| 107 return v8::String::NewUndetectable(FromWebCoreString(str), str.length()); | |
| 108 } | |
| 109 | |
| 110 // Return a V8 external string that shares the underlying buffer with the given | |
| 111 // WebCore string. The reference counting mechanism is used to keep the | |
| 112 // underlying buffer alive while the string is still live in the V8 engine. | |
| 113 v8::Local<v8::String> v8ExternalString(const String& str); | |
| 114 | |
| 115 inline v8::Handle<v8::Value> v8StringOrNull(const String& str) { | |
| 116 return str.isNull() | |
| 117 ? v8::Handle<v8::Value>(v8::Null()) | |
| 118 : v8::Handle<v8::Value>(v8String(str)); | |
| 119 } | |
| 120 | |
| 121 inline v8::Handle<v8::Value> v8StringOrUndefined(const String& str) { | |
| 122 return str.isNull() | |
| 123 ? v8::Handle<v8::Value>(v8::Undefined()) | |
| 124 : v8::Handle<v8::Value>(v8String(str)); | |
| 125 } | |
| 126 | |
| 127 inline v8::Handle<v8::Value> v8StringOrFalse(const String& str) { | |
| 128 return str.isNull() | |
| 129 ? v8::Handle<v8::Value>(v8::False()) | |
| 130 : v8::Handle<v8::Value>(v8String(str)); | |
| 131 } | |
| 132 | |
| 133 } // namespace WebCore | |
| 134 | |
| 135 #endif // V8_BINDING_H__ | |
| OLD | NEW |