php_v8js_macros.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2012 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | [email protected] so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Author: Jani Taskinen <[email protected]> |
  16. | Author: Patrick Reilly <[email protected]> |
  17. +----------------------------------------------------------------------+
  18. */
  19. /* $Id$ */
  20. #ifndef PHP_V8JS_MACROS_H
  21. #define PHP_V8JS_MACROS_H
  22. extern "C" {
  23. #include "php.h"
  24. #include "php_v8js.h"
  25. }
  26. #include <v8.h>
  27. #include <chrono>
  28. #include <stack>
  29. #include <thread>
  30. #include <map>
  31. #include <vector>
  32. #include <mutex>
  33. /* V8Js Version */
  34. #define V8JS_VERSION "0.1.3"
  35. /* Helper macros */
  36. #define V8JS_SYM(v) v8::String::NewSymbol(v, sizeof(v) - 1)
  37. #define V8JS_SYML(v, l) v8::String::NewSymbol(v, l)
  38. #define V8JS_STR(v) v8::String::New(v)
  39. #define V8JS_STRL(v, l) v8::String::New(v, l)
  40. #define V8JS_INT(v) v8::Integer::New(v)
  41. #define V8JS_FLOAT(v) v8::Number::New(v)
  42. #define V8JS_BOOL(v) v8::Boolean::New(v)
  43. #define V8JS_NULL v8::Null()
  44. #define V8JS_UNDEFINED v8::Undefined()
  45. #define V8JS_MN(name) v8js_method_##name
  46. #define V8JS_METHOD(name) void V8JS_MN(name)(const v8::FunctionCallbackInfo<v8::Value>& info)
  47. #define V8JS_THROW(type, message, message_len) v8::ThrowException(v8::Exception::type(V8JS_STRL(message, message_len)))
  48. #define V8JS_GLOBAL v8::Context::GetCurrent()->Global()
  49. /* Helper macros */
  50. #if PHP_V8_API_VERSION < 2005009
  51. # define V8JS_GET_CLASS_NAME(var, obj) \
  52. /* Hack to prevent calling possibly set user-defined __toString() messing class name */ \
  53. v8::Local<v8::Function> constructor = v8::Local<v8::Function>::Cast(obj->Get(V8JS_SYM("constructor"))); \
  54. v8::String::Utf8Value var(constructor->GetName());
  55. #else
  56. # define V8JS_GET_CLASS_NAME(var, obj) \
  57. v8::String::Utf8Value var(obj->GetConstructorName());
  58. #endif
  59. #if ZEND_MODULE_API_NO >= 20100409
  60. # define ZEND_HASH_KEY_DC , const zend_literal *key
  61. # define ZEND_HASH_KEY_CC , key
  62. #else
  63. # define ZEND_HASH_KEY_DC
  64. # define ZEND_HASH_KEY_CC
  65. #endif
  66. /* Global flags */
  67. #define V8JS_GLOBAL_SET_FLAGS(flags) V8JS_GLOBAL->SetHiddenValue(V8JS_SYM("__php_flags__"), V8JS_INT(flags))
  68. #define V8JS_GLOBAL_GET_FLAGS() V8JS_GLOBAL->GetHiddenValue(V8JS_SYM("__php_flags__"))->IntegerValue();
  69. /* Options */
  70. #define V8JS_FLAG_NONE (1<<0)
  71. #define V8JS_FLAG_FORCE_ARRAY (1<<1)
  72. /* Extracts a C string from a V8 Utf8Value. */
  73. static const char * ToCString(const v8::String::Utf8Value &value) /* {{{ */
  74. {
  75. return *value ? *value : "<string conversion failed>";
  76. }
  77. /* }}} */
  78. /* Extern Class entries */
  79. extern zend_class_entry *php_ce_v8_object;
  80. extern zend_class_entry *php_ce_v8_function;
  81. /* Create PHP V8 object */
  82. void php_v8js_create_v8(zval *, v8::Handle<v8::Value>, int, v8::Isolate * TSRMLS_DC);
  83. /* Fetch V8 object properties */
  84. int php_v8js_v8_get_properties_hash(v8::Handle<v8::Value>, HashTable *, int, v8::Isolate * TSRMLS_DC);
  85. /* Convert zval into V8 value */
  86. v8::Handle<v8::Value> zval_to_v8js(zval *, v8::Isolate * TSRMLS_DC);
  87. /* Convert V8 value into zval */
  88. int v8js_to_zval(v8::Handle<v8::Value>, zval *, int, v8::Isolate * TSRMLS_DC);
  89. /* Register accessors into passed object */
  90. void php_v8js_register_accessors(v8::Local<v8::ObjectTemplate>, zval *, v8::Isolate * TSRMLS_DC);
  91. /* {{{ Context container */
  92. struct php_v8js_ctx {
  93. zend_object std;
  94. v8::Persistent<v8::String> object_name;
  95. v8::Persistent<v8::Context> context;
  96. zend_bool report_uncaught;
  97. zval *pending_exception;
  98. int in_execution;
  99. v8::Isolate *isolate;
  100. bool time_limit_hit;
  101. bool memory_limit_hit;
  102. v8::Persistent<v8::FunctionTemplate> global_template;
  103. zval *module_loader;
  104. std::vector<char *> modules_stack;
  105. std::vector<char *> modules_base;
  106. };
  107. /* }}} */
  108. // Timer context
  109. struct php_v8js_timer_ctx
  110. {
  111. long time_limit;
  112. long memory_limit;
  113. std::chrono::time_point<std::chrono::high_resolution_clock> time_point;
  114. php_v8js_ctx *v8js_ctx;
  115. };
  116. /* Module globals */
  117. ZEND_BEGIN_MODULE_GLOBALS(v8js)
  118. int v8_initialized;
  119. HashTable *extensions;
  120. int disposed_contexts; /* Disposed contexts since last time V8 did GC */
  121. /* Ini globals */
  122. char *v8_flags; /* V8 command line flags */
  123. int max_disposed_contexts; /* Max disposed context allowed before forcing V8 GC */
  124. // Timer thread globals
  125. std::stack<php_v8js_timer_ctx *> timer_stack;
  126. std::thread *timer_thread;
  127. std::mutex timer_mutex;
  128. bool timer_stop;
  129. std::map<char *, v8::Handle<v8::Object> > modules_loaded;
  130. ZEND_END_MODULE_GLOBALS(v8js)
  131. extern zend_v8js_globals v8js_globals;
  132. #ifdef ZTS
  133. # define V8JSG(v) TSRMG(v8js_globals_id, zend_v8js_globals *, v)
  134. #else
  135. # define V8JSG(v) (v8js_globals.v)
  136. #endif
  137. /* Register builtin methods into passed object */
  138. void php_v8js_register_methods(v8::Handle<v8::ObjectTemplate>, php_v8js_ctx *c);
  139. #endif /* PHP_V8JS_MACROS_H */
  140. /*
  141. * Local variables:
  142. * tab-width: 4
  143. * c-basic-offset: 4
  144. * End:
  145. * vim600: noet sw=4 ts=4 fdm=marker
  146. * vim<600: noet sw=4 ts=4
  147. */