<commit_before>from topaz.modules.ffi import type as ffitype
from topaz.modules.ffi.type import W_TypeObject
from topaz.modules.ffi._memory_access import (read_and_wrap_from_address,
                                              unwrap_and_write_to_address)

from rpython.rtyper.lltypesystem import rffi, lltype
from rpython.rlib.objectmodel import compute_unique_id
from rpython.rlib import clibffi, jit

from pypy.module._cffi_backend import misc

# This will definitely leak memory
registration = {}

class Data(object):
    def __init__(self, space, w_proc, w_callback_info):
        self.space = space
        self.w_proc = w_proc
        self.w_callback_info = w_callback_info

    def invoke(self, ll_args, ll_res):
        space = self.space
        args_w = []
        for i in range(len(self.w_callback_info.arg_types_w)):
            w_arg_type = self.w_callback_info.arg_types_w[i]
            ll_arg = rffi.cast(rffi.CCHARP, ll_args[i])
            w_arg = self._read_and_wrap_llpointer(space, ll_arg, w_arg_type)
            args_w.append(w_arg)
        w_res = space.send(self.w_proc, 'call', args_w)
        self._unwrap_and_write_rubyobj(space, w_res, ll_res)

    def _read_and_wrap_llpointer(self, space, ll_adr, w_arg_type):
        assert isinstance(w_arg_type, W_TypeObject)
        typeindex = w_arg_type.typeindex
        for t in ffitype.unrolling_types:
            if t == typeindex:
                return read_and_wrap_from_address(space, ll_adr, t, out=False)
        assert 0

    def _unwrap_and_write_rubyobj(self, space, w_obj, ll_adr):
        ll_adr = rffi.cast(rffi.CCHARP, ll_adr)
        w_ret_type = self.w_callback_info.w_ret_type
        assert isinstance(w_ret_type, W_TypeObject)
        typeindex = w_ret_type.typeindex
        for t in ffitype.unrolling_types:
            if t == typeindex:
                unwrap_and_write_to_address(space, w_obj, ll_adr, t, out=True)

class Closure(object):
    def __init__(self, callback_data):
        self.heap = clibffi.closureHeap.alloc()
        self.callback_data = callback_data
        w_callback_info = self.callback_data.w_callback_info<commit_msg>Added OOP approch to callback<commit_after>from topaz.modules.ffi import type as ffitype
from topaz.modules.ffi.type import W_BuiltinType

from rpython.rtyper.lltypesystem import rffi, lltype
from rpython.rlib.objectmodel import compute_unique_id
from rpython.rlib import clibffi, jit

from pypy.module._cffi_backend import misc

# This will definitely leak memory
registration = {}

class Data(object):
    def __init__(self, space, w_proc, w_callback_info):
        self.space = space
        self.w_proc = w_proc
        self.w_callback_info = w_callback_info

    def invoke(self, ll_args, ll_res):
        space = self.space
        args_w = []
        for i in range(len(self.w_callback_info.arg_types_w)):
            w_arg_type = self.w_callback_info.arg_types_w[i]
            ll_arg = rffi.cast(rffi.CCHARP, ll_args[i])
            w_arg = self._read_and_wrap_llpointer(space, ll_arg, w_arg_type)
            args_w.append(w_arg)
        w_res = space.send(self.w_proc, 'call', args_w)
        self._unwrap_and_write_rubyobj(space, w_res, ll_res)

    def _read_and_wrap_llpointer(self, space, ll_adr, w_arg_type):
        assert isinstance(w_arg_type, W_BuiltinType)
        return w_arg_type.rw_strategy.read(space, ll_adr)

    def _unwrap_and_write_rubyobj(self, space, w_obj, ll_adr):
        ll_adr = rffi.cast(rffi.CCHARP, ll_adr)
        w_ret_type = self.w_callback_info.w_ret_type
        assert isinstance(w_ret_type, W_BuiltinType)
        w_ret_type.rw_strategy.write(space, ll_adr, w_obj)

class Closure(object):
    def __init__(self, callback_data):
        self.heap = clibffi.closureHeap.alloc()
        self.callback_data = callback_data
        w_callback_info = self.callback_data.w_callback_info
{'fraction_non_alphanumeric': 0.08062015503875969, 'fraction_numerical': 0.00025839793281653745, 'mean_word_length': 3.1489817792068595, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4914693', 'n_tokens_mistral': 1367, 'n_tokens_neox': 1349, 'n_words': 258}