php_v8js_macros.h 8.7 KB

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