Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2011 the V8 project authors. All rights reserved. | |
|
jochen (gone - plz use gerrit)
2014/05/26 14:34:49
nit 2014
vogelheim
2014/05/27 15:20:23
Done.
| |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "natives.h" | |
| 6 | |
| 7 #include "checks.h" | |
| 8 #include "list.h" | |
| 9 #include "list-inl.h" | |
| 10 #include "vector.h" | |
| 11 #include "snapshot-source-sink.h" | |
| 12 | |
| 13 namespace v8 { | |
| 14 namespace internal { | |
| 15 | |
| 16 | |
| 17 /** | |
| 18 * NativesStore stores the 'native' (builtin) JS libraries. | |
| 19 * | |
| 20 * NativesStore needs to be initialized before using V8, usually by the | |
| 21 * embedder calling v8::SetNativesDataBlob, which calls SetNativesFromFile | |
| 22 * below. | |
| 23 */ | |
| 24 class NativesStore { | |
| 25 public: | |
| 26 ~NativesStore() {} | |
| 27 | |
| 28 int GetBuiltinsCount() { return native_names_.length(); } | |
| 29 int GetDebuggerCount() { return debugger_count_; } | |
| 30 Vector<const char> GetScriptName(int index) { return native_names_[index]; } | |
| 31 Vector<const char> GetRawScriptSource(int index) { | |
| 32 return native_source_[index]; | |
| 33 } | |
| 34 | |
| 35 int GetIndex(const char* name) { | |
| 36 for (int i = 0; i < native_names_.length(); ++i) { | |
| 37 if (strcmp(name, native_names_[i].start()) == 0) { | |
| 38 return i; | |
| 39 } | |
| 40 } | |
| 41 ASSERT(false); | |
| 42 return -1; | |
| 43 } | |
| 44 | |
| 45 int GetRawScriptsSize() { | |
| 46 ASSERT(false); // Used for compression. Doesn't really make sense here. | |
| 47 return 0; | |
| 48 } | |
| 49 | |
| 50 Vector<const byte> GetScriptsSource() { | |
| 51 ASSERT(false); // Used for compression. Doesn't really make sense here. | |
| 52 return Vector<const byte>(); | |
| 53 } | |
| 54 | |
| 55 static NativesStore* MakeFromScriptsSource(SnapshotByteSource* source) { | |
| 56 NativesStore* store = new NativesStore; | |
| 57 | |
| 58 // We expect the libraries in the following format: | |
| 59 // int: # of debugger sources. | |
| 60 // 2N blobs: N pairs of source name + actual source. | |
| 61 // then, repeat for non-debugger sources. | |
| 62 int debugger_count = source->GetInt(); | |
| 63 for (int i = 0; i < debugger_count; ++i) | |
| 64 store->ReadNameAndContentPair(source); | |
| 65 int library_count = source->GetInt(); | |
| 66 for (int i = 0; i < library_count; ++i) | |
| 67 store->ReadNameAndContentPair(source); | |
| 68 | |
| 69 store->debugger_count_ = debugger_count; | |
| 70 return store; | |
| 71 } | |
| 72 | |
| 73 private: | |
| 74 NativesStore() : debugger_count_(0) {} | |
| 75 | |
| 76 bool ReadNameAndContentPair(SnapshotByteSource* bytes) { | |
| 77 const byte* name; | |
| 78 int name_length; | |
| 79 const byte* source; | |
| 80 int source_length; | |
| 81 bool success = bytes->GetBlob(&name, &name_length) && | |
| 82 bytes->GetBlob(&source, &source_length); | |
| 83 if (success) { | |
| 84 Vector<const char> name_vector( | |
| 85 reinterpret_cast<const char*>(name), name_length); | |
| 86 Vector<const char> source_vector( | |
| 87 reinterpret_cast<const char*>(source), source_length); | |
| 88 native_names_.Add(name_vector); | |
| 89 native_source_.Add(source_vector); | |
| 90 } | |
| 91 return success; | |
| 92 } | |
| 93 | |
| 94 List<Vector<const char> > native_names_; | |
| 95 List<Vector<const char> > native_source_; | |
| 96 int debugger_count_; | |
| 97 }; | |
|
jochen (gone - plz use gerrit)
2014/05/26 14:34:49
nit. DISALLOW_COPY_AND_ASSIGN()
vogelheim
2014/05/27 15:20:23
Done.
| |
| 98 | |
| 99 | |
| 100 template<NativeType type> | |
| 101 class NativesHolder { | |
| 102 public: | |
| 103 static NativesStore* get() { | |
| 104 ASSERT(holder_); | |
| 105 return holder_; | |
| 106 } | |
| 107 static void set(NativesStore* store) { | |
| 108 ASSERT(store); | |
| 109 holder_ = store; | |
| 110 } | |
| 111 | |
| 112 private: | |
| 113 static NativesStore* holder_; | |
| 114 }; | |
| 115 | |
| 116 template<NativeType type> | |
| 117 NativesStore* NativesHolder<type>::holder_ = NULL; | |
| 118 | |
| 119 | |
| 120 /** | |
| 121 * Read the Natives (library sources) blob, as generated by js2c + the build | |
| 122 * system. | |
| 123 */ | |
| 124 void SetNativesFromFile(StartupData* natives_blob) { | |
| 125 ASSERT(natives_blob); | |
| 126 ASSERT(natives_blob->data); | |
| 127 ASSERT(natives_blob->raw_size > 0); | |
| 128 | |
| 129 SnapshotByteSource bytes( | |
| 130 reinterpret_cast<const byte*>(natives_blob->data), | |
| 131 natives_blob->raw_size); | |
| 132 NativesHolder<CORE>::set(NativesStore::MakeFromScriptsSource(&bytes)); | |
| 133 NativesHolder<EXPERIMENTAL>::set(NativesStore::MakeFromScriptsSource(&bytes)); | |
| 134 ASSERT(!bytes.HasMore()); | |
| 135 } | |
| 136 | |
| 137 | |
| 138 // Implement NativesCollection<T> bsaed on NativesHolder + NativesStore. | |
| 139 // | |
| 140 // (The callers expect a purely static interface, since this is how the | |
| 141 // natives are usually compiled in. Since we implement them based on | |
| 142 // runtime content, we have to implement this indirection to offer | |
| 143 // a static interface.) | |
| 144 template<NativeType type> | |
| 145 int NativesCollection<type>::GetBuiltinsCount() { | |
| 146 return NativesHolder<type>::get()->GetBuiltinsCount(); | |
| 147 } | |
| 148 | |
| 149 template<NativeType type> | |
| 150 int NativesCollection<type>::GetDebuggerCount() { | |
| 151 return NativesHolder<type>::get()->GetDebuggerCount(); | |
| 152 } | |
| 153 | |
| 154 template<NativeType type> | |
| 155 int NativesCollection<type>::GetIndex(const char* name) { | |
| 156 return NativesHolder<type>::get()->GetIndex(name); | |
| 157 } | |
| 158 | |
| 159 template<NativeType type> | |
| 160 int NativesCollection<type>::GetRawScriptsSize() { | |
| 161 return NativesHolder<type>::get()->GetRawScriptsSize(); | |
| 162 } | |
| 163 | |
| 164 template<NativeType type> | |
| 165 Vector<const char> NativesCollection<type>::GetRawScriptSource(int index) { | |
| 166 return NativesHolder<type>::get()->GetRawScriptSource(index); | |
| 167 } | |
| 168 | |
| 169 template<NativeType type> | |
| 170 Vector<const char> NativesCollection<type>::GetScriptName(int index) { | |
| 171 return NativesHolder<type>::get()->GetScriptName(index); | |
| 172 } | |
| 173 | |
| 174 template<NativeType type> | |
| 175 Vector<const byte> NativesCollection<type>::GetScriptsSource() { | |
| 176 return NativesHolder<type>::get()->GetScriptsSource(); | |
| 177 } | |
| 178 | |
| 179 | |
| 180 // The compiler can't 'see' all uses of the static methods and hence | |
| 181 // my chose to elide them. This we'll explicitly instantiate these. | |
| 182 template class NativesCollection<CORE>; | |
| 183 template class NativesCollection<EXPERIMENTAL>; | |
| 184 template class NativesCollection<D8>; | |
| 185 template class NativesCollection<TEST>; | |
| 186 | |
| 187 } // namespace v8::internal | |
| 188 } // namespace v8 | |
| OLD | NEW |