Changeset 126262 in webkit


Ignore:
Timestamp:
Aug 21, 2012 9:42:25 PM (12 years ago)
Author:
haraken@chromium.org
Message:

[V8] Move String related code in V8Binding to a separate file
https://bugs.webkit.org/show_bug.cgi?id=94571

Reviewed by Adam Barth.

This patch moves V8Parameter, V8ParameterBase and String related code
in V8Binding to a separate file.

No tests. No change in behavior.

  • UseV8.cmake:
  • WebCore.gypi:
  • bindings/v8/V8Binding.cpp:
  • bindings/v8/V8Binding.h:
  • bindings/v8/V8StringResource.cpp: Added.
  • bindings/v8/V8StringResource.h: Added.

(WebCore):

Location:
trunk/Source/WebCore
Files:
2 added
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r126258 r126262  
     12012-08-21  Kentaro Hara  <haraken@chromium.org>
     2
     3        [V8] Move String related code in V8Binding to a separate file
     4        https://bugs.webkit.org/show_bug.cgi?id=94571
     5
     6        Reviewed by Adam Barth.
     7
     8        This patch moves V8Parameter, V8ParameterBase and String related code
     9        in V8Binding to a separate file.
     10
     11        No tests. No change in behavior.
     12
     13        * UseV8.cmake:
     14        * WebCore.gypi:
     15        * bindings/v8/V8Binding.cpp:
     16        * bindings/v8/V8Binding.h:
     17        * bindings/v8/V8StringResource.cpp: Added.
     18        * bindings/v8/V8StringResource.h: Added.
     19        (WebCore):
     20
    1212012-08-21  Sukolsak Sakshuwong  <sukolsak@google.com>
    222
  • trunk/Source/WebCore/UseV8.cmake

    r126222 r126262  
    147147    bindings/v8/custom/V8SQLTransactionSyncCustom.cpp
    148148    bindings/v8/custom/V8StorageCustom.cpp
     149    bindings/v8/custom/V8StringResource.cpp
    149150    bindings/v8/custom/V8StyleSheetCustom.cpp
    150151    bindings/v8/custom/V8StyleSheetListCustom.cpp
  • trunk/Source/WebCore/WebCore.gypi

    r126258 r126262  
    22902290            'bindings/v8/V8RecursionScope.cpp',
    22912291            'bindings/v8/V8RecursionScope.h',
     2292            'bindings/v8/V8StringResource.cpp',
     2293            'bindings/v8/V8StringResource.h',
    22922294            'bindings/v8/V8ThrowException.cpp',
    22932295            'bindings/v8/V8ThrowException.h',
  • trunk/Source/WebCore/bindings/v8/V8Binding.cpp

    r126237 r126262  
    207207}
    208208
    209 template <class S> struct StringTraits
    210 {
    211     static S fromStringResource(WebCoreStringResource* resource);
    212 
    213     static S fromV8String(v8::Handle<v8::String> v8String, int length);
    214 };
    215 
    216 template<>
    217 struct StringTraits<String>
    218 {
    219     static String fromStringResource(WebCoreStringResource* resource)
    220     {
    221         return resource->webcoreString();
    222     }
    223 
    224     static String fromV8String(v8::Handle<v8::String> v8String, int length)
    225     {
    226         ASSERT(v8String->Length() == length);
    227         // NOTE: as of now, String(const UChar*, int) performs String::createUninitialized
    228         // anyway, so no need to optimize like we do for AtomicString below.
    229         UChar* buffer;
    230         String result = String::createUninitialized(length, buffer);
    231         v8String->Write(reinterpret_cast<uint16_t*>(buffer), 0, length);
    232         return result;
    233     }
    234 };
    235 
    236 template<>
    237 struct StringTraits<AtomicString>
    238 {
    239     static AtomicString fromStringResource(WebCoreStringResource* resource)
    240     {
    241         return resource->atomicString();
    242     }
    243 
    244     static AtomicString fromV8String(v8::Handle<v8::String> v8String, int length)
    245     {
    246         ASSERT(v8String->Length() == length);
    247         static const int inlineBufferSize = 16;
    248         if (length <= inlineBufferSize) {
    249             UChar inlineBuffer[inlineBufferSize];
    250             v8String->Write(reinterpret_cast<uint16_t*>(inlineBuffer), 0, length);
    251             return AtomicString(inlineBuffer, length);
    252         }
    253         UChar* buffer;
    254         String tmp = String::createUninitialized(length, buffer);
    255         v8String->Write(reinterpret_cast<uint16_t*>(buffer), 0, length);
    256         return AtomicString(tmp);
    257     }
    258 };
    259 
    260 template <typename StringType>
    261 StringType v8StringToWebCoreString(v8::Handle<v8::String> v8String, ExternalMode external)
    262 {
    263     WebCoreStringResource* stringResource = WebCoreStringResource::toStringResource(v8String);
    264     if (stringResource)
    265         return StringTraits<StringType>::fromStringResource(stringResource);
    266 
    267     int length = v8String->Length();
    268     if (!length) {
    269         // Avoid trying to morph empty strings, as they do not have enough room to contain the external reference.
    270         return StringImpl::empty();
    271     }
    272 
    273     StringType result(StringTraits<StringType>::fromV8String(v8String, length));
    274 
    275     if (external == Externalize && v8String->CanMakeExternal()) {
    276         stringResource = new WebCoreStringResource(result);
    277         if (!v8String->MakeExternal(stringResource)) {
    278             // In case of a failure delete the external resource as it was not used.
    279             delete stringResource;
    280         }
    281     }
    282     return result;
    283 }
    284    
    285 // Explicitly instantiate the above template with the expected parameterizations,
    286 // to ensure the compiler generates the code; otherwise link errors can result in GCC 4.4.
    287 template String v8StringToWebCoreString<String>(v8::Handle<v8::String>, ExternalMode);
    288 template AtomicString v8StringToWebCoreString<AtomicString>(v8::Handle<v8::String>, ExternalMode);
    289 
    290 // Fast but non thread-safe version.
    291 String int32ToWebCoreStringFast(int value)
    292 {
    293     // Caching of small strings below is not thread safe: newly constructed AtomicString
    294     // are not safely published.
    295     ASSERT(isMainThread());
    296 
    297     // Most numbers used are <= 100. Even if they aren't used there's very little cost in using the space.
    298     const int kLowNumbers = 100;
    299     DEFINE_STATIC_LOCAL(Vector<AtomicString>, lowNumbers, (kLowNumbers + 1));
    300     String webCoreString;
    301     if (0 <= value && value <= kLowNumbers) {
    302         webCoreString = lowNumbers[value];
    303         if (!webCoreString) {
    304             AtomicString valueString = AtomicString(String::number(value));
    305             lowNumbers[value] = valueString;
    306             webCoreString = valueString;
    307         }
    308     } else
    309         webCoreString = String::number(value);
    310     return webCoreString;
    311 }
    312 
    313 String int32ToWebCoreString(int value)
    314 {
    315     // If we are on the main thread (this should always true for non-workers), call the faster one.
    316     if (isMainThread())
    317         return int32ToWebCoreStringFast(value);
    318     return String::number(value);
    319 }
    320 
    321209v8::Persistent<v8::FunctionTemplate> createRawTemplate()
    322210{
  • trunk/Source/WebCore/bindings/v8/V8Binding.h

    r126237 r126262  
    4242#include "V8PerIsolateData.h"
    4343#include "V8Proxy.h"
     44#include "V8StringResource.h"
    4445#include "V8ThrowException.h"
    4546#include "V8ValueCache.h"
     
    7677        return isolate ? v8::Null(isolate) : v8::Null();
    7778    }
    78 
    79     enum ExternalMode {
    80         Externalize,
    81         DoNotExternalize
    82     };
    83 
    84     template <typename StringType>
    85     StringType v8StringToWebCoreString(v8::Handle<v8::String>, ExternalMode);
    8679
    8780    // Convert v8 types to a WTF::String. If the V8 string is not already
     
    355348    v8::Persistent<v8::FunctionTemplate> createRawTemplate();
    356349
    357     String int32ToWebCoreString(int value);
    358 
    359350    PassRefPtr<DOMStringList> toDOMStringList(v8::Handle<v8::Value>);
    360351
     
    378369    void crashIfV8IsDead();
    379370
    380     class V8ParameterBase {
    381     public:
    382         operator String() { return toString<String>(); }
    383         operator AtomicString() { return toString<AtomicString>(); }
    384 
    385     protected:
    386         V8ParameterBase(v8::Local<v8::Value> object) : m_v8Object(object), m_mode(Externalize), m_string() { }
    387 
    388         bool prepareBase()
    389         {
    390             if (m_v8Object.IsEmpty())
    391                 return true;
    392 
    393             if (LIKELY(m_v8Object->IsString()))
    394                 return true;
    395 
    396             if (LIKELY(m_v8Object->IsInt32())) {
    397                 setString(int32ToWebCoreString(m_v8Object->Int32Value()));
    398                 return true;
    399             }
    400 
    401             m_mode = DoNotExternalize;
    402             v8::TryCatch block;
    403             m_v8Object = m_v8Object->ToString();
    404             // Handle the case where an exception is thrown as part of invoking toString on the object.
    405             if (block.HasCaught()) {
    406                 block.ReThrow();
    407                 return false;
    408             }
    409             return true;
    410         }
    411 
    412         v8::Local<v8::Value> object() { return m_v8Object; }
    413 
    414         void setString(const String& string)
    415         {
    416             m_string = string;
    417             m_v8Object.Clear(); // To signal that String is ready.
    418         }
    419 
    420      private:
    421         v8::Local<v8::Value> m_v8Object;
    422         ExternalMode m_mode;
    423         String m_string;
    424 
    425         template <class StringType>
    426         StringType toString()
    427         {
    428             if (LIKELY(!m_v8Object.IsEmpty()))
    429                 return v8StringToWebCoreString<StringType>(m_v8Object.As<v8::String>(), m_mode);
    430 
    431             return StringType(m_string);
    432         }
    433     };
    434 
    435     // V8Parameter is an adapter class that converts V8 values to Strings
    436     // or AtomicStrings as appropriate, using multiple typecast operators.
    437     enum V8ParameterMode {
    438         DefaultMode,
    439         WithNullCheck,
    440         WithUndefinedOrNullCheck
    441     };
    442     template <V8ParameterMode MODE = DefaultMode>
    443     class V8Parameter: public V8ParameterBase {
    444     public:
    445         V8Parameter(v8::Local<v8::Value> object) : V8ParameterBase(object) { }
    446         V8Parameter(v8::Local<v8::Value> object, bool) : V8ParameterBase(object) { prepare(); }
    447 
    448         bool prepare();
    449     };
    450 
    451     template<> inline bool V8Parameter<DefaultMode>::prepare()
    452     {
    453         return V8ParameterBase::prepareBase();
    454     }
    455 
    456     template<> inline bool V8Parameter<WithNullCheck>::prepare()
    457     {
    458         if (object().IsEmpty() || object()->IsNull()) {
    459             setString(String());
    460             return true;
    461         }
    462 
    463         return V8ParameterBase::prepareBase();
    464     }
    465 
    466     template<> inline bool V8Parameter<WithUndefinedOrNullCheck>::prepare()
    467     {
    468         if (object().IsEmpty() || object()->IsNull() || object()->IsUndefined()) {
    469             setString(String());
    470             return true;
    471         }
    472 
    473         return V8ParameterBase::prepareBase();
    474     }
    475 
    476371} // namespace WebCore
    477372
Note: See TracChangeset for help on using the changeset viewer.