1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
// Copyright 2014 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 ToV8_h
#define ToV8_h
#include "bindings/core/v8/DOMDataStore.h"
#include "bindings/core/v8/ScriptValue.h"
#include "bindings/core/v8/ScriptWrappable.h"
#include "bindings/core/v8/V8Binding.h"
#include "platform/heap/Handle.h"
#include "wtf/Forward.h"
#include <v8.h>
namespace blink {
class DOMWindow;
class EventTarget;
class WorkerGlobalScope;
// ScriptWrappable
inline v8::Handle<v8::Value> toV8(ScriptWrappable* impl, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
{
if (UNLIKELY(!impl))
return v8::Null(isolate);
v8::Handle<v8::Value> wrapper = DOMDataStore::getWrapper(impl, isolate);
if (!wrapper.IsEmpty())
return wrapper;
return impl->wrap(creationContext, isolate);
}
inline v8::Handle<v8::Value> toV8(Node* impl, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
{
if (UNLIKELY(!impl))
return v8::Null(isolate);
v8::Handle<v8::Value> wrapper = DOMDataStore::getWrapper(impl, isolate);
if (!wrapper.IsEmpty())
return wrapper;
return ScriptWrappable::fromNode(impl)->wrap(creationContext, isolate);
}
// [Custom=ToV8]
v8::Handle<v8::Value> toV8(DOMWindow*, v8::Handle<v8::Object> creationContext, v8::Isolate*);
v8::Handle<v8::Value> toV8(EventTarget*, v8::Handle<v8::Object> creationContext, v8::Isolate*);
v8::Handle<v8::Value> toV8(WorkerGlobalScope*, v8::Handle<v8::Object> creationContext, v8::Isolate*);
// PassRefPtr, RawPtr and RefPtr
template<typename T>
inline v8::Handle<v8::Value> toV8(PassRefPtr<T> impl, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
{
return toV8(impl.get(), creationContext, isolate);
}
template<typename T>
inline v8::Handle<v8::Value> toV8(RawPtr<T> impl, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
{
return toV8(impl.get(), creationContext, isolate);
}
template<typename T>
inline v8::Handle<v8::Value> toV8(const RefPtr<T>& impl, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
{
return toV8(impl.get(), creationContext, isolate);
}
// Primitives
inline v8::Handle<v8::Value> toV8(const String& value, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
{
return v8String(isolate, value);
}
inline v8::Handle<v8::Value> toV8(const char* value, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
{
return v8String(isolate, value);
}
template<size_t sizeOfValue>
inline v8::Handle<v8::Value> toV8SignedIntegerInternal(int64_t value, v8::Isolate*);
template<>
inline v8::Handle<v8::Value> toV8SignedIntegerInternal<4>(int64_t value, v8::Isolate* isolate)
{
return v8::Integer::New(isolate, static_cast<int32_t>(value));
}
template<>
inline v8::Handle<v8::Value> toV8SignedIntegerInternal<8>(int64_t value, v8::Isolate* isolate)
{
int32_t valueIn32Bit = static_cast<int32_t>(value);
if (valueIn32Bit == value)
return v8::Integer::New(isolate, value);
// V8 doesn't have a 64-bit integer implementation.
return v8::Number::New(isolate, value);
}
template<size_t sizeOfValue>
inline v8::Handle<v8::Value> toV8UnsignedIntegerInternal(uint64_t value, v8::Isolate*);
template<>
inline v8::Handle<v8::Value> toV8UnsignedIntegerInternal<4>(uint64_t value, v8::Isolate* isolate)
{
return v8::Integer::NewFromUnsigned(isolate, static_cast<uint32_t>(value));
}
template<>
inline v8::Handle<v8::Value> toV8UnsignedIntegerInternal<8>(uint64_t value, v8::Isolate* isolate)
{
uint32_t valueIn32Bit = static_cast<uint32_t>(value);
if (valueIn32Bit == value)
return v8::Integer::NewFromUnsigned(isolate, value);
// V8 doesn't have a 64-bit integer implementation.
return v8::Number::New(isolate, value);
}
inline v8::Handle<v8::Value> toV8(int value, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
{
return toV8SignedIntegerInternal<sizeof value>(value, isolate);
}
inline v8::Handle<v8::Value> toV8(long value, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
{
return toV8SignedIntegerInternal<sizeof value>(value, isolate);
}
inline v8::Handle<v8::Value> toV8(long long value, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
{
return toV8SignedIntegerInternal<sizeof value>(value, isolate);
}
inline v8::Handle<v8::Value> toV8(unsigned value, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
{
return toV8UnsignedIntegerInternal<sizeof value>(value, isolate);
}
inline v8::Handle<v8::Value> toV8(unsigned long value, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
{
return toV8UnsignedIntegerInternal<sizeof value>(value, isolate);
}
inline v8::Handle<v8::Value> toV8(unsigned long long value, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
{
return toV8UnsignedIntegerInternal<sizeof value>(value, isolate);
}
inline v8::Handle<v8::Value> toV8(double value, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
{
return v8::Number::New(isolate, value);
}
inline v8::Handle<v8::Value> toV8(bool value, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
{
return v8::Boolean::New(isolate, value);
}
// Identity operator
inline v8::Handle<v8::Value> toV8(v8::Handle<v8::Value> value, v8::Handle<v8::Object> creationContext, v8::Isolate*)
{
return value;
}
// Undefined
struct ToV8UndefinedGenerator { }; // Used only for having toV8 return v8::Undefined.
inline v8::Handle<v8::Value> toV8(const ToV8UndefinedGenerator& value, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
{
return v8::Undefined(isolate);
}
// ScriptValue
inline v8::Handle<v8::Value> toV8(const ScriptValue& value, v8::Handle<v8::Object> creationContext, v8::Isolate*)
{
return value.v8Value();
}
// Array
template<typename Sequence>
inline v8::Handle<v8::Value> toV8SequenceInternal(const Sequence& sequence, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
{
v8::Local<v8::Array> array = v8::Array::New(isolate, sequence.size());
uint32_t index = 0;
typename Sequence::const_iterator end = sequence.end();
for (typename Sequence::const_iterator iter = sequence.begin(); iter != end; ++iter)
array->Set(v8::Integer::New(isolate, index++), toV8(*iter, creationContext, isolate));
return array;
}
template<typename T, size_t inlineCapacity>
inline v8::Handle<v8::Value> toV8(const Vector<T, inlineCapacity>& value, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
{
return toV8SequenceInternal(value, creationContext, isolate);
}
template<typename T, size_t inlineCapacity>
inline v8::Handle<v8::Value> toV8(const HeapVector<T, inlineCapacity>& value, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
{
return toV8SequenceInternal(value, creationContext, isolate);
}
} // namespace blink
#endif // ToV8_h
|