| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 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 | namespace v8 { |
| 33 | namespace internal { |
| 34 | |
| Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 35 | |
| Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 36 | // A SourceCodeCache uses a FixedArray to store pairs of |
| 37 | // (AsciiString*, JSFunction*), mapping names of native code files |
| 38 | // (runtime.js, etc.) to precompiled functions. Instead of mapping |
| 39 | // names to functions it might make sense to let the JS2C tool |
| 40 | // generate an index for each native JS file. |
| 41 | class SourceCodeCache BASE_EMBEDDED { |
| Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 42 | public: |
| Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 43 | explicit SourceCodeCache(Script::Type type): type_(type), cache_(NULL) { } |
| Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 44 | |
| Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 45 | void Initialize(bool create_heap_objects) { |
| 46 | cache_ = create_heap_objects ? HEAP->empty_fixed_array() : NULL; |
| 47 | } |
| 48 | |
| 49 | void Iterate(ObjectVisitor* v) { |
| 50 | v->VisitPointer(BitCast<Object**, FixedArray**>(&cache_)); |
| 51 | } |
| 52 | |
| 53 | bool Lookup(Vector<const char> name, Handle<SharedFunctionInfo>* handle) { |
| 54 | for (int i = 0; i < cache_->length(); i+=2) { |
| 55 | SeqAsciiString* str = SeqAsciiString::cast(cache_->get(i)); |
| 56 | if (str->IsEqualTo(name)) { |
| 57 | *handle = Handle<SharedFunctionInfo>( |
| 58 | SharedFunctionInfo::cast(cache_->get(i + 1))); |
| 59 | return true; |
| 60 | } |
| 61 | } |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | void Add(Vector<const char> name, Handle<SharedFunctionInfo> shared) { |
| 66 | HandleScope scope; |
| 67 | int length = cache_->length(); |
| 68 | Handle<FixedArray> new_array = |
| 69 | FACTORY->NewFixedArray(length + 2, TENURED); |
| 70 | cache_->CopyTo(0, *new_array, 0, cache_->length()); |
| 71 | cache_ = *new_array; |
| 72 | Handle<String> str = FACTORY->NewStringFromAscii(name, TENURED); |
| 73 | cache_->set(length, *str); |
| 74 | cache_->set(length + 1, *shared); |
| 75 | Script::cast(shared->script())->set_type(Smi::FromInt(type_)); |
| 76 | } |
| Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 77 | |
| 78 | private: |
| Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 79 | Script::Type type_; |
| 80 | FixedArray* cache_; |
| 81 | DISALLOW_COPY_AND_ASSIGN(SourceCodeCache); |
| Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 85 | // The Boostrapper is the public interface for creating a JavaScript global |
| 86 | // context. |
| Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 87 | class Bootstrapper { |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 88 | public: |
| 89 | // Requires: Heap::Setup has been called. |
| Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 90 | void Initialize(bool create_heap_objects); |
| 91 | void TearDown(); |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 92 | |
| 93 | // Creates a JavaScript Global Context with initial object graph. |
| 94 | // The returned value is a global handle casted to V8Environment*. |
| Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 95 | Handle<Context> CreateEnvironment( |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 96 | Handle<Object> global_object, |
| 97 | v8::Handle<v8::ObjectTemplate> global_template, |
| 98 | v8::ExtensionConfiguration* extensions); |
| 99 | |
| 100 | // Detach the environment from its outer global object. |
| Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 101 | void DetachGlobal(Handle<Context> env); |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 102 | |
| Andrei Popescu | 74b3c14 | 2010-03-29 12:03:09 +0100 | [diff] [blame] | 103 | // Reattach an outer global object to an environment. |
| Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 104 | void ReattachGlobal(Handle<Context> env, Handle<Object> global_object); |
| Andrei Popescu | 74b3c14 | 2010-03-29 12:03:09 +0100 | [diff] [blame] | 105 | |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 106 | // Traverses the pointers for memory management. |
| Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 107 | void Iterate(ObjectVisitor* v); |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 108 | |
| Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 109 | // Accessor for the native scripts source code. |
| Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 110 | Handle<String> NativesSourceLookup(int index); |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 111 | |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 112 | // Tells whether bootstrapping is active. |
| Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 113 | bool IsActive() const { return nesting_ != 0; } |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 114 | |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 115 | // Support for thread preemption. |
| Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 116 | RLYSTC int ArchiveSpacePerThread(); |
| 117 | char* ArchiveState(char* to); |
| 118 | char* RestoreState(char* from); |
| 119 | void FreeThreadResources(); |
| Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 120 | |
| 121 | // This will allocate a char array that is deleted when V8 is shut down. |
| 122 | // It should only be used for strictly finite allocations. |
| Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 123 | char* AllocateAutoDeletedArray(int bytes); |
| Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 124 | |
| 125 | // Used for new context creation. |
| Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 126 | bool InstallExtensions(Handle<Context> global_context, |
| 127 | v8::ExtensionConfiguration* extensions); |
| 128 | |
| 129 | SourceCodeCache* extensions_cache() { return &extensions_cache_; } |
| 130 | |
| 131 | private: |
| 132 | typedef int NestingCounterType; |
| 133 | NestingCounterType nesting_; |
| 134 | SourceCodeCache extensions_cache_; |
| 135 | // This is for delete, not delete[]. |
| 136 | List<char*>* delete_these_non_arrays_on_tear_down_; |
| 137 | // This is for delete[] |
| 138 | List<char*>* delete_these_arrays_on_tear_down_; |
| 139 | |
| 140 | friend class BootstrapperActive; |
| 141 | friend class Isolate; |
| 142 | friend class NativesExternalStringResource; |
| 143 | |
| 144 | Bootstrapper(); |
| 145 | |
| 146 | DISALLOW_COPY_AND_ASSIGN(Bootstrapper); |
| 147 | }; |
| 148 | |
| 149 | |
| 150 | class BootstrapperActive BASE_EMBEDDED { |
| 151 | public: |
| 152 | BootstrapperActive() { |
| 153 | ++Isolate::Current()->bootstrapper()->nesting_; |
| 154 | } |
| 155 | |
| 156 | ~BootstrapperActive() { |
| 157 | --Isolate::Current()->bootstrapper()->nesting_; |
| 158 | } |
| 159 | |
| 160 | private: |
| 161 | DISALLOW_COPY_AND_ASSIGN(BootstrapperActive); |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 162 | }; |
| 163 | |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 164 | |
| 165 | class NativesExternalStringResource |
| 166 | : public v8::String::ExternalAsciiStringResource { |
| 167 | public: |
| Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 168 | explicit NativesExternalStringResource(Bootstrapper* bootstrapper, |
| 169 | const char* source); |
| Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 170 | |
| 171 | const char* data() const { |
| 172 | return data_; |
| 173 | } |
| 174 | |
| 175 | size_t length() const { |
| 176 | return length_; |
| 177 | } |
| 178 | private: |
| 179 | const char* data_; |
| 180 | size_t length_; |
| 181 | }; |
| 182 | |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 183 | }} // namespace v8::internal |
| 184 | |
| 185 | #endif // V8_BOOTSTRAPPER_H_ |