示例#1
0
def register_finalizer(caller, builder, env, context, type, gcmod, obj):
    """
    Register a finalizer for the object given as pointer `obj`.
    """
    from flypy.pipeline import phase
    curphase = env['flypy.state.phase']

    #(TODO: (indirect) allocation of a new object in __del__ will recurse
    # infinitely)

    if '__del__' in type.fields:
        # Compile __del__
        __del__ = type.fields['__del__']
        lfunc, env = phase.apply_phase(phase.codegen, __del__, (type,),
                                       env['flypy.target'])

        # Retrieve function address of __del__
        cfunc = env["codegen.llvm.ctypes"]
        pointerval = ctypes.cast(cfunc, ctypes.c_void_p).value
        ptr = ir.Pointer(pointerval, ptypes.Pointer(ptypes.Void))
        context[ptr] = Pointer[void]

        # Cast object to void *
        obj_p = builder.convert(ptypes.Opaque, obj)
        context[obj_p] = Pointer[void]

        # Call gc_add_finalizer with (obj, ptr)
        result = caller.call(curphase, gcmod.gc_add_finalizer, [obj_p, ptr])
        context[result] = void

        return [obj_p, result]
示例#2
0
def register_finalizer(caller, builder, env, context, type, gcmod, obj):
    """
    Register a finalizer for the object given as pointer `obj`.
    """
    from flypy.pipeline import phase
    curphase = env['flypy.state.phase']

    #(TODO: (indirect) allocation of a new object in __del__ will recurse
    # infinitely)

    if '__del__' in type.fields:
        # Compile __del__
        __del__ = type.fields['__del__']
        lfunc, env = phase.apply_phase(phase.codegen, __del__, (type, ),
                                       env['flypy.target'])

        # Retrieve function address of __del__
        cfunc = env["codegen.llvm.ctypes"]
        pointerval = ctypes.cast(cfunc, ctypes.c_void_p).value
        ptr = ir.Pointer(pointerval, ptypes.Pointer(ptypes.Void))
        context[ptr] = Pointer[void]

        # Cast object to void *
        obj_p = builder.convert(ptypes.Opaque, obj)
        context[obj_p] = Pointer[void]

        # Call gc_add_finalizer with (obj, ptr)
        result = caller.call(curphase, gcmod.gc_add_finalizer, [obj_p, ptr])
        context[result] = void

        return [obj_p, result]
示例#3
0
    def call(self, phase_to_run, nb_func, args, argtypes=None, result=None):
        from flypy.pipeline import phase

        # Apply phase
        if argtypes is None:
            argtypes = tuple(self.context[arg] for arg in args)

        target = self.env['flypy.target']
        f, env = phase.apply_phase(phase_to_run, nb_func, argtypes, target)

        # Generate call
        result = self.builder.call(ptypes.Opaque, f, args, result=result)

        # Update context
        self.context[result] = env["flypy.typing.restype"]

        return result
示例#4
0
文件: utils.py 项目: filmackay/flypy
    def call(self, phase_to_run, nb_func, args, argtypes=None, result=None):
        from flypy.pipeline import phase

        # Apply phase
        if argtypes is None:
            argtypes = tuple(self.context[arg] for arg in args)

        target = self.env['flypy.target']
        f, env = phase.apply_phase(phase_to_run, nb_func, argtypes, target)

        # Generate call
        result = self.builder.call(ptypes.Opaque, f, args, result=result)

        # Update context
        self.context[result] = env["flypy.typing.restype"]

        return result