php_v8js_macros.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2017 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. | Author: Stefan Siegl <[email protected]> |
  12. +----------------------------------------------------------------------+
  13. */
  14. #ifndef PHP_V8JS_MACROS_H
  15. #define PHP_V8JS_MACROS_H
  16. #if __GNUC__ == 4 && __GNUC_MINOR__ == 4
  17. #define _GLIBCXX_USE_NANOSLEEP 1
  18. #endif
  19. #include <chrono>
  20. #include <deque>
  21. #include <thread>
  22. #include <map>
  23. #include <list>
  24. #include <vector>
  25. #include <mutex>
  26. #include <cmath>
  27. extern "C" {
  28. #ifndef _WIN32
  29. #include "php_config.h"
  30. #endif
  31. /* work around incompatibilities regarding isnan() and isfinite() macros,
  32. * affecting PHP versions before 7.4. */
  33. #undef HAVE_DECL_ISFINITE
  34. #undef HAVE_DECL_ISNAN
  35. #define HAVE_DECL_ISFINITE 0
  36. #define HAVE_DECL_ISNAN 0
  37. #include "php.h"
  38. #include "php_v8js.h"
  39. }
  40. #ifdef _WIN32
  41. /* On Windows a symbol COMPILER is defined. However v8.h has an enum with that
  42. * name which hence would be broken unless undefined here. */
  43. #undef COMPILER
  44. #endif
  45. #ifdef V8_HAS_INITIALIZE_SANDBOX
  46. #define V8_ENABLE_SANDBOX 1
  47. #endif
  48. #include <v8.h>
  49. #include <v8-platform.h>
  50. #include "v8js_class.h"
  51. #include "v8js_v8.h"
  52. #ifndef PATH_MAX
  53. /* Some platforms (Windows among others) don't have a PATH_MAX, for the moment
  54. * just assume an arbitrary upper bound of 4096 chars.
  55. * Anyways we should fix (get rid of) the code that uses PATH_MAX as it doesn't
  56. * even check for buffer overflows. FIXME */
  57. #define PATH_MAX 4096
  58. #endif
  59. /* V8Js Version */
  60. #define PHP_V8JS_VERSION "2.1.2"
  61. /* Options */
  62. #define V8JS_FLAG_NONE (1<<0)
  63. #define V8JS_FLAG_FORCE_ARRAY (1<<1)
  64. #define V8JS_FLAG_PROPAGATE_PHP_EXCEPTIONS (1<<2)
  65. /* These are not defined by Zend */
  66. #define ZEND_WAKEUP_FUNC_NAME "__wakeup"
  67. #define ZEND_SLEEP_FUNC_NAME "__sleep"
  68. #define ZEND_SET_STATE_FUNC_NAME "__set_state"
  69. /* Convert zval into V8 value */
  70. v8::Local<v8::Value> zval_to_v8js(zval *, v8::Isolate *);
  71. /* Convert zend_long into V8 value */
  72. v8::Local<v8::Value> zend_long_to_v8js(zend_long, v8::Isolate *);
  73. /* Convert V8 value into zval */
  74. int v8js_to_zval(v8::Local<v8::Value>, zval *, int, v8::Isolate *);
  75. struct v8js_accessor_ctx
  76. {
  77. zend_string *variable_name;
  78. v8::Isolate *isolate;
  79. };
  80. void v8js_accessor_ctx_dtor(v8js_accessor_ctx *);
  81. /* Register accessors into passed object */
  82. void v8js_register_accessors(std::vector<v8js_accessor_ctx*> *accessor_list, v8::Local<v8::FunctionTemplate>, zval *, v8::Isolate *);
  83. /* Forward declarations */
  84. struct v8js_timer_ctx;
  85. /* Module globals */
  86. ZEND_BEGIN_MODULE_GLOBALS(v8js)
  87. // Thread-local cache whether V8 has been initialized so far
  88. bool v8_initialized;
  89. /* Ini globals */
  90. bool use_date; /* Generate JS Date objects instead of PHP DateTime */
  91. bool use_array_access; /* Convert ArrayAccess, Countable objects to array-like objects */
  92. // Timer thread globals
  93. std::deque<v8js_timer_ctx *> timer_stack;
  94. std::thread *timer_thread;
  95. std::mutex timer_mutex;
  96. bool timer_stop;
  97. bool fatal_error_abort;
  98. ZEND_END_MODULE_GLOBALS(v8js)
  99. extern zend_v8js_globals v8js_globals;
  100. ZEND_EXTERN_MODULE_GLOBALS(v8js)
  101. #define V8JSG(v) ZEND_MODULE_GLOBALS_ACCESSOR(v8js, v)
  102. /*
  103. * Process-wide globals
  104. *
  105. * The zend_v8js_globals structure declared above is created once per thread
  106. * (in a ZTS environment). If a multi-threaded PHP process uses V8 there is
  107. * some stuff shared among all of these threads
  108. *
  109. * - whether V8 has been initialized at all
  110. * - the V8 backend platform
  111. * - loaded extensions
  112. * - V8 "command line" flags
  113. *
  114. * In a ZTS-enabled environment access to all of these variables must happen
  115. * while holding a mutex lock.
  116. */
  117. struct _v8js_process_globals {
  118. #ifdef ZTS
  119. bool v8_initialized;
  120. std::mutex lock;
  121. #endif
  122. HashTable *extensions;
  123. /* V8 command line flags */
  124. char *v8_flags;
  125. /* Path to icudtl.dat file */
  126. char *icudtl_dat_path;
  127. std::unique_ptr<v8::Platform> v8_platform;
  128. };
  129. extern struct _v8js_process_globals v8js_process_globals;
  130. /* Register builtin methods into passed object */
  131. void v8js_register_methods(v8::Local<v8::ObjectTemplate>, v8js_ctx *c);
  132. #ifdef ZEND_HASH_INC_APPLY_COUNT
  133. #ifndef GC_PROTECT_RECURSION
  134. # define GC_PROTECT_RECURSION(ht) ZEND_HASH_INC_APPLY_COUNT(ht)
  135. #endif
  136. #endif
  137. #ifdef ZEND_HASH_DEC_APPLY_COUNT
  138. #ifndef GC_UNPROTECT_RECURSION
  139. # define GC_UNPROTECT_RECURSION(ht) ZEND_HASH_DEC_APPLY_COUNT(ht)
  140. #endif
  141. #endif
  142. #endif /* PHP_V8JS_MACROS_H */
  143. /*
  144. * Local variables:
  145. * tab-width: 4
  146. * c-basic-offset: 4
  147. * indent-tabs-mode: t
  148. * End:
  149. * vim600: noet sw=4 ts=4 fdm=marker
  150. * vim<600: noet sw=4 ts=4
  151. */