def test_primitive_be_display(): assert space.objtable["w_display"] is None mock_display = model.W_PointersObject(space, space.w_Point, 4) w_wordbmp = model.W_WordsObject(space, space.w_Array, 10) mock_display.store(space, 0, w_wordbmp) # bitmap mock_display.store(space, 1, space.wrap_int(32)) # width mock_display.store(space, 2, space.wrap_int(10)) # height mock_display.store(space, 3, space.wrap_int(1)) # depth prim(primitives.BE_DISPLAY, [mock_display]) assert space.objtable["w_display"] is mock_display w_bitmap = mock_display.fetch(space, 0) assert w_bitmap is not w_wordbmp assert isinstance(w_bitmap, model_display.W_DisplayBitmap) sdldisplay = w_bitmap.display assert isinstance(sdldisplay, display.SDLDisplay) mock_display2 = model.W_PointersObject(space, space.w_Point, 4) mock_display2.store(space, 0, model.W_WordsObject(space, space.w_Array, 10)) # bitmap mock_display2.store(space, 1, space.wrap_int(32)) # width mock_display2.store(space, 2, space.wrap_int(10)) # height mock_display2.store(space, 3, space.wrap_int(1)) # depth prim(primitives.BE_DISPLAY, [mock_display2]) assert space.objtable["w_display"] is mock_display2 w_bitmap2 = mock_display.fetch(space, 0) assert isinstance(w_bitmap2, model_display.W_DisplayBitmap) assert w_bitmap.display is w_bitmap2.display assert sdldisplay.width == 32 assert sdldisplay.height == 10 prim(primitives.BE_DISPLAY, [mock_display]) assert space.objtable["w_display"] is mock_display assert mock_display.fetch(space, 0) is w_bitmap
def initialize_methoddict(self): "NOT_RPYTHON" # this is only for testing. if self._s_methoddict is None: w_methoddict = model.W_PointersObject(self.space, None, 2) w_methoddict.store(self.space, constants.METHODDICT_VALUES_INDEX, model.W_PointersObject(self.space, None, 0)) self.store_s_methoddict( w_methoddict.as_methoddict_get_shadow(self.space))
def initialize_methoddict(self): "NOT_RPYTHON" # this is only for testing. if self._s_methoddict is None: w_methoddict = model.W_PointersObject(self.space, None, 2) w_methoddict._store(1, model.W_PointersObject(self.space, None, 0)) self._s_methoddict = w_methoddict.as_methoddict_get_shadow( self.space) self.s_methoddict().sync_cache() self.s_methoddict().invalid = False
def test_not_is_same_object(w_o1=model.W_PointersObject(space, None, 0), w_o2=model.W_PointersObject(space, None, 0)): assert not w_o1.is_same_object(w_o2) assert not w_o2.is_same_object(w_o1) w_o2 = model.W_SmallInteger(2) assert not w_o1.is_same_object(w_o2) assert not w_o2.is_same_object(w_o1) w_o2 = model.W_Float(5.5) assert not w_o1.is_same_object(w_o2) assert not w_o2.is_same_object(w_o1)
def make_form(bits, width, height, depth, o_x=0, o_y=0): w_f = model.W_PointersObject(space, space.w_Array, 5) w_f.store(space, 0, model.W_WordsObject(space, space.w_Array, len(bits))) w_f.fetch(space, 0).words = bits w_f.store(space, 1, w(width)) w_f.store(space, 2, w(height)) w_f.store(space, 3, w(depth)) w_f.store(space, 4, model.W_PointersObject(space, space.w_Point, 2)) w_f.fetch(space, 4).store(space, 0, w(o_x)) w_f.fetch(space, 4).store(space, 1, w(o_y)) return w_f
def test_not_is_same_object(w_o1=None, w_o2=None): if w_o1 is None: w_o1 = model.W_PointersObject(space, None, 0) if w_o2 is None: w_o2 = model.W_PointersObject(space, None, 0) assert not w_o1.is_same_object(w_o2) assert not w_o2.is_same_object(w_o1) w_o2 = model.W_SmallInteger(2) assert not w_o1.is_same_object(w_o2) assert not w_o2.is_same_object(w_o1) w_o2 = model.W_Float(5.5) assert not w_o1.is_same_object(w_o2) assert not w_o2.is_same_object(w_o1)
def test_contextOn_do_(): """ contextOn:do: is some very heavy meta programming. It creates and returns a separate stack frame, settings it's sender to nil, thereby manipulating the senders of two contexts. The Point in there should actually be UnhandledError or something. The test here is just that this works. ctx := ContextPart contextOn: Point do: ['nothing'] """ ContextPart = space.w_MethodContext.as_class_get_shadow( space).s_superclass().w_self() ContextPartClass = ContextPart.getclass(space).as_class_get_shadow( space).w_self() contextOnDo = find_symbol("contextOn:do:", ContextPartClass) bytes = [ 0x42, 0x43, # Push the classes 0x8f, 0x00, 0x00, 0x02, # Push block, 0x24, 0x7d, # in the block 0xf1, 0x81, 0xc0, 0x7c # Send contextOn:do: ] Association = space.classtable["w_Point"] # Wrong class, doesn't matter. ctxAssoc = model.W_PointersObject(space, Association, 2) ctxAssoc.store(space, 0, w('ctx')) ctxAssoc.store(space, 1, space.w_nil) contextPartAssoc = model.W_PointersObject(space, Association, 2) contextPartAssoc.store(space, 0, w('ContextPart')) contextPartAssoc.store(space, 1, ContextPart) errorAssoc = model.W_PointersObject(space, Association, 2) errorAssoc.store(space, 0, w('Point')) errorAssoc.store(space, 1, Association) w_method = space.make_method( bytes, [ctxAssoc, contextOnDo, contextPartAssoc, errorAssoc, w('nothing')]) result = interp.execute_method(w_method) assert isinstance(result, model.W_PointersObject) s = result.as_context_get_shadow(space) assert s.w_method().lookup_selector == "on:do:" assert s.w_method().primitive() == 199 assert s.s_sender() == None
def bootstrap_class(space, instsize, w_superclass=None, w_metaclass=None, name='?', format=shadow.POINTERS, varsized=False): from spyvm import model w_class = model.W_PointersObject(space, w_metaclass, 0) # a dummy placeholder for testing # XXX s = instantiate(shadow.ClassShadow) s.space = space s.version = shadow.Version() s._w_self = w_class s.subclass_s = {} s._s_superclass = None s.store_w_superclass(w_superclass) s.name = name s._instance_size = instsize s.instance_kind = format s._s_methoddict = None s.instance_varsized = varsized or format != shadow.POINTERS w_class.store_shadow(s) return w_class
def test_linked_list(): w_object = model.W_PointersObject(space, None, 2) w_last = link(space.w_nil) w_lb1 = link(w_last) w_lb2 = link(w_lb1) w_lb3 = link(w_lb2) w_lb4 = link(w_lb3) w_first = link(w_lb4) linkedlist = wrapper.LinkedListWrapper(space, w_object) linkedlist.store_first_link(w_first) linkedlist.store_last_link(w_last) assert w_first is linkedlist.first_link() assert w_last is linkedlist.last_link() assert linkedlist.remove_first_link_of_list() is w_first assert linkedlist.remove_first_link_of_list() is w_lb4 assert linkedlist.remove_first_link_of_list() is w_lb3 assert not linkedlist.is_empty_list() assert linkedlist.remove_first_link_of_list() is w_lb2 assert linkedlist.remove_first_link_of_list() is w_lb1 assert linkedlist.remove_first_link_of_list() is w_last assert linkedlist.is_empty_list() linkedlist.add_last_link(w_first) assert linkedlist.first_link() is w_first assert linkedlist.last_link() is w_first linkedlist.add_last_link(w_last) assert linkedlist.first_link() is w_first assert linkedlist.last_link() is w_last py.test.raises(WrapperException, linkedlist.remove, space.w_nil) linkedlist.remove(w_first) assert linkedlist.first_link() is w_last linkedlist.store_first_link(w_first) wrapper.LinkWrapper(space, w_first).store_next_link(w_last) linkedlist.remove(w_last) assert linkedlist.last_link() is w_first
def test_bitblt_copy_bits(monkeypatch): class CallCopyBitsSimulation(Exception): pass class Image(): def __init__(self): self.w_simulateCopyBits = "simulateCopyBits" mock_bitblt = model.W_PointersObject(space, space.w_Point, 15) mock_bitblt.store(space, 3, space.wrap_int(15)) # combination rule def perform_mock(w_selector, argcount, interp): if w_selector == "simulateCopyBits" or w_selector.as_string( ) == "simulateCopyBits": assert argcount == 0 raise CallCopyBitsSimulation def sync_cache_mock(self): raise CallCopyBitsSimulation interp, w_frame, argument_count = mock(space, [mock_bitblt], None) if interp.image is None: interp.image = Image() try: monkeypatch.setattr(w_frame.shadow, "_sendSelfSelector", perform_mock) monkeypatch.setattr(bitblt.BitBltShadow, "strategy_switched", sync_cache_mock) with py.test.raises(CallCopyBitsSimulation): prim_table[primitives.BITBLT_COPY_BITS]( interp, w_frame.as_context_get_shadow(space), argument_count - 1) finally: monkeypatch.undo() assert w_frame.shadow.pop() is mock_bitblt # the receiver
def test_is_same_object(w_o1=None, w_o2=None): if w_o1 is None: w_o1 = model.W_PointersObject(space, None, 0) if w_o2 is None: w_o2 = w_o1 assert w_o1.is_same_object(w_o2) assert w_o2.is_same_object(w_o1)
def methodcontext(w_sender=None, pc=13, stackpointer=0, stacksize=5, method=None): if w_sender is None: w_sender = space.w_nil if method is None: method = create_method() w_object = model.W_PointersObject( space, space.w_MethodContext, constants.MTHDCTX_TEMP_FRAME_START + method.tempsize() + stacksize) w_object.store(space, constants.CTXPART_SENDER_INDEX, w_sender) w_object.store(space, constants.CTXPART_PC_INDEX, space.wrap_int(pc)) w_object.store(space, constants.CTXPART_STACKP_INDEX, space.wrap_int(method.tempsize() + stackpointer)) w_object.store(space, constants.MTHDCTX_METHOD, method) # XXX w_object.store(space, constants.MTHDCTX_CLOSURE_OR_NIL, space.w_nil) w_object.store(space, constants.MTHDCTX_RECEIVER, space.wrap_string('receiver')) w_object.store(space, constants.MTHDCTX_TEMP_FRAME_START, space.wrap_string('el')) return w_object
def test_primitive_force_display_update(monkeypatch): # XXX: Patch SDLDisplay -> get_pixelbuffer() to circumvent # double-free bug def get_pixelbuffer(self): from rpython.rtyper.lltypesystem import lltype, rffi return lltype.malloc(rffi.ULONGP.TO, self.width * self.height * 32, flavor='raw') display.SDLDisplay.get_pixelbuffer = get_pixelbuffer mock_display = model.W_PointersObject(space, space.w_Point, 4) w_wordbmp = model.W_WordsObject(space, space.w_Array, 10) mock_display.store(space, 0, w_wordbmp) # bitmap mock_display.store(space, 1, space.wrap_int(32)) # width mock_display.store(space, 2, space.wrap_int(10)) # height mock_display.store(space, 3, space.wrap_int(1)) # depth prim(primitives.BE_DISPLAY, [mock_display]) class DisplayFlush(Exception): pass def flush_to_screen_mock(self, force=False): raise DisplayFlush try: monkeypatch.setattr(space.get_display().__class__, "flip", flush_to_screen_mock) with py.test.raises(DisplayFlush): prim(primitives.FORCE_DISPLAY_UPDATE, [mock_display]) finally: monkeypatch.undo()
def test_accessor_generators(): w_o = model.W_PointersObject(space, None, 1) w = wrapper.LinkWrapper(space, w_o) w_o._vars[0] = "hello" assert w.next_link() == "hello" w.store_next_link("boe") assert w.next_link() == "boe"
def new_scheduler(w_process=space.w_nil, prioritydict=None): priority_list = new_prioritylist(prioritydict) w_scheduler = model.W_PointersObject(space, None, 2) scheduler = wrapper.SchedulerWrapper(space, w_scheduler) scheduler.store_active_process(w_process) scheduler.write(0, priority_list._w_self) return scheduler
def make_bootstrap_objects(self): def bld_char(i): w_cinst = self.w_Character.as_class_get_shadow(self).new() w_cinst.store(self, constants.CHARACTER_VALUE_INDEX, model.W_SmallInteger(i)) return w_cinst w_charactertable = model.W_PointersObject(self, self.classtable['w_Array'], 256) self.w_charactertable = w_charactertable for i in range(256): self.w_charactertable.atput0(self, i, bld_char(i)) # Very special nil hack: in order to allow W_PointersObject's to # initialize their fields to nil, we have to create it in the model # package, and then patch up its fields here: def patch_nil(w_nil): from spyvm.fieldtypes import nilTyper w_nil.space = self w_nil.fieldtypes = nilTyper w_nil.s_class = self.classtable[ 'w_UndefinedObject'].as_class_get_penumbra(self) return w_nil w_nil = self.w_nil = patch_nil(model.w_nil) w_true = self.classtable['w_True'].as_class_get_shadow(self).new() self.w_true = w_true w_false = self.classtable['w_False'].as_class_get_shadow(self).new() self.w_false = w_false self.w_minus_one = model.W_SmallInteger(-1) self.w_zero = model.W_SmallInteger(0) self.w_one = model.W_SmallInteger(1) self.w_two = model.W_SmallInteger(2) w_special_selectors = model.W_PointersObject( self, self.classtable['w_Array'], len(constants.SPECIAL_SELECTORS) * 2) self.w_special_selectors = w_special_selectors self.objtable = {} for name in constants.objects_in_special_object_table: name = "w_" + name try: self.objtable[name] = locals()[name] except KeyError, e: self.objtable[name] = None
def test_simpleread(): w_o = model.W_PointersObject(space, None, 2) w = wrapper.Wrapper(space, w_o) w_o._vars[0] = "hello" assert w.read(0) == "hello" w.write(1, "b") assert w.read(1) == "b" py.test.raises(WrapperException, "w.read(2)") py.test.raises(WrapperException, "w.write(2, \"test\")")
def test_bitBlt_values(): w_bb = model.W_PointersObject(space, space.w_Array, 15) w_bb.store(space, 0, make_form([0] * 1230 * 20, 1230, 20, 1)) w_bb.store(space, 1, w_bb.fetch(space, 0)) w_bb.store(space, 2, space.w_nil) w_bb.store(space, 3, w(7)) # combination rule w_bb.store(space, 4, w(1)) # dest x w_bb.store(space, 5, w(0)) # dest y w_bb.store(space, 6, w(1220)) # width w_bb.store(space, 7, w(15)) # height w_bb.store(space, 8, w(0)) # source x w_bb.store(space, 9, w(0)) # source y w_bb.store(space, 10, w(0)) # clip x w_bb.store(space, 11, w(0)) # clip y w_bb.store(space, 12, w(1220)) # clip width w_bb.store(space, 13, w(15)) # clip height w_bb.store(space, 14, model.W_PointersObject(space, space.w_Array, 5)) # color map s_bb = w_bb.as_special_get_shadow(space, bitblt.BitBltShadow) s_bb.loadBitBlt() s_bb.clipRange() assert not (s_bb.width <= 0 or s_bb.height <= 0) s_bb.destMaskAndPointerInit() s_bb.checkSourceOverlap() s_bb.sourceSkewAndPointerInit() assert s_bb.destX == 1 assert s_bb.destY == 0 assert s_bb.sourceX == 0 assert s_bb.sourceY == 0 assert s_bb.destX == 1 assert s_bb.destY == 0 assert s_bb.width == 1220 assert s_bb.height == 15 assert s_bb.hDir == -1 assert s_bb.vDir == 1 assert s_bb.sourceDelta == 79 assert s_bb.destDelta == 78 assert s_bb.skew == 31 assert s_bb.sourceIndex == 38 assert s_bb.destIndex == 38
def w_self(self): if self._w_self is not None: return self._w_self else: space = self.space w_self = model.W_PointersObject(space, space.w_MethodContext, self._w_self_size) w_self.store_strategy(self) self._w_self = w_self return w_self
def new_frame(bytes, receiver=space.w_nil, space=space): assert isinstance(bytes, str) w_method = model.W_CompiledMethod(len(bytes)) w_method.islarge = 1 w_method.bytes = bytes w_method.argsize = 2 w_method.tempsize = 8 w_method.setliterals([model.W_PointersObject(space, None, 2)]) s_frame = w_method.as_compiledmethod_get_shadow(space).create_frame( space, receiver, ["foo", "bar"]) return s_frame.w_self(), s_frame
def bootstrap_class(self, instsize, w_superclass=None, w_metaclass=None, name='?', format=storage_classes.POINTERS, varsized=False): w_class = model.W_PointersObject(self, w_metaclass, 0) self.patch_class(w_class, instsize, w_superclass, w_metaclass, name, format, varsized) return w_class
def blockcontext(w_sender=space.w_nil, pc=13, stackpointer=1, stacksize=5, home=methodcontext()): w_object = model.W_PointersObject(space, space.w_MethodContext, constants.MTHDCTX_TEMP_FRAME_START+stacksize) w_object.store(space, constants.CTXPART_SENDER_INDEX, w_sender) w_object.store(space, constants.CTXPART_PC_INDEX, space.wrap_int(pc)) w_object.store(space, constants.CTXPART_STACKP_INDEX, space.wrap_int(stackpointer)) w_object.store(space, constants.BLKCTX_BLOCK_ARGUMENT_COUNT_INDEX, space.wrap_int(54)) w_object.store(space, constants.BLKCTX_INITIAL_IP_INDEX, space.wrap_int(17)) w_object.store(space, constants.BLKCTX_HOME_INDEX, home) w_object.store(space, constants.BLKCTX_STACK_START, space.wrap_string('el')) return w_object
def new_prioritylist(prioritydict=None): if prioritydict is not None: maxpriority = max(prioritydict.keys()) else: maxpriority = 5 prioritydict = {} w_prioritylist = model.W_PointersObject(space, None, maxpriority) prioritylist = wrapper.Wrapper(space, w_prioritylist) for i in range(maxpriority): prioritylist.write(i, new_processlist(prioritydict.get(i, []))._w_self) return prioritylist
def new_processlist(processes_w=[]): w_processlist = model.W_PointersObject(space, None, 2) w_first = space.w_nil w_last = space.w_nil for w_process in processes_w[::-1]: w_first = newprocess(w_first, w_processlist)._w_self if w_last is space.w_nil: w_last = w_first pl = wrapper.ProcessListWrapper(space, w_processlist) pl.store_first_link(w_first) pl.store_last_link(w_last) return pl
def new_process(w_next=space.w_nil, w_my_list=space.w_nil, w_suspended_context=space.w_nil, priority=0): w_priority = space.wrap_int(priority) w_process = model.W_PointersObject(space, None, 4) process = wrapper.ProcessWrapper(space, w_process) process.store_next_link(w_next) process.store_my_list(w_my_list) process.store_suspended_context(w_suspended_context) process.write(2, w_priority) return process
def make_method(self, bytes, literals=None, numargs=0): if not isinstance(bytes, str): bytes = "".join([chr(x) for x in bytes]) w_method = model.W_CompiledMethod(self, len(bytes)) w_method.islarge = 1 w_method.bytes = bytes w_method.argsize = numargs w_method._tempsize = 8 if literals is None: literals = [model.W_PointersObject(self, None, 2)] w_method.setliterals(literals) return w_method
def build_block_context(space, s_home, argcnt, pc): size = s_home.size() - s_home.tempsize() w_self = model.W_PointersObject(space, space.w_BlockContext, size) ctx = ContextPartShadow(space, w_self, size, is_block_context=True) ctx.store_expected_argument_count(argcnt) ctx.store_w_home(s_home.w_self()) ctx.store_initialip(pc) ctx.store_pc(pc) w_self.store_shadow(ctx) ctx.init_stack_ptr() return ctx
def test_primitive_be_display(): # XXX: Patch SDLDisplay -> get_pixelbuffer() to circumvent # double-free bug def get_pixelbuffer(self): from rpython.rtyper.lltypesystem import lltype, rffi return lltype.malloc(rffi.ULONGP.TO, self.width * self.height * 32, flavor='raw') display.SDLDisplay.get_pixelbuffer = get_pixelbuffer assert space.objtable["w_display"] is None mock_display = model.W_PointersObject(space, space.w_Point, 4) w_wordbmp = model.W_WordsObject(space, space.w_Array, 10) mock_display.store(space, 0, w_wordbmp) # bitmap mock_display.store(space, 1, space.wrap_int(32)) # width mock_display.store(space, 2, space.wrap_int(10)) # height mock_display.store(space, 3, space.wrap_int(1)) # depth prim(primitives.BE_DISPLAY, [mock_display]) assert space.objtable["w_display"] is mock_display w_bitmap = mock_display.fetch(space, 0) assert w_bitmap is not w_wordbmp assert isinstance(w_bitmap, model.W_DisplayBitmap) sdldisplay = w_bitmap.display assert isinstance(sdldisplay, display.SDLDisplay) mock_display2 = model.W_PointersObject(space, space.w_Point, 4) mock_display2.store(space, 0, model.W_WordsObject(space, space.w_Array, 10)) # bitmap mock_display2.store(space, 1, space.wrap_int(32)) # width mock_display2.store(space, 2, space.wrap_int(10)) # height mock_display2.store(space, 3, space.wrap_int(1)) # depth prim(primitives.BE_DISPLAY, [mock_display2]) assert space.objtable["w_display"] is mock_display2 w_bitmap2 = mock_display.fetch(space, 0) assert isinstance(w_bitmap2, model.W_DisplayBitmap) assert w_bitmap.display is w_bitmap2.display assert sdldisplay.width == 32 assert sdldisplay.height == 10 prim(primitives.BE_DISPLAY, [mock_display]) assert space.objtable["w_display"] is mock_display assert mock_display.fetch(space, 0) is w_bitmap
def test_ContextPart_jump(): """ Code: Create a Block context that jumps back to its sender, instead of returning normally. The Block is not executed to the end, the sender chain is manipulated. The local variable should be the value pushed on the sender context before jumping to it. a := 5. a := [ thisContext sender push: 2. thisContext sender jump. 10 ] value. ^ a """ ContextPart = space.w_MethodContext.as_class_get_shadow( space).s_superclass().w_self() push = find_symbol("push:", ContextPart) sender = find_symbol("sender", ContextPart) jump = find_symbol("jump", ContextPart) bytes = [ 0x21, 0x82, 0xc0, # Set a 0x8f, 0x00, 0x00, 0x0b, # Push block 0x89, 0xd3, # Send sender 0x77, 0xe2, # Send push 0x87, 0x89, 0xd3, 0xd4, # Send jump 0x87, 0x25, 0x7d, # Block rest (not executed) 0xc9, 0x82, 0xc0, 0x40, 0x7c ] # Send value and return Association = space.classtable["w_Point"] # Wrong class, doesn't matter. assoc = model.W_PointersObject(space, Association, 2) assoc.store(space, 0, w('a')) assoc.store(space, 1, w(3)) w_method = space.make_method( bytes, [assoc, w(5), push, sender, jump, w(10)]) result = interp.execute_method(w_method) assert isinstance(result, model.W_SmallInteger) assert result.value == 2
def new(self, extrasize=0): w_cls = self.w_self() if self.instance_kind == POINTERS: size = self.instsize() + extrasize w_new = model.W_PointersObject(self.space, w_cls, size) elif self.instance_kind == WORDS: w_new = model.W_WordsObject(self.space, w_cls, extrasize) elif self.instance_kind == BYTES: w_new = model.W_BytesObject(self.space, w_cls, extrasize) elif self.instance_kind == COMPILED_METHOD: w_new = model.W_CompiledMethod(self.space, extrasize) elif self.instance_kind == FLOAT: w_new = model.W_Float(0) # Squeak gives a random piece of memory elif self.instance_kind == LARGE_POSITIVE_INTEGER: if extrasize <= 4: w_new = model.W_LargePositiveInteger1Word(0, extrasize) else: w_new = model.W_BytesObject(self.space, w_cls, extrasize) elif self.instance_kind == WEAK_POINTERS: size = self.instsize() + extrasize w_new = model.W_PointersObject(self.space, w_cls, size, weak=True) else: raise NotImplementedError(self.instance_kind) return w_new