Пример #1
0
 def assert_w_object_type(format,
                          expected_type,
                          length=0,
                          classid=0,
                          body=""):
     objbytes = ints2str(joinbits([0, 0, length], [22, 2, 8]),
                         joinbits([classid, 0, format, 0],
                                  [22, 2, 5, 3])) + body
     r = imagereader_mock(SPUR_VERSION_HEADER + objbytes)
     stream = r.stream
     r.read_version()
     monkeypatch.setattr(r.readerStrategy, 'g_class_of',
                         lambda chunk: g_class_mock)
     stream.reset_count()
     chunk, pos = r.readerStrategy.read_object()
     g_object = squeakimage.GenericObject()
     g_object.initialize(chunk, r.readerStrategy, r.space)
     w_object = g_object.init_w_object(r.space)
     g_object.fillin(r.space)
     g_object.fillin_weak(r.space)
     assert w_object is g_object.w_object
     assert isinstance(w_object, expected_type)
     return w_object, r.space
Пример #2
0
 def assert_w_object_type(format,
                          expected_type,
                          length=0,
                          compact_class_index=0,
                          body="",
                          assert_is_weak=False):
     objbytes = ints2str(
         joinbits([3, length + 1, format, compact_class_index, 0],
                  [2, 6, 4, 5, 12])) + body
     r = imagereader_mock(SIMPLE_VERSION_HEADER + objbytes)
     r.read_version()
     monkeypatch.setattr(r.readerStrategy, 'g_class_of',
                         lambda chunk: g_class_mock)
     chunk, pos = r.readerStrategy.read_object()
     g_object = squeakimage.GenericObject()
     g_object.initialize(chunk, r.readerStrategy, r.space)
     w_object = g_object.init_w_object(r.space)
     g_object.fillin(r.space)
     g_object.fillin_weak(r.space)
     assert w_object is g_object.w_object
     assert isinstance(w_object, expected_type)
     if assert_is_weak:
         assert w_object.is_weak()
     return w_object, r.space
Пример #3
0
 def fun():
     rstrat = f()
     rstrat.special_g_objects = [squeakimage.GenericObject()]
     return rstrat
