• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2011 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.
4 
5 #ifndef V8_SNAPSHOT_NATIVES_H_
6 #define V8_SNAPSHOT_NATIVES_H_
7 
8 #include "include/v8.h"
9 #include "src/objects.h"
10 #include "src/vector.h"
11 
12 namespace v8 { class StartupData; }  // Forward declaration.
13 
14 namespace v8 {
15 namespace internal {
16 
17 enum NativeType {
18   CORE,
19   EXTRAS,
20   EXPERIMENTAL_EXTRAS,
21   D8,
22   TEST
23 };
24 
25 // Extra handling for V8_EXPORT_PRIVATE in combination with USING_V8_SHARED
26 // since definition of methods of classes marked as dllimport is not allowed.
27 template <NativeType type>
28 #ifdef USING_V8_SHARED
29 class NativesCollection {
30 #else
31 class V8_EXPORT_PRIVATE NativesCollection {
32 #endif  // USING_V8_SHARED
33 
34  public:
35   // The following methods are implemented in js2c-generated code:
36 
37   // Number of built-in scripts.
38   static int GetBuiltinsCount();
39   // Number of debugger implementation scripts.
40   static int GetDebuggerCount();
41 
42   // These are used to access built-in scripts.  The debugger implementation
43   // scripts have an index in the interval [0, GetDebuggerCount()).  The
44   // non-debugger scripts have an index in the interval [GetDebuggerCount(),
45   // GetNativesCount()).
46   static int GetIndex(const char* name);
47   static Vector<const char> GetScriptSource(int index);
48   static Vector<const char> GetScriptName(int index);
49   static Vector<const char> GetScriptsSource();
50 };
51 
52 typedef NativesCollection<CORE> Natives;
53 typedef NativesCollection<EXTRAS> ExtraNatives;
54 typedef NativesCollection<EXPERIMENTAL_EXTRAS> ExperimentalExtraNatives;
55 
56 
57 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
58 // Used for reading the natives at runtime. Implementation in natives-empty.cc
59 void SetNativesFromFile(StartupData* natives_blob);
60 void ReadNatives();
61 void DisposeNatives();
62 #endif
63 
64 class NativesExternalStringResource final
65     : public v8::String::ExternalOneByteStringResource {
66  public:
67   NativesExternalStringResource(NativeType type, int index);
68 
data()69   const char* data() const override { return data_; }
length()70   size_t length() const override { return length_; }
71 
EncodeForSerialization()72   v8::String::ExternalOneByteStringResource* EncodeForSerialization() const {
73     DCHECK(type_ == CORE || type_ == EXTRAS);
74     intptr_t val = (index_ << 1) | ((type_ == CORE) ? 0 : 1);
75     val = val << kPointerSizeLog2;  // Pointer align.
76     return reinterpret_cast<v8::String::ExternalOneByteStringResource*>(val);
77   }
78 
79   // Decode from serialization.
DecodeForDeserialization(const v8::String::ExternalOneByteStringResource * encoded)80   static NativesExternalStringResource* DecodeForDeserialization(
81       const v8::String::ExternalOneByteStringResource* encoded) {
82     intptr_t val = reinterpret_cast<intptr_t>(encoded) >> kPointerSizeLog2;
83     NativeType type = (val & 1) ? EXTRAS : CORE;
84     int index = static_cast<int>(val >> 1);
85     return new NativesExternalStringResource(type, index);
86   }
87 
88  private:
89   const char* data_;
90   size_t length_;
91   NativeType type_;
92   int index_;
93 };
94 
95 }  // namespace internal
96 }  // namespace v8
97 
98 #endif  // V8_SNAPSHOT_NATIVES_H_
99