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/natives.h"
7 #include "src/base/logging.h"
9 #include "src/list-inl.h"
10 #include "src/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) {
137 static NativesStore* holder_;
140 template<NativeType type>
141 NativesStore* NativesHolder<type>::holder_ = NULL;
145 * Read the Natives (library sources) blob, as generated by js2c + the build
148 void SetNativesFromFile(StartupData* natives_blob) {
149 DCHECK(natives_blob);
150 DCHECK(natives_blob->data);
151 DCHECK(natives_blob->raw_size > 0);
153 SnapshotByteSource bytes(natives_blob->data, natives_blob->raw_size);
154 NativesHolder<CORE>::set(NativesStore::MakeFromScriptsSource(&bytes));
155 NativesHolder<EXPERIMENTAL>::set(NativesStore::MakeFromScriptsSource(&bytes));
156 DCHECK(!bytes.HasMore());
160 // Implement NativesCollection<T> bsaed on NativesHolder + NativesStore.
162 // (The callers expect a purely static interface, since this is how the
163 // natives are usually compiled in. Since we implement them based on
164 // runtime content, we have to implement this indirection to offer
165 // a static interface.)
166 template<NativeType type>
167 int NativesCollection<type>::GetBuiltinsCount() {
168 return NativesHolder<type>::get()->GetBuiltinsCount();
171 template<NativeType type>
172 int NativesCollection<type>::GetDebuggerCount() {
173 return NativesHolder<type>::get()->GetDebuggerCount();
176 template<NativeType type>
177 int NativesCollection<type>::GetIndex(const char* name) {
178 return NativesHolder<type>::get()->GetIndex(name);
181 template <NativeType type>
182 Vector<const char> NativesCollection<type>::GetScriptSource(int index) {
183 return NativesHolder<type>::get()->GetScriptSource(index);
186 template<NativeType type>
187 Vector<const char> NativesCollection<type>::GetScriptName(int index) {
188 return NativesHolder<type>::get()->GetScriptName(index);
191 template <NativeType type>
192 Vector<const char> NativesCollection<type>::GetScriptsSource() {
193 return NativesHolder<type>::get()->GetScriptsSource();
197 // The compiler can't 'see' all uses of the static methods and hence
198 // my choice to elide them. This we'll explicitly instantiate these.
199 template class NativesCollection<CORE>;
200 template class NativesCollection<EXPERIMENTAL>;
202 } // namespace v8::internal