| 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 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 ALWAYS_INLINE static void Write(v8::Local<v8::String> v8_string, | 69 ALWAYS_INLINE static void Write(v8::Local<v8::String> v8_string, |
| 70 CharType* buffer, | 70 CharType* buffer, |
| 71 int length) { | 71 int length) { |
| 72 v8_string->WriteOneByte(buffer, 0, length); | 72 v8_string->WriteOneByte(buffer, 0, length); |
| 73 } | 73 } |
| 74 }; | 74 }; |
| 75 | 75 |
| 76 template <typename V8StringTrait> | 76 template <typename V8StringTrait> |
| 77 String StringTraits<String>::FromV8String(v8::Local<v8::String> v8_string, | 77 String StringTraits<String>::FromV8String(v8::Local<v8::String> v8_string, |
| 78 int length) { | 78 int length) { |
| 79 ASSERT(v8_string->Length() == length); | 79 DCHECK_EQ(v8_string->Length(), length); |
| 80 typename V8StringTrait::CharType* buffer; | 80 typename V8StringTrait::CharType* buffer; |
| 81 String result = String::CreateUninitialized(length, buffer); | 81 String result = String::CreateUninitialized(length, buffer); |
| 82 V8StringTrait::Write(v8_string, buffer, length); | 82 V8StringTrait::Write(v8_string, buffer, length); |
| 83 return result; | 83 return result; |
| 84 } | 84 } |
| 85 | 85 |
| 86 template <typename V8StringTrait> | 86 template <typename V8StringTrait> |
| 87 AtomicString StringTraits<AtomicString>::FromV8String( | 87 AtomicString StringTraits<AtomicString>::FromV8String( |
| 88 v8::Local<v8::String> v8_string, | 88 v8::Local<v8::String> v8_string, |
| 89 int length) { | 89 int length) { |
| 90 ASSERT(v8_string->Length() == length); | 90 DCHECK_EQ(v8_string->Length(), length); |
| 91 static const int kInlineBufferSize = | 91 static const int kInlineBufferSize = |
| 92 32 / sizeof(typename V8StringTrait::CharType); | 92 32 / sizeof(typename V8StringTrait::CharType); |
| 93 if (length <= kInlineBufferSize) { | 93 if (length <= kInlineBufferSize) { |
| 94 typename V8StringTrait::CharType inline_buffer[kInlineBufferSize]; | 94 typename V8StringTrait::CharType inline_buffer[kInlineBufferSize]; |
| 95 V8StringTrait::Write(v8_string, inline_buffer, length); | 95 V8StringTrait::Write(v8_string, inline_buffer, length); |
| 96 return AtomicString(inline_buffer, length); | 96 return AtomicString(inline_buffer, length); |
| 97 } | 97 } |
| 98 typename V8StringTrait::CharType* buffer; | 98 typename V8StringTrait::CharType* buffer; |
| 99 String string = String::CreateUninitialized(length, buffer); | 99 String string = String::CreateUninitialized(length, buffer); |
| 100 V8StringTrait::Write(v8_string, buffer, length); | 100 V8StringTrait::Write(v8_string, buffer, length); |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 template String V8StringToWebCoreString<String>(v8::Local<v8::String>, | 152 template String V8StringToWebCoreString<String>(v8::Local<v8::String>, |
| 153 ExternalMode); | 153 ExternalMode); |
| 154 template AtomicString V8StringToWebCoreString<AtomicString>( | 154 template AtomicString V8StringToWebCoreString<AtomicString>( |
| 155 v8::Local<v8::String>, | 155 v8::Local<v8::String>, |
| 156 ExternalMode); | 156 ExternalMode); |
| 157 | 157 |
| 158 // Fast but non thread-safe version. | 158 // Fast but non thread-safe version. |
| 159 String Int32ToWebCoreStringFast(int value) { | 159 String Int32ToWebCoreStringFast(int value) { |
| 160 // Caching of small strings below is not thread safe: newly constructed | 160 // Caching of small strings below is not thread safe: newly constructed |
| 161 // AtomicString are not safely published. | 161 // AtomicString are not safely published. |
| 162 ASSERT(IsMainThread()); | 162 DCHECK(IsMainThread()); |
| 163 | 163 |
| 164 // Most numbers used are <= 100. Even if they aren't used there's very little | 164 // Most numbers used are <= 100. Even if they aren't used there's very little |
| 165 // cost in using the space. | 165 // cost in using the space. |
| 166 const int kLowNumbers = 100; | 166 const int kLowNumbers = 100; |
| 167 DEFINE_STATIC_LOCAL(Vector<AtomicString>, low_numbers, (kLowNumbers + 1)); | 167 DEFINE_STATIC_LOCAL(Vector<AtomicString>, low_numbers, (kLowNumbers + 1)); |
| 168 String web_core_string; | 168 String web_core_string; |
| 169 if (0 <= value && value <= kLowNumbers) { | 169 if (0 <= value && value <= kLowNumbers) { |
| 170 web_core_string = low_numbers[value]; | 170 web_core_string = low_numbers[value]; |
| 171 if (!web_core_string) { | 171 if (!web_core_string) { |
| 172 AtomicString value_string = AtomicString::Number(value); | 172 AtomicString value_string = AtomicString::Number(value); |
| 173 low_numbers[value] = value_string; | 173 low_numbers[value] = value_string; |
| 174 web_core_string = value_string; | 174 web_core_string = value_string; |
| 175 } | 175 } |
| 176 } else { | 176 } else { |
| 177 web_core_string = String::Number(value); | 177 web_core_string = String::Number(value); |
| 178 } | 178 } |
| 179 return web_core_string; | 179 return web_core_string; |
| 180 } | 180 } |
| 181 | 181 |
| 182 String Int32ToWebCoreString(int value) { | 182 String Int32ToWebCoreString(int value) { |
| 183 // If we are on the main thread (this should always true for non-workers), | 183 // If we are on the main thread (this should always true for non-workers), |
| 184 // call the faster one. | 184 // call the faster one. |
| 185 if (IsMainThread()) | 185 if (IsMainThread()) |
| 186 return Int32ToWebCoreStringFast(value); | 186 return Int32ToWebCoreStringFast(value); |
| 187 return String::Number(value); | 187 return String::Number(value); |
| 188 } | 188 } |
| 189 | 189 |
| 190 } // namespace blink | 190 } // namespace blink |
| OLD | NEW |