def test_singleExtendedSuperBytecode(bytecode=singleExtendedSuperBytecode + chr((0<<5) + 0)): w_supersuper = mockclass(space, 0) w_super = mockclass(space, 0, w_superclass=w_supersuper) w_class = mockclass(space, 0, w_superclass=w_super) w_object = w_class.as_class_get_shadow(space).new() # first call method installed in w_class bytecodes = singleExtendedSendBytecode + chr(0) # which does a call to its super meth1 = model.W_CompiledMethod(2) meth1.bytes = pushReceiverBytecode + bytecode w_class.as_class_get_shadow(space).installmethod("foo", meth1) # and that one again to its super meth2 = model.W_CompiledMethod(2) meth2.bytes = pushReceiverBytecode + bytecode w_super.as_class_get_shadow(space).installmethod("foo", meth2) meth3 = model.W_CompiledMethod(0) w_supersuper.as_class_get_shadow(space).installmethod("foo", meth3) meth1.literals = fakeliterals(space, "foo") meth2.literals = fakeliterals(space, "foo") interp = new_interpreter(bytecodes) interp.w_active_context().as_methodcontext_get_shadow(space).w_method().literals = fakeliterals(space, "foo") interp.s_active_context().push(w_object) interp.step() for w_specificclass in [w_super, w_supersuper]: callerContext = interp.w_active_context() interp.step() interp.step() assert interp.s_active_context().w_sender() == callerContext assert interp.s_active_context().stack() == [] assert interp.w_active_context().as_methodcontext_get_shadow(space).w_receiver() == w_object meth = w_specificclass.as_class_get_shadow(space).s_methoddict().methoddict["foo"] assert interp.w_active_context().as_methodcontext_get_shadow(space).w_method() == meth assert callerContext.as_context_get_shadow(space).stack() == []
def test_methoddict(): methods = { 'foo': model.W_CompiledMethod(0), 'bar': model.W_CompiledMethod(0) } w_class = build_smalltalk_class("Demo", 0x90, methods=methods) classshadow = w_class.as_class_get_shadow(space) assert classshadow.s_methoddict().methoddict == methods
def new_interpreter(bytes): assert isinstance(bytes, str) w_method = model.W_CompiledMethod(0, bytes=bytes) w_frame = w_method.create_frame(objtable.w_nil, []) interp = interpreter.Interpreter() interp.w_active_context = w_frame return interp
def sendBytecodesTest(w_class, w_object, bytecodes): for bytecode, result in [(returnReceiver, w_object), (returnTrue, interpreter.Interpreter.TRUE), (returnFalse, interpreter.Interpreter.FALSE), (returnNil, interpreter.Interpreter.NIL), (returnTopFromMethod, interpreter.Interpreter.ONE) ]: shadow = w_class.as_class_get_shadow() w_method = model.W_CompiledMethod(2) w_method.bytes = pushConstantOneBytecode + bytecode shadow.installmethod("foo", w_method) interp = new_interpreter(bytecodes) interp.w_active_context.w_method().literals = fakeliterals("foo") interp.w_active_context.push(w_object) callerContext = interp.w_active_context interp.step() assert interp.w_active_context.w_sender == callerContext assert interp.w_active_context.stack == [] assert interp.w_active_context.w_receiver == w_object assert interp.w_active_context.w_method() == shadow.methoddict["foo"] assert callerContext.stack == [] interp.step() interp.step() assert interp.w_active_context == callerContext assert interp.w_active_context.stack == [result]
def method(tempsize=3, argsize=2, bytes="abcde"): w_m = model.W_CompiledMethod() w_m.bytes = bytes w_m.tempsize = tempsize w_m.argsize = argsize w_m.literalsize = 2 return w_m
def test_bc_objectAtAndAtPut(): # ^ self objectAt: 1. yields the method header # ^ self objectAt: 2. yields the first literal (22) # ^ self objectAt: 2 put: 3. changes the first literal to 3 # ^ self objectAt: 2. yields the new first literal (3) py.test.skip("Broken, fix me") prim_meth = model.W_CompiledMethod(0) prim_meth.literals = fakeliterals(22) mhs = fakesymbol("methodheader") oal = fakeliterals("objectAt:") oalp = fakeliterals("objectAt:put:", 3) def test(): assert interpret_bc([112, 118, 224, 124], oal, receiver=prim_meth) == mhs assert interpret_bc([112, 119, 224, 124], oal, receiver=prim_meth).value == 22 assert interpret_bc([112, 119, 33, 240, 124], oalp, receiver=prim_meth).value == 3 assert interpret_bc([112, 119, 224, 124], oal, receiver=prim_meth).value == 3 run_with_faked_methods( [[classtable.w_CompiledMethod, primitives.OBJECT_AT, 1, "objectAt:"], [ classtable.w_CompiledMethod, primitives.OBJECT_AT_PUT, 2, "objectAt:put:" ]], test)
def test_w_compiledin(): w_super = mockclass(0) w_class = mockclass(0, w_superclass=w_super) supershadow = w_super.as_class_get_shadow() supershadow.installmethod("foo", model.W_CompiledMethod(0)) classshadow = w_class.as_class_get_shadow() assert classshadow.lookup("foo").w_compiledin is w_super
def test_size_of_compiled_method(): literalsize = 3 bytecount = 3 w_cm = model.W_CompiledMethod(bytecount) w_cm.literalsize = literalsize assert prim( primitives.SIZE, [w_cm ]).value == (literalsize + 1) * constants.BYTES_PER_WORD + bytecount
def new_interpreter(bytes, receiver=objtable.w_nil): assert isinstance(bytes, str) w_method = model.W_CompiledMethod(len(bytes)) w_method.bytes = bytes w_method.argsize = 2 w_method.tempsize = 1 w_frame = w_method.create_frame(receiver, ["foo", "bar"]) interp = interpreter.Interpreter() interp.w_active_context = w_frame return interp
def build(): bytecode = ''.join(map(chr, [ 16, 119, 178, 154, 118, 164, 11, 112, 16, 118, 177, 224, 112, 16, 119, 177, 224, 176, 124 ])) shadow = mockclass(0).as_class_get_shadow() method = model.W_CompiledMethod(1, bytecode, 1) method.literals = fakeliterals("fib:") shadow.installmethod("fib:", method) w_object = shadow.new() interp = new_interpreter(sendLiteralSelectorBytecode(16) + returnTopFromMethod) interp.w_active_context.w_method().literals = fakeliterals("fib:") return interp, w_object
def new_interpreter(bytes, receiver=space.w_nil): 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_frame = w_method.create_frame(space, receiver, ["foo", "bar"]) interp = interpreter.Interpreter(space) interp.store_w_active_context(w_frame) return interp
def test_compiledmethod_at0(): w_method = model.W_CompiledMethod() w_method.bytes = "abc" w_method.header = 100 w_method.literals = [ 'lit1', 'lit2' ] w_method.literalsize = 2 assert space.unwrap_int(w_method.at0(space, 0)) == 100 assert w_method.at0(space, 4) == 'lit1' assert w_method.at0(space, 8) == 'lit2' assert space.unwrap_int(w_method.at0(space, 12)) == ord('a') assert space.unwrap_int(w_method.at0(space, 13)) == ord('b') assert space.unwrap_int(w_method.at0(space, 14)) == ord('c')
def new(self, extrasize=0): w_cls = self.w_self() if self.instance_kind == POINTERS: w_new = model.W_PointersObject(w_cls, self.instance_size + extrasize) elif self.instance_kind == WORDS: w_new = model.W_WordsObject(w_cls, extrasize) elif self.instance_kind == BYTES: w_new = model.W_BytesObject(w_cls, extrasize) elif self.instance_kind == COMPILED_METHOD: w_new = model.W_CompiledMethod(extrasize) else: raise NotImplementedError(self.instance_kind) return w_new
def test_create_frame(): w_method = model.W_CompiledMethod(len("hello")) w_method.bytes = "hello" w_method.argsize = 2 w_method.tempsize = 1 w_frame = w_method.create_frame("receiver", ["foo", "bar"]) assert w_frame.w_receiver == "receiver" assert w_frame.gettemp(0) == "foo" assert w_frame.gettemp(1) == "bar" assert w_frame.gettemp(2) is objtable.w_nil w_frame.settemp(2, "spam") assert w_frame.gettemp(2) == "spam" assert w_frame.getNextBytecode() == ord("h") assert w_frame.getNextBytecode() == ord("e") assert w_frame.getNextBytecode() == ord("l")
def test_callPrimitiveAndPush_fallback(): interp = new_interpreter(bytecodePrimAdd) shadow = mockclass(0).as_class_get_shadow() w_method = model.W_CompiledMethod(0) w_method.argsize = 1 w_method.literalsize = 1 shadow.installmethod("+", w_method) w_object = shadow.new() interp.w_active_context.push(w_object) interp.w_active_context.push(interp.ONE) interp.step() assert interp.w_active_context.w_method() == shadow.methoddict["+"] assert interp.w_active_context.w_receiver is w_object assert interp.w_active_context.gettemp(0) == interp.ONE assert interp.w_active_context.stack == []
def test_create_frame(): w_method = model.W_CompiledMethod(len("hello")) w_method.bytes="hello" w_method.islarge = 1 w_method.argsize=2 w_method.tempsize=8 w_frame = w_method.create_frame(space, "receiver", ["foo", "bar"]) s_frame = w_frame.as_context_get_shadow(space) assert s_frame.w_receiver() == "receiver" assert s_frame.gettemp(0) == "foo" assert s_frame.gettemp(1) == "bar" assert s_frame.gettemp(2) is space.w_nil s_frame.settemp(2, "spam") assert s_frame.gettemp(2) == "spam" assert s_frame.getNextBytecode() == ord("h") assert s_frame.getNextBytecode() == ord("e") assert s_frame.getNextBytecode() == ord("l")
def test_callPrimitiveAndPush_fallback(): interp = new_interpreter(bytecodePrimAdd) shadow = mockclass(space, 0).as_class_get_shadow(space) w_method = model.W_CompiledMethod(0) w_method.argsize = 1 w_method.tempsize = 1 w_method.literalsize = 1 shadow.installmethod("+", w_method) w_object = shadow.new() interp.s_active_context().push(w_object) interp.s_active_context().push(space.w_one) interp.step() assert interp.w_active_context().as_methodcontext_get_shadow(space).w_method() == shadow.s_methoddict().methoddict["+"] assert interp.s_active_context().w_receiver() is w_object assert interp.w_active_context().as_methodcontext_get_shadow(space).gettemp(0).is_same_object(space.w_one) assert interp.s_active_context().stack() == []
def test_fibWithArgument(): bytecode = ''.join(map(chr, [ 16, 119, 178, 154, 118, 164, 11, 112, 16, 118, 177, 224, 112, 16, 119, 177, 224, 176, 124 ])) shadow = mockclass(space, 0).as_class_get_shadow(space) method = model.W_CompiledMethod(len(bytecode)) method.literalsize = 1 method.bytes = bytecode method.argsize = 1 method.tempsize = 1 method.literals = fakeliterals(space, "fib:") shadow.installmethod("fib:", method) w_object = shadow.new() interp = new_interpreter(sendLiteralSelectorBytecode(16) + returnTopFromMethod) interp.w_active_context().as_methodcontext_get_shadow(space).w_method().literals = fakeliterals(space, "fib:") interp.s_active_context().push(w_object) interp.s_active_context().push(space.wrap_int(8)) result = interp.interpret() assert space.unwrap_int(result) == 34
def test_compiledmethod_atput0(): w_method = model.W_CompiledMethod(3) newheader = joinbits([0,2,0,0,0,0],[9,8,1,6,4,1]) assert w_method.getliteralsize() == 0 w_method.atput0(space, 0, space.wrap_int(newheader)) assert w_method.getliteralsize() == 8 # 2 from new header * BYTES_PER_WORD (= 4) w_method.atput0(space, 4, 'lit1') w_method.atput0(space, 8, 'lit2') w_method.atput0(space, 12, space.wrap_int(ord('a'))) w_method.atput0(space, 13, space.wrap_int(ord('b'))) w_method.atput0(space, 14, space.wrap_int(ord('c'))) assert space.unwrap_int(w_method.at0(space, 0)) == newheader assert w_method.at0(space, 4) == 'lit1' assert w_method.at0(space, 8) == 'lit2' assert space.unwrap_int(w_method.at0(space, 12)) == ord('a') assert space.unwrap_int(w_method.at0(space, 13)) == ord('b') assert space.unwrap_int(w_method.at0(space, 14)) == ord('c')
def run_with_faked_methods(methods, func): # Install faked compiled methods that just invoke the primitive: for (w_class, primnum, argsize, methname) in methods: s_class = w_class.as_class_get_shadow() prim_meth = model.W_CompiledMethod(0) prim_meth.primitive = primnum prim_meth.w_compiledin = w_class prim_meth.argsize = argsize s_class.installmethod(methname, prim_meth) try: func() finally: # Uninstall those methods: for (w_class, _, _, methname) in methods: s_class = w_class.as_class_get_shadow() del s_class.methoddict[methname]
def new(self, extrasize=0): from pypy.lang.smalltalk import classtable w_cls = self.w_self if w_cls == classtable.w_BlockContext: return model.W_BlockContext(None, None, 0, 0) elif w_cls == classtable.w_MethodContext: # From slang: Contexts must only be created with newForMethod: raise error.PrimitiveFailedError if self.instance_kind == POINTERS: return model.W_PointersObject(w_cls, self.instance_size + extrasize) elif self.instance_kind == WORDS: return model.W_WordsObject(w_cls, extrasize) elif self.instance_kind == BYTES: return model.W_BytesObject(w_cls, extrasize) elif self.instance_kind == COMPILED_METHOD: return model.W_CompiledMethod(extrasize) else: raise NotImplementedError(self.instance_kind)
def test_bc_objectAtAndAtPut(): # ^ self objectAt: 1. yields the method header # ^ self objectAt: 2. yields the first literal (22) # ^ self objectAt: 2 put: 3. changes the first literal to 3 # ^ self objectAt: 2. yields the new first literal (3) prim_meth = model.W_CompiledMethod(header=1024) prim_meth.literals = fakeliterals(space, 22) oal = fakeliterals(space, "objectAt:") oalp = fakeliterals(space, "objectAt:put:", 3) def test(): assert interpret_bc( [112, 118, 224, 124], oal, receiver=prim_meth).value == 1024 assert interpret_bc( [112, 119, 224, 124], oal, receiver=prim_meth).value == 22 assert interpret_bc( [112, 119, 33, 240, 124], oalp, receiver=prim_meth).value == 3 assert interpret_bc( [112, 119, 224, 124], oal, receiver=prim_meth).value == 3 run_with_faked_methods( [[space.w_CompiledMethod, primitives.OBJECT_AT, 1, "objectAt:"], [space.w_CompiledMethod, primitives.OBJECT_AT_PUT, 2, "objectAt:put:"]], test)
def sendBytecodesTest(w_class, w_object, bytecodes): for bytecode, result in [ (returnReceiver, w_object), (returnTrue, space.w_true), (returnFalse, space.w_false), (returnNil, space.w_nil), (returnTopFromMethod, space.w_one) ]: shadow = w_class.as_class_get_shadow(space) w_method = model.W_CompiledMethod(2) w_method.bytes = pushConstantOneBytecode + bytecode shadow.installmethod("foo", w_method) interp = new_interpreter(bytecodes) interp.w_active_context().as_methodcontext_get_shadow(space).w_method().literals = fakeliterals(space, "foo") interp.s_active_context().push(w_object) callerContext = interp.w_active_context() interp.step() assert interp.s_active_context().w_sender() == callerContext assert interp.s_active_context().stack() == [] assert interp.w_active_context().as_methodcontext_get_shadow(space).w_receiver().is_same_object(w_object) assert interp.w_active_context().as_methodcontext_get_shadow(space).w_method().is_same_object(shadow.s_methoddict().methoddict["foo"]) assert callerContext.as_context_get_shadow(space).stack() == [] interp.step() interp.step() assert interp.w_active_context() == callerContext assert interp.s_active_context().stack() == [result]
def func(interp, w_class, bytecount, header): # We ignore w_class because W_CompiledMethod is special w_method = model.W_CompiledMethod(bytecount, header) return w_method
def test_compiledmethod_setchar(): w_method = model.W_CompiledMethod(3) w_method.setchar(0, "c") assert w_method.bytes == "c\x00\x00"