v8  3.14.5(node0.10.28)
V8 is Google's open source JavaScript engine
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
bootstrapper.h
Go to the documentation of this file.
1 // Copyright 2006-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 
29 #ifndef V8_BOOTSTRAPPER_H_
30 #define V8_BOOTSTRAPPER_H_
31 
32 #include "allocation.h"
33 
34 namespace v8 {
35 namespace internal {
36 
37 
38 // A SourceCodeCache uses a FixedArray to store pairs of
39 // (AsciiString*, JSFunction*), mapping names of native code files
40 // (runtime.js, etc.) to precompiled functions. Instead of mapping
41 // names to functions it might make sense to let the JS2C tool
42 // generate an index for each native JS file.
43 class SourceCodeCache BASE_EMBEDDED {
44  public:
45  explicit SourceCodeCache(Script::Type type): type_(type), cache_(NULL) { }
46 
47  void Initialize(bool create_heap_objects) {
48  cache_ = create_heap_objects ? HEAP->empty_fixed_array() : NULL;
49  }
50 
51  void Iterate(ObjectVisitor* v) {
52  v->VisitPointer(BitCast<Object**, FixedArray**>(&cache_));
53  }
54 
56  for (int i = 0; i < cache_->length(); i+=2) {
57  SeqAsciiString* str = SeqAsciiString::cast(cache_->get(i));
58  if (str->IsEqualTo(name)) {
60  SharedFunctionInfo::cast(cache_->get(i + 1)));
61  return true;
62  }
63  }
64  return false;
65  }
66 
68  HandleScope scope;
69  int length = cache_->length();
70  Handle<FixedArray> new_array =
71  FACTORY->NewFixedArray(length + 2, TENURED);
72  cache_->CopyTo(0, *new_array, 0, cache_->length());
73  cache_ = *new_array;
74  Handle<String> str = FACTORY->NewStringFromAscii(name, TENURED);
75  cache_->set(length, *str);
76  cache_->set(length + 1, *shared);
77  Script::cast(shared->script())->set_type(Smi::FromInt(type_));
78  }
79 
80  private:
81  Script::Type type_;
82  FixedArray* cache_;
83  DISALLOW_COPY_AND_ASSIGN(SourceCodeCache);
84 };
85 
86 
87 // The Boostrapper is the public interface for creating a JavaScript global
88 // context.
89 class Bootstrapper {
90  public:
91  // Requires: Heap::SetUp has been called.
92  void Initialize(bool create_heap_objects);
93  void TearDown();
94 
95  // Creates a JavaScript Global Context with initial object graph.
96  // The returned value is a global handle casted to V8Environment*.
98  Isolate* isolate,
99  Handle<Object> global_object,
100  v8::Handle<v8::ObjectTemplate> global_template,
101  v8::ExtensionConfiguration* extensions);
102 
103  // Detach the environment from its outer global object.
104  void DetachGlobal(Handle<Context> env);
105 
106  // Reattach an outer global object to an environment.
107  void ReattachGlobal(Handle<Context> env, Handle<JSGlobalProxy> global_proxy);
108 
109  // Traverses the pointers for memory management.
110  void Iterate(ObjectVisitor* v);
111 
112  // Accessor for the native scripts source code.
114 
115  // Tells whether bootstrapping is active.
116  bool IsActive() const { return nesting_ != 0; }
117 
118  // Support for thread preemption.
119  static int ArchiveSpacePerThread();
120  char* ArchiveState(char* to);
121  char* RestoreState(char* from);
122  void FreeThreadResources();
123 
124  // This will allocate a char array that is deleted when V8 is shut down.
125  // It should only be used for strictly finite allocations.
126  char* AllocateAutoDeletedArray(int bytes);
127 
128  // Used for new context creation.
129  bool InstallExtensions(Handle<Context> native_context,
130  v8::ExtensionConfiguration* extensions);
131 
132  SourceCodeCache* extensions_cache() { return &extensions_cache_; }
133 
134  private:
135  typedef int NestingCounterType;
136  NestingCounterType nesting_;
137  SourceCodeCache extensions_cache_;
138  // This is for delete, not delete[].
139  List<char*>* delete_these_non_arrays_on_tear_down_;
140  // This is for delete[]
141  List<char*>* delete_these_arrays_on_tear_down_;
142 
143  friend class BootstrapperActive;
144  friend class Isolate;
146 
147  Bootstrapper();
148 
150 };
151 
152 
153 class BootstrapperActive BASE_EMBEDDED {
154  public:
156  ++Isolate::Current()->bootstrapper()->nesting_;
157  }
158 
160  --Isolate::Current()->bootstrapper()->nesting_;
161  }
162 
163  private:
164  DISALLOW_COPY_AND_ASSIGN(BootstrapperActive);
165 };
166 
167 
170  public:
172  const char* source,
173  size_t length);
174 
175  const char* data() const {
176  return data_;
177  }
178 
179  size_t length() const {
180  return length_;
181  }
182  private:
183  const char* data_;
184  size_t length_;
185 };
186 
187 }} // namespace v8::internal
188 
189 #endif // V8_BOOTSTRAPPER_H_
char * ArchiveState(char *to)
void Initialize(bool create_heap_objects)
Definition: bootstrapper.cc:95
char * AllocateAutoDeletedArray(int bytes)
static Smi * FromInt(int value)
Definition: objects-inl.h:981
SourceCodeCache * extensions_cache()
Definition: bootstrapper.h:132
char * RestoreState(char *from)
static Script * cast(Object *obj)
bool InstallExtensions(Handle< Context > native_context, v8::ExtensionConfiguration *extensions)
static SharedFunctionInfo * cast(Object *obj)
bool Lookup(Vector< const char > name, Handle< SharedFunctionInfo > *handle)
Definition: bootstrapper.h:55
friend class BootstrapperActive
Definition: bootstrapper.h:143
static SeqAsciiString * cast(Object *obj)
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition: globals.h:307
Handle< String > NativesSourceLookup(int index)
Definition: bootstrapper.cc:74
#define BASE_EMBEDDED
Definition: allocation.h:68
void Initialize(bool create_heap_objects)
Definition: bootstrapper.h:47
void Add(Vector< const char > name, Handle< SharedFunctionInfo > shared)
Definition: bootstrapper.h:67
Handle< Context > CreateEnvironment(Isolate *isolate, Handle< Object > global_object, v8::Handle< v8::ObjectTemplate > global_template, v8::ExtensionConfiguration *extensions)
void Iterate(ObjectVisitor *v)
void DetachGlobal(Handle< Context > env)
void Iterate(ObjectVisitor *v)
Definition: bootstrapper.h:51
#define HEAP
Definition: isolate.h:1433
#define FACTORY
Definition: isolate.h:1434
activate correct semantics for inheriting readonliness enable harmony semantics for typeof enable harmony enable harmony proxies enable all harmony harmony_scoping harmony_proxies harmony_scoping tracks arrays with only smi values automatically unbox arrays of doubles use crankshaft use hydrogen range analysis use hydrogen global value numbering use function inlining maximum number of AST nodes considered for a single inlining loop invariant code motion print statistics for hydrogen trace generated IR for specified phases trace register allocator trace range analysis trace representation types environment for every instruction put a break point before deoptimizing polymorphic inlining perform array bounds checks elimination use dead code elimination trace on stack replacement optimize closures cache optimized code for closures functions with arguments object loop weight for representation inference allow uint32 values on optimize frames if they are used only in safe operations track parallel recompilation enable all profiler experiments number of stack frames inspected by the profiler call recompile stub directly when self optimizing trigger profiler ticks based on counting instead of timing weight back edges by jump distance for interrupt triggering percentage of ICs that must have type info to allow optimization watch_ic_patching retry_self_opt interrupt_at_exit extra verbose compilation tracing generate extra emit comments in code disassembly enable use of SSE3 instructions if available enable use of CMOV instruction if available enable use of SAHF instruction if enable use of VFP3 instructions if available this implies enabling ARMv7 and VFP2 enable use of VFP2 instructions if available enable use of SDIV and UDIV instructions if enable loading bit constant by means of movw movt instruction enable unaligned accesses for enable use of MIPS FPU instructions if NULL
Definition: flags.cc:301
bool IsEqualTo(Vector< const char > str)
Definition: objects.cc:7163
void ReattachGlobal(Handle< Context > env, Handle< JSGlobalProxy > global_proxy)
SourceCodeCache(Script::Type type)
Definition: bootstrapper.h:45
NativesExternalStringResource(Bootstrapper *bootstrapper, const char *source, size_t length)
Definition: bootstrapper.cc:51
static int ArchiveSpacePerThread()