Line data Source code
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 : EXTRAS,
19 : TEST
20 : };
21 :
22 : // Extra handling for V8_EXPORT_PRIVATE in combination with USING_V8_SHARED
23 : // since definition of methods of classes marked as dllimport is not allowed.
24 : template <NativeType type>
25 : #ifdef USING_V8_SHARED
26 : class NativesCollection {
27 : #else
28 : class V8_EXPORT_PRIVATE NativesCollection {
29 : #endif // USING_V8_SHARED
30 :
31 : public:
32 : // The following methods are implemented in js2c-generated code:
33 :
34 : // Number of built-in scripts.
35 : static int GetBuiltinsCount();
36 : static int GetIndex(const char* name);
37 : static Vector<const char> GetScriptSource(int index);
38 : static Vector<const char> GetScriptName(int index);
39 : static Vector<const char> GetScriptsSource();
40 : };
41 :
42 : typedef NativesCollection<EXTRAS> ExtraNatives;
43 :
44 :
45 : #ifdef V8_USE_EXTERNAL_STARTUP_DATA
46 : // Used for reading the natives at runtime. Implementation in natives-empty.cc
47 : void SetNativesFromFile(StartupData* natives_blob);
48 : void ReadNatives();
49 : void DisposeNatives();
50 : #endif
51 :
52 123056 : class NativesExternalStringResource final
53 : : public v8::String::ExternalOneByteStringResource {
54 : public:
55 : NativesExternalStringResource(NativeType type, int index);
56 :
57 5789510 : const char* data() const override { return data_; }
58 61654 : size_t length() const override { return length_; }
59 :
60 : v8::String::ExternalOneByteStringResource* EncodeForSerialization() const {
61 : DCHECK(type_ == EXTRAS);
62 206 : intptr_t val = (index_ << 1) | 1;
63 206 : val = val << kSystemPointerSizeLog2; // Pointer align.
64 206 : return reinterpret_cast<v8::String::ExternalOneByteStringResource*>(val);
65 : }
66 :
67 : // Decode from serialization.
68 61433 : static NativesExternalStringResource* DecodeForDeserialization(
69 : const v8::String::ExternalOneByteStringResource* encoded) {
70 : intptr_t val =
71 61433 : reinterpret_cast<intptr_t>(encoded) >> kSystemPointerSizeLog2;
72 : DCHECK(val & 1);
73 61433 : int index = static_cast<int>(val >> 1);
74 61433 : return new NativesExternalStringResource(EXTRAS, index);
75 : }
76 :
77 : private:
78 : const char* data_;
79 : size_t length_;
80 : NativeType type_;
81 : int index_;
82 : };
83 :
84 : } // namespace internal
85 : } // namespace v8
86 :
87 : #endif // V8_SNAPSHOT_NATIVES_H_
|