Пример #4
0
def test_object_format_spur(monkeypatch):
    g_class_mock = squeakimage.GenericObject()
    from rpython.rlib import objectmodel
    from rsqueakvm.storage_classes import ClassShadow
    w_class_mock = objectmodel.instantiate(W_PointersObject)
    w_class_mock.strategy = ClassShadow(None, w_class_mock, 3, None)
    w_class_mock.strategy._instance_size = 0
    g_class_mock.w_object = w_class_mock

    def assert_w_object_type(format,
                             expected_type,
                             length=0,
                             classid=0,
                             body=""):
        objbytes = ints2str(joinbits([0, 0, length], [22, 2, 8]),
                            joinbits([classid, 0, format, 0],
                                     [22, 2, 5, 3])) + body
        r = imagereader_mock(SPUR_VERSION_HEADER + objbytes)
        stream = r.stream
        r.read_version()
        monkeypatch.setattr(r.readerStrategy, 'g_class_of',
                            lambda chunk: g_class_mock)
        stream.reset_count()
        chunk, pos = r.readerStrategy.read_object()
        g_object = squeakimage.GenericObject()
        g_object.initialize(chunk, r.readerStrategy, r.space)
        w_object = g_object.init_w_object(r.space)
        g_object.fillin(r.space)
        g_object.fillin_weak(r.space)
        assert w_object is g_object.w_object
        assert isinstance(w_object, expected_type)
        return w_object, r.space

    # 0 zero sized object
    w_obj, space = assert_w_object_type(0,
                                        W_PointersObject,
                                        body=("\x00" * 3 + "\x01") * 2)
    # 1 fixed-size object with inst-vars
    body_1_to_9 = ints2str(*(joinbits([1, n], [1, 31]) for n in range(1, 10)))
    w_obj, space = assert_w_object_type(1,
                                        W_PointersObject,
                                        length=2,
                                        body=body_1_to_9)
    assert w_obj.size() == 2
    assert w_obj.fetch(space, 0) == space.wrap_int(1)
    assert w_obj.fetch(space, 1) == space.wrap_int(2)
    # 2 variable sized object without inst vars
    w_obj, space = assert_w_object_type(2,
                                        W_PointersObject,
                                        length=2,
                                        body=body_1_to_9)
    assert w_obj.size() == 2
    assert w_obj.fetch(space, 0) == space.wrap_int(1)
    assert w_obj.fetch(space, 1) == space.wrap_int(2)
    # 3 variable sized object with inst vars
    w_obj, space = assert_w_object_type(3,
                                        W_PointersObject,
                                        length=2,
                                        body=body_1_to_9)
    assert w_obj.size() == 2
    assert w_obj.fetch(space, 0) == space.wrap_int(1)
    assert w_obj.fetch(space, 1) == space.wrap_int(2)
    # 4 weak variable sized object with inst vars
    w_obj, space = assert_w_object_type(4,
                                        W_PointersObject,
                                        length=2,
                                        body=body_1_to_9)
    assert w_obj.is_weak()
    assert w_obj.size() == 2
    assert w_obj.fetch(space, 0) == space.wrap_int(1)
    assert w_obj.fetch(space, 1) == space.wrap_int(2)
    # 5 weak fixed sized object with inst vars (Ephemeron)
    w_obj, space = assert_w_object_type(5,
                                        W_PointersObject,
                                        length=2,
                                        body=body_1_to_9)
    assert w_obj.is_weak()
    assert w_obj.size() == 2
    assert w_obj.fetch(space, 0) == space.wrap_int(1)
    assert w_obj.fetch(space, 1) == space.wrap_int(2)
    # 6 unused
    # 7 forwarding object, does not occur in images
    # 8 unused
    # 9 64 bit indexables
    w_obj, space = assert_w_object_type(9,
                                        W_WordsObject,
                                        length=2,
                                        body=body_1_to_9)
    # 64-bit not supported yet
    # assert w_obj.getlong(space, 0) == chrs2long(body_1_to_9[:8])
    # 10-11 32 bit indexables (11 unused in 32 bits)
    w_obj, space = assert_w_object_type(10,
                                        W_WordsObject,
                                        length=2,
                                        body=body_1_to_9)
    assert w_obj.size() == 2
    assert w_obj.getword(0) == chrs2int(body_1_to_9[:4])
    assert w_obj.getword(1) == chrs2int(body_1_to_9[4:8])
    # TODO: add tests for correct reading of 32 bit indexables with trailing slots (11)
    # 12-15 16 bit indexables (14,15 unused in 32 bits)
    w_obj, space = assert_w_object_type(12,
                                        W_WordsObject,
                                        length=2,
                                        body=body_1_to_9)
    # XXX assert w_obj.size() == 4
    # XXX assert w_obj.getword(0) == chrs2int("\x00\x00" + body_1_to_9[:2])
    # XXX assert w_obj.getword(1) == chrs2int("\x00\x00" + body_1_to_9[2:4])
    # TODO: add tests for correct reading of 16 bit indexables with trailing slots (13-15)
    # 16-23 8 bit indexables (20-23 unused in 32 bits)
    w_obj, space = assert_w_object_type(18,
                                        W_BytesObject,
                                        length=2,
                                        body=body_1_to_9)
    assert w_obj.size() == 6
    assert space.unwrap_string(w_obj) == body_1_to_9[:6]
    # TODO: add tests for correct reading of 8 bit indexables with trailing slots (17-23)
    # 24-31 compiled methods (28-31 unused in 32 bits)
    literals = [chrs2int(body_1_to_9[:4])]
    bytecodes = "\x00\x01\x02\x03" + "\x04\x05\x06\x07"
    w_obj, space = assert_w_object_type(
        24,
        W_CompiledMethod,
        length=1 + 1 + 1,
        body=ints2str(
            joinbits([1, 0, 0, 3, 2, 0, 1], [16, 1, 1, 6, 4, 2, 1]) << 1 | 1, *
            literals) + bytecodes)
    assert w_obj.literals[0] == space.wrap_int(1)
    assert w_obj.bytes == list("\x00\x01\x02\x03")
