-
Notifications
You must be signed in to change notification settings - Fork 200
/
v8js_commonjs.cc
120 lines (97 loc) · 3.49 KB
/
v8js_commonjs.cc
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
/*
+----------------------------------------------------------------------+
| PHP Version 7 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2013 The PHP Group |
+----------------------------------------------------------------------+
| http://www.opensource.org/licenses/mit-license.php MIT License |
+----------------------------------------------------------------------+
| Author: Jani Taskinen <[email protected]> |
| Author: Patrick Reilly <[email protected]> |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php_v8js_macros.h"
extern "C" {
#include "php.h"
#include "zend_exceptions.h"
}
static void v8js_commonjs_split_terms(const char *identifier, std::vector<char *> &terms)
{
char *term = (char *) emalloc(PATH_MAX), *ptr = term;
while (*identifier > 0) {
if (*identifier == '/') {
if (ptr > term) {
// Terminate term string and add to terms vector
*ptr++ = 0;
terms.push_back(estrdup(term));
// Reset term string
ptr = term;
}
} else {
*ptr++ = *identifier;
}
identifier++;
}
if (ptr > term) {
// Terminate term string and add to terms vector
*ptr++ = 0;
terms.push_back(estrdup(term));
}
efree(term);
}
void v8js_commonjs_normalise_identifier(const char *base, const char *identifier, char *normalised_path, char *module_name)
{
std::vector<char *> id_terms, terms;
v8js_commonjs_split_terms(identifier, id_terms);
// If we have a relative module identifier then include the base terms
if (!strcmp(id_terms.front(), ".") || !strcmp(id_terms.front(), "..")) {
v8js_commonjs_split_terms(base, terms);
}
terms.insert(terms.end(), id_terms.begin(), id_terms.end());
std::vector<char *> normalised_terms;
for (std::vector<char *>::iterator it = terms.begin(); it != terms.end(); it++) {
char *term = *it;
if (!strcmp(term, "..")) {
// Ignore parent term (..) if it's the first normalised term
if (normalised_terms.size() > 0) {
// Remove the parent normalized term (and free it)
efree(normalised_terms.back());
normalised_terms.pop_back();
}
// free the ".." term
efree(term);
} else if (strcmp(term, ".")) {
// Add the term if it's not the current term (.)
normalised_terms.push_back(term);
} else {
// Discard "." term
efree(term);
}
}
// Initialise the normalised path string
*normalised_path = 0;
*module_name = 0;
strcat(module_name, normalised_terms.back());
efree(normalised_terms.back());
normalised_terms.pop_back();
for (std::vector<char *>::iterator it = normalised_terms.begin(); it != normalised_terms.end(); it++) {
char *term = *it;
if (strlen(normalised_path) > 0) {
strcat(normalised_path, "/");
}
strcat(normalised_path, term);
efree(term);
}
}
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* indent-tabs-mode: t
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/