Commits

Alex Gaynor  committed bd79cde

make constants work

  • Participants
  • Parent commits 3489b70
  • Branches erase-raw-mem

Comments (0)

Files changed (2)

File pypy/rlib/rerased_raw.py

 
 from pypy.annotation import model as annmodel
 from pypy.annotation.bookkeeper import getbookkeeper
-from pypy.rpython.annlowlevel import hlstr, llstr, llhelper
+from pypy.rpython.annlowlevel import (hlstr, llstr, llhelper,
+    cast_instance_to_base_ptr)
+from pypy.rpython.rclass import getinstancerepr
 from pypy.rpython.extregistry import ExtRegistryEntry
 from pypy.rpython.lltypesystem import rffi, lltype, llmemory
 from pypy.rpython.lltypesystem.rstr import STR, string_repr
 from pypy.rpython.rmodel import Repr
+from pypy.tool.pairtype import pairtype
 
 
 INT = "i"
     def specialize_call(self, hop):
         return hop.r_result.rtyper_new(hop)
 
+class UntypedStoragePrebuiltEntry(ExtRegistryEntry):
+    _type_ = UntypedStorage
+
+    def compute_annotation(self):
+        return SomeUntypedStorage()
 
 class SomeUntypedStorage(annmodel.SomeObject):
     def _check_idx(self, s_idx):
         assert annmodel.SomeInteger().contains(s_idx)
 
     def rtyper_makerepr(self, rtyper):
-        return UntypedStorageRepr()
+        return UntypedStorageRepr(rtyper)
 
     def method_getlength(self):
         return annmodel.SomeInteger()
         self._check_idx(s_idx)
         assert isinstance(s_obj, annmodel.SomeInstance)
 
+class __extend__(pairtype(SomeUntypedStorage, SomeUntypedStorage)):
+    def union((self, other)):
+        return SomeUntypedStorage()
+
+
+
 UNTYPEDSTORAGE = lltype.GcStruct("untypedstorage",
     ("shape", lltype.Ptr(STR)),
     ("data", lltype.Array(llmemory.Address)),
     lowleveltype = lltype.Ptr(UNTYPEDSTORAGE)
     lltype.attachRuntimeTypeInfo(lowleveltype.TO, customtraceptr=trace_untypedstorage_ptr)
 
+    def __init__(self, rtyper):
+        self.rtyper = rtyper
+
     def _read_index(self, hop):
         v_arr = hop.inputarg(self, arg=0)
         v_idx = hop.inputarg(lltype.Signed, arg=1)
         c_name = hop.inputconst(lltype.Void, "data")
         hop.genop("setinteriorfield", [v_arr, c_name, v_idx, v_value])
 
+    def convert_const(self, value):
+        storage = self.ll_new(llstr(value.shape))
+        for idx, (char, obj) in enumerate(zip(value.shape, value.storage)):
+            if char == INT:
+                storage.data[idx] = rffi.cast(llmemory.Address, obj)
+            elif char == INSTANCE:
+                bk = self.rtyper.annotator.bookkeeper
+                classdef = bk.getuniqueclassdef(type(obj))
+                instancerepr = getinstancerepr(self.rtyper, classdef)
+                ptr = instancerepr.convert_const(obj)
+                storage.data[idx] = llmemory.cast_ptr_to_adr(ptr)
+        return storage
+
     def rtyper_new(self, hop):
         [v_shape] = hop.inputargs(string_repr)
         hop.exception_cannot_occur()

File pypy/rlib/test/test_rerased_raw.py

             return storage.getshape()
 
         llres = self.interpret(f, [])
-        assert hlstr(llres) == "ooi"
+        assert hlstr(llres) == "ooi"
+
+    def test_const(self):
+        class A(object):
+            def __init__(self, v):
+                self.v = v
+        storage = rerased_raw.UntypedStorage("io")
+        storage.setint(0, 1)
+        storage.setinstance(1, A(20))
+        def f(i):
+            A(i)
+            if i:
+                local_storage = rerased_raw.UntypedStorage("ii")
+            else:
+                local_storage = storage
+            return local_storage.getint(0) + local_storage.getinstance(1, A).v
+
+        res = self.interpret(f, [0])
+        assert res == 21