Example #1
0
    def def_method(self,
                   signature,
                   name=None,
                   error=None,
                   flags=0,
                   error_handler=handle_as_warning,
                   check_num_args=True):
        try:
            i = signature.index('this')
            signature[i] = ThisUnwrapper(self.instance_class)
        except ValueError:
            pass

        def inner(ll_func):
            fname = name or ll_func.__name__
            fullname = self.name + "::" + fname
            func = new_function(ll_func, signature, fullname, error,
                                error_handler, check_num_args)
            method = Method(func, flags, self)
            meth_id = method.get_identifier()
            if not meth_id in self.methods:
                raise ValueError(
                    "Unknown method %s was not declared in the class "
                    "definition" % method.repr())
            if self.methods[meth_id] is not None:
                raise ValueError("Duplicate implementation for method %s!" %
                                 method.repr())
            self.methods[meth_id] = method
            if meth_id == '__construct':
                self.constructor_method = method
            return ll_func

        return inner
Example #2
0
from hippy import consts
from hippy.klass import def_class
from hippy.objects.instanceobject import W_InstanceObject
from hippy.builtin import wrap_method, ThisUnwrapper
from hippy.builtin_klass import GetterSetterWrapper


class W_ReflectionFunctionAbstract(W_InstanceObject):
    pass


@wrap_method(['interp', ThisUnwrapper(W_ReflectionFunctionAbstract)],
             name='ReflectionFunctionAbstract::getName')
def get_name(interp, this):
    return _get_name(interp, this)


def _get_class(interp, this):
    return interp.space.wrap(this.ref_klass.name)


def _set_class(interp, this, w_value):
    pass


def _get_name(interp, this):
    return interp.space.wrap(this.ref_method.get_name())


def _set_name(interp, this, w_value):
    pass
Example #3
0
    def get_parameters(self):
        args = self.function.get_signature().args
        parameters = []
        for i in range(len(args)):
            parameters.append(
                (self.space.wrap(i),
                 k_ReflectionParameter.call_args(
                     self.interp,
                     [self.space.wrap(self.name),
                      self.space.wrap(i)])))

        return self.space.new_array_from_pairs(parameters)


@wrap_method(['interp', ThisUnwrapper(W_ReflectionFunction), W_Root],
             name='ReflectionFunction::__construct')
def construct(interp, this, function):
    if isinstance(function, W_ClosureObject):
        this.ref_fun = ClosureWrapper(interp, function)
    else:
        this.ref_fun = NameWrapper(interp, function)


@wrap_method(['interp', ThisUnwrapper(W_ReflectionFunction)],
             name='ReflectionFunction::getName')
def get_name(interp, this):
    return this.ref_fun.get_name()


