v8js_commonjs.cc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  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. extern "C" {
  17. #include "php.h"
  18. #include "zend_exceptions.h"
  19. }
  20. #include "php_v8js_macros.h"
  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. // Initialise the term string
  25. *term = 0;
  26. while (*identifier > 0) {
  27. if (*identifier == '/') {
  28. if (strlen(term) > 0) {
  29. // Terminate term string and add to terms vector
  30. *ptr++ = 0;
  31. terms.push_back(estrdup(term));
  32. // Reset term string
  33. memset(term, 0, strlen(term));
  34. ptr = term;
  35. }
  36. } else {
  37. *ptr++ = *identifier;
  38. }
  39. identifier++;
  40. }
  41. if (strlen(term) > 0) {
  42. // Terminate term string and add to terms vector
  43. *ptr++ = 0;
  44. terms.push_back(estrdup(term));
  45. }
  46. efree(term);
  47. }
  48. void v8js_commonjs_normalise_identifier(const char *base, const char *identifier, char *normalised_path, char *module_name)
  49. {
  50. std::vector<char *> id_terms, terms;
  51. v8js_commonjs_split_terms(identifier, id_terms);
  52. // If we have a relative module identifier then include the base terms
  53. if (!strcmp(id_terms.front(), ".") || !strcmp(id_terms.front(), "..")) {
  54. v8js_commonjs_split_terms(base, terms);
  55. }
  56. terms.insert(terms.end(), id_terms.begin(), id_terms.end());
  57. std::vector<char *> normalised_terms;
  58. for (std::vector<char *>::iterator it = terms.begin(); it != terms.end(); it++) {
  59. char *term = *it;
  60. if (!strcmp(term, "..")) {
  61. // Ignore parent term (..) if it's the first normalised term
  62. if (normalised_terms.size() > 0) {
  63. // Remove the parent normalized term (and free it)
  64. efree(normalised_terms.back());
  65. normalised_terms.pop_back();
  66. }
  67. // free the ".." term
  68. efree(term);
  69. } else if (strcmp(term, ".")) {
  70. // Add the term if it's not the current term (.)
  71. normalised_terms.push_back(term);
  72. } else {
  73. // Discard "." term
  74. efree(term);
  75. }
  76. }
  77. // Initialise the normalised path string
  78. *normalised_path = 0;
  79. *module_name = 0;
  80. strcat(module_name, normalised_terms.back());
  81. efree(normalised_terms.back());
  82. normalised_terms.pop_back();
  83. for (std::vector<char *>::iterator it = normalised_terms.begin(); it != normalised_terms.end(); it++) {
  84. char *term = *it;
  85. if (strlen(normalised_path) > 0) {
  86. strcat(normalised_path, "/");
  87. }
  88. strcat(normalised_path, term);
  89. efree(term);
  90. }
  91. }