forked from NativeScript/runtimes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModuleInternal.h
More file actions
153 lines (115 loc) · 4.94 KB
/
Copy pathModuleInternal.h
File metadata and controls
153 lines (115 loc) · 4.94 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#ifndef JNI_MODULE_H_
#define JNI_MODULE_H_
#include <string>
#include "js_native_api_types.h"
#include "robin_hood.h"
typedef napi_value napi_register_module_v(napi_env env, napi_value exports);
#if defined(TARGET_ENGINE_QUICKJS)
typedef struct JSContext JSContext;
typedef struct JSModuleDef JSModuleDef;
#endif
namespace nativescript {
class ModuleInternal {
public:
ModuleInternal();
void Init(napi_env env, const std::string& baseDir = "");
napi_value Require(napi_env env, const std::string& moduleName,
const std::string& callingModuleDirName);
napi_status Load(napi_env env, const std::string& path);
/*
* Reuses `Load` logic and adds TryCatch exception handling to push any
* unhandled exceptions during script's initial load through the worker
* scope's `onerror` handler (if implemented before the exception was thrown)
*/
void LoadWorker(napi_env env, const std::string& path);
/*
* Checks if target script exists, will throw if negative
* Used before initializing workers, to ensure a thread will not be created,
* when the file doesn't exist
*/
static void CheckFileExists(napi_env env, const std::string& path,
const std::string& baseDir);
static std::string EnsureFileProtocol(const std::string& path);
static int MODULE_PROLOGUE_LENGTH;
void DeInit();
private:
enum class ModulePathKind { Global, Relative, Absolute };
struct ModuleCacheEntry {
ModuleCacheEntry(napi_ref _obj) : obj(_obj), isData(false) {}
ModuleCacheEntry(napi_ref _obj, bool _isData)
: obj(_obj), isData(_isData) {}
bool isData;
napi_ref obj;
};
static napi_value RequireCallback(napi_env env, napi_callback_info info);
static napi_value LoadInternalModule(napi_env env,
const std::string& moduleName);
static napi_value RequireNativeCallback(napi_env env,
napi_callback_info info);
static napi_value RequireNativeV8Callback(napi_env env,
napi_callback_info info);
napi_value RequireCallbackImpl(napi_env env, napi_callback_info info);
napi_value WrapModuleContent(napi_env env, const std::string& path);
std::string ResolvePath(napi_env env, const std::string& baseDir,
const std::string& moduleName);
std::string ResolvePathFromPackageJson(napi_env env, const std::string& path,
bool& error);
napi_value LoadImpl(napi_env env, const std::string& moduleName,
const std::string& baseDir, bool& isData);
napi_value LoadModule(napi_env env, const std::string& path,
const std::string& moduleCacheKey);
napi_value LoadData(napi_env env, const std::string& path);
napi_value LoadScript(napi_env env, const std::string& modulePath,
napi_value fullRequiredModulePath);
napi_value GetRequireFunction(napi_env env, const std::string& dirName);
// ES Module support functions
bool IsESModule(const std::string& path);
napi_value LoadESModule(napi_env env, const std::string& path);
#if defined(TARGET_ENGINE_QUICKJS)
void InitQuickJSESModuleLoader(napi_env env);
std::string NormalizeQuickJSImportSpecifier(const std::string& baseName,
const std::string& moduleName);
JSModuleDef* LoadQuickJSImportModule(JSContext* ctx,
const std::string& moduleName);
#endif
// void SaveScriptCache(napi_env env, napi_value script, const std::string&
// path);
ModulePathKind GetModulePathKind(const std::string& path);
static const char* MODULE_PROLOGUE;
static const char* MODULE_EPILOGUE;
napi_env m_env;
napi_ref m_requireFunction;
napi_ref m_requireFactoryFunction;
robin_hood::unordered_map<std::string, napi_ref> m_requireCache;
robin_hood::unordered_map<std::string, ModuleCacheEntry> m_loadedModules;
class TempModule {
public:
TempModule(ModuleInternal* module, const std::string& modulePath,
const std::string& cacheKey, napi_ref poModuleObj)
: m_module(module),
m_dispose(true),
m_modulePath(modulePath),
m_cacheKey(cacheKey),
m_poModuleObj(poModuleObj) {
m_module->m_loadedModules.emplace(m_modulePath,
ModuleCacheEntry(m_poModuleObj));
m_module->m_loadedModules.emplace(m_cacheKey,
ModuleCacheEntry(m_poModuleObj));
}
~TempModule() {
if (m_dispose) {
m_module->m_loadedModules.erase(m_modulePath);
m_module->m_loadedModules.erase(m_cacheKey);
}
}
void SaveToCache() { m_dispose = false; }
private:
bool m_dispose;
ModuleInternal* m_module;
std::string m_modulePath;
std::string m_cacheKey;
napi_ref m_poModuleObj;
};
};
} // namespace nativescript
#endif /* JNI_MODULE_H_ */