1 // Copyright 2014 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "src/snapshot/natives.h"
7 #include "src/base/logging.h"
9 #include "src/list-inl.h"
10 #include "src/snapshot/snapshot-source-sink.h"
11 #include "src/vector.h"
13 #ifndef V8_USE_EXTERNAL_STARTUP_DATA
14 #error natives-external.cc is used only for the external snapshot build.
15 #endif // V8_USE_EXTERNAL_STARTUP_DATA
23 * NativesStore stores the 'native' (builtin) JS libraries.
25 * NativesStore needs to be initialized before using V8, usually by the
26 * embedder calling v8::SetNativesDataBlob, which calls SetNativesFromFile
32 for (int i = 0; i < native_names_.length(); i++) {
33 native_names_[i].Dispose();
37 int GetBuiltinsCount() { return native_ids_.length(); }
38 int GetDebuggerCount() { return debugger_count_; }
40 Vector<const char> GetScriptSource(int index) {
41 return native_source_[index];
44 Vector<const char> GetScriptName(int index) { return native_names_[index]; }
46 int GetIndex(const char* id) {
47 for (int i = 0; i < native_ids_.length(); ++i) {
48 int native_id_length = native_ids_[i].length();
49 if ((static_cast<int>(strlen(id)) == native_id_length) &&
50 (strncmp(id, native_ids_[i].start(), native_id_length) == 0)) {
58 Vector<const char> GetScriptsSource() {
59 DCHECK(false); // Not implemented.
60 return Vector<const char>();
63 static NativesStore* MakeFromScriptsSource(SnapshotByteSource* source) {
64 NativesStore* store = new NativesStore;
66 // We expect the libraries in the following format:
67 // int: # of debugger sources.
68 // 2N blobs: N pairs of source name + actual source.
69 // then, repeat for non-debugger sources.
70 int debugger_count = source->GetInt();
71 for (int i = 0; i < debugger_count; ++i)
72 store->ReadNameAndContentPair(source);
73 int library_count = source->GetInt();
74 for (int i = 0; i < library_count; ++i)
75 store->ReadNameAndContentPair(source);
77 store->debugger_count_ = debugger_count;
82 NativesStore() : debugger_count_(0) {}
84 Vector<const char> NameFromId(const byte* id, int id_length) {
85 const char native[] = "native ";
86 const char extension[] = ".js";
87 Vector<char> name(Vector<char>::New(id_length + sizeof(native) - 1 +
88 sizeof(extension) - 1));
89 memcpy(name.start(), native, sizeof(native) - 1);
90 memcpy(name.start() + sizeof(native) - 1, id, id_length);
91 memcpy(name.start() + sizeof(native) - 1 + id_length, extension,
92 sizeof(extension) - 1);
93 return Vector<const char>::cast(name);
96 bool ReadNameAndContentPair(SnapshotByteSource* bytes) {
101 bool success = bytes->GetBlob(&id, &id_length) &&
102 bytes->GetBlob(&source, &source_length);
104 Vector<const char> id_vector(reinterpret_cast<const char*>(id),
106 Vector<const char> source_vector(
107 reinterpret_cast<const char*>(source), source_length);
108 native_ids_.Add(id_vector);
109 native_source_.Add(source_vector);
110 native_names_.Add(NameFromId(id, id_length));
115 List<Vector<const char> > native_ids_;
116 List<Vector<const char> > native_names_;
117 List<Vector<const char> > native_source_;
120 DISALLOW_COPY_AND_ASSIGN(NativesStore);
124 template<NativeType type>
125 class NativesHolder {
127 static NativesStore* get() {
131 static void set(NativesStore* store) {
135 static bool empty() { return holder_ == NULL; }
136 static void Dispose() {
142 static NativesStore* holder_;
145 template<NativeType type>
146 NativesStore* NativesHolder<type>::holder_ = NULL;
149 // The natives blob. Memory is owned by caller.
150 static StartupData* natives_blob_ = NULL;
154 * Read the Natives blob, as previously set by SetNativesFromFile.
157 if (natives_blob_ && NativesHolder<CORE>::empty()) {
158 SnapshotByteSource bytes(natives_blob_->data, natives_blob_->raw_size);
159 NativesHolder<CORE>::set(NativesStore::MakeFromScriptsSource(&bytes));
160 NativesHolder<EXPERIMENTAL>::set(
161 NativesStore::MakeFromScriptsSource(&bytes));
162 DCHECK(!bytes.HasMore());
168 * Set the Natives (library sources) blob, as generated by js2c + the build
171 void SetNativesFromFile(StartupData* natives_blob) {
172 DCHECK(!natives_blob_);
173 DCHECK(natives_blob);
174 DCHECK(natives_blob->data);
175 DCHECK(natives_blob->raw_size > 0);
177 natives_blob_ = natives_blob;
183 * Release memory allocated by SetNativesFromFile.
185 void DisposeNatives() {
186 NativesHolder<CORE>::Dispose();
187 NativesHolder<EXPERIMENTAL>::Dispose();
191 // Implement NativesCollection<T> bsaed on NativesHolder + NativesStore.
193 // (The callers expect a purely static interface, since this is how the
194 // natives are usually compiled in. Since we implement them based on
195 // runtime content, we have to implement this indirection to offer
196 // a static interface.)
197 template<NativeType type>
198 int NativesCollection<type>::GetBuiltinsCount() {
199 return NativesHolder<type>::get()->GetBuiltinsCount();
202 template<NativeType type>
203 int NativesCollection<type>::GetDebuggerCount() {
204 return NativesHolder<type>::get()->GetDebuggerCount();
207 template<NativeType type>
208 int NativesCollection<type>::GetIndex(const char* name) {
209 return NativesHolder<type>::get()->GetIndex(name);
212 template <NativeType type>
213 Vector<const char> NativesCollection<type>::GetScriptSource(int index) {
214 return NativesHolder<type>::get()->GetScriptSource(index);
217 template<NativeType type>
218 Vector<const char> NativesCollection<type>::GetScriptName(int index) {
219 return NativesHolder<type>::get()->GetScriptName(index);
222 template <NativeType type>
223 Vector<const char> NativesCollection<type>::GetScriptsSource() {
224 return NativesHolder<type>::get()->GetScriptsSource();
228 // The compiler can't 'see' all uses of the static methods and hence
229 // my choice to elide them. This we'll explicitly instantiate these.
230 template class NativesCollection<CORE>;
231 template class NativesCollection<EXPERIMENTAL>;
233 } // namespace v8::internal