blob: 1221f352cc678a35def37a2316c09a13814b3ace [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2008 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#ifndef V8_API_H_
29#define V8_API_H_
30
31#include "apiutils.h"
32#include "factory.h"
33
34namespace v8 {
35
36// Constants used in the implementation of the API. The most natural thing
37// would usually be to place these with the classes that use them, but
38// we want to keep them out of v8.h because it is an externally
39// visible file.
40class Consts {
41 public:
42 enum TemplateType {
43 FUNCTION_TEMPLATE = 0,
44 OBJECT_TEMPLATE = 1
45 };
46};
47
48
49// Utilities for working with neander-objects, primitive
50// env-independent JSObjects used by the api.
51class NeanderObject {
52 public:
53 explicit NeanderObject(int size);
54 inline NeanderObject(v8::internal::Handle<v8::internal::Object> obj);
55 inline NeanderObject(v8::internal::Object* obj);
56 inline v8::internal::Object* get(int index);
57 inline void set(int index, v8::internal::Object* value);
58 inline v8::internal::Handle<v8::internal::JSObject> value() { return value_; }
59 int size();
60 private:
61 v8::internal::Handle<v8::internal::JSObject> value_;
62};
63
64
65// Utilities for working with neander-arrays, a simple extensible
66// array abstraction built on neander-objects.
67class NeanderArray {
68 public:
69 NeanderArray();
70 inline NeanderArray(v8::internal::Handle<v8::internal::Object> obj);
71 inline v8::internal::Handle<v8::internal::JSObject> value() {
72 return obj_.value();
73 }
74
75 void add(v8::internal::Handle<v8::internal::Object> value);
76
77 int length();
78
79 v8::internal::Object* get(int index);
80 // Change the value at an index to undefined value. If the index is
81 // out of bounds, the request is ignored. Returns the old value.
82 void set(int index, v8::internal::Object* value);
83 private:
84 NeanderObject obj_;
85};
86
87
88NeanderObject::NeanderObject(v8::internal::Handle<v8::internal::Object> obj)
89 : value_(v8::internal::Handle<v8::internal::JSObject>::cast(obj)) { }
90
91
92NeanderObject::NeanderObject(v8::internal::Object* obj)
93 : value_(v8::internal::Handle<v8::internal::JSObject>(
94 v8::internal::JSObject::cast(obj))) { }
95
96
97NeanderArray::NeanderArray(v8::internal::Handle<v8::internal::Object> obj)
98 : obj_(obj) { }
99
100
101v8::internal::Object* NeanderObject::get(int offset) {
102 ASSERT(value()->HasFastElements());
103 return v8::internal::FixedArray::cast(value()->elements())->get(offset);
104}
105
106
107void NeanderObject::set(int offset, v8::internal::Object* value) {
108 ASSERT(value_->HasFastElements());
109 v8::internal::FixedArray::cast(value_->elements())->set(offset, value);
110}
111
112
113template <typename T> static inline T ToCData(v8::internal::Object* obj) {
114 STATIC_ASSERT(sizeof(T) == sizeof(v8::internal::Address));
115 return reinterpret_cast<T>(
116 reinterpret_cast<intptr_t>(v8::internal::Proxy::cast(obj)->proxy()));
117}
118
119
120template <typename T>
121static inline v8::internal::Handle<v8::internal::Object> FromCData(T obj) {
122 STATIC_ASSERT(sizeof(T) == sizeof(v8::internal::Address));
123 return v8::internal::Factory::NewProxy(
124 reinterpret_cast<v8::internal::Address>(reinterpret_cast<intptr_t>(obj)));
125}
126
127
128v8::Arguments::Arguments(v8::Local<v8::Value> data,
129 v8::Local<v8::Object> holder,
130 v8::Local<v8::Function> callee,
131 bool is_construct_call,
132 void** values, int length)
133 : data_(data), holder_(holder), callee_(callee),
134 is_construct_call_(is_construct_call),
135 values_(values), length_(length) { }
136
137
138enum ExtensionTraversalState {
139 UNVISITED, VISITED, INSTALLED
140};
141
142
143class RegisteredExtension {
144 public:
145 explicit RegisteredExtension(Extension* extension);
146 static void Register(RegisteredExtension* that);
147 Extension* extension() { return extension_; }
148 RegisteredExtension* next() { return next_; }
149 RegisteredExtension* next_auto() { return next_auto_; }
150 ExtensionTraversalState state() { return state_; }
151 void set_state(ExtensionTraversalState value) { state_ = value; }
152 static RegisteredExtension* first_extension() { return first_extension_; }
153 private:
154 Extension* extension_;
155 RegisteredExtension* next_;
156 RegisteredExtension* next_auto_;
157 ExtensionTraversalState state_;
158 static RegisteredExtension* first_extension_;
159 static RegisteredExtension* first_auto_extension_;
160};
161
162
163class Utils {
164 public:
165 static bool ReportApiFailure(const char* location, const char* message);
166
167 static Local<FunctionTemplate> ToFunctionTemplate(NeanderObject obj);
168 static Local<ObjectTemplate> ToObjectTemplate(NeanderObject obj);
169
170 static inline Local<Context> ToLocal(
171 v8::internal::Handle<v8::internal::Context> obj);
172 static inline Local<Value> ToLocal(
173 v8::internal::Handle<v8::internal::Object> obj);
174 static inline Local<Function> ToLocal(
175 v8::internal::Handle<v8::internal::JSFunction> obj);
176 static inline Local<String> ToLocal(
177 v8::internal::Handle<v8::internal::String> obj);
178 static inline Local<Object> ToLocal(
179 v8::internal::Handle<v8::internal::JSObject> obj);
180 static inline Local<Array> ToLocal(
181 v8::internal::Handle<v8::internal::JSArray> obj);
182 static inline Local<External> ToLocal(
183 v8::internal::Handle<v8::internal::Proxy> obj);
184 static inline Local<Message> MessageToLocal(
185 v8::internal::Handle<v8::internal::Object> obj);
186 static inline Local<Number> NumberToLocal(
187 v8::internal::Handle<v8::internal::Object> obj);
188 static inline Local<Integer> IntegerToLocal(
189 v8::internal::Handle<v8::internal::Object> obj);
190 static inline Local<Uint32> Uint32ToLocal(
191 v8::internal::Handle<v8::internal::Object> obj);
192 static inline Local<FunctionTemplate> ToLocal(
193 v8::internal::Handle<v8::internal::FunctionTemplateInfo> obj);
194 static inline Local<ObjectTemplate> ToLocal(
195 v8::internal::Handle<v8::internal::ObjectTemplateInfo> obj);
196 static inline Local<Signature> ToLocal(
197 v8::internal::Handle<v8::internal::SignatureInfo> obj);
198 static inline Local<TypeSwitch> ToLocal(
199 v8::internal::Handle<v8::internal::TypeSwitchInfo> obj);
200
201 static inline v8::internal::Handle<v8::internal::TemplateInfo>
202 OpenHandle(const Template* that);
203 static inline v8::internal::Handle<v8::internal::FunctionTemplateInfo>
204 OpenHandle(const FunctionTemplate* that);
205 static inline v8::internal::Handle<v8::internal::ObjectTemplateInfo>
206 OpenHandle(const ObjectTemplate* that);
207 static inline v8::internal::Handle<v8::internal::Object>
208 OpenHandle(const Data* data);
209 static inline v8::internal::Handle<v8::internal::JSObject>
210 OpenHandle(const v8::Object* data);
211 static inline v8::internal::Handle<v8::internal::JSArray>
212 OpenHandle(const v8::Array* data);
213 static inline v8::internal::Handle<v8::internal::String>
214 OpenHandle(const String* data);
215 static inline v8::internal::Handle<v8::internal::JSFunction>
216 OpenHandle(const Script* data);
217 static inline v8::internal::Handle<v8::internal::JSFunction>
218 OpenHandle(const Function* data);
219 static inline v8::internal::Handle<v8::internal::JSObject>
220 OpenHandle(const Message* message);
221 static inline v8::internal::Handle<v8::internal::Context>
222 OpenHandle(const v8::Context* context);
223 static inline v8::internal::Handle<v8::internal::SignatureInfo>
224 OpenHandle(const v8::Signature* sig);
225 static inline v8::internal::Handle<v8::internal::TypeSwitchInfo>
226 OpenHandle(const v8::TypeSwitch* that);
227 static inline v8::internal::Handle<v8::internal::Proxy>
228 OpenHandle(const v8::External* that);
229};
230
231
232template <class T>
233static inline T* ToApi(v8::internal::Handle<v8::internal::Object> obj) {
234 return reinterpret_cast<T*>(obj.location());
235}
236
237
238template <class T>
239v8::internal::Handle<T> v8::internal::Handle<T>::EscapeFrom(
240 v8::HandleScope* scope) {
241 return Utils::OpenHandle(*scope->Close(Utils::ToLocal(*this)));
242}
243
244
245// Implementations of ToLocal
246
247#define MAKE_TO_LOCAL(Name, From, To) \
248 Local<v8::To> Utils::Name(v8::internal::Handle<v8::internal::From> obj) { \
249 ASSERT(!obj->IsTheHole()); \
250 return Local<To>(reinterpret_cast<To*>(obj.location())); \
251 }
252
253MAKE_TO_LOCAL(ToLocal, Context, Context)
254MAKE_TO_LOCAL(ToLocal, Object, Value)
255MAKE_TO_LOCAL(ToLocal, JSFunction, Function)
256MAKE_TO_LOCAL(ToLocal, String, String)
257MAKE_TO_LOCAL(ToLocal, JSObject, Object)
258MAKE_TO_LOCAL(ToLocal, JSArray, Array)
259MAKE_TO_LOCAL(ToLocal, Proxy, External)
260MAKE_TO_LOCAL(ToLocal, FunctionTemplateInfo, FunctionTemplate)
261MAKE_TO_LOCAL(ToLocal, ObjectTemplateInfo, ObjectTemplate)
262MAKE_TO_LOCAL(ToLocal, SignatureInfo, Signature)
263MAKE_TO_LOCAL(ToLocal, TypeSwitchInfo, TypeSwitch)
264MAKE_TO_LOCAL(MessageToLocal, Object, Message)
265MAKE_TO_LOCAL(NumberToLocal, Object, Number)
266MAKE_TO_LOCAL(IntegerToLocal, Object, Integer)
267MAKE_TO_LOCAL(Uint32ToLocal, Object, Uint32)
268
269#undef MAKE_TO_LOCAL
270
271
272// Implementations of OpenHandle
273
274#define MAKE_OPEN_HANDLE(From, To) \
275 v8::internal::Handle<v8::internal::To> Utils::OpenHandle(\
276 const v8::From* that) { \
277 return v8::internal::Handle<v8::internal::To>( \
278 reinterpret_cast<v8::internal::To**>(const_cast<v8::From*>(that))); \
279 }
280
281MAKE_OPEN_HANDLE(Template, TemplateInfo)
282MAKE_OPEN_HANDLE(FunctionTemplate, FunctionTemplateInfo)
283MAKE_OPEN_HANDLE(ObjectTemplate, ObjectTemplateInfo)
284MAKE_OPEN_HANDLE(Signature, SignatureInfo)
285MAKE_OPEN_HANDLE(TypeSwitch, TypeSwitchInfo)
286MAKE_OPEN_HANDLE(Data, Object)
287MAKE_OPEN_HANDLE(Object, JSObject)
288MAKE_OPEN_HANDLE(Array, JSArray)
289MAKE_OPEN_HANDLE(String, String)
290MAKE_OPEN_HANDLE(Script, JSFunction)
291MAKE_OPEN_HANDLE(Function, JSFunction)
292MAKE_OPEN_HANDLE(Message, JSObject)
293MAKE_OPEN_HANDLE(Context, Context)
294MAKE_OPEN_HANDLE(External, Proxy)
295
296#undef MAKE_OPEN_HANDLE
297
298
299namespace internal {
300
301// This class is here in order to be able to declare it a friend of
302// HandleScope. Moving these methods to be members of HandleScope would be
303// neat in some ways, but it would expose external implementation details in
304// our public header file, which is undesirable.
305//
306// There is a singleton instance of this class to hold the per-thread data.
307// For multithreaded V8 programs this data is copied in and out of storage
308// so that the currently executing thread always has its own copy of this
309// data.
310class HandleScopeImplementer {
311 public:
312
313 HandleScopeImplementer()
314 : blocks_(0),
315 entered_contexts_(0),
316 saved_contexts_(0),
317 spare_(NULL),
318 ignore_out_of_memory_(false),
319 call_depth_(0) { }
320
321 static HandleScopeImplementer* instance();
322
323 // Threading support for handle data.
324 static int ArchiveSpacePerThread();
325 static char* RestoreThread(char* from);
326 static char* ArchiveThread(char* to);
327 static void FreeThreadResources();
328
329 // Garbage collection support.
330 static void Iterate(v8::internal::ObjectVisitor* v);
331 static char* Iterate(v8::internal::ObjectVisitor* v, char* data);
332
333
334 inline internal::Object** GetSpareOrNewBlock();
335 inline void DeleteExtensions(int extensions);
336
337 inline void IncrementCallDepth() {call_depth_++;}
338 inline void DecrementCallDepth() {call_depth_--;}
339 inline bool CallDepthIsZero() { return call_depth_ == 0; }
340
341 inline void EnterContext(Handle<Object> context);
342 inline bool LeaveLastContext();
343
344 // Returns the last entered context or an empty handle if no
345 // contexts have been entered.
346 inline Handle<Object> LastEnteredContext();
347
348 inline void SaveContext(Context* context);
349 inline Context* RestoreContext();
350 inline bool HasSavedContexts();
351
352 inline List<internal::Object**>* blocks() { return &blocks_; }
353 inline bool ignore_out_of_memory() { return ignore_out_of_memory_; }
354 inline void set_ignore_out_of_memory(bool value) {
355 ignore_out_of_memory_ = value;
356 }
357
358 private:
359 void ResetAfterArchive() {
360 blocks_.Initialize(0);
361 entered_contexts_.Initialize(0);
362 saved_contexts_.Initialize(0);
363 spare_ = NULL;
364 ignore_out_of_memory_ = false;
365 call_depth_ = 0;
366 }
367
368 void Free() {
369 ASSERT(blocks_.length() == 0);
370 ASSERT(entered_contexts_.length() == 0);
371 ASSERT(saved_contexts_.length() == 0);
372 blocks_.Free();
373 entered_contexts_.Free();
374 saved_contexts_.Free();
375 if (spare_ != NULL) {
376 DeleteArray(spare_);
377 spare_ = NULL;
378 }
379 ASSERT(call_depth_ == 0);
380 }
381
382 List<internal::Object**> blocks_;
383 // Used as a stack to keep track of entered contexts.
384 List<Handle<Object> > entered_contexts_;
385 // Used as a stack to keep track of saved contexts.
386 List<Context*> saved_contexts_;
387 Object** spare_;
388 bool ignore_out_of_memory_;
389 int call_depth_;
390 // This is only used for threading support.
391 v8::ImplementationUtilities::HandleScopeData handle_scope_data_;
392
393 void IterateThis(ObjectVisitor* v);
394 char* RestoreThreadHelper(char* from);
395 char* ArchiveThreadHelper(char* to);
396
397 DISALLOW_COPY_AND_ASSIGN(HandleScopeImplementer);
398};
399
400
401static const int kHandleBlockSize = v8::internal::KB - 2; // fit in one page
402
403
404void HandleScopeImplementer::SaveContext(Context* context) {
405 saved_contexts_.Add(context);
406}
407
408
409Context* HandleScopeImplementer::RestoreContext() {
410 return saved_contexts_.RemoveLast();
411}
412
413
414bool HandleScopeImplementer::HasSavedContexts() {
415 return !saved_contexts_.is_empty();
416}
417
418
419void HandleScopeImplementer::EnterContext(Handle<Object> context) {
420 entered_contexts_.Add(context);
421}
422
423
424bool HandleScopeImplementer::LeaveLastContext() {
425 if (entered_contexts_.is_empty()) return false;
426 entered_contexts_.RemoveLast();
427 return true;
428}
429
430
431Handle<Object> HandleScopeImplementer::LastEnteredContext() {
432 if (entered_contexts_.is_empty()) return Handle<Object>::null();
433 return entered_contexts_.last();
434}
435
436
437// If there's a spare block, use it for growing the current scope.
438internal::Object** HandleScopeImplementer::GetSpareOrNewBlock() {
439 internal::Object** block = (spare_ != NULL) ?
440 spare_ :
441 NewArray<internal::Object*>(kHandleBlockSize);
442 spare_ = NULL;
443 return block;
444}
445
446
447void HandleScopeImplementer::DeleteExtensions(int extensions) {
448 if (spare_ != NULL) {
449 DeleteArray(spare_);
450 spare_ = NULL;
451 }
452 for (int i = extensions; i > 1; --i) {
453 internal::Object** block = blocks_.RemoveLast();
454#ifdef DEBUG
455 v8::ImplementationUtilities::ZapHandleRange(block,
456 &block[kHandleBlockSize]);
457#endif
458 DeleteArray(block);
459 }
460 spare_ = blocks_.RemoveLast();
461#ifdef DEBUG
462 v8::ImplementationUtilities::ZapHandleRange(
463 spare_,
464 &spare_[kHandleBlockSize]);
465#endif
466}
467
468} } // namespace v8::internal
469
470#endif // V8_API_H_