php_v8js_macros.h 8.4 KB

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