php_v8js_macros.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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::NewSymbol(v, sizeof(v) - 1)
  31. #define V8JS_SYML(v, l) v8::String::NewSymbol(v, l)
  32. #define V8JS_STR(v) v8::String::New(v)
  33. #define V8JS_STRL(v, l) v8::String::New(v, l)
  34. #define V8JS_INT(v) v8::Integer::New(v)
  35. #define V8JS_FLOAT(v) v8::Number::New(v)
  36. #define V8JS_BOOL(v) v8::Boolean::New(v)
  37. #define V8JS_NULL v8::Null()
  38. #define V8JS_UNDEFINED v8::Undefined()
  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. #else
  57. # define ZEND_HASH_KEY_DC
  58. # define ZEND_HASH_KEY_CC
  59. #endif
  60. /* Global flags */
  61. #define V8JS_GLOBAL_SET_FLAGS(flags) V8JS_GLOBAL->SetHiddenValue(V8JS_SYM("__php_flags__"), V8JS_INT(flags))
  62. #define V8JS_GLOBAL_GET_FLAGS() V8JS_GLOBAL->GetHiddenValue(V8JS_SYM("__php_flags__"))->IntegerValue();
  63. /* Options */
  64. #define V8JS_FLAG_NONE (1<<0)
  65. #define V8JS_FLAG_FORCE_ARRAY (1<<1)
  66. #define V8JS_DEBUG_AUTO_BREAK_NEVER 0
  67. #define V8JS_DEBUG_AUTO_BREAK_ONCE 1
  68. #define V8JS_DEBUG_AUTO_BREAK_ALWAYS 2
  69. /* Extracts a C string from a V8 Utf8Value. */
  70. static inline const char * ToCString(const v8::String::Utf8Value &value) /* {{{ */
  71. {
  72. return *value ? *value : "<string conversion failed>";
  73. }
  74. /* }}} */
  75. /* Extern Class entries */
  76. extern zend_class_entry *php_ce_v8_object;
  77. extern zend_class_entry *php_ce_v8_function;
  78. /* Create PHP V8 object */
  79. void php_v8js_create_v8(zval *, v8::Handle<v8::Value>, int, v8::Isolate * TSRMLS_DC);
  80. /* Fetch V8 object properties */
  81. int php_v8js_v8_get_properties_hash(v8::Handle<v8::Value>, HashTable *, int, v8::Isolate * TSRMLS_DC);
  82. /* Convert zval into V8 value */
  83. v8::Handle<v8::Value> zval_to_v8js(zval *, v8::Isolate * TSRMLS_DC);
  84. /* Convert V8 value into zval */
  85. int v8js_to_zval(v8::Handle<v8::Value>, zval *, int, v8::Isolate * TSRMLS_DC);
  86. /* Register accessors into passed object */
  87. void php_v8js_register_accessors(v8::Local<v8::ObjectTemplate>, zval *, v8::Isolate * TSRMLS_DC);
  88. /* {{{ Context container */
  89. struct php_v8js_ctx {
  90. zend_object std;
  91. v8::Persistent<v8::String> object_name;
  92. v8::Persistent<v8::Context> context;
  93. zend_bool report_uncaught;
  94. zval *pending_exception;
  95. int in_execution;
  96. v8::Isolate *isolate;
  97. bool time_limit_hit;
  98. bool memory_limit_hit;
  99. v8::Persistent<v8::FunctionTemplate> global_template;
  100. zval *module_loader;
  101. std::vector<char *> modules_stack;
  102. std::vector<char *> modules_base;
  103. };
  104. /* }}} */
  105. // Timer context
  106. struct php_v8js_timer_ctx
  107. {
  108. long time_limit;
  109. long memory_limit;
  110. std::chrono::time_point<std::chrono::high_resolution_clock> time_point;
  111. php_v8js_ctx *v8js_ctx;
  112. };
  113. /* {{{ Object container */
  114. struct php_v8js_object {
  115. zend_object std;
  116. v8::Persistent<v8::Value> v8obj;
  117. int flags;
  118. v8::Isolate *isolate;
  119. };
  120. /* }}} */
  121. /* Module globals */
  122. ZEND_BEGIN_MODULE_GLOBALS(v8js)
  123. int v8_initialized;
  124. HashTable *extensions;
  125. int disposed_contexts; /* Disposed contexts since last time V8 did GC */
  126. /* Ini globals */
  127. char *v8_flags; /* V8 command line flags */
  128. int max_disposed_contexts; /* Max disposed context allowed before forcing V8 GC */
  129. // Timer thread globals
  130. std::stack<php_v8js_timer_ctx *> timer_stack;
  131. std::thread *timer_thread;
  132. std::mutex timer_mutex;
  133. bool timer_stop;
  134. std::map<char *, v8::Handle<v8::Object> > modules_loaded;
  135. ZEND_END_MODULE_GLOBALS(v8js)
  136. extern zend_v8js_globals v8js_globals;
  137. ZEND_EXTERN_MODULE_GLOBALS(v8js)
  138. #ifdef ZTS
  139. # define V8JSG(v) TSRMG(v8js_globals_id, zend_v8js_globals *, v)
  140. #else
  141. # define V8JSG(v) (v8js_globals.v)
  142. #endif
  143. /* Register builtin methods into passed object */
  144. void php_v8js_register_methods(v8::Handle<v8::ObjectTemplate>, php_v8js_ctx *c);
  145. #endif /* PHP_V8JS_MACROS_H */
  146. /*
  147. * Local variables:
  148. * tab-width: 4
  149. * c-basic-offset: 4
  150. * End:
  151. * vim600: noet sw=4 ts=4 fdm=marker
  152. * vim<600: noet sw=4 ts=4
  153. */