ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1 | // Copyright 2006-2008 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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 | |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 32 | #include "allocation.h" |
| 33 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 34 | namespace v8 { |
| 35 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 36 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 37 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 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 { |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 44 | public: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 45 | explicit SourceCodeCache(Script::Type type): type_(type), cache_(NULL) { } |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 46 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 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) { |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 57 | SeqOneByteString* str = SeqOneByteString::cast(cache_->get(i)); |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 58 | if (str->IsUtf8EqualTo(name)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 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) { |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 68 | HandleScope scope(shared->GetIsolate()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 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 | } |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 79 | |
| 80 | private: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 81 | Script::Type type_; |
| 82 | FixedArray* cache_; |
| 83 | DISALLOW_COPY_AND_ASSIGN(SourceCodeCache); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 87 | // The Boostrapper is the public interface for creating a JavaScript global |
| 88 | // context. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 89 | class Bootstrapper { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 90 | public: |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 91 | // Requires: Heap::SetUp has been called. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 92 | void Initialize(bool create_heap_objects); |
| 93 | void TearDown(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 94 | |
| 95 | // Creates a JavaScript Global Context with initial object graph. |
| 96 | // The returned value is a global handle casted to V8Environment*. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 97 | Handle<Context> CreateEnvironment( |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 98 | Handle<Object> global_object, |
| 99 | v8::Handle<v8::ObjectTemplate> global_template, |
| 100 | v8::ExtensionConfiguration* extensions); |
| 101 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 102 | // Detach the environment from its outer global object. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 103 | void DetachGlobal(Handle<Context> env); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 104 | |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 105 | // Reattach an outer global object to an environment. |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 106 | void ReattachGlobal(Handle<Context> env, Handle<JSGlobalProxy> global_proxy); |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 107 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 108 | // Traverses the pointers for memory management. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 109 | void Iterate(ObjectVisitor* v); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 110 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 111 | // Accessor for the native scripts source code. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 112 | Handle<String> NativesSourceLookup(int index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 113 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 114 | // Tells whether bootstrapping is active. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 115 | bool IsActive() const { return nesting_ != 0; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 116 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 117 | // Support for thread preemption. |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 118 | static int ArchiveSpacePerThread(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 119 | char* ArchiveState(char* to); |
| 120 | char* RestoreState(char* from); |
| 121 | void FreeThreadResources(); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 122 | |
| 123 | // This will allocate a char array that is deleted when V8 is shut down. |
| 124 | // It should only be used for strictly finite allocations. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 125 | char* AllocateAutoDeletedArray(int bytes); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 126 | |
| 127 | // Used for new context creation. |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 128 | bool InstallExtensions(Handle<Context> native_context, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 129 | v8::ExtensionConfiguration* extensions); |
| 130 | |
| 131 | SourceCodeCache* extensions_cache() { return &extensions_cache_; } |
| 132 | |
| 133 | private: |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 134 | Isolate* isolate_; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 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; |
| 145 | friend class NativesExternalStringResource; |
| 146 | |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 147 | explicit Bootstrapper(Isolate* isolate); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 148 | |
| 149 | DISALLOW_COPY_AND_ASSIGN(Bootstrapper); |
| 150 | }; |
| 151 | |
| 152 | |
| 153 | class BootstrapperActive BASE_EMBEDDED { |
| 154 | public: |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 155 | explicit BootstrapperActive(Bootstrapper* bootstrapper) |
| 156 | : bootstrapper_(bootstrapper) { |
| 157 | ++bootstrapper_->nesting_; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | ~BootstrapperActive() { |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 161 | --bootstrapper_->nesting_; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | private: |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 165 | Bootstrapper* bootstrapper_; |
| 166 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 167 | DISALLOW_COPY_AND_ASSIGN(BootstrapperActive); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 168 | }; |
| 169 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 170 | |
| 171 | class NativesExternalStringResource |
| 172 | : public v8::String::ExternalAsciiStringResource { |
| 173 | public: |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 174 | NativesExternalStringResource(Bootstrapper* bootstrapper, |
| 175 | const char* source, |
| 176 | size_t length); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 177 | |
| 178 | const char* data() const { |
| 179 | return data_; |
| 180 | } |
| 181 | |
| 182 | size_t length() const { |
| 183 | return length_; |
| 184 | } |
| 185 | private: |
| 186 | const char* data_; |
| 187 | size_t length_; |
| 188 | }; |
| 189 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 190 | }} // namespace v8::internal |
| 191 | |
| 192 | #endif // V8_BOOTSTRAPPER_H_ |