php_v8js_macros.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. /* Helper macros */
  30. #define V8JS_SYM(v) v8::String::NewFromUtf8(isolate, v, v8::String::kInternalizedString, sizeof(v) - 1)
  31. #define V8JS_SYML(v, l) v8::String::NewFromUtf8(isolate, v, v8::String::kInternalizedString, l)
  32. #define V8JS_STR(v) v8::String::NewFromUtf8(isolate, v)
  33. #define V8JS_STRL(v, l) v8::String::NewFromUtf8(isolate, v, v8::String::kNormalString, l)
  34. #define V8JS_INT(v) v8::Integer::New(v, isolate)
  35. #define V8JS_FLOAT(v) v8::Number::New(isolate, v)
  36. #define V8JS_BOOL(v) ((v)?v8::True(isolate):v8::False(isolate))
  37. #define V8JS_NULL v8::Null(isolate)
  38. #define V8JS_UNDEFINED v8::Undefined(isolate)
  39. #define V8JS_MN(name) v8js_method_##name
  40. #define V8JS_METHOD(name) void V8JS_MN(name)(const v8::FunctionCallbackInfo<v8::Value>& info)
  41. #define V8JS_THROW(type, message, message_len) v8::ThrowException(v8::Exception::type(V8JS_STRL(message, message_len)))
  42. #define V8JS_GLOBAL v8::Context::GetCurrent()->Global()
  43. /* Helper macros */
  44. #if PHP_V8_API_VERSION < 2005009
  45. # define V8JS_GET_CLASS_NAME(var, obj) \
  46. /* Hack to prevent calling possibly set user-defined __toString() messing class name */ \
  47. v8::Local<v8::Function> constructor = v8::Local<v8::Function>::Cast(obj->Get(V8JS_SYM("constructor"))); \
  48. v8::String::Utf8Value var(constructor->GetName());
  49. #else
  50. # define V8JS_GET_CLASS_NAME(var, obj) \
  51. v8::String::Utf8Value var(obj->GetConstructorName());
  52. #endif
  53. #if ZEND_MODULE_API_NO >= 20100409
  54. # define ZEND_HASH_KEY_DC , const zend_literal *key
  55. # define ZEND_HASH_KEY_CC , key
  56. # define ZEND_HASH_KEY_NULL , NULL
  57. #else
  58. # define ZEND_HASH_KEY_DC
  59. # define ZEND_HASH_KEY_CC
  60. # define ZEND_HASH_KEY_NULL
  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. /* Global flags */
  70. #define V8JS_GLOBAL_SET_FLAGS(flags) V8JS_GLOBAL->SetHiddenValue(V8JS_SYM("__php_flags__"), V8JS_INT(flags))
  71. #define V8JS_GLOBAL_GET_FLAGS() V8JS_GLOBAL->GetHiddenValue(V8JS_SYM("__php_flags__"))->IntegerValue();
  72. /* Options */
  73. #define V8JS_FLAG_NONE (1<<0)
  74. #define V8JS_FLAG_FORCE_ARRAY (1<<1)
  75. #define V8JS_DEBUG_AUTO_BREAK_NEVER 0
  76. #define V8JS_DEBUG_AUTO_BREAK_ONCE 1
  77. #define V8JS_DEBUG_AUTO_BREAK_ALWAYS 2
  78. /* Extracts a C string from a V8 Utf8Value. */
  79. static inline const char * ToCString(const v8::String::Utf8Value &value) /* {{{ */
  80. {
  81. return *value ? *value : "<string conversion failed>";
  82. }
  83. /* }}} */
  84. /* Extern Class entries */
  85. extern zend_class_entry *php_ce_v8_object;
  86. extern zend_class_entry *php_ce_v8_function;
  87. /* Create PHP V8 object */
  88. void php_v8js_create_v8(zval *, v8::Handle<v8::Value>, int, v8::Isolate * TSRMLS_DC);
  89. /* Fetch V8 object properties */
  90. int php_v8js_v8_get_properties_hash(v8::Handle<v8::Value>, HashTable *, int, v8::Isolate * TSRMLS_DC);
  91. /* Convert zval into V8 value */
  92. v8::Handle<v8::Value> zval_to_v8js(zval *, v8::Isolate * TSRMLS_DC);
  93. /* Convert V8 value into zval */
  94. int v8js_to_zval(v8::Handle<v8::Value>, zval *, int, v8::Isolate * TSRMLS_DC);
  95. /* Register accessors into passed object */
  96. void php_v8js_register_accessors(v8::Local<v8::ObjectTemplate>, zval *, v8::Isolate * TSRMLS_DC);
  97. /* {{{ Context container */
  98. struct php_v8js_ctx {
  99. zend_object std;
  100. v8::Persistent<v8::String> object_name;
  101. v8::Persistent<v8::Context> context;
  102. zend_bool report_uncaught;
  103. zval *pending_exception;
  104. int in_execution;
  105. v8::Isolate *isolate;
  106. bool time_limit_hit;
  107. bool memory_limit_hit;
  108. v8::Persistent<v8::FunctionTemplate> global_template;
  109. zval *module_loader;
  110. std::vector<char *> modules_stack;
  111. std::vector<char *> modules_base;
  112. };
  113. /* }}} */
  114. // Timer context
  115. struct php_v8js_timer_ctx
  116. {
  117. long time_limit;
  118. long memory_limit;
  119. std::chrono::time_point<std::chrono::high_resolution_clock> time_point;
  120. php_v8js_ctx *v8js_ctx;
  121. };
  122. /* {{{ Object container */
  123. struct php_v8js_object {
  124. zend_object std;
  125. v8::Persistent<v8::Value> v8obj;
  126. int flags;
  127. v8::Isolate *isolate;
  128. };
  129. /* }}} */
  130. /* Module globals */
  131. ZEND_BEGIN_MODULE_GLOBALS(v8js)
  132. int v8_initialized;
  133. HashTable *extensions;
  134. int disposed_contexts; /* Disposed contexts since last time V8 did GC */
  135. /* Ini globals */
  136. char *v8_flags; /* V8 command line flags */
  137. int max_disposed_contexts; /* Max disposed context allowed before forcing V8 GC */
  138. // Timer thread globals
  139. std::stack<php_v8js_timer_ctx *> timer_stack;
  140. std::thread *timer_thread;
  141. std::mutex timer_mutex;
  142. bool timer_stop;
  143. std::map<char *, v8::Handle<v8::Object> > modules_loaded;
  144. ZEND_END_MODULE_GLOBALS(v8js)
  145. extern zend_v8js_globals v8js_globals;
  146. ZEND_EXTERN_MODULE_GLOBALS(v8js)
  147. #ifdef ZTS
  148. # define V8JSG(v) TSRMG(v8js_globals_id, zend_v8js_globals *, v)
  149. #else
  150. # define V8JSG(v) (v8js_globals.v)
  151. #endif
  152. /* Register builtin methods into passed object */
  153. void php_v8js_register_methods(v8::Handle<v8::ObjectTemplate>, php_v8js_ctx *c);
  154. #endif /* PHP_V8JS_MACROS_H */
  155. /*
  156. * Local variables:
  157. * tab-width: 4
  158. * c-basic-offset: 4
  159. * End:
  160. * vim600: noet sw=4 ts=4 fdm=marker
  161. * vim<600: noet sw=4 ts=4
  162. */