v8js_commonjs.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2012 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | [email protected] so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Author: Simon Best <[email protected]> |
  16. +----------------------------------------------------------------------+
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. #include "config.h"
  20. #endif
  21. extern "C" {
  22. #include "php.h"
  23. #include "zend_exceptions.h"
  24. }
  25. #include "php_v8js_macros.h"
  26. void php_v8js_commonjs_split_terms(char *identifier, std::vector<char *> &terms)
  27. {
  28. char *term = (char *)malloc(PATH_MAX), *ptr = term;
  29. // Initialise the term string
  30. *term = 0;
  31. while (*identifier > 0) {
  32. if (*identifier == '/') {
  33. if (strlen(term) > 0) {
  34. // Terminate term string and add to terms vector
  35. *ptr++ = 0;
  36. terms.push_back(strdup(term));
  37. // Reset term string
  38. memset(term, 0, strlen(term));
  39. ptr = term;
  40. }
  41. } else {
  42. *ptr++ = *identifier;
  43. }
  44. identifier++;
  45. }
  46. if (strlen(term) > 0) {
  47. // Terminate term string and add to terms vector
  48. *ptr++ = 0;
  49. terms.push_back(strdup(term));
  50. }
  51. if (term > 0) {
  52. free(term);
  53. }
  54. }
  55. void php_v8js_commonjs_normalise_identifier(char *base, char *identifier, char *normalised_path, char *module_name)
  56. {
  57. std::vector<char *> id_terms, terms;
  58. php_v8js_commonjs_split_terms(identifier, id_terms);
  59. // If we have a relative module identifier then include the base terms
  60. if (!strcmp(id_terms.front(), ".") || !strcmp(id_terms.front(), "..")) {
  61. php_v8js_commonjs_split_terms(base, terms);
  62. }
  63. terms.insert(terms.end(), id_terms.begin(), id_terms.end());
  64. std::vector<char *> normalised_terms;
  65. for (std::vector<char *>::iterator it = terms.begin(); it != terms.end(); it++) {
  66. char *term = *it;
  67. if (!strcmp(term, "..")) {
  68. // Ignore parent term (..) if it's the first normalised term
  69. if (normalised_terms.size() > 0) {
  70. // Remove the parent normalized term
  71. normalised_terms.pop_back();
  72. }
  73. } else if (strcmp(term, ".")) {
  74. // Add the term if it's not the current term (.)
  75. normalised_terms.push_back(term);
  76. }
  77. }
  78. // Initialise the normalised path string
  79. *normalised_path = 0;
  80. *module_name = 0;
  81. strcat(module_name, 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. }
  90. }