-
-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathModuleInternal.h
More file actions
94 lines (82 loc) · 3.91 KB
/
ModuleInternal.h
File metadata and controls
94 lines (82 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#ifndef ModuleInternal_h
#define ModuleInternal_h
#include "Common.h"
#include "robin_hood.h"
namespace tns {
class ModuleInternal {
public:
ModuleInternal(v8::Local<v8::Context> context);
bool RunModule(v8::Isolate* isolate, std::string path);
void RunScript(v8::Isolate* isolate, std::string script);
static v8::Local<v8::Value> LoadScript(v8::Isolate* isolate,
const std::string& path);
private:
static void RequireCallback(const v8::FunctionCallbackInfo<v8::Value>& info);
v8::Local<v8::Function> GetRequireFunction(v8::Isolate* isolate,
const std::string& dirName);
v8::Local<v8::Object> LoadImpl(v8::Isolate* isolate,
const std::string& moduleName,
const std::string& baseDir, bool& isData);
// Compile (and cache) a classic script; returns the compiled Script handle.
static v8::Local<v8::Script> LoadClassicScript(v8::Isolate* isolate,
const std::string& path);
// Compile/link/evaluate an ES module; returns its namespace object.
static v8::Local<v8::Value> LoadESModule(v8::Isolate* isolate,
const std::string& path);
static v8::Local<v8::String> WrapModuleContent(v8::Isolate* isolate,
const std::string& path);
v8::Local<v8::Object> LoadModule(v8::Isolate* isolate,
const std::string& modulePath,
const std::string& cacheKey);
v8::Local<v8::Object> LoadData(v8::Isolate* isolate,
const std::string& modulePath);
std::string ResolvePath(v8::Isolate* isolate, const std::string& baseDir,
const std::string& moduleName);
std::string ResolvePathFromPackageJson(const std::string& packageJson,
bool& error);
v8::Local<v8::Object> CreatePlaceholderModule(v8::Isolate* isolate,
const std::string& moduleName,
const std::string& cacheKey);
static v8::ScriptCompiler::CachedData* LoadScriptCache(
const std::string& path);
static void SaveScriptCache(const v8::Local<v8::Script> script,
const std::string& path);
static void SaveScriptCache(const v8::ScriptCompiler::CachedData* cache,
const std::string& path);
static std::string GetCacheFileName(const std::string& path);
v8::MaybeLocal<v8::Value> RunScriptString(v8::Isolate* isolate,
v8::Local<v8::Context> context,
const std::string script);
std::unique_ptr<v8::Persistent<v8::Function>> requireFunction_;
std::unique_ptr<v8::Persistent<v8::Function>> requireFactoryFunction_;
robin_hood::unordered_map<std::string,
std::shared_ptr<v8::Persistent<v8::Object>>>
loadedModules_;
struct TempModule {
public:
TempModule(ModuleInternal* module, std::string modulePath,
std::string cacheKey,
std::shared_ptr<v8::Persistent<v8::Object>> poModuleObj)
: module_(module),
dispose_(true),
modulePath_(modulePath),
cacheKey_(cacheKey) {
module->loadedModules_.emplace(modulePath, poModuleObj);
module->loadedModules_.emplace(cacheKey, poModuleObj);
}
~TempModule() {
if (this->dispose_) {
this->module_->loadedModules_.erase(modulePath_);
this->module_->loadedModules_.erase(cacheKey_);
}
}
void SaveToCache() { this->dispose_ = false; }
private:
ModuleInternal* module_;
bool dispose_;
std::string modulePath_;
std::string cacheKey_;
};
};
} // namespace tns
#endif /* ModuleInternal_h */