Home | History | Annotate | Download | only in src
      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 
     55   bool Lookup(Vector<const char> name, Handle<SharedFunctionInfo>* handle) {
     56     for (int i = 0; i < cache_->length(); i+=2) {
     57       SeqOneByteString* str = SeqOneByteString::cast(cache_->get(i));
     58       if (str->IsUtf8EqualTo(name)) {
     59         *handle = Handle<SharedFunctionInfo>(
     60             SharedFunctionInfo::cast(cache_->get(i + 1)));
     61         return true;
     62       }
     63     }
     64     return false;
     65   }
     66 
     67   void Add(Vector<const char> name, Handle<SharedFunctionInfo> shared) {
     68     Isolate* isolate = shared->GetIsolate();
     69     Factory* factory = isolate->factory();
     70     HandleScope scope(isolate);
     71     int length = cache_->length();
     72     Handle<FixedArray> new_array = factory->NewFixedArray(length + 2, TENURED);
     73     cache_->CopyTo(0, *new_array, 0, cache_->length());
     74     cache_ = *new_array;
     75     Handle<String> str = factory->NewStringFromAscii(name, TENURED);
     76     cache_->set(length, *str);
     77     cache_->set(length + 1, *shared);
     78     Script::cast(shared->script())->set_type(Smi::FromInt(type_));
     79   }
     80 
     81  private:
     82   Script::Type type_;
     83   FixedArray* cache_;
     84   DISALLOW_COPY_AND_ASSIGN(SourceCodeCache);
     85 };
     86 
     87 
     88 // The Boostrapper is the public interface for creating a JavaScript global
     89 // context.
     90 class Bootstrapper {
     91  public:
     92   static void InitializeOncePerProcess();
     93 
     94   // Requires: Heap::SetUp has been called.
     95   void Initialize(bool create_heap_objects);
     96   void TearDown();
     97 
     98   // Creates a JavaScript Global Context with initial object graph.
     99   // The returned value is a global handle casted to V8Environment*.
    100   Handle<Context> CreateEnvironment(
    101       Handle<Object> global_object,
    102       v8::Handle<v8::ObjectTemplate> global_template,
    103       v8::ExtensionConfiguration* extensions);
    104 
    105   // Detach the environment from its outer global object.
    106   void DetachGlobal(Handle<Context> env);
    107 
    108   // Reattach an outer global object to an environment.
    109   void ReattachGlobal(Handle<Context> env, Handle<JSGlobalProxy> global_proxy);
    110 
    111   // Traverses the pointers for memory management.
    112   void Iterate(ObjectVisitor* v);
    113 
    114   // Accessor for the native scripts source code.
    115   Handle<String> NativesSourceLookup(int index);
    116 
    117   // Tells whether bootstrapping is active.
    118   bool IsActive() const { return nesting_ != 0; }
    119 
    120   // Support for thread preemption.
    121   static int ArchiveSpacePerThread();
    122   char* ArchiveState(char* to);
    123   char* RestoreState(char* from);
    124   void FreeThreadResources();
    125 
    126   // This will allocate a char array that is deleted when V8 is shut down.
    127   // It should only be used for strictly finite allocations.
    128   char* AllocateAutoDeletedArray(int bytes);
    129 
    130   // Used for new context creation.
    131   bool InstallExtensions(Handle<Context> native_context,
    132                          v8::ExtensionConfiguration* extensions);
    133 
    134   SourceCodeCache* extensions_cache() { return &extensions_cache_; }
    135 
    136  private:
    137   Isolate* isolate_;
    138   typedef int NestingCounterType;
    139   NestingCounterType nesting_;
    140   SourceCodeCache extensions_cache_;
    141   // This is for delete, not delete[].
    142   List<char*>* delete_these_non_arrays_on_tear_down_;
    143   // This is for delete[]
    144   List<char*>* delete_these_arrays_on_tear_down_;
    145 
    146   friend class BootstrapperActive;
    147   friend class Isolate;
    148   friend class NativesExternalStringResource;
    149 
    150   explicit Bootstrapper(Isolate* isolate);
    151 
    152   DISALLOW_COPY_AND_ASSIGN(Bootstrapper);
    153 };
    154 
    155 
    156 class BootstrapperActive BASE_EMBEDDED {
    157  public:
    158   explicit BootstrapperActive(Bootstrapper* bootstrapper)
    159       : bootstrapper_(bootstrapper) {
    160     ++bootstrapper_->nesting_;
    161   }
    162 
    163   ~BootstrapperActive() {
    164     --bootstrapper_->nesting_;
    165   }
    166 
    167  private:
    168   Bootstrapper* bootstrapper_;
    169 
    170   DISALLOW_COPY_AND_ASSIGN(BootstrapperActive);
    171 };
    172 
    173 
    174 class NativesExternalStringResource
    175     : public v8::String::ExternalAsciiStringResource {
    176  public:
    177   NativesExternalStringResource(Bootstrapper* bootstrapper,
    178                                 const char* source,
    179                                 size_t length);
    180 
    181   const char* data() const {
    182     return data_;
    183   }
    184 
    185   size_t length() const {
    186     return length_;
    187   }
    188  private:
    189   const char* data_;
    190   size_t length_;
    191 };
    192 
    193 }}  // namespace v8::internal
    194 
    195 #endif  // V8_BOOTSTRAPPER_H_
    196