| 1 |
from rpython.rtyper.lltypesystem import lltype, rffi, llmemory |
| 2 |
from rpython.rlib.objectmodel import we_are_translated |
| 3 |
from rpython.rlib import rgc, jit |
| 4 |
from rpython.rlib.debug import ll_assert |
| 5 |
from pypy.interpreter.error import OperationError, oefmt |
| 6 |
from pypy.interpreter.baseobjspace import W_Root |
| 7 |
from pypy.module._hpy_universal.apiset import API |
| 8 |
from pypy.module._hpy_universal import llapi |
| 9 |
from pypy.module._hpy_universal.interp_type import W_HPyObject, W_HPyTypeObject |
| 10 |
|
| 11 |
class UntranslatedHPyFieldStorage(object): |
| 12 |
""" |
| 13 |
Proper support for HPyField is possible only after translation, because it |
| 14 |
needs a tight integration with the GC. |
| 15 |
|
| 16 |
For untranslated apptests, we use this small helper to keep track of which |
| 17 |
object is stored in every HPyField. We don't need anything fancy, just |
| 18 |
enough logic to make the tests work. In particular, we want to ensure that |
| 19 |
if we HPyField_Store() something, we can HPyField_Load() it later. |
| 20 |
""" |
| 21 |
|
| 22 |
def __init__(self): |
| 23 |
self._fields = {} |
| 24 |
|
| 25 |
def _freeze_(self): |
| 26 |
raise Exception('You should not use UntranslatedHPyFieldStorage ' |
| 27 |
'from translated code') |
| 28 |
|
| 29 |
def store_w(self, pf, w_obj): |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
unique_id = rffi.cast(lltype.Signed, pf) |
| 39 |
pf[0] = unique_id |
| 40 |
self._fields[unique_id] = w_obj |
| 41 |
|
| 42 |
def delete(self, pf): |
| 43 |
unique_id = rffi.cast(lltype.Signed, pf) |
| 44 |
self._fields.pop(unique_id, None) |
| 45 |
pf[0] = 0 |
| 46 |
|
| 47 |
def load_w(self, f): |
| 48 |
|
| 49 |
if f not in self._fields: |
| 50 |
|
| 51 |
|
| 52 |
assert False, 'boom' |
| 53 |
return self._fields[f] |
| 54 |
|
| 55 |
_STORAGE = UntranslatedHPyFieldStorage() |
| 56 |
|
| 57 |
@jit.dont_look_inside |
| 58 |
def field_store_w(space, pf, w_target, w_value): |
| 59 |
ll_assert(isinstance(w_target, W_HPyObject), 'h_target is not a valid HPy object') |
| 60 |
assert isinstance(w_target, W_HPyObject) |
| 61 |
assert isinstance(w_value, W_Root) |
| 62 |
if not we_are_translated(): |
| 63 |
_STORAGE.store_w(pf, w_value) |
| 64 |
else: |
| 65 |
storage = w_target._hpy_get_gc_storage(space) |
| 66 |
|
| 67 |
ll_assert(bool(storage.tp_traverse), "required tp_traverse function missing on storage") |
| 68 |
assert bool(storage.tp_traverse) |
| 69 |
rgc.ll_writebarrier(storage) |
| 70 |
|
| 71 |
gcref = rgc.cast_instance_to_gcref(w_value) |
| 72 |
pf[0] = rffi.cast(lltype.Signed, gcref) |
| 73 |
|
| 74 |
|
| 75 |
@jit.dont_look_inside |
| 76 |
@API.func("void HPyField_Store(HPyContext *ctx, HPy h_target, HPyField *pf, HPy h)") |
| 77 |
def HPyField_Store(space, handles, ctx, h_target, pf, h): |
| 78 |
|
| 79 |
if not we_are_translated(): |
| 80 |
|
| 81 |
if h == handles.NULL: |
| 82 |
_STORAGE.delete(pf) |
| 83 |
else: |
| 84 |
w_obj = handles.deref(h) |
| 85 |
_STORAGE.store_w(pf, w_obj) |
| 86 |
return |
| 87 |
|
| 88 |
|
| 89 |
if h == handles.NULL: |
| 90 |
pf[0] = 0 |
| 91 |
else: |
| 92 |
w_target = handles.deref(h_target) |
| 93 |
ll_assert(isinstance(w_target, W_HPyObject) or isinstance(w_target, W_HPyTypeObject), 'h_target is not a valid HPy object') |
| 94 |
assert (isinstance(w_target, W_HPyObject) or isinstance(w_target, W_HPyTypeObject)) |
| 95 |
storage = w_target._hpy_get_gc_storage(space) |
| 96 |
|
| 97 |
ll_assert(bool(storage.tp_traverse), "required tp_traverse function missing on storage") |
| 98 |
assert bool(storage.tp_traverse) |
| 99 |
rgc.ll_writebarrier(storage) |
| 100 |
|
| 101 |
w_obj = handles.deref(h) |
| 102 |
gcref = rgc.cast_instance_to_gcref(w_obj) |
| 103 |
pf[0] = rffi.cast(lltype.Signed, gcref) |
| 104 |
|
| 105 |
@jit.dont_look_inside |
| 106 |
def field_load_w(space, w_source, f): |
| 107 |
if we_are_translated(): |
| 108 |
ll_assert(isinstance(w_source, W_HPyObject) or isinstance(w_source, W_HPyTypeObject), 'h_target is not a valid HPy object') |
| 109 |
assert (isinstance(w_source, W_HPyObject) or isinstance(w_source, W_HPyTypeObject)) |
| 110 |
gcref = rffi.cast(llmemory.GCREF, f) |
| 111 |
w_obj = rgc.try_cast_gcref_to_instance(W_Root, gcref) |
| 112 |
|
| 113 |
|
| 114 |
assert w_obj is not None |
| 115 |
else: |
| 116 |
|
| 117 |
w_obj = _STORAGE.load_w(f) |
| 118 |
return w_obj |
| 119 |
|
| 120 |
def field_delete_w(pf): |
| 121 |
if we_are_translated(): |
| 122 |
pf[0] = 0 |
| 123 |
else: |
| 124 |
_STORAGE.delete(pf) |
| 125 |
|
| 126 |
|
| 127 |
@API.func("HPy HPyField_Load(HPyContext *ctx, HPy source_object, HPyField source_field)") |
| 128 |
def HPyField_Load(space, handles, ctx, h_source, f): |
| 129 |
w_source = handles.deref(h_source) |
| 130 |
w_obj = field_load_w(space, w_source, f) |
| 131 |
return handles.new(w_obj) |
| 132 |
|
| 133 |
def is_hpy_object(w_obj): |
| 134 |
return isinstance(w_obj, W_HPyObject) or isinstance(w_obj, W_HPyTypeObject) |
| 135 |
|
| 136 |
def hpy_get_referents(space, w_obj): |
| 137 |
""" |
| 138 |
NOT_RPYTHON. Called by gc.get_referents, only for hpy objects and only for tests |
| 139 |
""" |
| 140 |
w_type = space.type(w_obj) |
| 141 |
assert is_hpy_object(w_obj) |
| 142 |
assert isinstance(w_type, W_HPyTypeObject) |
| 143 |
if w_type.tp_traverse: |
| 144 |
ll_collect_fields = _collect_fields.get_llhelper(space) |
| 145 |
assert _collect_fields.allfields == [] |
| 146 |
NULL = llapi.cts.cast('void *', 0) |
| 147 |
w_type.tp_traverse(w_obj._hpy_get_raw_storage(space), ll_collect_fields, NULL) |
| 148 |
result = _collect_fields.allfields |
| 149 |
_collect_fields.allfields = [] |
| 150 |
return result |
| 151 |
|
| 152 |
|
| 153 |
@API.func("int _collect_fields(HPyField *f, void *arg)", |
| 154 |
error_value=API.int(-1), is_helper=True) |
| 155 |
def _collect_fields(space, handles, f, arg): |
| 156 |
""" |
| 157 |
Only for tests, see hpy_get_referents |
| 158 |
""" |
| 159 |
assert not we_are_translated() |
| 160 |
unique_id = rffi.cast(lltype.Signed, f) |
| 161 |
w_obj = _STORAGE._fields[unique_id] |
| 162 |
_collect_fields.allfields.append(w_obj) |
| 163 |
return API.int(0) |
| 164 |
_collect_fields.allfields = [] |