/
JSModules.h
128 lines (100 loc) · 2.64 KB
/
JSModules.h
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
/*
Copyright 2016 Nidium Inc. All rights reserved.
Use of this source code is governed by a MIT license
that can be found in the LICENSE file.
*/
#ifndef binding_jsmodules_h__
#define binding_jsmodules_h__
#include <string.h>
#include <jsapi.h>
#include <jspubtd.h>
#include "Core/Hash.h"
namespace Nidium {
namespace Core {
class Path;
}
namespace Binding {
class JSModules;
// {{{ JSModule
class JSModule
{
public:
JSModule(JSContext *cx,
JSModules *modules,
JSModule *parent,
const char *name);
enum ModuleType
{
kModuleType_None,
kModuleType_JS,
kModuleType_Native,
kModuleType_NativeEmbedded,
kModuleType_JSON,
kModuleType_NidiumComponent
};
char *m_AbsoluteDir;
Core::Path *m_FilePath;
char *m_Name;
int m_ModuleType;
bool m_Cached;
JS::Heap<JSObject *> m_Exports;
JSModule *m_Parent;
JSModules *m_Modules;
bool init();
bool initJS();
bool initMain();
bool initNative();
bool initNativeEmbedded();
JS::Value require(char *name);
bool findModulePath();
~JSModule();
private:
JSContext *m_Cx;
void * m_DLModule = nullptr;
JS::Value load(JS::Value &scope);
};
// }}}
// {{{ JSModules
class JSModules
{
public:
friend class JSModule;
JSModules(JSContext *cx) : m_Main(NULL), m_Cx(cx)
{
m_Paths[0] = static_cast<const char *>("modules");
m_Paths[1] = static_cast<const char *>("node_modules");
memset(&m_EnvPaths[0], '\0', sizeof(m_EnvPaths));
}
JSModule *m_Main;
typedef JSObject *(*EmbeddedCallback)(JSContext *cx);
~JSModules()
{
for (int i = 0; m_EnvPaths[i] != NULL; i++) {
free(m_EnvPaths[i]);
}
delete m_Main;
}
void add(JSModule *module);
void remove(JSModule *module);
JSModule *find(JSModule *module);
bool init();
bool init(JSModule *module);
static void RegisterEmbedded(const char *name,
EmbeddedCallback registerCallback);
static EmbeddedCallback FindEmbedded(const char *name);
static bool GetFileContent(Core::Path *p, char **content, size_t *size);
private:
Core::Hash<JSModule *> m_Cache;
static Core::Hash<void *> m_EmbeddedModules;
const char *m_Paths[2];
char *m_EnvPaths[64];
JSContext *m_Cx;
bool initJS(JSModule *cmodule);
static std::string FindModuleInPath(JSModule *module, const char *path);
static bool LoadDirectoryModule(std::string &dir);
static void DirName(std::string &source);
};
// }}}
} // namespace Binding
} // namespace Nidium
#endif