@wrap_method(['interp', ThisUnwrapper(W_ReflectionFunction)],
Example #4
0
    def build(self, klass):
        return GetterSetter(self.getter, self.setter, self.name, klass,
                            self.accflags)


class W_ExceptionObject(W_InstanceObject):
    def setup(self, interp):
        self.traceback = interp.get_traceback()

    def get_message(self, interp):
        return self.getattr(interp, 'message', k_Exception)


@wrap_method([
    'interp',
    ThisUnwrapper(W_ExceptionObject),
    Optional(str),
    Optional(int),
    Optional('object')
],
             name='Exception::__construct')
def new_exception(interp, this, message='', code=0, w_previous=None):
    this.setattr(interp, 'file', interp.space.wrap(this.traceback[0][0]),
                 k_Exception)
    this.setattr(interp, 'message', interp.space.wrap(message), k_Exception)
    this.setattr(interp, 'code', interp.space.wrap(code), k_Exception)
    if w_previous is None:
        w_previous = interp.space.w_Null
    elif not k_Exception.is_parent_of(w_previous.klass):
        interp.fatal("Wrong parameters for "
                     "Exception([string $exception [, long $code [, "
Example #5
0
            getter = self.getter
            setter = self.setter
        return GetterSetter(getter, setter, self.name, klass, self.accflags)


class W_ExceptionObject(W_InstanceObject):
    def setup(self, interp):
        self.traceback = interp.get_traceback()

    def get_message(self, interp):
        return self.getattr(interp, 'message', k_Exception)


@wrap_method([
    'interp',
    ThisUnwrapper(W_ExceptionObject),
    Optional(str),
    Optional(int),
    Optional(Nullable('object'))
],
             name='Exception::__construct')
def new_exception(interp, this, message='', code=0, w_previous=None):
    space = interp.space
    this.setattr(interp, 'file', space.wrap(this.traceback[0][0]), k_Exception)
    this.setattr(interp, 'line', space.wrap(this.traceback[0][2]), k_Exception)
    this.setattr(interp, 'message', space.wrap(message), k_Exception)
    this.setattr(interp, 'code', space.wrap(code), k_Exception)
    if w_previous is None:
        w_previous = space.w_Null
    elif not k_Exception.is_parent_of(w_previous.klass):
        interp.fatal("Wrong parameters for "
Example #6
0
    open_mode = None

    def __init__(self, klass, dct_w):
        W_InstanceObject.__init__(self, klass, dct_w)

    def clone(self, interp, contextclass):
        w_res = W_InstanceObject.clone(self, interp, contextclass)
        w_res.file_name = self.file_name
        w_res.path_name = self.path_name
        w_res.delimiter = self.delimiter
        w_res.enclosure = self.enclosure
        w_res.open_mode = self.open_mode
        return w_res


@wrap_method(['interp', ThisUnwrapper(W_SplFileInfo), str],
             name='SplFileInfo::__construct')
def construct(interp, this, file_name):
    this.file_name = file_name
    this.path_name = rpath.realpath(file_name)


@wrap_method(['interp', ThisUnwrapper(W_SplFileInfo)],
             name='SplFileInfo::__toString')
def spl_toString(interp, this):
    return interp.space.wrap(this.file_name)


@wrap_method(['interp', ThisUnwrapper(W_SplFileInfo), Optional(str)],
             name='SplFileInfo::getBasename')
def get_basename(interp, this, suffix=''):
Example #7
0
from hippy.builtin_klass import k_Iterator
from hippy.builtin import ThisUnwrapper, Optional, wrap_method
from hippy.klass import def_class
from hippy.objects.base import W_Root
from hippy.objects.instanceobject import W_InstanceObject
from hippy import consts


class W_ApplevelArrayIterator(W_InstanceObject):
    pass


@wrap_method(
    ['interp',
     ThisUnwrapper(W_ApplevelArrayIterator),
     Optional(W_Root)],
    name='ArrayIterator::__construct')
def ArrayIterator_construct(interp, this, w_arr=None):
    if w_arr is None:
        w_arr = interp.space.new_array_from_list([])
    this.setattr(interp, "storage", w_arr, k_ArrayIterator)


@wrap_method([], name='ArrayIterator::current')
def ArrayIterator_current():
    pass


@wrap_method([], name='ArrayIterator::next')
def ArrayIterator_next():
    pass
Example #8
0
@wrap_method(['interp', W_Root, str, Optional(bool)],
             name='ReflectionProperty::export', flags=consts.ACC_STATIC)
def export(interp, w_klass, name, return_string=False):
    refl = k_ReflectionProperty.call_args(interp,
            [w_klass, interp.space.wrap(name)])
    result = refl.get_str()
    if return_string:
        return interp.space.wrap(result)
    else:
        interp.writestr(result)
        interp.writestr('\n')
        return interp.space.w_Null


@wrap_method(['interp', ThisUnwrapper(W_ReflectionProperty), W_Root, str],
             name='ReflectionProperty::__construct')
def construct(interp, this, w_class, property_name):
    space = interp.space
    if space.is_str(w_class):
        class_name = space.str_w(w_class)
        klass = interp.lookup_class_or_intf(class_name)
        if klass is None:
            msg = "Class %s does not exist" % class_name
            raise PHPException(k_ReflectionException.call_args(
                interp, [space.wrap(msg)]))
    elif isinstance(w_class, W_InstanceObject):
        klass = w_class.klass
        class_name = klass.name
    else:
        msg = ("The parameter class is expected to be either a string "
Example #9
0
from hippy import consts
from hippy.klass import def_class
from hippy.objects.base import W_Root
from hippy.objects.instanceobject import W_InstanceObject
from hippy.objects.strobject import W_ConstStringObject
from hippy.builtin import wrap_method, ThisUnwrapper
from hippy.builtin_klass import GetterSetterWrapper


class W_ReflectionParameter(W_InstanceObject):
    pass


@wrap_method(['interp', ThisUnwrapper(W_ReflectionParameter), W_Root, int],
             name='ReflectionParameter::__construct')
def construct(interp, this, function, parameter):

    if isinstance(function, W_ConstStringObject):
        name = interp.space.str_w(function)
        function  = interp.lookup_function(name)
        signature = function.get_signature()

        this.ref_parameter = signature.args[parameter]
    else:
        args = function.as_rdict().values()
        klass_name = interp.space.str_w(args[0])
        method_name = interp.space.str_w(args[1])
        klass = interp.lookup_class_or_intf(klass_name)
        method = klass.methods[method_name]
        signature = method.get_signature()
Example #10
0
        return w_res


class W_SplFileObject(W_SplFileInfo):
    def clone(self, interp, contextclass):
        w_res = W_InstanceObject.clone(self, interp, contextclass)
        assert isinstance(w_res, W_SplFileObject)
        w_res.file_name = self.file_name
        w_res.path_name = self.path_name
        w_res.delimiter = self.delimiter
        w_res.enclosure = self.enclosure
        w_res.open_mode = self.open_mode
        return w_res


@wrap_method(['interp', ThisUnwrapper(W_SplFileInfo), str],
             name='SplFileInfo::__construct')
def construct(interp, this, file_name):
    this.file_name = file_name
    this.path_name = rpath.realpath(file_name)


@wrap_method(['interp', ThisUnwrapper(W_SplFileInfo)],
             name='SplFileInfo::__toString')
def spl_toString(interp, this):
    return interp.space.wrap(this.file_name)


@wrap_method(['interp', ThisUnwrapper(W_SplFileInfo), Optional(str)],
             name='SplFileInfo::getBasename')
def get_basename(interp, this, suffix=''):
Example #11
0
from hippy import consts
from hippy.builtin import wrap_method, Optional, ThisUnwrapper
from hippy.builtin import StringArg
from hippy.builtin import LongArg
from hippy.builtin import InstanceUnwrapper, handle_as_exception
from hippy.klass import def_class
from hippy.objspace import getspace

from hippy.module.date import timelib
from hippy.module.date import W_DateTimeZone, W_DateTime
from hippy.module.date import common




@wrap_method(['interp', ThisUnwrapper(W_DateTimeZone), StringArg()],
             name='DateTimeZone::__construct', error_handler=handle_as_exception)
def construct(interp, this, timezone_name):
    common.initialize_timezone(interp, "DateTimeZone::__construct", this, timezone_name)


@wrap_method(['space', ThisUnwrapper(W_DateTimeZone)],
             name='DateTimeZone::getName', error=False)
def get_name(space, this):
    return space.wrap(this.timezone_info.get_name())


@wrap_method(['interp', ThisUnwrapper(W_DateTimeZone),
              InstanceUnwrapper(W_DateTime, 'DateTime', null=False)],
             name='DateTimeZone::getOffset', error=False)
def get_offset(interp, this, w_datetime):
Example #12
0
        self.startline = startline
        self.endline = endline
        self.doc = doc


class W_ReflectionClass(W_InstanceObject):
    refl_klass = None

    def get_refl_klass(self, interp):
        if self.refl_klass is None:
            interp.fatal("Internal error: Failed to retrieve the "
                         "reflection object")
        return self.refl_klass


@wrap_method(['interp', ThisUnwrapper(W_ReflectionClass), W_Root],
             name='ReflectionClass::__construct')
def reflection_class_construct(interp, this, klass):
    space = interp.space

    if isinstance(klass, W_ConstStringObject):
        name = space.str_w(klass)
        this.refl_klass = interp.lookup_class_or_intf(name)

    if isinstance(klass, W_InstanceObject):
        this.refl_klass = klass.getclass()


@wrap_method(['interp', ThisUnwrapper(W_ReflectionClass), 'args_w'],
             name='ReflectionClass::newInstance')
def reflection_class_new_instance(interp, this, args_w):
Example #13
0
        w_this = self.w_this if self.static is False else None
        thisclass = w_this.getclass() if w_this is not None else None
        return W_CallClosure(thisclass, self._func, w_this, self.closure_args)

    def put_closure(self, args_w):
        n = len(self.closure_args)
        for i, w_arg in enumerate(args_w):
            self.closure_args[n - i - 1] = w_arg

    def getmeth(self, space, name, contextclass=None):
        if name.lower() == "__invoke":
            return self.get_callable()
        return W_InstanceObject.getmeth(self, space, name, contextclass)


@wrap_method(['interp', ThisUnwrapper(W_ClosureObject), 'args_w'],
             name='Closure::__invoke')
def closure_invoke(interp, this, args_w):
    return this._func.call_args(interp, args_w)


k_Closure = def_class('Closure', [closure_invoke])


def new_closure(space, func, w_this, static=False):
    w_res = W_ClosureObject(func,
                            k_Closure,
                            k_Closure.get_initial_storage_w(space)[:],
                            w_this=w_this,
                            static=static)
    return w_res
Example #14
0
        interp.warn(warn_str)
        return interp.space.w_False

    w_directory = W_Directory(k_Directory, [])
    w_directory.path = space.newstr(directory)

    w_handle = W_DirResource(space, directory)
    w_handle.open()
    w_directory.handle = w_handle

    return w_directory


@wrap_method([
    'interp',
    ThisUnwrapper(W_Directory),
    Optional(Resource(W_DirResource, True))
],
             name='Directory::read')
def dir_read(interp, this, w_dir=None):
    if w_dir is None:
        w_dir = this.handle
    assert isinstance(w_dir, W_DirResource)
    if not w_dir.is_valid():
        interp.warn("Directory::read(): %d is not a valid Directory resource" %
                    w_dir.res_id)
        return interp.space.w_False
    return w_dir.read()


@wrap_method([
Example #15
0
from hippy.objects.base import W_Root
from hippy.objects.instanceobject import W_InstanceObject
from hippy.objects.intobject import W_IntObject
from hippy.module.spl.exception import (k_LogicException,
                                        k_BadMethodCallException,
                                        k_InvalidArgumentException,
                                        k_UnexpectedValueException)
from hippy.module.spl.interface import k_OuterIterator
from hippy.module.spl.arrayiter import k_ArrayIterator


class W_IteratorIterator(W_InstanceObject):
    inner = None


@wrap_method(['interp', ThisUnwrapper(W_IteratorIterator), 'object'],
             name='IteratorIterator::__construct')
def ii_construct(interp, this, w_iterator):
    if w_iterator.klass.is_iterable:
        w_iterator = interp.getmeth(w_iterator,
                                    'getIterator').call_args(interp, [])
    this.inner = w_iterator


@wrap_method(['interp', ThisUnwrapper(W_IteratorIterator)],
             name='IteratorIterator::current')
def ii_current(interp, this):
    return interp.getmeth(this.inner, 'current').call_args(interp, [])


@wrap_method(['interp', ThisUnwrapper(W_IteratorIterator)],
Example #16
0
from hippy.objspace import getspace
from hippy.builtin import (wrap_method, Optional, ThisUnwrapper, StringArg,
                           LongArg, BoolArg, InstanceUnwrapper,
                           handle_as_exception)
from hippy.klass import def_class
from hippy.builtin_klass import GetterSetterWrapper, k_Exception

from hippy.module.date import timelib
from hippy.module.date import W_DateTime, W_DateTimeZone
from hippy.module.date.dateinterval_klass import W_DateInterval, k_DateInterval
from hippy.module.date import common


@wrap_method([
    'interp',
    ThisUnwrapper(W_DateTime),
    Optional(StringArg()),
    Optional(InstanceUnwrapper(W_DateTimeZone, 'DateTimeZone'))
],
             name='DateTime::__construct',
             error_handler=handle_as_exception)
def construct(interp, this, format_string=None, w_datetimezone=None):

    error = common.initialize_date(interp, 'DateTime::__construct', this,
                                   format_string, w_datetimezone)

    if error:
        raise PHPException(
            k_Exception.call_args(interp, [
                interp.space.wrap("%s(): %s" %
                                  ('DateTime::__construct', error))
Example #17
0
from rpython.rlib.rarithmetic import intmask

from hippy import consts
from hippy.error import PHPException
from hippy.builtin import wrap_method, ThisUnwrapper, StringArg
from hippy.builtin_klass import GetterSetterWrapper, k_Exception
from hippy.klass import def_class
from hippy.module.date import timelib
from hippy.objects.instanceobject import W_InstanceObject


class W_DateInterval(W_InstanceObject):
    pass


@wrap_method(['interp', ThisUnwrapper(W_DateInterval), StringArg(None)],
             name='DateInterval::__construct')
def construct(interp, this, spec):

    exc_obj = k_Exception.call_args(
        interp, [interp.space.wrap('Unknown or bad format (%s)' % spec)]
    )

    if not (len(spec) > 1 and spec[0] == 'P'):
        raise PHPException(exc_obj)

    index = 1
    time = False
    formats = {'y': 0, 'm': 0, 'd':0, 'h':0, 'i':0 ,'s': 0}

    while index < len(spec):