php_v8js_macros.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. /* Extracts a C string from a V8 Utf8Value. */
  67. static const char * ToCString(const v8::String::Utf8Value &value) /* {{{ */
  68. {
  69. return *value ? *value : "<string conversion failed>";
  70. }
  71. /* }}} */
  72. /* Extern Class entries */
  73. extern zend_class_entry *php_ce_v8_object;
  74. extern zend_class_entry *php_ce_v8_function;
  75. /* Create PHP V8 object */
  76. void php_v8js_create_v8(zval *, v8::Handle<v8::Value>, int, v8::Isolate * TSRMLS_DC);
  77. /* Fetch V8 object properties */
  78. int php_v8js_v8_get_properties_hash(v8::Handle<v8::Value>, HashTable *, int, v8::Isolate * TSRMLS_DC);
  79. /* Convert zval into V8 value */
  80. v8::Handle<v8::Value> zval_to_v8js(zval *, v8::Isolate * TSRMLS_DC);
  81. /* Convert V8 value into zval */
  82. int v8js_to_zval(v8::Handle<v8::Value>, zval *, int, v8::Isolate * TSRMLS_DC);
  83. /* Register accessors into passed object */
  84. void php_v8js_register_accessors(v8::Local<v8::ObjectTemplate>, zval *, v8::Isolate * TSRMLS_DC);
  85. /* {{{ Context container */
  86. struct php_v8js_ctx {
  87. zend_object std;
  88. v8::Persistent<v8::String> object_name;
  89. v8::Persistent<v8::Context> context;
  90. zend_bool report_uncaught;
  91. zval *pending_exception;
  92. int in_execution;
  93. v8::Isolate *isolate;
  94. bool time_limit_hit;
  95. bool memory_limit_hit;
  96. v8::Persistent<v8::FunctionTemplate> global_template;
  97. zval *module_loader;
  98. std::vector<char *> modules_stack;
  99. std::vector<char *> modules_base;
  100. };
  101. /* }}} */
  102. // Timer context
  103. struct php_v8js_timer_ctx
  104. {
  105. long time_limit;
  106. long memory_limit;
  107. std::chrono::time_point<std::chrono::high_resolution_clock> time_point;
  108. php_v8js_ctx *v8js_ctx;
  109. };
  110. /* Module globals */
  111. ZEND_BEGIN_MODULE_GLOBALS(v8js)
  112. int v8_initialized;
  113. HashTable *extensions;
  114. int disposed_contexts; /* Disposed contexts since last time V8 did GC */
  115. /* Ini globals */
  116. char *v8_flags; /* V8 command line flags */
  117. int max_disposed_contexts; /* Max disposed context allowed before forcing V8 GC */
  118. // Timer thread globals
  119. std::stack<php_v8js_timer_ctx *> timer_stack;
  120. std::thread *timer_thread;
  121. std::mutex timer_mutex;
  122. bool timer_stop;
  123. std::map<char *, v8::Handle<v8::Object> > modules_loaded;
  124. ZEND_END_MODULE_GLOBALS(v8js)
  125. extern zend_v8js_globals v8js_globals;
  126. #ifdef ZTS
  127. # define V8JSG(v) TSRMG(v8js_globals_id, zend_v8js_globals *, v)
  128. #else
  129. # define V8JSG(v) (v8js_globals.v)
  130. #endif
  131. /* Register builtin methods into passed object */
  132. void php_v8js_register_methods(v8::Handle<v8::ObjectTemplate>, php_v8js_ctx *c);
  133. #endif /* PHP_V8JS_MACROS_H */
  134. /*
  135. * Local variables:
  136. * tab-width: 4
  137. * c-basic-offset: 4
  138. * End:
  139. * vim600: noet sw=4 ts=4 fdm=marker
  140. * vim<600: noet sw=4 ts=4
  141. */