PageRenderTime 60ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://github.com/facebook/hiphop-php
C Header | 279 lines | 203 code | 45 blank | 31 comment | 17 complexity | 4365662a75bbaec60fd7b2438ffe1cfd MD5 | raw file
Possible License(s): LGPL-2.1, BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, MIT, LGPL-2.0, Apache-2.0
  1. /*
  2. +----------------------------------------------------------------------+
  3. | HipHop for PHP |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 2010-present 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. struct Reflection {
  40. static Class* s_ReflectionExceptionClass;
  41. static Class* s_ReflectionExtensionClass;
  42. [[noreturn]]
  43. static void ThrowReflectionExceptionObject(const Variant& message);
  44. };
  45. struct ReflectionFileHandle {
  46. ReflectionFileHandle(): m_unit(nullptr) {}
  47. explicit ReflectionFileHandle(const Unit* unit): m_unit(unit) {};
  48. ReflectionFileHandle(const ReflectionFileHandle&) = delete;
  49. ReflectionFileHandle& operator=(const ReflectionFileHandle& other) {
  50. m_unit = other.m_unit;
  51. return *this;
  52. }
  53. static ReflectionFileHandle* Get(ObjectData* obj) {
  54. return Native::data<ReflectionFileHandle>(obj);
  55. }
  56. static const Unit* GetUnitFor(ObjectData* obj) {
  57. return Native::data<ReflectionFileHandle>(obj)->getUnit();
  58. }
  59. const Unit* getUnit() { return m_unit; }
  60. void setUnit(const Unit* unit) {
  61. assertx(unit != nullptr);
  62. assertx(m_unit == nullptr);
  63. m_unit = unit;
  64. }
  65. private:
  66. LowPtr<const Unit> m_unit{nullptr};
  67. };
  68. /* A ReflectionFuncHandle is a NativeData object wrapping a Func*
  69. * for the purposes of ReflectionFunction and ReflectionMethod. */
  70. struct ReflectionFuncHandle {
  71. ReflectionFuncHandle(): m_func(nullptr) {}
  72. explicit ReflectionFuncHandle(const Func* func): m_func(func) {};
  73. ReflectionFuncHandle(const ReflectionFuncHandle&) = delete;
  74. ReflectionFuncHandle& operator=(const ReflectionFuncHandle& other) {
  75. m_func = other.m_func;
  76. return *this;
  77. }
  78. static ReflectionFuncHandle* Get(ObjectData* obj) {
  79. return Native::data<ReflectionFuncHandle>(obj);
  80. }
  81. static const Func* GetFuncFor(ObjectData* obj) {
  82. return Native::data<ReflectionFuncHandle>(obj)->getFunc();
  83. }
  84. const Func* getFunc() { return m_func; }
  85. void setFunc(const Func* func) {
  86. assertx(func != nullptr);
  87. assertx(m_func == nullptr);
  88. m_func = func;
  89. }
  90. private:
  91. LowPtr<const Func> m_func{nullptr};
  92. };
  93. /* A ReflectionClassHandle is a NativeData object wrapping a Class* for the
  94. * purposes of ReflectionClass. */
  95. struct ReflectionClassHandle {
  96. ReflectionClassHandle(): m_cls(nullptr) {}
  97. explicit ReflectionClassHandle(const Class* cls): m_cls(cls) {};
  98. ReflectionClassHandle(const ReflectionClassHandle&) = delete;
  99. ReflectionClassHandle& operator=(const ReflectionClassHandle& that_) {
  100. m_cls = that_.m_cls;
  101. return *this;
  102. }
  103. String init(const String& name) {
  104. auto const cls = Unit::loadClass(name.get());
  105. if (!cls) return empty_string();
  106. setClass(cls);
  107. return cls->nameStr();
  108. }
  109. static ReflectionClassHandle* Get(ObjectData* obj) {
  110. return Native::data<ReflectionClassHandle>(obj);
  111. }
  112. static const Class* GetClassFor(ObjectData* obj) {
  113. return Native::data<ReflectionClassHandle>(obj)->getClass();
  114. }
  115. const Class* getClass() const { return m_cls; }
  116. void setClass(const Class* cls) {
  117. assertx(cls != nullptr);
  118. assertx(m_cls == nullptr);
  119. m_cls = cls;
  120. }
  121. Variant sleep() const {
  122. return String(getClass()->nameStr());
  123. }
  124. void wakeup(const Variant& content, ObjectData* obj);
  125. private:
  126. LowPtr<const Class> m_cls{nullptr};
  127. };
  128. /* A ReflectionConstHandle is a NativeData object wrapping a Const*
  129. * for the purposes of ReflectionTypeConstant. */
  130. struct ReflectionConstHandle {
  131. ReflectionConstHandle(): m_const(nullptr), m_cls(nullptr) {}
  132. explicit ReflectionConstHandle(const Class::Const* cns, const Class* cls):
  133. m_const(cns), m_cls(cls) {};
  134. static ReflectionConstHandle* Get(ObjectData* obj) {
  135. return Native::data<ReflectionConstHandle>(obj);
  136. }
  137. static const Class::Const* GetConstFor(ObjectData* obj) {
  138. return Native::data<ReflectionConstHandle>(obj)->getConst();
  139. }
  140. static const Class* GetClassFor(ObjectData* obj) {
  141. return Native::data<ReflectionConstHandle>(obj)->getClass();
  142. }
  143. const Class::Const* getConst() const { return m_const; }
  144. const Class* getClass() const { return m_cls; }
  145. void setConst(const Class::Const* cns) {
  146. assertx(cns != nullptr);
  147. assertx(m_const == nullptr);
  148. m_const = cns;
  149. }
  150. void setClass(const Class* cls) {
  151. assertx(cls != nullptr);
  152. assertx(m_cls == nullptr);
  153. m_cls = cls;
  154. }
  155. private:
  156. const Class::Const* m_const;
  157. LowPtr<const Class> m_cls;
  158. };
  159. /**
  160. * A ReflectionPropHandle is a NativeData object that represents an instance,
  161. * static, or dynamic property for the purposes of ReflectionProperty.
  162. */
  163. struct ReflectionPropHandle {
  164. enum Type : uint8_t {
  165. Invalid = 0,
  166. Instance = 1,
  167. Static = 2,
  168. Dynamic = 3,
  169. };
  170. ReflectionPropHandle(): m_prop(nullptr), m_type(Invalid) {}
  171. Type getType() const { return m_type; }
  172. const Class::Prop* getProp() const {
  173. assertx(m_type == Instance);
  174. return m_prop;
  175. }
  176. const Class::SProp* getSProp() const {
  177. assertx(m_type == Static);
  178. return m_sprop;
  179. }
  180. void setInstanceProp(const Class::Prop* prop) {
  181. assertx(prop != nullptr);
  182. m_type = Instance;
  183. m_prop = prop;
  184. }
  185. void setStaticProp(const Class::SProp* sprop) {
  186. assertx(sprop != nullptr);
  187. m_type = Static;
  188. m_sprop = sprop;
  189. }
  190. void setDynamicProp() {
  191. m_type = Dynamic;
  192. }
  193. private:
  194. union {
  195. const Class::Prop* m_prop;
  196. const Class::SProp* m_sprop;
  197. };
  198. Type m_type;
  199. };
  200. /* A ReflectionTypeAliasHandle is a NativeData object wrapping a TypeAliasReq*
  201. * for the purposes of static ReflectionTypeAlias. */
  202. struct ReflectionTypeAliasHandle {
  203. ReflectionTypeAliasHandle(): m_req(nullptr) {}
  204. explicit ReflectionTypeAliasHandle(const TypeAliasReq* req): m_req(req) {};
  205. static ReflectionTypeAliasHandle* Get(ObjectData* obj) {
  206. return Native::data<ReflectionTypeAliasHandle>(obj);
  207. }
  208. static const TypeAliasReq* GetTypeAliasReqFor(ObjectData* obj) {
  209. return Native::data<ReflectionTypeAliasHandle>(obj)->getTypeAliasReq();
  210. }
  211. const TypeAliasReq* getTypeAliasReq() const { return m_req; }
  212. void setTypeAliasReq(const TypeAliasReq* req) {
  213. assertx(req != nullptr);
  214. assertx(m_req == nullptr);
  215. m_req = req;
  216. }
  217. private:
  218. template <typename F> friend void scan(const ReflectionTypeAliasHandle&, F&);
  219. const TypeAliasReq* m_req;
  220. };
  221. namespace DebuggerReflection {
  222. Array get_function_info(const String& name);
  223. Array get_class_info(const String& name);
  224. }
  225. // These helpers are shared by an FB-specific extension.
  226. Class* get_cls(const Variant& class_or_object);
  227. const Func* get_method_func(const Class* cls, const String& meth_name);
  228. Variant default_arg_from_php_code(const Func::ParamInfo& fpi, const Func* func,
  229. unsigned argIdx);
  230. bool resolveDefaultParameterConstant(const char *value, int64_t valueLen,
  231. Variant &cns);
  232. ///////////////////////////////////////////////////////////////////////////////
  233. }
  234. #endif // incl_HPHP_EXT_REFLECTION_H_