Commit c7c2bd2e authored by adithyas's avatar adithyas Committed by Commit bot

Extract WebCoreStringResouce out of V8StringResource

WebCoreStringResource (renamed to StringResource) is used by V8ValueCache and needs to move to platform/bindings.

BUG=682322

Review-Url: https://codereview.chromium.org/2810413003
Cr-Commit-Position: refs/heads/master@{#465286}
parent 57176fb7
...@@ -107,6 +107,8 @@ bindings_core_v8_files = ...@@ -107,6 +107,8 @@ bindings_core_v8_files =
"core/v8/ScriptPromiseResolver.h", "core/v8/ScriptPromiseResolver.h",
"core/v8/ScriptRegexp.cpp", "core/v8/ScriptRegexp.cpp",
"core/v8/ScriptRegexp.h", "core/v8/ScriptRegexp.h",
"core/v8/StringResource.cpp",
"core/v8/StringResource.h",
"core/v8/ScriptSourceCode.cpp", "core/v8/ScriptSourceCode.cpp",
"core/v8/ScriptSourceCode.h", "core/v8/ScriptSourceCode.h",
"core/v8/ScriptState.cpp", "core/v8/ScriptState.cpp",
...@@ -196,7 +198,6 @@ bindings_core_v8_files = ...@@ -196,7 +198,6 @@ bindings_core_v8_files =
"core/v8/V8ResizeObserverCallbackCustom.cpp", "core/v8/V8ResizeObserverCallbackCustom.cpp",
"core/v8/V8ScriptRunner.cpp", "core/v8/V8ScriptRunner.cpp",
"core/v8/V8ScriptRunner.h", "core/v8/V8ScriptRunner.h",
"core/v8/V8StringResource.cpp",
"core/v8/V8StringResource.h", "core/v8/V8StringResource.h",
"core/v8/V8ThrowException.cpp", "core/v8/V8ThrowException.cpp",
"core/v8/V8ThrowException.h", "core/v8/V8ThrowException.h",
......
/* // Copyright 2017 The Chromium Authors. All rights reserved.
* Copyright (C) 2009 Google Inc. All rights reserved. // Use of this source code is governed by a BSD-style license that can be
* // found in the LICENSE file.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions #include "bindings/core/v8/StringResource.h"
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "bindings/core/v8/V8StringResource.h"
#include "bindings/core/v8/V8Binding.h" #include "bindings/core/v8/V8Binding.h"
...@@ -31,14 +10,14 @@ namespace blink { ...@@ -31,14 +10,14 @@ namespace blink {
template <class StringClass> template <class StringClass>
struct StringTraits { struct StringTraits {
static const StringClass& FromStringResource(WebCoreStringResourceBase*); static const StringClass& FromStringResource(StringResourceBase*);
template <typename V8StringTrait> template <typename V8StringTrait>
static StringClass FromV8String(v8::Local<v8::String>, int); static StringClass FromV8String(v8::Local<v8::String>, int);
}; };
template <> template <>
struct StringTraits<String> { struct StringTraits<String> {
static const String& FromStringResource(WebCoreStringResourceBase* resource) { static const String& FromStringResource(StringResourceBase* resource) {
return resource->WebcoreString(); return resource->WebcoreString();
} }
template <typename V8StringTrait> template <typename V8StringTrait>
...@@ -47,8 +26,7 @@ struct StringTraits<String> { ...@@ -47,8 +26,7 @@ struct StringTraits<String> {
template <> template <>
struct StringTraits<AtomicString> { struct StringTraits<AtomicString> {
static const AtomicString& FromStringResource( static const AtomicString& FromStringResource(StringResourceBase* resource) {
WebCoreStringResourceBase* resource) {
return resource->GetAtomicString(); return resource->GetAtomicString();
} }
template <typename V8StringTrait> template <typename V8StringTrait>
...@@ -76,7 +54,7 @@ struct V8StringOneByteTrait { ...@@ -76,7 +54,7 @@ struct V8StringOneByteTrait {
template <typename V8StringTrait> template <typename V8StringTrait>
String StringTraits<String>::FromV8String(v8::Local<v8::String> v8_string, String StringTraits<String>::FromV8String(v8::Local<v8::String> v8_string,
int length) { int length) {
DCHECK_EQ(v8_string->Length(), length); DCHECK(v8_string->Length() == length);
typename V8StringTrait::CharType* buffer; typename V8StringTrait::CharType* buffer;
String result = String::CreateUninitialized(length, buffer); String result = String::CreateUninitialized(length, buffer);
V8StringTrait::Write(v8_string, buffer, length); V8StringTrait::Write(v8_string, buffer, length);
...@@ -87,7 +65,7 @@ template <typename V8StringTrait> ...@@ -87,7 +65,7 @@ template <typename V8StringTrait>
AtomicString StringTraits<AtomicString>::FromV8String( AtomicString StringTraits<AtomicString>::FromV8String(
v8::Local<v8::String> v8_string, v8::Local<v8::String> v8_string,
int length) { int length) {
DCHECK_EQ(v8_string->Length(), length); DCHECK(v8_string->Length() == length);
static const int kInlineBufferSize = static const int kInlineBufferSize =
32 / sizeof(typename V8StringTrait::CharType); 32 / sizeof(typename V8StringTrait::CharType);
if (length <= kInlineBufferSize) { if (length <= kInlineBufferSize) {
...@@ -110,11 +88,11 @@ StringType V8StringToWebCoreString(v8::Local<v8::String> v8_string, ...@@ -110,11 +88,11 @@ StringType V8StringToWebCoreString(v8::Local<v8::String> v8_string,
v8::String::ExternalStringResourceBase* resource = v8::String::ExternalStringResourceBase* resource =
v8_string->GetExternalStringResourceBase(&encoding); v8_string->GetExternalStringResourceBase(&encoding);
if (LIKELY(!!resource)) { if (LIKELY(!!resource)) {
WebCoreStringResourceBase* base; StringResourceBase* base;
if (encoding == v8::String::ONE_BYTE_ENCODING) if (encoding == v8::String::ONE_BYTE_ENCODING)
base = static_cast<WebCoreStringResource8*>(resource); base = static_cast<StringResource8*>(resource);
else else
base = static_cast<WebCoreStringResource16*>(resource); base = static_cast<StringResource16*>(resource);
return StringTraits<StringType>::FromStringResource(base); return StringTraits<StringType>::FromStringResource(base);
} }
} }
...@@ -133,13 +111,11 @@ StringType V8StringToWebCoreString(v8::Local<v8::String> v8_string, ...@@ -133,13 +111,11 @@ StringType V8StringToWebCoreString(v8::Local<v8::String> v8_string,
return result; return result;
if (result.Is8Bit()) { if (result.Is8Bit()) {
WebCoreStringResource8* string_resource = StringResource8* string_resource = new StringResource8(result);
new WebCoreStringResource8(result);
if (UNLIKELY(!v8_string->MakeExternal(string_resource))) if (UNLIKELY(!v8_string->MakeExternal(string_resource)))
delete string_resource; delete string_resource;
} else { } else {
WebCoreStringResource16* string_resource = StringResource16* string_resource = new StringResource16(result);
new WebCoreStringResource16(result);
if (UNLIKELY(!v8_string->MakeExternal(string_resource))) if (UNLIKELY(!v8_string->MakeExternal(string_resource)))
delete string_resource; delete string_resource;
} }
......
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef StringResource_h
#define StringResource_h
#include "core/CoreExport.h"
#include "platform/wtf/Allocator.h"
#include "platform/wtf/Threading.h"
#include "platform/wtf/text/AtomicString.h"
#include "v8/include/v8.h"
namespace blink {
// StringResource is a helper class for V8ExternalString. It is used
// to manage the life-cycle of the underlying buffer of the external string.
class StringResourceBase {
USING_FAST_MALLOC(StringResourceBase);
WTF_MAKE_NONCOPYABLE(StringResourceBase);
public:
explicit StringResourceBase(const String& string) : plain_string_(string) {
#if DCHECK_IS_ON()
thread_id_ = WTF::CurrentThread();
#endif
DCHECK(!string.IsNull());
v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(
string.CharactersSizeInBytes());
}
explicit StringResourceBase(const AtomicString& string)
: plain_string_(string.GetString()), atomic_string_(string) {
#if DCHECK_IS_ON()
thread_id_ = WTF::CurrentThread();
#endif
DCHECK(!string.IsNull());
v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(
string.CharactersSizeInBytes());
}
virtual ~StringResourceBase() {
#if DCHECK_IS_ON()
DCHECK(thread_id_ == WTF::CurrentThread());
#endif
int64_t reduced_external_memory = plain_string_.CharactersSizeInBytes();
if (plain_string_.Impl() != atomic_string_.Impl() &&
!atomic_string_.IsNull())
reduced_external_memory += atomic_string_.CharactersSizeInBytes();
v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(
-reduced_external_memory);
}
const String& WebcoreString() { return plain_string_; }
const AtomicString& GetAtomicString() {
#if DCHECK_IS_ON()
DCHECK(thread_id_ == WTF::CurrentThread());
#endif
if (atomic_string_.IsNull()) {
atomic_string_ = AtomicString(plain_string_);
DCHECK(!atomic_string_.IsNull());
if (plain_string_.Impl() != atomic_string_.Impl()) {
v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(
atomic_string_.CharactersSizeInBytes());
}
}
return atomic_string_;
}
protected:
// A shallow copy of the string. Keeps the string buffer alive until the V8
// engine garbage collects it.
String plain_string_;
// If this string is atomic or has been made atomic earlier the
// atomic string is held here. In the case where the string starts
// off non-atomic and becomes atomic later it is necessary to keep
// the original string alive because v8 may keep derived pointers
// into that string.
AtomicString atomic_string_;
private:
#if DCHECK_IS_ON()
WTF::ThreadIdentifier thread_id_;
#endif
};
class StringResource16 final : public StringResourceBase,
public v8::String::ExternalStringResource {
WTF_MAKE_NONCOPYABLE(StringResource16);
public:
explicit StringResource16(const String& string) : StringResourceBase(string) {
DCHECK(!string.Is8Bit());
}
explicit StringResource16(const AtomicString& string)
: StringResourceBase(string) {
DCHECK(!string.Is8Bit());
}
size_t length() const override { return plain_string_.Impl()->length(); }
const uint16_t* data() const override {
return reinterpret_cast<const uint16_t*>(
plain_string_.Impl()->Characters16());
}
};
class StringResource8 final : public StringResourceBase,
public v8::String::ExternalOneByteStringResource {
WTF_MAKE_NONCOPYABLE(StringResource8);
public:
explicit StringResource8(const String& string) : StringResourceBase(string) {
DCHECK(string.Is8Bit());
}
explicit StringResource8(const AtomicString& string)
: StringResourceBase(string) {
DCHECK(string.Is8Bit());
}
size_t length() const override { return plain_string_.Impl()->length(); }
const char* data() const override {
return reinterpret_cast<const char*>(plain_string_.Impl()->Characters8());
}
};
enum ExternalMode { kExternalize, kDoNotExternalize };
template <typename StringType>
CORE_EXPORT StringType V8StringToWebCoreString(v8::Local<v8::String>,
ExternalMode);
CORE_EXPORT String Int32ToWebCoreString(int value);
} // namespace blink
#endif // StringResource_h
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
#define V8StringResource_h #define V8StringResource_h
#include "bindings/core/v8/ExceptionState.h" #include "bindings/core/v8/ExceptionState.h"
#include "bindings/core/v8/StringResource.h"
#include "core/CoreExport.h" #include "core/CoreExport.h"
#include "platform/wtf/Allocator.h" #include "platform/wtf/Allocator.h"
#include "platform/wtf/Threading.h" #include "platform/wtf/Threading.h"
...@@ -35,130 +36,6 @@ ...@@ -35,130 +36,6 @@
namespace blink { namespace blink {
// WebCoreStringResource is a helper class for v8ExternalString. It is used
// to manage the life-cycle of the underlying buffer of the external string.
class WebCoreStringResourceBase {
USING_FAST_MALLOC(WebCoreStringResourceBase);
WTF_MAKE_NONCOPYABLE(WebCoreStringResourceBase);
public:
explicit WebCoreStringResourceBase(const String& string)
: plain_string_(string) {
#if DCHECK_IS_ON()
thread_id_ = WTF::CurrentThread();
#endif
DCHECK(!string.IsNull());
v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(
string.CharactersSizeInBytes());
}
explicit WebCoreStringResourceBase(const AtomicString& string)
: plain_string_(string.GetString()), atomic_string_(string) {
#if DCHECK_IS_ON()
thread_id_ = WTF::CurrentThread();
#endif
DCHECK(!string.IsNull());
v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(
string.CharactersSizeInBytes());
}
virtual ~WebCoreStringResourceBase() {
#if DCHECK_IS_ON()
DCHECK_EQ(thread_id_, WTF::CurrentThread());
#endif
int64_t reduced_external_memory = plain_string_.CharactersSizeInBytes();
if (plain_string_.Impl() != atomic_string_.Impl() &&
!atomic_string_.IsNull())
reduced_external_memory += atomic_string_.CharactersSizeInBytes();
v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(
-reduced_external_memory);
}
const String& WebcoreString() { return plain_string_; }
const AtomicString& GetAtomicString() {
#if DCHECK_IS_ON()
DCHECK_EQ(thread_id_, WTF::CurrentThread());
#endif
if (atomic_string_.IsNull()) {
atomic_string_ = AtomicString(plain_string_);
DCHECK(!atomic_string_.IsNull());
if (plain_string_.Impl() != atomic_string_.Impl())
v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(
atomic_string_.CharactersSizeInBytes());
}
return atomic_string_;
}
protected:
// A shallow copy of the string. Keeps the string buffer alive until the V8
// engine garbage collects it.
String plain_string_;
// If this string is atomic or has been made atomic earlier the
// atomic string is held here. In the case where the string starts
// off non-atomic and becomes atomic later it is necessary to keep
// the original string alive because v8 may keep derived pointers
// into that string.
AtomicString atomic_string_;
private:
#if DCHECK_IS_ON()
WTF::ThreadIdentifier thread_id_;
#endif
};
class WebCoreStringResource16 final
: public WebCoreStringResourceBase,
public v8::String::ExternalStringResource {
WTF_MAKE_NONCOPYABLE(WebCoreStringResource16);
public:
explicit WebCoreStringResource16(const String& string)
: WebCoreStringResourceBase(string) {
DCHECK(!string.Is8Bit());
}
explicit WebCoreStringResource16(const AtomicString& string)
: WebCoreStringResourceBase(string) {
DCHECK(!string.Is8Bit());
}
size_t length() const override { return plain_string_.Impl()->length(); }
const uint16_t* data() const override {
return reinterpret_cast<const uint16_t*>(
plain_string_.Impl()->Characters16());
}
};
class WebCoreStringResource8 final
: public WebCoreStringResourceBase,
public v8::String::ExternalOneByteStringResource {
WTF_MAKE_NONCOPYABLE(WebCoreStringResource8);
public:
explicit WebCoreStringResource8(const String& string)
: WebCoreStringResourceBase(string) {
DCHECK(string.Is8Bit());
}
explicit WebCoreStringResource8(const AtomicString& string)
: WebCoreStringResourceBase(string) {
DCHECK(string.Is8Bit());
}
size_t length() const override { return plain_string_.Impl()->length(); }
const char* data() const override {
return reinterpret_cast<const char*>(plain_string_.Impl()->Characters8());
}
};
enum ExternalMode { kExternalize, kDoNotExternalize };
template <typename StringType>
CORE_EXPORT StringType V8StringToWebCoreString(v8::Local<v8::String>,
ExternalMode);
CORE_EXPORT String Int32ToWebCoreString(int value);
// V8StringResource is an adapter class that converts V8 values to Strings // V8StringResource is an adapter class that converts V8 values to Strings
// or AtomicStrings as appropriate, using multiple typecast operators. // or AtomicStrings as appropriate, using multiple typecast operators.
enum V8StringResourceMode { enum V8StringResourceMode {
......
...@@ -70,8 +70,7 @@ void StringCache::Dispose() { ...@@ -70,8 +70,7 @@ void StringCache::Dispose() {
static v8::Local<v8::String> MakeExternalString(v8::Isolate* isolate, static v8::Local<v8::String> MakeExternalString(v8::Isolate* isolate,
const String& string) { const String& string) {
if (string.Is8Bit()) { if (string.Is8Bit()) {
WebCoreStringResource8* string_resource = StringResource8* string_resource = new StringResource8(string);
new WebCoreStringResource8(string);
v8::Local<v8::String> new_string; v8::Local<v8::String> new_string;
if (!v8::String::NewExternalOneByte(isolate, string_resource) if (!v8::String::NewExternalOneByte(isolate, string_resource)
.ToLocal(&new_string)) { .ToLocal(&new_string)) {
...@@ -81,8 +80,7 @@ static v8::Local<v8::String> MakeExternalString(v8::Isolate* isolate, ...@@ -81,8 +80,7 @@ static v8::Local<v8::String> MakeExternalString(v8::Isolate* isolate,
return new_string; return new_string;
} }
WebCoreStringResource16* string_resource = StringResource16* string_resource = new StringResource16(string);
new WebCoreStringResource16(string);
v8::Local<v8::String> new_string; v8::Local<v8::String> new_string;
if (!v8::String::NewExternalTwoByte(isolate, string_resource) if (!v8::String::NewExternalTwoByte(isolate, string_resource)
.ToLocal(&new_string)) { .ToLocal(&new_string)) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment