예제 #1
0
    def _build_methods(self):
        assert len(self.methods) == 0
        methods_temp = {}
        for i in range(capi.c_num_methods(self.space, self)):
            idx = capi.c_method_index_at(self.space, self, i)
            pyname = helper.map_operator_name(self.space,
                capi.c_method_name(self.space, self, idx),
                capi.c_method_num_args(self.space, self, idx),
                capi.c_method_result_type(self.space, self, idx))
            cppmethod = self._make_cppfunction(pyname, idx)
            methods_temp.setdefault(pyname, []).append(cppmethod)
        # the following covers the case where the only kind of operator[](idx)
        # returns are the ones that produce non-const references; these can be
        # used for __getitem__ just as much as for __setitem__, though
        if not "__getitem__" in methods_temp:
            try:
                for m in methods_temp["__setitem__"]:
                    cppmethod = self._make_cppfunction("__getitem__", m.index)
                    methods_temp.setdefault("__getitem__", []).append(cppmethod)
            except KeyError:
                pass          # just means there's no __setitem__ either

        # create the overload methods from the method sets
        for pyname, methods in methods_temp.iteritems():
            CPPMethodSort(methods).sort()
            overload = W_CPPOverload(self.space, self, methods[:])
            self.methods[pyname] = overload
예제 #2
0
 def _find_methods(self):
     num_methods = capi.c_num_methods(self)
     args_temp = {}
     for i in range(num_methods):
         method_name = capi.c_method_name(self, i)
         pymethod_name = helper.map_operator_name(
             method_name, capi.c_method_num_args(self, i), capi.c_method_result_type(self, i)
         )
         if not pymethod_name in self.methods:
             cppfunction = self._make_cppfunction(i)
             overload = args_temp.setdefault(pymethod_name, [])
             overload.append(cppfunction)
     for name, functions in args_temp.iteritems():
         overload = W_CPPOverload(self.space, self, functions[:])
         self.methods[name] = overload
예제 #3
0
    def _setup(self, cppthis):
        self.converters = [
            converter.get_converter(self.space, arg_type, arg_dflt) for arg_type, arg_dflt in self.arg_defs
        ]
        self.executor = executor.get_executor(self.space, capi.c_method_result_type(self.scope, self.index))

        # Each CPPMethod corresponds one-to-one to a C++ equivalent and cppthis
        # has been offset to the matching class. Hence, the libffi pointer is
        # uniquely defined and needs to be setup only once.
        methgetter = capi.c_get_methptr_getter(self.scope, self.index)
        if methgetter and cppthis:  # methods only for now
            funcptr = methgetter(rffi.cast(capi.C_OBJECT, cppthis))
            argtypes_libffi = [conv.libffitype for conv in self.converters if conv.libffitype]
            if len(argtypes_libffi) == len(self.converters) and self.executor.libffitype:
                # add c++ this to the arguments
                libffifunc = libffi.Func(
                    "XXX", [libffi.types.pointer] + argtypes_libffi, self.executor.libffitype, funcptr
                )
                self._libffifunc = libffifunc
예제 #4
0
    def _setup(self, cppthis):
        self.converters = [converter.get_converter(self.space, arg_type, arg_dflt)
                               for arg_type, arg_dflt in self.arg_defs]
        self.executor = executor.get_executor(
            self.space, capi.c_method_result_type(self.space, self.scope, self.index))

        for conv in self.converters:
            if conv.uses_local:
                self.uses_local = True
                break

        # Each CPPMethod corresponds one-to-one to a C++ equivalent and cppthis
        # has been offset to the matching class. Hence, the libffi pointer is
        # uniquely defined and needs to be setup only once.
        methgetter = capi.c_get_methptr_getter(self.space, self.scope, self.index)
        if methgetter and cppthis:      # methods only for now
            cif_descr = lltype.nullptr(jit_libffi.CIF_DESCRIPTION)
            try:
                funcaddr = methgetter(rffi.cast(capi.C_OBJECT, cppthis))
                self._funcaddr = rffi.cast(rffi.VOIDP, funcaddr)

                nargs = self.args_expected + 1                   # +1: cppthis

                # memory block for CIF description (note: not tracked as the life
                # time of methods is normally the duration of the application)
                size = llmemory.sizeof(jit_libffi.CIF_DESCRIPTION, nargs)

                # allocate the buffer
                cif_descr = lltype.malloc(jit_libffi.CIF_DESCRIPTION_P.TO,
                                          llmemory.raw_malloc_usage(size),
                                          flavor='raw', track_allocation=False)

                # array of 'ffi_type*' values, one per argument
                size = rffi.sizeof(jit_libffi.FFI_TYPE_P) * nargs
                atypes = lltype.malloc(rffi.CCHARP.TO, llmemory.raw_malloc_usage(size),
                                       flavor='raw', track_allocation=False)
                cif_descr.atypes = rffi.cast(jit_libffi.FFI_TYPE_PP, atypes)

                # argument type specification
                cif_descr.atypes[0] = jit_libffi.types.pointer   # cppthis
                for i, conv in enumerate(self.converters):
                    if not conv.libffitype:
                        raise FastCallNotPossible
                    cif_descr.atypes[i+1] = conv.libffitype

                # result type specification
                cif_descr.rtype = self.executor.libffitype

                # exchange ---

                # first, enough room for an array of 'nargs' pointers
                exchange_offset = rffi.sizeof(rffi.CCHARP) * nargs
                exchange_offset = (exchange_offset + 7) & ~7     # alignment
                cif_descr.exchange_result = exchange_offset
                cif_descr.exchange_result_libffi = exchange_offset

                # TODO: left this out while testing (see ctypefunc.py)
                # For results of precisely these types, libffi has a
                # strange rule that they will be returned as a whole
                # 'ffi_arg' if they are smaller.  The difference
                # only matters on big-endian.

                # then enough room for the result, rounded up to sizeof(ffi_arg)
                exchange_offset += max(rffi.getintfield(cif_descr.rtype, 'c_size'),
                                       jit_libffi.SIZE_OF_FFI_ARG)

                # loop over args
                for i in range(nargs):
                    exchange_offset = (exchange_offset + 7) & ~7 # alignment
                    cif_descr.exchange_args[i] = exchange_offset
                    exchange_offset += rffi.getintfield(cif_descr.atypes[i], 'c_size')

                # store the exchange data size
                cif_descr.exchange_size = exchange_offset

                # --- exchange

                # extra
                cif_descr.abi = clibffi.FFI_DEFAULT_ABI
                cif_descr.nargs = self.args_expected + 1         # +1: cppthis

                res = jit_libffi.jit_ffi_prep_cif(cif_descr)
                if res != clibffi.FFI_OK:
                    raise FastCallNotPossible

            except Exception, e:
                if cif_descr:
                    lltype.free(cif_descr.atypes, flavor='raw', track_allocation=False)
                    lltype.free(cif_descr, flavor='raw', track_allocation=False)
                cif_descr = lltype.nullptr(jit_libffi.CIF_DESCRIPTION)
                self._funcaddr = lltype.nullptr(rffi.VOIDP.TO)

            self.cif_descr = cif_descr