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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
|
/*
* Copyright (C) 2006, 2007, 2008, 2009 Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * 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.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 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 THE COPYRIGHT
* OWNER OR 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 "config.h"
#include "bindings/v8/V8Binding.h"
#include "V8Element.h"
#include "V8NodeFilter.h"
#include "V8Window.h"
#include "V8WorkerGlobalScope.h"
#include "V8XPathNSResolver.h"
#include "bindings/v8/ScriptController.h"
#include "bindings/v8/V8NodeFilterCondition.h"
#include "bindings/v8/V8ObjectConstructor.h"
#include "bindings/v8/V8WindowShell.h"
#include "bindings/v8/WorkerScriptController.h"
#include "bindings/v8/custom/V8CustomXPathNSResolver.h"
#include "core/dom/Element.h"
#include "core/dom/NodeFilter.h"
#include "core/dom/QualifiedName.h"
#include "core/inspector/BindingVisitors.h"
#include "core/loader/FrameLoader.h"
#include "core/loader/FrameLoaderClient.h"
#include "core/frame/Frame.h"
#include "core/frame/Settings.h"
#include "core/workers/WorkerGlobalScope.h"
#include "core/xml/XPathNSResolver.h"
#include "gin/public/isolate_holder.h"
#include "wtf/ArrayBufferContents.h"
#include "wtf/MainThread.h"
#include "wtf/MathExtras.h"
#include "wtf/StdLibExtras.h"
#include "wtf/Threading.h"
#include "wtf/text/AtomicString.h"
#include "wtf/text/CString.h"
#include "wtf/text/StringBuffer.h"
#include "wtf/text/StringHash.h"
#include "wtf/text/WTFString.h"
namespace WebCore {
v8::Handle<v8::Value> setDOMException(int exceptionCode, v8::Isolate* isolate)
{
// FIXME: pass in an ExceptionState instead for better creationContext.
return V8ThrowException::throwDOMException(exceptionCode, v8::Handle<v8::Object>(), isolate);
}
v8::Handle<v8::Value> setDOMException(int exceptionCode, const String& message, v8::Isolate* isolate)
{
return V8ThrowException::throwDOMException(exceptionCode, message, v8::Handle<v8::Object>(), isolate);
}
v8::Handle<v8::Value> throwError(V8ErrorType errorType, const String& message, v8::Isolate* isolate)
{
return V8ThrowException::throwError(errorType, message, isolate);
}
v8::Handle<v8::Value> throwError(v8::Handle<v8::Value> exception, v8::Isolate* isolate)
{
return V8ThrowException::throwError(exception, isolate);
}
v8::Handle<v8::Value> throwUninformativeAndGenericTypeError(v8::Isolate* isolate)
{
return V8ThrowException::throwTypeError(String(), isolate);
}
v8::Handle<v8::Value> throwTypeError(const String& message, v8::Isolate* isolate)
{
return V8ThrowException::throwTypeError(message, isolate);
}
class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
virtual void* Allocate(size_t size) OVERRIDE
{
void* data;
WTF::ArrayBufferContents::allocateMemory(size, WTF::ArrayBufferContents::ZeroInitialize, data);
return data;
}
virtual void* AllocateUninitialized(size_t size) OVERRIDE
{
void* data;
WTF::ArrayBufferContents::allocateMemory(size, WTF::ArrayBufferContents::DontInitialize, data);
return data;
}
virtual void Free(void* data, size_t size) OVERRIDE
{
WTF::ArrayBufferContents::freeMemory(data, size);
}
};
v8::ArrayBuffer::Allocator* v8ArrayBufferAllocator()
{
DEFINE_STATIC_LOCAL(ArrayBufferAllocator, arrayBufferAllocator, ());
return &arrayBufferAllocator;
}
PassRefPtr<NodeFilter> toNodeFilter(v8::Handle<v8::Value> callback, v8::Isolate* isolate)
{
RefPtr<NodeFilter> filter = NodeFilter::create();
// FIXME: Should pass in appropriate creationContext
v8::Handle<v8::Object> filterWrapper = toV8(filter, v8::Handle<v8::Object>(), isolate).As<v8::Object>();
RefPtr<NodeFilterCondition> condition = V8NodeFilterCondition::create(callback, filterWrapper, isolate);
filter->setCondition(condition.release());
return filter.release();
}
const int32_t kMaxInt32 = 0x7fffffff;
const int32_t kMinInt32 = -kMaxInt32 - 1;
const uint32_t kMaxUInt32 = 0xffffffff;
const int64_t kJSMaxInteger = 0x20000000000000LL - 1; // 2^53 - 1, maximum integer exactly representable in ECMAScript.
static double enforceRange(double x, double minimum, double maximum, bool& ok)
{
if (std::isnan(x) || std::isinf(x)) {
ok = false;
return 0;
}
x = trunc(x);
if (x < minimum || x > maximum) {
ok = false;
return 0;
}
return x;
}
template <typename T>
struct IntTypeLimits {
};
template <>
struct IntTypeLimits<int8_t> {
static const int8_t minValue = -128;
static const int8_t maxValue = 127;
static const unsigned numberOfValues = 256; // 2^8
};
template <>
struct IntTypeLimits<uint8_t> {
static const uint8_t maxValue = 255;
static const unsigned numberOfValues = 256; // 2^8
};
template <>
struct IntTypeLimits<int16_t> {
static const short minValue = -32768;
static const short maxValue = 32767;
static const unsigned numberOfValues = 65536; // 2^16
};
template <>
struct IntTypeLimits<uint16_t> {
static const unsigned short maxValue = 65535;
static const unsigned numberOfValues = 65536; // 2^16
};
template <typename T>
static inline T toSmallerInt(v8::Handle<v8::Value> value, IntegerConversionConfiguration configuration, bool& ok)
{
typedef IntTypeLimits<T> LimitsTrait;
ok = true;
// Fast case. The value is already a 32-bit integer in the right range.
if (value->IsInt32()) {
int32_t result = value->Int32Value();
if (result >= LimitsTrait::minValue && result <= LimitsTrait::maxValue)
return static_cast<T>(result);
if (configuration == EnforceRange) {
ok = false;
return 0;
}
result %= LimitsTrait::numberOfValues;
return static_cast<T>(result > LimitsTrait::maxValue ? result - LimitsTrait::numberOfValues : result);
}
// Can the value be converted to a number?
v8::Local<v8::Number> numberObject = value->ToNumber();
if (numberObject.IsEmpty()) {
ok = false;
return 0;
}
if (configuration == EnforceRange)
return enforceRange(numberObject->Value(), LimitsTrait::minValue, LimitsTrait::maxValue, ok);
double numberValue = numberObject->Value();
if (std::isnan(numberValue) || std::isinf(numberValue) || !numberValue)
return 0;
numberValue = numberValue < 0 ? -floor(fabs(numberValue)) : floor(fabs(numberValue));
numberValue = fmod(numberValue, LimitsTrait::numberOfValues);
return static_cast<T>(numberValue > LimitsTrait::maxValue ? numberValue - LimitsTrait::numberOfValues : numberValue);
}
template <typename T>
static inline T toSmallerUInt(v8::Handle<v8::Value> value, IntegerConversionConfiguration configuration, bool& ok)
{
typedef IntTypeLimits<T> LimitsTrait;
ok = true;
// Fast case. The value is a 32-bit signed integer - possibly positive?
if (value->IsInt32()) {
int32_t result = value->Int32Value();
if (result >= 0 && result <= LimitsTrait::maxValue)
return static_cast<T>(result);
if (configuration == EnforceRange) {
ok = false;
return 0;
}
return static_cast<T>(result);
}
// Can the value be converted to a number?
v8::Local<v8::Number> numberObject = value->ToNumber();
if (numberObject.IsEmpty()) {
ok = false;
return 0;
}
if (configuration == EnforceRange)
return enforceRange(numberObject->Value(), 0, LimitsTrait::maxValue, ok);
// Does the value convert to nan or to an infinity?
double numberValue = numberObject->Value();
if (std::isnan(numberValue) || std::isinf(numberValue) || !numberValue)
return 0;
if (configuration == Clamp)
return clampTo<T>(numberObject->Value());
numberValue = numberValue < 0 ? -floor(fabs(numberValue)) : floor(fabs(numberValue));
return static_cast<T>(fmod(numberValue, LimitsTrait::numberOfValues));
}
int8_t toInt8(v8::Handle<v8::Value> value, IntegerConversionConfiguration configuration, bool& ok)
{
return toSmallerInt<int8_t>(value, configuration, ok);
}
uint8_t toUInt8(v8::Handle<v8::Value> value, IntegerConversionConfiguration configuration, bool& ok)
{
return toSmallerUInt<uint8_t>(value, configuration, ok);
}
int16_t toInt16(v8::Handle<v8::Value> value, IntegerConversionConfiguration configuration, bool& ok)
{
return toSmallerInt<int16_t>(value, configuration, ok);
}
uint16_t toUInt16(v8::Handle<v8::Value> value, IntegerConversionConfiguration configuration, bool& ok)
{
return toSmallerUInt<uint16_t>(value, configuration, ok);
}
int32_t toInt32(v8::Handle<v8::Value> value, IntegerConversionConfiguration configuration, bool& ok)
{
ok = true;
// Fast case. The value is already a 32-bit integer.
if (value->IsInt32())
return value->Int32Value();
// Can the value be converted to a number?
ok = false;
V8TRYCATCH_RETURN(v8::Local<v8::Number>, numberObject, value->ToNumber(), 0);
if (numberObject.IsEmpty()) {
return 0;
}
ok = true;
if (configuration == EnforceRange)
return enforceRange(numberObject->Value(), kMinInt32, kMaxInt32, ok);
// Does the value convert to nan or to an infinity?
double numberValue = numberObject->Value();
if (std::isnan(numberValue) || std::isinf(numberValue))
return 0;
if (configuration == Clamp)
return clampTo<int32_t>(numberObject->Value());
V8TRYCATCH_RETURN(int32_t, result, numberObject->Int32Value(), 0);
return result;
}
uint32_t toUInt32(v8::Handle<v8::Value> value, IntegerConversionConfiguration configuration, bool& ok)
{
ok = true;
// Fast case. The value is already a 32-bit unsigned integer.
if (value->IsUint32())
return value->Uint32Value();
// Fast case. The value is a 32-bit signed integer - possibly positive?
if (value->IsInt32()) {
int32_t result = value->Int32Value();
if (result >= 0)
return result;
if (configuration == EnforceRange) {
ok = false;
return 0;
}
return result;
}
// Can the value be converted to a number?
ok = false;
V8TRYCATCH_RETURN(v8::Local<v8::Number>, numberObject, value->ToNumber(), 0);
if (numberObject.IsEmpty()) {
return 0;
}
ok = true;
if (configuration == EnforceRange)
return enforceRange(numberObject->Value(), 0, kMaxUInt32, ok);
// Does the value convert to nan or to an infinity?
double numberValue = numberObject->Value();
if (std::isnan(numberValue) || std::isinf(numberValue))
return 0;
if (configuration == Clamp)
return clampTo<uint32_t>(numberObject->Value());
V8TRYCATCH_RETURN(uint32_t, result, numberObject->Uint32Value(), 0);
return result;
}
int64_t toInt64(v8::Handle<v8::Value> value, IntegerConversionConfiguration configuration, bool& ok)
{
ok = true;
// Fast case. The value is a 32-bit integer.
if (value->IsInt32())
return value->Int32Value();
// Can the value be converted to a number?
v8::Local<v8::Number> numberObject = value->ToNumber();
if (numberObject.IsEmpty()) {
ok = false;
return 0;
}
double x = numberObject->Value();
if (configuration == EnforceRange)
return enforceRange(x, -kJSMaxInteger, kJSMaxInteger, ok);
// Does the value convert to nan or to an infinity?
if (std::isnan(x) || std::isinf(x))
return 0;
// NaNs and +/-Infinity should be 0, otherwise modulo 2^64.
unsigned long long integer;
doubleToInteger(x, integer);
return integer;
}
uint64_t toUInt64(v8::Handle<v8::Value> value, IntegerConversionConfiguration configuration, bool& ok)
{
ok = true;
// Fast case. The value is a 32-bit unsigned integer.
if (value->IsUint32())
return value->Uint32Value();
// Fast case. The value is a 32-bit integer.
if (value->IsInt32()) {
int32_t result = value->Int32Value();
if (result >= 0)
return result;
if (configuration == EnforceRange) {
ok = false;
return 0;
}
return result;
}
// Can the value be converted to a number?
v8::Local<v8::Number> numberObject = value->ToNumber();
if (numberObject.IsEmpty()) {
ok = false;
return 0;
}
double x = numberObject->Value();
if (configuration == EnforceRange)
return enforceRange(x, 0, kJSMaxInteger, ok);
// Does the value convert to nan or to an infinity?
if (std::isnan(x) || std::isinf(x))
return 0;
// NaNs and +/-Infinity should be 0, otherwise modulo 2^64.
unsigned long long integer;
doubleToInteger(x, integer);
return integer;
}
v8::Handle<v8::FunctionTemplate> createRawTemplate(v8::Isolate* isolate)
{
v8::EscapableHandleScope scope(isolate);
v8::Local<v8::FunctionTemplate> result = v8::FunctionTemplate::New(isolate, V8ObjectConstructor::isValidConstructorMode);
return scope.Escape(result);
}
PassRefPtr<XPathNSResolver> toXPathNSResolver(v8::Handle<v8::Value> value, v8::Isolate* isolate)
{
RefPtr<XPathNSResolver> resolver;
if (V8XPathNSResolver::hasInstance(value, isolate, worldType(isolate)))
resolver = V8XPathNSResolver::toNative(v8::Handle<v8::Object>::Cast(value));
else if (value->IsObject())
resolver = V8CustomXPathNSResolver::create(value->ToObject(), isolate);
return resolver;
}
v8::Handle<v8::Object> toInnerGlobalObject(v8::Handle<v8::Context> context)
{
return v8::Handle<v8::Object>::Cast(context->Global()->GetPrototype());
}
DOMWindow* toDOMWindow(v8::Handle<v8::Context> context)
{
v8::Handle<v8::Object> global = context->Global();
ASSERT(!global.IsEmpty());
v8::Handle<v8::Object> window = global->FindInstanceInPrototypeChain(V8Window::domTemplate(context->GetIsolate(), MainWorld));
if (!window.IsEmpty())
return V8Window::toNative(window);
window = global->FindInstanceInPrototypeChain(V8Window::domTemplate(context->GetIsolate(), IsolatedWorld));
ASSERT(!window.IsEmpty());
return V8Window::toNative(window);
}
ExecutionContext* toExecutionContext(v8::Handle<v8::Context> context)
{
v8::Handle<v8::Object> global = context->Global();
v8::Handle<v8::Object> windowWrapper = global->FindInstanceInPrototypeChain(V8Window::domTemplate(context->GetIsolate(), MainWorld));
if (!windowWrapper.IsEmpty())
return V8Window::toNative(windowWrapper)->executionContext();
windowWrapper = global->FindInstanceInPrototypeChain(V8Window::domTemplate(context->GetIsolate(), IsolatedWorld));
if (!windowWrapper.IsEmpty())
return V8Window::toNative(windowWrapper)->executionContext();
v8::Handle<v8::Object> workerWrapper = global->FindInstanceInPrototypeChain(V8WorkerGlobalScope::domTemplate(context->GetIsolate(), WorkerWorld));
if (!workerWrapper.IsEmpty())
return V8WorkerGlobalScope::toNative(workerWrapper)->executionContext();
// FIXME: Is this line of code reachable?
return 0;
}
DOMWindow* activeDOMWindow()
{
v8::Handle<v8::Context> context = v8::Isolate::GetCurrent()->GetCallingContext();
if (context.IsEmpty()) {
// Unfortunately, when processing script from a plug-in, we might not
// have a calling context. In those cases, we fall back to the
// entered context.
context = v8::Isolate::GetCurrent()->GetEnteredContext();
}
return toDOMWindow(context);
}
ExecutionContext* activeExecutionContext()
{
v8::Handle<v8::Context> context = v8::Isolate::GetCurrent()->GetCallingContext();
if (context.IsEmpty()) {
// Unfortunately, when processing script from a plug-in, we might not
// have a calling context. In those cases, we fall back to the
// entered context.
context = v8::Isolate::GetCurrent()->GetEnteredContext();
}
return toExecutionContext(context);
}
DOMWindow* firstDOMWindow()
{
return toDOMWindow(v8::Isolate::GetCurrent()->GetEnteredContext());
}
Document* currentDocument()
{
return toDOMWindow(v8::Isolate::GetCurrent()->GetCurrentContext())->document();
}
Frame* toFrameIfNotDetached(v8::Handle<v8::Context> context)
{
DOMWindow* window = toDOMWindow(context);
if (window->isCurrentlyDisplayedInFrame())
return window->frame();
// We return 0 here because |context| is detached from the Frame. If we
// did return |frame| we could get in trouble because the frame could be
// navigated to another security origin.
return 0;
}
v8::Local<v8::Context> toV8Context(ExecutionContext* context, DOMWrapperWorld* world)
{
if (context->isDocument()) {
ASSERT(world);
if (Frame* frame = toDocument(context)->frame())
return frame->script().windowShell(world)->context();
} else if (context->isWorkerGlobalScope()) {
ASSERT(!world);
if (WorkerScriptController* script = toWorkerGlobalScope(context)->script())
return script->context();
}
return v8::Local<v8::Context>();
}
bool handleOutOfMemory()
{
v8::Local<v8::Context> context = v8::Isolate::GetCurrent()->GetCurrentContext();
if (!context->HasOutOfMemoryException())
return false;
// Warning, error, disable JS for this frame?
Frame* frame = toFrameIfNotDetached(context);
if (!frame)
return true;
frame->script().clearForOutOfMemory();
frame->loader().client()->didExhaustMemoryAvailableForScript();
if (Settings* settings = frame->settings())
settings->setScriptEnabled(false);
return true;
}
v8::Local<v8::Value> handleMaxRecursionDepthExceeded(v8::Isolate* isolate)
{
throwError(v8RangeError, "Maximum call stack size exceeded.", isolate);
return v8::Local<v8::Value>();
}
void crashIfV8IsDead()
{
if (v8::V8::IsDead()) {
// FIXME: We temporarily deal with V8 internal error situations
// such as out-of-memory by crashing the renderer.
CRASH();
}
}
WrapperWorldType worldType(v8::Isolate* isolate)
{
V8PerIsolateData* data = V8PerIsolateData::from(isolate);
if (!data->workerDOMDataStore())
return worldTypeInMainThread(isolate);
return WorkerWorld;
}
WrapperWorldType worldTypeInMainThread(v8::Isolate* isolate)
{
if (!DOMWrapperWorld::isolatedWorldsExist())
return MainWorld;
ASSERT(!isolate->GetEnteredContext().IsEmpty());
DOMWrapperWorld* isolatedWorld = DOMWrapperWorld::isolatedWorld(isolate->GetEnteredContext());
if (isolatedWorld)
return IsolatedWorld;
return MainWorld;
}
DOMWrapperWorld* isolatedWorldForIsolate(v8::Isolate* isolate)
{
V8PerIsolateData* data = V8PerIsolateData::from(isolate);
if (data->workerDOMDataStore())
return 0;
if (!DOMWrapperWorld::isolatedWorldsExist())
return 0;
ASSERT(isolate->InContext());
return DOMWrapperWorld::isolatedWorld(isolate->GetCurrentContext());
}
v8::Local<v8::Value> getHiddenValueFromMainWorldWrapper(v8::Isolate* isolate, ScriptWrappable* wrappable, v8::Handle<v8::String> key)
{
v8::Local<v8::Object> wrapper = wrappable->newLocalWrapper(isolate);
return wrapper.IsEmpty() ? v8::Local<v8::Value>() : wrapper->GetHiddenValue(key);
}
static gin::IsolateHolder* mainIsolateHolder = 0;
v8::Isolate* mainThreadIsolate()
{
ASSERT(mainIsolateHolder);
ASSERT(isMainThread());
return mainIsolateHolder->isolate();
}
void setMainThreadIsolate(v8::Isolate* isolate)
{
ASSERT(!mainIsolateHolder || !isolate);
ASSERT(isMainThread());
if (isolate) {
mainIsolateHolder = new gin::IsolateHolder(isolate);
} else {
delete mainIsolateHolder;
mainIsolateHolder = 0;
}
}
v8::Isolate* toIsolate(ExecutionContext* context)
{
if (context && context->isDocument())
return mainThreadIsolate();
return v8::Isolate::GetCurrent();
}
v8::Isolate* toIsolate(Frame* frame)
{
return frame->script().isolate();
}
} // namespace WebCore
|