php_v8js_macros.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. #if __GNUC__ == 4 && __GNUC_MINOR__ == 4
  17. #define _GLIBCXX_USE_NANOSLEEP 1
  18. #endif
  19. extern "C" {
  20. #include "php.h"
  21. #include "php_v8js.h"
  22. }
  23. #ifdef _WIN32
  24. /* On Windows a symbol COMPILER is defined. However v8.h has an enum with that
  25. * name which hence would be broken unless undefined here. */
  26. #undef COMPILER
  27. #endif
  28. #include <v8.h>
  29. #include <chrono>
  30. #include <stack>
  31. #include <thread>
  32. #include <map>
  33. #include <list>
  34. #include <vector>
  35. #include <mutex>
  36. /* V8Js Version */
  37. #define V8JS_VERSION "0.1.5"
  38. /* Helper macros */
  39. #define V8JS_SYM(v) v8::String::NewFromUtf8(isolate, v, v8::String::kInternalizedString, sizeof(v) - 1)
  40. #define V8JS_SYML(v, l) v8::String::NewFromUtf8(isolate, v, v8::String::kInternalizedString, l)
  41. #define V8JS_STR(v) v8::String::NewFromUtf8(isolate, v)
  42. #define V8JS_STRL(v, l) v8::String::NewFromUtf8(isolate, v, v8::String::kNormalString, l)
  43. #define V8JS_INT(v) v8::Integer::New(isolate, v)
  44. #define V8JS_UINT(v) v8::Integer::NewFromUnsigned(isolate, v)
  45. #define V8JS_FLOAT(v) v8::Number::New(isolate, v)
  46. #define V8JS_BOOL(v) ((v)?v8::True(isolate):v8::False(isolate))
  47. #define V8JS_DATE(v) v8::Date::New(isolate, v)
  48. #define V8JS_NULL v8::Null(isolate)
  49. #define V8JS_UNDEFINED v8::Undefined(isolate)
  50. #define V8JS_MN(name) v8js_method_##name
  51. #define V8JS_METHOD(name) void V8JS_MN(name)(const v8::FunctionCallbackInfo<v8::Value>& info)
  52. #define V8JS_THROW(isolate, type, message, message_len) (isolate)->ThrowException(v8::Exception::type(V8JS_STRL(message, message_len)))
  53. #define V8JS_GLOBAL(isolate) ((isolate)->GetCurrentContext()->Global())
  54. /* Abbreviate long type names */
  55. typedef v8::Persistent<v8::FunctionTemplate, v8::CopyablePersistentTraits<v8::FunctionTemplate> > v8js_tmpl_t;
  56. typedef v8::Persistent<v8::Object, v8::CopyablePersistentTraits<v8::Object> > v8js_persistent_obj_t;
  57. /* Hidden field name used to link JS wrappers with underlying PHP object */
  58. #define PHPJS_OBJECT_KEY "phpjs::object"
  59. /* Helper macros */
  60. #define V8JS_GET_CLASS_NAME(var, obj) \
  61. v8::String::Utf8Value var(obj->GetConstructorName());
  62. #if ZEND_MODULE_API_NO >= 20100409
  63. # define ZEND_HASH_KEY_DC , const zend_literal *key
  64. # define ZEND_HASH_KEY_CC , key
  65. # define ZEND_HASH_KEY_NULL , NULL
  66. #else
  67. # define ZEND_HASH_KEY_DC
  68. # define ZEND_HASH_KEY_CC
  69. # define ZEND_HASH_KEY_NULL
  70. #endif
  71. /* method signatures of zend_update_property and zend_read_property were
  72. * declared as 'char *' instead of 'const char *' before PHP 5.4 */
  73. #if ZEND_MODULE_API_NO >= 20100525
  74. # define V8JS_CONST
  75. #else
  76. # define V8JS_CONST (char *)
  77. #endif
  78. /* Global flags */
  79. #define V8JS_GLOBAL_SET_FLAGS(isolate,flags) V8JS_GLOBAL(isolate)->SetHiddenValue(V8JS_SYM("__php_flags__"), V8JS_INT(flags))
  80. #define V8JS_GLOBAL_GET_FLAGS(isolate) V8JS_GLOBAL(isolate)->GetHiddenValue(V8JS_SYM("__php_flags__"))->IntegerValue();
  81. /* Options */
  82. #define V8JS_FLAG_NONE (1<<0)
  83. #define V8JS_FLAG_FORCE_ARRAY (1<<1)
  84. #define V8JS_DEBUG_AUTO_BREAK_NEVER 0
  85. #define V8JS_DEBUG_AUTO_BREAK_ONCE 1
  86. #define V8JS_DEBUG_AUTO_BREAK_ALWAYS 2
  87. /* Extracts a C string from a V8 Utf8Value. */
  88. static inline const char * ToCString(const v8::String::Utf8Value &value) /* {{{ */
  89. {
  90. return *value ? *value : "<string conversion failed>";
  91. }
  92. /* }}} */
  93. /* Extern Class entries */
  94. extern zend_class_entry *php_ce_v8_object;
  95. extern zend_class_entry *php_ce_v8_function;
  96. /* Create PHP V8 object */
  97. void php_v8js_create_v8(zval *, v8::Handle<v8::Value>, int, v8::Isolate * TSRMLS_DC);
  98. /* Fetch V8 object properties */
  99. int php_v8js_v8_get_properties_hash(v8::Handle<v8::Value>, HashTable *, int, v8::Isolate * TSRMLS_DC);
  100. /* Convert zval into V8 value */
  101. v8::Handle<v8::Value> zval_to_v8js(zval *, v8::Isolate * TSRMLS_DC);
  102. /* Convert V8 value into zval */
  103. int v8js_to_zval(v8::Handle<v8::Value>, zval *, int, v8::Isolate * TSRMLS_DC);
  104. struct php_v8js_accessor_ctx
  105. {
  106. char *variable_name_string;
  107. uint variable_name_string_len;
  108. v8::Isolate *isolate;
  109. };
  110. void php_v8js_accessor_ctx_dtor(php_v8js_accessor_ctx * TSRMLS_DC);
  111. /* Register accessors into passed object */
  112. void php_v8js_register_accessors(std::vector<php_v8js_accessor_ctx*> *accessor_list, v8::Local<v8::FunctionTemplate>, zval *, v8::Isolate * TSRMLS_DC);
  113. struct php_v8js_object;
  114. /* {{{ Context container */
  115. struct php_v8js_ctx {
  116. zend_object std;
  117. v8::Persistent<v8::String> object_name;
  118. v8::Persistent<v8::Context> context;
  119. zend_bool report_uncaught;
  120. zval *pending_exception;
  121. int in_execution;
  122. v8::Isolate *isolate;
  123. bool time_limit_hit;
  124. bool memory_limit_hit;
  125. v8::Persistent<v8::FunctionTemplate> global_template;
  126. zval *module_loader;
  127. std::vector<char *> modules_stack;
  128. std::vector<char *> modules_base;
  129. std::map<char *, v8js_persistent_obj_t> modules_loaded;
  130. std::map<const char *,v8js_tmpl_t> template_cache;
  131. std::map<zval *, v8js_persistent_obj_t> weak_objects;
  132. std::map<v8js_tmpl_t *, v8js_persistent_obj_t> weak_closures;
  133. std::list<php_v8js_object *> php_v8js_objects;
  134. std::vector<php_v8js_accessor_ctx *> accessor_list;
  135. char *tz;
  136. #ifdef ZTS
  137. void ***zts_ctx;
  138. #endif
  139. };
  140. /* }}} */
  141. #ifdef ZTS
  142. # define V8JS_TSRMLS_FETCH() TSRMLS_FETCH_FROM_CTX(((php_v8js_ctx *) isolate->GetData(0))->zts_ctx);
  143. #else
  144. # define V8JS_TSRMLS_FETCH()
  145. #endif
  146. // Timer context
  147. struct php_v8js_timer_ctx
  148. {
  149. long time_limit;
  150. long memory_limit;
  151. std::chrono::time_point<std::chrono::high_resolution_clock> time_point;
  152. php_v8js_ctx *v8js_ctx;
  153. bool killed;
  154. };
  155. /* {{{ Object container */
  156. struct php_v8js_object {
  157. zend_object std;
  158. v8::Persistent<v8::Value> v8obj;
  159. int flags;
  160. struct php_v8js_ctx *ctx;
  161. HashTable *properties;
  162. };
  163. /* }}} */
  164. /* Resource declaration */
  165. /* Module globals */
  166. ZEND_BEGIN_MODULE_GLOBALS(v8js)
  167. int v8_initialized;
  168. HashTable *extensions;
  169. /* Ini globals */
  170. char *v8_flags; /* V8 command line flags */
  171. bool use_date; /* Generate JS Date objects instead of PHP DateTime */
  172. // Timer thread globals
  173. std::stack<php_v8js_timer_ctx *> timer_stack;
  174. std::thread *timer_thread;
  175. std::mutex timer_mutex;
  176. bool timer_stop;
  177. // fatal error unwinding
  178. bool fatal_error_abort;
  179. int error_num;
  180. char *error_message;
  181. jmp_buf *unwind_env;
  182. void (*old_error_handler)(int, const char *, const uint, const char*, va_list);
  183. ZEND_END_MODULE_GLOBALS(v8js)
  184. extern zend_v8js_globals v8js_globals;
  185. ZEND_EXTERN_MODULE_GLOBALS(v8js)
  186. #ifdef ZTS
  187. # define V8JSG(v) TSRMG(v8js_globals_id, zend_v8js_globals *, v)
  188. #else
  189. # define V8JSG(v) (v8js_globals.v)
  190. #endif
  191. /* Register builtin methods into passed object */
  192. void php_v8js_register_methods(v8::Handle<v8::ObjectTemplate>, php_v8js_ctx *c);
  193. typedef struct _php_v8js_script {
  194. char *name;
  195. v8::Isolate *isolate;
  196. v8::Persistent<v8::Script, v8::CopyablePersistentTraits<v8::Script>> *script;
  197. } php_v8js_script;
  198. static void php_v8js_script_free(php_v8js_script *res, bool dispose_persistent);
  199. #endif /* PHP_V8JS_MACROS_H */
  200. /*
  201. * Local variables:
  202. * tab-width: 4
  203. * c-basic-offset: 4
  204. * End:
  205. * vim600: noet sw=4 ts=4 fdm=marker
  206. * vim<600: noet sw=4 ts=4
  207. */