blob: 484392696c2a38fad09e44e7d7d293e9aaf3380f [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 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
28#ifndef V8_API_H_
29#define V8_API_H_
30
31#include "factory.h"
32
33namespace v8 {
34
35// Constants used in the implementation of the API. The most natural thing
36// would usually be to place these with the classes that use them, but
37// we want to keep them out of v8.h because it is an externally
38// visible file.
39class Consts {
40 public:
41 enum TemplateType {
42 FUNCTION_TEMPLATE = 0,
43 OBJECT_TEMPLATE = 1
44 };
45};
46
47
48// Utilities for working with neander-objects, primitive
49// env-independent JSObjects used by the api.
50class NeanderObject {
51 public:
52 explicit NeanderObject(int size);
53 inline NeanderObject(v8::internal::Handle<v8::internal::Object> obj);
54 inline NeanderObject(v8::internal::Object* obj);
55 inline v8::internal::Object* get(int index);
56 inline void set(int index, v8::internal::Object* value);
57 inline v8::internal::Handle<v8::internal::JSObject> value() { return value_; }
58 int size();
59 private:
60 v8::internal::Handle<v8::internal::JSObject> value_;
61};
62
63
64// Utilities for working with neander-arrays, a simple extensible
65// array abstraction built on neander-objects.
66class NeanderArray {
67 public:
68 NeanderArray();
69 inline NeanderArray(v8::internal::Handle<v8::internal::Object> obj);
70 inline v8::internal::Handle<v8::internal::JSObject> value() {
71 return obj_.value();
72 }
73
74 void add(v8::internal::Handle<v8::internal::Object> value);
75
76 int length();
77
78 v8::internal::Object* get(int index);
79 // Change the value at an index to undefined value. If the index is
80 // out of bounds, the request is ignored. Returns the old value.
81 void set(int index, v8::internal::Object* value);
82 private:
83 NeanderObject obj_;
84};
85
86
87NeanderObject::NeanderObject(v8::internal::Handle<v8::internal::Object> obj)
88 : value_(v8::internal::Handle<v8::internal::JSObject>::cast(obj)) { }
89
90
91NeanderObject::NeanderObject(v8::internal::Object* obj)
92 : value_(v8::internal::Handle<v8::internal::JSObject>(
93 v8::internal::JSObject::cast(obj))) { }
94
95
96NeanderArray::NeanderArray(v8::internal::Handle<v8::internal::Object> obj)
97 : obj_(obj) { }
98
99
100v8::internal::Object* NeanderObject::get(int offset) {
101 ASSERT(value()->HasFastElements());
102 return v8::internal::FixedArray::cast(value()->elements())->get(offset);
103}
104
105
106void NeanderObject::set(int offset, v8::internal::Object* value) {
107 ASSERT(value_->HasFastElements());
108 v8::internal::FixedArray::cast(value_->elements())->set(offset, value);
109}
110
111
112template <typename T> static inline T ToCData(v8::internal::Object* obj) {
113 STATIC_ASSERT(sizeof(T) == sizeof(v8::internal::Address));
114 return reinterpret_cast<T>(
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000115 reinterpret_cast<intptr_t>(v8::internal::Proxy::cast(obj)->proxy()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000116}
117
118
119template <typename T>
120static inline v8::internal::Handle<v8::internal::Object> FromCData(T obj) {
121 STATIC_ASSERT(sizeof(T) == sizeof(v8::internal::Address));
122 return v8::internal::Factory::NewProxy(
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000123 reinterpret_cast<v8::internal::Address>(reinterpret_cast<intptr_t>(obj)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000124}
125
126
127v8::Arguments::Arguments(v8::Local<v8::Value> data,
128 v8::Local<v8::Object> holder,
129 v8::Local<v8::Function> callee,
130 bool is_construct_call,
131 void** values, int length)
132 : data_(data), holder_(holder), callee_(callee),
133 is_construct_call_(is_construct_call),
134 values_(values), length_(length) { }
135
136
137enum ExtensionTraversalState {
138 UNVISITED, VISITED, INSTALLED
139};
140
141
142class RegisteredExtension {
143 public:
144 explicit RegisteredExtension(Extension* extension);
145 static void Register(RegisteredExtension* that);
146 Extension* extension() { return extension_; }
147 RegisteredExtension* next() { return next_; }
148 RegisteredExtension* next_auto() { return next_auto_; }
149 ExtensionTraversalState state() { return state_; }
150 void set_state(ExtensionTraversalState value) { state_ = value; }
151 static RegisteredExtension* first_extension() { return first_extension_; }
152 private:
153 Extension* extension_;
154 RegisteredExtension* next_;
155 RegisteredExtension* next_auto_;
156 ExtensionTraversalState state_;
157 static RegisteredExtension* first_extension_;
158 static RegisteredExtension* first_auto_extension_;
159};
160
161
162class ImplementationUtilities {
163 public:
164 static v8::Handle<v8::Primitive> Undefined();
165 static v8::Handle<v8::Primitive> Null();
166 static v8::Handle<v8::Boolean> True();
167 static v8::Handle<v8::Boolean> False();
168
169 static int GetNameCount(ExtensionConfiguration* that) {
170 return that->name_count_;
171 }
172
173 static const char** GetNames(ExtensionConfiguration* that) {
174 return that->names_;
175 }
176
177 static v8::Arguments NewArguments(Local<Value> data,
178 Local<Object> holder,
179 Local<Function> callee,
180 bool is_construct_call,
181 void** argv, int argc) {
182 return v8::Arguments(data, holder, callee, is_construct_call, argv, argc);
183 }
184
185 // Introduce an alias for the handle scope data to allow non-friends
186 // to access the HandleScope data.
187 typedef v8::HandleScope::Data HandleScopeData;
188
189 static HandleScopeData* CurrentHandleScope() {
190 return &v8::HandleScope::current_;
191 }
192
193#ifdef DEBUG
194 static void ZapHandleRange(void** begin, void** end) {
195 v8::HandleScope::ZapRange(begin, end);
196 }
197#endif
198};
199
200
201class Utils {
202 public:
203 static bool ReportApiFailure(const char* location, const char* message);
204
205 static Local<FunctionTemplate> ToFunctionTemplate(NeanderObject obj);
206 static Local<ObjectTemplate> ToObjectTemplate(NeanderObject obj);
207
208 static inline Local<Context> ToLocal(
209 v8::internal::Handle<v8::internal::Context> obj);
210 static inline Local<Value> ToLocal(
211 v8::internal::Handle<v8::internal::Object> obj);
212 static inline Local<Function> ToLocal(
213 v8::internal::Handle<v8::internal::JSFunction> obj);
214 static inline Local<String> ToLocal(
215 v8::internal::Handle<v8::internal::String> obj);
216 static inline Local<Object> ToLocal(
217 v8::internal::Handle<v8::internal::JSObject> obj);
218 static inline Local<Array> ToLocal(
219 v8::internal::Handle<v8::internal::JSArray> obj);
220 static inline Local<External> ToLocal(
221 v8::internal::Handle<v8::internal::Proxy> obj);
222 static inline Local<Message> MessageToLocal(
223 v8::internal::Handle<v8::internal::Object> obj);
224 static inline Local<Number> NumberToLocal(
225 v8::internal::Handle<v8::internal::Object> obj);
226 static inline Local<Integer> IntegerToLocal(
227 v8::internal::Handle<v8::internal::Object> obj);
228 static inline Local<Uint32> Uint32ToLocal(
229 v8::internal::Handle<v8::internal::Object> obj);
230 static inline Local<FunctionTemplate> ToLocal(
231 v8::internal::Handle<v8::internal::FunctionTemplateInfo> obj);
232 static inline Local<ObjectTemplate> ToLocal(
233 v8::internal::Handle<v8::internal::ObjectTemplateInfo> obj);
234 static inline Local<Signature> ToLocal(
235 v8::internal::Handle<v8::internal::SignatureInfo> obj);
236 static inline Local<TypeSwitch> ToLocal(
237 v8::internal::Handle<v8::internal::TypeSwitchInfo> obj);
238
239 static inline v8::internal::Handle<v8::internal::TemplateInfo>
240 OpenHandle(Template* that);
241 static inline v8::internal::Handle<v8::internal::FunctionTemplateInfo>
242 OpenHandle(FunctionTemplate* that);
243 static inline v8::internal::Handle<v8::internal::ObjectTemplateInfo>
244 OpenHandle(ObjectTemplate* that);
245 static inline v8::internal::Handle<v8::internal::Object>
246 OpenHandle(Data* data);
247 static inline v8::internal::Handle<v8::internal::JSObject>
248 OpenHandle(v8::Object* data);
249 static inline v8::internal::Handle<v8::internal::JSArray>
250 OpenHandle(v8::Array* data);
251 static inline v8::internal::Handle<v8::internal::String>
252 OpenHandle(String* data);
253 static inline v8::internal::Handle<v8::internal::JSFunction>
254 OpenHandle(Script* data);
255 static inline v8::internal::Handle<v8::internal::JSFunction>
256 OpenHandle(Function* data);
257 static inline v8::internal::Handle<v8::internal::JSObject>
258 OpenHandle(Message* message);
259 static inline v8::internal::Handle<v8::internal::Context>
260 OpenHandle(v8::Context* context);
261 static inline v8::internal::Handle<v8::internal::SignatureInfo>
262 OpenHandle(v8::Signature* sig);
263 static inline v8::internal::Handle<v8::internal::TypeSwitchInfo>
264 OpenHandle(v8::TypeSwitch* that);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000265 static inline v8::internal::Handle<v8::internal::Proxy>
266 OpenHandle(v8::External* that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000267};
268
269
270template <class T>
271static inline T* ToApi(v8::internal::Handle<v8::internal::Object> obj) {
272 return reinterpret_cast<T*>(obj.location());
273}
274
275
276template <class T>
277v8::internal::Handle<T> v8::internal::Handle<T>::EscapeFrom(
278 HandleScope* scope) {
279 return Utils::OpenHandle(*scope->Close(Utils::ToLocal(*this)));
280}
281
282
283// Implementations of ToLocal
284
285#define MAKE_TO_LOCAL(Name, From, To) \
286 Local<v8::To> Utils::Name(v8::internal::Handle<v8::internal::From> obj) { \
287 return Local<To>(reinterpret_cast<To*>(obj.location())); \
288 }
289
290MAKE_TO_LOCAL(ToLocal, Context, Context)
291MAKE_TO_LOCAL(ToLocal, Object, Value)
292MAKE_TO_LOCAL(ToLocal, JSFunction, Function)
293MAKE_TO_LOCAL(ToLocal, String, String)
294MAKE_TO_LOCAL(ToLocal, JSObject, Object)
295MAKE_TO_LOCAL(ToLocal, JSArray, Array)
296MAKE_TO_LOCAL(ToLocal, Proxy, External)
297MAKE_TO_LOCAL(ToLocal, FunctionTemplateInfo, FunctionTemplate)
298MAKE_TO_LOCAL(ToLocal, ObjectTemplateInfo, ObjectTemplate)
299MAKE_TO_LOCAL(ToLocal, SignatureInfo, Signature)
300MAKE_TO_LOCAL(ToLocal, TypeSwitchInfo, TypeSwitch)
301MAKE_TO_LOCAL(MessageToLocal, Object, Message)
302MAKE_TO_LOCAL(NumberToLocal, Object, Number)
303MAKE_TO_LOCAL(IntegerToLocal, Object, Integer)
304MAKE_TO_LOCAL(Uint32ToLocal, Object, Uint32)
305
306#undef MAKE_TO_LOCAL
307
308
309// Implementations of OpenHandle
310
311#define MAKE_OPEN_HANDLE(From, To) \
312 v8::internal::Handle<v8::internal::To> Utils::OpenHandle(v8::From* that) { \
313 return v8::internal::Handle<v8::internal::To>( \
314 reinterpret_cast<v8::internal::To**>(that)); \
315 }
316
317MAKE_OPEN_HANDLE(Template, TemplateInfo)
318MAKE_OPEN_HANDLE(FunctionTemplate, FunctionTemplateInfo)
319MAKE_OPEN_HANDLE(ObjectTemplate, ObjectTemplateInfo)
320MAKE_OPEN_HANDLE(Signature, SignatureInfo)
321MAKE_OPEN_HANDLE(TypeSwitch, TypeSwitchInfo)
322MAKE_OPEN_HANDLE(Data, Object)
323MAKE_OPEN_HANDLE(Object, JSObject)
324MAKE_OPEN_HANDLE(Array, JSArray)
325MAKE_OPEN_HANDLE(String, String)
326MAKE_OPEN_HANDLE(Script, JSFunction)
327MAKE_OPEN_HANDLE(Function, JSFunction)
328MAKE_OPEN_HANDLE(Message, JSObject)
329MAKE_OPEN_HANDLE(Context, Context)
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000330MAKE_OPEN_HANDLE(External, Proxy)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000331
332#undef MAKE_OPEN_HANDLE
333
334
335namespace internal {
336
337// This class is here in order to be able to declare it a friend of
338// HandleScope. Moving these methods to be members of HandleScope would be
339// neat in some ways, but it would expose external implementation details in
340// our public header file, which is undesirable.
341//
342// There is a singleton instance of this class to hold the per-thread data.
343// For multithreaded V8 programs this data is copied in and out of storage
344// so that the currently executing thread always has its own copy of this
345// data.
346class HandleScopeImplementer {
347 public:
348
349 HandleScopeImplementer()
350 : blocks(0),
kasper.lund44510672008-07-25 07:37:58 +0000351 entered_contexts_(0),
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000352 saved_contexts_(0) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000353 Initialize();
354 }
355
356 void Initialize() {
357 blocks.Initialize(0);
kasper.lund44510672008-07-25 07:37:58 +0000358 entered_contexts_.Initialize(0);
359 saved_contexts_.Initialize(0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000360 spare = NULL;
361 ignore_out_of_memory = false;
362 call_depth = 0;
363 }
364
365 static HandleScopeImplementer* instance();
366
367 // Threading support for handle data.
368 static int ArchiveSpacePerThread();
369 static char* RestoreThread(char* from);
370 static char* ArchiveThread(char* to);
371
372 // Garbage collection support.
373 static void Iterate(v8::internal::ObjectVisitor* v);
374 static char* Iterate(v8::internal::ObjectVisitor* v, char* data);
375
376
377 inline void** GetSpareOrNewBlock();
378 inline void DeleteExtensions(int extensions);
379
380 inline void IncrementCallDepth() {call_depth++;}
381 inline void DecrementCallDepth() {call_depth--;}
382 inline bool CallDepthIsZero() { return call_depth == 0; }
383
kasper.lund44510672008-07-25 07:37:58 +0000384 inline void EnterContext(Handle<Object> context);
385 inline bool LeaveLastContext();
386
387 // Returns the last entered context or an empty handle if no
388 // contexts have been entered.
389 inline Handle<Object> LastEnteredContext();
390
391 inline void SaveContext(Handle<Object> context);
392 inline Handle<Object> RestoreContext();
393 inline bool HasSavedContexts();
394
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000395 inline List<void**>* Blocks() { return &blocks; }
396
397 inline bool IgnoreOutOfMemory() { return ignore_out_of_memory; }
398 inline void SetIgnoreOutOfMemory(bool value) { ignore_out_of_memory = value; }
399
400 private:
401 List<void**> blocks;
402 Object** spare;
403 int call_depth;
kasper.lund44510672008-07-25 07:37:58 +0000404 // Used as a stack to keep track of entered contexts.
405 List<Handle<Object> > entered_contexts_;
406 // Used as a stack to keep track of saved contexts.
407 List<Handle<Object> > saved_contexts_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000408 bool ignore_out_of_memory;
409 // This is only used for threading support.
410 ImplementationUtilities::HandleScopeData handle_scope_data_;
411
412 static void Iterate(ObjectVisitor* v,
413 List<void**>* blocks,
414 ImplementationUtilities::HandleScopeData* handle_data);
415 char* RestoreThreadHelper(char* from);
416 char* ArchiveThreadHelper(char* to);
417
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000418 DISALLOW_COPY_AND_ASSIGN(HandleScopeImplementer);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000419};
420
421
422static const int kHandleBlockSize = v8::internal::KB - 2; // fit in one page
423
424
kasper.lund44510672008-07-25 07:37:58 +0000425void HandleScopeImplementer::SaveContext(Handle<Object> context) {
426 saved_contexts_.Add(context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000427}
428
429
kasper.lund44510672008-07-25 07:37:58 +0000430Handle<Object> HandleScopeImplementer::RestoreContext() {
431 return saved_contexts_.RemoveLast();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000432}
433
434
kasper.lund44510672008-07-25 07:37:58 +0000435bool HandleScopeImplementer::HasSavedContexts() {
436 return !saved_contexts_.is_empty();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000437}
438
439
kasper.lund44510672008-07-25 07:37:58 +0000440void HandleScopeImplementer::EnterContext(Handle<Object> context) {
441 entered_contexts_.Add(context);
442}
443
444
445bool HandleScopeImplementer::LeaveLastContext() {
446 if (entered_contexts_.is_empty()) return false;
447 entered_contexts_.RemoveLast();
448 return true;
449}
450
451
452Handle<Object> HandleScopeImplementer::LastEnteredContext() {
453 if (entered_contexts_.is_empty()) return Handle<Object>::null();
454 return entered_contexts_.last();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000455}
456
457
458// If there's a spare block, use it for growing the current scope.
459void** HandleScopeImplementer::GetSpareOrNewBlock() {
460 void** block = (spare != NULL) ?
461 reinterpret_cast<void**>(spare) :
462 NewArray<void*>(kHandleBlockSize);
463 spare = NULL;
464 return block;
465}
466
467
468void HandleScopeImplementer::DeleteExtensions(int extensions) {
469 if (spare != NULL) {
470 DeleteArray(spare);
471 spare = NULL;
472 }
473 for (int i = extensions; i > 1; --i) {
474 void** block = blocks.RemoveLast();
475#ifdef DEBUG
476 ImplementationUtilities::ZapHandleRange(block, &block[kHandleBlockSize]);
477#endif
478 DeleteArray(block);
479 }
480 spare = reinterpret_cast<Object**>(blocks.RemoveLast());
481#ifdef DEBUG
482 ImplementationUtilities::ZapHandleRange(
483 reinterpret_cast<void**>(spare),
484 reinterpret_cast<void**>(&spare[kHandleBlockSize]));
485#endif
486}
487
488} } // namespace v8::internal
489
490#endif // V8_API_H_