| 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 | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 | 28 |
| 29 #include "bindings/core/v8/V8Binding.h" | 29 #include "bindings/core/v8/V8Binding.h" |
| 30 #include "core/inspector/BindingVisitors.h" | 30 #include "core/inspector/BindingVisitors.h" |
| 31 #include "wtf/MainThread.h" | 31 #include "wtf/MainThread.h" |
| 32 | 32 |
| 33 namespace blink { | 33 namespace blink { |
| 34 | 34 |
| 35 template<class StringClass> struct StringTraits { | 35 template<class StringClass> struct StringTraits { |
| 36 static const StringClass& fromStringResource(WebCoreStringResourceBase*); | 36 static const StringClass& fromStringResource(WebCoreStringResourceBase*); |
| 37 template <typename V8StringTrait> | 37 template <typename V8StringTrait> |
| 38 static StringClass fromV8String(v8::Handle<v8::String>, int); | 38 static StringClass fromV8String(v8::Local<v8::String>, int); |
| 39 }; | 39 }; |
| 40 | 40 |
| 41 template<> | 41 template<> |
| 42 struct StringTraits<String> { | 42 struct StringTraits<String> { |
| 43 static const String& fromStringResource(WebCoreStringResourceBase* resource) | 43 static const String& fromStringResource(WebCoreStringResourceBase* resource) |
| 44 { | 44 { |
| 45 return resource->webcoreString(); | 45 return resource->webcoreString(); |
| 46 } | 46 } |
| 47 template <typename V8StringTrait> | 47 template <typename V8StringTrait> |
| 48 static String fromV8String(v8::Handle<v8::String>, int); | 48 static String fromV8String(v8::Local<v8::String>, int); |
| 49 }; | 49 }; |
| 50 | 50 |
| 51 template<> | 51 template<> |
| 52 struct StringTraits<AtomicString> { | 52 struct StringTraits<AtomicString> { |
| 53 static const AtomicString& fromStringResource(WebCoreStringResourceBase* res
ource) | 53 static const AtomicString& fromStringResource(WebCoreStringResourceBase* res
ource) |
| 54 { | 54 { |
| 55 return resource->atomicString(); | 55 return resource->atomicString(); |
| 56 } | 56 } |
| 57 template <typename V8StringTrait> | 57 template <typename V8StringTrait> |
| 58 static AtomicString fromV8String(v8::Handle<v8::String>, int); | 58 static AtomicString fromV8String(v8::Local<v8::String>, int); |
| 59 }; | 59 }; |
| 60 | 60 |
| 61 struct V8StringTwoBytesTrait { | 61 struct V8StringTwoBytesTrait { |
| 62 typedef UChar CharType; | 62 typedef UChar CharType; |
| 63 ALWAYS_INLINE static void write(v8::Handle<v8::String> v8String, CharType* b
uffer, int length) | 63 ALWAYS_INLINE static void write(v8::Local<v8::String> v8String, CharType* bu
ffer, int length) |
| 64 { | 64 { |
| 65 v8String->Write(reinterpret_cast<uint16_t*>(buffer), 0, length); | 65 v8String->Write(reinterpret_cast<uint16_t*>(buffer), 0, length); |
| 66 } | 66 } |
| 67 }; | 67 }; |
| 68 | 68 |
| 69 struct V8StringOneByteTrait { | 69 struct V8StringOneByteTrait { |
| 70 typedef LChar CharType; | 70 typedef LChar CharType; |
| 71 ALWAYS_INLINE static void write(v8::Handle<v8::String> v8String, CharType* b
uffer, int length) | 71 ALWAYS_INLINE static void write(v8::Local<v8::String> v8String, CharType* bu
ffer, int length) |
| 72 { | 72 { |
| 73 v8String->WriteOneByte(buffer, 0, length); | 73 v8String->WriteOneByte(buffer, 0, length); |
| 74 } | 74 } |
| 75 }; | 75 }; |
| 76 | 76 |
| 77 template <typename V8StringTrait> | 77 template <typename V8StringTrait> |
| 78 String StringTraits<String>::fromV8String(v8::Handle<v8::String> v8String, int l
ength) | 78 String StringTraits<String>::fromV8String(v8::Local<v8::String> v8String, int le
ngth) |
| 79 { | 79 { |
| 80 ASSERT(v8String->Length() == length); | 80 ASSERT(v8String->Length() == length); |
| 81 typename V8StringTrait::CharType* buffer; | 81 typename V8StringTrait::CharType* buffer; |
| 82 String result = String::createUninitialized(length, buffer); | 82 String result = String::createUninitialized(length, buffer); |
| 83 V8StringTrait::write(v8String, buffer, length); | 83 V8StringTrait::write(v8String, buffer, length); |
| 84 return result; | 84 return result; |
| 85 } | 85 } |
| 86 | 86 |
| 87 template <typename V8StringTrait> | 87 template <typename V8StringTrait> |
| 88 AtomicString StringTraits<AtomicString>::fromV8String(v8::Handle<v8::String> v8S
tring, int length) | 88 AtomicString StringTraits<AtomicString>::fromV8String(v8::Local<v8::String> v8St
ring, int length) |
| 89 { | 89 { |
| 90 ASSERT(v8String->Length() == length); | 90 ASSERT(v8String->Length() == length); |
| 91 static const int inlineBufferSize = 32 / sizeof(typename V8StringTrait::Char
Type); | 91 static const int inlineBufferSize = 32 / sizeof(typename V8StringTrait::Char
Type); |
| 92 if (length <= inlineBufferSize) { | 92 if (length <= inlineBufferSize) { |
| 93 typename V8StringTrait::CharType inlineBuffer[inlineBufferSize]; | 93 typename V8StringTrait::CharType inlineBuffer[inlineBufferSize]; |
| 94 V8StringTrait::write(v8String, inlineBuffer, length); | 94 V8StringTrait::write(v8String, inlineBuffer, length); |
| 95 return AtomicString(inlineBuffer, length); | 95 return AtomicString(inlineBuffer, length); |
| 96 } | 96 } |
| 97 typename V8StringTrait::CharType* buffer; | 97 typename V8StringTrait::CharType* buffer; |
| 98 String string = String::createUninitialized(length, buffer); | 98 String string = String::createUninitialized(length, buffer); |
| 99 V8StringTrait::write(v8String, buffer, length); | 99 V8StringTrait::write(v8String, buffer, length); |
| 100 return AtomicString(string); | 100 return AtomicString(string); |
| 101 } | 101 } |
| 102 | 102 |
| 103 template<typename StringType> | 103 template<typename StringType> |
| 104 StringType v8StringToWebCoreString(v8::Handle<v8::String> v8String, ExternalMode
external) | 104 StringType v8StringToWebCoreString(v8::Local<v8::String> v8String, ExternalMode
external) |
| 105 { | 105 { |
| 106 { | 106 { |
| 107 // This portion of this function is very hot in certain Dromeao benchmar
ks. | 107 // This portion of this function is very hot in certain Dromeao benchmar
ks. |
| 108 v8::String::Encoding encoding; | 108 v8::String::Encoding encoding; |
| 109 v8::String::ExternalStringResourceBase* resource = v8String->GetExternal
StringResourceBase(&encoding); | 109 v8::String::ExternalStringResourceBase* resource = v8String->GetExternal
StringResourceBase(&encoding); |
| 110 if (LIKELY(!!resource)) { | 110 if (LIKELY(!!resource)) { |
| 111 WebCoreStringResourceBase* base; | 111 WebCoreStringResourceBase* base; |
| 112 if (encoding == v8::String::ONE_BYTE_ENCODING) | 112 if (encoding == v8::String::ONE_BYTE_ENCODING) |
| 113 base = static_cast<WebCoreStringResource8*>(resource); | 113 base = static_cast<WebCoreStringResource8*>(resource); |
| 114 else | 114 else |
| (...skipping 19 matching lines...) Expand all Loading... |
| 134 } else { | 134 } else { |
| 135 WebCoreStringResource16* stringResource = new WebCoreStringResource16(re
sult); | 135 WebCoreStringResource16* stringResource = new WebCoreStringResource16(re
sult); |
| 136 if (UNLIKELY(!v8String->MakeExternal(stringResource))) | 136 if (UNLIKELY(!v8String->MakeExternal(stringResource))) |
| 137 delete stringResource; | 137 delete stringResource; |
| 138 } | 138 } |
| 139 return result; | 139 return result; |
| 140 } | 140 } |
| 141 | 141 |
| 142 // Explicitly instantiate the above template with the expected parameterizations
, | 142 // Explicitly instantiate the above template with the expected parameterizations
, |
| 143 // to ensure the compiler generates the code; otherwise link errors can result i
n GCC 4.4. | 143 // to ensure the compiler generates the code; otherwise link errors can result i
n GCC 4.4. |
| 144 template String v8StringToWebCoreString<String>(v8::Handle<v8::String>, External
Mode); | 144 template String v8StringToWebCoreString<String>(v8::Local<v8::String>, ExternalM
ode); |
| 145 template AtomicString v8StringToWebCoreString<AtomicString>(v8::Handle<v8::Strin
g>, ExternalMode); | 145 template AtomicString v8StringToWebCoreString<AtomicString>(v8::Local<v8::String
>, ExternalMode); |
| 146 | 146 |
| 147 // Fast but non thread-safe version. | 147 // Fast but non thread-safe version. |
| 148 String int32ToWebCoreStringFast(int value) | 148 String int32ToWebCoreStringFast(int value) |
| 149 { | 149 { |
| 150 // Caching of small strings below is not thread safe: newly constructed Atom
icString | 150 // Caching of small strings below is not thread safe: newly constructed Atom
icString |
| 151 // are not safely published. | 151 // are not safely published. |
| 152 ASSERT(isMainThread()); | 152 ASSERT(isMainThread()); |
| 153 | 153 |
| 154 // Most numbers used are <= 100. Even if they aren't used there's very littl
e cost in using the space. | 154 // Most numbers used are <= 100. Even if they aren't used there's very littl
e cost in using the space. |
| 155 const int kLowNumbers = 100; | 155 const int kLowNumbers = 100; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 170 | 170 |
| 171 String int32ToWebCoreString(int value) | 171 String int32ToWebCoreString(int value) |
| 172 { | 172 { |
| 173 // If we are on the main thread (this should always true for non-workers), c
all the faster one. | 173 // If we are on the main thread (this should always true for non-workers), c
all the faster one. |
| 174 if (isMainThread()) | 174 if (isMainThread()) |
| 175 return int32ToWebCoreStringFast(value); | 175 return int32ToWebCoreStringFast(value); |
| 176 return String::number(value); | 176 return String::number(value); |
| 177 } | 177 } |
| 178 | 178 |
| 179 } // namespace blink | 179 } // namespace blink |
| OLD | NEW |