Changeset 126150 in webkit


Ignore:
Timestamp:
Aug 21, 2012 3:22:54 AM (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

    r126146 r126150  
     12012-08-20  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-17  Simon Hausmann  <simon.hausmann@nokia.com>
    222
  • trunk/Source/WebCore/UseV8.cmake

    r126066 r126150  
    146146    bindings/v8/custom/V8SQLTransactionSyncCustom.cpp
    147147    bindings/v8/custom/V8StorageCustom.cpp
     148    bindings/v8/custom/V8StringResource.cpp
    148149    bindings/v8/custom/V8StyleSheetCustom.cpp
    149150    bindings/v8/custom/V8StyleSheetListCustom.cpp
  • trunk/Source/WebCore/WebCore.gypi

    r126066 r126150  
    22892289            'bindings/v8/V8RecursionScope.cpp',
    22902290            'bindings/v8/V8RecursionScope.h',
     2291            'bindings/v8/V8StringResource.cpp',
     2292            'bindings/v8/V8StringResource.h',
    22912293            'bindings/v8/V8ThrowException.cpp',
    22922294            'bindings/v8/V8ThrowException.h',
  • trunk/Source/WebCore/bindings/v8/V8Binding.cpp

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

    r126137 r126150  
    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
     
    365358    v8::Persistent<v8::FunctionTemplate> getToStringTemplate();
    366359
    367     String int32ToWebCoreString(int value);
    368 
    369360    PassRefPtr<DOMStringList> toDOMStringList(v8::Handle<v8::Value>);
    370361
     
    385376    void crashIfV8IsDead();
    386377
    387     class V8ParameterBase {
    388     public:
    389         operator String() { return toString<String>(); }
    390         operator AtomicString() { return toString<AtomicString>(); }
    391 
    392     protected:
    393         V8ParameterBase(v8::Local<v8::Value> object) : m_v8Object(object), m_mode(Externalize), m_string() { }
    394 
    395         bool prepareBase()
    396         {
    397             if (m_v8Object.IsEmpty())
    398                 return true;
    399 
    400             if (LIKELY(m_v8Object->IsString()))
    401                 return true;
    402 
    403             if (LIKELY(m_v8Object->IsInt32())) {
    404                 setString(int32ToWebCoreString(m_v8Object->Int32Value()));
    405                 return true;
    406             }
    407 
    408             m_mode = DoNotExternalize;
    409             v8::TryCatch block;
    410             m_v8Object = m_v8Object->ToString();
    411             // Handle the case where an exception is thrown as part of invoking toString on the object.
    412             if (block.HasCaught()) {
    413                 block.ReThrow();
    414                 return false;
    415             }
    416             return true;
    417         }
    418 
    419         v8::Local<v8::Value> object() { return m_v8Object; }
    420 
    421         void setString(const String& string)
    422         {
    423             m_string = string;
    424             m_v8Object.Clear(); // To signal that String is ready.
    425         }
    426 
    427      private:
    428         v8::Local<v8::Value> m_v8Object;
    429         ExternalMode m_mode;
    430         String m_string;
    431 
    432         template <class StringType>
    433         StringType toString()
    434         {
    435             if (LIKELY(!m_v8Object.IsEmpty()))
    436                 return v8StringToWebCoreString<StringType>(m_v8Object.As<v8::String>(), m_mode);
    437 
    438             return StringType(m_string);
    439         }
    440     };
    441 
    442     // V8Parameter is an adapter class that converts V8 values to Strings
    443     // or AtomicStrings as appropriate, using multiple typecast operators.
    444     enum V8ParameterMode {
    445         DefaultMode,
    446         WithNullCheck,
    447         WithUndefinedOrNullCheck
    448     };
    449     template <V8ParameterMode MODE = DefaultMode>
    450     class V8Parameter: public V8ParameterBase {
    451     public:
    452         V8Parameter(v8::Local<v8::Value> object) : V8ParameterBase(object) { }
    453         V8Parameter(v8::Local<v8::Value> object, bool) : V8ParameterBase(object) { prepare(); }
    454 
    455         bool prepare();
    456     };
    457 
    458     template<> inline bool V8Parameter<DefaultMode>::prepare()
    459     {
    460         return V8ParameterBase::prepareBase();
    461     }
    462 
    463     template<> inline bool V8Parameter<WithNullCheck>::prepare()
    464     {
    465         if (object().IsEmpty() || object()->IsNull()) {
    466             setString(String());
    467             return true;
    468         }
    469 
    470         return V8ParameterBase::prepareBase();
    471     }
    472 
    473     template<> inline bool V8Parameter<WithUndefinedOrNullCheck>::prepare()
    474     {
    475         if (object().IsEmpty() || object()->IsNull() || object()->IsUndefined()) {
    476             setString(String());
    477             return true;
    478         }
    479 
    480         return V8ParameterBase::prepareBase();
    481     }
    482 
    483378} // namespace WebCore
    484379
Note: See TracChangeset for help on using the changeset viewer.