php_v8js_macros.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2016 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. #ifndef isnan
  28. /* php.h requires the isnan() macro, which is removed by c++ <cmath> header,
  29. * work around: re-define the macro to std::isnan function */
  30. #define isnan(a) std::isnan(a)
  31. #endif
  32. extern "C" {
  33. #include "php.h"
  34. #include "php_v8js.h"
  35. }
  36. #ifdef _WIN32
  37. /* On Windows a symbol COMPILER is defined. However v8.h has an enum with that
  38. * name which hence would be broken unless undefined here. */
  39. #undef COMPILER
  40. #endif
  41. #include <v8.h>
  42. #include "v8js_class.h"
  43. #include "v8js_v8.h"
  44. #ifndef PATH_MAX
  45. /* Some platforms (Windows among others) don't have a PATH_MAX, for the moment
  46. * just assume an arbitrary upper bound of 4096 chars.
  47. * Anyways we should fix (get rid of) the code that uses PATH_MAX as it doesn't
  48. * even check for buffer overflows. FIXME */
  49. #define PATH_MAX 4096
  50. #endif
  51. /* V8Js Version */
  52. #define PHP_V8JS_VERSION "1.2.0"
  53. /* Hidden field name used to link JS wrappers with underlying PHP object */
  54. #define PHPJS_OBJECT_KEY "phpjs::object"
  55. /* Helper macros */
  56. #define V8JS_GET_CLASS_NAME(var, obj) \
  57. v8::String::Utf8Value var(obj->GetConstructorName());
  58. #if PHP_V8_API_VERSION >= 3030000
  59. #define V8JS_V8GENERATOR_SUPPORT 1
  60. #define V8JS_GENERATOR_EXPORT_SUPPORT 1
  61. #endif
  62. /* method signatures of zend_update_property and zend_read_property were
  63. * declared as 'char *' instead of 'const char *' before PHP 5.4 */
  64. #if ZEND_MODULE_API_NO >= 20100525
  65. # define V8JS_CONST
  66. #else
  67. # define V8JS_CONST (char *)
  68. #endif
  69. /* Options */
  70. #define V8JS_FLAG_NONE (1<<0)
  71. #define V8JS_FLAG_FORCE_ARRAY (1<<1)
  72. #define V8JS_FLAG_PROPAGATE_PHP_EXCEPTIONS (1<<2)
  73. /* These are not defined by Zend */
  74. #define ZEND_WAKEUP_FUNC_NAME "__wakeup"
  75. #define ZEND_SLEEP_FUNC_NAME "__sleep"
  76. #define ZEND_SET_STATE_FUNC_NAME "__set_state"
  77. /* Convert zval into V8 value */
  78. v8::Handle<v8::Value> zval_to_v8js(zval *, v8::Isolate * TSRMLS_DC);
  79. /* Convert V8 value into zval */
  80. int v8js_to_zval(v8::Handle<v8::Value>, zval *, int, v8::Isolate * TSRMLS_DC);
  81. struct v8js_accessor_ctx
  82. {
  83. zend_string *variable_name;
  84. v8::Isolate *isolate;
  85. };
  86. void v8js_accessor_ctx_dtor(v8js_accessor_ctx * TSRMLS_DC);
  87. /* Register accessors into passed object */
  88. void v8js_register_accessors(std::vector<v8js_accessor_ctx*> *accessor_list, v8::Local<v8::FunctionTemplate>, zval *, v8::Isolate * TSRMLS_DC);
  89. /* Forward declarations */
  90. struct v8js_timer_ctx;
  91. /* Module globals */
  92. ZEND_BEGIN_MODULE_GLOBALS(v8js)
  93. // Thread-local cache whether V8 has been initialized so far
  94. int v8_initialized;
  95. /* Ini globals */
  96. bool use_date; /* Generate JS Date objects instead of PHP DateTime */
  97. bool use_array_access; /* Convert ArrayAccess, Countable objects to array-like objects */
  98. bool compat_php_exceptions; /* Don't stop JS execution on PHP exception */
  99. // Timer thread globals
  100. std::deque<v8js_timer_ctx *> timer_stack;
  101. std::thread *timer_thread;
  102. std::mutex timer_mutex;
  103. bool timer_stop;
  104. bool fatal_error_abort;
  105. ZEND_END_MODULE_GLOBALS(v8js)
  106. extern zend_v8js_globals v8js_globals;
  107. ZEND_EXTERN_MODULE_GLOBALS(v8js)
  108. #define V8JSG(v) ZEND_MODULE_GLOBALS_ACCESSOR(v8js, v)
  109. /*
  110. * Process-wide globals
  111. *
  112. * The zend_v8js_globals structure declared above is created once per thread
  113. * (in a ZTS environment). If a multi-threaded PHP process uses V8 there is
  114. * some stuff shared among all of these threads
  115. *
  116. * - whether V8 has been initialized at all
  117. * - the V8 backend platform
  118. * - loaded extensions
  119. * - V8 "command line" flags
  120. *
  121. * In a ZTS-enabled environment access to all of these variables must happen
  122. * while holding a mutex lock.
  123. */
  124. struct _v8js_process_globals {
  125. #ifdef ZTS
  126. int v8_initialized;
  127. std::mutex lock;
  128. #endif
  129. HashTable *extensions;
  130. /* V8 command line flags */
  131. char *v8_flags;
  132. #if PHP_V8_API_VERSION >= 3029036
  133. v8::Platform *v8_platform;
  134. #endif
  135. };
  136. extern struct _v8js_process_globals v8js_process_globals;
  137. /* Register builtin methods into passed object */
  138. void v8js_register_methods(v8::Handle<v8::ObjectTemplate>, v8js_ctx *c);
  139. #endif /* PHP_V8JS_MACROS_H */
  140. /*
  141. * Local variables:
  142. * tab-width: 4
  143. * c-basic-offset: 4
  144. * End:
  145. * vim600: noet sw=4 ts=4 fdm=marker
  146. * vim<600: noet sw=4 ts=4
  147. */