def test_smoke(self): ctx = Py27EmitterContext().emit_fragment(Return(Add(Load(0), Load(1)))) self.assertEqual( ctx.last_builder.buf.tolist(), [124, 0, 0, 124, 1, 0, 23, 83]) self.assertEqual( ctx.last_builder.stack_changes, [dec_inc(-0, +1), dec_inc(-0, +1), dec_inc(-2, +1), dec_inc(-1, 0)]) self.assertEqual(ctx.last_builder.stack_usage(), (0, 0, 2)) self.assertTrue(ctx.last_builder.is_valid_stack(), True)
def test_Sub(self): ctx = Py27EmitterContext().emit_fragment(Subtract()) # XXX: this should not be allowed in practice self.assertEqual(ctx.last_builder.buf.tolist(), [24]) self.assertEqual(ctx.last_builder.stack_changes, [dec_inc(-2, +1)]) self.assertEqual(ctx.last_builder.stack_usage(), (-2, -1, 0)) self.assertFalse(ctx.last_builder.is_valid_stack())
class UNARY_NEGATIVE(Py27Op): """Negate the topmost item on the stack.""" stack = common.dec_inc(-1, +1) @classmethod def simulate(cls, ctx, op_arg): """Simulate execution of the operation.""" ctx.stack.append(Neg(ctx.stack.pop()))
class RETURN_VALUE(Py27Op): """Pop one value and return it.""" stack = common.dec_inc(-1, +0) @classmethod def simulate(cls, ctx, op_arg): """Simulate execution of the operation.""" result = Return(ctx.stack.pop()) ctx.retval = result ctx.ops.append(result)
class BINARY_MULTIPLY(Py27Op): """Multiply two topmost arguments from the stack.""" stack = common.dec_inc(-2, +1) @classmethod def simulate(cls, ctx, op_arg): """Simulate execution of the operation.""" b = ctx.stack.pop() a = ctx.stack.pop() result = Multiply(a, b) ctx.stack.append(result)
class BINARY_SUBTRACT(Py27Op): """Subtract two topmost arguments from the stack.""" stack = common.dec_inc(-2, +1) @classmethod def simulate(cls, ctx, op_arg): """Simulate execution of the operation.""" b = ctx.stack.pop() a = ctx.stack.pop() result = Subtract(a, b) ctx.stack.append(result)
class LOAD_FAST(Py27Op): """Load local variable onto the stack.""" has_arg = True stack = common.dec_inc(-0, +1) @classmethod def simulate(cls, ctx, op_arg): """Simulate execution of the operation.""" varname = ctx.varnames[op_arg] result = Load(varname) ctx.stack.append(result)
class LOAD_CONST(Py27Op): """Load a constant onto the stack.""" has_arg = True stack = common.dec_inc(-0, +1) @classmethod def simulate(cls, ctx, op_arg): """Simulate execution of the operation.""" value = ctx.consts[op_arg] result = Const(value) ctx.stack.append(result)
class STORE_FAST(Py27Op): """Store a value from the stack into a local variable.""" has_arg = True stack = common.dec_inc(-1, +0) @classmethod def simulate(cls, ctx, op_arg): """Simulate execution of the operation.""" varname = ctx.varnames[op_arg] value = ctx.stack.pop() result = Store(varname, value) ctx.locals[op_arg] = result ctx.ops.append(result)
def test_Return(self): ctx = Py27EmitterContext().emit_fragment(Return()) self.assertEqual(ctx.last_builder.buf.tolist(), [83]) self.assertEqual(ctx.last_builder.stack_changes, [dec_inc(-1, +0)]) self.assertEqual(ctx.last_builder.stack_usage(), (-1, -1, 0)) self.assertFalse(ctx.last_builder.is_valid_stack())