libv8 3.16.14.19.1-universal-darwin

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4937 @@
1
+ // Copyright 2012 the V8 project authors. All rights reserved.
2
+ // Redistribution and use in source and binary forms, with or without
3
+ // modification, are permitted provided that the following conditions are
4
+ // met:
5
+ //
6
+ // * Redistributions of source code must retain the above copyright
7
+ // notice, this list of conditions and the following disclaimer.
8
+ // * Redistributions in binary form must reproduce the above
9
+ // copyright notice, this list of conditions and the following
10
+ // disclaimer in the documentation and/or other materials provided
11
+ // with the distribution.
12
+ // * Neither the name of Google Inc. nor the names of its
13
+ // contributors may be used to endorse or promote products derived
14
+ // from this software without specific prior written permission.
15
+ //
16
+ // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17
+ // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18
+ // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19
+ // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20
+ // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
+ // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
+ // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
+ // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
+ // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
+ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26
+ // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
+
28
+ /** \mainpage V8 API Reference Guide
29
+ *
30
+ * V8 is Google's open source JavaScript engine.
31
+ *
32
+ * This set of documents provides reference material generated from the
33
+ * V8 header file, include/v8.h.
34
+ *
35
+ * For other documentation see http://code.google.com/apis/v8/
36
+ */
37
+
38
+ #ifndef V8_H_
39
+ #define V8_H_
40
+
41
+ // TODO(svenpanne) Remove me when the Chrome bindings are adapted.
42
+ #define V8_DISABLE_DEPRECATIONS 1
43
+
44
+ #include "v8stdint.h"
45
+
46
+ #ifdef _WIN32
47
+
48
+ // Setup for Windows DLL export/import. When building the V8 DLL the
49
+ // BUILDING_V8_SHARED needs to be defined. When building a program which uses
50
+ // the V8 DLL USING_V8_SHARED needs to be defined. When either building the V8
51
+ // static library or building a program which uses the V8 static library neither
52
+ // BUILDING_V8_SHARED nor USING_V8_SHARED should be defined.
53
+ #if defined(BUILDING_V8_SHARED) && defined(USING_V8_SHARED)
54
+ #error both BUILDING_V8_SHARED and USING_V8_SHARED are set - please check the\
55
+ build configuration to ensure that at most one of these is set
56
+ #endif
57
+
58
+ #ifdef BUILDING_V8_SHARED
59
+ #define V8EXPORT __declspec(dllexport)
60
+ #elif USING_V8_SHARED
61
+ #define V8EXPORT __declspec(dllimport)
62
+ #else
63
+ #define V8EXPORT
64
+ #endif // BUILDING_V8_SHARED
65
+
66
+ #else // _WIN32
67
+
68
+ // Setup for Linux shared library export.
69
+ #if defined(__GNUC__) && ((__GNUC__ >= 4) || \
70
+ (__GNUC__ == 3 && __GNUC_MINOR__ >= 3)) && defined(V8_SHARED)
71
+ #ifdef BUILDING_V8_SHARED
72
+ #define V8EXPORT __attribute__ ((visibility("default")))
73
+ #else
74
+ #define V8EXPORT
75
+ #endif
76
+ #else
77
+ #define V8EXPORT
78
+ #endif
79
+
80
+ #endif // _WIN32
81
+
82
+ #if defined(__GNUC__) && !defined(DEBUG)
83
+ #define V8_INLINE(declarator) inline __attribute__((always_inline)) declarator
84
+ #elif defined(_MSC_VER) && !defined(DEBUG)
85
+ #define V8_INLINE(declarator) __forceinline declarator
86
+ #else
87
+ #define V8_INLINE(declarator) inline declarator
88
+ #endif
89
+
90
+ #if defined(__GNUC__) && !V8_DISABLE_DEPRECATIONS
91
+ #define V8_DEPRECATED(declarator) declarator __attribute__ ((deprecated))
92
+ #elif defined(_MSC_VER) && !V8_DISABLE_DEPRECATIONS
93
+ #define V8_DEPRECATED(declarator) __declspec(deprecated) declarator
94
+ #else
95
+ #define V8_DEPRECATED(declarator) declarator
96
+ #endif
97
+
98
+ /**
99
+ * The v8 JavaScript engine.
100
+ */
101
+ namespace v8 {
102
+
103
+ class Context;
104
+ class String;
105
+ class StringObject;
106
+ class Value;
107
+ class Utils;
108
+ class Number;
109
+ class NumberObject;
110
+ class Object;
111
+ class Array;
112
+ class Int32;
113
+ class Uint32;
114
+ class External;
115
+ class Primitive;
116
+ class Boolean;
117
+ class BooleanObject;
118
+ class Integer;
119
+ class Function;
120
+ class Date;
121
+ class ImplementationUtilities;
122
+ class Signature;
123
+ class AccessorSignature;
124
+ template <class T> class Handle;
125
+ template <class T> class Local;
126
+ template <class T> class Persistent;
127
+ class FunctionTemplate;
128
+ class ObjectTemplate;
129
+ class Data;
130
+ class AccessorInfo;
131
+ class StackTrace;
132
+ class StackFrame;
133
+ class Isolate;
134
+
135
+ namespace internal {
136
+
137
+ class Arguments;
138
+ class Object;
139
+ class Heap;
140
+ class HeapObject;
141
+ class Isolate;
142
+ }
143
+
144
+
145
+ // --- Weak Handles ---
146
+
147
+
148
+ /**
149
+ * A weak reference callback function.
150
+ *
151
+ * This callback should either explicitly invoke Dispose on |object| if
152
+ * V8 wrapper is not needed anymore, or 'revive' it by invocation of MakeWeak.
153
+ *
154
+ * \param object the weak global object to be reclaimed by the garbage collector
155
+ * \param parameter the value passed in when making the weak global object
156
+ */
157
+ typedef void (*WeakReferenceCallback)(Persistent<Value> object,
158
+ void* parameter);
159
+
160
+ // TODO(svenpanne) Temporary definition until Chrome is in sync.
161
+ typedef void (*NearDeathCallback)(Isolate* isolate,
162
+ Persistent<Value> object,
163
+ void* parameter);
164
+
165
+ // --- Handles ---
166
+
167
+ #define TYPE_CHECK(T, S) \
168
+ while (false) { \
169
+ *(static_cast<T* volatile*>(0)) = static_cast<S*>(0); \
170
+ }
171
+
172
+ /**
173
+ * An object reference managed by the v8 garbage collector.
174
+ *
175
+ * All objects returned from v8 have to be tracked by the garbage
176
+ * collector so that it knows that the objects are still alive. Also,
177
+ * because the garbage collector may move objects, it is unsafe to
178
+ * point directly to an object. Instead, all objects are stored in
179
+ * handles which are known by the garbage collector and updated
180
+ * whenever an object moves. Handles should always be passed by value
181
+ * (except in cases like out-parameters) and they should never be
182
+ * allocated on the heap.
183
+ *
184
+ * There are two types of handles: local and persistent handles.
185
+ * Local handles are light-weight and transient and typically used in
186
+ * local operations. They are managed by HandleScopes. Persistent
187
+ * handles can be used when storing objects across several independent
188
+ * operations and have to be explicitly deallocated when they're no
189
+ * longer used.
190
+ *
191
+ * It is safe to extract the object stored in the handle by
192
+ * dereferencing the handle (for instance, to extract the Object* from
193
+ * a Handle<Object>); the value will still be governed by a handle
194
+ * behind the scenes and the same rules apply to these values as to
195
+ * their handles.
196
+ */
197
+ template <class T> class Handle {
198
+ public:
199
+ /**
200
+ * Creates an empty handle.
201
+ */
202
+ V8_INLINE(Handle()) : val_(0) {}
203
+
204
+ /**
205
+ * Creates a new handle for the specified value.
206
+ */
207
+ V8_INLINE(explicit Handle(T* val)) : val_(val) {}
208
+
209
+ /**
210
+ * Creates a handle for the contents of the specified handle. This
211
+ * constructor allows you to pass handles as arguments by value and
212
+ * to assign between handles. However, if you try to assign between
213
+ * incompatible handles, for instance from a Handle<String> to a
214
+ * Handle<Number> it will cause a compile-time error. Assigning
215
+ * between compatible handles, for instance assigning a
216
+ * Handle<String> to a variable declared as Handle<Value>, is legal
217
+ * because String is a subclass of Value.
218
+ */
219
+ template <class S> V8_INLINE(Handle(Handle<S> that))
220
+ : val_(reinterpret_cast<T*>(*that)) {
221
+ /**
222
+ * This check fails when trying to convert between incompatible
223
+ * handles. For example, converting from a Handle<String> to a
224
+ * Handle<Number>.
225
+ */
226
+ TYPE_CHECK(T, S);
227
+ }
228
+
229
+ /**
230
+ * Returns true if the handle is empty.
231
+ */
232
+ V8_INLINE(bool IsEmpty() const) { return val_ == 0; }
233
+
234
+ /**
235
+ * Sets the handle to be empty. IsEmpty() will then return true.
236
+ */
237
+ V8_INLINE(void Clear()) { val_ = 0; }
238
+
239
+ V8_INLINE(T* operator->() const) { return val_; }
240
+
241
+ V8_INLINE(T* operator*() const) { return val_; }
242
+
243
+ /**
244
+ * Checks whether two handles are the same.
245
+ * Returns true if both are empty, or if the objects
246
+ * to which they refer are identical.
247
+ * The handles' references are not checked.
248
+ */
249
+ template <class S> V8_INLINE(bool operator==(Handle<S> that) const) {
250
+ internal::Object** a = reinterpret_cast<internal::Object**>(**this);
251
+ internal::Object** b = reinterpret_cast<internal::Object**>(*that);
252
+ if (a == 0) return b == 0;
253
+ if (b == 0) return false;
254
+ return *a == *b;
255
+ }
256
+
257
+ /**
258
+ * Checks whether two handles are different.
259
+ * Returns true if only one of the handles is empty, or if
260
+ * the objects to which they refer are different.
261
+ * The handles' references are not checked.
262
+ */
263
+ template <class S> V8_INLINE(bool operator!=(Handle<S> that) const) {
264
+ return !operator==(that);
265
+ }
266
+
267
+ template <class S> V8_INLINE(static Handle<T> Cast(Handle<S> that)) {
268
+ #ifdef V8_ENABLE_CHECKS
269
+ // If we're going to perform the type check then we have to check
270
+ // that the handle isn't empty before doing the checked cast.
271
+ if (that.IsEmpty()) return Handle<T>();
272
+ #endif
273
+ return Handle<T>(T::Cast(*that));
274
+ }
275
+
276
+ template <class S> V8_INLINE(Handle<S> As()) {
277
+ return Handle<S>::Cast(*this);
278
+ }
279
+
280
+ private:
281
+ T* val_;
282
+ };
283
+
284
+
285
+ /**
286
+ * A light-weight stack-allocated object handle. All operations
287
+ * that return objects from within v8 return them in local handles. They
288
+ * are created within HandleScopes, and all local handles allocated within a
289
+ * handle scope are destroyed when the handle scope is destroyed. Hence it
290
+ * is not necessary to explicitly deallocate local handles.
291
+ */
292
+ template <class T> class Local : public Handle<T> {
293
+ public:
294
+ V8_INLINE(Local());
295
+ template <class S> V8_INLINE(Local(Local<S> that))
296
+ : Handle<T>(reinterpret_cast<T*>(*that)) {
297
+ /**
298
+ * This check fails when trying to convert between incompatible
299
+ * handles. For example, converting from a Handle<String> to a
300
+ * Handle<Number>.
301
+ */
302
+ TYPE_CHECK(T, S);
303
+ }
304
+ template <class S> V8_INLINE(Local(S* that) : Handle<T>(that)) { }
305
+ template <class S> V8_INLINE(static Local<T> Cast(Local<S> that)) {
306
+ #ifdef V8_ENABLE_CHECKS
307
+ // If we're going to perform the type check then we have to check
308
+ // that the handle isn't empty before doing the checked cast.
309
+ if (that.IsEmpty()) return Local<T>();
310
+ #endif
311
+ return Local<T>(T::Cast(*that));
312
+ }
313
+
314
+ template <class S> V8_INLINE(Local<S> As()) {
315
+ return Local<S>::Cast(*this);
316
+ }
317
+
318
+ /**
319
+ * Create a local handle for the content of another handle.
320
+ * The referee is kept alive by the local handle even when
321
+ * the original handle is destroyed/disposed.
322
+ */
323
+ V8_INLINE(static Local<T> New(Handle<T> that));
324
+ V8_INLINE(static Local<T> New(Isolate* isolate, Handle<T> that));
325
+ };
326
+
327
+
328
+ /**
329
+ * An object reference that is independent of any handle scope. Where
330
+ * a Local handle only lives as long as the HandleScope in which it was
331
+ * allocated, a Persistent handle remains valid until it is explicitly
332
+ * disposed.
333
+ *
334
+ * A persistent handle contains a reference to a storage cell within
335
+ * the v8 engine which holds an object value and which is updated by
336
+ * the garbage collector whenever the object is moved. A new storage
337
+ * cell can be created using Persistent::New and existing handles can
338
+ * be disposed using Persistent::Dispose. Since persistent handles
339
+ * are passed by value you may have many persistent handle objects
340
+ * that point to the same storage cell. For instance, if you pass a
341
+ * persistent handle as an argument to a function you will not get two
342
+ * different storage cells but rather two references to the same
343
+ * storage cell.
344
+ */
345
+ template <class T> class Persistent : public Handle<T> {
346
+ public:
347
+ /**
348
+ * Creates an empty persistent handle that doesn't point to any
349
+ * storage cell.
350
+ */
351
+ V8_INLINE(Persistent());
352
+
353
+ /**
354
+ * Creates a persistent handle for the same storage cell as the
355
+ * specified handle. This constructor allows you to pass persistent
356
+ * handles as arguments by value and to assign between persistent
357
+ * handles. However, attempting to assign between incompatible
358
+ * persistent handles, for instance from a Persistent<String> to a
359
+ * Persistent<Number> will cause a compile-time error. Assigning
360
+ * between compatible persistent handles, for instance assigning a
361
+ * Persistent<String> to a variable declared as Persistent<Value>,
362
+ * is allowed as String is a subclass of Value.
363
+ */
364
+ template <class S> V8_INLINE(Persistent(Persistent<S> that))
365
+ : Handle<T>(reinterpret_cast<T*>(*that)) {
366
+ /**
367
+ * This check fails when trying to convert between incompatible
368
+ * handles. For example, converting from a Handle<String> to a
369
+ * Handle<Number>.
370
+ */
371
+ TYPE_CHECK(T, S);
372
+ }
373
+
374
+ template <class S> V8_INLINE(Persistent(S* that)) : Handle<T>(that) { }
375
+
376
+ /**
377
+ * "Casts" a plain handle which is known to be a persistent handle
378
+ * to a persistent handle.
379
+ */
380
+ template <class S> explicit V8_INLINE(Persistent(Handle<S> that))
381
+ : Handle<T>(*that) { }
382
+
383
+ template <class S> V8_INLINE(static Persistent<T> Cast(Persistent<S> that)) {
384
+ #ifdef V8_ENABLE_CHECKS
385
+ // If we're going to perform the type check then we have to check
386
+ // that the handle isn't empty before doing the checked cast.
387
+ if (that.IsEmpty()) return Persistent<T>();
388
+ #endif
389
+ return Persistent<T>(T::Cast(*that));
390
+ }
391
+
392
+ template <class S> V8_INLINE(Persistent<S> As()) {
393
+ return Persistent<S>::Cast(*this);
394
+ }
395
+
396
+ /** Deprecated. Use Isolate version instead. */
397
+ V8_DEPRECATED(static Persistent<T> New(Handle<T> that));
398
+
399
+ /**
400
+ * Creates a new persistent handle for an existing local or persistent handle.
401
+ */
402
+ V8_INLINE(static Persistent<T> New(Isolate* isolate, Handle<T> that));
403
+
404
+ /** Deprecated. Use Isolate version instead. */
405
+ V8_DEPRECATED(void Dispose());
406
+
407
+ /**
408
+ * Releases the storage cell referenced by this persistent handle.
409
+ * Does not remove the reference to the cell from any handles.
410
+ * This handle's reference, and any other references to the storage
411
+ * cell remain and IsEmpty will still return false.
412
+ */
413
+ V8_INLINE(void Dispose(Isolate* isolate));
414
+
415
+ /** Deprecated. Use Isolate version instead. */
416
+ V8_DEPRECATED(void MakeWeak(void* parameters,
417
+ WeakReferenceCallback callback));
418
+
419
+ /**
420
+ * Make the reference to this object weak. When only weak handles
421
+ * refer to the object, the garbage collector will perform a
422
+ * callback to the given V8::NearDeathCallback function, passing
423
+ * it the object reference and the given parameters.
424
+ */
425
+ V8_INLINE(void MakeWeak(Isolate* isolate,
426
+ void* parameters,
427
+ NearDeathCallback callback));
428
+
429
+ /** Deprecated. Use Isolate version instead. */
430
+ V8_DEPRECATED(void ClearWeak());
431
+
432
+ /** Clears the weak reference to this object. */
433
+ V8_INLINE(void ClearWeak(Isolate* isolate));
434
+
435
+ /** Deprecated. Use Isolate version instead. */
436
+ V8_DEPRECATED(void MarkIndependent());
437
+
438
+ /**
439
+ * Marks the reference to this object independent. Garbage collector is free
440
+ * to ignore any object groups containing this object. Weak callback for an
441
+ * independent handle should not assume that it will be preceded by a global
442
+ * GC prologue callback or followed by a global GC epilogue callback.
443
+ */
444
+ V8_INLINE(void MarkIndependent(Isolate* isolate));
445
+
446
+ /** Deprecated. Use Isolate version instead. */
447
+ V8_DEPRECATED(void MarkPartiallyDependent());
448
+
449
+ /**
450
+ * Marks the reference to this object partially dependent. Partially dependent
451
+ * handles only depend on other partially dependent handles and these
452
+ * dependencies are provided through object groups. It provides a way to build
453
+ * smaller object groups for young objects that represent only a subset of all
454
+ * external dependencies. This mark is automatically cleared after each
455
+ * garbage collection.
456
+ */
457
+ V8_INLINE(void MarkPartiallyDependent(Isolate* isolate));
458
+
459
+ /** Deprecated. Use Isolate version instead. */
460
+ V8_DEPRECATED(bool IsIndependent() const);
461
+
462
+ /** Returns true if this handle was previously marked as independent. */
463
+ V8_INLINE(bool IsIndependent(Isolate* isolate) const);
464
+
465
+ /** Deprecated. Use Isolate version instead. */
466
+ V8_DEPRECATED(bool IsNearDeath() const);
467
+
468
+ /** Checks if the handle holds the only reference to an object. */
469
+ V8_INLINE(bool IsNearDeath(Isolate* isolate) const);
470
+
471
+ /** Deprecated. Use Isolate version instead. */
472
+ V8_DEPRECATED(bool IsWeak() const);
473
+
474
+ /** Returns true if the handle's reference is weak. */
475
+ V8_INLINE(bool IsWeak(Isolate* isolate) const);
476
+
477
+ /** Deprecated. Use Isolate version instead. */
478
+ V8_DEPRECATED(void SetWrapperClassId(uint16_t class_id));
479
+
480
+ /**
481
+ * Assigns a wrapper class ID to the handle. See RetainedObjectInfo interface
482
+ * description in v8-profiler.h for details.
483
+ */
484
+ V8_INLINE(void SetWrapperClassId(Isolate* isolate, uint16_t class_id));
485
+
486
+ /** Deprecated. Use Isolate version instead. */
487
+ V8_DEPRECATED(uint16_t WrapperClassId() const);
488
+
489
+ /**
490
+ * Returns the class ID previously assigned to this handle or 0 if no class ID
491
+ * was previously assigned.
492
+ */
493
+ V8_INLINE(uint16_t WrapperClassId(Isolate* isolate) const);
494
+
495
+ private:
496
+ friend class ImplementationUtilities;
497
+ friend class ObjectTemplate;
498
+ };
499
+
500
+
501
+ /**
502
+ * A stack-allocated class that governs a number of local handles.
503
+ * After a handle scope has been created, all local handles will be
504
+ * allocated within that handle scope until either the handle scope is
505
+ * deleted or another handle scope is created. If there is already a
506
+ * handle scope and a new one is created, all allocations will take
507
+ * place in the new handle scope until it is deleted. After that,
508
+ * new handles will again be allocated in the original handle scope.
509
+ *
510
+ * After the handle scope of a local handle has been deleted the
511
+ * garbage collector will no longer track the object stored in the
512
+ * handle and may deallocate it. The behavior of accessing a handle
513
+ * for which the handle scope has been deleted is undefined.
514
+ */
515
+ class V8EXPORT HandleScope {
516
+ public:
517
+ HandleScope();
518
+
519
+ ~HandleScope();
520
+
521
+ /**
522
+ * Closes the handle scope and returns the value as a handle in the
523
+ * previous scope, which is the new current scope after the call.
524
+ */
525
+ template <class T> Local<T> Close(Handle<T> value);
526
+
527
+ /**
528
+ * Counts the number of allocated handles.
529
+ */
530
+ static int NumberOfHandles();
531
+
532
+ /**
533
+ * Creates a new handle with the given value.
534
+ */
535
+ static internal::Object** CreateHandle(internal::Object* value);
536
+ static internal::Object** CreateHandle(internal::Isolate* isolate,
537
+ internal::Object* value);
538
+ // Faster version, uses HeapObject to obtain the current Isolate.
539
+ static internal::Object** CreateHandle(internal::HeapObject* value);
540
+
541
+ private:
542
+ // Make it hard to create heap-allocated or illegal handle scopes by
543
+ // disallowing certain operations.
544
+ HandleScope(const HandleScope&);
545
+ void operator=(const HandleScope&);
546
+ void* operator new(size_t size);
547
+ void operator delete(void*, size_t);
548
+
549
+ // This Data class is accessible internally as HandleScopeData through a
550
+ // typedef in the ImplementationUtilities class.
551
+ class V8EXPORT Data {
552
+ public:
553
+ internal::Object** next;
554
+ internal::Object** limit;
555
+ int level;
556
+ V8_INLINE(void Initialize()) {
557
+ next = limit = NULL;
558
+ level = 0;
559
+ }
560
+ };
561
+
562
+ void Leave();
563
+
564
+ internal::Isolate* isolate_;
565
+ internal::Object** prev_next_;
566
+ internal::Object** prev_limit_;
567
+
568
+ // Allow for the active closing of HandleScopes which allows to pass a handle
569
+ // from the HandleScope being closed to the next top most HandleScope.
570
+ bool is_closed_;
571
+ internal::Object** RawClose(internal::Object** value);
572
+
573
+ friend class ImplementationUtilities;
574
+ };
575
+
576
+
577
+ // --- Special objects ---
578
+
579
+
580
+ /**
581
+ * The superclass of values and API object templates.
582
+ */
583
+ class V8EXPORT Data {
584
+ private:
585
+ Data();
586
+ };
587
+
588
+
589
+ /**
590
+ * Pre-compilation data that can be associated with a script. This
591
+ * data can be calculated for a script in advance of actually
592
+ * compiling it, and can be stored between compilations. When script
593
+ * data is given to the compile method compilation will be faster.
594
+ */
595
+ class V8EXPORT ScriptData { // NOLINT
596
+ public:
597
+ virtual ~ScriptData() { }
598
+
599
+ /**
600
+ * Pre-compiles the specified script (context-independent).
601
+ *
602
+ * \param input Pointer to UTF-8 script source code.
603
+ * \param length Length of UTF-8 script source code.
604
+ */
605
+ static ScriptData* PreCompile(const char* input, int length);
606
+
607
+ /**
608
+ * Pre-compiles the specified script (context-independent).
609
+ *
610
+ * NOTE: Pre-compilation using this method cannot happen on another thread
611
+ * without using Lockers.
612
+ *
613
+ * \param source Script source code.
614
+ */
615
+ static ScriptData* PreCompile(Handle<String> source);
616
+
617
+ /**
618
+ * Load previous pre-compilation data.
619
+ *
620
+ * \param data Pointer to data returned by a call to Data() of a previous
621
+ * ScriptData. Ownership is not transferred.
622
+ * \param length Length of data.
623
+ */
624
+ static ScriptData* New(const char* data, int length);
625
+
626
+ /**
627
+ * Returns the length of Data().
628
+ */
629
+ virtual int Length() = 0;
630
+
631
+ /**
632
+ * Returns a serialized representation of this ScriptData that can later be
633
+ * passed to New(). NOTE: Serialized data is platform-dependent.
634
+ */
635
+ virtual const char* Data() = 0;
636
+
637
+ /**
638
+ * Returns true if the source code could not be parsed.
639
+ */
640
+ virtual bool HasError() = 0;
641
+ };
642
+
643
+
644
+ /**
645
+ * The origin, within a file, of a script.
646
+ */
647
+ class ScriptOrigin {
648
+ public:
649
+ V8_INLINE(ScriptOrigin(
650
+ Handle<Value> resource_name,
651
+ Handle<Integer> resource_line_offset = Handle<Integer>(),
652
+ Handle<Integer> resource_column_offset = Handle<Integer>()))
653
+ : resource_name_(resource_name),
654
+ resource_line_offset_(resource_line_offset),
655
+ resource_column_offset_(resource_column_offset) { }
656
+ V8_INLINE(Handle<Value> ResourceName() const);
657
+ V8_INLINE(Handle<Integer> ResourceLineOffset() const);
658
+ V8_INLINE(Handle<Integer> ResourceColumnOffset() const);
659
+ private:
660
+ Handle<Value> resource_name_;
661
+ Handle<Integer> resource_line_offset_;
662
+ Handle<Integer> resource_column_offset_;
663
+ };
664
+
665
+
666
+ /**
667
+ * A compiled JavaScript script.
668
+ */
669
+ class V8EXPORT Script {
670
+ public:
671
+ /**
672
+ * Compiles the specified script (context-independent).
673
+ *
674
+ * \param source Script source code.
675
+ * \param origin Script origin, owned by caller, no references are kept
676
+ * when New() returns
677
+ * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
678
+ * using pre_data speeds compilation if it's done multiple times.
679
+ * Owned by caller, no references are kept when New() returns.
680
+ * \param script_data Arbitrary data associated with script. Using
681
+ * this has same effect as calling SetData(), but allows data to be
682
+ * available to compile event handlers.
683
+ * \return Compiled script object (context independent; when run it
684
+ * will use the currently entered context).
685
+ */
686
+ static Local<Script> New(Handle<String> source,
687
+ ScriptOrigin* origin = NULL,
688
+ ScriptData* pre_data = NULL,
689
+ Handle<String> script_data = Handle<String>());
690
+
691
+ /**
692
+ * Compiles the specified script using the specified file name
693
+ * object (typically a string) as the script's origin.
694
+ *
695
+ * \param source Script source code.
696
+ * \param file_name file name object (typically a string) to be used
697
+ * as the script's origin.
698
+ * \return Compiled script object (context independent; when run it
699
+ * will use the currently entered context).
700
+ */
701
+ static Local<Script> New(Handle<String> source,
702
+ Handle<Value> file_name);
703
+
704
+ /**
705
+ * Compiles the specified script (bound to current context).
706
+ *
707
+ * \param source Script source code.
708
+ * \param origin Script origin, owned by caller, no references are kept
709
+ * when Compile() returns
710
+ * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
711
+ * using pre_data speeds compilation if it's done multiple times.
712
+ * Owned by caller, no references are kept when Compile() returns.
713
+ * \param script_data Arbitrary data associated with script. Using
714
+ * this has same effect as calling SetData(), but makes data available
715
+ * earlier (i.e. to compile event handlers).
716
+ * \return Compiled script object, bound to the context that was active
717
+ * when this function was called. When run it will always use this
718
+ * context.
719
+ */
720
+ static Local<Script> Compile(Handle<String> source,
721
+ ScriptOrigin* origin = NULL,
722
+ ScriptData* pre_data = NULL,
723
+ Handle<String> script_data = Handle<String>());
724
+
725
+ /**
726
+ * Compiles the specified script using the specified file name
727
+ * object (typically a string) as the script's origin.
728
+ *
729
+ * \param source Script source code.
730
+ * \param file_name File name to use as script's origin
731
+ * \param script_data Arbitrary data associated with script. Using
732
+ * this has same effect as calling SetData(), but makes data available
733
+ * earlier (i.e. to compile event handlers).
734
+ * \return Compiled script object, bound to the context that was active
735
+ * when this function was called. When run it will always use this
736
+ * context.
737
+ */
738
+ static Local<Script> Compile(Handle<String> source,
739
+ Handle<Value> file_name,
740
+ Handle<String> script_data = Handle<String>());
741
+
742
+ /**
743
+ * Runs the script returning the resulting value. If the script is
744
+ * context independent (created using ::New) it will be run in the
745
+ * currently entered context. If it is context specific (created
746
+ * using ::Compile) it will be run in the context in which it was
747
+ * compiled.
748
+ */
749
+ Local<Value> Run();
750
+
751
+ /**
752
+ * Returns the script id value.
753
+ */
754
+ Local<Value> Id();
755
+
756
+ /**
757
+ * Associate an additional data object with the script. This is mainly used
758
+ * with the debugger as this data object is only available through the
759
+ * debugger API.
760
+ */
761
+ void SetData(Handle<String> data);
762
+ };
763
+
764
+
765
+ /**
766
+ * An error message.
767
+ */
768
+ class V8EXPORT Message {
769
+ public:
770
+ Local<String> Get() const;
771
+ Local<String> GetSourceLine() const;
772
+
773
+ /**
774
+ * Returns the resource name for the script from where the function causing
775
+ * the error originates.
776
+ */
777
+ Handle<Value> GetScriptResourceName() const;
778
+
779
+ /**
780
+ * Returns the resource data for the script from where the function causing
781
+ * the error originates.
782
+ */
783
+ Handle<Value> GetScriptData() const;
784
+
785
+ /**
786
+ * Exception stack trace. By default stack traces are not captured for
787
+ * uncaught exceptions. SetCaptureStackTraceForUncaughtExceptions allows
788
+ * to change this option.
789
+ */
790
+ Handle<StackTrace> GetStackTrace() const;
791
+
792
+ /**
793
+ * Returns the number, 1-based, of the line where the error occurred.
794
+ */
795
+ int GetLineNumber() const;
796
+
797
+ /**
798
+ * Returns the index within the script of the first character where
799
+ * the error occurred.
800
+ */
801
+ int GetStartPosition() const;
802
+
803
+ /**
804
+ * Returns the index within the script of the last character where
805
+ * the error occurred.
806
+ */
807
+ int GetEndPosition() const;
808
+
809
+ /**
810
+ * Returns the index within the line of the first character where
811
+ * the error occurred.
812
+ */
813
+ int GetStartColumn() const;
814
+
815
+ /**
816
+ * Returns the index within the line of the last character where
817
+ * the error occurred.
818
+ */
819
+ int GetEndColumn() const;
820
+
821
+ // TODO(1245381): Print to a string instead of on a FILE.
822
+ static void PrintCurrentStackTrace(FILE* out);
823
+
824
+ static const int kNoLineNumberInfo = 0;
825
+ static const int kNoColumnInfo = 0;
826
+ };
827
+
828
+
829
+ /**
830
+ * Representation of a JavaScript stack trace. The information collected is a
831
+ * snapshot of the execution stack and the information remains valid after
832
+ * execution continues.
833
+ */
834
+ class V8EXPORT StackTrace {
835
+ public:
836
+ /**
837
+ * Flags that determine what information is placed captured for each
838
+ * StackFrame when grabbing the current stack trace.
839
+ */
840
+ enum StackTraceOptions {
841
+ kLineNumber = 1,
842
+ kColumnOffset = 1 << 1 | kLineNumber,
843
+ kScriptName = 1 << 2,
844
+ kFunctionName = 1 << 3,
845
+ kIsEval = 1 << 4,
846
+ kIsConstructor = 1 << 5,
847
+ kScriptNameOrSourceURL = 1 << 6,
848
+ kOverview = kLineNumber | kColumnOffset | kScriptName | kFunctionName,
849
+ kDetailed = kOverview | kIsEval | kIsConstructor | kScriptNameOrSourceURL
850
+ };
851
+
852
+ /**
853
+ * Returns a StackFrame at a particular index.
854
+ */
855
+ Local<StackFrame> GetFrame(uint32_t index) const;
856
+
857
+ /**
858
+ * Returns the number of StackFrames.
859
+ */
860
+ int GetFrameCount() const;
861
+
862
+ /**
863
+ * Returns StackTrace as a v8::Array that contains StackFrame objects.
864
+ */
865
+ Local<Array> AsArray();
866
+
867
+ /**
868
+ * Grab a snapshot of the current JavaScript execution stack.
869
+ *
870
+ * \param frame_limit The maximum number of stack frames we want to capture.
871
+ * \param options Enumerates the set of things we will capture for each
872
+ * StackFrame.
873
+ */
874
+ static Local<StackTrace> CurrentStackTrace(
875
+ int frame_limit,
876
+ StackTraceOptions options = kOverview);
877
+ };
878
+
879
+
880
+ /**
881
+ * A single JavaScript stack frame.
882
+ */
883
+ class V8EXPORT StackFrame {
884
+ public:
885
+ /**
886
+ * Returns the number, 1-based, of the line for the associate function call.
887
+ * This method will return Message::kNoLineNumberInfo if it is unable to
888
+ * retrieve the line number, or if kLineNumber was not passed as an option
889
+ * when capturing the StackTrace.
890
+ */
891
+ int GetLineNumber() const;
892
+
893
+ /**
894
+ * Returns the 1-based column offset on the line for the associated function
895
+ * call.
896
+ * This method will return Message::kNoColumnInfo if it is unable to retrieve
897
+ * the column number, or if kColumnOffset was not passed as an option when
898
+ * capturing the StackTrace.
899
+ */
900
+ int GetColumn() const;
901
+
902
+ /**
903
+ * Returns the name of the resource that contains the script for the
904
+ * function for this StackFrame.
905
+ */
906
+ Local<String> GetScriptName() const;
907
+
908
+ /**
909
+ * Returns the name of the resource that contains the script for the
910
+ * function for this StackFrame or sourceURL value if the script name
911
+ * is undefined and its source ends with //@ sourceURL=... string.
912
+ */
913
+ Local<String> GetScriptNameOrSourceURL() const;
914
+
915
+ /**
916
+ * Returns the name of the function associated with this stack frame.
917
+ */
918
+ Local<String> GetFunctionName() const;
919
+
920
+ /**
921
+ * Returns whether or not the associated function is compiled via a call to
922
+ * eval().
923
+ */
924
+ bool IsEval() const;
925
+
926
+ /**
927
+ * Returns whether or not the associated function is called as a
928
+ * constructor via "new".
929
+ */
930
+ bool IsConstructor() const;
931
+ };
932
+
933
+
934
+ // --- Value ---
935
+
936
+
937
+ /**
938
+ * The superclass of all JavaScript values and objects.
939
+ */
940
+ class V8EXPORT Value : public Data {
941
+ public:
942
+ /**
943
+ * Returns true if this value is the undefined value. See ECMA-262
944
+ * 4.3.10.
945
+ */
946
+ V8_INLINE(bool IsUndefined() const);
947
+
948
+ /**
949
+ * Returns true if this value is the null value. See ECMA-262
950
+ * 4.3.11.
951
+ */
952
+ V8_INLINE(bool IsNull() const);
953
+
954
+ /**
955
+ * Returns true if this value is true.
956
+ */
957
+ bool IsTrue() const;
958
+
959
+ /**
960
+ * Returns true if this value is false.
961
+ */
962
+ bool IsFalse() const;
963
+
964
+ /**
965
+ * Returns true if this value is an instance of the String type.
966
+ * See ECMA-262 8.4.
967
+ */
968
+ V8_INLINE(bool IsString() const);
969
+
970
+ /**
971
+ * Returns true if this value is a function.
972
+ */
973
+ bool IsFunction() const;
974
+
975
+ /**
976
+ * Returns true if this value is an array.
977
+ */
978
+ bool IsArray() const;
979
+
980
+ /**
981
+ * Returns true if this value is an object.
982
+ */
983
+ bool IsObject() const;
984
+
985
+ /**
986
+ * Returns true if this value is boolean.
987
+ */
988
+ bool IsBoolean() const;
989
+
990
+ /**
991
+ * Returns true if this value is a number.
992
+ */
993
+ bool IsNumber() const;
994
+
995
+ /**
996
+ * Returns true if this value is external.
997
+ */
998
+ bool IsExternal() const;
999
+
1000
+ /**
1001
+ * Returns true if this value is a 32-bit signed integer.
1002
+ */
1003
+ bool IsInt32() const;
1004
+
1005
+ /**
1006
+ * Returns true if this value is a 32-bit unsigned integer.
1007
+ */
1008
+ bool IsUint32() const;
1009
+
1010
+ /**
1011
+ * Returns true if this value is a Date.
1012
+ */
1013
+ bool IsDate() const;
1014
+
1015
+ /**
1016
+ * Returns true if this value is a Boolean object.
1017
+ */
1018
+ bool IsBooleanObject() const;
1019
+
1020
+ /**
1021
+ * Returns true if this value is a Number object.
1022
+ */
1023
+ bool IsNumberObject() const;
1024
+
1025
+ /**
1026
+ * Returns true if this value is a String object.
1027
+ */
1028
+ bool IsStringObject() const;
1029
+
1030
+ /**
1031
+ * Returns true if this value is a NativeError.
1032
+ */
1033
+ bool IsNativeError() const;
1034
+
1035
+ /**
1036
+ * Returns true if this value is a RegExp.
1037
+ */
1038
+ bool IsRegExp() const;
1039
+
1040
+ Local<Boolean> ToBoolean() const;
1041
+ Local<Number> ToNumber() const;
1042
+ Local<String> ToString() const;
1043
+ Local<String> ToDetailString() const;
1044
+ Local<Object> ToObject() const;
1045
+ Local<Integer> ToInteger() const;
1046
+ Local<Uint32> ToUint32() const;
1047
+ Local<Int32> ToInt32() const;
1048
+
1049
+ /**
1050
+ * Attempts to convert a string to an array index.
1051
+ * Returns an empty handle if the conversion fails.
1052
+ */
1053
+ Local<Uint32> ToArrayIndex() const;
1054
+
1055
+ bool BooleanValue() const;
1056
+ double NumberValue() const;
1057
+ int64_t IntegerValue() const;
1058
+ uint32_t Uint32Value() const;
1059
+ int32_t Int32Value() const;
1060
+
1061
+ /** JS == */
1062
+ bool Equals(Handle<Value> that) const;
1063
+ bool StrictEquals(Handle<Value> that) const;
1064
+
1065
+ private:
1066
+ V8_INLINE(bool QuickIsUndefined() const);
1067
+ V8_INLINE(bool QuickIsNull() const);
1068
+ V8_INLINE(bool QuickIsString() const);
1069
+ bool FullIsUndefined() const;
1070
+ bool FullIsNull() const;
1071
+ bool FullIsString() const;
1072
+ };
1073
+
1074
+
1075
+ /**
1076
+ * The superclass of primitive values. See ECMA-262 4.3.2.
1077
+ */
1078
+ class V8EXPORT Primitive : public Value { };
1079
+
1080
+
1081
+ /**
1082
+ * A primitive boolean value (ECMA-262, 4.3.14). Either the true
1083
+ * or false value.
1084
+ */
1085
+ class V8EXPORT Boolean : public Primitive {
1086
+ public:
1087
+ bool Value() const;
1088
+ V8_INLINE(static Handle<Boolean> New(bool value));
1089
+ };
1090
+
1091
+
1092
+ /**
1093
+ * A JavaScript string value (ECMA-262, 4.3.17).
1094
+ */
1095
+ class V8EXPORT String : public Primitive {
1096
+ public:
1097
+ enum Encoding {
1098
+ UNKNOWN_ENCODING = 0x1,
1099
+ TWO_BYTE_ENCODING = 0x0,
1100
+ ASCII_ENCODING = 0x4,
1101
+ ONE_BYTE_ENCODING = 0x4
1102
+ };
1103
+ /**
1104
+ * Returns the number of characters in this string.
1105
+ */
1106
+ int Length() const;
1107
+
1108
+ /**
1109
+ * Returns the number of bytes in the UTF-8 encoded
1110
+ * representation of this string.
1111
+ */
1112
+ int Utf8Length() const;
1113
+
1114
+ /**
1115
+ * A fast conservative check for non-ASCII characters. May
1116
+ * return true even for ASCII strings, but if it returns
1117
+ * false you can be sure that all characters are in the range
1118
+ * 0-127.
1119
+ */
1120
+ bool MayContainNonAscii() const;
1121
+
1122
+ /**
1123
+ * Returns whether this string contains only one byte data.
1124
+ */
1125
+ bool IsOneByte() const;
1126
+
1127
+ /**
1128
+ * Write the contents of the string to an external buffer.
1129
+ * If no arguments are given, expects the buffer to be large
1130
+ * enough to hold the entire string and NULL terminator. Copies
1131
+ * the contents of the string and the NULL terminator into the
1132
+ * buffer.
1133
+ *
1134
+ * WriteUtf8 will not write partial UTF-8 sequences, preferring to stop
1135
+ * before the end of the buffer.
1136
+ *
1137
+ * Copies up to length characters into the output buffer.
1138
+ * Only null-terminates if there is enough space in the buffer.
1139
+ *
1140
+ * \param buffer The buffer into which the string will be copied.
1141
+ * \param start The starting position within the string at which
1142
+ * copying begins.
1143
+ * \param length The number of characters to copy from the string. For
1144
+ * WriteUtf8 the number of bytes in the buffer.
1145
+ * \param nchars_ref The number of characters written, can be NULL.
1146
+ * \param options Various options that might affect performance of this or
1147
+ * subsequent operations.
1148
+ * \return The number of characters copied to the buffer excluding the null
1149
+ * terminator. For WriteUtf8: The number of bytes copied to the buffer
1150
+ * including the null terminator (if written).
1151
+ */
1152
+ enum WriteOptions {
1153
+ NO_OPTIONS = 0,
1154
+ HINT_MANY_WRITES_EXPECTED = 1,
1155
+ NO_NULL_TERMINATION = 2,
1156
+ PRESERVE_ASCII_NULL = 4
1157
+ };
1158
+
1159
+ // 16-bit character codes.
1160
+ int Write(uint16_t* buffer,
1161
+ int start = 0,
1162
+ int length = -1,
1163
+ int options = NO_OPTIONS) const;
1164
+ // ASCII characters.
1165
+ int WriteAscii(char* buffer,
1166
+ int start = 0,
1167
+ int length = -1,
1168
+ int options = NO_OPTIONS) const;
1169
+ // One byte characters.
1170
+ int WriteOneByte(uint8_t* buffer,
1171
+ int start = 0,
1172
+ int length = -1,
1173
+ int options = NO_OPTIONS) const;
1174
+ // UTF-8 encoded characters.
1175
+ int WriteUtf8(char* buffer,
1176
+ int length = -1,
1177
+ int* nchars_ref = NULL,
1178
+ int options = NO_OPTIONS) const;
1179
+
1180
+ /**
1181
+ * A zero length string.
1182
+ */
1183
+ static v8::Local<v8::String> Empty();
1184
+ V8_INLINE(static v8::Local<v8::String> Empty(Isolate* isolate));
1185
+
1186
+ /**
1187
+ * Returns true if the string is external
1188
+ */
1189
+ bool IsExternal() const;
1190
+
1191
+ /**
1192
+ * Returns true if the string is both external and ASCII
1193
+ */
1194
+ bool IsExternalAscii() const;
1195
+
1196
+ class V8EXPORT ExternalStringResourceBase { // NOLINT
1197
+ public:
1198
+ virtual ~ExternalStringResourceBase() {}
1199
+
1200
+ protected:
1201
+ ExternalStringResourceBase() {}
1202
+
1203
+ /**
1204
+ * Internally V8 will call this Dispose method when the external string
1205
+ * resource is no longer needed. The default implementation will use the
1206
+ * delete operator. This method can be overridden in subclasses to
1207
+ * control how allocated external string resources are disposed.
1208
+ */
1209
+ virtual void Dispose() { delete this; }
1210
+
1211
+ private:
1212
+ // Disallow copying and assigning.
1213
+ ExternalStringResourceBase(const ExternalStringResourceBase&);
1214
+ void operator=(const ExternalStringResourceBase&);
1215
+
1216
+ friend class v8::internal::Heap;
1217
+ };
1218
+
1219
+ /**
1220
+ * An ExternalStringResource is a wrapper around a two-byte string
1221
+ * buffer that resides outside V8's heap. Implement an
1222
+ * ExternalStringResource to manage the life cycle of the underlying
1223
+ * buffer. Note that the string data must be immutable.
1224
+ */
1225
+ class V8EXPORT ExternalStringResource
1226
+ : public ExternalStringResourceBase {
1227
+ public:
1228
+ /**
1229
+ * Override the destructor to manage the life cycle of the underlying
1230
+ * buffer.
1231
+ */
1232
+ virtual ~ExternalStringResource() {}
1233
+
1234
+ /**
1235
+ * The string data from the underlying buffer.
1236
+ */
1237
+ virtual const uint16_t* data() const = 0;
1238
+
1239
+ /**
1240
+ * The length of the string. That is, the number of two-byte characters.
1241
+ */
1242
+ virtual size_t length() const = 0;
1243
+
1244
+ protected:
1245
+ ExternalStringResource() {}
1246
+ };
1247
+
1248
+ /**
1249
+ * An ExternalAsciiStringResource is a wrapper around an ASCII
1250
+ * string buffer that resides outside V8's heap. Implement an
1251
+ * ExternalAsciiStringResource to manage the life cycle of the
1252
+ * underlying buffer. Note that the string data must be immutable
1253
+ * and that the data must be strict (7-bit) ASCII, not Latin-1 or
1254
+ * UTF-8, which would require special treatment internally in the
1255
+ * engine and, in the case of UTF-8, do not allow efficient indexing.
1256
+ * Use String::New or convert to 16 bit data for non-ASCII.
1257
+ */
1258
+
1259
+ class V8EXPORT ExternalAsciiStringResource
1260
+ : public ExternalStringResourceBase {
1261
+ public:
1262
+ /**
1263
+ * Override the destructor to manage the life cycle of the underlying
1264
+ * buffer.
1265
+ */
1266
+ virtual ~ExternalAsciiStringResource() {}
1267
+ /** The string data from the underlying buffer.*/
1268
+ virtual const char* data() const = 0;
1269
+ /** The number of ASCII characters in the string.*/
1270
+ virtual size_t length() const = 0;
1271
+ protected:
1272
+ ExternalAsciiStringResource() {}
1273
+ };
1274
+
1275
+ typedef ExternalAsciiStringResource ExternalOneByteStringResource;
1276
+
1277
+ /**
1278
+ * If the string is an external string, return the ExternalStringResourceBase
1279
+ * regardless of the encoding, otherwise return NULL. The encoding of the
1280
+ * string is returned in encoding_out.
1281
+ */
1282
+ V8_INLINE(ExternalStringResourceBase* GetExternalStringResourceBase(
1283
+ Encoding* encoding_out) const);
1284
+
1285
+ /**
1286
+ * Get the ExternalStringResource for an external string. Returns
1287
+ * NULL if IsExternal() doesn't return true.
1288
+ */
1289
+ V8_INLINE(ExternalStringResource* GetExternalStringResource() const);
1290
+
1291
+ /**
1292
+ * Get the ExternalAsciiStringResource for an external ASCII string.
1293
+ * Returns NULL if IsExternalAscii() doesn't return true.
1294
+ */
1295
+ const ExternalAsciiStringResource* GetExternalAsciiStringResource() const;
1296
+
1297
+ V8_INLINE(static String* Cast(v8::Value* obj));
1298
+
1299
+ /**
1300
+ * Allocates a new string from either UTF-8 encoded or ASCII data.
1301
+ * The second parameter 'length' gives the buffer length. If omitted,
1302
+ * the function calls 'strlen' to determine the buffer length.
1303
+ */
1304
+ static Local<String> New(const char* data, int length = -1);
1305
+
1306
+ /** Allocates a new string from 16-bit character codes.*/
1307
+ static Local<String> New(const uint16_t* data, int length = -1);
1308
+
1309
+ /** Creates a symbol. Returns one if it exists already.*/
1310
+ static Local<String> NewSymbol(const char* data, int length = -1);
1311
+
1312
+ /**
1313
+ * Creates a new string by concatenating the left and the right strings
1314
+ * passed in as parameters.
1315
+ */
1316
+ static Local<String> Concat(Handle<String> left, Handle<String> right);
1317
+
1318
+ /**
1319
+ * Creates a new external string using the data defined in the given
1320
+ * resource. When the external string is no longer live on V8's heap the
1321
+ * resource will be disposed by calling its Dispose method. The caller of
1322
+ * this function should not otherwise delete or modify the resource. Neither
1323
+ * should the underlying buffer be deallocated or modified except through the
1324
+ * destructor of the external string resource.
1325
+ */
1326
+ static Local<String> NewExternal(ExternalStringResource* resource);
1327
+
1328
+ /**
1329
+ * Associate an external string resource with this string by transforming it
1330
+ * in place so that existing references to this string in the JavaScript heap
1331
+ * will use the external string resource. The external string resource's
1332
+ * character contents need to be equivalent to this string.
1333
+ * Returns true if the string has been changed to be an external string.
1334
+ * The string is not modified if the operation fails. See NewExternal for
1335
+ * information on the lifetime of the resource.
1336
+ */
1337
+ bool MakeExternal(ExternalStringResource* resource);
1338
+
1339
+ /**
1340
+ * Creates a new external string using the ASCII data defined in the given
1341
+ * resource. When the external string is no longer live on V8's heap the
1342
+ * resource will be disposed by calling its Dispose method. The caller of
1343
+ * this function should not otherwise delete or modify the resource. Neither
1344
+ * should the underlying buffer be deallocated or modified except through the
1345
+ * destructor of the external string resource.
1346
+ */
1347
+ static Local<String> NewExternal(ExternalAsciiStringResource* resource);
1348
+
1349
+ /**
1350
+ * Associate an external string resource with this string by transforming it
1351
+ * in place so that existing references to this string in the JavaScript heap
1352
+ * will use the external string resource. The external string resource's
1353
+ * character contents need to be equivalent to this string.
1354
+ * Returns true if the string has been changed to be an external string.
1355
+ * The string is not modified if the operation fails. See NewExternal for
1356
+ * information on the lifetime of the resource.
1357
+ */
1358
+ bool MakeExternal(ExternalAsciiStringResource* resource);
1359
+
1360
+ /**
1361
+ * Returns true if this string can be made external.
1362
+ */
1363
+ bool CanMakeExternal();
1364
+
1365
+ /** Creates an undetectable string from the supplied ASCII or UTF-8 data.*/
1366
+ static Local<String> NewUndetectable(const char* data, int length = -1);
1367
+
1368
+ /** Creates an undetectable string from the supplied 16-bit character codes.*/
1369
+ static Local<String> NewUndetectable(const uint16_t* data, int length = -1);
1370
+
1371
+ /**
1372
+ * Converts an object to a UTF-8-encoded character array. Useful if
1373
+ * you want to print the object. If conversion to a string fails
1374
+ * (e.g. due to an exception in the toString() method of the object)
1375
+ * then the length() method returns 0 and the * operator returns
1376
+ * NULL.
1377
+ */
1378
+ class V8EXPORT Utf8Value {
1379
+ public:
1380
+ explicit Utf8Value(Handle<v8::Value> obj);
1381
+ ~Utf8Value();
1382
+ char* operator*() { return str_; }
1383
+ const char* operator*() const { return str_; }
1384
+ int length() const { return length_; }
1385
+ private:
1386
+ char* str_;
1387
+ int length_;
1388
+
1389
+ // Disallow copying and assigning.
1390
+ Utf8Value(const Utf8Value&);
1391
+ void operator=(const Utf8Value&);
1392
+ };
1393
+
1394
+ /**
1395
+ * Converts an object to an ASCII string.
1396
+ * Useful if you want to print the object.
1397
+ * If conversion to a string fails (eg. due to an exception in the toString()
1398
+ * method of the object) then the length() method returns 0 and the * operator
1399
+ * returns NULL.
1400
+ */
1401
+ class V8EXPORT AsciiValue {
1402
+ public:
1403
+ explicit AsciiValue(Handle<v8::Value> obj);
1404
+ ~AsciiValue();
1405
+ char* operator*() { return str_; }
1406
+ const char* operator*() const { return str_; }
1407
+ int length() const { return length_; }
1408
+ private:
1409
+ char* str_;
1410
+ int length_;
1411
+
1412
+ // Disallow copying and assigning.
1413
+ AsciiValue(const AsciiValue&);
1414
+ void operator=(const AsciiValue&);
1415
+ };
1416
+
1417
+ /**
1418
+ * Converts an object to a two-byte string.
1419
+ * If conversion to a string fails (eg. due to an exception in the toString()
1420
+ * method of the object) then the length() method returns 0 and the * operator
1421
+ * returns NULL.
1422
+ */
1423
+ class V8EXPORT Value {
1424
+ public:
1425
+ explicit Value(Handle<v8::Value> obj);
1426
+ ~Value();
1427
+ uint16_t* operator*() { return str_; }
1428
+ const uint16_t* operator*() const { return str_; }
1429
+ int length() const { return length_; }
1430
+ private:
1431
+ uint16_t* str_;
1432
+ int length_;
1433
+
1434
+ // Disallow copying and assigning.
1435
+ Value(const Value&);
1436
+ void operator=(const Value&);
1437
+ };
1438
+
1439
+ private:
1440
+ void VerifyExternalStringResourceBase(ExternalStringResourceBase* v,
1441
+ Encoding encoding) const;
1442
+ void VerifyExternalStringResource(ExternalStringResource* val) const;
1443
+ static void CheckCast(v8::Value* obj);
1444
+ };
1445
+
1446
+
1447
+ /**
1448
+ * A JavaScript number value (ECMA-262, 4.3.20)
1449
+ */
1450
+ class V8EXPORT Number : public Primitive {
1451
+ public:
1452
+ double Value() const;
1453
+ static Local<Number> New(double value);
1454
+ V8_INLINE(static Number* Cast(v8::Value* obj));
1455
+ private:
1456
+ Number();
1457
+ static void CheckCast(v8::Value* obj);
1458
+ };
1459
+
1460
+
1461
+ /**
1462
+ * A JavaScript value representing a signed integer.
1463
+ */
1464
+ class V8EXPORT Integer : public Number {
1465
+ public:
1466
+ static Local<Integer> New(int32_t value);
1467
+ static Local<Integer> NewFromUnsigned(uint32_t value);
1468
+ static Local<Integer> New(int32_t value, Isolate*);
1469
+ static Local<Integer> NewFromUnsigned(uint32_t value, Isolate*);
1470
+ int64_t Value() const;
1471
+ V8_INLINE(static Integer* Cast(v8::Value* obj));
1472
+ private:
1473
+ Integer();
1474
+ static void CheckCast(v8::Value* obj);
1475
+ };
1476
+
1477
+
1478
+ /**
1479
+ * A JavaScript value representing a 32-bit signed integer.
1480
+ */
1481
+ class V8EXPORT Int32 : public Integer {
1482
+ public:
1483
+ int32_t Value() const;
1484
+ private:
1485
+ Int32();
1486
+ };
1487
+
1488
+
1489
+ /**
1490
+ * A JavaScript value representing a 32-bit unsigned integer.
1491
+ */
1492
+ class V8EXPORT Uint32 : public Integer {
1493
+ public:
1494
+ uint32_t Value() const;
1495
+ private:
1496
+ Uint32();
1497
+ };
1498
+
1499
+
1500
+ enum PropertyAttribute {
1501
+ None = 0,
1502
+ ReadOnly = 1 << 0,
1503
+ DontEnum = 1 << 1,
1504
+ DontDelete = 1 << 2
1505
+ };
1506
+
1507
+ enum ExternalArrayType {
1508
+ kExternalByteArray = 1,
1509
+ kExternalUnsignedByteArray,
1510
+ kExternalShortArray,
1511
+ kExternalUnsignedShortArray,
1512
+ kExternalIntArray,
1513
+ kExternalUnsignedIntArray,
1514
+ kExternalFloatArray,
1515
+ kExternalDoubleArray,
1516
+ kExternalPixelArray
1517
+ };
1518
+
1519
+ /**
1520
+ * Accessor[Getter|Setter] are used as callback functions when
1521
+ * setting|getting a particular property. See Object and ObjectTemplate's
1522
+ * method SetAccessor.
1523
+ */
1524
+ typedef Handle<Value> (*AccessorGetter)(Local<String> property,
1525
+ const AccessorInfo& info);
1526
+
1527
+
1528
+ typedef void (*AccessorSetter)(Local<String> property,
1529
+ Local<Value> value,
1530
+ const AccessorInfo& info);
1531
+
1532
+
1533
+ /**
1534
+ * Access control specifications.
1535
+ *
1536
+ * Some accessors should be accessible across contexts. These
1537
+ * accessors have an explicit access control parameter which specifies
1538
+ * the kind of cross-context access that should be allowed.
1539
+ *
1540
+ * Additionally, for security, accessors can prohibit overwriting by
1541
+ * accessors defined in JavaScript. For objects that have such
1542
+ * accessors either locally or in their prototype chain it is not
1543
+ * possible to overwrite the accessor by using __defineGetter__ or
1544
+ * __defineSetter__ from JavaScript code.
1545
+ */
1546
+ enum AccessControl {
1547
+ DEFAULT = 0,
1548
+ ALL_CAN_READ = 1,
1549
+ ALL_CAN_WRITE = 1 << 1,
1550
+ PROHIBITS_OVERWRITING = 1 << 2
1551
+ };
1552
+
1553
+
1554
+ /**
1555
+ * A JavaScript object (ECMA-262, 4.3.3)
1556
+ */
1557
+ class V8EXPORT Object : public Value {
1558
+ public:
1559
+ bool Set(Handle<Value> key,
1560
+ Handle<Value> value,
1561
+ PropertyAttribute attribs = None);
1562
+
1563
+ bool Set(uint32_t index, Handle<Value> value);
1564
+
1565
+ // Sets a local property on this object bypassing interceptors and
1566
+ // overriding accessors or read-only properties.
1567
+ //
1568
+ // Note that if the object has an interceptor the property will be set
1569
+ // locally, but since the interceptor takes precedence the local property
1570
+ // will only be returned if the interceptor doesn't return a value.
1571
+ //
1572
+ // Note also that this only works for named properties.
1573
+ bool ForceSet(Handle<Value> key,
1574
+ Handle<Value> value,
1575
+ PropertyAttribute attribs = None);
1576
+
1577
+ Local<Value> Get(Handle<Value> key);
1578
+
1579
+ Local<Value> Get(uint32_t index);
1580
+
1581
+ /**
1582
+ * Gets the property attributes of a property which can be None or
1583
+ * any combination of ReadOnly, DontEnum and DontDelete. Returns
1584
+ * None when the property doesn't exist.
1585
+ */
1586
+ PropertyAttribute GetPropertyAttributes(Handle<Value> key);
1587
+
1588
+ // TODO(1245389): Replace the type-specific versions of these
1589
+ // functions with generic ones that accept a Handle<Value> key.
1590
+ bool Has(Handle<String> key);
1591
+
1592
+ bool Delete(Handle<String> key);
1593
+
1594
+ // Delete a property on this object bypassing interceptors and
1595
+ // ignoring dont-delete attributes.
1596
+ bool ForceDelete(Handle<Value> key);
1597
+
1598
+ bool Has(uint32_t index);
1599
+
1600
+ bool Delete(uint32_t index);
1601
+
1602
+ bool SetAccessor(Handle<String> name,
1603
+ AccessorGetter getter,
1604
+ AccessorSetter setter = 0,
1605
+ Handle<Value> data = Handle<Value>(),
1606
+ AccessControl settings = DEFAULT,
1607
+ PropertyAttribute attribute = None);
1608
+
1609
+ /**
1610
+ * Returns an array containing the names of the enumerable properties
1611
+ * of this object, including properties from prototype objects. The
1612
+ * array returned by this method contains the same values as would
1613
+ * be enumerated by a for-in statement over this object.
1614
+ */
1615
+ Local<Array> GetPropertyNames();
1616
+
1617
+ /**
1618
+ * This function has the same functionality as GetPropertyNames but
1619
+ * the returned array doesn't contain the names of properties from
1620
+ * prototype objects.
1621
+ */
1622
+ Local<Array> GetOwnPropertyNames();
1623
+
1624
+ /**
1625
+ * Get the prototype object. This does not skip objects marked to
1626
+ * be skipped by __proto__ and it does not consult the security
1627
+ * handler.
1628
+ */
1629
+ Local<Value> GetPrototype();
1630
+
1631
+ /**
1632
+ * Set the prototype object. This does not skip objects marked to
1633
+ * be skipped by __proto__ and it does not consult the security
1634
+ * handler.
1635
+ */
1636
+ bool SetPrototype(Handle<Value> prototype);
1637
+
1638
+ /**
1639
+ * Finds an instance of the given function template in the prototype
1640
+ * chain.
1641
+ */
1642
+ Local<Object> FindInstanceInPrototypeChain(Handle<FunctionTemplate> tmpl);
1643
+
1644
+ /**
1645
+ * Call builtin Object.prototype.toString on this object.
1646
+ * This is different from Value::ToString() that may call
1647
+ * user-defined toString function. This one does not.
1648
+ */
1649
+ Local<String> ObjectProtoToString();
1650
+
1651
+ /**
1652
+ * Returns the function invoked as a constructor for this object.
1653
+ * May be the null value.
1654
+ */
1655
+ Local<Value> GetConstructor();
1656
+
1657
+ /**
1658
+ * Returns the name of the function invoked as a constructor for this object.
1659
+ */
1660
+ Local<String> GetConstructorName();
1661
+
1662
+ /** Gets the number of internal fields for this Object. */
1663
+ int InternalFieldCount();
1664
+
1665
+ /** Gets the value from an internal field. */
1666
+ V8_INLINE(Local<Value> GetInternalField(int index));
1667
+
1668
+ /** Sets the value in an internal field. */
1669
+ void SetInternalField(int index, Handle<Value> value);
1670
+
1671
+ /**
1672
+ * Gets a 2-byte-aligned native pointer from an internal field. This field
1673
+ * must have been set by SetAlignedPointerInInternalField, everything else
1674
+ * leads to undefined behavior.
1675
+ */
1676
+ V8_INLINE(void* GetAlignedPointerFromInternalField(int index));
1677
+
1678
+ /**
1679
+ * Sets a 2-byte-aligned native pointer in an internal field. To retrieve such
1680
+ * a field, GetAlignedPointerFromInternalField must be used, everything else
1681
+ * leads to undefined behavior.
1682
+ */
1683
+ void SetAlignedPointerInInternalField(int index, void* value);
1684
+
1685
+ // Testers for local properties.
1686
+ bool HasOwnProperty(Handle<String> key);
1687
+ bool HasRealNamedProperty(Handle<String> key);
1688
+ bool HasRealIndexedProperty(uint32_t index);
1689
+ bool HasRealNamedCallbackProperty(Handle<String> key);
1690
+
1691
+ /**
1692
+ * If result.IsEmpty() no real property was located in the prototype chain.
1693
+ * This means interceptors in the prototype chain are not called.
1694
+ */
1695
+ Local<Value> GetRealNamedPropertyInPrototypeChain(Handle<String> key);
1696
+
1697
+ /**
1698
+ * If result.IsEmpty() no real property was located on the object or
1699
+ * in the prototype chain.
1700
+ * This means interceptors in the prototype chain are not called.
1701
+ */
1702
+ Local<Value> GetRealNamedProperty(Handle<String> key);
1703
+
1704
+ /** Tests for a named lookup interceptor.*/
1705
+ bool HasNamedLookupInterceptor();
1706
+
1707
+ /** Tests for an index lookup interceptor.*/
1708
+ bool HasIndexedLookupInterceptor();
1709
+
1710
+ /**
1711
+ * Turns on access check on the object if the object is an instance of
1712
+ * a template that has access check callbacks. If an object has no
1713
+ * access check info, the object cannot be accessed by anyone.
1714
+ */
1715
+ void TurnOnAccessCheck();
1716
+
1717
+ /**
1718
+ * Returns the identity hash for this object. The current implementation
1719
+ * uses a hidden property on the object to store the identity hash.
1720
+ *
1721
+ * The return value will never be 0. Also, it is not guaranteed to be
1722
+ * unique.
1723
+ */
1724
+ int GetIdentityHash();
1725
+
1726
+ /**
1727
+ * Access hidden properties on JavaScript objects. These properties are
1728
+ * hidden from the executing JavaScript and only accessible through the V8
1729
+ * C++ API. Hidden properties introduced by V8 internally (for example the
1730
+ * identity hash) are prefixed with "v8::".
1731
+ */
1732
+ bool SetHiddenValue(Handle<String> key, Handle<Value> value);
1733
+ Local<Value> GetHiddenValue(Handle<String> key);
1734
+ bool DeleteHiddenValue(Handle<String> key);
1735
+
1736
+ /**
1737
+ * Returns true if this is an instance of an api function (one
1738
+ * created from a function created from a function template) and has
1739
+ * been modified since it was created. Note that this method is
1740
+ * conservative and may return true for objects that haven't actually
1741
+ * been modified.
1742
+ */
1743
+ bool IsDirty();
1744
+
1745
+ /**
1746
+ * Clone this object with a fast but shallow copy. Values will point
1747
+ * to the same values as the original object.
1748
+ */
1749
+ Local<Object> Clone();
1750
+
1751
+ /**
1752
+ * Returns the context in which the object was created.
1753
+ */
1754
+ Local<Context> CreationContext();
1755
+
1756
+ /**
1757
+ * Set the backing store of the indexed properties to be managed by the
1758
+ * embedding layer. Access to the indexed properties will follow the rules
1759
+ * spelled out in CanvasPixelArray.
1760
+ * Note: The embedding program still owns the data and needs to ensure that
1761
+ * the backing store is preserved while V8 has a reference.
1762
+ */
1763
+ void SetIndexedPropertiesToPixelData(uint8_t* data, int length);
1764
+ bool HasIndexedPropertiesInPixelData();
1765
+ uint8_t* GetIndexedPropertiesPixelData();
1766
+ int GetIndexedPropertiesPixelDataLength();
1767
+
1768
+ /**
1769
+ * Set the backing store of the indexed properties to be managed by the
1770
+ * embedding layer. Access to the indexed properties will follow the rules
1771
+ * spelled out for the CanvasArray subtypes in the WebGL specification.
1772
+ * Note: The embedding program still owns the data and needs to ensure that
1773
+ * the backing store is preserved while V8 has a reference.
1774
+ */
1775
+ void SetIndexedPropertiesToExternalArrayData(void* data,
1776
+ ExternalArrayType array_type,
1777
+ int number_of_elements);
1778
+ bool HasIndexedPropertiesInExternalArrayData();
1779
+ void* GetIndexedPropertiesExternalArrayData();
1780
+ ExternalArrayType GetIndexedPropertiesExternalArrayDataType();
1781
+ int GetIndexedPropertiesExternalArrayDataLength();
1782
+
1783
+ /**
1784
+ * Checks whether a callback is set by the
1785
+ * ObjectTemplate::SetCallAsFunctionHandler method.
1786
+ * When an Object is callable this method returns true.
1787
+ */
1788
+ bool IsCallable();
1789
+
1790
+ /**
1791
+ * Call an Object as a function if a callback is set by the
1792
+ * ObjectTemplate::SetCallAsFunctionHandler method.
1793
+ */
1794
+ Local<Value> CallAsFunction(Handle<Object> recv,
1795
+ int argc,
1796
+ Handle<Value> argv[]);
1797
+
1798
+ /**
1799
+ * Call an Object as a constructor if a callback is set by the
1800
+ * ObjectTemplate::SetCallAsFunctionHandler method.
1801
+ * Note: This method behaves like the Function::NewInstance method.
1802
+ */
1803
+ Local<Value> CallAsConstructor(int argc, Handle<Value> argv[]);
1804
+
1805
+ static Local<Object> New();
1806
+ V8_INLINE(static Object* Cast(Value* obj));
1807
+
1808
+ private:
1809
+ Object();
1810
+ static void CheckCast(Value* obj);
1811
+ Local<Value> SlowGetInternalField(int index);
1812
+ void* SlowGetAlignedPointerFromInternalField(int index);
1813
+ };
1814
+
1815
+
1816
+ /**
1817
+ * An instance of the built-in array constructor (ECMA-262, 15.4.2).
1818
+ */
1819
+ class V8EXPORT Array : public Object {
1820
+ public:
1821
+ uint32_t Length() const;
1822
+
1823
+ /**
1824
+ * Clones an element at index |index|. Returns an empty
1825
+ * handle if cloning fails (for any reason).
1826
+ */
1827
+ Local<Object> CloneElementAt(uint32_t index);
1828
+
1829
+ /**
1830
+ * Creates a JavaScript array with the given length. If the length
1831
+ * is negative the returned array will have length 0.
1832
+ */
1833
+ static Local<Array> New(int length = 0);
1834
+
1835
+ V8_INLINE(static Array* Cast(Value* obj));
1836
+ private:
1837
+ Array();
1838
+ static void CheckCast(Value* obj);
1839
+ };
1840
+
1841
+
1842
+ /**
1843
+ * A JavaScript function object (ECMA-262, 15.3).
1844
+ */
1845
+ class V8EXPORT Function : public Object {
1846
+ public:
1847
+ Local<Object> NewInstance() const;
1848
+ Local<Object> NewInstance(int argc, Handle<Value> argv[]) const;
1849
+ Local<Value> Call(Handle<Object> recv, int argc, Handle<Value> argv[]);
1850
+ void SetName(Handle<String> name);
1851
+ Handle<Value> GetName() const;
1852
+
1853
+ /**
1854
+ * Name inferred from variable or property assignment of this function.
1855
+ * Used to facilitate debugging and profiling of JavaScript code written
1856
+ * in an OO style, where many functions are anonymous but are assigned
1857
+ * to object properties.
1858
+ */
1859
+ Handle<Value> GetInferredName() const;
1860
+
1861
+ /**
1862
+ * Returns zero based line number of function body and
1863
+ * kLineOffsetNotFound if no information available.
1864
+ */
1865
+ int GetScriptLineNumber() const;
1866
+ /**
1867
+ * Returns zero based column number of function body and
1868
+ * kLineOffsetNotFound if no information available.
1869
+ */
1870
+ int GetScriptColumnNumber() const;
1871
+ Handle<Value> GetScriptId() const;
1872
+ ScriptOrigin GetScriptOrigin() const;
1873
+ V8_INLINE(static Function* Cast(Value* obj));
1874
+ static const int kLineOffsetNotFound;
1875
+
1876
+ private:
1877
+ Function();
1878
+ static void CheckCast(Value* obj);
1879
+ };
1880
+
1881
+
1882
+ /**
1883
+ * An instance of the built-in Date constructor (ECMA-262, 15.9).
1884
+ */
1885
+ class V8EXPORT Date : public Object {
1886
+ public:
1887
+ static Local<Value> New(double time);
1888
+
1889
+ /**
1890
+ * A specialization of Value::NumberValue that is more efficient
1891
+ * because we know the structure of this object.
1892
+ */
1893
+ double NumberValue() const;
1894
+
1895
+ V8_INLINE(static Date* Cast(v8::Value* obj));
1896
+
1897
+ /**
1898
+ * Notification that the embedder has changed the time zone,
1899
+ * daylight savings time, or other date / time configuration
1900
+ * parameters. V8 keeps a cache of various values used for
1901
+ * date / time computation. This notification will reset
1902
+ * those cached values for the current context so that date /
1903
+ * time configuration changes would be reflected in the Date
1904
+ * object.
1905
+ *
1906
+ * This API should not be called more than needed as it will
1907
+ * negatively impact the performance of date operations.
1908
+ */
1909
+ static void DateTimeConfigurationChangeNotification();
1910
+
1911
+ private:
1912
+ static void CheckCast(v8::Value* obj);
1913
+ };
1914
+
1915
+
1916
+ /**
1917
+ * A Number object (ECMA-262, 4.3.21).
1918
+ */
1919
+ class V8EXPORT NumberObject : public Object {
1920
+ public:
1921
+ static Local<Value> New(double value);
1922
+
1923
+ /**
1924
+ * Returns the Number held by the object.
1925
+ */
1926
+ double NumberValue() const;
1927
+
1928
+ V8_INLINE(static NumberObject* Cast(v8::Value* obj));
1929
+
1930
+ private:
1931
+ static void CheckCast(v8::Value* obj);
1932
+ };
1933
+
1934
+
1935
+ /**
1936
+ * A Boolean object (ECMA-262, 4.3.15).
1937
+ */
1938
+ class V8EXPORT BooleanObject : public Object {
1939
+ public:
1940
+ static Local<Value> New(bool value);
1941
+
1942
+ /**
1943
+ * Returns the Boolean held by the object.
1944
+ */
1945
+ bool BooleanValue() const;
1946
+
1947
+ V8_INLINE(static BooleanObject* Cast(v8::Value* obj));
1948
+
1949
+ private:
1950
+ static void CheckCast(v8::Value* obj);
1951
+ };
1952
+
1953
+
1954
+ /**
1955
+ * A String object (ECMA-262, 4.3.18).
1956
+ */
1957
+ class V8EXPORT StringObject : public Object {
1958
+ public:
1959
+ static Local<Value> New(Handle<String> value);
1960
+
1961
+ /**
1962
+ * Returns the String held by the object.
1963
+ */
1964
+ Local<String> StringValue() const;
1965
+
1966
+ V8_INLINE(static StringObject* Cast(v8::Value* obj));
1967
+
1968
+ private:
1969
+ static void CheckCast(v8::Value* obj);
1970
+ };
1971
+
1972
+
1973
+ /**
1974
+ * An instance of the built-in RegExp constructor (ECMA-262, 15.10).
1975
+ */
1976
+ class V8EXPORT RegExp : public Object {
1977
+ public:
1978
+ /**
1979
+ * Regular expression flag bits. They can be or'ed to enable a set
1980
+ * of flags.
1981
+ */
1982
+ enum Flags {
1983
+ kNone = 0,
1984
+ kGlobal = 1,
1985
+ kIgnoreCase = 2,
1986
+ kMultiline = 4
1987
+ };
1988
+
1989
+ /**
1990
+ * Creates a regular expression from the given pattern string and
1991
+ * the flags bit field. May throw a JavaScript exception as
1992
+ * described in ECMA-262, 15.10.4.1.
1993
+ *
1994
+ * For example,
1995
+ * RegExp::New(v8::String::New("foo"),
1996
+ * static_cast<RegExp::Flags>(kGlobal | kMultiline))
1997
+ * is equivalent to evaluating "/foo/gm".
1998
+ */
1999
+ static Local<RegExp> New(Handle<String> pattern, Flags flags);
2000
+
2001
+ /**
2002
+ * Returns the value of the source property: a string representing
2003
+ * the regular expression.
2004
+ */
2005
+ Local<String> GetSource() const;
2006
+
2007
+ /**
2008
+ * Returns the flags bit field.
2009
+ */
2010
+ Flags GetFlags() const;
2011
+
2012
+ V8_INLINE(static RegExp* Cast(v8::Value* obj));
2013
+
2014
+ private:
2015
+ static void CheckCast(v8::Value* obj);
2016
+ };
2017
+
2018
+
2019
+ /**
2020
+ * A JavaScript value that wraps a C++ void*. This type of value is mainly used
2021
+ * to associate C++ data structures with JavaScript objects.
2022
+ */
2023
+ class V8EXPORT External : public Value {
2024
+ public:
2025
+ static Local<External> New(void* value);
2026
+ V8_INLINE(static External* Cast(Value* obj));
2027
+ void* Value() const;
2028
+ private:
2029
+ static void CheckCast(v8::Value* obj);
2030
+ };
2031
+
2032
+
2033
+ // --- Templates ---
2034
+
2035
+
2036
+ /**
2037
+ * The superclass of object and function templates.
2038
+ */
2039
+ class V8EXPORT Template : public Data {
2040
+ public:
2041
+ /** Adds a property to each instance created by this template.*/
2042
+ void Set(Handle<String> name, Handle<Data> value,
2043
+ PropertyAttribute attributes = None);
2044
+ V8_INLINE(void Set(const char* name, Handle<Data> value));
2045
+ private:
2046
+ Template();
2047
+
2048
+ friend class ObjectTemplate;
2049
+ friend class FunctionTemplate;
2050
+ };
2051
+
2052
+
2053
+ /**
2054
+ * The argument information given to function call callbacks. This
2055
+ * class provides access to information about the context of the call,
2056
+ * including the receiver, the number and values of arguments, and
2057
+ * the holder of the function.
2058
+ */
2059
+ class V8EXPORT Arguments {
2060
+ public:
2061
+ V8_INLINE(int Length() const);
2062
+ V8_INLINE(Local<Value> operator[](int i) const);
2063
+ V8_INLINE(Local<Function> Callee() const);
2064
+ V8_INLINE(Local<Object> This() const);
2065
+ V8_INLINE(Local<Object> Holder() const);
2066
+ V8_INLINE(bool IsConstructCall() const);
2067
+ V8_INLINE(Local<Value> Data() const);
2068
+ V8_INLINE(Isolate* GetIsolate() const);
2069
+
2070
+ private:
2071
+ static const int kIsolateIndex = 0;
2072
+ static const int kDataIndex = -1;
2073
+ static const int kCalleeIndex = -2;
2074
+ static const int kHolderIndex = -3;
2075
+
2076
+ friend class ImplementationUtilities;
2077
+ V8_INLINE(Arguments(internal::Object** implicit_args,
2078
+ internal::Object** values,
2079
+ int length,
2080
+ bool is_construct_call));
2081
+ internal::Object** implicit_args_;
2082
+ internal::Object** values_;
2083
+ int length_;
2084
+ bool is_construct_call_;
2085
+ };
2086
+
2087
+
2088
+ /**
2089
+ * The information passed to an accessor callback about the context
2090
+ * of the property access.
2091
+ */
2092
+ class V8EXPORT AccessorInfo {
2093
+ public:
2094
+ V8_INLINE(AccessorInfo(internal::Object** args))
2095
+ : args_(args) { }
2096
+ V8_INLINE(Isolate* GetIsolate() const);
2097
+ V8_INLINE(Local<Value> Data() const);
2098
+ V8_INLINE(Local<Object> This() const);
2099
+ V8_INLINE(Local<Object> Holder() const);
2100
+
2101
+ private:
2102
+ internal::Object** args_;
2103
+ };
2104
+
2105
+
2106
+ typedef Handle<Value> (*InvocationCallback)(const Arguments& args);
2107
+
2108
+ /**
2109
+ * NamedProperty[Getter|Setter] are used as interceptors on object.
2110
+ * See ObjectTemplate::SetNamedPropertyHandler.
2111
+ */
2112
+ typedef Handle<Value> (*NamedPropertyGetter)(Local<String> property,
2113
+ const AccessorInfo& info);
2114
+
2115
+
2116
+ /**
2117
+ * Returns the value if the setter intercepts the request.
2118
+ * Otherwise, returns an empty handle.
2119
+ */
2120
+ typedef Handle<Value> (*NamedPropertySetter)(Local<String> property,
2121
+ Local<Value> value,
2122
+ const AccessorInfo& info);
2123
+
2124
+ /**
2125
+ * Returns a non-empty handle if the interceptor intercepts the request.
2126
+ * The result is an integer encoding property attributes (like v8::None,
2127
+ * v8::DontEnum, etc.)
2128
+ */
2129
+ typedef Handle<Integer> (*NamedPropertyQuery)(Local<String> property,
2130
+ const AccessorInfo& info);
2131
+
2132
+
2133
+ /**
2134
+ * Returns a non-empty handle if the deleter intercepts the request.
2135
+ * The return value is true if the property could be deleted and false
2136
+ * otherwise.
2137
+ */
2138
+ typedef Handle<Boolean> (*NamedPropertyDeleter)(Local<String> property,
2139
+ const AccessorInfo& info);
2140
+
2141
+ /**
2142
+ * Returns an array containing the names of the properties the named
2143
+ * property getter intercepts.
2144
+ */
2145
+ typedef Handle<Array> (*NamedPropertyEnumerator)(const AccessorInfo& info);
2146
+
2147
+
2148
+ /**
2149
+ * Returns the value of the property if the getter intercepts the
2150
+ * request. Otherwise, returns an empty handle.
2151
+ */
2152
+ typedef Handle<Value> (*IndexedPropertyGetter)(uint32_t index,
2153
+ const AccessorInfo& info);
2154
+
2155
+
2156
+ /**
2157
+ * Returns the value if the setter intercepts the request.
2158
+ * Otherwise, returns an empty handle.
2159
+ */
2160
+ typedef Handle<Value> (*IndexedPropertySetter)(uint32_t index,
2161
+ Local<Value> value,
2162
+ const AccessorInfo& info);
2163
+
2164
+
2165
+ /**
2166
+ * Returns a non-empty handle if the interceptor intercepts the request.
2167
+ * The result is an integer encoding property attributes.
2168
+ */
2169
+ typedef Handle<Integer> (*IndexedPropertyQuery)(uint32_t index,
2170
+ const AccessorInfo& info);
2171
+
2172
+ /**
2173
+ * Returns a non-empty handle if the deleter intercepts the request.
2174
+ * The return value is true if the property could be deleted and false
2175
+ * otherwise.
2176
+ */
2177
+ typedef Handle<Boolean> (*IndexedPropertyDeleter)(uint32_t index,
2178
+ const AccessorInfo& info);
2179
+
2180
+ /**
2181
+ * Returns an array containing the indices of the properties the
2182
+ * indexed property getter intercepts.
2183
+ */
2184
+ typedef Handle<Array> (*IndexedPropertyEnumerator)(const AccessorInfo& info);
2185
+
2186
+
2187
+ /**
2188
+ * Access type specification.
2189
+ */
2190
+ enum AccessType {
2191
+ ACCESS_GET,
2192
+ ACCESS_SET,
2193
+ ACCESS_HAS,
2194
+ ACCESS_DELETE,
2195
+ ACCESS_KEYS
2196
+ };
2197
+
2198
+
2199
+ /**
2200
+ * Returns true if cross-context access should be allowed to the named
2201
+ * property with the given key on the host object.
2202
+ */
2203
+ typedef bool (*NamedSecurityCallback)(Local<Object> host,
2204
+ Local<Value> key,
2205
+ AccessType type,
2206
+ Local<Value> data);
2207
+
2208
+
2209
+ /**
2210
+ * Returns true if cross-context access should be allowed to the indexed
2211
+ * property with the given index on the host object.
2212
+ */
2213
+ typedef bool (*IndexedSecurityCallback)(Local<Object> host,
2214
+ uint32_t index,
2215
+ AccessType type,
2216
+ Local<Value> data);
2217
+
2218
+
2219
+ /**
2220
+ * A FunctionTemplate is used to create functions at runtime. There
2221
+ * can only be one function created from a FunctionTemplate in a
2222
+ * context. The lifetime of the created function is equal to the
2223
+ * lifetime of the context. So in case the embedder needs to create
2224
+ * temporary functions that can be collected using Scripts is
2225
+ * preferred.
2226
+ *
2227
+ * A FunctionTemplate can have properties, these properties are added to the
2228
+ * function object when it is created.
2229
+ *
2230
+ * A FunctionTemplate has a corresponding instance template which is
2231
+ * used to create object instances when the function is used as a
2232
+ * constructor. Properties added to the instance template are added to
2233
+ * each object instance.
2234
+ *
2235
+ * A FunctionTemplate can have a prototype template. The prototype template
2236
+ * is used to create the prototype object of the function.
2237
+ *
2238
+ * The following example shows how to use a FunctionTemplate:
2239
+ *
2240
+ * \code
2241
+ * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
2242
+ * t->Set("func_property", v8::Number::New(1));
2243
+ *
2244
+ * v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
2245
+ * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
2246
+ * proto_t->Set("proto_const", v8::Number::New(2));
2247
+ *
2248
+ * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
2249
+ * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
2250
+ * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
2251
+ * instance_t->Set("instance_property", Number::New(3));
2252
+ *
2253
+ * v8::Local<v8::Function> function = t->GetFunction();
2254
+ * v8::Local<v8::Object> instance = function->NewInstance();
2255
+ * \endcode
2256
+ *
2257
+ * Let's use "function" as the JS variable name of the function object
2258
+ * and "instance" for the instance object created above. The function
2259
+ * and the instance will have the following properties:
2260
+ *
2261
+ * \code
2262
+ * func_property in function == true;
2263
+ * function.func_property == 1;
2264
+ *
2265
+ * function.prototype.proto_method() invokes 'InvokeCallback'
2266
+ * function.prototype.proto_const == 2;
2267
+ *
2268
+ * instance instanceof function == true;
2269
+ * instance.instance_accessor calls 'InstanceAccessorCallback'
2270
+ * instance.instance_property == 3;
2271
+ * \endcode
2272
+ *
2273
+ * A FunctionTemplate can inherit from another one by calling the
2274
+ * FunctionTemplate::Inherit method. The following graph illustrates
2275
+ * the semantics of inheritance:
2276
+ *
2277
+ * \code
2278
+ * FunctionTemplate Parent -> Parent() . prototype -> { }
2279
+ * ^ ^
2280
+ * | Inherit(Parent) | .__proto__
2281
+ * | |
2282
+ * FunctionTemplate Child -> Child() . prototype -> { }
2283
+ * \endcode
2284
+ *
2285
+ * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
2286
+ * object of the Child() function has __proto__ pointing to the
2287
+ * Parent() function's prototype object. An instance of the Child
2288
+ * function has all properties on Parent's instance templates.
2289
+ *
2290
+ * Let Parent be the FunctionTemplate initialized in the previous
2291
+ * section and create a Child FunctionTemplate by:
2292
+ *
2293
+ * \code
2294
+ * Local<FunctionTemplate> parent = t;
2295
+ * Local<FunctionTemplate> child = FunctionTemplate::New();
2296
+ * child->Inherit(parent);
2297
+ *
2298
+ * Local<Function> child_function = child->GetFunction();
2299
+ * Local<Object> child_instance = child_function->NewInstance();
2300
+ * \endcode
2301
+ *
2302
+ * The Child function and Child instance will have the following
2303
+ * properties:
2304
+ *
2305
+ * \code
2306
+ * child_func.prototype.__proto__ == function.prototype;
2307
+ * child_instance.instance_accessor calls 'InstanceAccessorCallback'
2308
+ * child_instance.instance_property == 3;
2309
+ * \endcode
2310
+ */
2311
+ class V8EXPORT FunctionTemplate : public Template {
2312
+ public:
2313
+ /** Creates a function template.*/
2314
+ static Local<FunctionTemplate> New(
2315
+ InvocationCallback callback = 0,
2316
+ Handle<Value> data = Handle<Value>(),
2317
+ Handle<Signature> signature = Handle<Signature>(),
2318
+ int length = 0);
2319
+ /** Returns the unique function instance in the current execution context.*/
2320
+ Local<Function> GetFunction();
2321
+
2322
+ /**
2323
+ * Set the call-handler callback for a FunctionTemplate. This
2324
+ * callback is called whenever the function created from this
2325
+ * FunctionTemplate is called.
2326
+ */
2327
+ void SetCallHandler(InvocationCallback callback,
2328
+ Handle<Value> data = Handle<Value>());
2329
+
2330
+ /** Set the predefined length property for the FunctionTemplate. */
2331
+ void SetLength(int length);
2332
+
2333
+ /** Get the InstanceTemplate. */
2334
+ Local<ObjectTemplate> InstanceTemplate();
2335
+
2336
+ /** Causes the function template to inherit from a parent function template.*/
2337
+ void Inherit(Handle<FunctionTemplate> parent);
2338
+
2339
+ /**
2340
+ * A PrototypeTemplate is the template used to create the prototype object
2341
+ * of the function created by this template.
2342
+ */
2343
+ Local<ObjectTemplate> PrototypeTemplate();
2344
+
2345
+ /**
2346
+ * Set the class name of the FunctionTemplate. This is used for
2347
+ * printing objects created with the function created from the
2348
+ * FunctionTemplate as its constructor.
2349
+ */
2350
+ void SetClassName(Handle<String> name);
2351
+
2352
+ /**
2353
+ * Determines whether the __proto__ accessor ignores instances of
2354
+ * the function template. If instances of the function template are
2355
+ * ignored, __proto__ skips all instances and instead returns the
2356
+ * next object in the prototype chain.
2357
+ *
2358
+ * Call with a value of true to make the __proto__ accessor ignore
2359
+ * instances of the function template. Call with a value of false
2360
+ * to make the __proto__ accessor not ignore instances of the
2361
+ * function template. By default, instances of a function template
2362
+ * are not ignored.
2363
+ */
2364
+ void SetHiddenPrototype(bool value);
2365
+
2366
+ /**
2367
+ * Sets the ReadOnly flag in the attributes of the 'prototype' property
2368
+ * of functions created from this FunctionTemplate to true.
2369
+ */
2370
+ void ReadOnlyPrototype();
2371
+
2372
+ /**
2373
+ * Returns true if the given object is an instance of this function
2374
+ * template.
2375
+ */
2376
+ bool HasInstance(Handle<Value> object);
2377
+
2378
+ private:
2379
+ FunctionTemplate();
2380
+ void AddInstancePropertyAccessor(Handle<String> name,
2381
+ AccessorGetter getter,
2382
+ AccessorSetter setter,
2383
+ Handle<Value> data,
2384
+ AccessControl settings,
2385
+ PropertyAttribute attributes,
2386
+ Handle<AccessorSignature> signature);
2387
+ void SetNamedInstancePropertyHandler(NamedPropertyGetter getter,
2388
+ NamedPropertySetter setter,
2389
+ NamedPropertyQuery query,
2390
+ NamedPropertyDeleter remover,
2391
+ NamedPropertyEnumerator enumerator,
2392
+ Handle<Value> data);
2393
+ void SetIndexedInstancePropertyHandler(IndexedPropertyGetter getter,
2394
+ IndexedPropertySetter setter,
2395
+ IndexedPropertyQuery query,
2396
+ IndexedPropertyDeleter remover,
2397
+ IndexedPropertyEnumerator enumerator,
2398
+ Handle<Value> data);
2399
+ void SetInstanceCallAsFunctionHandler(InvocationCallback callback,
2400
+ Handle<Value> data);
2401
+
2402
+ friend class Context;
2403
+ friend class ObjectTemplate;
2404
+ };
2405
+
2406
+
2407
+ /**
2408
+ * An ObjectTemplate is used to create objects at runtime.
2409
+ *
2410
+ * Properties added to an ObjectTemplate are added to each object
2411
+ * created from the ObjectTemplate.
2412
+ */
2413
+ class V8EXPORT ObjectTemplate : public Template {
2414
+ public:
2415
+ /** Creates an ObjectTemplate. */
2416
+ static Local<ObjectTemplate> New();
2417
+
2418
+ /** Creates a new instance of this template.*/
2419
+ Local<Object> NewInstance();
2420
+
2421
+ /**
2422
+ * Sets an accessor on the object template.
2423
+ *
2424
+ * Whenever the property with the given name is accessed on objects
2425
+ * created from this ObjectTemplate the getter and setter callbacks
2426
+ * are called instead of getting and setting the property directly
2427
+ * on the JavaScript object.
2428
+ *
2429
+ * \param name The name of the property for which an accessor is added.
2430
+ * \param getter The callback to invoke when getting the property.
2431
+ * \param setter The callback to invoke when setting the property.
2432
+ * \param data A piece of data that will be passed to the getter and setter
2433
+ * callbacks whenever they are invoked.
2434
+ * \param settings Access control settings for the accessor. This is a bit
2435
+ * field consisting of one of more of
2436
+ * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
2437
+ * The default is to not allow cross-context access.
2438
+ * ALL_CAN_READ means that all cross-context reads are allowed.
2439
+ * ALL_CAN_WRITE means that all cross-context writes are allowed.
2440
+ * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
2441
+ * cross-context access.
2442
+ * \param attribute The attributes of the property for which an accessor
2443
+ * is added.
2444
+ * \param signature The signature describes valid receivers for the accessor
2445
+ * and is used to perform implicit instance checks against them. If the
2446
+ * receiver is incompatible (i.e. is not an instance of the constructor as
2447
+ * defined by FunctionTemplate::HasInstance()), an implicit TypeError is
2448
+ * thrown and no callback is invoked.
2449
+ */
2450
+ void SetAccessor(Handle<String> name,
2451
+ AccessorGetter getter,
2452
+ AccessorSetter setter = 0,
2453
+ Handle<Value> data = Handle<Value>(),
2454
+ AccessControl settings = DEFAULT,
2455
+ PropertyAttribute attribute = None,
2456
+ Handle<AccessorSignature> signature =
2457
+ Handle<AccessorSignature>());
2458
+
2459
+ /**
2460
+ * Sets a named property handler on the object template.
2461
+ *
2462
+ * Whenever a named property is accessed on objects created from
2463
+ * this object template, the provided callback is invoked instead of
2464
+ * accessing the property directly on the JavaScript object.
2465
+ *
2466
+ * \param getter The callback to invoke when getting a property.
2467
+ * \param setter The callback to invoke when setting a property.
2468
+ * \param query The callback to invoke to check if a property is present,
2469
+ * and if present, get its attributes.
2470
+ * \param deleter The callback to invoke when deleting a property.
2471
+ * \param enumerator The callback to invoke to enumerate all the named
2472
+ * properties of an object.
2473
+ * \param data A piece of data that will be passed to the callbacks
2474
+ * whenever they are invoked.
2475
+ */
2476
+ void SetNamedPropertyHandler(NamedPropertyGetter getter,
2477
+ NamedPropertySetter setter = 0,
2478
+ NamedPropertyQuery query = 0,
2479
+ NamedPropertyDeleter deleter = 0,
2480
+ NamedPropertyEnumerator enumerator = 0,
2481
+ Handle<Value> data = Handle<Value>());
2482
+
2483
+ /**
2484
+ * Sets an indexed property handler on the object template.
2485
+ *
2486
+ * Whenever an indexed property is accessed on objects created from
2487
+ * this object template, the provided callback is invoked instead of
2488
+ * accessing the property directly on the JavaScript object.
2489
+ *
2490
+ * \param getter The callback to invoke when getting a property.
2491
+ * \param setter The callback to invoke when setting a property.
2492
+ * \param query The callback to invoke to check if an object has a property.
2493
+ * \param deleter The callback to invoke when deleting a property.
2494
+ * \param enumerator The callback to invoke to enumerate all the indexed
2495
+ * properties of an object.
2496
+ * \param data A piece of data that will be passed to the callbacks
2497
+ * whenever they are invoked.
2498
+ */
2499
+ void SetIndexedPropertyHandler(IndexedPropertyGetter getter,
2500
+ IndexedPropertySetter setter = 0,
2501
+ IndexedPropertyQuery query = 0,
2502
+ IndexedPropertyDeleter deleter = 0,
2503
+ IndexedPropertyEnumerator enumerator = 0,
2504
+ Handle<Value> data = Handle<Value>());
2505
+
2506
+ /**
2507
+ * Sets the callback to be used when calling instances created from
2508
+ * this template as a function. If no callback is set, instances
2509
+ * behave like normal JavaScript objects that cannot be called as a
2510
+ * function.
2511
+ */
2512
+ void SetCallAsFunctionHandler(InvocationCallback callback,
2513
+ Handle<Value> data = Handle<Value>());
2514
+
2515
+ /**
2516
+ * Mark object instances of the template as undetectable.
2517
+ *
2518
+ * In many ways, undetectable objects behave as though they are not
2519
+ * there. They behave like 'undefined' in conditionals and when
2520
+ * printed. However, properties can be accessed and called as on
2521
+ * normal objects.
2522
+ */
2523
+ void MarkAsUndetectable();
2524
+
2525
+ /**
2526
+ * Sets access check callbacks on the object template.
2527
+ *
2528
+ * When accessing properties on instances of this object template,
2529
+ * the access check callback will be called to determine whether or
2530
+ * not to allow cross-context access to the properties.
2531
+ * The last parameter specifies whether access checks are turned
2532
+ * on by default on instances. If access checks are off by default,
2533
+ * they can be turned on on individual instances by calling
2534
+ * Object::TurnOnAccessCheck().
2535
+ */
2536
+ void SetAccessCheckCallbacks(NamedSecurityCallback named_handler,
2537
+ IndexedSecurityCallback indexed_handler,
2538
+ Handle<Value> data = Handle<Value>(),
2539
+ bool turned_on_by_default = true);
2540
+
2541
+ /**
2542
+ * Gets the number of internal fields for objects generated from
2543
+ * this template.
2544
+ */
2545
+ int InternalFieldCount();
2546
+
2547
+ /**
2548
+ * Sets the number of internal fields for objects generated from
2549
+ * this template.
2550
+ */
2551
+ void SetInternalFieldCount(int value);
2552
+
2553
+ private:
2554
+ ObjectTemplate();
2555
+ static Local<ObjectTemplate> New(Handle<FunctionTemplate> constructor);
2556
+ friend class FunctionTemplate;
2557
+ };
2558
+
2559
+
2560
+ /**
2561
+ * A Signature specifies which receivers and arguments are valid
2562
+ * parameters to a function.
2563
+ */
2564
+ class V8EXPORT Signature : public Data {
2565
+ public:
2566
+ static Local<Signature> New(Handle<FunctionTemplate> receiver =
2567
+ Handle<FunctionTemplate>(),
2568
+ int argc = 0,
2569
+ Handle<FunctionTemplate> argv[] = 0);
2570
+ private:
2571
+ Signature();
2572
+ };
2573
+
2574
+
2575
+ /**
2576
+ * An AccessorSignature specifies which receivers are valid parameters
2577
+ * to an accessor callback.
2578
+ */
2579
+ class V8EXPORT AccessorSignature : public Data {
2580
+ public:
2581
+ static Local<AccessorSignature> New(Handle<FunctionTemplate> receiver =
2582
+ Handle<FunctionTemplate>());
2583
+ private:
2584
+ AccessorSignature();
2585
+ };
2586
+
2587
+
2588
+ /**
2589
+ * A utility for determining the type of objects based on the template
2590
+ * they were constructed from.
2591
+ */
2592
+ class V8EXPORT TypeSwitch : public Data {
2593
+ public:
2594
+ static Local<TypeSwitch> New(Handle<FunctionTemplate> type);
2595
+ static Local<TypeSwitch> New(int argc, Handle<FunctionTemplate> types[]);
2596
+ int match(Handle<Value> value);
2597
+ private:
2598
+ TypeSwitch();
2599
+ };
2600
+
2601
+
2602
+ // --- Extensions ---
2603
+
2604
+ class V8EXPORT ExternalAsciiStringResourceImpl
2605
+ : public String::ExternalAsciiStringResource {
2606
+ public:
2607
+ ExternalAsciiStringResourceImpl() : data_(0), length_(0) {}
2608
+ ExternalAsciiStringResourceImpl(const char* data, size_t length)
2609
+ : data_(data), length_(length) {}
2610
+ const char* data() const { return data_; }
2611
+ size_t length() const { return length_; }
2612
+
2613
+ private:
2614
+ const char* data_;
2615
+ size_t length_;
2616
+ };
2617
+
2618
+ /**
2619
+ * Ignore
2620
+ */
2621
+ class V8EXPORT Extension { // NOLINT
2622
+ public:
2623
+ // Note that the strings passed into this constructor must live as long
2624
+ // as the Extension itself.
2625
+ Extension(const char* name,
2626
+ const char* source = 0,
2627
+ int dep_count = 0,
2628
+ const char** deps = 0,
2629
+ int source_length = -1);
2630
+ virtual ~Extension() { }
2631
+ virtual v8::Handle<v8::FunctionTemplate>
2632
+ GetNativeFunction(v8::Handle<v8::String> name) {
2633
+ return v8::Handle<v8::FunctionTemplate>();
2634
+ }
2635
+
2636
+ const char* name() const { return name_; }
2637
+ size_t source_length() const { return source_length_; }
2638
+ const String::ExternalAsciiStringResource* source() const {
2639
+ return &source_; }
2640
+ int dependency_count() { return dep_count_; }
2641
+ const char** dependencies() { return deps_; }
2642
+ void set_auto_enable(bool value) { auto_enable_ = value; }
2643
+ bool auto_enable() { return auto_enable_; }
2644
+
2645
+ private:
2646
+ const char* name_;
2647
+ size_t source_length_; // expected to initialize before source_
2648
+ ExternalAsciiStringResourceImpl source_;
2649
+ int dep_count_;
2650
+ const char** deps_;
2651
+ bool auto_enable_;
2652
+
2653
+ // Disallow copying and assigning.
2654
+ Extension(const Extension&);
2655
+ void operator=(const Extension&);
2656
+ };
2657
+
2658
+
2659
+ void V8EXPORT RegisterExtension(Extension* extension);
2660
+
2661
+
2662
+ /**
2663
+ * Ignore
2664
+ */
2665
+ class V8EXPORT DeclareExtension {
2666
+ public:
2667
+ V8_INLINE(DeclareExtension(Extension* extension)) {
2668
+ RegisterExtension(extension);
2669
+ }
2670
+ };
2671
+
2672
+
2673
+ // --- Statics ---
2674
+
2675
+
2676
+ Handle<Primitive> V8EXPORT Undefined();
2677
+ Handle<Primitive> V8EXPORT Null();
2678
+ Handle<Boolean> V8EXPORT True();
2679
+ Handle<Boolean> V8EXPORT False();
2680
+
2681
+ V8_INLINE(Handle<Primitive> Undefined(Isolate* isolate));
2682
+ V8_INLINE(Handle<Primitive> Null(Isolate* isolate));
2683
+ V8_INLINE(Handle<Boolean> True(Isolate* isolate));
2684
+ V8_INLINE(Handle<Boolean> False(Isolate* isolate));
2685
+
2686
+
2687
+ /**
2688
+ * A set of constraints that specifies the limits of the runtime's memory use.
2689
+ * You must set the heap size before initializing the VM - the size cannot be
2690
+ * adjusted after the VM is initialized.
2691
+ *
2692
+ * If you are using threads then you should hold the V8::Locker lock while
2693
+ * setting the stack limit and you must set a non-default stack limit separately
2694
+ * for each thread.
2695
+ */
2696
+ class V8EXPORT ResourceConstraints {
2697
+ public:
2698
+ ResourceConstraints();
2699
+ int max_young_space_size() const { return max_young_space_size_; }
2700
+ void set_max_young_space_size(int value) { max_young_space_size_ = value; }
2701
+ int max_old_space_size() const { return max_old_space_size_; }
2702
+ void set_max_old_space_size(int value) { max_old_space_size_ = value; }
2703
+ int max_executable_size() { return max_executable_size_; }
2704
+ void set_max_executable_size(int value) { max_executable_size_ = value; }
2705
+ uint32_t* stack_limit() const { return stack_limit_; }
2706
+ // Sets an address beyond which the VM's stack may not grow.
2707
+ void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
2708
+ private:
2709
+ int max_young_space_size_;
2710
+ int max_old_space_size_;
2711
+ int max_executable_size_;
2712
+ uint32_t* stack_limit_;
2713
+ };
2714
+
2715
+
2716
+ bool V8EXPORT SetResourceConstraints(ResourceConstraints* constraints);
2717
+
2718
+
2719
+ // --- Exceptions ---
2720
+
2721
+
2722
+ typedef void (*FatalErrorCallback)(const char* location, const char* message);
2723
+
2724
+
2725
+ typedef void (*MessageCallback)(Handle<Message> message, Handle<Value> error);
2726
+
2727
+
2728
+ /**
2729
+ * Schedules an exception to be thrown when returning to JavaScript. When an
2730
+ * exception has been scheduled it is illegal to invoke any JavaScript
2731
+ * operation; the caller must return immediately and only after the exception
2732
+ * has been handled does it become legal to invoke JavaScript operations.
2733
+ */
2734
+ Handle<Value> V8EXPORT ThrowException(Handle<Value> exception);
2735
+
2736
+ /**
2737
+ * Create new error objects by calling the corresponding error object
2738
+ * constructor with the message.
2739
+ */
2740
+ class V8EXPORT Exception {
2741
+ public:
2742
+ static Local<Value> RangeError(Handle<String> message);
2743
+ static Local<Value> ReferenceError(Handle<String> message);
2744
+ static Local<Value> SyntaxError(Handle<String> message);
2745
+ static Local<Value> TypeError(Handle<String> message);
2746
+ static Local<Value> Error(Handle<String> message);
2747
+ };
2748
+
2749
+
2750
+ // --- Counters Callbacks ---
2751
+
2752
+ typedef int* (*CounterLookupCallback)(const char* name);
2753
+
2754
+ typedef void* (*CreateHistogramCallback)(const char* name,
2755
+ int min,
2756
+ int max,
2757
+ size_t buckets);
2758
+
2759
+ typedef void (*AddHistogramSampleCallback)(void* histogram, int sample);
2760
+
2761
+ // --- Memory Allocation Callback ---
2762
+ enum ObjectSpace {
2763
+ kObjectSpaceNewSpace = 1 << 0,
2764
+ kObjectSpaceOldPointerSpace = 1 << 1,
2765
+ kObjectSpaceOldDataSpace = 1 << 2,
2766
+ kObjectSpaceCodeSpace = 1 << 3,
2767
+ kObjectSpaceMapSpace = 1 << 4,
2768
+ kObjectSpaceLoSpace = 1 << 5,
2769
+
2770
+ kObjectSpaceAll = kObjectSpaceNewSpace | kObjectSpaceOldPointerSpace |
2771
+ kObjectSpaceOldDataSpace | kObjectSpaceCodeSpace | kObjectSpaceMapSpace |
2772
+ kObjectSpaceLoSpace
2773
+ };
2774
+
2775
+ enum AllocationAction {
2776
+ kAllocationActionAllocate = 1 << 0,
2777
+ kAllocationActionFree = 1 << 1,
2778
+ kAllocationActionAll = kAllocationActionAllocate | kAllocationActionFree
2779
+ };
2780
+
2781
+ typedef void (*MemoryAllocationCallback)(ObjectSpace space,
2782
+ AllocationAction action,
2783
+ int size);
2784
+
2785
+ // --- Leave Script Callback ---
2786
+ typedef void (*CallCompletedCallback)();
2787
+
2788
+ // --- Failed Access Check Callback ---
2789
+ typedef void (*FailedAccessCheckCallback)(Local<Object> target,
2790
+ AccessType type,
2791
+ Local<Value> data);
2792
+
2793
+ // --- AllowCodeGenerationFromStrings callbacks ---
2794
+
2795
+ /**
2796
+ * Callback to check if code generation from strings is allowed. See
2797
+ * Context::AllowCodeGenerationFromStrings.
2798
+ */
2799
+ typedef bool (*AllowCodeGenerationFromStringsCallback)(Local<Context> context);
2800
+
2801
+ // --- Garbage Collection Callbacks ---
2802
+
2803
+ /**
2804
+ * Applications can register callback functions which will be called
2805
+ * before and after a garbage collection. Allocations are not
2806
+ * allowed in the callback functions, you therefore cannot manipulate
2807
+ * objects (set or delete properties for example) since it is possible
2808
+ * such operations will result in the allocation of objects.
2809
+ */
2810
+ enum GCType {
2811
+ kGCTypeScavenge = 1 << 0,
2812
+ kGCTypeMarkSweepCompact = 1 << 1,
2813
+ kGCTypeAll = kGCTypeScavenge | kGCTypeMarkSweepCompact
2814
+ };
2815
+
2816
+ enum GCCallbackFlags {
2817
+ kNoGCCallbackFlags = 0,
2818
+ kGCCallbackFlagCompacted = 1 << 0
2819
+ };
2820
+
2821
+ typedef void (*GCPrologueCallback)(GCType type, GCCallbackFlags flags);
2822
+ typedef void (*GCEpilogueCallback)(GCType type, GCCallbackFlags flags);
2823
+
2824
+ typedef void (*GCCallback)();
2825
+
2826
+
2827
+ /**
2828
+ * Collection of V8 heap information.
2829
+ *
2830
+ * Instances of this class can be passed to v8::V8::HeapStatistics to
2831
+ * get heap statistics from V8.
2832
+ */
2833
+ class V8EXPORT HeapStatistics {
2834
+ public:
2835
+ HeapStatistics();
2836
+ size_t total_heap_size() { return total_heap_size_; }
2837
+ size_t total_heap_size_executable() { return total_heap_size_executable_; }
2838
+ size_t total_physical_size() { return total_physical_size_; }
2839
+ size_t used_heap_size() { return used_heap_size_; }
2840
+ size_t heap_size_limit() { return heap_size_limit_; }
2841
+
2842
+ private:
2843
+ void set_total_heap_size(size_t size) { total_heap_size_ = size; }
2844
+ void set_total_heap_size_executable(size_t size) {
2845
+ total_heap_size_executable_ = size;
2846
+ }
2847
+ void set_total_physical_size(size_t size) {
2848
+ total_physical_size_ = size;
2849
+ }
2850
+ void set_used_heap_size(size_t size) { used_heap_size_ = size; }
2851
+ void set_heap_size_limit(size_t size) { heap_size_limit_ = size; }
2852
+
2853
+ size_t total_heap_size_;
2854
+ size_t total_heap_size_executable_;
2855
+ size_t total_physical_size_;
2856
+ size_t used_heap_size_;
2857
+ size_t heap_size_limit_;
2858
+
2859
+ friend class V8;
2860
+ };
2861
+
2862
+
2863
+ class RetainedObjectInfo;
2864
+
2865
+ /**
2866
+ * Isolate represents an isolated instance of the V8 engine. V8
2867
+ * isolates have completely separate states. Objects from one isolate
2868
+ * must not be used in other isolates. When V8 is initialized a
2869
+ * default isolate is implicitly created and entered. The embedder
2870
+ * can create additional isolates and use them in parallel in multiple
2871
+ * threads. An isolate can be entered by at most one thread at any
2872
+ * given time. The Locker/Unlocker API must be used to synchronize.
2873
+ */
2874
+ class V8EXPORT Isolate {
2875
+ public:
2876
+ /**
2877
+ * Stack-allocated class which sets the isolate for all operations
2878
+ * executed within a local scope.
2879
+ */
2880
+ class V8EXPORT Scope {
2881
+ public:
2882
+ explicit Scope(Isolate* isolate) : isolate_(isolate) {
2883
+ isolate->Enter();
2884
+ }
2885
+
2886
+ ~Scope() { isolate_->Exit(); }
2887
+
2888
+ private:
2889
+ Isolate* const isolate_;
2890
+
2891
+ // Prevent copying of Scope objects.
2892
+ Scope(const Scope&);
2893
+ Scope& operator=(const Scope&);
2894
+ };
2895
+
2896
+ /**
2897
+ * Creates a new isolate. Does not change the currently entered
2898
+ * isolate.
2899
+ *
2900
+ * When an isolate is no longer used its resources should be freed
2901
+ * by calling Dispose(). Using the delete operator is not allowed.
2902
+ */
2903
+ static Isolate* New();
2904
+
2905
+ /**
2906
+ * Returns the entered isolate for the current thread or NULL in
2907
+ * case there is no current isolate.
2908
+ */
2909
+ static Isolate* GetCurrent();
2910
+
2911
+ /**
2912
+ * Methods below this point require holding a lock (using Locker) in
2913
+ * a multi-threaded environment.
2914
+ */
2915
+
2916
+ /**
2917
+ * Sets this isolate as the entered one for the current thread.
2918
+ * Saves the previously entered one (if any), so that it can be
2919
+ * restored when exiting. Re-entering an isolate is allowed.
2920
+ */
2921
+ void Enter();
2922
+
2923
+ /**
2924
+ * Exits this isolate by restoring the previously entered one in the
2925
+ * current thread. The isolate may still stay the same, if it was
2926
+ * entered more than once.
2927
+ *
2928
+ * Requires: this == Isolate::GetCurrent().
2929
+ */
2930
+ void Exit();
2931
+
2932
+ /**
2933
+ * Disposes the isolate. The isolate must not be entered by any
2934
+ * thread to be disposable.
2935
+ */
2936
+ void Dispose();
2937
+
2938
+ /**
2939
+ * Associate embedder-specific data with the isolate
2940
+ */
2941
+ V8_INLINE(void SetData(void* data));
2942
+
2943
+ /**
2944
+ * Retrieve embedder-specific data from the isolate.
2945
+ * Returns NULL if SetData has never been called.
2946
+ */
2947
+ V8_INLINE(void* GetData());
2948
+
2949
+ private:
2950
+ Isolate();
2951
+ Isolate(const Isolate&);
2952
+ ~Isolate();
2953
+ Isolate& operator=(const Isolate&);
2954
+ void* operator new(size_t size);
2955
+ void operator delete(void*, size_t);
2956
+ };
2957
+
2958
+
2959
+ class V8EXPORT StartupData {
2960
+ public:
2961
+ enum CompressionAlgorithm {
2962
+ kUncompressed,
2963
+ kBZip2
2964
+ };
2965
+
2966
+ const char* data;
2967
+ int compressed_size;
2968
+ int raw_size;
2969
+ };
2970
+
2971
+
2972
+ /**
2973
+ * A helper class for driving V8 startup data decompression. It is based on
2974
+ * "CompressedStartupData" API functions from the V8 class. It isn't mandatory
2975
+ * for an embedder to use this class, instead, API functions can be used
2976
+ * directly.
2977
+ *
2978
+ * For an example of the class usage, see the "shell.cc" sample application.
2979
+ */
2980
+ class V8EXPORT StartupDataDecompressor { // NOLINT
2981
+ public:
2982
+ StartupDataDecompressor();
2983
+ virtual ~StartupDataDecompressor();
2984
+ int Decompress();
2985
+
2986
+ protected:
2987
+ virtual int DecompressData(char* raw_data,
2988
+ int* raw_data_size,
2989
+ const char* compressed_data,
2990
+ int compressed_data_size) = 0;
2991
+
2992
+ private:
2993
+ char** raw_data;
2994
+ };
2995
+
2996
+
2997
+ /**
2998
+ * EntropySource is used as a callback function when v8 needs a source
2999
+ * of entropy.
3000
+ */
3001
+ typedef bool (*EntropySource)(unsigned char* buffer, size_t length);
3002
+
3003
+
3004
+ /**
3005
+ * ReturnAddressLocationResolver is used as a callback function when v8 is
3006
+ * resolving the location of a return address on the stack. Profilers that
3007
+ * change the return address on the stack can use this to resolve the stack
3008
+ * location to whereever the profiler stashed the original return address.
3009
+ *
3010
+ * \param return_addr_location points to a location on stack where a machine
3011
+ * return address resides.
3012
+ * \returns either return_addr_location, or else a pointer to the profiler's
3013
+ * copy of the original return address.
3014
+ *
3015
+ * \note the resolver function must not cause garbage collection.
3016
+ */
3017
+ typedef uintptr_t (*ReturnAddressLocationResolver)(
3018
+ uintptr_t return_addr_location);
3019
+
3020
+
3021
+ /**
3022
+ * FunctionEntryHook is the type of the profile entry hook called at entry to
3023
+ * any generated function when function-level profiling is enabled.
3024
+ *
3025
+ * \param function the address of the function that's being entered.
3026
+ * \param return_addr_location points to a location on stack where the machine
3027
+ * return address resides. This can be used to identify the caller of
3028
+ * \p function, and/or modified to divert execution when \p function exits.
3029
+ *
3030
+ * \note the entry hook must not cause garbage collection.
3031
+ */
3032
+ typedef void (*FunctionEntryHook)(uintptr_t function,
3033
+ uintptr_t return_addr_location);
3034
+
3035
+
3036
+ /**
3037
+ * A JIT code event is issued each time code is added, moved or removed.
3038
+ *
3039
+ * \note removal events are not currently issued.
3040
+ */
3041
+ struct JitCodeEvent {
3042
+ enum EventType {
3043
+ CODE_ADDED,
3044
+ CODE_MOVED,
3045
+ CODE_REMOVED
3046
+ };
3047
+
3048
+ // Type of event.
3049
+ EventType type;
3050
+ // Start of the instructions.
3051
+ void* code_start;
3052
+ // Size of the instructions.
3053
+ size_t code_len;
3054
+
3055
+ union {
3056
+ // Only valid for CODE_ADDED.
3057
+ struct {
3058
+ // Name of the object associated with the code, note that the string is
3059
+ // not zero-terminated.
3060
+ const char* str;
3061
+ // Number of chars in str.
3062
+ size_t len;
3063
+ } name;
3064
+ // New location of instructions. Only valid for CODE_MOVED.
3065
+ void* new_code_start;
3066
+ };
3067
+ };
3068
+
3069
+ /**
3070
+ * Option flags passed to the SetJitCodeEventHandler function.
3071
+ */
3072
+ enum JitCodeEventOptions {
3073
+ kJitCodeEventDefault = 0,
3074
+ // Generate callbacks for already existent code.
3075
+ kJitCodeEventEnumExisting = 1
3076
+ };
3077
+
3078
+
3079
+ /**
3080
+ * Callback function passed to SetJitCodeEventHandler.
3081
+ *
3082
+ * \param event code add, move or removal event.
3083
+ */
3084
+ typedef void (*JitCodeEventHandler)(const JitCodeEvent* event);
3085
+
3086
+
3087
+ /**
3088
+ * Interface for iterating through all external resources in the heap.
3089
+ */
3090
+ class V8EXPORT ExternalResourceVisitor { // NOLINT
3091
+ public:
3092
+ virtual ~ExternalResourceVisitor() {}
3093
+ virtual void VisitExternalString(Handle<String> string) {}
3094
+ };
3095
+
3096
+
3097
+ /**
3098
+ * Interface for iterating through all the persistent handles in the heap.
3099
+ */
3100
+ class V8EXPORT PersistentHandleVisitor { // NOLINT
3101
+ public:
3102
+ virtual ~PersistentHandleVisitor() {}
3103
+ virtual void VisitPersistentHandle(Persistent<Value> value,
3104
+ uint16_t class_id) {}
3105
+ };
3106
+
3107
+
3108
+ /**
3109
+ * Container class for static utility functions.
3110
+ */
3111
+ class V8EXPORT V8 {
3112
+ public:
3113
+ /** Set the callback to invoke in case of fatal errors. */
3114
+ static void SetFatalErrorHandler(FatalErrorCallback that);
3115
+
3116
+ /**
3117
+ * Set the callback to invoke to check if code generation from
3118
+ * strings should be allowed.
3119
+ */
3120
+ static void SetAllowCodeGenerationFromStringsCallback(
3121
+ AllowCodeGenerationFromStringsCallback that);
3122
+
3123
+ /**
3124
+ * Ignore out-of-memory exceptions.
3125
+ *
3126
+ * V8 running out of memory is treated as a fatal error by default.
3127
+ * This means that the fatal error handler is called and that V8 is
3128
+ * terminated.
3129
+ *
3130
+ * IgnoreOutOfMemoryException can be used to not treat an
3131
+ * out-of-memory situation as a fatal error. This way, the contexts
3132
+ * that did not cause the out of memory problem might be able to
3133
+ * continue execution.
3134
+ */
3135
+ static void IgnoreOutOfMemoryException();
3136
+
3137
+ /**
3138
+ * Check if V8 is dead and therefore unusable. This is the case after
3139
+ * fatal errors such as out-of-memory situations.
3140
+ */
3141
+ static bool IsDead();
3142
+
3143
+ /**
3144
+ * The following 4 functions are to be used when V8 is built with
3145
+ * the 'compress_startup_data' flag enabled. In this case, the
3146
+ * embedder must decompress startup data prior to initializing V8.
3147
+ *
3148
+ * This is how interaction with V8 should look like:
3149
+ * int compressed_data_count = v8::V8::GetCompressedStartupDataCount();
3150
+ * v8::StartupData* compressed_data =
3151
+ * new v8::StartupData[compressed_data_count];
3152
+ * v8::V8::GetCompressedStartupData(compressed_data);
3153
+ * ... decompress data (compressed_data can be updated in-place) ...
3154
+ * v8::V8::SetDecompressedStartupData(compressed_data);
3155
+ * ... now V8 can be initialized
3156
+ * ... make sure the decompressed data stays valid until V8 shutdown
3157
+ *
3158
+ * A helper class StartupDataDecompressor is provided. It implements
3159
+ * the protocol of the interaction described above, and can be used in
3160
+ * most cases instead of calling these API functions directly.
3161
+ */
3162
+ static StartupData::CompressionAlgorithm GetCompressedStartupDataAlgorithm();
3163
+ static int GetCompressedStartupDataCount();
3164
+ static void GetCompressedStartupData(StartupData* compressed_data);
3165
+ static void SetDecompressedStartupData(StartupData* decompressed_data);
3166
+
3167
+ /**
3168
+ * Adds a message listener.
3169
+ *
3170
+ * The same message listener can be added more than once and in that
3171
+ * case it will be called more than once for each message.
3172
+ */
3173
+ static bool AddMessageListener(MessageCallback that);
3174
+
3175
+ /**
3176
+ * Remove all message listeners from the specified callback function.
3177
+ */
3178
+ static void RemoveMessageListeners(MessageCallback that);
3179
+
3180
+ /**
3181
+ * Tells V8 to capture current stack trace when uncaught exception occurs
3182
+ * and report it to the message listeners. The option is off by default.
3183
+ */
3184
+ static void SetCaptureStackTraceForUncaughtExceptions(
3185
+ bool capture,
3186
+ int frame_limit = 10,
3187
+ StackTrace::StackTraceOptions options = StackTrace::kOverview);
3188
+
3189
+ /**
3190
+ * Sets V8 flags from a string.
3191
+ */
3192
+ static void SetFlagsFromString(const char* str, int length);
3193
+
3194
+ /**
3195
+ * Sets V8 flags from the command line.
3196
+ */
3197
+ static void SetFlagsFromCommandLine(int* argc,
3198
+ char** argv,
3199
+ bool remove_flags);
3200
+
3201
+ /** Get the version string. */
3202
+ static const char* GetVersion();
3203
+
3204
+ /**
3205
+ * Enables the host application to provide a mechanism for recording
3206
+ * statistics counters.
3207
+ */
3208
+ static void SetCounterFunction(CounterLookupCallback);
3209
+
3210
+ /**
3211
+ * Enables the host application to provide a mechanism for recording
3212
+ * histograms. The CreateHistogram function returns a
3213
+ * histogram which will later be passed to the AddHistogramSample
3214
+ * function.
3215
+ */
3216
+ static void SetCreateHistogramFunction(CreateHistogramCallback);
3217
+ static void SetAddHistogramSampleFunction(AddHistogramSampleCallback);
3218
+
3219
+ /** Callback function for reporting failed access checks.*/
3220
+ static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);
3221
+
3222
+ /**
3223
+ * Enables the host application to receive a notification before a
3224
+ * garbage collection. Allocations are not allowed in the
3225
+ * callback function, you therefore cannot manipulate objects (set
3226
+ * or delete properties for example) since it is possible such
3227
+ * operations will result in the allocation of objects. It is possible
3228
+ * to specify the GCType filter for your callback. But it is not possible to
3229
+ * register the same callback function two times with different
3230
+ * GCType filters.
3231
+ */
3232
+ static void AddGCPrologueCallback(
3233
+ GCPrologueCallback callback, GCType gc_type_filter = kGCTypeAll);
3234
+
3235
+ /**
3236
+ * This function removes callback which was installed by
3237
+ * AddGCPrologueCallback function.
3238
+ */
3239
+ static void RemoveGCPrologueCallback(GCPrologueCallback callback);
3240
+
3241
+ /**
3242
+ * The function is deprecated. Please use AddGCPrologueCallback instead.
3243
+ * Enables the host application to receive a notification before a
3244
+ * garbage collection. Allocations are not allowed in the
3245
+ * callback function, you therefore cannot manipulate objects (set
3246
+ * or delete properties for example) since it is possible such
3247
+ * operations will result in the allocation of objects.
3248
+ */
3249
+ V8_DEPRECATED(static void SetGlobalGCPrologueCallback(GCCallback));
3250
+
3251
+ /**
3252
+ * Enables the host application to receive a notification after a
3253
+ * garbage collection. Allocations are not allowed in the
3254
+ * callback function, you therefore cannot manipulate objects (set
3255
+ * or delete properties for example) since it is possible such
3256
+ * operations will result in the allocation of objects. It is possible
3257
+ * to specify the GCType filter for your callback. But it is not possible to
3258
+ * register the same callback function two times with different
3259
+ * GCType filters.
3260
+ */
3261
+ static void AddGCEpilogueCallback(
3262
+ GCEpilogueCallback callback, GCType gc_type_filter = kGCTypeAll);
3263
+
3264
+ /**
3265
+ * This function removes callback which was installed by
3266
+ * AddGCEpilogueCallback function.
3267
+ */
3268
+ static void RemoveGCEpilogueCallback(GCEpilogueCallback callback);
3269
+
3270
+ /**
3271
+ * The function is deprecated. Please use AddGCEpilogueCallback instead.
3272
+ * Enables the host application to receive a notification after a
3273
+ * major garbage collection. Allocations are not allowed in the
3274
+ * callback function, you therefore cannot manipulate objects (set
3275
+ * or delete properties for example) since it is possible such
3276
+ * operations will result in the allocation of objects.
3277
+ */
3278
+ V8_DEPRECATED(static void SetGlobalGCEpilogueCallback(GCCallback));
3279
+
3280
+ /**
3281
+ * Enables the host application to provide a mechanism to be notified
3282
+ * and perform custom logging when V8 Allocates Executable Memory.
3283
+ */
3284
+ static void AddMemoryAllocationCallback(MemoryAllocationCallback callback,
3285
+ ObjectSpace space,
3286
+ AllocationAction action);
3287
+
3288
+ /**
3289
+ * Removes callback that was installed by AddMemoryAllocationCallback.
3290
+ */
3291
+ static void RemoveMemoryAllocationCallback(MemoryAllocationCallback callback);
3292
+
3293
+ /**
3294
+ * Adds a callback to notify the host application when a script finished
3295
+ * running. If a script re-enters the runtime during executing, the
3296
+ * CallCompletedCallback is only invoked when the outer-most script
3297
+ * execution ends. Executing scripts inside the callback do not trigger
3298
+ * further callbacks.
3299
+ */
3300
+ static void AddCallCompletedCallback(CallCompletedCallback callback);
3301
+
3302
+ /**
3303
+ * Removes callback that was installed by AddCallCompletedCallback.
3304
+ */
3305
+ static void RemoveCallCompletedCallback(CallCompletedCallback callback);
3306
+
3307
+ /**
3308
+ * Allows the host application to group objects together. If one
3309
+ * object in the group is alive, all objects in the group are alive.
3310
+ * After each garbage collection, object groups are removed. It is
3311
+ * intended to be used in the before-garbage-collection callback
3312
+ * function, for instance to simulate DOM tree connections among JS
3313
+ * wrapper objects. Object groups for all dependent handles need to
3314
+ * be provided for kGCTypeMarkSweepCompact collections, for all other
3315
+ * garbage collection types it is sufficient to provide object groups
3316
+ * for partially dependent handles only.
3317
+ * See v8-profiler.h for RetainedObjectInfo interface description.
3318
+ */
3319
+ static void AddObjectGroup(Persistent<Value>* objects,
3320
+ size_t length,
3321
+ RetainedObjectInfo* info = NULL);
3322
+ static void AddObjectGroup(Isolate* isolate,
3323
+ Persistent<Value>* objects,
3324
+ size_t length,
3325
+ RetainedObjectInfo* info = NULL);
3326
+
3327
+ /**
3328
+ * Allows the host application to declare implicit references between
3329
+ * the objects: if |parent| is alive, all |children| are alive too.
3330
+ * After each garbage collection, all implicit references
3331
+ * are removed. It is intended to be used in the before-garbage-collection
3332
+ * callback function.
3333
+ */
3334
+ static void AddImplicitReferences(Persistent<Object> parent,
3335
+ Persistent<Value>* children,
3336
+ size_t length);
3337
+
3338
+ /**
3339
+ * Initializes from snapshot if possible. Otherwise, attempts to
3340
+ * initialize from scratch. This function is called implicitly if
3341
+ * you use the API without calling it first.
3342
+ */
3343
+ static bool Initialize();
3344
+
3345
+ /**
3346
+ * Allows the host application to provide a callback which can be used
3347
+ * as a source of entropy for random number generators.
3348
+ */
3349
+ static void SetEntropySource(EntropySource source);
3350
+
3351
+ /**
3352
+ * Allows the host application to provide a callback that allows v8 to
3353
+ * cooperate with a profiler that rewrites return addresses on stack.
3354
+ */
3355
+ static void SetReturnAddressLocationResolver(
3356
+ ReturnAddressLocationResolver return_address_resolver);
3357
+
3358
+ /**
3359
+ * Allows the host application to provide the address of a function that's
3360
+ * invoked on entry to every V8-generated function.
3361
+ * Note that \p entry_hook is invoked at the very start of each
3362
+ * generated function.
3363
+ *
3364
+ * \param entry_hook a function that will be invoked on entry to every
3365
+ * V8-generated function.
3366
+ * \returns true on success on supported platforms, false on failure.
3367
+ * \note Setting a new entry hook function when one is already active will
3368
+ * fail.
3369
+ */
3370
+ static bool SetFunctionEntryHook(FunctionEntryHook entry_hook);
3371
+
3372
+ /**
3373
+ * Allows the host application to provide the address of a function that is
3374
+ * notified each time code is added, moved or removed.
3375
+ *
3376
+ * \param options options for the JIT code event handler.
3377
+ * \param event_handler the JIT code event handler, which will be invoked
3378
+ * each time code is added, moved or removed.
3379
+ * \note \p event_handler won't get notified of existent code.
3380
+ * \note since code removal notifications are not currently issued, the
3381
+ * \p event_handler may get notifications of code that overlaps earlier
3382
+ * code notifications. This happens when code areas are reused, and the
3383
+ * earlier overlapping code areas should therefore be discarded.
3384
+ * \note the events passed to \p event_handler and the strings they point to
3385
+ * are not guaranteed to live past each call. The \p event_handler must
3386
+ * copy strings and other parameters it needs to keep around.
3387
+ * \note the set of events declared in JitCodeEvent::EventType is expected to
3388
+ * grow over time, and the JitCodeEvent structure is expected to accrue
3389
+ * new members. The \p event_handler function must ignore event codes
3390
+ * it does not recognize to maintain future compatibility.
3391
+ */
3392
+ static void SetJitCodeEventHandler(JitCodeEventOptions options,
3393
+ JitCodeEventHandler event_handler);
3394
+
3395
+ /**
3396
+ * Adjusts the amount of registered external memory. Used to give
3397
+ * V8 an indication of the amount of externally allocated memory
3398
+ * that is kept alive by JavaScript objects. V8 uses this to decide
3399
+ * when to perform global garbage collections. Registering
3400
+ * externally allocated memory will trigger global garbage
3401
+ * collections more often than otherwise in an attempt to garbage
3402
+ * collect the JavaScript objects keeping the externally allocated
3403
+ * memory alive.
3404
+ *
3405
+ * \param change_in_bytes the change in externally allocated memory
3406
+ * that is kept alive by JavaScript objects.
3407
+ * \returns the adjusted value.
3408
+ */
3409
+ static intptr_t AdjustAmountOfExternalAllocatedMemory(
3410
+ intptr_t change_in_bytes);
3411
+
3412
+ /**
3413
+ * Suspends recording of tick samples in the profiler.
3414
+ * When the V8 profiling mode is enabled (usually via command line
3415
+ * switches) this function suspends recording of tick samples.
3416
+ * Profiling ticks are discarded until ResumeProfiler() is called.
3417
+ *
3418
+ * See also the --prof and --prof_auto command line switches to
3419
+ * enable V8 profiling.
3420
+ */
3421
+ static void PauseProfiler();
3422
+
3423
+ /**
3424
+ * Resumes recording of tick samples in the profiler.
3425
+ * See also PauseProfiler().
3426
+ */
3427
+ static void ResumeProfiler();
3428
+
3429
+ /**
3430
+ * Return whether profiler is currently paused.
3431
+ */
3432
+ static bool IsProfilerPaused();
3433
+
3434
+ /**
3435
+ * Retrieve the V8 thread id of the calling thread.
3436
+ *
3437
+ * The thread id for a thread should only be retrieved after the V8
3438
+ * lock has been acquired with a Locker object with that thread.
3439
+ */
3440
+ static int GetCurrentThreadId();
3441
+
3442
+ /**
3443
+ * Forcefully terminate execution of a JavaScript thread. This can
3444
+ * be used to terminate long-running scripts.
3445
+ *
3446
+ * TerminateExecution should only be called when then V8 lock has
3447
+ * been acquired with a Locker object. Therefore, in order to be
3448
+ * able to terminate long-running threads, preemption must be
3449
+ * enabled to allow the user of TerminateExecution to acquire the
3450
+ * lock.
3451
+ *
3452
+ * The termination is achieved by throwing an exception that is
3453
+ * uncatchable by JavaScript exception handlers. Termination
3454
+ * exceptions act as if they were caught by a C++ TryCatch exception
3455
+ * handler. If forceful termination is used, any C++ TryCatch
3456
+ * exception handler that catches an exception should check if that
3457
+ * exception is a termination exception and immediately return if
3458
+ * that is the case. Returning immediately in that case will
3459
+ * continue the propagation of the termination exception if needed.
3460
+ *
3461
+ * The thread id passed to TerminateExecution must have been
3462
+ * obtained by calling GetCurrentThreadId on the thread in question.
3463
+ *
3464
+ * \param thread_id The thread id of the thread to terminate.
3465
+ */
3466
+ static void TerminateExecution(int thread_id);
3467
+
3468
+ /**
3469
+ * Forcefully terminate the current thread of JavaScript execution
3470
+ * in the given isolate. If no isolate is provided, the default
3471
+ * isolate is used.
3472
+ *
3473
+ * This method can be used by any thread even if that thread has not
3474
+ * acquired the V8 lock with a Locker object.
3475
+ *
3476
+ * \param isolate The isolate in which to terminate the current JS execution.
3477
+ */
3478
+ static void TerminateExecution(Isolate* isolate = NULL);
3479
+
3480
+ /**
3481
+ * Is V8 terminating JavaScript execution.
3482
+ *
3483
+ * Returns true if JavaScript execution is currently terminating
3484
+ * because of a call to TerminateExecution. In that case there are
3485
+ * still JavaScript frames on the stack and the termination
3486
+ * exception is still active.
3487
+ *
3488
+ * \param isolate The isolate in which to check.
3489
+ */
3490
+ static bool IsExecutionTerminating(Isolate* isolate = NULL);
3491
+
3492
+ /**
3493
+ * Releases any resources used by v8 and stops any utility threads
3494
+ * that may be running. Note that disposing v8 is permanent, it
3495
+ * cannot be reinitialized.
3496
+ *
3497
+ * It should generally not be necessary to dispose v8 before exiting
3498
+ * a process, this should happen automatically. It is only necessary
3499
+ * to use if the process needs the resources taken up by v8.
3500
+ */
3501
+ static bool Dispose();
3502
+
3503
+ /**
3504
+ * Get statistics about the heap memory usage.
3505
+ */
3506
+ static void GetHeapStatistics(HeapStatistics* heap_statistics);
3507
+
3508
+ /**
3509
+ * Iterates through all external resources referenced from current isolate
3510
+ * heap. GC is not invoked prior to iterating, therefore there is no
3511
+ * guarantee that visited objects are still alive.
3512
+ */
3513
+ static void VisitExternalResources(ExternalResourceVisitor* visitor);
3514
+
3515
+ /**
3516
+ * Iterates through all the persistent handles in the current isolate's heap
3517
+ * that have class_ids.
3518
+ */
3519
+ static void VisitHandlesWithClassIds(PersistentHandleVisitor* visitor);
3520
+
3521
+ /**
3522
+ * Iterates through all the persistent handles in the current isolate's heap
3523
+ * that have class_ids and are candidates to be marked as partially dependent
3524
+ * handles. This will visit handles to young objects created since the last
3525
+ * garbage collection but is free to visit an arbitrary superset of these
3526
+ * objects.
3527
+ */
3528
+ static void VisitHandlesForPartialDependence(
3529
+ Isolate* isolate, PersistentHandleVisitor* visitor);
3530
+
3531
+ /**
3532
+ * Optional notification that the embedder is idle.
3533
+ * V8 uses the notification to reduce memory footprint.
3534
+ * This call can be used repeatedly if the embedder remains idle.
3535
+ * Returns true if the embedder should stop calling IdleNotification
3536
+ * until real work has been done. This indicates that V8 has done
3537
+ * as much cleanup as it will be able to do.
3538
+ *
3539
+ * The hint argument specifies the amount of work to be done in the function
3540
+ * on scale from 1 to 1000. There is no guarantee that the actual work will
3541
+ * match the hint.
3542
+ */
3543
+ static bool IdleNotification(int hint = 1000);
3544
+
3545
+ /**
3546
+ * Optional notification that the system is running low on memory.
3547
+ * V8 uses these notifications to attempt to free memory.
3548
+ */
3549
+ static void LowMemoryNotification();
3550
+
3551
+ /**
3552
+ * Optional notification that a context has been disposed. V8 uses
3553
+ * these notifications to guide the GC heuristic. Returns the number
3554
+ * of context disposals - including this one - since the last time
3555
+ * V8 had a chance to clean up.
3556
+ */
3557
+ static int ContextDisposedNotification();
3558
+
3559
+ private:
3560
+ V8();
3561
+
3562
+ static internal::Object** GlobalizeReference(internal::Isolate* isolate,
3563
+ internal::Object** handle);
3564
+ static void DisposeGlobal(internal::Isolate* isolate,
3565
+ internal::Object** global_handle);
3566
+ static void MakeWeak(internal::Isolate* isolate,
3567
+ internal::Object** global_handle,
3568
+ void* data,
3569
+ WeakReferenceCallback weak_reference_callback,
3570
+ NearDeathCallback near_death_callback);
3571
+ static void ClearWeak(internal::Isolate* isolate,
3572
+ internal::Object** global_handle);
3573
+
3574
+ template <class T> friend class Handle;
3575
+ template <class T> friend class Local;
3576
+ template <class T> friend class Persistent;
3577
+ friend class Context;
3578
+ };
3579
+
3580
+
3581
+ /**
3582
+ * An external exception handler.
3583
+ */
3584
+ class V8EXPORT TryCatch {
3585
+ public:
3586
+ /**
3587
+ * Creates a new try/catch block and registers it with v8. Note that
3588
+ * all TryCatch blocks should be stack allocated because the memory
3589
+ * location itself is compared against JavaScript try/catch blocks.
3590
+ */
3591
+ TryCatch();
3592
+
3593
+ /**
3594
+ * Unregisters and deletes this try/catch block.
3595
+ */
3596
+ ~TryCatch();
3597
+
3598
+ /**
3599
+ * Returns true if an exception has been caught by this try/catch block.
3600
+ */
3601
+ bool HasCaught() const;
3602
+
3603
+ /**
3604
+ * For certain types of exceptions, it makes no sense to continue
3605
+ * execution.
3606
+ *
3607
+ * Currently, the only type of exception that can be caught by a
3608
+ * TryCatch handler and for which it does not make sense to continue
3609
+ * is termination exception. Such exceptions are thrown when the
3610
+ * TerminateExecution methods are called to terminate a long-running
3611
+ * script.
3612
+ *
3613
+ * If CanContinue returns false, the correct action is to perform
3614
+ * any C++ cleanup needed and then return.
3615
+ */
3616
+ bool CanContinue() const;
3617
+
3618
+ /**
3619
+ * Throws the exception caught by this TryCatch in a way that avoids
3620
+ * it being caught again by this same TryCatch. As with ThrowException
3621
+ * it is illegal to execute any JavaScript operations after calling
3622
+ * ReThrow; the caller must return immediately to where the exception
3623
+ * is caught.
3624
+ */
3625
+ Handle<Value> ReThrow();
3626
+
3627
+ /**
3628
+ * Returns the exception caught by this try/catch block. If no exception has
3629
+ * been caught an empty handle is returned.
3630
+ *
3631
+ * The returned handle is valid until this TryCatch block has been destroyed.
3632
+ */
3633
+ Local<Value> Exception() const;
3634
+
3635
+ /**
3636
+ * Returns the .stack property of the thrown object. If no .stack
3637
+ * property is present an empty handle is returned.
3638
+ */
3639
+ Local<Value> StackTrace() const;
3640
+
3641
+ /**
3642
+ * Returns the message associated with this exception. If there is
3643
+ * no message associated an empty handle is returned.
3644
+ *
3645
+ * The returned handle is valid until this TryCatch block has been
3646
+ * destroyed.
3647
+ */
3648
+ Local<v8::Message> Message() const;
3649
+
3650
+ /**
3651
+ * Clears any exceptions that may have been caught by this try/catch block.
3652
+ * After this method has been called, HasCaught() will return false.
3653
+ *
3654
+ * It is not necessary to clear a try/catch block before using it again; if
3655
+ * another exception is thrown the previously caught exception will just be
3656
+ * overwritten. However, it is often a good idea since it makes it easier
3657
+ * to determine which operation threw a given exception.
3658
+ */
3659
+ void Reset();
3660
+
3661
+ /**
3662
+ * Set verbosity of the external exception handler.
3663
+ *
3664
+ * By default, exceptions that are caught by an external exception
3665
+ * handler are not reported. Call SetVerbose with true on an
3666
+ * external exception handler to have exceptions caught by the
3667
+ * handler reported as if they were not caught.
3668
+ */
3669
+ void SetVerbose(bool value);
3670
+
3671
+ /**
3672
+ * Set whether or not this TryCatch should capture a Message object
3673
+ * which holds source information about where the exception
3674
+ * occurred. True by default.
3675
+ */
3676
+ void SetCaptureMessage(bool value);
3677
+
3678
+ private:
3679
+ // Make it hard to create heap-allocated TryCatch blocks.
3680
+ TryCatch(const TryCatch&);
3681
+ void operator=(const TryCatch&);
3682
+ void* operator new(size_t size);
3683
+ void operator delete(void*, size_t);
3684
+
3685
+ v8::internal::Isolate* isolate_;
3686
+ void* next_;
3687
+ void* exception_;
3688
+ void* message_;
3689
+ bool is_verbose_ : 1;
3690
+ bool can_continue_ : 1;
3691
+ bool capture_message_ : 1;
3692
+ bool rethrow_ : 1;
3693
+
3694
+ friend class v8::internal::Isolate;
3695
+ };
3696
+
3697
+
3698
+ // --- Context ---
3699
+
3700
+
3701
+ /**
3702
+ * Ignore
3703
+ */
3704
+ class V8EXPORT ExtensionConfiguration {
3705
+ public:
3706
+ ExtensionConfiguration(int name_count, const char* names[])
3707
+ : name_count_(name_count), names_(names) { }
3708
+ private:
3709
+ friend class ImplementationUtilities;
3710
+ int name_count_;
3711
+ const char** names_;
3712
+ };
3713
+
3714
+
3715
+ /**
3716
+ * A sandboxed execution context with its own set of built-in objects
3717
+ * and functions.
3718
+ */
3719
+ class V8EXPORT Context {
3720
+ public:
3721
+ /**
3722
+ * Returns the global proxy object or global object itself for
3723
+ * detached contexts.
3724
+ *
3725
+ * Global proxy object is a thin wrapper whose prototype points to
3726
+ * actual context's global object with the properties like Object, etc.
3727
+ * This is done that way for security reasons (for more details see
3728
+ * https://wiki.mozilla.org/Gecko:SplitWindow).
3729
+ *
3730
+ * Please note that changes to global proxy object prototype most probably
3731
+ * would break VM---v8 expects only global object as a prototype of
3732
+ * global proxy object.
3733
+ *
3734
+ * If DetachGlobal() has been invoked, Global() would return actual global
3735
+ * object until global is reattached with ReattachGlobal().
3736
+ */
3737
+ Local<Object> Global();
3738
+
3739
+ /**
3740
+ * Detaches the global object from its context before
3741
+ * the global object can be reused to create a new context.
3742
+ */
3743
+ void DetachGlobal();
3744
+
3745
+ /**
3746
+ * Reattaches a global object to a context. This can be used to
3747
+ * restore the connection between a global object and a context
3748
+ * after DetachGlobal has been called.
3749
+ *
3750
+ * \param global_object The global object to reattach to the
3751
+ * context. For this to work, the global object must be the global
3752
+ * object that was associated with this context before a call to
3753
+ * DetachGlobal.
3754
+ */
3755
+ void ReattachGlobal(Handle<Object> global_object);
3756
+
3757
+ /** Creates a new context.
3758
+ *
3759
+ * Returns a persistent handle to the newly allocated context. This
3760
+ * persistent handle has to be disposed when the context is no
3761
+ * longer used so the context can be garbage collected.
3762
+ *
3763
+ * \param extensions An optional extension configuration containing
3764
+ * the extensions to be installed in the newly created context.
3765
+ *
3766
+ * \param global_template An optional object template from which the
3767
+ * global object for the newly created context will be created.
3768
+ *
3769
+ * \param global_object An optional global object to be reused for
3770
+ * the newly created context. This global object must have been
3771
+ * created by a previous call to Context::New with the same global
3772
+ * template. The state of the global object will be completely reset
3773
+ * and only object identify will remain.
3774
+ */
3775
+ static Persistent<Context> New(
3776
+ ExtensionConfiguration* extensions = NULL,
3777
+ Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
3778
+ Handle<Value> global_object = Handle<Value>());
3779
+
3780
+ /** Returns the last entered context. */
3781
+ static Local<Context> GetEntered();
3782
+
3783
+ /** Returns the context that is on the top of the stack. */
3784
+ static Local<Context> GetCurrent();
3785
+
3786
+ /**
3787
+ * Returns the context of the calling JavaScript code. That is the
3788
+ * context of the top-most JavaScript frame. If there are no
3789
+ * JavaScript frames an empty handle is returned.
3790
+ */
3791
+ static Local<Context> GetCalling();
3792
+
3793
+ /**
3794
+ * Sets the security token for the context. To access an object in
3795
+ * another context, the security tokens must match.
3796
+ */
3797
+ void SetSecurityToken(Handle<Value> token);
3798
+
3799
+ /** Restores the security token to the default value. */
3800
+ void UseDefaultSecurityToken();
3801
+
3802
+ /** Returns the security token of this context.*/
3803
+ Handle<Value> GetSecurityToken();
3804
+
3805
+ /**
3806
+ * Enter this context. After entering a context, all code compiled
3807
+ * and run is compiled and run in this context. If another context
3808
+ * is already entered, this old context is saved so it can be
3809
+ * restored when the new context is exited.
3810
+ */
3811
+ void Enter();
3812
+
3813
+ /**
3814
+ * Exit this context. Exiting the current context restores the
3815
+ * context that was in place when entering the current context.
3816
+ */
3817
+ void Exit();
3818
+
3819
+ /** Returns true if the context has experienced an out of memory situation. */
3820
+ bool HasOutOfMemoryException();
3821
+
3822
+ /** Returns true if V8 has a current context. */
3823
+ static bool InContext();
3824
+
3825
+ /** Returns an isolate associated with a current context. */
3826
+ v8::Isolate* GetIsolate();
3827
+
3828
+ /**
3829
+ * Gets the embedder data with the given index, which must have been set by a
3830
+ * previous call to SetEmbedderData with the same index. Note that index 0
3831
+ * currently has a special meaning for Chrome's debugger.
3832
+ */
3833
+ V8_INLINE(Local<Value> GetEmbedderData(int index));
3834
+
3835
+ /**
3836
+ * Sets the embedder data with the given index, growing the data as
3837
+ * needed. Note that index 0 currently has a special meaning for Chrome's
3838
+ * debugger.
3839
+ */
3840
+ void SetEmbedderData(int index, Handle<Value> value);
3841
+
3842
+ /**
3843
+ * Gets a 2-byte-aligned native pointer from the embedder data with the given
3844
+ * index, which must have bees set by a previous call to
3845
+ * SetAlignedPointerInEmbedderData with the same index. Note that index 0
3846
+ * currently has a special meaning for Chrome's debugger.
3847
+ */
3848
+ V8_INLINE(void* GetAlignedPointerFromEmbedderData(int index));
3849
+
3850
+ /**
3851
+ * Sets a 2-byte-aligned native pointer in the embedder data with the given
3852
+ * index, growing the data as needed. Note that index 0 currently has a
3853
+ * special meaning for Chrome's debugger.
3854
+ */
3855
+ void SetAlignedPointerInEmbedderData(int index, void* value);
3856
+
3857
+ /**
3858
+ * Control whether code generation from strings is allowed. Calling
3859
+ * this method with false will disable 'eval' and the 'Function'
3860
+ * constructor for code running in this context. If 'eval' or the
3861
+ * 'Function' constructor are used an exception will be thrown.
3862
+ *
3863
+ * If code generation from strings is not allowed the
3864
+ * V8::AllowCodeGenerationFromStrings callback will be invoked if
3865
+ * set before blocking the call to 'eval' or the 'Function'
3866
+ * constructor. If that callback returns true, the call will be
3867
+ * allowed, otherwise an exception will be thrown. If no callback is
3868
+ * set an exception will be thrown.
3869
+ */
3870
+ void AllowCodeGenerationFromStrings(bool allow);
3871
+
3872
+ /**
3873
+ * Returns true if code generation from strings is allowed for the context.
3874
+ * For more details see AllowCodeGenerationFromStrings(bool) documentation.
3875
+ */
3876
+ bool IsCodeGenerationFromStringsAllowed();
3877
+
3878
+ /**
3879
+ * Sets the error description for the exception that is thrown when
3880
+ * code generation from strings is not allowed and 'eval' or the 'Function'
3881
+ * constructor are called.
3882
+ */
3883
+ void SetErrorMessageForCodeGenerationFromStrings(Handle<String> message);
3884
+
3885
+ /**
3886
+ * Stack-allocated class which sets the execution context for all
3887
+ * operations executed within a local scope.
3888
+ */
3889
+ class Scope {
3890
+ public:
3891
+ explicit V8_INLINE(Scope(Handle<Context> context)) : context_(context) {
3892
+ context_->Enter();
3893
+ }
3894
+ V8_INLINE(~Scope()) { context_->Exit(); }
3895
+ private:
3896
+ Handle<Context> context_;
3897
+ };
3898
+
3899
+ private:
3900
+ friend class Value;
3901
+ friend class Script;
3902
+ friend class Object;
3903
+ friend class Function;
3904
+
3905
+ Local<Value> SlowGetEmbedderData(int index);
3906
+ void* SlowGetAlignedPointerFromEmbedderData(int index);
3907
+ };
3908
+
3909
+
3910
+ /**
3911
+ * Multiple threads in V8 are allowed, but only one thread at a time is allowed
3912
+ * to use any given V8 isolate, see the comments in the Isolate class. The
3913
+ * definition of 'using a V8 isolate' includes accessing handles or holding onto
3914
+ * object pointers obtained from V8 handles while in the particular V8 isolate.
3915
+ * It is up to the user of V8 to ensure, perhaps with locking, that this
3916
+ * constraint is not violated. In addition to any other synchronization
3917
+ * mechanism that may be used, the v8::Locker and v8::Unlocker classes must be
3918
+ * used to signal thead switches to V8.
3919
+ *
3920
+ * v8::Locker is a scoped lock object. While it's active, i.e. between its
3921
+ * construction and destruction, the current thread is allowed to use the locked
3922
+ * isolate. V8 guarantees that an isolate can be locked by at most one thread at
3923
+ * any time. In other words, the scope of a v8::Locker is a critical section.
3924
+ *
3925
+ * Sample usage:
3926
+ * \code
3927
+ * ...
3928
+ * {
3929
+ * v8::Locker locker(isolate);
3930
+ * v8::Isolate::Scope isolate_scope(isolate);
3931
+ * ...
3932
+ * // Code using V8 and isolate goes here.
3933
+ * ...
3934
+ * } // Destructor called here
3935
+ * \endcode
3936
+ *
3937
+ * If you wish to stop using V8 in a thread A you can do this either by
3938
+ * destroying the v8::Locker object as above or by constructing a v8::Unlocker
3939
+ * object:
3940
+ *
3941
+ * \code
3942
+ * {
3943
+ * isolate->Exit();
3944
+ * v8::Unlocker unlocker(isolate);
3945
+ * ...
3946
+ * // Code not using V8 goes here while V8 can run in another thread.
3947
+ * ...
3948
+ * } // Destructor called here.
3949
+ * isolate->Enter();
3950
+ * \endcode
3951
+ *
3952
+ * The Unlocker object is intended for use in a long-running callback from V8,
3953
+ * where you want to release the V8 lock for other threads to use.
3954
+ *
3955
+ * The v8::Locker is a recursive lock, i.e. you can lock more than once in a
3956
+ * given thread. This can be useful if you have code that can be called either
3957
+ * from code that holds the lock or from code that does not. The Unlocker is
3958
+ * not recursive so you can not have several Unlockers on the stack at once, and
3959
+ * you can not use an Unlocker in a thread that is not inside a Locker's scope.
3960
+ *
3961
+ * An unlocker will unlock several lockers if it has to and reinstate the
3962
+ * correct depth of locking on its destruction, e.g.:
3963
+ *
3964
+ * \code
3965
+ * // V8 not locked.
3966
+ * {
3967
+ * v8::Locker locker(isolate);
3968
+ * Isolate::Scope isolate_scope(isolate);
3969
+ * // V8 locked.
3970
+ * {
3971
+ * v8::Locker another_locker(isolate);
3972
+ * // V8 still locked (2 levels).
3973
+ * {
3974
+ * isolate->Exit();
3975
+ * v8::Unlocker unlocker(isolate);
3976
+ * // V8 not locked.
3977
+ * }
3978
+ * isolate->Enter();
3979
+ * // V8 locked again (2 levels).
3980
+ * }
3981
+ * // V8 still locked (1 level).
3982
+ * }
3983
+ * // V8 Now no longer locked.
3984
+ * \endcode
3985
+ */
3986
+ class V8EXPORT Unlocker {
3987
+ public:
3988
+ /**
3989
+ * Initialize Unlocker for a given Isolate.
3990
+ */
3991
+ V8_INLINE(explicit Unlocker(Isolate* isolate)) { Initialize(isolate); }
3992
+
3993
+ /** Deprecated. Use Isolate version instead. */
3994
+ V8_DEPRECATED(Unlocker());
3995
+
3996
+ ~Unlocker();
3997
+ private:
3998
+ void Initialize(Isolate* isolate);
3999
+
4000
+ internal::Isolate* isolate_;
4001
+ };
4002
+
4003
+
4004
+ class V8EXPORT Locker {
4005
+ public:
4006
+ /**
4007
+ * Initialize Locker for a given Isolate.
4008
+ */
4009
+ V8_INLINE(explicit Locker(Isolate* isolate)) { Initialize(isolate); }
4010
+
4011
+ /** Deprecated. Use Isolate version instead. */
4012
+ V8_DEPRECATED(Locker());
4013
+
4014
+ ~Locker();
4015
+
4016
+ /**
4017
+ * Start preemption.
4018
+ *
4019
+ * When preemption is started, a timer is fired every n milliseconds
4020
+ * that will switch between multiple threads that are in contention
4021
+ * for the V8 lock.
4022
+ */
4023
+ static void StartPreemption(int every_n_ms);
4024
+
4025
+ /**
4026
+ * Stop preemption.
4027
+ */
4028
+ static void StopPreemption();
4029
+
4030
+ /**
4031
+ * Returns whether or not the locker for a given isolate, is locked by the
4032
+ * current thread.
4033
+ */
4034
+ static bool IsLocked(Isolate* isolate);
4035
+
4036
+ /**
4037
+ * Returns whether v8::Locker is being used by this V8 instance.
4038
+ */
4039
+ static bool IsActive();
4040
+
4041
+ private:
4042
+ void Initialize(Isolate* isolate);
4043
+
4044
+ bool has_lock_;
4045
+ bool top_level_;
4046
+ internal::Isolate* isolate_;
4047
+
4048
+ static bool active_;
4049
+
4050
+ // Disallow copying and assigning.
4051
+ Locker(const Locker&);
4052
+ void operator=(const Locker&);
4053
+ };
4054
+
4055
+
4056
+ /**
4057
+ * A struct for exporting HeapStats data from V8, using "push" model.
4058
+ */
4059
+ struct HeapStatsUpdate;
4060
+
4061
+
4062
+ /**
4063
+ * An interface for exporting data from V8, using "push" model.
4064
+ */
4065
+ class V8EXPORT OutputStream { // NOLINT
4066
+ public:
4067
+ enum OutputEncoding {
4068
+ kAscii = 0 // 7-bit ASCII.
4069
+ };
4070
+ enum WriteResult {
4071
+ kContinue = 0,
4072
+ kAbort = 1
4073
+ };
4074
+ virtual ~OutputStream() {}
4075
+ /** Notify about the end of stream. */
4076
+ virtual void EndOfStream() = 0;
4077
+ /** Get preferred output chunk size. Called only once. */
4078
+ virtual int GetChunkSize() { return 1024; }
4079
+ /** Get preferred output encoding. Called only once. */
4080
+ virtual OutputEncoding GetOutputEncoding() { return kAscii; }
4081
+ /**
4082
+ * Writes the next chunk of snapshot data into the stream. Writing
4083
+ * can be stopped by returning kAbort as function result. EndOfStream
4084
+ * will not be called in case writing was aborted.
4085
+ */
4086
+ virtual WriteResult WriteAsciiChunk(char* data, int size) = 0;
4087
+ /**
4088
+ * Writes the next chunk of heap stats data into the stream. Writing
4089
+ * can be stopped by returning kAbort as function result. EndOfStream
4090
+ * will not be called in case writing was aborted.
4091
+ */
4092
+ virtual WriteResult WriteHeapStatsChunk(HeapStatsUpdate* data, int count) {
4093
+ return kAbort;
4094
+ };
4095
+ };
4096
+
4097
+
4098
+ /**
4099
+ * An interface for reporting progress and controlling long-running
4100
+ * activities.
4101
+ */
4102
+ class V8EXPORT ActivityControl { // NOLINT
4103
+ public:
4104
+ enum ControlOption {
4105
+ kContinue = 0,
4106
+ kAbort = 1
4107
+ };
4108
+ virtual ~ActivityControl() {}
4109
+ /**
4110
+ * Notify about current progress. The activity can be stopped by
4111
+ * returning kAbort as the callback result.
4112
+ */
4113
+ virtual ControlOption ReportProgressValue(int done, int total) = 0;
4114
+ };
4115
+
4116
+
4117
+ // --- Implementation ---
4118
+
4119
+
4120
+ namespace internal {
4121
+
4122
+ const int kApiPointerSize = sizeof(void*); // NOLINT
4123
+ const int kApiIntSize = sizeof(int); // NOLINT
4124
+
4125
+ // Tag information for HeapObject.
4126
+ const int kHeapObjectTag = 1;
4127
+ const int kHeapObjectTagSize = 2;
4128
+ const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1;
4129
+
4130
+ // Tag information for Smi.
4131
+ const int kSmiTag = 0;
4132
+ const int kSmiTagSize = 1;
4133
+ const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1;
4134
+
4135
+ template <size_t ptr_size> struct SmiTagging;
4136
+
4137
+ // Smi constants for 32-bit systems.
4138
+ template <> struct SmiTagging<4> {
4139
+ static const int kSmiShiftSize = 0;
4140
+ static const int kSmiValueSize = 31;
4141
+ V8_INLINE(static int SmiToInt(internal::Object* value)) {
4142
+ int shift_bits = kSmiTagSize + kSmiShiftSize;
4143
+ // Throw away top 32 bits and shift down (requires >> to be sign extending).
4144
+ return static_cast<int>(reinterpret_cast<intptr_t>(value)) >> shift_bits;
4145
+ }
4146
+ };
4147
+
4148
+ // Smi constants for 64-bit systems.
4149
+ template <> struct SmiTagging<8> {
4150
+ static const int kSmiShiftSize = 31;
4151
+ static const int kSmiValueSize = 32;
4152
+ V8_INLINE(static int SmiToInt(internal::Object* value)) {
4153
+ int shift_bits = kSmiTagSize + kSmiShiftSize;
4154
+ // Shift down and throw away top 32 bits.
4155
+ return static_cast<int>(reinterpret_cast<intptr_t>(value) >> shift_bits);
4156
+ }
4157
+ };
4158
+
4159
+ typedef SmiTagging<kApiPointerSize> PlatformSmiTagging;
4160
+ const int kSmiShiftSize = PlatformSmiTagging::kSmiShiftSize;
4161
+ const int kSmiValueSize = PlatformSmiTagging::kSmiValueSize;
4162
+
4163
+ /**
4164
+ * This class exports constants and functionality from within v8 that
4165
+ * is necessary to implement inline functions in the v8 api. Don't
4166
+ * depend on functions and constants defined here.
4167
+ */
4168
+ class Internals {
4169
+ public:
4170
+ // These values match non-compiler-dependent values defined within
4171
+ // the implementation of v8.
4172
+ static const int kHeapObjectMapOffset = 0;
4173
+ static const int kMapInstanceTypeOffset = 1 * kApiPointerSize + kApiIntSize;
4174
+ static const int kStringResourceOffset = 3 * kApiPointerSize;
4175
+
4176
+ static const int kOddballKindOffset = 3 * kApiPointerSize;
4177
+ static const int kForeignAddressOffset = kApiPointerSize;
4178
+ static const int kJSObjectHeaderSize = 3 * kApiPointerSize;
4179
+ static const int kFixedArrayHeaderSize = 2 * kApiPointerSize;
4180
+ static const int kContextHeaderSize = 2 * kApiPointerSize;
4181
+ static const int kContextEmbedderDataIndex = 54;
4182
+ static const int kFullStringRepresentationMask = 0x07;
4183
+ static const int kStringEncodingMask = 0x4;
4184
+ static const int kExternalTwoByteRepresentationTag = 0x02;
4185
+ static const int kExternalAsciiRepresentationTag = 0x06;
4186
+
4187
+ static const int kIsolateStateOffset = 0;
4188
+ static const int kIsolateEmbedderDataOffset = 1 * kApiPointerSize;
4189
+ static const int kIsolateRootsOffset = 3 * kApiPointerSize;
4190
+ static const int kUndefinedValueRootIndex = 5;
4191
+ static const int kNullValueRootIndex = 7;
4192
+ static const int kTrueValueRootIndex = 8;
4193
+ static const int kFalseValueRootIndex = 9;
4194
+ static const int kEmptySymbolRootIndex = 119;
4195
+
4196
+ static const int kNodeClassIdOffset = 1 * kApiPointerSize;
4197
+ static const int kNodeFlagsOffset = 1 * kApiPointerSize + 3;
4198
+ static const int kNodeStateMask = 0xf;
4199
+ static const int kNodeStateIsWeakValue = 2;
4200
+ static const int kNodeStateIsNearDeathValue = 4;
4201
+ static const int kNodeIsIndependentShift = 4;
4202
+ static const int kNodeIsPartiallyDependentShift = 5;
4203
+
4204
+ static const int kJSObjectType = 0xab;
4205
+ static const int kFirstNonstringType = 0x80;
4206
+ static const int kOddballType = 0x82;
4207
+ static const int kForeignType = 0x85;
4208
+
4209
+ static const int kUndefinedOddballKind = 5;
4210
+ static const int kNullOddballKind = 3;
4211
+
4212
+ V8_INLINE(static bool HasHeapObjectTag(internal::Object* value)) {
4213
+ return ((reinterpret_cast<intptr_t>(value) & kHeapObjectTagMask) ==
4214
+ kHeapObjectTag);
4215
+ }
4216
+
4217
+ V8_INLINE(static int SmiValue(internal::Object* value)) {
4218
+ return PlatformSmiTagging::SmiToInt(value);
4219
+ }
4220
+
4221
+ V8_INLINE(static int GetInstanceType(internal::Object* obj)) {
4222
+ typedef internal::Object O;
4223
+ O* map = ReadField<O*>(obj, kHeapObjectMapOffset);
4224
+ return ReadField<uint8_t>(map, kMapInstanceTypeOffset);
4225
+ }
4226
+
4227
+ V8_INLINE(static int GetOddballKind(internal::Object* obj)) {
4228
+ typedef internal::Object O;
4229
+ return SmiValue(ReadField<O*>(obj, kOddballKindOffset));
4230
+ }
4231
+
4232
+ V8_INLINE(static bool IsExternalTwoByteString(int instance_type)) {
4233
+ int representation = (instance_type & kFullStringRepresentationMask);
4234
+ return representation == kExternalTwoByteRepresentationTag;
4235
+ }
4236
+
4237
+ V8_INLINE(static bool IsInitialized(v8::Isolate* isolate)) {
4238
+ uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) + kIsolateStateOffset;
4239
+ return *reinterpret_cast<int*>(addr) == 1;
4240
+ }
4241
+
4242
+ V8_INLINE(static uint8_t GetNodeFlag(internal::Object** obj, int shift)) {
4243
+ uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
4244
+ return *addr & (1 << shift);
4245
+ }
4246
+
4247
+ V8_INLINE(static void UpdateNodeFlag(internal::Object** obj,
4248
+ bool value, int shift)) {
4249
+ uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
4250
+ uint8_t mask = 1 << shift;
4251
+ *addr = (*addr & ~mask) | (value << shift);
4252
+ }
4253
+
4254
+ V8_INLINE(static uint8_t GetNodeState(internal::Object** obj)) {
4255
+ uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
4256
+ return *addr & kNodeStateMask;
4257
+ }
4258
+
4259
+ V8_INLINE(static void UpdateNodeState(internal::Object** obj,
4260
+ uint8_t value)) {
4261
+ uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
4262
+ *addr = (*addr & ~kNodeStateMask) | value;
4263
+ }
4264
+
4265
+ V8_INLINE(static void SetEmbedderData(v8::Isolate* isolate, void* data)) {
4266
+ uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) +
4267
+ kIsolateEmbedderDataOffset;
4268
+ *reinterpret_cast<void**>(addr) = data;
4269
+ }
4270
+
4271
+ V8_INLINE(static void* GetEmbedderData(v8::Isolate* isolate)) {
4272
+ uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) +
4273
+ kIsolateEmbedderDataOffset;
4274
+ return *reinterpret_cast<void**>(addr);
4275
+ }
4276
+
4277
+ V8_INLINE(static internal::Object** GetRoot(v8::Isolate* isolate,
4278
+ int index)) {
4279
+ uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) + kIsolateRootsOffset;
4280
+ return reinterpret_cast<internal::Object**>(addr + index * kApiPointerSize);
4281
+ }
4282
+
4283
+ template <typename T>
4284
+ V8_INLINE(static T ReadField(Object* ptr, int offset)) {
4285
+ uint8_t* addr = reinterpret_cast<uint8_t*>(ptr) + offset - kHeapObjectTag;
4286
+ return *reinterpret_cast<T*>(addr);
4287
+ }
4288
+
4289
+ template <typename T>
4290
+ V8_INLINE(static T ReadEmbedderData(Context* context, int index)) {
4291
+ typedef internal::Object O;
4292
+ typedef internal::Internals I;
4293
+ O* ctx = *reinterpret_cast<O**>(context);
4294
+ int embedder_data_offset = I::kContextHeaderSize +
4295
+ (internal::kApiPointerSize * I::kContextEmbedderDataIndex);
4296
+ O* embedder_data = I::ReadField<O*>(ctx, embedder_data_offset);
4297
+ int value_offset =
4298
+ I::kFixedArrayHeaderSize + (internal::kApiPointerSize * index);
4299
+ return I::ReadField<T>(embedder_data, value_offset);
4300
+ }
4301
+
4302
+ V8_INLINE(static bool CanCastToHeapObject(void* o)) { return false; }
4303
+ V8_INLINE(static bool CanCastToHeapObject(Context* o)) { return true; }
4304
+ V8_INLINE(static bool CanCastToHeapObject(String* o)) { return true; }
4305
+ V8_INLINE(static bool CanCastToHeapObject(Object* o)) { return true; }
4306
+ V8_INLINE(static bool CanCastToHeapObject(Message* o)) { return true; }
4307
+ V8_INLINE(static bool CanCastToHeapObject(StackTrace* o)) { return true; }
4308
+ V8_INLINE(static bool CanCastToHeapObject(StackFrame* o)) { return true; }
4309
+ };
4310
+
4311
+ } // namespace internal
4312
+
4313
+
4314
+ template <class T>
4315
+ Local<T>::Local() : Handle<T>() { }
4316
+
4317
+
4318
+ template <class T>
4319
+ Local<T> Local<T>::New(Handle<T> that) {
4320
+ if (that.IsEmpty()) return Local<T>();
4321
+ T* that_ptr = *that;
4322
+ internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
4323
+ if (internal::Internals::CanCastToHeapObject(that_ptr)) {
4324
+ return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
4325
+ reinterpret_cast<internal::HeapObject*>(*p))));
4326
+ }
4327
+ return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p)));
4328
+ }
4329
+
4330
+
4331
+ template <class T>
4332
+ Local<T> Local<T>::New(Isolate* isolate, Handle<T> that) {
4333
+ if (that.IsEmpty()) return Local<T>();
4334
+ T* that_ptr = *that;
4335
+ internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
4336
+ return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
4337
+ reinterpret_cast<internal::Isolate*>(isolate), *p)));
4338
+ }
4339
+
4340
+
4341
+ template <class T>
4342
+ Persistent<T> Persistent<T>::New(Handle<T> that) {
4343
+ return New(Isolate::GetCurrent(), that);
4344
+ }
4345
+
4346
+
4347
+ template <class T>
4348
+ Persistent<T> Persistent<T>::New(Isolate* isolate, Handle<T> that) {
4349
+ if (that.IsEmpty()) return Persistent<T>();
4350
+ internal::Object** p = reinterpret_cast<internal::Object**>(*that);
4351
+ return Persistent<T>(reinterpret_cast<T*>(
4352
+ V8::GlobalizeReference(reinterpret_cast<internal::Isolate*>(isolate),
4353
+ p)));
4354
+ }
4355
+
4356
+
4357
+ template <class T>
4358
+ bool Persistent<T>::IsIndependent() const {
4359
+ return IsIndependent(Isolate::GetCurrent());
4360
+ }
4361
+
4362
+
4363
+ template <class T>
4364
+ bool Persistent<T>::IsIndependent(Isolate* isolate) const {
4365
+ typedef internal::Internals I;
4366
+ if (this->IsEmpty()) return false;
4367
+ if (!I::IsInitialized(isolate)) return false;
4368
+ return I::GetNodeFlag(reinterpret_cast<internal::Object**>(**this),
4369
+ I::kNodeIsIndependentShift);
4370
+ }
4371
+
4372
+
4373
+ template <class T>
4374
+ bool Persistent<T>::IsNearDeath() const {
4375
+ return IsNearDeath(Isolate::GetCurrent());
4376
+ }
4377
+
4378
+
4379
+ template <class T>
4380
+ bool Persistent<T>::IsNearDeath(Isolate* isolate) const {
4381
+ typedef internal::Internals I;
4382
+ if (this->IsEmpty()) return false;
4383
+ if (!I::IsInitialized(isolate)) return false;
4384
+ return I::GetNodeState(reinterpret_cast<internal::Object**>(**this)) ==
4385
+ I::kNodeStateIsNearDeathValue;
4386
+ }
4387
+
4388
+
4389
+ template <class T>
4390
+ bool Persistent<T>::IsWeak() const {
4391
+ return IsWeak(Isolate::GetCurrent());
4392
+ }
4393
+
4394
+
4395
+ template <class T>
4396
+ bool Persistent<T>::IsWeak(Isolate* isolate) const {
4397
+ typedef internal::Internals I;
4398
+ if (this->IsEmpty()) return false;
4399
+ if (!I::IsInitialized(isolate)) return false;
4400
+ return I::GetNodeState(reinterpret_cast<internal::Object**>(**this)) ==
4401
+ I::kNodeStateIsWeakValue;
4402
+ }
4403
+
4404
+
4405
+ template <class T>
4406
+ void Persistent<T>::Dispose() {
4407
+ Dispose(Isolate::GetCurrent());
4408
+ }
4409
+
4410
+
4411
+ template <class T>
4412
+ void Persistent<T>::Dispose(Isolate* isolate) {
4413
+ if (this->IsEmpty()) return;
4414
+ V8::DisposeGlobal(reinterpret_cast<internal::Isolate*>(isolate),
4415
+ reinterpret_cast<internal::Object**>(**this));
4416
+ }
4417
+
4418
+
4419
+ template <class T>
4420
+ Persistent<T>::Persistent() : Handle<T>() { }
4421
+
4422
+ template <class T>
4423
+ void Persistent<T>::MakeWeak(void* parameters, WeakReferenceCallback callback) {
4424
+ Isolate* isolate = Isolate::GetCurrent();
4425
+ V8::MakeWeak(reinterpret_cast<internal::Isolate*>(isolate),
4426
+ reinterpret_cast<internal::Object**>(**this),
4427
+ parameters,
4428
+ callback,
4429
+ NULL);
4430
+ }
4431
+
4432
+ template <class T>
4433
+ void Persistent<T>::MakeWeak(Isolate* isolate,
4434
+ void* parameters,
4435
+ NearDeathCallback callback) {
4436
+ V8::MakeWeak(reinterpret_cast<internal::Isolate*>(isolate),
4437
+ reinterpret_cast<internal::Object**>(**this),
4438
+ parameters,
4439
+ NULL,
4440
+ callback);
4441
+ }
4442
+
4443
+ template <class T>
4444
+ void Persistent<T>::ClearWeak() {
4445
+ ClearWeak(Isolate::GetCurrent());
4446
+ }
4447
+
4448
+ template <class T>
4449
+ void Persistent<T>::ClearWeak(Isolate* isolate) {
4450
+ V8::ClearWeak(reinterpret_cast<internal::Isolate*>(isolate),
4451
+ reinterpret_cast<internal::Object**>(**this));
4452
+ }
4453
+
4454
+ template <class T>
4455
+ void Persistent<T>::MarkIndependent() {
4456
+ MarkIndependent(Isolate::GetCurrent());
4457
+ }
4458
+
4459
+ template <class T>
4460
+ void Persistent<T>::MarkIndependent(Isolate* isolate) {
4461
+ typedef internal::Internals I;
4462
+ if (this->IsEmpty()) return;
4463
+ if (!I::IsInitialized(isolate)) return;
4464
+ I::UpdateNodeFlag(reinterpret_cast<internal::Object**>(**this),
4465
+ true,
4466
+ I::kNodeIsIndependentShift);
4467
+ }
4468
+
4469
+ template <class T>
4470
+ void Persistent<T>::MarkPartiallyDependent() {
4471
+ MarkPartiallyDependent(Isolate::GetCurrent());
4472
+ }
4473
+
4474
+ template <class T>
4475
+ void Persistent<T>::MarkPartiallyDependent(Isolate* isolate) {
4476
+ typedef internal::Internals I;
4477
+ if (this->IsEmpty()) return;
4478
+ if (!I::IsInitialized(isolate)) return;
4479
+ I::UpdateNodeFlag(reinterpret_cast<internal::Object**>(**this),
4480
+ true,
4481
+ I::kNodeIsPartiallyDependentShift);
4482
+ }
4483
+
4484
+ template <class T>
4485
+ void Persistent<T>::SetWrapperClassId(uint16_t class_id) {
4486
+ SetWrapperClassId(Isolate::GetCurrent(), class_id);
4487
+ }
4488
+
4489
+ template <class T>
4490
+ void Persistent<T>::SetWrapperClassId(Isolate* isolate, uint16_t class_id) {
4491
+ typedef internal::Internals I;
4492
+ if (this->IsEmpty()) return;
4493
+ if (!I::IsInitialized(isolate)) return;
4494
+ internal::Object** obj = reinterpret_cast<internal::Object**>(**this);
4495
+ uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + I::kNodeClassIdOffset;
4496
+ *reinterpret_cast<uint16_t*>(addr) = class_id;
4497
+ }
4498
+
4499
+ template <class T>
4500
+ uint16_t Persistent<T>::WrapperClassId() const {
4501
+ return WrapperClassId(Isolate::GetCurrent());
4502
+ }
4503
+
4504
+ template <class T>
4505
+ uint16_t Persistent<T>::WrapperClassId(Isolate* isolate) const {
4506
+ typedef internal::Internals I;
4507
+ if (this->IsEmpty()) return 0;
4508
+ if (!I::IsInitialized(isolate)) return 0;
4509
+ internal::Object** obj = reinterpret_cast<internal::Object**>(**this);
4510
+ uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + I::kNodeClassIdOffset;
4511
+ return *reinterpret_cast<uint16_t*>(addr);
4512
+ }
4513
+
4514
+ Arguments::Arguments(internal::Object** implicit_args,
4515
+ internal::Object** values, int length,
4516
+ bool is_construct_call)
4517
+ : implicit_args_(implicit_args),
4518
+ values_(values),
4519
+ length_(length),
4520
+ is_construct_call_(is_construct_call) { }
4521
+
4522
+
4523
+ Local<Value> Arguments::operator[](int i) const {
4524
+ if (i < 0 || length_ <= i) return Local<Value>(*Undefined());
4525
+ return Local<Value>(reinterpret_cast<Value*>(values_ - i));
4526
+ }
4527
+
4528
+
4529
+ Local<Function> Arguments::Callee() const {
4530
+ return Local<Function>(reinterpret_cast<Function*>(
4531
+ &implicit_args_[kCalleeIndex]));
4532
+ }
4533
+
4534
+
4535
+ Local<Object> Arguments::This() const {
4536
+ return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
4537
+ }
4538
+
4539
+
4540
+ Local<Object> Arguments::Holder() const {
4541
+ return Local<Object>(reinterpret_cast<Object*>(
4542
+ &implicit_args_[kHolderIndex]));
4543
+ }
4544
+
4545
+
4546
+ Local<Value> Arguments::Data() const {
4547
+ return Local<Value>(reinterpret_cast<Value*>(&implicit_args_[kDataIndex]));
4548
+ }
4549
+
4550
+
4551
+ Isolate* Arguments::GetIsolate() const {
4552
+ return *reinterpret_cast<Isolate**>(&implicit_args_[kIsolateIndex]);
4553
+ }
4554
+
4555
+
4556
+ bool Arguments::IsConstructCall() const {
4557
+ return is_construct_call_;
4558
+ }
4559
+
4560
+
4561
+ int Arguments::Length() const {
4562
+ return length_;
4563
+ }
4564
+
4565
+
4566
+ template <class T>
4567
+ Local<T> HandleScope::Close(Handle<T> value) {
4568
+ internal::Object** before = reinterpret_cast<internal::Object**>(*value);
4569
+ internal::Object** after = RawClose(before);
4570
+ return Local<T>(reinterpret_cast<T*>(after));
4571
+ }
4572
+
4573
+ Handle<Value> ScriptOrigin::ResourceName() const {
4574
+ return resource_name_;
4575
+ }
4576
+
4577
+
4578
+ Handle<Integer> ScriptOrigin::ResourceLineOffset() const {
4579
+ return resource_line_offset_;
4580
+ }
4581
+
4582
+
4583
+ Handle<Integer> ScriptOrigin::ResourceColumnOffset() const {
4584
+ return resource_column_offset_;
4585
+ }
4586
+
4587
+
4588
+ Handle<Boolean> Boolean::New(bool value) {
4589
+ return value ? True() : False();
4590
+ }
4591
+
4592
+
4593
+ void Template::Set(const char* name, v8::Handle<Data> value) {
4594
+ Set(v8::String::New(name), value);
4595
+ }
4596
+
4597
+
4598
+ Local<Value> Object::GetInternalField(int index) {
4599
+ #ifndef V8_ENABLE_CHECKS
4600
+ typedef internal::Object O;
4601
+ typedef internal::Internals I;
4602
+ O* obj = *reinterpret_cast<O**>(this);
4603
+ // Fast path: If the object is a plain JSObject, which is the common case, we
4604
+ // know where to find the internal fields and can return the value directly.
4605
+ if (I::GetInstanceType(obj) == I::kJSObjectType) {
4606
+ int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
4607
+ O* value = I::ReadField<O*>(obj, offset);
4608
+ O** result = HandleScope::CreateHandle(value);
4609
+ return Local<Value>(reinterpret_cast<Value*>(result));
4610
+ }
4611
+ #endif
4612
+ return SlowGetInternalField(index);
4613
+ }
4614
+
4615
+
4616
+ void* Object::GetAlignedPointerFromInternalField(int index) {
4617
+ #ifndef V8_ENABLE_CHECKS
4618
+ typedef internal::Object O;
4619
+ typedef internal::Internals I;
4620
+ O* obj = *reinterpret_cast<O**>(this);
4621
+ // Fast path: If the object is a plain JSObject, which is the common case, we
4622
+ // know where to find the internal fields and can return the value directly.
4623
+ if (I::GetInstanceType(obj) == I::kJSObjectType) {
4624
+ int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
4625
+ return I::ReadField<void*>(obj, offset);
4626
+ }
4627
+ #endif
4628
+ return SlowGetAlignedPointerFromInternalField(index);
4629
+ }
4630
+
4631
+
4632
+ String* String::Cast(v8::Value* value) {
4633
+ #ifdef V8_ENABLE_CHECKS
4634
+ CheckCast(value);
4635
+ #endif
4636
+ return static_cast<String*>(value);
4637
+ }
4638
+
4639
+
4640
+ Local<String> String::Empty(Isolate* isolate) {
4641
+ typedef internal::Object* S;
4642
+ typedef internal::Internals I;
4643
+ if (!I::IsInitialized(isolate)) return Empty();
4644
+ S* slot = I::GetRoot(isolate, I::kEmptySymbolRootIndex);
4645
+ return Local<String>(reinterpret_cast<String*>(slot));
4646
+ }
4647
+
4648
+
4649
+ String::ExternalStringResource* String::GetExternalStringResource() const {
4650
+ typedef internal::Object O;
4651
+ typedef internal::Internals I;
4652
+ O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
4653
+ String::ExternalStringResource* result;
4654
+ if (I::IsExternalTwoByteString(I::GetInstanceType(obj))) {
4655
+ void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
4656
+ result = reinterpret_cast<String::ExternalStringResource*>(value);
4657
+ } else {
4658
+ result = NULL;
4659
+ }
4660
+ #ifdef V8_ENABLE_CHECKS
4661
+ VerifyExternalStringResource(result);
4662
+ #endif
4663
+ return result;
4664
+ }
4665
+
4666
+
4667
+ String::ExternalStringResourceBase* String::GetExternalStringResourceBase(
4668
+ String::Encoding* encoding_out) const {
4669
+ typedef internal::Object O;
4670
+ typedef internal::Internals I;
4671
+ O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
4672
+ int type = I::GetInstanceType(obj) & I::kFullStringRepresentationMask;
4673
+ *encoding_out = static_cast<Encoding>(type & I::kStringEncodingMask);
4674
+ ExternalStringResourceBase* resource = NULL;
4675
+ if (type == I::kExternalAsciiRepresentationTag ||
4676
+ type == I::kExternalTwoByteRepresentationTag) {
4677
+ void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
4678
+ resource = static_cast<ExternalStringResourceBase*>(value);
4679
+ }
4680
+ #ifdef V8_ENABLE_CHECKS
4681
+ VerifyExternalStringResourceBase(resource, *encoding_out);
4682
+ #endif
4683
+ return resource;
4684
+ }
4685
+
4686
+
4687
+ bool Value::IsUndefined() const {
4688
+ #ifdef V8_ENABLE_CHECKS
4689
+ return FullIsUndefined();
4690
+ #else
4691
+ return QuickIsUndefined();
4692
+ #endif
4693
+ }
4694
+
4695
+ bool Value::QuickIsUndefined() const {
4696
+ typedef internal::Object O;
4697
+ typedef internal::Internals I;
4698
+ O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
4699
+ if (!I::HasHeapObjectTag(obj)) return false;
4700
+ if (I::GetInstanceType(obj) != I::kOddballType) return false;
4701
+ return (I::GetOddballKind(obj) == I::kUndefinedOddballKind);
4702
+ }
4703
+
4704
+
4705
+ bool Value::IsNull() const {
4706
+ #ifdef V8_ENABLE_CHECKS
4707
+ return FullIsNull();
4708
+ #else
4709
+ return QuickIsNull();
4710
+ #endif
4711
+ }
4712
+
4713
+ bool Value::QuickIsNull() const {
4714
+ typedef internal::Object O;
4715
+ typedef internal::Internals I;
4716
+ O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
4717
+ if (!I::HasHeapObjectTag(obj)) return false;
4718
+ if (I::GetInstanceType(obj) != I::kOddballType) return false;
4719
+ return (I::GetOddballKind(obj) == I::kNullOddballKind);
4720
+ }
4721
+
4722
+
4723
+ bool Value::IsString() const {
4724
+ #ifdef V8_ENABLE_CHECKS
4725
+ return FullIsString();
4726
+ #else
4727
+ return QuickIsString();
4728
+ #endif
4729
+ }
4730
+
4731
+ bool Value::QuickIsString() const {
4732
+ typedef internal::Object O;
4733
+ typedef internal::Internals I;
4734
+ O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
4735
+ if (!I::HasHeapObjectTag(obj)) return false;
4736
+ return (I::GetInstanceType(obj) < I::kFirstNonstringType);
4737
+ }
4738
+
4739
+
4740
+ Number* Number::Cast(v8::Value* value) {
4741
+ #ifdef V8_ENABLE_CHECKS
4742
+ CheckCast(value);
4743
+ #endif
4744
+ return static_cast<Number*>(value);
4745
+ }
4746
+
4747
+
4748
+ Integer* Integer::Cast(v8::Value* value) {
4749
+ #ifdef V8_ENABLE_CHECKS
4750
+ CheckCast(value);
4751
+ #endif
4752
+ return static_cast<Integer*>(value);
4753
+ }
4754
+
4755
+
4756
+ Date* Date::Cast(v8::Value* value) {
4757
+ #ifdef V8_ENABLE_CHECKS
4758
+ CheckCast(value);
4759
+ #endif
4760
+ return static_cast<Date*>(value);
4761
+ }
4762
+
4763
+
4764
+ StringObject* StringObject::Cast(v8::Value* value) {
4765
+ #ifdef V8_ENABLE_CHECKS
4766
+ CheckCast(value);
4767
+ #endif
4768
+ return static_cast<StringObject*>(value);
4769
+ }
4770
+
4771
+
4772
+ NumberObject* NumberObject::Cast(v8::Value* value) {
4773
+ #ifdef V8_ENABLE_CHECKS
4774
+ CheckCast(value);
4775
+ #endif
4776
+ return static_cast<NumberObject*>(value);
4777
+ }
4778
+
4779
+
4780
+ BooleanObject* BooleanObject::Cast(v8::Value* value) {
4781
+ #ifdef V8_ENABLE_CHECKS
4782
+ CheckCast(value);
4783
+ #endif
4784
+ return static_cast<BooleanObject*>(value);
4785
+ }
4786
+
4787
+
4788
+ RegExp* RegExp::Cast(v8::Value* value) {
4789
+ #ifdef V8_ENABLE_CHECKS
4790
+ CheckCast(value);
4791
+ #endif
4792
+ return static_cast<RegExp*>(value);
4793
+ }
4794
+
4795
+
4796
+ Object* Object::Cast(v8::Value* value) {
4797
+ #ifdef V8_ENABLE_CHECKS
4798
+ CheckCast(value);
4799
+ #endif
4800
+ return static_cast<Object*>(value);
4801
+ }
4802
+
4803
+
4804
+ Array* Array::Cast(v8::Value* value) {
4805
+ #ifdef V8_ENABLE_CHECKS
4806
+ CheckCast(value);
4807
+ #endif
4808
+ return static_cast<Array*>(value);
4809
+ }
4810
+
4811
+
4812
+ Function* Function::Cast(v8::Value* value) {
4813
+ #ifdef V8_ENABLE_CHECKS
4814
+ CheckCast(value);
4815
+ #endif
4816
+ return static_cast<Function*>(value);
4817
+ }
4818
+
4819
+
4820
+ External* External::Cast(v8::Value* value) {
4821
+ #ifdef V8_ENABLE_CHECKS
4822
+ CheckCast(value);
4823
+ #endif
4824
+ return static_cast<External*>(value);
4825
+ }
4826
+
4827
+
4828
+ Isolate* AccessorInfo::GetIsolate() const {
4829
+ return *reinterpret_cast<Isolate**>(&args_[-3]);
4830
+ }
4831
+
4832
+
4833
+ Local<Value> AccessorInfo::Data() const {
4834
+ return Local<Value>(reinterpret_cast<Value*>(&args_[-2]));
4835
+ }
4836
+
4837
+
4838
+ Local<Object> AccessorInfo::This() const {
4839
+ return Local<Object>(reinterpret_cast<Object*>(&args_[0]));
4840
+ }
4841
+
4842
+
4843
+ Local<Object> AccessorInfo::Holder() const {
4844
+ return Local<Object>(reinterpret_cast<Object*>(&args_[-1]));
4845
+ }
4846
+
4847
+
4848
+ Handle<Primitive> Undefined(Isolate* isolate) {
4849
+ typedef internal::Object* S;
4850
+ typedef internal::Internals I;
4851
+ if (!I::IsInitialized(isolate)) return Undefined();
4852
+ S* slot = I::GetRoot(isolate, I::kUndefinedValueRootIndex);
4853
+ return Handle<Primitive>(reinterpret_cast<Primitive*>(slot));
4854
+ }
4855
+
4856
+
4857
+ Handle<Primitive> Null(Isolate* isolate) {
4858
+ typedef internal::Object* S;
4859
+ typedef internal::Internals I;
4860
+ if (!I::IsInitialized(isolate)) return Null();
4861
+ S* slot = I::GetRoot(isolate, I::kNullValueRootIndex);
4862
+ return Handle<Primitive>(reinterpret_cast<Primitive*>(slot));
4863
+ }
4864
+
4865
+
4866
+ Handle<Boolean> True(Isolate* isolate) {
4867
+ typedef internal::Object* S;
4868
+ typedef internal::Internals I;
4869
+ if (!I::IsInitialized(isolate)) return True();
4870
+ S* slot = I::GetRoot(isolate, I::kTrueValueRootIndex);
4871
+ return Handle<Boolean>(reinterpret_cast<Boolean*>(slot));
4872
+ }
4873
+
4874
+
4875
+ Handle<Boolean> False(Isolate* isolate) {
4876
+ typedef internal::Object* S;
4877
+ typedef internal::Internals I;
4878
+ if (!I::IsInitialized(isolate)) return False();
4879
+ S* slot = I::GetRoot(isolate, I::kFalseValueRootIndex);
4880
+ return Handle<Boolean>(reinterpret_cast<Boolean*>(slot));
4881
+ }
4882
+
4883
+
4884
+ void Isolate::SetData(void* data) {
4885
+ typedef internal::Internals I;
4886
+ I::SetEmbedderData(this, data);
4887
+ }
4888
+
4889
+
4890
+ void* Isolate::GetData() {
4891
+ typedef internal::Internals I;
4892
+ return I::GetEmbedderData(this);
4893
+ }
4894
+
4895
+
4896
+ Local<Value> Context::GetEmbedderData(int index) {
4897
+ #ifndef V8_ENABLE_CHECKS
4898
+ typedef internal::Object O;
4899
+ typedef internal::Internals I;
4900
+ O** result = HandleScope::CreateHandle(I::ReadEmbedderData<O*>(this, index));
4901
+ return Local<Value>(reinterpret_cast<Value*>(result));
4902
+ #else
4903
+ return SlowGetEmbedderData(index);
4904
+ #endif
4905
+ }
4906
+
4907
+
4908
+ void* Context::GetAlignedPointerFromEmbedderData(int index) {
4909
+ #ifndef V8_ENABLE_CHECKS
4910
+ typedef internal::Internals I;
4911
+ return I::ReadEmbedderData<void*>(this, index);
4912
+ #else
4913
+ return SlowGetAlignedPointerFromEmbedderData(index);
4914
+ #endif
4915
+ }
4916
+
4917
+
4918
+ /**
4919
+ * \example shell.cc
4920
+ * A simple shell that takes a list of expressions on the
4921
+ * command-line and executes them.
4922
+ */
4923
+
4924
+
4925
+ /**
4926
+ * \example process.cc
4927
+ */
4928
+
4929
+
4930
+ } // namespace v8
4931
+
4932
+
4933
+ #undef V8EXPORT
4934
+ #undef TYPE_CHECK
4935
+
4936
+
4937
+ #endif // V8_H_