Alex Gaynor avatar Alex Gaynor committed b7a5342

(arigato, alex, fijal): Can now get/set instances!

Comments (0)

Files changed (2)

pypy/rlib/rerased_raw.py

 """
 
 from pypy.annotation import model as annmodel
+from pypy.annotation.bookkeeper import getbookkeeper
 from pypy.rpython.extregistry import ExtRegistryEntry
 from pypy.rpython.lltypesystem import rffi, lltype, llmemory
 from pypy.rpython.rmodel import Repr
     # def setfloat(self, idx, f):
     #     self.storage[idx] = f
 
-    def getinstance(self, cls, idx):
+    def getinstance(self, idx, cls):
         obj = self.storage[idx]
         assert isinstance(obj, cls)
         return obj
 
 
 class SomeUntypedStorage(annmodel.SomeObject):
+    def _check_idx(self, s_idx):
+        assert annmodel.SomeInteger().contains(s_idx)
+
     def rtyper_makerepr(self, rtyper):
         return UntypedStorageRepr()
 
     def method_getint(self, s_idx):
-        assert annmodel.SomeInteger().contains(s_idx)
+        self._check_idx(s_idx)
         return annmodel.SomeInteger()
 
     def method_setint(self, s_idx, s_v):
-        assert annmodel.SomeInteger().contains(s_idx)
+        self._check_idx(s_idx)
         assert annmodel.SomeInteger().contains(s_v)
 
+    def method_getinstance(self, s_idx, s_cls):
+        self._check_idx(s_idx)
+        assert isinstance(s_cls, annmodel.SomePBC)
+        bookkeeper = getbookkeeper()
+        classdef = bookkeeper.getuniqueclassdef(s_cls.const)
+        return annmodel.SomeInstance(classdef, can_be_None=True)
+
+    def method_setinstance(self, s_idx, s_obj):
+        self._check_idx(s_idx)
+        assert isinstance(s_obj, annmodel.SomeInstance)
+
 class UntypedStorageRepr(Repr):
     lowleveltype = lltype.Ptr(lltype.GcArray(lltype.Signed))
 
         [v_arr, v_idx, v_value] = hop.inputargs(self, lltype.Signed, lltype.Signed)
         hop.genop("setarrayitem", [v_arr, v_idx, v_value])
 
+    def rtype_method_getinstance(self, hop):
+        v_arr = hop.inputarg(self, arg=0)
+        v_idx = hop.inputarg(lltype.Signed, arg=1)
+        v_result = hop.genop("getarrayitem", [v_arr, v_idx], resulttype=lltype.Signed)
+        v_addr = hop.genop("cast_int_to_adr", [v_result], resulttype=llmemory.Address)
+        return hop.genop("cast_adr_to_ptr", [v_addr], resulttype=hop.r_result.lowleveltype)
+
+    def rtype_method_setinstance(self, hop):
+        [v_arr, v_idx, v_instance] = hop.inputargs(self, lltype.Signed, hop.args_r[2])
+        v_addr = hop.genop("cast_ptr_to_adr", [v_instance], resulttype=llmemory.Address)
+        v_result = hop.genop("cast_adr_to_int", [v_addr, hop.inputconst(lltype.Void, "symbolic")], resulttype=lltype.Signed)
+        hop.genop("setarrayitem", [v_arr, v_idx, v_result])
+
     @classmethod
     def ll_new(cls, size):
         return lltype.malloc(cls.lowleveltype.TO, size)

pypy/rlib/test/test_rerased_raw.py

     storage = rerased_raw.UntypedStorage(1)
     storage.setinstance(0, A(4))
 
-    assert storage.getinstance(A, 0).value == 4
+    assert storage.getinstance(0, A).value == 4
 
 
 class TestRerasedRawLLType(LLRtypeMixin, BaseRtypingTest):
             return storage.getint(0)
 
         res = self.interpret(f, [4])
-        assert res == 4
+        assert res == 4
+
+    def test_instance(self):
+        class A(object):
+            def __init__(self, v):
+                self.v = v
+
+        def f(x):
+            storage = rerased_raw.UntypedStorage(1)
+            storage.setinstance(0, A(x))
+            return storage.getinstance(0, A).v
+
+        res = self.interpret(f, [27])
+        assert res == 27
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.