php_v8js_macros.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. /* $Id$ */
  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. extern "C" {
  20. #include "php.h"
  21. #include "php_v8js.h"
  22. }
  23. #include <v8.h>
  24. #include <chrono>
  25. #include <stack>
  26. #include <thread>
  27. #include <map>
  28. #include <vector>
  29. #include <mutex>
  30. /* V8Js Version */
  31. #define V8JS_VERSION "0.1.3"
  32. /* V8 from 3.23.12 has most v8::Anything::New constructors expect isolates
  33. as their first argument. Older versions don't provide these. */
  34. #if PHP_V8_API_VERSION < 3023012
  35. #define V8JS_NEW(t, i, v...) t::New(v)
  36. #else
  37. #define V8JS_NEW(t, i, v...) t::New(i, v)
  38. #endif
  39. /* Helper macros */
  40. #define V8JS_SYM(v) v8::String::NewFromUtf8(isolate, v, v8::String::kInternalizedString, sizeof(v) - 1)
  41. #define V8JS_SYML(v, l) v8::String::NewFromUtf8(isolate, v, v8::String::kInternalizedString, l)
  42. #define V8JS_STR(v) v8::String::NewFromUtf8(isolate, v)
  43. #define V8JS_STRL(v, l) v8::String::NewFromUtf8(isolate, v, v8::String::kNormalString, l)
  44. #define V8JS_INT(v) v8::Integer::New(v, isolate)
  45. #define V8JS_FLOAT(v) v8::Number::New(isolate, v)
  46. #define V8JS_BOOL(v) ((v)?v8::True(isolate):v8::False(isolate))
  47. #define V8JS_DATE(v) v8::Date::New(isolate, v)
  48. #define V8JS_NULL v8::Null(isolate)
  49. #define V8JS_UNDEFINED v8::Undefined(isolate)
  50. #define V8JS_MN(name) v8js_method_##name
  51. #define V8JS_METHOD(name) void V8JS_MN(name)(const v8::FunctionCallbackInfo<v8::Value>& info)
  52. #define V8JS_THROW(isolate, type, message, message_len) (isolate)->ThrowException(v8::Exception::type(V8JS_STRL(message, message_len)))
  53. #define V8JS_GLOBAL(isolate) ((isolate)->GetCurrentContext()->Global())
  54. #if PHP_V8_API_VERSION < 3022000
  55. /* CopyablePersistentTraits is only part of V8 from 3.22.0 on,
  56. to be compatible with lower versions add our own (compatible) version. */
  57. namespace v8 {
  58. template<class T>
  59. struct CopyablePersistentTraits {
  60. typedef Persistent<T, CopyablePersistentTraits<T> > CopyablePersistent;
  61. static const bool kResetInDestructor = true;
  62. template<class S, class M>
  63. #if PHP_V8_API_VERSION >= 3021015
  64. static V8_INLINE void Copy(const Persistent<S, M>& source,
  65. CopyablePersistent* dest)
  66. #else
  67. V8_INLINE(static void Copy(const Persistent<S, M>& source,
  68. CopyablePersistent* dest))
  69. #endif
  70. {
  71. // do nothing, just allow copy
  72. }
  73. };
  74. }
  75. #endif
  76. /* Abbreviate long type names */
  77. typedef v8::Persistent<v8::FunctionTemplate, v8::CopyablePersistentTraits<v8::FunctionTemplate> > v8js_tmpl_t;
  78. /* Hidden field name used to link JS wrappers with underlying PHP object */
  79. #define PHPJS_OBJECT_KEY "phpjs::object"
  80. /* Helper macros */
  81. #if PHP_V8_API_VERSION < 2005009
  82. # define V8JS_GET_CLASS_NAME(var, obj) \
  83. /* Hack to prevent calling possibly set user-defined __toString() messing class name */ \
  84. v8::Local<v8::Function> constructor = v8::Local<v8::Function>::Cast(obj->Get(V8JS_SYM("constructor"))); \
  85. v8::String::Utf8Value var(constructor->GetName());
  86. #else
  87. # define V8JS_GET_CLASS_NAME(var, obj) \
  88. v8::String::Utf8Value var(obj->GetConstructorName());
  89. #endif
  90. #if ZEND_MODULE_API_NO >= 20100409
  91. # define ZEND_HASH_KEY_DC , const zend_literal *key
  92. # define ZEND_HASH_KEY_CC , key
  93. # define ZEND_HASH_KEY_NULL , NULL
  94. #else
  95. # define ZEND_HASH_KEY_DC
  96. # define ZEND_HASH_KEY_CC
  97. # define ZEND_HASH_KEY_NULL
  98. #endif
  99. /* method signatures of zend_update_property and zend_read_property were
  100. * declared as 'char *' instead of 'const char *' before PHP 5.4 */
  101. #if ZEND_MODULE_API_NO >= 20100525
  102. # define V8JS_CONST
  103. #else
  104. # define V8JS_CONST (char *)
  105. #endif
  106. /* Global flags */
  107. #define V8JS_GLOBAL_SET_FLAGS(isolate,flags) V8JS_GLOBAL(isolate)->SetHiddenValue(V8JS_SYM("__php_flags__"), V8JS_INT(flags))
  108. #define V8JS_GLOBAL_GET_FLAGS(isolate) V8JS_GLOBAL(isolate)->GetHiddenValue(V8JS_SYM("__php_flags__"))->IntegerValue();
  109. /* Options */
  110. #define V8JS_FLAG_NONE (1<<0)
  111. #define V8JS_FLAG_FORCE_ARRAY (1<<1)
  112. #define V8JS_DEBUG_AUTO_BREAK_NEVER 0
  113. #define V8JS_DEBUG_AUTO_BREAK_ONCE 1
  114. #define V8JS_DEBUG_AUTO_BREAK_ALWAYS 2
  115. /* Extracts a C string from a V8 Utf8Value. */
  116. static inline const char * ToCString(const v8::String::Utf8Value &value) /* {{{ */
  117. {
  118. return *value ? *value : "<string conversion failed>";
  119. }
  120. /* }}} */
  121. /* Extern Class entries */
  122. extern zend_class_entry *php_ce_v8_object;
  123. extern zend_class_entry *php_ce_v8_function;
  124. /* Create PHP V8 object */
  125. void php_v8js_create_v8(zval *, v8::Handle<v8::Value>, int, v8::Isolate * TSRMLS_DC);
  126. /* Fetch V8 object properties */
  127. int php_v8js_v8_get_properties_hash(v8::Handle<v8::Value>, HashTable *, int, v8::Isolate * TSRMLS_DC);
  128. /* Convert zval into V8 value */
  129. v8::Handle<v8::Value> zval_to_v8js(zval *, v8::Isolate * TSRMLS_DC);
  130. /* Convert V8 value into zval */
  131. int v8js_to_zval(v8::Handle<v8::Value>, zval *, int, v8::Isolate * TSRMLS_DC);
  132. struct php_v8js_accessor_ctx
  133. {
  134. char *variable_name_string;
  135. uint variable_name_string_len;
  136. v8::Isolate *isolate;
  137. };
  138. void php_v8js_accessor_ctx_dtor(php_v8js_accessor_ctx * TSRMLS_DC);
  139. /* Register accessors into passed object */
  140. void php_v8js_register_accessors(std::vector<php_v8js_accessor_ctx*> *accessor_list, v8::Local<v8::FunctionTemplate>, zval *, v8::Isolate * TSRMLS_DC);
  141. /* {{{ Context container */
  142. struct php_v8js_ctx {
  143. zend_object std;
  144. v8::Persistent<v8::String> object_name;
  145. v8::Persistent<v8::Context> context;
  146. zend_bool report_uncaught;
  147. zval *pending_exception;
  148. int in_execution;
  149. v8::Isolate *isolate;
  150. bool time_limit_hit;
  151. bool memory_limit_hit;
  152. v8::Persistent<v8::FunctionTemplate> global_template;
  153. zval *module_loader;
  154. std::vector<char *> modules_stack;
  155. std::vector<char *> modules_base;
  156. std::map<const char *,v8js_tmpl_t> template_cache;
  157. std::vector<php_v8js_accessor_ctx *> accessor_list;
  158. char *tz;
  159. #ifdef ZTS
  160. void ***zts_ctx;
  161. #endif
  162. };
  163. /* }}} */
  164. #ifdef ZTS
  165. # if PHP_V8_API_VERSION <= 3023008
  166. /* Until V8 3.23.8 Isolate could only take one external pointer. */
  167. # define V8JS_TSRMLS_FETCH() TSRMLS_FETCH_FROM_CTX(((php_v8js_ctx *) isolate->GetData())->zts_ctx);
  168. # else
  169. # define V8JS_TSRMLS_FETCH() TSRMLS_FETCH_FROM_CTX(((php_v8js_ctx *) isolate->GetData(0))->zts_ctx);
  170. # endif
  171. #else
  172. # define V8JS_TSRMLS_FETCH()
  173. #endif
  174. // Timer context
  175. struct php_v8js_timer_ctx
  176. {
  177. long time_limit;
  178. long memory_limit;
  179. std::chrono::time_point<std::chrono::high_resolution_clock> time_point;
  180. php_v8js_ctx *v8js_ctx;
  181. bool killed;
  182. };
  183. /* {{{ Object container */
  184. struct php_v8js_object {
  185. zend_object std;
  186. v8::Persistent<v8::Value> v8obj;
  187. int flags;
  188. v8::Isolate *isolate;
  189. HashTable *properties;
  190. };
  191. /* }}} */
  192. /* Module globals */
  193. ZEND_BEGIN_MODULE_GLOBALS(v8js)
  194. int v8_initialized;
  195. HashTable *extensions;
  196. /* Ini globals */
  197. char *v8_flags; /* V8 command line flags */
  198. bool use_date; /* Generate JS Date objects instead of PHP DateTime */
  199. // Timer thread globals
  200. std::stack<php_v8js_timer_ctx *> timer_stack;
  201. std::thread *timer_thread;
  202. std::mutex timer_mutex;
  203. bool timer_stop;
  204. std::map<char *, v8::Handle<v8::Object> > modules_loaded;
  205. ZEND_END_MODULE_GLOBALS(v8js)
  206. extern zend_v8js_globals v8js_globals;
  207. ZEND_EXTERN_MODULE_GLOBALS(v8js)
  208. #ifdef ZTS
  209. # define V8JSG(v) TSRMG(v8js_globals_id, zend_v8js_globals *, v)
  210. #else
  211. # define V8JSG(v) (v8js_globals.v)
  212. #endif
  213. /* Register builtin methods into passed object */
  214. void php_v8js_register_methods(v8::Handle<v8::ObjectTemplate>, php_v8js_ctx *c);
  215. #endif /* PHP_V8JS_MACROS_H */
  216. /*
  217. * Local variables:
  218. * tab-width: 4
  219. * c-basic-offset: 4
  220. * End:
  221. * vim600: noet sw=4 ts=4 fdm=marker
  222. * vim<600: noet sw=4 ts=4
  223. */