v8js_v8.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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::NewStringType::kInternalized, sizeof(v) - 1).ToLocalChecked())
  18. #define V8JS_SYML(v, l) (v8::String::NewFromUtf8(isolate, v, v8::NewStringType::kInternalized, l).ToLocalChecked())
  19. #define V8JS_ZSYM(v) (v8::String::NewFromUtf8(isolate, ZSTR_VAL(v), v8::NewStringType::kInternalized, ZSTR_LEN(v)).ToLocalChecked())
  20. #define V8JS_STR(v) (v8::String::NewFromUtf8(isolate, v, v8::NewStringType::kNormal).ToLocalChecked())
  21. #define V8JS_STRL(v, l) (v8::String::NewFromUtf8(isolate, v, v8::NewStringType::kNormal, l).ToLocalChecked())
  22. #define V8JS_ZSTR(v) (v8::String::NewFromUtf8(isolate, ZSTR_VAL(v), v8::NewStringType::kNormal, ZSTR_LEN(v)).ToLocalChecked())
  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_NULL v8::Null(isolate)
  30. #define V8JS_UNDEFINED v8::Undefined(isolate)
  31. #define V8JS_MN(name) v8js_method_##name
  32. #define V8JS_METHOD(name) void V8JS_MN(name)(const v8::FunctionCallbackInfo<v8::Value>& info)
  33. #define V8JS_THROW(isolate, type, message, message_len) (isolate)->ThrowException(v8::Exception::type(V8JS_STRL(message, message_len)))
  34. #define V8JS_GLOBAL(isolate) ((isolate)->GetCurrentContext()->Global())
  35. /* On Windows there are max and min macros, which would clobber the
  36. * method names of std::numeric_limits< > otherwise. */
  37. #undef max
  38. #undef min
  39. /* Extracts a C string from a V8 Utf8Value. */
  40. static inline const char * ToCString(const v8::String::Utf8Value &value) /* {{{ */
  41. {
  42. return *value ? *value : "<string conversion failed>";
  43. }
  44. /* }}} */
  45. void v8js_v8_init();
  46. void v8js_v8_call(v8js_ctx *c, zval **return_value,
  47. long flags, long time_limit, size_t memory_limit,
  48. std::function< v8::MaybeLocal<v8::Value>(v8::Isolate *) >& v8_call);
  49. void v8js_terminate_execution(v8::Isolate *isolate);
  50. /* Fetch V8 object properties */
  51. int v8js_get_properties_hash(v8::Local<v8::Value> jsValue, HashTable *retval, int flags, v8::Isolate *isolate);
  52. #define V8JS_CTX_PROLOGUE_EX(ctx, ret) \
  53. if (!V8JSG(v8_initialized)) { \
  54. zend_error(E_ERROR, "V8 not initialized"); \
  55. return ret; \
  56. } \
  57. \
  58. v8::Isolate *isolate = (ctx)->isolate; \
  59. v8::Locker locker(isolate); \
  60. v8::Isolate::Scope isolate_scope(isolate); \
  61. v8::HandleScope handle_scope(isolate); \
  62. v8::Local<v8::Context> v8_context = v8::Local<v8::Context>::New(isolate, (ctx)->context); \
  63. v8::Context::Scope context_scope(v8_context);
  64. #define V8JS_CTX_PROLOGUE(ctx) \
  65. V8JS_CTX_PROLOGUE_EX(ctx,)
  66. #define V8JS_BEGIN_CTX(ctx, object) \
  67. v8js_ctx *(ctx); \
  68. (ctx) = Z_V8JS_CTX_OBJ_P(object); \
  69. V8JS_CTX_PROLOGUE(ctx);
  70. #define V8JS_BEGIN_CTX_OBJ(ctx, object) \
  71. v8js_ctx *(ctx); \
  72. (ctx) = Z_V8JS_CTX_OBJ(object); \
  73. V8JS_CTX_PROLOGUE(ctx);
  74. // In PHP 8.1, mismatched tentative return types emit a deprecation notice.
  75. // https://wiki.php.net/rfc/internal_method_return_types
  76. //
  77. // When compiling for earlier php versions, the return type is dropped.
  78. #if PHP_VERSION_ID < 80100
  79. #define ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_INFO_EX(name, return_reference, required_num_args, type, allow_null) \
  80. ZEND_BEGIN_ARG_INFO_EX(name, return_reference, required_num_args, allow_null)
  81. #endif
  82. #ifndef IS_VOID
  83. #define IS_VOID 99
  84. #endif
  85. #ifndef IS_MIXED
  86. #define IS_MIXED 99
  87. #endif
  88. #ifndef _IS_BOOL
  89. #define _IS_BOOL 99
  90. #endif
  91. #ifndef IS_LONG
  92. #define IS_LONG 99
  93. #endif
  94. #endif /* V8JS_V8_H */
  95. /*
  96. * Local variables:
  97. * tab-width: 4
  98. * c-basic-offset: 4
  99. * indent-tabs-mode: t
  100. * End:
  101. * vim600: noet sw=4 ts=4 fdm=marker
  102. * vim<600: noet sw=4 ts=4
  103. */