OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/natives.h" | 5 #include "src/natives.h" |
6 | 6 |
7 #include "src/base/logging.h" | 7 #include "src/base/logging.h" |
8 #include "src/list.h" | 8 #include "src/list.h" |
9 #include "src/list-inl.h" | 9 #include "src/list-inl.h" |
10 #include "src/snapshot-source-sink.h" | 10 #include "src/snapshot-source-sink.h" |
11 #include "src/vector.h" | 11 #include "src/vector.h" |
12 | 12 |
13 namespace v8 { | 13 namespace v8 { |
14 namespace internal { | 14 namespace internal { |
15 | 15 |
16 | 16 |
17 /** | 17 /** |
18 * NativesStore stores the 'native' (builtin) JS libraries. | 18 * NativesStore stores the 'native' (builtin) JS libraries. |
19 * | 19 * |
20 * NativesStore needs to be initialized before using V8, usually by the | 20 * NativesStore needs to be initialized before using V8, usually by the |
21 * embedder calling v8::SetNativesDataBlob, which calls SetNativesFromFile | 21 * embedder calling v8::SetNativesDataBlob, which calls SetNativesFromFile |
22 * below. | 22 * below. |
23 */ | 23 */ |
24 class NativesStore { | 24 class NativesStore { |
25 public: | 25 public: |
26 ~NativesStore() {} | 26 ~NativesStore() { |
| 27 for (int i = 0; i < native_names_.length(); i++) { |
| 28 native_names_[i].Dispose(); |
| 29 } |
| 30 } |
27 | 31 |
28 int GetBuiltinsCount() { return native_names_.length(); } | 32 int GetBuiltinsCount() { return native_ids_.length(); } |
29 int GetDebuggerCount() { return debugger_count_; } | 33 int GetDebuggerCount() { return debugger_count_; } |
| 34 |
30 Vector<const char> GetScriptName(int index) { return native_names_[index]; } | 35 Vector<const char> GetScriptName(int index) { return native_names_[index]; } |
| 36 |
31 Vector<const char> GetRawScriptSource(int index) { | 37 Vector<const char> GetRawScriptSource(int index) { |
32 return native_source_[index]; | 38 return native_source_[index]; |
33 } | 39 } |
34 | 40 |
35 int GetIndex(const char* name) { | 41 int GetIndex(const char* id) { |
36 for (int i = 0; i < native_names_.length(); ++i) { | 42 for (int i = 0; i < native_ids_.length(); ++i) { |
37 int native_name_length = native_names_[i].length(); | 43 int native_id_length = native_ids_[i].length(); |
38 if ((static_cast<int>(strlen(name)) == native_name_length) && | 44 if ((static_cast<int>(strlen(id)) == native_id_length) && |
39 (strncmp(name, native_names_[i].start(), native_name_length) == 0)) { | 45 (strncmp(id, native_ids_[i].start(), native_id_length) == 0)) { |
40 return i; | 46 return i; |
41 } | 47 } |
42 } | 48 } |
43 DCHECK(false); | 49 DCHECK(false); |
44 return -1; | 50 return -1; |
45 } | 51 } |
46 | 52 |
47 int GetRawScriptsSize() { | 53 int GetRawScriptsSize() { |
48 DCHECK(false); // Used for compression. Doesn't really make sense here. | 54 DCHECK(false); // Used for compression. Doesn't really make sense here. |
49 return 0; | 55 return 0; |
(...skipping 18 matching lines...) Expand all Loading... |
68 for (int i = 0; i < library_count; ++i) | 74 for (int i = 0; i < library_count; ++i) |
69 store->ReadNameAndContentPair(source); | 75 store->ReadNameAndContentPair(source); |
70 | 76 |
71 store->debugger_count_ = debugger_count; | 77 store->debugger_count_ = debugger_count; |
72 return store; | 78 return store; |
73 } | 79 } |
74 | 80 |
75 private: | 81 private: |
76 NativesStore() : debugger_count_(0) {} | 82 NativesStore() : debugger_count_(0) {} |
77 | 83 |
| 84 Vector<const char> NameFromId(const byte* id, int id_length) { |
| 85 Vector<char> name(Vector<char>::New(id_length + 11)); |
| 86 SimpleStringBuilder builder(name.start(), name.length()); |
| 87 builder.AddString("native "); |
| 88 builder.AddSubstring(reinterpret_cast<const char*>(id), id_length); |
| 89 builder.AddString(".js"); |
| 90 return Vector<const char>::cast(name); |
| 91 } |
| 92 |
78 bool ReadNameAndContentPair(SnapshotByteSource* bytes) { | 93 bool ReadNameAndContentPair(SnapshotByteSource* bytes) { |
79 const byte* name; | 94 const byte* id; |
80 int name_length; | 95 int id_length; |
81 const byte* source; | 96 const byte* source; |
82 int source_length; | 97 int source_length; |
83 bool success = bytes->GetBlob(&name, &name_length) && | 98 bool success = bytes->GetBlob(&id, &id_length) && |
84 bytes->GetBlob(&source, &source_length); | 99 bytes->GetBlob(&source, &source_length); |
85 if (success) { | 100 if (success) { |
86 Vector<const char> name_vector( | 101 Vector<const char> id_vector(reinterpret_cast<const char*>(id), |
87 reinterpret_cast<const char*>(name), name_length); | 102 id_length); |
88 Vector<const char> source_vector( | 103 Vector<const char> source_vector( |
89 reinterpret_cast<const char*>(source), source_length); | 104 reinterpret_cast<const char*>(source), source_length); |
90 native_names_.Add(name_vector); | 105 native_ids_.Add(id_vector); |
91 native_source_.Add(source_vector); | 106 native_source_.Add(source_vector); |
| 107 native_names_.Add(NameFromId(id, id_length)); |
92 } | 108 } |
93 return success; | 109 return success; |
94 } | 110 } |
95 | 111 |
| 112 List<Vector<const char> > native_ids_; |
96 List<Vector<const char> > native_names_; | 113 List<Vector<const char> > native_names_; |
97 List<Vector<const char> > native_source_; | 114 List<Vector<const char> > native_source_; |
98 int debugger_count_; | 115 int debugger_count_; |
99 | 116 |
100 DISALLOW_COPY_AND_ASSIGN(NativesStore); | 117 DISALLOW_COPY_AND_ASSIGN(NativesStore); |
101 }; | 118 }; |
102 | 119 |
103 | 120 |
104 template<NativeType type> | 121 template<NativeType type> |
105 class NativesHolder { | 122 class NativesHolder { |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
189 | 206 |
190 // The compiler can't 'see' all uses of the static methods and hence | 207 // The compiler can't 'see' all uses of the static methods and hence |
191 // my chose to elide them. This we'll explicitly instantiate these. | 208 // my chose to elide them. This we'll explicitly instantiate these. |
192 template class NativesCollection<CORE>; | 209 template class NativesCollection<CORE>; |
193 template class NativesCollection<EXPERIMENTAL>; | 210 template class NativesCollection<EXPERIMENTAL>; |
194 template class NativesCollection<D8>; | 211 template class NativesCollection<D8>; |
195 template class NativesCollection<TEST>; | 212 template class NativesCollection<TEST>; |
196 | 213 |
197 } // namespace v8::internal | 214 } // namespace v8::internal |
198 } // namespace v8 | 215 } // namespace v8 |
OLD | NEW |