forked from phpv8/v8js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathv8js_amd.cc
107 lines (87 loc) · 3.29 KB
/
v8js_amd.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
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| 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
extern "C" {
#include "php.h"
#include "zend_exceptions.h"
}
#include "php_v8js_macros.h"
void php_v8js_amd_split_terms(char *input, std::vector<const char *> &terms)
{
char *term = (char *)malloc(PATH_MAX), *ptr = (char *)term;
// Initialise the term string
*term = 0;
while (*input > 0) {
if (*input == '/') {
if (strlen(term) > 0) {
// Terminate term string and add to terms vector
*ptr++ = 0;
terms.push_back(strdup(term));
// Reset term string
memset(term, 0, strlen(term));
ptr = term;
}
} else {
*ptr++ = *input;
}
input++;
}
if (strlen(term) > 0) {
// Terminate term string and add to terms vector
*ptr++ = 0;
terms.push_back(strdup(term));
}
if (term > 0) {
free(term);
}
}
void php_v8js_amd_normalise_id(const char *base_module_id, const char *module_id, char *normalised_module_id)
{
// Initialise the normalised module ID string
*normalised_module_id = 0;
// Special case modules are prefied with @
if (*module_id == '@') {
strcat(normalised_module_id, module_id);
return;
}
std::vector<const char *> id_terms, terms;
php_v8js_amd_split_terms((char *)module_id, id_terms);
// If we have a relative module ID then include the base module ID
if (!strcmp(id_terms.front(), ".") || !strcmp(id_terms.front(), "..")) {
php_v8js_amd_split_terms((char *)base_module_id, terms);
terms.pop_back();
}
terms.insert(terms.end(), id_terms.begin(), id_terms.end());
std::vector<const char *> normalised_terms;
for (std::vector<const char *>::iterator it = terms.begin(); it != terms.end(); it++) {
const 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
normalised_terms.pop_back();
}
} else if (strcmp(term, ".")) {
// Add the term if it's not the current term (.)
normalised_terms.push_back(term);
}
}
for (std::vector<const char *>::iterator it = normalised_terms.begin(); it != normalised_terms.end(); it++) {
if (strlen(normalised_module_id) > 0) {
strcat(normalised_module_id, "/");
}
strcat(normalised_module_id, *it);
}
}