OLD | NEW |
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 15 matching lines...) Expand all Loading... |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
28 | 28 |
29 #ifndef V8_BOOTSTRAPPER_H_ | 29 #ifndef V8_BOOTSTRAPPER_H_ |
30 #define V8_BOOTSTRAPPER_H_ | 30 #define V8_BOOTSTRAPPER_H_ |
31 | 31 |
32 namespace v8 { | 32 namespace v8 { |
33 namespace internal { | 33 namespace internal { |
34 | 34 |
35 | 35 |
36 class BootstrapperActive BASE_EMBEDDED { | 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 { |
37 public: | 42 public: |
38 BootstrapperActive() { nesting_++; } | 43 explicit SourceCodeCache(Script::Type type): type_(type), cache_(NULL) { } |
39 ~BootstrapperActive() { nesting_--; } | |
40 | 44 |
41 // Support for thread preemption. | 45 void Initialize(bool create_heap_objects) { |
42 static int ArchiveSpacePerThread(); | 46 cache_ = create_heap_objects ? HEAP->empty_fixed_array() : NULL; |
43 static char* ArchiveState(char* to); | 47 } |
44 static char* RestoreState(char* from); | 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 } |
45 | 77 |
46 private: | 78 private: |
47 static bool IsActive() { return nesting_ != 0; } | 79 Script::Type type_; |
48 static int nesting_; | 80 FixedArray* cache_; |
49 friend class Bootstrapper; | 81 DISALLOW_COPY_AND_ASSIGN(SourceCodeCache); |
50 }; | 82 }; |
51 | 83 |
52 | 84 |
53 // The Boostrapper is the public interface for creating a JavaScript global | 85 // The Boostrapper is the public interface for creating a JavaScript global |
54 // context. | 86 // context. |
55 class Bootstrapper : public AllStatic { | 87 class Bootstrapper { |
56 public: | 88 public: |
57 // Requires: Heap::Setup has been called. | 89 // Requires: Heap::Setup has been called. |
58 static void Initialize(bool create_heap_objects); | 90 void Initialize(bool create_heap_objects); |
59 static void TearDown(); | 91 void TearDown(); |
60 | 92 |
61 // Creates a JavaScript Global Context with initial object graph. | 93 // Creates a JavaScript Global Context with initial object graph. |
62 // The returned value is a global handle casted to V8Environment*. | 94 // The returned value is a global handle casted to V8Environment*. |
63 static Handle<Context> CreateEnvironment( | 95 Handle<Context> CreateEnvironment( |
64 Handle<Object> global_object, | 96 Handle<Object> global_object, |
65 v8::Handle<v8::ObjectTemplate> global_template, | 97 v8::Handle<v8::ObjectTemplate> global_template, |
66 v8::ExtensionConfiguration* extensions); | 98 v8::ExtensionConfiguration* extensions); |
67 | 99 |
68 // Detach the environment from its outer global object. | 100 // Detach the environment from its outer global object. |
69 static void DetachGlobal(Handle<Context> env); | 101 void DetachGlobal(Handle<Context> env); |
70 | 102 |
71 // Reattach an outer global object to an environment. | 103 // Reattach an outer global object to an environment. |
72 static void ReattachGlobal(Handle<Context> env, Handle<Object> global_object); | 104 void ReattachGlobal(Handle<Context> env, Handle<Object> global_object); |
73 | 105 |
74 // Traverses the pointers for memory management. | 106 // Traverses the pointers for memory management. |
75 static void Iterate(ObjectVisitor* v); | 107 void Iterate(ObjectVisitor* v); |
76 | 108 |
77 // Accessor for the native scripts source code. | 109 // Accessor for the native scripts source code. |
78 static Handle<String> NativesSourceLookup(int index); | 110 Handle<String> NativesSourceLookup(int index); |
79 | 111 |
80 // Tells whether bootstrapping is active. | 112 // Tells whether bootstrapping is active. |
81 static bool IsActive() { return BootstrapperActive::IsActive(); } | 113 bool IsActive() const { return nesting_ != 0; } |
82 | 114 |
83 // Support for thread preemption. | 115 // Support for thread preemption. |
84 static int ArchiveSpacePerThread(); | 116 RLYSTC int ArchiveSpacePerThread(); |
85 static char* ArchiveState(char* to); | 117 char* ArchiveState(char* to); |
86 static char* RestoreState(char* from); | 118 char* RestoreState(char* from); |
87 static void FreeThreadResources(); | 119 void FreeThreadResources(); |
88 | 120 |
89 // This will allocate a char array that is deleted when V8 is shut down. | 121 // This will allocate a char array that is deleted when V8 is shut down. |
90 // It should only be used for strictly finite allocations. | 122 // It should only be used for strictly finite allocations. |
91 static char* AllocateAutoDeletedArray(int bytes); | 123 char* AllocateAutoDeletedArray(int bytes); |
92 | 124 |
93 // Used for new context creation. | 125 // Used for new context creation. |
94 static bool InstallExtensions(Handle<Context> global_context, | 126 bool InstallExtensions(Handle<Context> global_context, |
95 v8::ExtensionConfiguration* extensions); | 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); |
96 }; | 162 }; |
97 | 163 |
98 | 164 |
99 class NativesExternalStringResource | 165 class NativesExternalStringResource |
100 : public v8::String::ExternalAsciiStringResource { | 166 : public v8::String::ExternalAsciiStringResource { |
101 public: | 167 public: |
102 explicit NativesExternalStringResource(const char* source); | 168 explicit NativesExternalStringResource(Bootstrapper* bootstrapper, |
| 169 const char* source); |
103 | 170 |
104 const char* data() const { | 171 const char* data() const { |
105 return data_; | 172 return data_; |
106 } | 173 } |
107 | 174 |
108 size_t length() const { | 175 size_t length() const { |
109 return length_; | 176 return length_; |
110 } | 177 } |
111 private: | 178 private: |
112 const char* data_; | 179 const char* data_; |
113 size_t length_; | 180 size_t length_; |
114 }; | 181 }; |
115 | 182 |
116 }} // namespace v8::internal | 183 }} // namespace v8::internal |
117 | 184 |
118 #endif // V8_BOOTSTRAPPER_H_ | 185 #endif // V8_BOOTSTRAPPER_H_ |
OLD | NEW |