v8js_commonjs.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2013 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | http://www.opensource.org/licenses/mit-license.php MIT License |
  8. +----------------------------------------------------------------------+
  9. | Author: Jani Taskinen <[email protected]> |
  10. | Author: Patrick Reilly <[email protected]> |
  11. +----------------------------------------------------------------------+
  12. */
  13. #ifdef HAVE_CONFIG_H
  14. #include "config.h"
  15. #endif
  16. #include "php_v8js_macros.h"
  17. extern "C" {
  18. #include "php.h"
  19. #include "zend_exceptions.h"
  20. }
  21. static void v8js_commonjs_split_terms(const char *identifier, std::vector<char *> &terms)
  22. {
  23. char *term = (char *) emalloc(PATH_MAX), *ptr = term;
  24. while (*identifier > 0) {
  25. if (*identifier == '/') {
  26. if (ptr > term) {
  27. // Terminate term string and add to terms vector
  28. *ptr++ = 0;
  29. terms.push_back(estrdup(term));
  30. // Reset term string
  31. ptr = term;
  32. }
  33. } else {
  34. *ptr++ = *identifier;
  35. }
  36. identifier++;
  37. }
  38. if (ptr > term) {
  39. // Terminate term string and add to terms vector
  40. *ptr++ = 0;
  41. terms.push_back(estrdup(term));
  42. }
  43. efree(term);
  44. }
  45. void v8js_commonjs_normalise_identifier(const char *base, const char *identifier, char *normalised_path, char *module_name)
  46. {
  47. std::vector<char *> id_terms, terms;
  48. v8js_commonjs_split_terms(identifier, id_terms);
  49. // If we have a relative module identifier then include the base terms
  50. if (!strcmp(id_terms.front(), ".") || !strcmp(id_terms.front(), "..")) {
  51. v8js_commonjs_split_terms(base, terms);
  52. }
  53. terms.insert(terms.end(), id_terms.begin(), id_terms.end());
  54. std::vector<char *> normalised_terms;
  55. for (std::vector<char *>::iterator it = terms.begin(); it != terms.end(); it++) {
  56. char *term = *it;
  57. if (!strcmp(term, "..")) {
  58. // Ignore parent term (..) if it's the first normalised term
  59. if (normalised_terms.size() > 0) {
  60. // Remove the parent normalized term (and free it)
  61. efree(normalised_terms.back());
  62. normalised_terms.pop_back();
  63. }
  64. // free the ".." term
  65. efree(term);
  66. } else if (strcmp(term, ".")) {
  67. // Add the term if it's not the current term (.)
  68. normalised_terms.push_back(term);
  69. } else {
  70. // Discard "." term
  71. efree(term);
  72. }
  73. }
  74. // Initialise the normalised path string
  75. *normalised_path = 0;
  76. *module_name = 0;
  77. strcat(module_name, normalised_terms.back());
  78. efree(normalised_terms.back());
  79. normalised_terms.pop_back();
  80. for (std::vector<char *>::iterator it = normalised_terms.begin(); it != normalised_terms.end(); it++) {
  81. char *term = *it;
  82. if (strlen(normalised_path) > 0) {
  83. strcat(normalised_path, "/");
  84. }
  85. strcat(normalised_path, term);
  86. efree(term);
  87. }
  88. }
  89. /*
  90. * Local variables:
  91. * tab-width: 4
  92. * c-basic-offset: 4
  93. * indent-tabs-mode: t
  94. * End:
  95. * vim600: noet sw=4 ts=4 fdm=marker
  96. * vim<600: noet sw=4 ts=4
  97. */