PageRenderTime 59ms CodeModel.GetById 6ms RepoModel.GetById 0ms app.codeStats 0ms

/objects.py

https://bitbucket.org/asuhan/happy/
Python | 482 lines | 351 code | 119 blank | 12 comment | 45 complexity | da34a7f0b4a778bd2a9e389cd28570ee MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. from rpython.rlib.objectmodel import r_dict, compute_hash
  2. from zend import *
  3. import hphp_string
  4. class obj(object):
  5. pass
  6. class zval(obj):
  7. def get_type(self):
  8. return self.type
  9. def copy_value_from(self, zv):
  10. assert isinstance(zv, zval)#, "Invalid zval: %s" % repr(zv)
  11. self.type = zv.type
  12. self.is_null = zv.is_null
  13. masked_type = zv.type & IS_CONSTANT_TYPE_MASK
  14. if masked_type == IS_NULL:
  15. pass
  16. elif masked_type == IS_LONG or masked_type == IS_BOOL:
  17. self.lval = zv.lval
  18. elif masked_type == IS_DOUBLE:
  19. self.dval = zv.dval
  20. elif masked_type == IS_STRING or masked_type == IS_CONSTANT:
  21. self.str = zv.str
  22. elif masked_type == IS_ARRAY or masked_type == IS_CONSTANT_ARRAY:
  23. # TODO: fix for constant array
  24. self.happy_ht = zv.happy_ht
  25. elif masked_type == IS_OBJECT:
  26. self.obj = zv.obj.get_copy()
  27. elif masked_type == IS_RESOURCE:
  28. self.happy_res = zv.happy_res
  29. else:
  30. raise Exception('Not implemented yet: %d' % (masked_type))
  31. def copy_from(self, zv):
  32. assert isinstance(zv, zval)#, "Invalid zval: %s" % repr(zv)
  33. self.refcount__gc = zv.refcount__gc
  34. self.is_ref__gc = zv.is_ref__gc
  35. self.copy_value_from(zv)
  36. # Keep the RPython inference happy since pointers can either
  37. # point to other pointers or a rad.APCFile.CStruct
  38. class ptr(obj):
  39. def __init__(self):
  40. pass
  41. # This is a 'pseudo' pointer, we could get rid of it anyway.
  42. class ulong_ptr:
  43. def __init__(self, longval, null):
  44. assert isinstance(longval, int)
  45. self.longval = longval
  46. self.null = null
  47. def is_null(self):
  48. return self.null
  49. def assign(self, longval):
  50. assert isinstance(longval, int)
  51. if self.null:
  52. raise Exception('null pointer assignment')
  53. self.longval = longval
  54. def deref(self):
  55. if self.null:
  56. raise Exception('null pointer dereference')
  57. return self.longval
  58. def __nonzero__(self):
  59. raise Exception('use is_null() for casting to bool')
  60. def copy_from(self, p):
  61. assert isinstance(ptr, ulong_ptr)
  62. self.longval = p.longval
  63. self.null = p.null
  64. class double_ptr:
  65. def __init__(self, doubleval, null):
  66. assert isinstance(doubleval, float)
  67. self.doubleval = doubleval
  68. self.null = null
  69. def is_null(self):
  70. return self.null
  71. def assign(self, doubleval):
  72. assert isinstance(doubleval, float)
  73. if self.null:
  74. raise Exception('null pointer assignment')
  75. self.doubleval = doubleval
  76. def deref(self):
  77. if self.null:
  78. raise Exception('null pointer dereference')
  79. return self.doubleval
  80. def __nonzero__(self):
  81. raise Exception('use is_null() for casting to bool')
  82. def copy_from(self, p):
  83. assert isinstance(ptr, double_ptr)
  84. self.doubleval = p.doubleval
  85. self.null = p.null
  86. class zend_function_ptr:
  87. def __init__(self, zend_function, null):
  88. #assert isinstance(zend_function, parser_api.ZendOpArray)
  89. self.__zend_function = zend_function
  90. self.__null = null
  91. def is_null(self):
  92. return self.__null
  93. def assign(self, zend_function):
  94. #assert isinstance(zend_function, parser_api.ZendOpArray)
  95. if self.__null:
  96. raise Exception('null pointer assignment')
  97. self.__zend_function = zend_function
  98. def deref(self):
  99. if self.__null:
  100. raise Exception('null pointer dereference')
  101. return self.__zend_function
  102. def __nonzero__(self):
  103. raise Exception('use is_null() for casting to bool')
  104. # FIXME: merge these two
  105. class ZendClassEntryPtr:
  106. def __init__(self, ce):
  107. self.__ce = ce
  108. def assign(self, ce):
  109. self.__ce = ce
  110. def deref(self):
  111. return self.__ce
  112. class class_entry_ptr(ptr):
  113. def __init__(self, ce):
  114. self.__ce = ce
  115. def get_class_entry(self):
  116. return self.__ce
  117. def copy(self):
  118. return class_entry_ptr(self.__ce)
  119. class op_array_ptr(ptr):
  120. def __init__(self, op_array):
  121. self.op_array = op_array
  122. def get_op_array(self):
  123. return self.op_array
  124. def copy(self):
  125. return op_array_ptr(self.op_array)
  126. def arg_info(self):
  127. return self.op_array.arg_info
  128. def fn_type(self):
  129. return self.op_array.type
  130. def fn_flags(self):
  131. return self.op_array.fn_flags
  132. def set_fn_flags(self, flags):
  133. self.op_array.fn_flags = flags
  134. def prototype(self):
  135. return self.op_array.prototype
  136. def set_prototype(self, prototype):
  137. self.op_array.prototype = prototype
  138. def function_name(self):
  139. return self.op_array.function_name
  140. def scope(self):
  141. return self.op_array.scope
  142. def static_variables(self):
  143. return self.op_array.static_variables
  144. def incref(self):
  145. # TODO: check if we need refcounting
  146. pass
  147. class property_info_ptr(ptr):
  148. def __init__(self, prop_info):
  149. self.prop_info = prop_info
  150. def copy(self):
  151. return property_info_ptr(self.prop_info)
  152. class zval_ptr(ptr):
  153. def __init__(self, zval=None):
  154. assert zval is None or isinstance(zval, obj)
  155. self.__zval = zval
  156. def is_null(self):
  157. return self.__zval is None
  158. def assign(self, zval):
  159. """Equivalent to *self = zval; from C"""
  160. assert isinstance(zval, obj)
  161. if self.__zval is None:
  162. raise Exception('null pointer assignment')
  163. self.__zval.copy_from(zval)
  164. def deref(self):
  165. """Equivalent to *self from C"""
  166. if self.__zval is None:
  167. raise Exception('null pointer dereference')
  168. return self.__zval
  169. def __nonzero__(self):
  170. raise Exception('use is_null() for casting to bool')
  171. def __eq__(self, o):
  172. if isinstance(o, zval_ptr):
  173. return self.__zval is o.__zval
  174. return False
  175. def __ne__(self, o):
  176. return not self.__eq__(o)
  177. def __repr__(self):
  178. return "%08x=>%s" % (id(self), (
  179. repr(self.__zval) if self.__zval is not None else "None"))
  180. def copy(self):
  181. return zval_ptr(self.__zval)
  182. def copy_from(self, p):
  183. """Equivalent to self = p; from C"""
  184. assert isinstance(p, zval_ptr)#, "Invalid zval_ptr: %s" % (repr(p))
  185. self.__zval = p.__zval
  186. class zval_ptr_ptr(ptr):
  187. def __init__(self, zp=None):
  188. assert zp is None or isinstance(zp, ptr)
  189. self.__zp = zp
  190. def is_null(self):
  191. return self.__zp is None
  192. def assign(self, zp):
  193. assert isinstance(zp, ptr)
  194. if self.__zp is None:
  195. raise Exception('null pointer assignment')
  196. self.__zp.copy_from(zp)
  197. def deref(self):
  198. if self.__zp is None:
  199. raise Exception('null pointer dereference')
  200. return self.__zp
  201. def deref_copy(self):
  202. if self.__zp is None:
  203. raise Exception('null pointer dereference')
  204. return self.__zp.copy()
  205. def __nonzero__(self):
  206. raise Exception('use is_null() for casting to bool')
  207. def __eq__(self, o):
  208. if isinstance(o, zval_ptr_ptr):
  209. return self.__zp is o.__zp
  210. return False
  211. def __ne__(self, o):
  212. return not self.__eq__(o)
  213. def __repr__(self):
  214. return "%08x=>%s" % (id(self), (
  215. repr(self.__zp) if self.__zp is not None else "None"))
  216. def copy(self):
  217. return zval_ptr_ptr(self.__zp)
  218. def copy_from(self, p):
  219. assert isinstance(p, zval_ptr_ptr)
  220. self.__zp = p.__zp
  221. class zval_ptr_ptr_ptr(ptr):
  222. def __init__(self, zpp=None):
  223. assert zpp is None or isinstance(zpp, zval_ptr_ptr)
  224. self.__zpp = zpp
  225. def is_null(self):
  226. return self.__zpp is None
  227. def assign(self, zpp):
  228. assert isinstance(zpp, zval_ptr_ptr)
  229. if self.__zpp is None:
  230. raise Exception('null pointer assignment')
  231. self.__zpp.copy_from(zpp)
  232. def deref(self):
  233. if self.__zpp is None:
  234. raise Exception('null pointer dereference')
  235. return self.__zpp
  236. def deref_copy(self):
  237. if self.__zpp is None:
  238. raise Exception('null pointer dereference')
  239. return self.__zpp.copy()
  240. def __nonzero__(self):
  241. raise Exception('use is_null() for casting to bool')
  242. def __eq__(self, o):
  243. if isinstance(o, zval_ptr_ptr_ptr):
  244. return self.__zpp is o.__zpp
  245. return False
  246. def __ne__(self, o):
  247. return not self.__eq__(o)
  248. def __repr__(self):
  249. return "%08x=>%s" % (id(self), (
  250. repr(self.__zpp) if self.__zpp is not None else "None"))
  251. def copy_from(self, p):
  252. assert isinstance(p, zval_ptr_ptr_ptr)
  253. self.__zpp = p.__zpp
  254. class zend_object(obj):
  255. def __init__(self):
  256. self.ce = None
  257. self.properties = None
  258. self.guards = None
  259. class zend_object_ptr(ptr):
  260. def __init__(self, obj):
  261. self.__obj = obj
  262. def deref(self):
  263. return self.__obj
  264. def assign(self, obj):
  265. assert isinstance(obj, zend_object)
  266. self.__obj = obj
  267. class zend_guard(obj):
  268. def __init__(self):
  269. self.in_get = False
  270. self.in_set = False
  271. self.in_unset = False
  272. self.in_isset = False
  273. class zend_object_value:
  274. def __init__(self):
  275. self.handle = 0
  276. self.handlers = None
  277. def get_copy(self):
  278. # TODO: check if this is really needed
  279. copy = zend_object_value()
  280. copy.handle = self.handle
  281. copy.handlers = self.handlers
  282. return copy
  283. class MutableString:
  284. def __init__(self, s=None):
  285. if s is None:
  286. self.__sd = hphp_string.null_string
  287. else:
  288. assert isinstance(s, str)
  289. self.__sd = hphp_string.new_string_data(s)
  290. def length(self):
  291. return hphp_string.string_data_get_length(self.__sd)
  292. def assign(self, idx, ch):
  293. assert(idx >= 0)
  294. assert len(ch) == 1
  295. hphp_string.string_data_set_char(self.__sd, idx, ch)
  296. def get(self, idx):
  297. return hphp_string.string_data_get_char(self.__sd, idx)
  298. def to_str(self):
  299. return hphp_string.string_data_to_str(self.__sd)
  300. def to_str_null(self):
  301. res = self.to_str()
  302. null_idx = 0
  303. for null_idx in range(0, len(res)):
  304. if res[null_idx] == '\0':
  305. break
  306. return res[:null_idx]
  307. def get_hash(self, length):
  308. return hphp_string.string_data_get_hash(self.__sd, length)
  309. def append(self, o):
  310. hphp_string.string_data_append(self.__sd, o.__sd)
  311. def get_copy(self):
  312. copy = MutableString()
  313. copy.__sd = hphp_string.string_data_copy(self.__sd)
  314. return copy
  315. def increment(self):
  316. hphp_string.string_data_increment(self.__sd)
  317. def is_numeric_string(self, allow_errors=True):
  318. return hphp_string.string_data_is_numeric(self.__sd, allow_errors)
  319. def get_string_data(self):
  320. return self.__sd
  321. def memcmp(self, o, length):
  322. return hphp_string.string_data_memcmp(self.__sd, o.__sd, length)
  323. def __eq__(self, other):
  324. return self.to_str() == other.to_str()
  325. def __ne__(self, other):
  326. return self.to_str() != other.to_str()
  327. @staticmethod
  328. def from_string_data(sd):
  329. ms = MutableString()
  330. ms.__sd = sd
  331. return ms
  332. class MutableStringPtr:
  333. def __init__(self, strval, strlen, null=False):
  334. assert (strval is None) or isinstance(strval, MutableString)
  335. self.strval = strval
  336. self.strlen = strlen
  337. self.null = null
  338. def is_null(self):
  339. return self.null
  340. def assign(self, strval, strlen):
  341. assert (strval is None) or isinstance(strval, MutableString)
  342. if self.null:
  343. raise Exception('null pointer assignment')
  344. self.strval = strval
  345. self.strlen = strlen
  346. def assign_pystr(self, pystr):
  347. import objects
  348. self.strval = objects.MutableString(pystr)
  349. self.strlen = len(pystr)
  350. def deref(self):
  351. if self.null:
  352. raise Exception('null pointer dereference')
  353. return self.strval
  354. def get_length(self):
  355. return self.strlen
  356. def __nonzero__(self):
  357. raise Exception('use is_null() for casting to bool')
  358. def append_strings(lhs, rhs):
  359. assert isinstance(lhs, MutableString)
  360. assert isinstance(rhs, MutableString)
  361. copy = lhs.get_copy()
  362. copy.append(rhs)
  363. return copy