Пример #5
0
def test_object_format_v3(monkeypatch):
    g_class_mock = squeakimage.GenericObject()
    from rpython.rlib import objectmodel
    from rsqueakvm.storage_classes import ClassShadow
    w_class_mock = objectmodel.instantiate(W_PointersObject)
    w_class_mock.strategy = ClassShadow(None, w_class_mock, 3, None)
    w_class_mock.strategy._instance_size = 0
    g_class_mock.w_object = w_class_mock

    def assert_w_object_type(format,
                             expected_type,
                             length=0,
                             compact_class_index=0,
                             body="",
                             assert_is_weak=False):
        objbytes = ints2str(
            joinbits([3, length + 1, format, compact_class_index, 0],
                     [2, 6, 4, 5, 12])) + body
        r = imagereader_mock(SIMPLE_VERSION_HEADER + objbytes)
        r.read_version()
        monkeypatch.setattr(r.readerStrategy, 'g_class_of',
                            lambda chunk: g_class_mock)
        chunk, pos = r.readerStrategy.read_object()
        g_object = squeakimage.GenericObject()
        g_object.initialize(chunk, r.readerStrategy, r.space)
        w_object = g_object.init_w_object(r.space)
        g_object.fillin(r.space)
        g_object.fillin_weak(r.space)
        assert w_object is g_object.w_object
        assert isinstance(w_object, expected_type)
        if assert_is_weak:
            assert w_object.is_weak()
        return w_object, r.space

    """ 0      no fields
        1      fixed fields only (all containing pointers)
        2      indexable fields only (all containing pointers)
        3      both fixed and indexable fields (all containing pointers)
        4      both fixed and indexable weak fields (all containing pointers).

        5      unused
        6      indexable word fields only (no pointers)
        7      indexable long (64-bit) fields (only in 64-bit images)

     8-11      indexable byte fields only (no pointers) (low 2 bits are low 2 bits of size)
    12-15     compiled methods:
                   # of literal oops specified in method header,
                   followed by indexable bytes (same interpretation of low 2 bits as above)
    """
    w_obj, _ = assert_w_object_type(0, W_PointersObject)
    assert w_obj.size() == 0
    body_42_and_1 = ints2str(joinbits([1, 42], [1, 31]),
                             joinbits([1, 1], [1, 31]))
    w_obj, space = assert_w_object_type(1,
                                        W_PointersObject,
                                        length=2,
                                        body=body_42_and_1)
    assert w_obj.size() == 2
    assert space.unwrap_int(w_obj.fetch(space, 0)) == 42
    assert space.unwrap_int(w_obj.fetch(space, 1)) == 1
    w_obj, space = assert_w_object_type(2,
                                        W_PointersObject,
                                        length=2,
                                        body=body_42_and_1)
    assert w_obj.size() == 2
    assert space.unwrap_int(w_obj.fetch(space, 0)) == 42
    assert space.unwrap_int(w_obj.fetch(space, 1)) == 1
    assert_w_object_type(3, W_PointersObject, length=2, body=body_42_and_1)
    w_obj, _ = assert_w_object_type(4,
                                    W_PointersObject,
                                    length=2,
                                    body=body_42_and_1)
    assert w_obj.is_weak()
    assert w_obj.size() == 2
    assert space.unwrap_int(w_obj.fetch(space, 0)) == 42
    assert space.unwrap_int(w_obj.fetch(space, 1)) == 1
    w_obj, space = assert_w_object_type(6,
                                        W_WordsObject,
                                        length=2,
                                        body=body_42_and_1)
    assert w_obj.size() == 2
    assert w_obj.getword(0) == 42 << 1 | 1
    assert w_obj.getword(1) == 1 << 1 | 1
    w_obj, space = assert_w_object_type(8,
                                        W_BytesObject,
                                        length=2,
                                        body=body_42_and_1)
    assert w_obj.size() == 8  # 2 * 32 bit == 8 * 8 bit
    assert space.unwrap_string(w_obj) == body_42_and_1
    w_obj, space = assert_w_object_type(12,
                                        W_CompiledMethod,
                                        length=2,
                                        body=body_42_and_1)
    assert w_obj.size() == 8