blob: a048acdb57740b77f521d8f6b3176cfc8182543e [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2007-2008 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// 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
v8.team.kasperl727e9952008-09-02 14:56:44 +000028/** \mainpage V8 API Reference Guide
ager@chromium.org9258b6b2008-09-11 09:11:10 +000029 *
30 * V8 is Google's open source JavaScript engine.
v8.team.kasperl727e9952008-09-02 14:56:44 +000031 *
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/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000036 */
v8.team.kasperl727e9952008-09-02 14:56:44 +000037
ager@chromium.org9258b6b2008-09-11 09:11:10 +000038#ifndef V8_H_
39#define V8_H_
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000040
41#include <stdio.h>
42
43#ifdef _WIN32
44typedef int int32_t;
45typedef unsigned int uint32_t;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000046typedef unsigned short uint16_t; // NOLINT
47typedef long long int64_t; // NOLINT
48
49// Setup for Windows DLL export/import. When building the V8 DLL the
50// BUILDING_V8_SHARED needs to be defined. When building a program which uses
51// the V8 DLL USING_V8_SHARED needs to be defined. When either building the V8
52// static library or building a program which uses the V8 static library neither
53// BUILDING_V8_SHARED nor USING_V8_SHARED should be defined.
54// The reason for having both EXPORT and EXPORT_INLINE is that classes which
55// have their code inside this header file needs to have __declspec(dllexport)
56// when building the DLL but cannot have __declspec(dllimport) when building
57// a program which uses the DLL.
58#if defined(BUILDING_V8_SHARED) && defined(USING_V8_SHARED)
59#error both BUILDING_V8_SHARED and USING_V8_SHARED are set - please check the\
60 build configuration to ensure that at most one of these is set
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000061#endif
62
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000063#ifdef BUILDING_V8_SHARED
64#define EXPORT __declspec(dllexport)
65#define EXPORT_INLINE __declspec(dllexport)
66#elif USING_V8_SHARED
67#define EXPORT __declspec(dllimport)
68#define EXPORT_INLINE
69#else
70#define EXPORT
71#define EXPORT_INLINE
72#endif // BUILDING_V8_SHARED
73
74#else // _WIN32
75
76#include <stdint.h>
77
78// Setup for Linux shared library export. There is no need to destinguish
79// neither between building or using the V8 shared library nor between using
80// the shared or static V8 library as there is on Windows. Therefore there is
ager@chromium.org7c537e22008-10-16 08:43:32 +000081// no checking of BUILDING_V8_SHARED and USING_V8_SHARED.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000082#if defined(__GNUC__) && (__GNUC__ >= 4)
83#define EXPORT __attribute__ ((visibility("default")))
84#define EXPORT_INLINE __attribute__ ((visibility("default")))
85#else // defined(__GNUC__) && (__GNUC__ >= 4)
86#define EXPORT
87#define EXPORT_INLINE
88#endif // defined(__GNUC__) && (__GNUC__ >= 4)
89
90#endif // _WIN32
91
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000092/**
v8.team.kasperl727e9952008-09-02 14:56:44 +000093 * The v8 JavaScript engine.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000094 */
95namespace v8 {
96
97class Context;
98class String;
99class Value;
100class Utils;
101class Number;
102class Object;
103class Array;
104class Int32;
105class Uint32;
106class External;
107class Primitive;
108class Boolean;
109class Integer;
110class Function;
111class Date;
112class ImplementationUtilities;
113class Signature;
114template <class T> class Handle;
115template <class T> class Local;
116template <class T> class Persistent;
117class FunctionTemplate;
118class ObjectTemplate;
119class Data;
120
121
122// --- W e a k H a n d l e s
123
124
125/**
126 * A weak reference callback function.
127 *
128 * \param object the weak global object to be reclaimed by the garbage collector
129 * \param parameter the value passed in when making the weak global object
130 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000131typedef void (*WeakReferenceCallback)(Persistent<Value> object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000132 void* parameter);
133
134
135// --- H a n d l e s ---
136
137#define TYPE_CHECK(T, S) \
138 while (false) { \
139 *(static_cast<T**>(0)) = static_cast<S*>(0); \
140 }
141
142/**
143 * An object reference managed by the v8 garbage collector.
144 *
145 * All objects returned from v8 have to be tracked by the garbage
146 * collector so that it knows that the objects are still alive. Also,
147 * because the garbage collector may move objects, it is unsafe to
148 * point directly to an object. Instead, all objects are stored in
149 * handles which are known by the garbage collector and updated
150 * whenever an object moves. Handles should always be passed by value
151 * (except in cases like out-parameters) and they should never be
152 * allocated on the heap.
153 *
154 * There are two types of handles: local and persistent handles.
155 * Local handles are light-weight and transient and typically used in
156 * local operations. They are managed by HandleScopes. Persistent
157 * handles can be used when storing objects across several independent
158 * operations and have to be explicitly deallocated when they're no
159 * longer used.
160 *
161 * It is safe to extract the object stored in the handle by
162 * dereferencing the handle (for instance, to extract the Object* from
163 * an Handle<Object>); the value will still be governed by a handle
164 * behind the scenes and the same rules apply to these values as to
165 * their handles.
166 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000167template <class T> class EXPORT_INLINE Handle {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000168 public:
169
170 /**
171 * Creates an empty handle.
172 */
173 Handle();
174
175 /**
176 * Creates a new handle for the specified value.
177 */
178 explicit Handle(T* val) : val_(val) { }
179
180 /**
181 * Creates a handle for the contents of the specified handle. This
182 * constructor allows you to pass handles as arguments by value and
v8.team.kasperl727e9952008-09-02 14:56:44 +0000183 * to assign between handles. However, if you try to assign between
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000184 * incompatible handles, for instance from a Handle<String> to a
185 * Handle<Number> it will cause a compiletime error. Assigning
186 * between compatible handles, for instance assigning a
187 * Handle<String> to a variable declared as Handle<Value>, is legal
188 * because String is a subclass of Value.
189 */
190 template <class S> inline Handle(Handle<S> that)
191 : val_(reinterpret_cast<T*>(*that)) {
192 /**
193 * This check fails when trying to convert between incompatible
194 * handles. For example, converting from a Handle<String> to a
195 * Handle<Number>.
196 */
197 TYPE_CHECK(T, S);
198 }
199
200 /**
201 * Returns true if the handle is empty.
202 */
ager@chromium.org32912102009-01-16 10:38:43 +0000203 bool IsEmpty() const { return val_ == 0; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000204
ager@chromium.org32912102009-01-16 10:38:43 +0000205 T* operator->() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000206
ager@chromium.org32912102009-01-16 10:38:43 +0000207 T* operator*() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000208
209 /**
210 * Sets the handle to be empty. IsEmpty() will then return true.
211 */
212 void Clear() { this->val_ = 0; }
213
214 /**
215 * Checks whether two handles are the same.
216 * Returns true if both are empty, or if the objects
217 * to which they refer are identical.
218 * The handles' references are not checked.
219 */
ager@chromium.org32912102009-01-16 10:38:43 +0000220 template <class S> bool operator==(Handle<S> that) const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000221 void** a = reinterpret_cast<void**>(**this);
222 void** b = reinterpret_cast<void**>(*that);
223 if (a == 0) return b == 0;
224 if (b == 0) return false;
225 return *a == *b;
226 }
227
228 /**
229 * Checks whether two handles are different.
230 * Returns true if only one of the handles is empty, or if
231 * the objects to which they refer are different.
232 * The handles' references are not checked.
233 */
ager@chromium.org32912102009-01-16 10:38:43 +0000234 template <class S> bool operator!=(Handle<S> that) const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000235 return !operator==(that);
236 }
237
238 template <class S> static inline Handle<T> Cast(Handle<S> that) {
239 if (that.IsEmpty()) return Handle<T>();
240 return Handle<T>(T::Cast(*that));
241 }
242
243 private:
244 T* val_;
245};
246
247
248/**
249 * A light-weight stack-allocated object handle. All operations
250 * that return objects from within v8 return them in local handles. They
251 * are created within HandleScopes, and all local handles allocated within a
252 * handle scope are destroyed when the handle scope is destroyed. Hence it
253 * is not necessary to explicitly deallocate local handles.
254 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000255template <class T> class EXPORT_INLINE Local : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000256 public:
257 Local();
258 template <class S> inline Local(Local<S> that)
259 : Handle<T>(reinterpret_cast<T*>(*that)) {
260 /**
261 * This check fails when trying to convert between incompatible
262 * handles. For example, converting from a Handle<String> to a
263 * Handle<Number>.
264 */
265 TYPE_CHECK(T, S);
266 }
267 template <class S> inline Local(S* that) : Handle<T>(that) { }
268 template <class S> static inline Local<T> Cast(Local<S> that) {
269 if (that.IsEmpty()) return Local<T>();
270 return Local<T>(T::Cast(*that));
271 }
272
273 /** Create a local handle for the content of another handle.
274 * The referee is kept alive by the local handle even when
275 * the original handle is destroyed/disposed.
276 */
277 static Local<T> New(Handle<T> that);
278};
279
280
281/**
282 * An object reference that is independent of any handle scope. Where
v8.team.kasperl727e9952008-09-02 14:56:44 +0000283 * a Local handle only lives as long as the HandleScope in which it was
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000284 * allocated, a Persistent handle remains valid until it is explicitly
285 * disposed.
286 *
287 * A persistent handle contains a reference to a storage cell within
288 * the v8 engine which holds an object value and which is updated by
289 * the garbage collector whenever the object is moved. A new storage
290 * cell can be created using Persistent::New and existing handles can
291 * be disposed using Persistent::Dispose. Since persistent handles
292 * are passed by value you may have many persistent handle objects
293 * that point to the same storage cell. For instance, if you pass a
294 * persistent handle as an argument to a function you will not get two
295 * different storage cells but rather two references to the same
296 * storage cell.
297 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000298template <class T> class EXPORT_INLINE Persistent : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000299 public:
300
301 /**
302 * Creates an empty persistent handle that doesn't point to any
303 * storage cell.
304 */
305 Persistent();
306
307 /**
308 * Creates a persistent handle for the same storage cell as the
309 * specified handle. This constructor allows you to pass persistent
310 * handles as arguments by value and to assign between persistent
v8.team.kasperl727e9952008-09-02 14:56:44 +0000311 * handles. However, attempting to assign between incompatible
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000312 * persistent handles, for instance from a Persistent<String> to a
v8.team.kasperl727e9952008-09-02 14:56:44 +0000313 * Persistent<Number> will cause a compiletime error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000314 * between compatible persistent handles, for instance assigning a
315 * Persistent<String> to a variable declared as Persistent<Value>,
v8.team.kasperl727e9952008-09-02 14:56:44 +0000316 * is allowed as String is a subclass of Value.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000317 */
318 template <class S> inline Persistent(Persistent<S> that)
319 : Handle<T>(reinterpret_cast<T*>(*that)) {
320 /**
321 * This check fails when trying to convert between incompatible
322 * handles. For example, converting from a Handle<String> to a
323 * Handle<Number>.
324 */
325 TYPE_CHECK(T, S);
326 }
327
328 template <class S> inline Persistent(S* that) : Handle<T>(that) { }
329
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000330 /**
331 * "Casts" a plain handle which is known to be a persistent handle
332 * to a persistent handle.
333 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000334 template <class S> explicit inline Persistent(Handle<S> that)
335 : Handle<T>(*that) { }
336
337 template <class S> static inline Persistent<T> Cast(Persistent<S> that) {
338 if (that.IsEmpty()) return Persistent<T>();
339 return Persistent<T>(T::Cast(*that));
340 }
341
342 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000343 * Creates a new persistent handle for an existing local or
344 * persistent handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000345 */
346 static Persistent<T> New(Handle<T> that);
347
348 /**
349 * Releases the storage cell referenced by this persistent handle.
350 * Does not remove the reference to the cell from any handles.
351 * This handle's reference, and any any other references to the storage
352 * cell remain and IsEmpty will still return false.
353 */
354 void Dispose();
355
356 /**
357 * Make the reference to this object weak. When only weak handles
358 * refer to the object, the garbage collector will perform a
359 * callback to the given V8::WeakReferenceCallback function, passing
360 * it the object reference and the given parameters.
361 */
362 void MakeWeak(void* parameters, WeakReferenceCallback callback);
363
364 /** Clears the weak reference to this object.*/
365 void ClearWeak();
366
367 /**
368 *Checks if the handle holds the only reference to an object.
369 */
ager@chromium.org32912102009-01-16 10:38:43 +0000370 bool IsNearDeath() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000371
372 /**
373 * Returns true if the handle's reference is weak.
374 */
ager@chromium.org32912102009-01-16 10:38:43 +0000375 bool IsWeak() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000376
377 private:
378 friend class ImplementationUtilities;
379 friend class ObjectTemplate;
380};
381
382
v8.team.kasperl727e9952008-09-02 14:56:44 +0000383 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000384 * A stack-allocated class that governs a number of local handles.
385 * After a handle scope has been created, all local handles will be
386 * allocated within that handle scope until either the handle scope is
387 * deleted or another handle scope is created. If there is already a
388 * handle scope and a new one is created, all allocations will take
v8.team.kasperl727e9952008-09-02 14:56:44 +0000389 * place in the new handle scope until it is deleted. After that,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000390 * new handles will again be allocated in the original handle scope.
391 *
392 * After the handle scope of a local handle has been deleted the
393 * garbage collector will no longer track the object stored in the
394 * handle and may deallocate it. The behavior of accessing a handle
395 * for which the handle scope has been deleted is undefined.
396 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000397class EXPORT HandleScope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000398 public:
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000399 HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000400
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000401 ~HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000402
403 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000404 * Closes the handle scope and returns the value as a handle in the
405 * previous scope, which is the new current scope after the call.
406 */
407 template <class T> Local<T> Close(Handle<T> value);
408
409 /**
410 * Counts the number of allocated handles.
411 */
412 static int NumberOfHandles();
413
414 /**
415 * Creates a new handle with the given value.
416 */
417 static void** CreateHandle(void* value);
418
419 private:
420 // Make it impossible to create heap-allocated or illegal handle
421 // scopes by disallowing certain operations.
422 HandleScope(const HandleScope&);
423 void operator=(const HandleScope&);
424 void* operator new(size_t size);
425 void operator delete(void*, size_t);
426
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000427 // This Data class is accessible internally through a typedef in the
428 // ImplementationUtilities class.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000429 class EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000430 public:
431 int extensions;
432 void** next;
433 void** limit;
434 inline void Initialize() {
435 extensions = -1;
436 next = limit = NULL;
437 }
438 };
439
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000440 Data previous_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000441
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000442 // Allow for the active closing of HandleScopes which allows to pass a handle
443 // from the HandleScope being closed to the next top most HandleScope.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000444 bool is_closed_;
445 void** RawClose(void** value);
446
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000447 friend class ImplementationUtilities;
448};
449
450
451// --- S p e c i a l o b j e c t s ---
452
453
454/**
455 * The superclass of values and API object templates.
456 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000457class EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000458 private:
459 Data();
460};
461
462
463/**
464 * Pre-compilation data that can be associated with a script. This
465 * data can be calculated for a script in advance of actually
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000466 * compiling it, and can be stored between compilations. When script
v8.team.kasperl727e9952008-09-02 14:56:44 +0000467 * data is given to the compile method compilation will be faster.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000468 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000469class EXPORT ScriptData { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000470 public:
471 virtual ~ScriptData() { }
472 static ScriptData* PreCompile(const char* input, int length);
473 static ScriptData* New(unsigned* data, int length);
474
475 virtual int Length() = 0;
476 virtual unsigned* Data() = 0;
477};
478
479
480/**
481 * The origin, within a file, of a script.
482 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000483class EXPORT ScriptOrigin {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000484 public:
mads.s.agercbaa0602008-08-14 13:41:48 +0000485 ScriptOrigin(Handle<Value> resource_name,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000486 Handle<Integer> resource_line_offset = Handle<Integer>(),
487 Handle<Integer> resource_column_offset = Handle<Integer>())
488 : resource_name_(resource_name),
489 resource_line_offset_(resource_line_offset),
490 resource_column_offset_(resource_column_offset) { }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000491 inline Handle<Value> ResourceName() const;
492 inline Handle<Integer> ResourceLineOffset() const;
493 inline Handle<Integer> ResourceColumnOffset() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000494 private:
mads.s.agercbaa0602008-08-14 13:41:48 +0000495 Handle<Value> resource_name_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000496 Handle<Integer> resource_line_offset_;
497 Handle<Integer> resource_column_offset_;
498};
499
500
501/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000502 * A compiled JavaScript script.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000503 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000504class EXPORT Script {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000505 public:
506
507 /**
508 * Compiles the specified script. The ScriptOrigin* and ScriptData*
509 * parameters are owned by the caller of Script::Compile. No
510 * references to these objects are kept after compilation finishes.
511 */
512 static Local<Script> Compile(Handle<String> source,
513 ScriptOrigin* origin = NULL,
514 ScriptData* pre_data = NULL);
515
mads.s.agercbaa0602008-08-14 13:41:48 +0000516 /**
517 * Compiles the specified script using the specified file name
518 * object (typically a string) as the script's origin.
519 */
520 static Local<Script> Compile(Handle<String> source,
521 Handle<Value> file_name);
522
v8.team.kasperl727e9952008-09-02 14:56:44 +0000523 /**
524 * Runs the script returning the resulting value.
525 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000526 Local<Value> Run();
527};
528
529
530/**
531 * An error message.
532 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000533class EXPORT Message {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000534 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000535 Local<String> Get() const;
536 Local<String> GetSourceLine() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000537
ager@chromium.org32912102009-01-16 10:38:43 +0000538 Handle<Value> GetScriptResourceName() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000539
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000540 /**
541 * Returns the number, 1-based, of the line where the error occurred.
542 */
ager@chromium.org32912102009-01-16 10:38:43 +0000543 int GetLineNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000544
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000545 /**
546 * Returns the index within the script of the first character where
547 * the error occurred.
548 */
ager@chromium.org32912102009-01-16 10:38:43 +0000549 int GetStartPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000550
551 /**
552 * Returns the index within the script of the last character where
553 * the error occurred.
554 */
ager@chromium.org32912102009-01-16 10:38:43 +0000555 int GetEndPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000556
557 /**
558 * Returns the index within the line of the first character where
559 * the error occurred.
560 */
ager@chromium.org32912102009-01-16 10:38:43 +0000561 int GetStartColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000562
563 /**
564 * Returns the index within the line of the last character where
565 * the error occurred.
566 */
ager@chromium.org32912102009-01-16 10:38:43 +0000567 int GetEndColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000568
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000569 // TODO(1245381): Print to a string instead of on a FILE.
570 static void PrintCurrentStackTrace(FILE* out);
571};
572
573
574// --- V a l u e ---
575
576
577/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000578 * The superclass of all JavaScript values and objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000579 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000580class EXPORT Value : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000581 public:
582
583 /**
584 * Returns true if this value is the undefined value. See ECMA-262
585 * 4.3.10.
586 */
ager@chromium.org32912102009-01-16 10:38:43 +0000587 bool IsUndefined() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000588
589 /**
590 * Returns true if this value is the null value. See ECMA-262
591 * 4.3.11.
592 */
ager@chromium.org32912102009-01-16 10:38:43 +0000593 bool IsNull() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000594
595 /**
596 * Returns true if this value is true.
597 */
ager@chromium.org32912102009-01-16 10:38:43 +0000598 bool IsTrue() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000599
600 /**
601 * Returns true if this value is false.
602 */
ager@chromium.org32912102009-01-16 10:38:43 +0000603 bool IsFalse() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000604
605 /**
606 * Returns true if this value is an instance of the String type.
607 * See ECMA-262 8.4.
608 */
ager@chromium.org32912102009-01-16 10:38:43 +0000609 bool IsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000610
611 /**
612 * Returns true if this value is a function.
613 */
ager@chromium.org32912102009-01-16 10:38:43 +0000614 bool IsFunction() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000615
616 /**
617 * Returns true if this value is an array.
618 */
ager@chromium.org32912102009-01-16 10:38:43 +0000619 bool IsArray() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000620
v8.team.kasperl727e9952008-09-02 14:56:44 +0000621 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000622 * Returns true if this value is an object.
623 */
ager@chromium.org32912102009-01-16 10:38:43 +0000624 bool IsObject() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000625
v8.team.kasperl727e9952008-09-02 14:56:44 +0000626 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000627 * Returns true if this value is boolean.
628 */
ager@chromium.org32912102009-01-16 10:38:43 +0000629 bool IsBoolean() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000630
v8.team.kasperl727e9952008-09-02 14:56:44 +0000631 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000632 * Returns true if this value is a number.
633 */
ager@chromium.org32912102009-01-16 10:38:43 +0000634 bool IsNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000635
v8.team.kasperl727e9952008-09-02 14:56:44 +0000636 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000637 * Returns true if this value is external.
638 */
ager@chromium.org32912102009-01-16 10:38:43 +0000639 bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000640
v8.team.kasperl727e9952008-09-02 14:56:44 +0000641 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000642 * Returns true if this value is a 32-bit signed integer.
643 */
ager@chromium.org32912102009-01-16 10:38:43 +0000644 bool IsInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000645
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000646 /**
647 * Returns true if this value is a Date.
648 */
ager@chromium.org32912102009-01-16 10:38:43 +0000649 bool IsDate() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000650
ager@chromium.org32912102009-01-16 10:38:43 +0000651 Local<Boolean> ToBoolean() const;
652 Local<Number> ToNumber() const;
653 Local<String> ToString() const;
654 Local<String> ToDetailString() const;
655 Local<Object> ToObject() const;
656 Local<Integer> ToInteger() const;
657 Local<Uint32> ToUint32() const;
658 Local<Int32> ToInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000659
660 /**
661 * Attempts to convert a string to an array index.
662 * Returns an empty handle if the conversion fails.
663 */
ager@chromium.org32912102009-01-16 10:38:43 +0000664 Local<Uint32> ToArrayIndex() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000665
ager@chromium.org32912102009-01-16 10:38:43 +0000666 bool BooleanValue() const;
667 double NumberValue() const;
668 int64_t IntegerValue() const;
669 uint32_t Uint32Value() const;
670 int32_t Int32Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000671
672 /** JS == */
ager@chromium.org32912102009-01-16 10:38:43 +0000673 bool Equals(Handle<Value> that) const;
674 bool StrictEquals(Handle<Value> that) const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000675};
676
677
678/**
679 * The superclass of primitive values. See ECMA-262 4.3.2.
680 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000681class EXPORT Primitive : public Value { };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000682
683
684/**
685 * A primitive boolean value (ECMA-262, 4.3.14). Either the true
686 * or false value.
687 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000688class EXPORT Boolean : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000689 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000690 bool Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000691 static inline Handle<Boolean> New(bool value);
692};
693
694
695/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000696 * A JavaScript string value (ECMA-262, 4.3.17).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000697 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000698class EXPORT String : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000699 public:
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000700
701 /**
702 * Returns the number of characters in this string.
703 */
ager@chromium.org32912102009-01-16 10:38:43 +0000704 int Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000705
v8.team.kasperl727e9952008-09-02 14:56:44 +0000706 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000707 * Returns the number of bytes in the UTF-8 encoded
708 * representation of this string.
709 */
ager@chromium.org32912102009-01-16 10:38:43 +0000710 int Utf8Length() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000711
712 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000713 * Write the contents of the string to an external buffer.
714 * If no arguments are given, expects the buffer to be large
715 * enough to hold the entire string and NULL terminator. Copies
716 * the contents of the string and the NULL terminator into the
717 * buffer.
718 *
719 * Copies up to length characters into the output buffer.
720 * Only null-terminates if there is enough space in the buffer.
721 *
722 * \param buffer The buffer into which the string will be copied.
723 * \param start The starting position within the string at which
724 * copying begins.
725 * \param length The number of bytes to copy from the string.
726 * \return The number of characters copied to the buffer
727 * excluding the NULL terminator.
728 */
ager@chromium.org32912102009-01-16 10:38:43 +0000729 int Write(uint16_t* buffer, int start = 0, int length = -1) const; // UTF-16
730 int WriteAscii(char* buffer, int start = 0, int length = -1) const; // ASCII
731 int WriteUtf8(char* buffer, int length = -1) const; // UTF-8
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000732
v8.team.kasperl727e9952008-09-02 14:56:44 +0000733 /**
734 * Returns true if the string is external
735 */
ager@chromium.org32912102009-01-16 10:38:43 +0000736 bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000737
v8.team.kasperl727e9952008-09-02 14:56:44 +0000738 /**
739 * Returns true if the string is both external and ascii
740 */
ager@chromium.org32912102009-01-16 10:38:43 +0000741 bool IsExternalAscii() const;
v8.team.kasperl727e9952008-09-02 14:56:44 +0000742 /**
743 * An ExternalStringResource is a wrapper around a two-byte string
744 * buffer that resides outside V8's heap. Implement an
745 * ExternalStringResource to manage the life cycle of the underlying
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000746 * buffer. Note that the string data must be immutable.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000747 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000748 class EXPORT ExternalStringResource { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000749 public:
750 /**
751 * Override the destructor to manage the life cycle of the underlying
752 * buffer.
753 */
754 virtual ~ExternalStringResource() {}
755 /** The string data from the underlying buffer.*/
756 virtual const uint16_t* data() const = 0;
757 /** The length of the string. That is, the number of two-byte characters.*/
758 virtual size_t length() const = 0;
759 protected:
760 ExternalStringResource() {}
761 private:
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000762 // Disallow copying and assigning.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000763 ExternalStringResource(const ExternalStringResource&);
764 void operator=(const ExternalStringResource&);
765 };
766
767 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000768 * An ExternalAsciiStringResource is a wrapper around an ascii
769 * string buffer that resides outside V8's heap. Implement an
770 * ExternalAsciiStringResource to manage the life cycle of the
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000771 * underlying buffer. Note that the string data must be immutable
772 * and that the data must be strict 7-bit ASCII, not Latin1 or
773 * UTF-8, which would require special treatment internally in the
774 * engine and, in the case of UTF-8, do not allow efficient indexing.
775 * Use String::New or convert to 16 bit data for non-ASCII.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000776 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000777
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000778 class EXPORT ExternalAsciiStringResource { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000779 public:
780 /**
781 * Override the destructor to manage the life cycle of the underlying
782 * buffer.
783 */
784 virtual ~ExternalAsciiStringResource() {}
785 /** The string data from the underlying buffer.*/
786 virtual const char* data() const = 0;
787 /** The number of ascii characters in the string.*/
788 virtual size_t length() const = 0;
789 protected:
790 ExternalAsciiStringResource() {}
791 private:
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000792 // Disallow copying and assigning.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000793 ExternalAsciiStringResource(const ExternalAsciiStringResource&);
794 void operator=(const ExternalAsciiStringResource&);
795 };
796
797 /**
798 * Get the ExternalStringResource for an external string. Only
799 * valid if IsExternal() returns true.
800 */
ager@chromium.org32912102009-01-16 10:38:43 +0000801 ExternalStringResource* GetExternalStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000802
803 /**
804 * Get the ExternalAsciiStringResource for an external ascii string.
805 * Only valid if IsExternalAscii() returns true.
806 */
ager@chromium.org32912102009-01-16 10:38:43 +0000807 ExternalAsciiStringResource* GetExternalAsciiStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000808
809 static String* Cast(v8::Value* obj);
810
811 /**
812 * Allocates a new string from either utf-8 encoded or ascii data.
813 * The second parameter 'length' gives the buffer length.
814 * If the data is utf-8 encoded, the caller must
815 * be careful to supply the length parameter.
816 * If it is not given, the function calls
817 * 'strlen' to determine the buffer length, it might be
kasper.lund7276f142008-07-30 08:49:36 +0000818 * wrong if 'data' contains a null character.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000819 */
820 static Local<String> New(const char* data, int length = -1);
821
822 /** Allocates a new string from utf16 data.*/
823 static Local<String> New(const uint16_t* data, int length = -1);
824
825 /** Creates a symbol. Returns one if it exists already.*/
826 static Local<String> NewSymbol(const char* data, int length = -1);
827
v8.team.kasperl727e9952008-09-02 14:56:44 +0000828 /**
829 * Creates a new external string using the data defined in the given
830 * resource. The resource is deleted when the external string is no
831 * longer live on V8's heap. The caller of this function should not
832 * delete or modify the resource. Neither should the underlying buffer be
833 * deallocated or modified except through the destructor of the
834 * external string resource.
835 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000836 static Local<String> NewExternal(ExternalStringResource* resource);
ager@chromium.org6f10e412009-02-13 10:11:16 +0000837
838 /**
839 * Associate an external string resource with this string by transforming it
840 * in place so that existing references to this string in the JavaScript heap
841 * will use the external string resource. The external string resource's
842 * character contents needs to be equivalent to this string.
843 * Returns true if the string has been changed to be an external string.
844 * The string is not modified if the operation fails.
845 */
846 bool MakeExternal(ExternalStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000847
v8.team.kasperl727e9952008-09-02 14:56:44 +0000848 /**
849 * Creates a new external string using the ascii data defined in the given
850 * resource. The resource is deleted when the external string is no
851 * longer live on V8's heap. The caller of this function should not
852 * delete or modify the resource. Neither should the underlying buffer be
853 * deallocated or modified except through the destructor of the
854 * external string resource.
855 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000856 static Local<String> NewExternal(ExternalAsciiStringResource* resource);
ager@chromium.org6f10e412009-02-13 10:11:16 +0000857
858 /**
859 * Associate an external string resource with this string by transforming it
860 * in place so that existing references to this string in the JavaScript heap
861 * will use the external string resource. The external string resource's
862 * character contents needs to be equivalent to this string.
863 * Returns true if the string has been changed to be an external string.
864 * The string is not modified if the operation fails.
865 */
866 bool MakeExternal(ExternalAsciiStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000867
kasper.lund7276f142008-07-30 08:49:36 +0000868 /** Creates an undetectable string from the supplied ascii or utf-8 data.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000869 static Local<String> NewUndetectable(const char* data, int length = -1);
870
kasper.lund7276f142008-07-30 08:49:36 +0000871 /** Creates an undetectable string from the supplied utf-16 data.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000872 static Local<String> NewUndetectable(const uint16_t* data, int length = -1);
873
874 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000875 * Converts an object to a utf8-encoded character array. Useful if
876 * you want to print the object.
877 */
878 class EXPORT Utf8Value {
879 public:
880 explicit Utf8Value(Handle<v8::Value> obj);
881 ~Utf8Value();
iposva@chromium.org245aa852009-02-10 00:49:54 +0000882 char* operator*() const { return str_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000883 int length() { return length_; }
884 private:
885 char* str_;
886 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000887
888 // Disallow copying and assigning.
889 Utf8Value(const Utf8Value&);
890 void operator=(const Utf8Value&);
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000891 };
892
893 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000894 * Converts an object to an ascii string.
895 * Useful if you want to print the object.
896 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000897 class EXPORT AsciiValue {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000898 public:
899 explicit AsciiValue(Handle<v8::Value> obj);
900 ~AsciiValue();
iposva@chromium.org245aa852009-02-10 00:49:54 +0000901 char* operator*() const { return str_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000902 int length() { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000903 private:
904 char* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000905 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000906
907 // Disallow copying and assigning.
908 AsciiValue(const AsciiValue&);
909 void operator=(const AsciiValue&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000910 };
911
912 /**
913 * Converts an object to a two-byte string.
914 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000915 class EXPORT Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000916 public:
917 explicit Value(Handle<v8::Value> obj);
918 ~Value();
iposva@chromium.org245aa852009-02-10 00:49:54 +0000919 uint16_t* operator*() const { return str_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000920 int length() { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000921 private:
922 uint16_t* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000923 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000924
925 // Disallow copying and assigning.
926 Value(const Value&);
927 void operator=(const Value&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000928 };
929};
930
931
932/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000933 * A JavaScript number value (ECMA-262, 4.3.20)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000934 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000935class EXPORT Number : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000936 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000937 double Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000938 static Local<Number> New(double value);
939 static Number* Cast(v8::Value* obj);
940 private:
941 Number();
942};
943
944
945/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000946 * A JavaScript value representing a signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000947 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000948class EXPORT Integer : public Number {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000949 public:
950 static Local<Integer> New(int32_t value);
ager@chromium.org32912102009-01-16 10:38:43 +0000951 int64_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000952 static Integer* Cast(v8::Value* obj);
953 private:
954 Integer();
955};
956
957
958/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000959 * A JavaScript value representing a 32-bit signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000960 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000961class EXPORT Int32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000962 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000963 int32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000964 private:
965 Int32();
966};
967
968
969/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000970 * A JavaScript value representing a 32-bit unsigned integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000971 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000972class EXPORT Uint32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000973 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000974 uint32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000975 private:
976 Uint32();
977};
978
979
980/**
981 * An instance of the built-in Date constructor (ECMA-262, 15.9).
982 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000983class EXPORT Date : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000984 public:
985 static Local<Value> New(double time);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000986
987 /**
988 * A specialization of Value::NumberValue that is more efficient
989 * because we know the structure of this object.
990 */
ager@chromium.org32912102009-01-16 10:38:43 +0000991 double NumberValue() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000992
993 static Date* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000994};
995
996
997enum PropertyAttribute {
998 None = 0,
999 ReadOnly = 1 << 0,
1000 DontEnum = 1 << 1,
1001 DontDelete = 1 << 2
1002};
1003
1004/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001005 * A JavaScript object (ECMA-262, 4.3.3)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001006 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001007class EXPORT Object : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001008 public:
1009 bool Set(Handle<Value> key,
1010 Handle<Value> value,
1011 PropertyAttribute attribs = None);
1012 Local<Value> Get(Handle<Value> key);
1013
1014 // TODO(1245389): Replace the type-specific versions of these
1015 // functions with generic ones that accept a Handle<Value> key.
1016 bool Has(Handle<String> key);
1017 bool Delete(Handle<String> key);
1018 bool Has(uint32_t index);
1019 bool Delete(uint32_t index);
1020
1021 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001022 * Returns an array containing the names of the enumerable properties
1023 * of this object, including properties from prototype objects. The
1024 * array returned by this method contains the same values as would
1025 * be enumerated by a for-in statement over this object.
1026 */
1027 Local<Array> GetPropertyNames();
1028
1029 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001030 * Get the prototype object. This does not skip objects marked to
1031 * be skipped by __proto__ and it does not consult the security
1032 * handler.
1033 */
1034 Local<Value> GetPrototype();
1035
1036 /**
1037 * Call builtin Object.prototype.toString on this object.
1038 * This is different from Value::ToString() that may call
1039 * user-defined toString function. This one does not.
1040 */
1041 Local<String> ObjectProtoToString();
1042
kasper.lund212ac232008-07-16 07:07:30 +00001043 /** Gets the number of internal fields for this Object. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001044 int InternalFieldCount();
kasper.lund212ac232008-07-16 07:07:30 +00001045 /** Gets the value in an internal field. */
1046 Local<Value> GetInternalField(int index);
1047 /** Sets the value in an internal field. */
1048 void SetInternalField(int index, Handle<Value> value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001049
1050 // Testers for local properties.
1051 bool HasRealNamedProperty(Handle<String> key);
1052 bool HasRealIndexedProperty(uint32_t index);
1053 bool HasRealNamedCallbackProperty(Handle<String> key);
1054
1055 /**
1056 * If result.IsEmpty() no real property was located in the prototype chain.
1057 * This means interceptors in the prototype chain are not called.
1058 */
1059 Handle<Value> GetRealNamedPropertyInPrototypeChain(Handle<String> key);
1060
1061 /** Tests for a named lookup interceptor.*/
1062 bool HasNamedLookupInterceptor();
1063
kasper.lund212ac232008-07-16 07:07:30 +00001064 /** Tests for an index lookup interceptor.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001065 bool HasIndexedLookupInterceptor();
1066
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001067 /**
1068 * Turns on access check on the object if the object is an instance of
1069 * a template that has access check callbacks. If an object has no
1070 * access check info, the object cannot be accessed by anyone.
1071 */
1072 void TurnOnAccessCheck();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001073
1074 static Local<Object> New();
1075 static Object* Cast(Value* obj);
1076 private:
1077 Object();
1078};
1079
1080
1081/**
1082 * An instance of the built-in array constructor (ECMA-262, 15.4.2).
1083 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001084class EXPORT Array : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001085 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001086 uint32_t Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001087
1088 static Local<Array> New(int length = 0);
1089 static Array* Cast(Value* obj);
1090 private:
1091 Array();
1092};
1093
1094
1095/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001096 * A JavaScript function object (ECMA-262, 15.3).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001097 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001098class EXPORT Function : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001099 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001100 Local<Object> NewInstance() const;
1101 Local<Object> NewInstance(int argc, Handle<Value> argv[]) const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001102 Local<Value> Call(Handle<Object> recv, int argc, Handle<Value> argv[]);
1103 void SetName(Handle<String> name);
ager@chromium.org32912102009-01-16 10:38:43 +00001104 Handle<Value> GetName() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001105 static Function* Cast(Value* obj);
1106 private:
1107 Function();
1108};
1109
1110
1111/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001112 * A JavaScript value that wraps a c++ void*. This type of value is
1113 * mainly used to associate c++ data structures with JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001114 * objects.
1115 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001116class EXPORT External : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001117 public:
1118 static Local<External> New(void* value);
1119 static External* Cast(Value* obj);
ager@chromium.org32912102009-01-16 10:38:43 +00001120 void* Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001121 private:
1122 External();
1123};
1124
1125
1126// --- T e m p l a t e s ---
1127
1128
1129/**
1130 * The superclass of object and function templates.
1131 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001132class EXPORT Template : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001133 public:
1134 /** Adds a property to each instance created by this template.*/
1135 void Set(Handle<String> name, Handle<Data> value,
1136 PropertyAttribute attributes = None);
1137 inline void Set(const char* name, Handle<Data> value);
1138 private:
1139 Template();
1140
1141 friend class ObjectTemplate;
1142 friend class FunctionTemplate;
1143};
1144
1145
1146/**
1147 * The argument information given to function call callbacks. This
v8.team.kasperl727e9952008-09-02 14:56:44 +00001148 * class provides access to information about the context of the call,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001149 * including the receiver, the number and values of arguments, and
1150 * the holder of the function.
1151 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001152class EXPORT Arguments {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001153 public:
1154 inline int Length() const;
1155 inline Local<Value> operator[](int i) const;
1156 inline Local<Function> Callee() const;
1157 inline Local<Object> This() const;
1158 inline Local<Object> Holder() const;
1159 inline bool IsConstructCall() const;
1160 inline Local<Value> Data() const;
1161 private:
1162 Arguments();
1163 friend class ImplementationUtilities;
1164 inline Arguments(Local<Value> data,
1165 Local<Object> holder,
1166 Local<Function> callee,
1167 bool is_construct_call,
1168 void** values, int length);
1169 Local<Value> data_;
1170 Local<Object> holder_;
1171 Local<Function> callee_;
1172 bool is_construct_call_;
1173 void** values_;
1174 int length_;
1175};
1176
1177
1178/**
1179 * The information passed to an accessor callback about the context
1180 * of the property access.
1181 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001182class EXPORT AccessorInfo {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001183 public:
1184 inline AccessorInfo(Local<Object> self,
1185 Local<Value> data,
1186 Local<Object> holder)
1187 : self_(self), data_(data), holder_(holder) { }
1188 inline Local<Value> Data() const;
1189 inline Local<Object> This() const;
1190 inline Local<Object> Holder() const;
1191 private:
1192 Local<Object> self_;
1193 Local<Value> data_;
1194 Local<Object> holder_;
1195};
1196
1197
1198typedef Handle<Value> (*InvocationCallback)(const Arguments& args);
1199
1200typedef int (*LookupCallback)(Local<Object> self, Local<String> name);
1201
1202/**
1203 * Accessor[Getter|Setter] are used as callback functions when
1204 * setting|getting a particular property. See objectTemplate::SetAccessor.
1205 */
1206typedef Handle<Value> (*AccessorGetter)(Local<String> property,
1207 const AccessorInfo& info);
1208
1209
1210typedef void (*AccessorSetter)(Local<String> property,
1211 Local<Value> value,
1212 const AccessorInfo& info);
1213
1214
1215/**
1216 * NamedProperty[Getter|Setter] are used as interceptors on object.
1217 * See ObjectTemplate::SetNamedPropertyHandler.
1218 */
1219typedef Handle<Value> (*NamedPropertyGetter)(Local<String> property,
1220 const AccessorInfo& info);
1221
1222
1223/**
1224 * Returns the value if the setter intercepts the request.
1225 * Otherwise, returns an empty handle.
1226 */
1227typedef Handle<Value> (*NamedPropertySetter)(Local<String> property,
1228 Local<Value> value,
1229 const AccessorInfo& info);
1230
1231
1232/**
1233 * Returns a non-empty handle if the interceptor intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001234 * The result is true if the property exists and false otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001235 */
1236typedef Handle<Boolean> (*NamedPropertyQuery)(Local<String> property,
1237 const AccessorInfo& info);
1238
1239
1240/**
1241 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001242 * The return value is true if the property could be deleted and false
1243 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001244 */
1245typedef Handle<Boolean> (*NamedPropertyDeleter)(Local<String> property,
1246 const AccessorInfo& info);
1247
1248/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001249 * Returns an array containing the names of the properties the named
1250 * property getter intercepts.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001251 */
1252typedef Handle<Array> (*NamedPropertyEnumerator)(const AccessorInfo& info);
1253
v8.team.kasperl727e9952008-09-02 14:56:44 +00001254
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001255/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001256 * Returns the value of the property if the getter intercepts the
1257 * request. Otherwise, returns an empty handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001258 */
1259typedef Handle<Value> (*IndexedPropertyGetter)(uint32_t index,
1260 const AccessorInfo& info);
1261
1262
1263/**
1264 * Returns the value if the setter intercepts the request.
1265 * Otherwise, returns an empty handle.
1266 */
1267typedef Handle<Value> (*IndexedPropertySetter)(uint32_t index,
1268 Local<Value> value,
1269 const AccessorInfo& info);
1270
1271
1272/**
1273 * Returns a non-empty handle if the interceptor intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001274 * The result is true if the property exists and false otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001275 */
1276typedef Handle<Boolean> (*IndexedPropertyQuery)(uint32_t index,
1277 const AccessorInfo& info);
1278
1279/**
1280 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001281 * The return value is true if the property could be deleted and false
1282 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001283 */
1284typedef Handle<Boolean> (*IndexedPropertyDeleter)(uint32_t index,
1285 const AccessorInfo& info);
1286
v8.team.kasperl727e9952008-09-02 14:56:44 +00001287/**
1288 * Returns an array containing the indices of the properties the
1289 * indexed property getter intercepts.
1290 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001291typedef Handle<Array> (*IndexedPropertyEnumerator)(const AccessorInfo& info);
1292
1293
1294/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001295 * Access control specifications.
1296 *
1297 * Some accessors should be accessible across contexts. These
1298 * accessors have an explicit access control parameter which specifies
1299 * the kind of cross-context access that should be allowed.
ager@chromium.org870a0b62008-11-04 11:43:05 +00001300 *
1301 * Additionally, for security, accessors can prohibit overwriting by
1302 * accessors defined in JavaScript. For objects that have such
1303 * accessors either locally or in their prototype chain it is not
1304 * possible to overwrite the accessor by using __defineGetter__ or
1305 * __defineSetter__ from JavaScript code.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001306 */
1307enum AccessControl {
ager@chromium.org870a0b62008-11-04 11:43:05 +00001308 DEFAULT = 0,
1309 ALL_CAN_READ = 1,
1310 ALL_CAN_WRITE = 1 << 1,
1311 PROHIBITS_OVERWRITING = 1 << 2
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001312};
1313
1314
1315/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001316 * Access type specification.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001317 */
1318enum AccessType {
1319 ACCESS_GET,
1320 ACCESS_SET,
1321 ACCESS_HAS,
1322 ACCESS_DELETE,
1323 ACCESS_KEYS
1324};
1325
v8.team.kasperl727e9952008-09-02 14:56:44 +00001326
1327/**
1328 * Returns true if cross-context access should be allowed to the named
1329 * property with the given key on the global object.
1330 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001331typedef bool (*NamedSecurityCallback)(Local<Object> global,
1332 Local<Value> key,
1333 AccessType type,
1334 Local<Value> data);
1335
v8.team.kasperl727e9952008-09-02 14:56:44 +00001336
1337/**
1338 * Returns true if cross-context access should be allowed to the indexed
1339 * property with the given index on the global object.
1340 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001341typedef bool (*IndexedSecurityCallback)(Local<Object> global,
1342 uint32_t index,
1343 AccessType type,
1344 Local<Value> data);
1345
1346
1347/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001348 * A FunctionTemplate is used to create functions at runtime. There
1349 * can only be one function created from a FunctionTemplate in a
1350 * context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001351 *
1352 * A FunctionTemplate can have properties, these properties are added to the
v8.team.kasperl727e9952008-09-02 14:56:44 +00001353 * function object when it is created.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001354 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001355 * A FunctionTemplate has a corresponding instance template which is
1356 * used to create object instances when the function is used as a
1357 * constructor. Properties added to the instance template are added to
1358 * each object instance.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001359 *
1360 * A FunctionTemplate can have a prototype template. The prototype template
1361 * is used to create the prototype object of the function.
1362 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001363 * The following example shows how to use a FunctionTemplate:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001364 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001365 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001366 * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
1367 * t->Set("func_property", v8::Number::New(1));
1368 *
1369 * v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
1370 * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
1371 * proto_t->Set("proto_const", v8::Number::New(2));
1372 *
1373 * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
1374 * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
1375 * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
1376 * instance_t->Set("instance_property", Number::New(3));
1377 *
1378 * v8::Local<v8::Function> function = t->GetFunction();
1379 * v8::Local<v8::Object> instance = function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00001380 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001381 *
1382 * Let's use "function" as the JS variable name of the function object
v8.team.kasperl727e9952008-09-02 14:56:44 +00001383 * and "instance" for the instance object created above. The function
1384 * and the instance will have the following properties:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001385 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001386 * \code
1387 * func_property in function == true;
1388 * function.func_property == 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001389 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001390 * function.prototype.proto_method() invokes 'InvokeCallback'
1391 * function.prototype.proto_const == 2;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001392 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001393 * instance instanceof function == true;
1394 * instance.instance_accessor calls 'InstanceAccessorCallback'
1395 * instance.instance_property == 3;
1396 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001397 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001398 * A FunctionTemplate can inherit from another one by calling the
1399 * FunctionTemplate::Inherit method. The following graph illustrates
1400 * the semantics of inheritance:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001401 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001402 * \code
1403 * FunctionTemplate Parent -> Parent() . prototype -> { }
1404 * ^ ^
1405 * | Inherit(Parent) | .__proto__
1406 * | |
1407 * FunctionTemplate Child -> Child() . prototype -> { }
1408 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001409 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001410 * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
1411 * object of the Child() function has __proto__ pointing to the
1412 * Parent() function's prototype object. An instance of the Child
1413 * function has all properties on Parent's instance templates.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001414 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001415 * Let Parent be the FunctionTemplate initialized in the previous
1416 * section and create a Child FunctionTemplate by:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001417 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001418 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001419 * Local<FunctionTemplate> parent = t;
1420 * Local<FunctionTemplate> child = FunctionTemplate::New();
1421 * child->Inherit(parent);
1422 *
1423 * Local<Function> child_function = child->GetFunction();
1424 * Local<Object> child_instance = child_function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00001425 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001426 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001427 * The Child function and Child instance will have the following
1428 * properties:
1429 *
1430 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001431 * child_func.prototype.__proto__ == function.prototype;
v8.team.kasperl727e9952008-09-02 14:56:44 +00001432 * child_instance.instance_accessor calls 'InstanceAccessorCallback'
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001433 * child_instance.instance_property == 3;
v8.team.kasperl727e9952008-09-02 14:56:44 +00001434 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001435 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001436class EXPORT FunctionTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001437 public:
1438 /** Creates a function template.*/
v8.team.kasperl727e9952008-09-02 14:56:44 +00001439 static Local<FunctionTemplate> New(
1440 InvocationCallback callback = 0,
1441 Handle<Value> data = Handle<Value>(),
1442 Handle<Signature> signature = Handle<Signature>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001443 /** Returns the unique function instance in the current execution context.*/
1444 Local<Function> GetFunction();
1445
v8.team.kasperl727e9952008-09-02 14:56:44 +00001446 /**
1447 * Set the call-handler callback for a FunctionTemplate. This
1448 * callback is called whenever the function created from this
1449 * FunctionTemplate is called.
1450 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001451 void SetCallHandler(InvocationCallback callback,
1452 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001453
v8.team.kasperl727e9952008-09-02 14:56:44 +00001454 /** Get the InstanceTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001455 Local<ObjectTemplate> InstanceTemplate();
1456
1457 /** Causes the function template to inherit from a parent function template.*/
1458 void Inherit(Handle<FunctionTemplate> parent);
1459
1460 /**
1461 * A PrototypeTemplate is the template used to create the prototype object
1462 * of the function created by this template.
1463 */
1464 Local<ObjectTemplate> PrototypeTemplate();
1465
v8.team.kasperl727e9952008-09-02 14:56:44 +00001466
1467 /**
1468 * Set the class name of the FunctionTemplate. This is used for
1469 * printing objects created with the function created from the
1470 * FunctionTemplate as its constructor.
1471 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001472 void SetClassName(Handle<String> name);
1473
1474 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001475 * Determines whether the __proto__ accessor ignores instances of
1476 * the function template. If instances of the function template are
1477 * ignored, __proto__ skips all instances and instead returns the
1478 * next object in the prototype chain.
1479 *
1480 * Call with a value of true to make the __proto__ accessor ignore
1481 * instances of the function template. Call with a value of false
1482 * to make the __proto__ accessor not ignore instances of the
1483 * function template. By default, instances of a function template
1484 * are not ignored.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001485 */
1486 void SetHiddenPrototype(bool value);
1487
1488 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001489 * Returns true if the given object is an instance of this function
1490 * template.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001491 */
1492 bool HasInstance(Handle<Value> object);
1493
1494 private:
1495 FunctionTemplate();
1496 void AddInstancePropertyAccessor(Handle<String> name,
1497 AccessorGetter getter,
1498 AccessorSetter setter,
1499 Handle<Value> data,
1500 AccessControl settings,
1501 PropertyAttribute attributes);
1502 void SetNamedInstancePropertyHandler(NamedPropertyGetter getter,
1503 NamedPropertySetter setter,
1504 NamedPropertyQuery query,
1505 NamedPropertyDeleter remover,
1506 NamedPropertyEnumerator enumerator,
1507 Handle<Value> data);
1508 void SetIndexedInstancePropertyHandler(IndexedPropertyGetter getter,
1509 IndexedPropertySetter setter,
1510 IndexedPropertyQuery query,
1511 IndexedPropertyDeleter remover,
1512 IndexedPropertyEnumerator enumerator,
1513 Handle<Value> data);
1514 void SetInstanceCallAsFunctionHandler(InvocationCallback callback,
1515 Handle<Value> data);
1516
1517 friend class Context;
1518 friend class ObjectTemplate;
1519};
1520
1521
1522/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001523 * An ObjectTemplate is used to create objects at runtime.
1524 *
1525 * Properties added to an ObjectTemplate are added to each object
1526 * created from the ObjectTemplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001527 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001528class EXPORT ObjectTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001529 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00001530 /** Creates an ObjectTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001531 static Local<ObjectTemplate> New();
v8.team.kasperl727e9952008-09-02 14:56:44 +00001532
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001533 /** Creates a new instance of this template.*/
1534 Local<Object> NewInstance();
1535
1536 /**
1537 * Sets an accessor on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001538 *
1539 * Whenever the property with the given name is accessed on objects
1540 * created from this ObjectTemplate the getter and setter callbacks
1541 * are called instead of getting and setting the property directly
1542 * on the JavaScript object.
1543 *
1544 * \param name The name of the property for which an accessor is added.
1545 * \param getter The callback to invoke when getting the property.
1546 * \param setter The callback to invoke when setting the property.
1547 * \param data A piece of data that will be passed to the getter and setter
1548 * callbacks whenever they are invoked.
1549 * \param settings Access control settings for the accessor. This is a bit
1550 * field consisting of one of more of
1551 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
1552 * The default is to not allow cross-context access.
1553 * ALL_CAN_READ means that all cross-context reads are allowed.
1554 * ALL_CAN_WRITE means that all cross-context writes are allowed.
1555 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
1556 * cross-context access.
1557 * \param attribute The attributes of the property for which an accessor
1558 * is added.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001559 */
1560 void SetAccessor(Handle<String> name,
1561 AccessorGetter getter,
1562 AccessorSetter setter = 0,
1563 Handle<Value> data = Handle<Value>(),
1564 AccessControl settings = DEFAULT,
1565 PropertyAttribute attribute = None);
1566
1567 /**
1568 * Sets a named property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001569 *
1570 * Whenever a named property is accessed on objects created from
1571 * this object template, the provided callback is invoked instead of
1572 * accessing the property directly on the JavaScript object.
1573 *
1574 * \param getter The callback to invoke when getting a property.
1575 * \param setter The callback to invoke when setting a property.
1576 * \param query The callback to invoke to check is an object has a property.
1577 * \param deleter The callback to invoke when deleting a property.
1578 * \param enumerator The callback to invoke to enumerate all the named
1579 * properties of an object.
1580 * \param data A piece of data that will be passed to the callbacks
1581 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001582 */
1583 void SetNamedPropertyHandler(NamedPropertyGetter getter,
1584 NamedPropertySetter setter = 0,
1585 NamedPropertyQuery query = 0,
1586 NamedPropertyDeleter deleter = 0,
1587 NamedPropertyEnumerator enumerator = 0,
1588 Handle<Value> data = Handle<Value>());
1589
1590 /**
1591 * Sets an indexed property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001592 *
1593 * Whenever an indexed property is accessed on objects created from
1594 * this object template, the provided callback is invoked instead of
1595 * accessing the property directly on the JavaScript object.
1596 *
1597 * \param getter The callback to invoke when getting a property.
1598 * \param setter The callback to invoke when setting a property.
1599 * \param query The callback to invoke to check is an object has a property.
1600 * \param deleter The callback to invoke when deleting a property.
1601 * \param enumerator The callback to invoke to enumerate all the indexed
1602 * properties of an object.
1603 * \param data A piece of data that will be passed to the callbacks
1604 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001605 */
1606 void SetIndexedPropertyHandler(IndexedPropertyGetter getter,
1607 IndexedPropertySetter setter = 0,
1608 IndexedPropertyQuery query = 0,
1609 IndexedPropertyDeleter deleter = 0,
1610 IndexedPropertyEnumerator enumerator = 0,
1611 Handle<Value> data = Handle<Value>());
1612 /**
1613 * Sets the callback to be used when calling instances created from
1614 * this template as a function. If no callback is set, instances
v8.team.kasperl727e9952008-09-02 14:56:44 +00001615 * behave like normal JavaScript objects that cannot be called as a
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001616 * function.
1617 */
1618 void SetCallAsFunctionHandler(InvocationCallback callback,
1619 Handle<Value> data = Handle<Value>());
1620
v8.team.kasperl727e9952008-09-02 14:56:44 +00001621 /**
1622 * Mark object instances of the template as undetectable.
1623 *
1624 * In many ways, undetectable objects behave as though they are not
1625 * there. They behave like 'undefined' in conditionals and when
1626 * printed. However, properties can be accessed and called as on
1627 * normal objects.
1628 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001629 void MarkAsUndetectable();
1630
v8.team.kasperl727e9952008-09-02 14:56:44 +00001631 /**
1632 * Sets access check callbacks on the object template.
1633 *
1634 * When accessing properties on instances of this object template,
1635 * the access check callback will be called to determine whether or
1636 * not to allow cross-context access to the properties.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001637 * The last parameter specifies whether access checks are turned
1638 * on by default on instances. If access checks are off by default,
1639 * they can be turned on on individual instances by calling
1640 * Object::TurnOnAccessCheck().
v8.team.kasperl727e9952008-09-02 14:56:44 +00001641 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001642 void SetAccessCheckCallbacks(NamedSecurityCallback named_handler,
1643 IndexedSecurityCallback indexed_handler,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001644 Handle<Value> data = Handle<Value>(),
1645 bool turned_on_by_default = true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001646
kasper.lund212ac232008-07-16 07:07:30 +00001647 /**
1648 * Gets the number of internal fields for objects generated from
1649 * this template.
1650 */
1651 int InternalFieldCount();
1652
1653 /**
1654 * Sets the number of internal fields for objects generated from
1655 * this template.
1656 */
1657 void SetInternalFieldCount(int value);
1658
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001659 private:
1660 ObjectTemplate();
1661 static Local<ObjectTemplate> New(Handle<FunctionTemplate> constructor);
1662 friend class FunctionTemplate;
1663};
1664
1665
1666/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001667 * A Signature specifies which receivers and arguments a function can
1668 * legally be called with.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001669 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001670class EXPORT Signature : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001671 public:
1672 static Local<Signature> New(Handle<FunctionTemplate> receiver =
1673 Handle<FunctionTemplate>(),
1674 int argc = 0,
1675 Handle<FunctionTemplate> argv[] = 0);
1676 private:
1677 Signature();
1678};
1679
1680
1681/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001682 * A utility for determining the type of objects based on the template
1683 * they were constructed from.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001684 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001685class EXPORT TypeSwitch : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001686 public:
1687 static Local<TypeSwitch> New(Handle<FunctionTemplate> type);
1688 static Local<TypeSwitch> New(int argc, Handle<FunctionTemplate> types[]);
1689 int match(Handle<Value> value);
1690 private:
1691 TypeSwitch();
1692};
1693
1694
1695// --- E x t e n s i o n s ---
1696
1697
1698/**
1699 * Ignore
1700 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001701class EXPORT Extension { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001702 public:
1703 Extension(const char* name,
1704 const char* source = 0,
1705 int dep_count = 0,
1706 const char** deps = 0);
1707 virtual ~Extension() { }
1708 virtual v8::Handle<v8::FunctionTemplate>
1709 GetNativeFunction(v8::Handle<v8::String> name) {
1710 return v8::Handle<v8::FunctionTemplate>();
1711 }
1712
1713 const char* name() { return name_; }
1714 const char* source() { return source_; }
1715 int dependency_count() { return dep_count_; }
1716 const char** dependencies() { return deps_; }
1717 void set_auto_enable(bool value) { auto_enable_ = value; }
1718 bool auto_enable() { return auto_enable_; }
1719
1720 private:
1721 const char* name_;
1722 const char* source_;
1723 int dep_count_;
1724 const char** deps_;
1725 bool auto_enable_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001726
1727 // Disallow copying and assigning.
1728 Extension(const Extension&);
1729 void operator=(const Extension&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001730};
1731
1732
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001733void EXPORT RegisterExtension(Extension* extension);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001734
1735
1736/**
1737 * Ignore
1738 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001739class EXPORT DeclareExtension {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001740 public:
1741 inline DeclareExtension(Extension* extension) {
1742 RegisterExtension(extension);
1743 }
1744};
1745
1746
1747// --- S t a t i c s ---
1748
1749
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001750Handle<Primitive> EXPORT Undefined();
1751Handle<Primitive> EXPORT Null();
1752Handle<Boolean> EXPORT True();
1753Handle<Boolean> EXPORT False();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001754
1755
1756/**
1757 * A set of constraints that specifies the limits of the runtime's
1758 * memory use.
1759 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001760class EXPORT ResourceConstraints {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001761 public:
1762 ResourceConstraints();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001763 int max_young_space_size() const { return max_young_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001764 void set_max_young_space_size(int value) { max_young_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001765 int max_old_space_size() const { return max_old_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001766 void set_max_old_space_size(int value) { max_old_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001767 uint32_t* stack_limit() const { return stack_limit_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001768 void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
1769 private:
1770 int max_young_space_size_;
1771 int max_old_space_size_;
1772 uint32_t* stack_limit_;
1773};
1774
1775
1776bool SetResourceConstraints(ResourceConstraints* constraints);
1777
1778
1779// --- E x c e p t i o n s ---
1780
1781
1782typedef void (*FatalErrorCallback)(const char* location, const char* message);
1783
1784
1785typedef void (*MessageCallback)(Handle<Message> message, Handle<Value> data);
1786
1787
1788/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001789 * Schedules an exception to be thrown when returning to JavaScript. When an
1790 * exception has been scheduled it is illegal to invoke any JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001791 * operation; the caller must return immediately and only after the exception
v8.team.kasperl727e9952008-09-02 14:56:44 +00001792 * has been handled does it become legal to invoke JavaScript operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001793 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001794Handle<Value> EXPORT ThrowException(Handle<Value> exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001795
1796/**
1797 * Create new error objects by calling the corresponding error object
1798 * constructor with the message.
1799 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001800class EXPORT Exception {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001801 public:
1802 static Local<Value> RangeError(Handle<String> message);
1803 static Local<Value> ReferenceError(Handle<String> message);
1804 static Local<Value> SyntaxError(Handle<String> message);
1805 static Local<Value> TypeError(Handle<String> message);
1806 static Local<Value> Error(Handle<String> message);
1807};
1808
1809
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001810// --- C o u n t e r s C a l l b a c k s ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001811
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001812typedef int* (*CounterLookupCallback)(const char* name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001813
1814// --- F a i l e d A c c e s s C h e c k C a l l b a c k ---
1815typedef void (*FailedAccessCheckCallback)(Local<Object> target,
1816 AccessType type,
1817 Local<Value> data);
1818
1819// --- G a r b a g e C o l l e c t i o n C a l l b a c k s
1820
1821/**
1822 * Applications can register a callback function which is called
v8.team.kasperl727e9952008-09-02 14:56:44 +00001823 * before and after a major garbage collection. Allocations are not
1824 * allowed in the callback function, you therefore cannot manipulate
1825 * objects (set or delete properties for example) since it is possible
1826 * such operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001827 */
1828typedef void (*GCCallback)();
1829
1830
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001831// --- C o n t e x t G e n e r a t o r ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001832
1833/**
1834 * Applications must provide a callback function which is called to generate
v8.team.kasperl727e9952008-09-02 14:56:44 +00001835 * a context if a context was not deserialized from the snapshot.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001836 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001837typedef Persistent<Context> (*ContextGenerator)();
1838
1839
1840/**
1841 * Container class for static utility functions.
1842 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001843class EXPORT V8 {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001844 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00001845 /** Set the callback to invoke in case of fatal errors. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001846 static void SetFatalErrorHandler(FatalErrorCallback that);
1847
v8.team.kasperl727e9952008-09-02 14:56:44 +00001848 /**
1849 * Ignore out-of-memory exceptions.
1850 *
1851 * V8 running out of memory is treated as a fatal error by default.
1852 * This means that the fatal error handler is called and that V8 is
1853 * terminated.
1854 *
1855 * IgnoreOutOfMemoryException can be used to not treat a
1856 * out-of-memory situation as a fatal error. This way, the contexts
1857 * that did not cause the out of memory problem might be able to
1858 * continue execution.
1859 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001860 static void IgnoreOutOfMemoryException();
1861
v8.team.kasperl727e9952008-09-02 14:56:44 +00001862 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001863 * Check if V8 is dead and therefore unusable. This is the case after
v8.team.kasperl727e9952008-09-02 14:56:44 +00001864 * fatal errors such as out-of-memory situations.
1865 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001866 static bool IsDead();
1867
1868 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001869 * Adds a message listener.
1870 *
1871 * The same message listener can be added more than once and it that
1872 * case it will be called more than once for each message.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001873 */
1874 static bool AddMessageListener(MessageCallback that,
1875 Handle<Value> data = Handle<Value>());
1876
1877 /**
1878 * Remove all message listeners from the specified callback function.
1879 */
1880 static void RemoveMessageListeners(MessageCallback that);
1881
1882 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001883 * Sets V8 flags from a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001884 */
1885 static void SetFlagsFromString(const char* str, int length);
1886
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001887 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001888 * Sets V8 flags from the command line.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001889 */
1890 static void SetFlagsFromCommandLine(int* argc,
1891 char** argv,
1892 bool remove_flags);
1893
kasper.lund7276f142008-07-30 08:49:36 +00001894 /** Get the version string. */
1895 static const char* GetVersion();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001896
1897 /**
1898 * Enables the host application to provide a mechanism for recording
1899 * statistics counters.
1900 */
1901 static void SetCounterFunction(CounterLookupCallback);
1902
1903 /**
1904 * Enables the computation of a sliding window of states. The sliding
1905 * window information is recorded in statistics counters.
1906 */
1907 static void EnableSlidingStateWindow();
1908
1909 /** Callback function for reporting failed access checks.*/
1910 static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);
1911
1912 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001913 * Enables the host application to receive a notification before a
1914 * major garbage colletion. Allocations are not allowed in the
1915 * callback function, you therefore cannot manipulate objects (set
1916 * or delete properties for example) since it is possible such
1917 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001918 */
1919 static void SetGlobalGCPrologueCallback(GCCallback);
1920
1921 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001922 * Enables the host application to receive a notification after a
1923 * major garbage collection. Allocations are not allowed in the
1924 * callback function, you therefore cannot manipulate objects (set
1925 * or delete properties for example) since it is possible such
1926 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001927 */
1928 static void SetGlobalGCEpilogueCallback(GCCallback);
1929
1930 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001931 * Allows the host application to group objects together. If one
1932 * object in the group is alive, all objects in the group are alive.
1933 * After each garbage collection, object groups are removed. It is
1934 * intended to be used in the before-garbage-collection callback
ager@chromium.org8bb60582008-12-11 12:02:20 +00001935 * function, for instance to simulate DOM tree connections among JS
v8.team.kasperl727e9952008-09-02 14:56:44 +00001936 * wrapper objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001937 */
ager@chromium.org8bb60582008-12-11 12:02:20 +00001938 static void AddObjectGroup(Persistent<Value>* objects, size_t length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001939
1940 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001941 * Initializes from snapshot if possible. Otherwise, attempts to
1942 * initialize from scratch.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001943 */
1944 static bool Initialize();
1945
kasper.lund7276f142008-07-30 08:49:36 +00001946 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001947 * Adjusts the amount of registered external memory. Used to give
1948 * V8 an indication of the amount of externally allocated memory
1949 * that is kept alive by JavaScript objects. V8 uses this to decide
1950 * when to perform global garbage collections. Registering
1951 * externally allocated memory will trigger global garbage
1952 * collections more often than otherwise in an attempt to garbage
1953 * collect the JavaScript objects keeping the externally allocated
1954 * memory alive.
1955 *
1956 * \param change_in_bytes the change in externally allocated memory
1957 * that is kept alive by JavaScript objects.
1958 * \returns the adjusted value.
kasper.lund7276f142008-07-30 08:49:36 +00001959 */
1960 static int AdjustAmountOfExternalAllocatedMemory(int change_in_bytes);
1961
iposva@chromium.org245aa852009-02-10 00:49:54 +00001962 /**
1963 * Suspends recording of tick samples in the profiler.
1964 * When the V8 profiling mode is enabled (usually via command line
1965 * switches) this function suspends recording of tick samples.
1966 * Profiling ticks are discarded until ResumeProfiler() is called.
1967 *
1968 * See also the --prof and --prof_auto command line switches to
1969 * enable V8 profiling.
1970 */
1971 static void PauseProfiler();
1972
1973 /**
1974 * Resumes recording of tick samples in the profiler.
1975 * See also PauseProfiler().
1976 */
1977 static void ResumeProfiler();
1978
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001979 private:
1980 V8();
1981
1982 static void** GlobalizeReference(void** handle);
1983 static void DisposeGlobal(void** global_handle);
1984 static void MakeWeak(void** global_handle, void* data, WeakReferenceCallback);
1985 static void ClearWeak(void** global_handle);
1986 static bool IsGlobalNearDeath(void** global_handle);
1987 static bool IsGlobalWeak(void** global_handle);
1988
1989 template <class T> friend class Handle;
1990 template <class T> friend class Local;
1991 template <class T> friend class Persistent;
1992 friend class Context;
1993};
1994
1995
1996/**
1997 * An external exception handler.
1998 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001999class EXPORT TryCatch {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002000 public:
2001
2002 /**
2003 * Creates a new try/catch block and registers it with v8.
2004 */
2005 TryCatch();
2006
2007 /**
2008 * Unregisters and deletes this try/catch block.
2009 */
2010 ~TryCatch();
2011
2012 /**
2013 * Returns true if an exception has been caught by this try/catch block.
2014 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002015 bool HasCaught() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002016
2017 /**
2018 * Returns the exception caught by this try/catch block. If no exception has
2019 * been caught an empty handle is returned.
2020 *
2021 * The returned handle is valid until this TryCatch block has been destroyed.
2022 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002023 Local<Value> Exception() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002024
2025 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002026 * Returns the message associated with this exception. If there is
2027 * no message associated an empty handle is returned.
2028 *
2029 * The returned handle is valid until this TryCatch block has been
2030 * destroyed.
2031 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002032 Local<v8::Message> Message() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002033
2034 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002035 * Clears any exceptions that may have been caught by this try/catch block.
2036 * After this method has been called, HasCaught() will return false.
2037 *
2038 * It is not necessary to clear a try/catch block before using it again; if
2039 * another exception is thrown the previously caught exception will just be
2040 * overwritten. However, it is often a good idea since it makes it easier
2041 * to determine which operation threw a given exception.
2042 */
2043 void Reset();
2044
v8.team.kasperl727e9952008-09-02 14:56:44 +00002045 /**
2046 * Set verbosity of the external exception handler.
2047 *
2048 * By default, exceptions that are caught by an external exception
2049 * handler are not reported. Call SetVerbose with true on an
2050 * external exception handler to have exceptions caught by the
2051 * handler reported as if they were not caught.
2052 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002053 void SetVerbose(bool value);
2054
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002055 /**
2056 * Set whether or not this TryCatch should capture a Message object
2057 * which holds source information about where the exception
2058 * occurred. True by default.
2059 */
2060 void SetCaptureMessage(bool value);
2061
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002062 public:
2063 TryCatch* next_;
2064 void* exception_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002065 void* message_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002066 bool is_verbose_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002067 bool capture_message_;
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002068 void* js_handler_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002069};
2070
2071
2072// --- C o n t e x t ---
2073
2074
2075/**
2076 * Ignore
2077 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002078class EXPORT ExtensionConfiguration {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002079 public:
2080 ExtensionConfiguration(int name_count, const char* names[])
2081 : name_count_(name_count), names_(names) { }
2082 private:
2083 friend class ImplementationUtilities;
2084 int name_count_;
2085 const char** names_;
2086};
2087
2088
2089/**
2090 * A sandboxed execution context with its own set of built-in objects
2091 * and functions.
2092 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002093class EXPORT Context {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002094 public:
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002095 /** Returns the global object of the context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002096 Local<Object> Global();
2097
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002098 /**
2099 * Detaches the global object from its context before
2100 * the global object can be reused to create a new context.
2101 */
2102 void DetachGlobal();
2103
v8.team.kasperl727e9952008-09-02 14:56:44 +00002104 /** Creates a new context. */
2105 static Persistent<Context> New(
2106 ExtensionConfiguration* extensions = 0,
2107 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
2108 Handle<Value> global_object = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002109
kasper.lund44510672008-07-25 07:37:58 +00002110 /** Returns the last entered context. */
2111 static Local<Context> GetEntered();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002112
kasper.lund44510672008-07-25 07:37:58 +00002113 /** Returns the context that is on the top of the stack. */
2114 static Local<Context> GetCurrent();
2115
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002116 /**
2117 * Sets the security token for the context. To access an object in
2118 * another context, the security tokens must match.
2119 */
2120 void SetSecurityToken(Handle<Value> token);
2121
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002122 /** Restores the security token to the default value. */
2123 void UseDefaultSecurityToken();
2124
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002125 /** Returns the security token of this context.*/
2126 Handle<Value> GetSecurityToken();
2127
v8.team.kasperl727e9952008-09-02 14:56:44 +00002128 /**
2129 * Enter this context. After entering a context, all code compiled
2130 * and run is compiled and run in this context. If another context
2131 * is already entered, this old context is saved so it can be
2132 * restored when the new context is exited.
2133 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002134 void Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002135
2136 /**
2137 * Exit this context. Exiting the current context restores the
2138 * context that was in place when entering the current context.
2139 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002140 void Exit();
2141
v8.team.kasperl727e9952008-09-02 14:56:44 +00002142 /** Returns true if the context has experienced an out of memory situation. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002143 bool HasOutOfMemoryException();
2144
v8.team.kasperl727e9952008-09-02 14:56:44 +00002145 /** Returns true if V8 has a current context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002146 static bool InContext();
2147
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002148 /**
2149 * Stack-allocated class which sets the execution context for all
2150 * operations executed within a local scope.
2151 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002152 class EXPORT Scope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002153 public:
2154 inline Scope(Handle<Context> context) : context_(context) {
2155 context_->Enter();
2156 }
2157 inline ~Scope() { context_->Exit(); }
2158 private:
2159 Handle<Context> context_;
2160 };
2161
2162 private:
2163 friend class Value;
2164 friend class Script;
2165 friend class Object;
2166 friend class Function;
2167};
2168
2169
2170/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002171 * Multiple threads in V8 are allowed, but only one thread at a time
2172 * is allowed to use V8. The definition of 'using V8' includes
2173 * accessing handles or holding onto object pointers obtained from V8
2174 * handles. It is up to the user of V8 to ensure (perhaps with
2175 * locking) that this constraint is not violated.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002176 *
2177 * If you wish to start using V8 in a thread you can do this by constructing
2178 * a v8::Locker object. After the code using V8 has completed for the
2179 * current thread you can call the destructor. This can be combined
2180 * with C++ scope-based construction as follows:
2181 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002182 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002183 * ...
2184 * {
2185 * v8::Locker locker;
2186 * ...
2187 * // Code using V8 goes here.
2188 * ...
2189 * } // Destructor called here
v8.team.kasperl727e9952008-09-02 14:56:44 +00002190 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002191 *
2192 * If you wish to stop using V8 in a thread A you can do this by either
2193 * by destroying the v8::Locker object as above or by constructing a
2194 * v8::Unlocker object:
2195 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002196 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002197 * {
2198 * v8::Unlocker unlocker;
2199 * ...
2200 * // Code not using V8 goes here while V8 can run in another thread.
2201 * ...
2202 * } // Destructor called here.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002203 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002204 *
2205 * The Unlocker object is intended for use in a long-running callback
2206 * from V8, where you want to release the V8 lock for other threads to
2207 * use.
2208 *
2209 * The v8::Locker is a recursive lock. That is, you can lock more than
2210 * once in a given thread. This can be useful if you have code that can
2211 * be called either from code that holds the lock or from code that does
2212 * not. The Unlocker is not recursive so you can not have several
2213 * Unlockers on the stack at once, and you can not use an Unlocker in a
2214 * thread that is not inside a Locker's scope.
2215 *
2216 * An unlocker will unlock several lockers if it has to and reinstate
2217 * the correct depth of locking on its destruction. eg.:
2218 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002219 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002220 * // V8 not locked.
2221 * {
2222 * v8::Locker locker;
2223 * // V8 locked.
2224 * {
2225 * v8::Locker another_locker;
2226 * // V8 still locked (2 levels).
2227 * {
2228 * v8::Unlocker unlocker;
2229 * // V8 not locked.
2230 * }
2231 * // V8 locked again (2 levels).
2232 * }
2233 * // V8 still locked (1 level).
2234 * }
2235 * // V8 Now no longer locked.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002236 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002237 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002238class EXPORT Unlocker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002239 public:
2240 Unlocker();
2241 ~Unlocker();
2242};
2243
2244
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002245class EXPORT Locker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002246 public:
2247 Locker();
2248 ~Locker();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002249
2250 /**
2251 * Start preemption.
2252 *
2253 * When preemption is started, a timer is fired every n milli seconds
2254 * that will switch between multiple threads that are in contention
2255 * for the V8 lock.
2256 */
2257 static void StartPreemption(int every_n_ms);
2258
2259 /**
2260 * Stop preemption.
2261 */
2262 static void StopPreemption();
2263
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002264 /**
2265 * Returns whether or not the locker is locked by the current thread.
2266 */
2267 static bool IsLocked();
2268
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002269 /**
2270 * Returns whether v8::Locker is being used by this V8 instance.
2271 */
2272 static bool IsActive() { return active_; }
2273
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002274 private:
2275 bool has_lock_;
2276 bool top_level_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002277
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002278 static bool active_;
2279
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002280 // Disallow copying and assigning.
2281 Locker(const Locker&);
2282 void operator=(const Locker&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002283};
2284
2285
2286
2287// --- I m p l e m e n t a t i o n ---
2288
2289template <class T>
2290Handle<T>::Handle() : val_(0) { }
2291
2292
2293template <class T>
2294Local<T>::Local() : Handle<T>() { }
2295
2296
2297template <class T>
2298Local<T> Local<T>::New(Handle<T> that) {
2299 if (that.IsEmpty()) return Local<T>();
2300 void** p = reinterpret_cast<void**>(*that);
2301 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p)));
2302}
2303
2304
2305template <class T>
2306Persistent<T> Persistent<T>::New(Handle<T> that) {
2307 if (that.IsEmpty()) return Persistent<T>();
2308 void** p = reinterpret_cast<void**>(*that);
2309 return Persistent<T>(reinterpret_cast<T*>(V8::GlobalizeReference(p)));
2310}
2311
2312
2313template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00002314bool Persistent<T>::IsNearDeath() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002315 if (this->IsEmpty()) return false;
2316 return V8::IsGlobalNearDeath(reinterpret_cast<void**>(**this));
2317}
2318
2319
2320template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00002321bool Persistent<T>::IsWeak() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002322 if (this->IsEmpty()) return false;
2323 return V8::IsGlobalWeak(reinterpret_cast<void**>(**this));
2324}
2325
2326
2327template <class T>
2328void Persistent<T>::Dispose() {
2329 if (this->IsEmpty()) return;
2330 V8::DisposeGlobal(reinterpret_cast<void**>(**this));
2331}
2332
2333
2334template <class T>
2335Persistent<T>::Persistent() : Handle<T>() { }
2336
2337template <class T>
2338void Persistent<T>::MakeWeak(void* parameters, WeakReferenceCallback callback) {
2339 V8::MakeWeak(reinterpret_cast<void**>(**this), parameters, callback);
2340}
2341
2342template <class T>
2343void Persistent<T>::ClearWeak() {
2344 V8::ClearWeak(reinterpret_cast<void**>(**this));
2345}
2346
2347template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00002348T* Handle<T>::operator->() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002349 return val_;
2350}
2351
2352
2353template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00002354T* Handle<T>::operator*() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002355 return val_;
2356}
2357
2358
2359Local<Value> Arguments::operator[](int i) const {
2360 if (i < 0 || length_ <= i) return Local<Value>(*Undefined());
2361 return Local<Value>(reinterpret_cast<Value*>(values_ - i));
2362}
2363
2364
2365Local<Function> Arguments::Callee() const {
2366 return callee_;
2367}
2368
2369
2370Local<Object> Arguments::This() const {
2371 return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
2372}
2373
2374
2375Local<Object> Arguments::Holder() const {
2376 return holder_;
2377}
2378
2379
2380Local<Value> Arguments::Data() const {
2381 return data_;
2382}
2383
2384
2385bool Arguments::IsConstructCall() const {
2386 return is_construct_call_;
2387}
2388
2389
2390int Arguments::Length() const {
2391 return length_;
2392}
2393
2394
2395Local<Value> AccessorInfo::Data() const {
2396 return data_;
2397}
2398
2399
2400Local<Object> AccessorInfo::This() const {
2401 return self_;
2402}
2403
2404
2405Local<Object> AccessorInfo::Holder() const {
2406 return holder_;
2407}
2408
2409
2410template <class T>
2411Local<T> HandleScope::Close(Handle<T> value) {
2412 void** after = RawClose(reinterpret_cast<void**>(*value));
2413 return Local<T>(reinterpret_cast<T*>(after));
2414}
2415
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002416Handle<Value> ScriptOrigin::ResourceName() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002417 return resource_name_;
2418}
2419
2420
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002421Handle<Integer> ScriptOrigin::ResourceLineOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002422 return resource_line_offset_;
2423}
2424
2425
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002426Handle<Integer> ScriptOrigin::ResourceColumnOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002427 return resource_column_offset_;
2428}
2429
2430
2431Handle<Boolean> Boolean::New(bool value) {
2432 return value ? True() : False();
2433}
2434
2435
2436void Template::Set(const char* name, v8::Handle<Data> value) {
2437 Set(v8::String::New(name), value);
2438}
2439
2440
2441/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002442 * \example shell.cc
2443 * A simple shell that takes a list of expressions on the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002444 * command-line and executes them.
2445 */
2446
2447
2448/**
2449 * \example process.cc
2450 */
2451
2452
2453} // namespace v8
2454
2455
2456#undef EXPORT
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002457#undef EXPORT_INLINE
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002458#undef TYPE_CHECK
2459
2460
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002461#endif // V8_H_