| 1 |
from pypy.interpreter.error import oefmt |
| 2 |
from pypy.interpreter.baseobjspace import W_Root |
| 3 |
from pypy.interpreter.typedef import TypeDef |
| 4 |
from pypy.module._cffi_backend.cdataobj import W_CData |
| 5 |
from pypy.module._cffi_backend import newtype |
| 6 |
from rpython.rlib import rgil |
| 7 |
from rpython.rlib.objectmodel import we_are_translated |
| 8 |
from rpython.rtyper.lltypesystem import lltype, rffi |
| 9 |
from rpython.translator.tool.cbuild import ExternalCompilationInfo |
| 10 |
|
| 11 |
|
| 12 |
class W_GlobSupport(W_Root): |
| 13 |
_immutable_fields_ = ['w_ctype', 'ptr', 'fetch_addr'] |
| 14 |
|
| 15 |
def __init__(self, space, name, w_ctype, ptr=lltype.nullptr(rffi.CCHARP.TO), |
| 16 |
fetch_addr=lltype.nullptr(rffi.VOIDP.TO)): |
| 17 |
self.space = space |
| 18 |
self.name = name |
| 19 |
self.w_ctype = w_ctype |
| 20 |
self.ptr = ptr |
| 21 |
self.fetch_addr = fetch_addr |
| 22 |
|
| 23 |
def fetch_global_var_addr(self): |
| 24 |
if self.ptr: |
| 25 |
result = self.ptr |
| 26 |
else: |
| 27 |
if not we_are_translated(): |
| 28 |
FNPTR = rffi.CCallback([], rffi.VOIDP) |
| 29 |
fetch_addr = rffi.cast(FNPTR, self.fetch_addr) |
| 30 |
rgil.release() |
| 31 |
result = fetch_addr() |
| 32 |
rgil.acquire() |
| 33 |
else: |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
result = pypy__cffi_fetch_var(self.fetch_addr) |
| 38 |
result = rffi.cast(rffi.CCHARP, result) |
| 39 |
if not result: |
| 40 |
from pypy.module._cffi_backend import ffi_obj |
| 41 |
ffierror = ffi_obj.get_ffi_error(self.space) |
| 42 |
raise oefmt(ffierror, "global variable '%s' is at address NULL", |
| 43 |
self.name) |
| 44 |
return result |
| 45 |
|
| 46 |
def read_global_var(self): |
| 47 |
return self.w_ctype.convert_to_object(self.fetch_global_var_addr()) |
| 48 |
|
| 49 |
def write_global_var(self, w_newvalue): |
| 50 |
self.w_ctype.convert_from_object(self.fetch_global_var_addr(), |
| 51 |
w_newvalue) |
| 52 |
|
| 53 |
def address(self): |
| 54 |
w_ctypeptr = newtype.new_pointer_type(self.space, self.w_ctype) |
| 55 |
return W_CData(self.space, self.fetch_global_var_addr(), w_ctypeptr) |
| 56 |
|
| 57 |
W_GlobSupport.typedef = TypeDef("_cffi_backend.__FFIGlobSupport") |
| 58 |
W_GlobSupport.typedef.acceptable_as_base_class = False |
| 59 |
|
| 60 |
|
| 61 |
eci = ExternalCompilationInfo(post_include_bits=[""" |
| 62 |
static void *pypy__cffi_fetch_var(void *fetch_addr) { |
| 63 |
return ((void*(*)(void))fetch_addr)(); |
| 64 |
} |
| 65 |
"""]) |
| 66 |
|
| 67 |
pypy__cffi_fetch_var = rffi.llexternal( |
| 68 |
"pypy__cffi_fetch_var", [rffi.VOIDP], rffi.VOIDP, |
| 69 |
compilation_info=eci) |