v8js_v8.h 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  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. #ifndef V8JS_V8_H
  14. #define V8JS_V8_H
  15. #include <functional>
  16. /* Helper macros */
  17. #define V8JS_SYM(v) v8::String::NewFromUtf8(isolate, v, v8::String::kInternalizedString, sizeof(v) - 1)
  18. #define V8JS_SYML(v, l) v8::String::NewFromUtf8(isolate, v, v8::String::kInternalizedString, l)
  19. #define V8JS_ZSYM(v) v8::String::NewFromUtf8(isolate, ZSTR_VAL(v), v8::String::kInternalizedString, ZSTR_LEN(v))
  20. #define V8JS_STR(v) v8::String::NewFromUtf8(isolate, v)
  21. #define V8JS_STRL(v, l) v8::String::NewFromUtf8(isolate, v, v8::String::kNormalString, l)
  22. #define V8JS_ZSTR(v) v8::String::NewFromUtf8(isolate, ZSTR_VAL(v), v8::String::kNormalString, ZSTR_LEN(v))
  23. #define V8JS_INT(v) v8::Integer::New(isolate, v)
  24. #define V8JS_UINT(v) v8::Integer::NewFromUnsigned(isolate, v)
  25. #define V8JS_FLOAT(v) v8::Number::New(isolate, v)
  26. #define V8JS_BOOL(v) ((v)?v8::True(isolate):v8::False(isolate))
  27. #define V8JS_TRUE() v8::True(isolate)
  28. #define V8JS_FALSE() v8::False(isolate)
  29. #define V8JS_DATE(v) v8::Date::New(isolate, v)
  30. #define V8JS_NULL v8::Null(isolate)
  31. #define V8JS_UNDEFINED v8::Undefined(isolate)
  32. #define V8JS_MN(name) v8js_method_##name
  33. #define V8JS_METHOD(name) void V8JS_MN(name)(const v8::FunctionCallbackInfo<v8::Value>& info)
  34. #define V8JS_THROW(isolate, type, message, message_len) (isolate)->ThrowException(v8::Exception::type(V8JS_STRL(message, message_len)))
  35. #define V8JS_GLOBAL(isolate) ((isolate)->GetCurrentContext()->Global())
  36. /* On Windows there are max and min macros, which would clobber the
  37. * method names of std::numeric_limits< > otherwise. */
  38. #undef max
  39. #undef min
  40. /* Extracts a C string from a V8 Utf8Value. */
  41. static inline const char * ToCString(const v8::String::Utf8Value &value) /* {{{ */
  42. {
  43. return *value ? *value : "<string conversion failed>";
  44. }
  45. /* }}} */
  46. void v8js_v8_init();
  47. void v8js_v8_call(v8js_ctx *c, zval **return_value,
  48. long flags, long time_limit, size_t memory_limit,
  49. std::function< v8::Local<v8::Value>(v8::Isolate *) >& v8_call);
  50. void v8js_terminate_execution(v8::Isolate *isolate);
  51. /* Fetch V8 object properties */
  52. int v8js_get_properties_hash(v8::Local<v8::Value> jsValue, HashTable *retval, int flags, v8::Isolate *isolate);
  53. #define V8JS_CTX_PROLOGUE_EX(ctx, ret) \
  54. if (!V8JSG(v8_initialized)) { \
  55. zend_error(E_ERROR, "V8 not initialized"); \
  56. return ret; \
  57. } \
  58. \
  59. v8::Isolate *isolate = (ctx)->isolate; \
  60. v8::Locker locker(isolate); \
  61. v8::Isolate::Scope isolate_scope(isolate); \
  62. v8::HandleScope handle_scope(isolate); \
  63. v8::Local<v8::Context> v8_context = v8::Local<v8::Context>::New(isolate, (ctx)->context); \
  64. v8::Context::Scope context_scope(v8_context);
  65. #define V8JS_CTX_PROLOGUE(ctx) \
  66. V8JS_CTX_PROLOGUE_EX(ctx,)
  67. #define V8JS_BEGIN_CTX(ctx, object) \
  68. v8js_ctx *(ctx); \
  69. (ctx) = Z_V8JS_CTX_OBJ_P(object); \
  70. V8JS_CTX_PROLOGUE(ctx);
  71. #endif /* V8JS_V8_H */
  72. /*
  73. * Local variables:
  74. * tab-width: 4
  75. * c-basic-offset: 4
  76. * indent-tabs-mode: t
  77. * End:
  78. * vim600: noet sw=4 ts=4 fdm=marker
  79. * vim<600: noet sw=4 ts=4
  80. */