1 |
|
|
#ifndef SRC_NODE_BUILTINS_H_ |
2 |
|
|
#define SRC_NODE_BUILTINS_H_ |
3 |
|
|
|
4 |
|
|
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS |
5 |
|
|
|
6 |
|
|
#include <map> |
7 |
|
|
#include <memory> |
8 |
|
|
#include <set> |
9 |
|
|
#include <string> |
10 |
|
|
#include <vector> |
11 |
|
|
#include "node_mutex.h" |
12 |
|
|
#include "node_union_bytes.h" |
13 |
|
|
#include "v8.h" |
14 |
|
|
|
15 |
|
|
// Forward declare test fixture for `friend` declaration. |
16 |
|
|
class PerProcessTest; |
17 |
|
|
|
18 |
|
|
namespace node { |
19 |
|
|
class SnapshotBuilder; |
20 |
|
|
class ExternalReferenceRegistry; |
21 |
|
|
namespace builtins { |
22 |
|
|
|
23 |
|
|
using BuiltinSourceMap = std::map<std::string, UnionBytes>; |
24 |
|
|
using BuiltinCodeCacheMap = |
25 |
|
|
std::unordered_map<std::string, |
26 |
|
|
std::unique_ptr<v8::ScriptCompiler::CachedData>>; |
27 |
|
|
|
28 |
|
|
struct CodeCacheInfo { |
29 |
|
|
std::string id; |
30 |
|
|
std::vector<uint8_t> data; |
31 |
|
|
}; |
32 |
|
|
|
33 |
|
|
// Handles compilation and caching of built-in JavaScript modules and |
34 |
|
|
// bootstrap scripts, whose source are bundled into the binary as static data. |
35 |
|
|
class NODE_EXTERN_PRIVATE BuiltinLoader { |
36 |
|
|
public: |
37 |
|
|
BuiltinLoader(const BuiltinLoader&) = delete; |
38 |
|
|
BuiltinLoader& operator=(const BuiltinLoader&) = delete; |
39 |
|
|
|
40 |
|
|
static void RegisterExternalReferences(ExternalReferenceRegistry* registry); |
41 |
|
|
static void Initialize(v8::Local<v8::Object> target, |
42 |
|
|
v8::Local<v8::Value> unused, |
43 |
|
|
v8::Local<v8::Context> context, |
44 |
|
|
void* priv); |
45 |
|
|
|
46 |
|
|
// The parameters used to compile the scripts are detected based on |
47 |
|
|
// the pattern of the id. |
48 |
|
|
static v8::MaybeLocal<v8::Function> LookupAndCompile( |
49 |
|
|
v8::Local<v8::Context> context, |
50 |
|
|
const char* id, |
51 |
|
|
Environment* optional_env); |
52 |
|
|
|
53 |
|
|
static v8::Local<v8::Object> GetSourceObject(v8::Local<v8::Context> context); |
54 |
|
|
// Returns config.gypi as a JSON string |
55 |
|
|
static v8::Local<v8::String> GetConfigString(v8::Isolate* isolate); |
56 |
|
|
static bool Exists(const char* id); |
57 |
|
|
static bool Add(const char* id, const UnionBytes& source); |
58 |
|
|
|
59 |
|
|
static bool CompileAllBuiltins(v8::Local<v8::Context> context); |
60 |
|
|
static void RefreshCodeCache(const std::vector<CodeCacheInfo>& in); |
61 |
|
|
static void CopyCodeCache(std::vector<CodeCacheInfo>* out); |
62 |
|
|
|
63 |
|
|
private: |
64 |
|
|
// Only allow access from friends. |
65 |
|
|
friend class CodeCacheBuilder; |
66 |
|
|
|
67 |
|
|
BuiltinLoader(); |
68 |
|
|
static BuiltinLoader* GetInstance(); |
69 |
|
|
|
70 |
|
|
// Generated by tools/js2c.py as node_javascript.cc |
71 |
|
|
void LoadJavaScriptSource(); // Loads data into source_ |
72 |
|
|
UnionBytes GetConfig(); // Return data for config.gypi |
73 |
|
|
|
74 |
|
|
std::vector<std::string> GetBuiltinIds(); |
75 |
|
|
|
76 |
|
|
struct BuiltinCategories { |
77 |
|
|
bool is_initialized = false; |
78 |
|
|
std::set<std::string> can_be_required; |
79 |
|
|
std::set<std::string> cannot_be_required; |
80 |
|
|
}; |
81 |
|
|
void InitializeBuiltinCategories(); |
82 |
|
|
const std::set<std::string>& GetCannotBeRequired(); |
83 |
|
|
const std::set<std::string>& GetCanBeRequired(); |
84 |
|
|
|
85 |
|
|
bool CanBeRequired(const char* id); |
86 |
|
|
bool CannotBeRequired(const char* id); |
87 |
|
|
|
88 |
|
|
BuiltinCodeCacheMap* code_cache(); |
89 |
|
5356 |
const Mutex& code_cache_mutex() const { return code_cache_mutex_; } |
90 |
|
|
|
91 |
|
|
v8::ScriptCompiler::CachedData* GetCodeCache(const char* id) const; |
92 |
|
|
enum class Result { kWithCache, kWithoutCache }; |
93 |
|
|
v8::MaybeLocal<v8::String> LoadBuiltinSource(v8::Isolate* isolate, |
94 |
|
|
const char* id); |
95 |
|
|
// If an exception is encountered (e.g. source code contains |
96 |
|
|
// syntax error), the returned value is empty. |
97 |
|
|
v8::MaybeLocal<v8::Function> LookupAndCompileInternal( |
98 |
|
|
v8::Local<v8::Context> context, |
99 |
|
|
const char* id, |
100 |
|
|
std::vector<v8::Local<v8::String>>* parameters, |
101 |
|
|
Result* result); |
102 |
|
|
|
103 |
|
|
static void RecordResult(const char* id, |
104 |
|
|
BuiltinLoader::Result result, |
105 |
|
|
Environment* env); |
106 |
|
|
static void GetBuiltinCategories( |
107 |
|
|
v8::Local<v8::Name> property, |
108 |
|
|
const v8::PropertyCallbackInfo<v8::Value>& info); |
109 |
|
|
static void GetCacheUsage(const v8::FunctionCallbackInfo<v8::Value>& args); |
110 |
|
|
// Passing ids of built-in source code into JS land as |
111 |
|
|
// internalBinding('builtins').builtinIds |
112 |
|
|
static void BuiltinIdsGetter(v8::Local<v8::Name> property, |
113 |
|
|
const v8::PropertyCallbackInfo<v8::Value>& info); |
114 |
|
|
// Passing config.gypi into JS land as internalBinding('builtins').config |
115 |
|
|
static void ConfigStringGetter( |
116 |
|
|
v8::Local<v8::Name> property, |
117 |
|
|
const v8::PropertyCallbackInfo<v8::Value>& info); |
118 |
|
|
// Compile a specific built-in as a function |
119 |
|
|
static void CompileFunction(const v8::FunctionCallbackInfo<v8::Value>& args); |
120 |
|
|
static void HasCachedBuiltins( |
121 |
|
|
const v8::FunctionCallbackInfo<v8::Value>& args); |
122 |
|
|
|
123 |
|
|
static BuiltinLoader instance_; |
124 |
|
|
BuiltinCategories builtin_categories_; |
125 |
|
|
BuiltinSourceMap source_; |
126 |
|
|
BuiltinCodeCacheMap code_cache_; |
127 |
|
|
UnionBytes config_; |
128 |
|
|
|
129 |
|
|
// Used to synchronize access to the code cache map |
130 |
|
|
Mutex code_cache_mutex_; |
131 |
|
|
|
132 |
|
|
bool has_code_cache_; |
133 |
|
|
|
134 |
|
|
friend class ::PerProcessTest; |
135 |
|
|
}; |
136 |
|
|
} // namespace builtins |
137 |
|
|
|
138 |
|
|
} // namespace node |
139 |
|
|
|
140 |
|
|
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS |
141 |
|
|
|
142 |
|
|
#endif // SRC_NODE_BUILTINS_H_ |