#ifndef V8StringResource_h
#define V8StringResource_h
#include <v8.h>
#include "wtf/Threading.h"
#include "wtf/text/AtomicString.h"
#include "wtf/text/WTFString.h"
namespace WebCore {
class ExternalStringVisitor;
class WebCoreStringResourceBase {
public:
static WebCoreStringResourceBase* toWebCoreStringResourceBase(v8::Handle<v8::String>);
explicit WebCoreStringResourceBase(const String& string)
: m_plainString(string)
{
#ifndef NDEBUG
m_threadId = WTF::currentThread();
#endif
ASSERT(!string.isNull());
v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(memoryConsumption(string));
}
explicit WebCoreStringResourceBase(const AtomicString& string)
: m_plainString(string.string())
, m_atomicString(string)
{
#ifndef NDEBUG
m_threadId = WTF::currentThread();
#endif
ASSERT(!string.isNull());
v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(memoryConsumption(string));
}
virtual ~WebCoreStringResourceBase()
{
#ifndef NDEBUG
ASSERT(m_threadId == WTF::currentThread());
#endif
int reducedExternalMemory = -memoryConsumption(m_plainString);
if (m_plainString.impl() != m_atomicString.impl() && !m_atomicString.isNull())
reducedExternalMemory -= memoryConsumption(m_atomicString.string());
v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(reducedExternalMemory);
}
const String& webcoreString() { return m_plainString; }
const AtomicString& atomicString()
{
#ifndef NDEBUG
ASSERT(m_threadId == WTF::currentThread());
#endif
if (m_atomicString.isNull()) {
m_atomicString = AtomicString(m_plainString);
ASSERT(!m_atomicString.isNull());
if (m_plainString.impl() != m_atomicString.impl())
v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(memoryConsumption(m_atomicString.string()));
}
return m_atomicString;
}
void visitStrings(ExternalStringVisitor*);
protected:
String m_plainString;
AtomicString m_atomicString;
private:
static int memoryConsumption(const String& string)
{
return string.length() * (string.is8Bit() ? sizeof(LChar) : sizeof(UChar));
}
#ifndef NDEBUG
WTF::ThreadIdentifier m_threadId;
#endif
};
class WebCoreStringResource16 FINAL : public WebCoreStringResourceBase, public v8::String::ExternalStringResource {
public:
explicit WebCoreStringResource16(const String& string)
: WebCoreStringResourceBase(string)
{
ASSERT(!string.is8Bit());
}
explicit WebCoreStringResource16(const AtomicString& string)
: WebCoreStringResourceBase(string)
{
ASSERT(!string.is8Bit());
}
virtual size_t length() const OVERRIDE { return m_plainString.impl()->length(); }
virtual const uint16_t* data() const OVERRIDE
{
return reinterpret_cast<const uint16_t*>(m_plainString.impl()->characters16());
}
};
class WebCoreStringResource8 FINAL : public WebCoreStringResourceBase, public v8::String::ExternalAsciiStringResource {
public:
explicit WebCoreStringResource8(const String& string)
: WebCoreStringResourceBase(string)
{
ASSERT(string.is8Bit());
}
explicit WebCoreStringResource8(const AtomicString& string)
: WebCoreStringResourceBase(string)
{
ASSERT(string.is8Bit());
}
virtual size_t length() const OVERRIDE { return m_plainString.impl()->length(); }
virtual const char* data() const OVERRIDE
{
return reinterpret_cast<const char*>(m_plainString.impl()->characters8());
}
};
enum ExternalMode {
Externalize,
DoNotExternalize
};
template <typename StringType>
StringType v8StringToWebCoreString(v8::Handle<v8::String>, ExternalMode);
String int32ToWebCoreString(int value);
enum V8StringResourceMode {
DefaultMode,
WithNullCheck,
WithUndefinedOrNullCheck
};
template <V8StringResourceMode Mode = DefaultMode>
class V8StringResource {
public:
V8StringResource(v8::Handle<v8::Value> object)
: m_v8Object(object)
, m_mode(Externalize)
, m_string()
{
}
bool prepare();
operator String() const { return toString<String>(); }
operator AtomicString() const { return toString<AtomicString>(); }
private:
bool prepareBase()
{
if (m_v8Object.IsEmpty())
return true;
if (LIKELY(m_v8Object->IsString()))
return true;
if (LIKELY(m_v8Object->IsInt32())) {
setString(int32ToWebCoreString(m_v8Object->Int32Value()));
return true;
}
m_mode = DoNotExternalize;
v8::TryCatch block;
m_v8Object = m_v8Object->ToString();
if (block.HasCaught()) {
block.ReThrow();
return false;
}
return true;
}
void setString(const String& string)
{
m_string = string;
m_v8Object.Clear();
}
template <class StringType>
StringType toString() const
{
if (LIKELY(!m_v8Object.IsEmpty()))
return v8StringToWebCoreString<StringType>(const_cast<v8::Handle<v8::Value>*>(&m_v8Object)->As<v8::String>(), m_mode);
return StringType(m_string);
}
v8::Handle<v8::Value> m_v8Object;
ExternalMode m_mode;
String m_string;
};
template<> inline bool V8StringResource<DefaultMode>::prepare()
{
return prepareBase();
}
template<> inline bool V8StringResource<WithNullCheck>::prepare()
{
if (m_v8Object.IsEmpty() || m_v8Object->IsNull()) {
setString(String());
return true;
}
return prepareBase();
}
template<> inline bool V8StringResource<WithUndefinedOrNullCheck>::prepare()
{
if (m_v8Object.IsEmpty() || m_v8Object->IsNull() || m_v8Object->IsUndefined()) {
setString(String());
return true;
}
return prepareBase();
}
}
#endif