PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/hphp/runtime/ext/reflection/ext_reflection.h

https://gitlab.com/0072016/0072016-PHP.LLC
C Header | 282 lines | 205 code | 46 blank | 31 comment | 13 complexity | d80d657055506533d95c1e3b06165c5f MD5 | raw file
  1. /*
  2. +----------------------------------------------------------------------+
  3. | HipHop for PHP |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 2010-2015 Facebook, Inc. (http://www.facebook.com) |
  6. | Copyright (c) 1997-2010 The PHP Group |
  7. +----------------------------------------------------------------------+
  8. | This source file is subject to version 3.01 of the PHP license, |
  9. | that is bundled with this package in the file LICENSE, and is |
  10. | available through the world-wide-web at the following url: |
  11. | http://www.php.net/license/3_01.txt |
  12. | If you did not receive a copy of the PHP license and are unable to |
  13. | obtain it through the world-wide-web, please send a note to |
  14. | license@php.net so we can mail you a copy immediately. |
  15. +----------------------------------------------------------------------+
  16. */
  17. #ifndef incl_HPHP_EXT_REFLECTION_H_
  18. #define incl_HPHP_EXT_REFLECTION_H_
  19. #include "hphp/runtime/ext/extension.h"
  20. #include "hphp/runtime/vm/native-data.h"
  21. namespace HPHP {
  22. ///////////////////////////////////////////////////////////////////////////////
  23. Array HHVM_FUNCTION(hphp_get_extension_info, const String& name);
  24. Variant HHVM_FUNCTION(hphp_invoke, const String& name, const Variant& params);
  25. Variant HHVM_FUNCTION(hphp_invoke_method, const Variant& obj, const String& cls,
  26. const String& name, const Variant& params);
  27. Object HHVM_FUNCTION(hphp_create_object, const String& name, const Variant& params);
  28. Object HHVM_FUNCTION(hphp_create_object_without_constructor,
  29. const String& name);
  30. Variant HHVM_FUNCTION(hphp_get_property, const Object& obj, const String& cls,
  31. const String& prop);
  32. void HHVM_FUNCTION(hphp_set_property, const Object& obj, const String& cls,
  33. const String& prop, const Variant& value);
  34. Variant HHVM_FUNCTION(hphp_get_static_property, const String& cls,
  35. const String& prop, bool force);
  36. void HHVM_FUNCTION(hphp_set_static_property, const String& cls,
  37. const String& prop, const Variant& value,
  38. bool force);
  39. class Reflection {
  40. public:
  41. static HPHP::Class* s_ReflectionExceptionClass;
  42. static Object AllocReflectionExceptionObject(const Variant& message);
  43. };
  44. /* A ReflectionFuncHandle is a NativeData object wrapping a Func*
  45. * for the purposes of ReflectionFunction and ReflectionMethod. */
  46. extern const StaticString s_ReflectionFuncHandle;
  47. class ReflectionFuncHandle {
  48. public:
  49. ReflectionFuncHandle(): m_func(nullptr) {}
  50. explicit ReflectionFuncHandle(const Func* func): m_func(func) {};
  51. ReflectionFuncHandle(const ReflectionFuncHandle&) = delete;
  52. ReflectionFuncHandle& operator=(const ReflectionFuncHandle& other) {
  53. m_func = other.m_func;
  54. return *this;
  55. }
  56. ~ReflectionFuncHandle() {}
  57. static ReflectionFuncHandle* Get(ObjectData* obj) {
  58. return Native::data<ReflectionFuncHandle>(obj);
  59. }
  60. static const Func* GetFuncFor(ObjectData* obj) {
  61. return Native::data<ReflectionFuncHandle>(obj)->getFunc();
  62. }
  63. const Func* getFunc() { return m_func; }
  64. void setFunc(const Func* func) {
  65. assert(func != nullptr);
  66. assert(m_func == nullptr);
  67. m_func = func;
  68. }
  69. private:
  70. template <typename F> friend void scan(const ReflectionFuncHandle&, F&);
  71. const Func* m_func{nullptr};
  72. };
  73. /* A ReflectionClassHandle is a NativeData object wrapping a Class* for the
  74. * purposes of ReflectionClass. */
  75. extern const StaticString s_ReflectionClassHandle;
  76. class ReflectionClassHandle {
  77. public:
  78. ReflectionClassHandle(): m_cls(nullptr) {}
  79. explicit ReflectionClassHandle(const Class* cls): m_cls(cls) {};
  80. ReflectionClassHandle(const ReflectionClassHandle&) = delete;
  81. ReflectionClassHandle& operator=(const ReflectionClassHandle& that_) {
  82. m_cls = that_.m_cls;
  83. return *this;
  84. }
  85. ~ReflectionClassHandle() {}
  86. String init(const String& name) {
  87. auto const cls = Unit::loadClass(name.get());
  88. if (!cls) return empty_string();
  89. setClass(cls);
  90. return cls->nameStr();
  91. }
  92. static ReflectionClassHandle* Get(ObjectData* obj) {
  93. return Native::data<ReflectionClassHandle>(obj);
  94. }
  95. static const Class* GetClassFor(ObjectData* obj) {
  96. return Native::data<ReflectionClassHandle>(obj)->getClass();
  97. }
  98. const Class* getClass() const { return m_cls; }
  99. void setClass(const Class* cls) {
  100. assert(cls != nullptr);
  101. assert(m_cls == nullptr);
  102. m_cls = cls;
  103. }
  104. Variant sleep() const {
  105. return String(getClass()->nameStr());
  106. }
  107. void wakeup(const Variant& content, ObjectData* obj);
  108. private:
  109. template <typename F> friend void scan(const ReflectionClassHandle&, F&);
  110. const Class* m_cls{nullptr};
  111. };
  112. /* A ReflectionConstHandle is a NativeData object wrapping a Const*
  113. * for the purposes of ReflectionTypeConstant. */
  114. extern const StaticString s_ReflectionConstHandle;
  115. class ReflectionConstHandle {
  116. public:
  117. ReflectionConstHandle(): m_const(nullptr) {}
  118. explicit ReflectionConstHandle(const Class::Const* cst): m_const(cst) {};
  119. ReflectionConstHandle(const ReflectionConstHandle&) = delete;
  120. ReflectionConstHandle& operator=(const ReflectionConstHandle& other) {
  121. m_const = other.m_const;
  122. return *this;
  123. }
  124. ~ReflectionConstHandle() {}
  125. static ReflectionConstHandle* Get(ObjectData* obj) {
  126. return Native::data<ReflectionConstHandle>(obj);
  127. }
  128. static const Class::Const* GetConstFor(ObjectData* obj) {
  129. return Native::data<ReflectionConstHandle>(obj)->getConst();
  130. }
  131. const Class::Const* getConst() { return m_const; }
  132. void setConst(const Class::Const* cst) {
  133. assert(cst != nullptr);
  134. assert(m_const == nullptr);
  135. m_const = cst;
  136. }
  137. private:
  138. template <typename F> friend void scan(const ReflectionConstHandle&, F&);
  139. const Class::Const* m_const{nullptr};
  140. };
  141. /* A ReflectionPropHandle is a NativeData object wrapping a Prop*
  142. * for the purposes of ReflectionProperty. */
  143. extern const StaticString s_ReflectionPropHandle;
  144. class ReflectionPropHandle {
  145. public:
  146. ReflectionPropHandle(): m_prop(nullptr) {}
  147. explicit ReflectionPropHandle(const Class::Prop* prop): m_prop(prop) {};
  148. ReflectionPropHandle(const ReflectionPropHandle& other) {
  149. m_prop = other.m_prop;
  150. }
  151. ReflectionPropHandle& operator=(const ReflectionPropHandle& other) {
  152. m_prop = other.m_prop;
  153. return *this;
  154. }
  155. ~ReflectionPropHandle() {}
  156. static ReflectionPropHandle* Get(ObjectData* obj) {
  157. return Native::data<ReflectionPropHandle>(obj);
  158. }
  159. static const Class::Prop* GetPropFor(ObjectData* obj) {
  160. return Native::data<ReflectionPropHandle>(obj)->getProp();
  161. }
  162. const Class::Prop* getProp() { return m_prop; }
  163. void setProp(const Class::Prop* prop) {
  164. assert(prop != nullptr);
  165. assert(m_prop == nullptr);
  166. m_prop = prop;
  167. }
  168. private:
  169. template <typename F> friend void scan(const ReflectionPropHandle&, F&);
  170. const Class::Prop* m_prop{nullptr};
  171. };
  172. /* A ReflectionSPropHandle is a NativeData object wrapping a SProp*
  173. * for the purposes of static ReflectionProperty. */
  174. extern const StaticString s_ReflectionSPropHandle;
  175. class ReflectionSPropHandle {
  176. public:
  177. ReflectionSPropHandle(): m_sprop(nullptr) {}
  178. explicit ReflectionSPropHandle(const Class::SProp* sprop): m_sprop(sprop) {};
  179. ReflectionSPropHandle(const ReflectionSPropHandle& other) {
  180. m_sprop = other.m_sprop;
  181. }
  182. ReflectionSPropHandle& operator=(const ReflectionSPropHandle& other) {
  183. m_sprop = other.m_sprop;
  184. return *this;
  185. }
  186. ~ReflectionSPropHandle() {}
  187. static ReflectionSPropHandle* Get(ObjectData* obj) {
  188. return Native::data<ReflectionSPropHandle>(obj);
  189. }
  190. static const Class::SProp* GetSPropFor(ObjectData* obj) {
  191. return Native::data<ReflectionSPropHandle>(obj)->getSProp();
  192. }
  193. const Class::SProp* getSProp() { return m_sprop; }
  194. void setSProp(const Class::SProp* sprop) {
  195. assert(sprop != nullptr);
  196. assert(m_sprop == nullptr);
  197. m_sprop = sprop;
  198. }
  199. private:
  200. template <typename F> friend void scan(const ReflectionSPropHandle&, F&);
  201. const Class::SProp* m_sprop{nullptr};
  202. };
  203. /* A ReflectionTypeAliasHandle is a NativeData object wrapping a TypeAliasReq*
  204. * for the purposes of static ReflectionTypeAlias. */
  205. struct ReflectionTypeAliasHandle {
  206. ReflectionTypeAliasHandle(): m_req(nullptr) {}
  207. explicit ReflectionTypeAliasHandle(const TypeAliasReq* req): m_req(req) {};
  208. static ReflectionTypeAliasHandle* Get(ObjectData* obj) {
  209. return Native::data<ReflectionTypeAliasHandle>(obj);
  210. }
  211. static const TypeAliasReq* GetTypeAliasReqFor(ObjectData* obj) {
  212. return Native::data<ReflectionTypeAliasHandle>(obj)->getTypeAliasReq();
  213. }
  214. const TypeAliasReq* getTypeAliasReq() const { return m_req; }
  215. void setTypeAliasReq(const TypeAliasReq* req) {
  216. assert(req != nullptr);
  217. assert(m_req == nullptr);
  218. m_req = req;
  219. }
  220. private:
  221. template <typename F> friend void scan(const ReflectionTypeAliasHandle&, F&);
  222. const TypeAliasReq* m_req;
  223. };
  224. namespace DebuggerReflection {
  225. Array get_function_info(const String& name);
  226. Array get_class_info(const String& name);
  227. }
  228. // These helpers are shared by an FB-specific extension.
  229. Class* get_cls(const Variant& class_or_object);
  230. const Func* get_method_func(const Class* cls, const String& meth_name);
  231. Variant default_arg_from_php_code(const Func::ParamInfo& fpi, const Func* func);
  232. bool resolveDefaultParameterConstant(const char *value, int64_t valueLen,
  233. Variant &cns);
  234. ///////////////////////////////////////////////////////////////////////////////
  235. }
  236. #endif // incl_HPHP_EXT_REFLECTION_H_