v8js_commonjs.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. void v8js_commonjs_split_terms(char *identifier, std::vector<char *> &terms)
  22. {
  23. char *term = (char *)malloc(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(strdup(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(strdup(term));
  45. }
  46. if (term > 0) {
  47. free(term);
  48. }
  49. }
  50. void v8js_commonjs_normalise_identifier(char *base, char *identifier, char *normalised_path, char *module_name)
  51. {
  52. std::vector<char *> id_terms, terms;
  53. v8js_commonjs_split_terms(identifier, id_terms);
  54. // If we have a relative module identifier then include the base terms
  55. if (!strcmp(id_terms.front(), ".") || !strcmp(id_terms.front(), "..")) {
  56. v8js_commonjs_split_terms(base, terms);
  57. }
  58. terms.insert(terms.end(), id_terms.begin(), id_terms.end());
  59. std::vector<char *> normalised_terms;
  60. for (std::vector<char *>::iterator it = terms.begin(); it != terms.end(); it++) {
  61. char *term = *it;
  62. if (!strcmp(term, "..")) {
  63. // Ignore parent term (..) if it's the first normalised term
  64. if (normalised_terms.size() > 0) {
  65. // Remove the parent normalized term
  66. normalised_terms.pop_back();
  67. }
  68. } else if (strcmp(term, ".")) {
  69. // Add the term if it's not the current term (.)
  70. normalised_terms.push_back(term);
  71. }
  72. }
  73. // Initialise the normalised path string
  74. *normalised_path = 0;
  75. *module_name = 0;
  76. strcat(module_name, normalised_terms.back());
  77. normalised_terms.pop_back();
  78. for (std::vector<char *>::iterator it = normalised_terms.begin(); it != normalised_terms.end(); it++) {
  79. char *term = *it;
  80. if (strlen(normalised_path) > 0) {
  81. strcat(normalised_path, "/");
  82. }
  83. strcat(normalised_path, term);
  84. }
  85. }