/objects.py
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
- from rpython.rlib.objectmodel import r_dict, compute_hash
- from zend import *
- import hphp_string
- class obj(object):
- pass
- class zval(obj):
- def get_type(self):
- return self.type
- def copy_value_from(self, zv):
- assert isinstance(zv, zval)#, "Invalid zval: %s" % repr(zv)
- self.type = zv.type
- self.is_null = zv.is_null
- masked_type = zv.type & IS_CONSTANT_TYPE_MASK
- if masked_type == IS_NULL:
- pass
- elif masked_type == IS_LONG or masked_type == IS_BOOL:
- self.lval = zv.lval
- elif masked_type == IS_DOUBLE:
- self.dval = zv.dval
- elif masked_type == IS_STRING or masked_type == IS_CONSTANT:
- self.str = zv.str
- elif masked_type == IS_ARRAY or masked_type == IS_CONSTANT_ARRAY:
- # TODO: fix for constant array
- self.happy_ht = zv.happy_ht
- elif masked_type == IS_OBJECT:
- self.obj = zv.obj.get_copy()
- elif masked_type == IS_RESOURCE:
- self.happy_res = zv.happy_res
- else:
- raise Exception('Not implemented yet: %d' % (masked_type))
- def copy_from(self, zv):
- assert isinstance(zv, zval)#, "Invalid zval: %s" % repr(zv)
- self.refcount__gc = zv.refcount__gc
- self.is_ref__gc = zv.is_ref__gc
- self.copy_value_from(zv)
- # Keep the RPython inference happy since pointers can either
- # point to other pointers or a rad.APCFile.CStruct
- class ptr(obj):
- def __init__(self):
- pass
- # This is a 'pseudo' pointer, we could get rid of it anyway.
- class ulong_ptr:
- def __init__(self, longval, null):
- assert isinstance(longval, int)
- self.longval = longval
- self.null = null
- def is_null(self):
- return self.null
- def assign(self, longval):
- assert isinstance(longval, int)
- if self.null:
- raise Exception('null pointer assignment')
- self.longval = longval
- def deref(self):
- if self.null:
- raise Exception('null pointer dereference')
- return self.longval
- def __nonzero__(self):
- raise Exception('use is_null() for casting to bool')
- def copy_from(self, p):
- assert isinstance(ptr, ulong_ptr)
- self.longval = p.longval
- self.null = p.null
- class double_ptr:
- def __init__(self, doubleval, null):
- assert isinstance(doubleval, float)
- self.doubleval = doubleval
- self.null = null
- def is_null(self):
- return self.null
- def assign(self, doubleval):
- assert isinstance(doubleval, float)
- if self.null:
- raise Exception('null pointer assignment')
- self.doubleval = doubleval
- def deref(self):
- if self.null:
- raise Exception('null pointer dereference')
- return self.doubleval
- def __nonzero__(self):
- raise Exception('use is_null() for casting to bool')
- def copy_from(self, p):
- assert isinstance(ptr, double_ptr)
- self.doubleval = p.doubleval
- self.null = p.null
- class zend_function_ptr:
- def __init__(self, zend_function, null):
- #assert isinstance(zend_function, parser_api.ZendOpArray)
- self.__zend_function = zend_function
- self.__null = null
- def is_null(self):
- return self.__null
- def assign(self, zend_function):
- #assert isinstance(zend_function, parser_api.ZendOpArray)
- if self.__null:
- raise Exception('null pointer assignment')
- self.__zend_function = zend_function
- def deref(self):
- if self.__null:
- raise Exception('null pointer dereference')
- return self.__zend_function
- def __nonzero__(self):
- raise Exception('use is_null() for casting to bool')
- # FIXME: merge these two
- class ZendClassEntryPtr:
- def __init__(self, ce):
- self.__ce = ce
- def assign(self, ce):
- self.__ce = ce
- def deref(self):
- return self.__ce
- class class_entry_ptr(ptr):
- def __init__(self, ce):
- self.__ce = ce
- def get_class_entry(self):
- return self.__ce
- def copy(self):
- return class_entry_ptr(self.__ce)
- class op_array_ptr(ptr):
- def __init__(self, op_array):
- self.op_array = op_array
- def get_op_array(self):
- return self.op_array
- def copy(self):
- return op_array_ptr(self.op_array)
- def arg_info(self):
- return self.op_array.arg_info
- def fn_type(self):
- return self.op_array.type
- def fn_flags(self):
- return self.op_array.fn_flags
- def set_fn_flags(self, flags):
- self.op_array.fn_flags = flags
- def prototype(self):
- return self.op_array.prototype
- def set_prototype(self, prototype):
- self.op_array.prototype = prototype
- def function_name(self):
- return self.op_array.function_name
- def scope(self):
- return self.op_array.scope
- def static_variables(self):
- return self.op_array.static_variables
- def incref(self):
- # TODO: check if we need refcounting
- pass
- class property_info_ptr(ptr):
- def __init__(self, prop_info):
- self.prop_info = prop_info
- def copy(self):
- return property_info_ptr(self.prop_info)
- class zval_ptr(ptr):
- def __init__(self, zval=None):
- assert zval is None or isinstance(zval, obj)
- self.__zval = zval
- def is_null(self):
- return self.__zval is None
- def assign(self, zval):
- """Equivalent to *self = zval; from C"""
- assert isinstance(zval, obj)
- if self.__zval is None:
- raise Exception('null pointer assignment')
- self.__zval.copy_from(zval)
- def deref(self):
- """Equivalent to *self from C"""
- if self.__zval is None:
- raise Exception('null pointer dereference')
- return self.__zval
- def __nonzero__(self):
- raise Exception('use is_null() for casting to bool')
- def __eq__(self, o):
- if isinstance(o, zval_ptr):
- return self.__zval is o.__zval
- return False
- def __ne__(self, o):
- return not self.__eq__(o)
- def __repr__(self):
- return "%08x=>%s" % (id(self), (
- repr(self.__zval) if self.__zval is not None else "None"))
- def copy(self):
- return zval_ptr(self.__zval)
- def copy_from(self, p):
- """Equivalent to self = p; from C"""
- assert isinstance(p, zval_ptr)#, "Invalid zval_ptr: %s" % (repr(p))
- self.__zval = p.__zval
- class zval_ptr_ptr(ptr):
- def __init__(self, zp=None):
- assert zp is None or isinstance(zp, ptr)
- self.__zp = zp
-
- def is_null(self):
- return self.__zp is None
- def assign(self, zp):
- assert isinstance(zp, ptr)
- if self.__zp is None:
- raise Exception('null pointer assignment')
- self.__zp.copy_from(zp)
- def deref(self):
- if self.__zp is None:
- raise Exception('null pointer dereference')
- return self.__zp
- def deref_copy(self):
- if self.__zp is None:
- raise Exception('null pointer dereference')
- return self.__zp.copy()
- def __nonzero__(self):
- raise Exception('use is_null() for casting to bool')
- def __eq__(self, o):
- if isinstance(o, zval_ptr_ptr):
- return self.__zp is o.__zp
- return False
- def __ne__(self, o):
- return not self.__eq__(o)
- def __repr__(self):
- return "%08x=>%s" % (id(self), (
- repr(self.__zp) if self.__zp is not None else "None"))
- def copy(self):
- return zval_ptr_ptr(self.__zp)
- def copy_from(self, p):
- assert isinstance(p, zval_ptr_ptr)
- self.__zp = p.__zp
- class zval_ptr_ptr_ptr(ptr):
- def __init__(self, zpp=None):
- assert zpp is None or isinstance(zpp, zval_ptr_ptr)
- self.__zpp = zpp
- def is_null(self):
- return self.__zpp is None
- def assign(self, zpp):
- assert isinstance(zpp, zval_ptr_ptr)
- if self.__zpp is None:
- raise Exception('null pointer assignment')
- self.__zpp.copy_from(zpp)
-
- def deref(self):
- if self.__zpp is None:
- raise Exception('null pointer dereference')
- return self.__zpp
-
- def deref_copy(self):
- if self.__zpp is None:
- raise Exception('null pointer dereference')
- return self.__zpp.copy()
- def __nonzero__(self):
- raise Exception('use is_null() for casting to bool')
-
- def __eq__(self, o):
- if isinstance(o, zval_ptr_ptr_ptr):
- return self.__zpp is o.__zpp
- return False
- def __ne__(self, o):
- return not self.__eq__(o)
- def __repr__(self):
- return "%08x=>%s" % (id(self), (
- repr(self.__zpp) if self.__zpp is not None else "None"))
- def copy_from(self, p):
- assert isinstance(p, zval_ptr_ptr_ptr)
- self.__zpp = p.__zpp
-
- class zend_object(obj):
- def __init__(self):
- self.ce = None
- self.properties = None
- self.guards = None
- class zend_object_ptr(ptr):
- def __init__(self, obj):
- self.__obj = obj
- def deref(self):
- return self.__obj
- def assign(self, obj):
- assert isinstance(obj, zend_object)
- self.__obj = obj
- class zend_guard(obj):
- def __init__(self):
- self.in_get = False
- self.in_set = False
- self.in_unset = False
- self.in_isset = False
- class zend_object_value:
- def __init__(self):
- self.handle = 0
- self.handlers = None
- def get_copy(self):
- # TODO: check if this is really needed
- copy = zend_object_value()
- copy.handle = self.handle
- copy.handlers = self.handlers
- return copy
- class MutableString:
- def __init__(self, s=None):
- if s is None:
- self.__sd = hphp_string.null_string
- else:
- assert isinstance(s, str)
- self.__sd = hphp_string.new_string_data(s)
- def length(self):
- return hphp_string.string_data_get_length(self.__sd)
- def assign(self, idx, ch):
- assert(idx >= 0)
- assert len(ch) == 1
- hphp_string.string_data_set_char(self.__sd, idx, ch)
- def get(self, idx):
- return hphp_string.string_data_get_char(self.__sd, idx)
- def to_str(self):
- return hphp_string.string_data_to_str(self.__sd)
- def to_str_null(self):
- res = self.to_str()
- null_idx = 0
- for null_idx in range(0, len(res)):
- if res[null_idx] == '\0':
- break
- return res[:null_idx]
- def get_hash(self, length):
- return hphp_string.string_data_get_hash(self.__sd, length)
- def append(self, o):
- hphp_string.string_data_append(self.__sd, o.__sd)
- def get_copy(self):
- copy = MutableString()
- copy.__sd = hphp_string.string_data_copy(self.__sd)
- return copy
- def increment(self):
- hphp_string.string_data_increment(self.__sd)
- def is_numeric_string(self, allow_errors=True):
- return hphp_string.string_data_is_numeric(self.__sd, allow_errors)
- def get_string_data(self):
- return self.__sd
- def memcmp(self, o, length):
- return hphp_string.string_data_memcmp(self.__sd, o.__sd, length)
- def __eq__(self, other):
- return self.to_str() == other.to_str()
- def __ne__(self, other):
- return self.to_str() != other.to_str()
- @staticmethod
- def from_string_data(sd):
- ms = MutableString()
- ms.__sd = sd
- return ms
- class MutableStringPtr:
- def __init__(self, strval, strlen, null=False):
- assert (strval is None) or isinstance(strval, MutableString)
- self.strval = strval
- self.strlen = strlen
- self.null = null
- def is_null(self):
- return self.null
- def assign(self, strval, strlen):
- assert (strval is None) or isinstance(strval, MutableString)
- if self.null:
- raise Exception('null pointer assignment')
- self.strval = strval
- self.strlen = strlen
- def assign_pystr(self, pystr):
- import objects
- self.strval = objects.MutableString(pystr)
- self.strlen = len(pystr)
- def deref(self):
- if self.null:
- raise Exception('null pointer dereference')
- return self.strval
- def get_length(self):
- return self.strlen
- def __nonzero__(self):
- raise Exception('use is_null() for casting to bool')
- def append_strings(lhs, rhs):
- assert isinstance(lhs, MutableString)
- assert isinstance(rhs, MutableString)
- copy = lhs.get_copy()
- copy.append(rhs)
- return copy