def test_bc_3_plus_4(): # value0 # " (self >> #value0) byteCode " # " (self >> #value0) literals " # # ^ [ 3 + 4 ] value assert interpret_bc([137, 117, 200, 164, 4, 32, 33, 176, 125, 201, 124], fakeliterals(wrap_int(3), wrap_int(4))).value == 7
def test_bc_3_plus_4(): # value0 # " (self >> #value0) byteCode " # " (self >> #value0) literals " # # ^ [ 3 + 4 ] value assert interpret_bc( [ 137, 117, 200, 164, 4, 32, 33, 176, 125, 201, 124], fakeliterals(wrap_int(3), wrap_int(4))).value == 7
def test(): interp = new_interpreter(sendLiteralSelectorBytecode(1 + 16)) interp.w_active_context.w_method().literals = fakeliterals("foo", "sub") interp.w_active_context.push(wrap_int(50)) interp.w_active_context.push(wrap_int(8)) callerContext = interp.w_active_context interp.step() assert interp.w_active_context is callerContext assert len(interp.w_active_context.stack) == 1 w_result = interp.w_active_context.pop() assert unwrap_int(w_result) == 42
def func(interp, receiver, argument): # left shift, must fail if we loose bits beyond 32 if argument > 0: shifted = receiver << argument if (shifted >> argument) != receiver: raise PrimitiveFailedError() return utility.wrap_int(shifted) # right shift, ok to lose bits else: return utility.wrap_int(receiver >> -argument)
def test(): interp = new_interpreter(sendLiteralSelectorBytecode(1 + 16)) interp.w_active_context.w_method().literals = fakeliterals( "foo", "sub") interp.w_active_context.push(wrap_int(50)) interp.w_active_context.push(wrap_int(8)) callerContext = interp.w_active_context interp.step() assert interp.w_active_context is callerContext assert len(interp.w_active_context.stack) == 1 w_result = interp.w_active_context.pop() assert unwrap_int(w_result) == 42
def fetch(self, index): from pypy.lang.smalltalk import utility if index == constants.BLKCTX_BLOCK_ARGUMENT_COUNT_INDEX: return utility.wrap_int(self.argcnt) elif index == constants.BLKCTX_INITIAL_IP_INDEX: return utility.wrap_int(self.initialip) elif index == constants.BLKCTX_HOME_INDEX: return self.w_home elif index >= constants.BLKCTX_TEMP_FRAME_START: stack_index = len(self.stack) - index - 1 return self.stack[stack_index] else: return W_ContextPart.fetch(self, index)
def fetch(self, index): from pypy.lang.smalltalk import utility, objtable if index == constants.CTXPART_SENDER_INDEX: if self.w_sender: return self.w_sender else: return objtable.w_nil elif index == constants.CTXPART_PC_INDEX: return utility.wrap_int(self.pc) elif index == constants.CTXPART_STACKP_INDEX: return utility.wrap_int(len(self.stack)) # Invalid! raise IndexError
def wrap(x): if isinstance(x, int): return utility.wrap_int(x) if isinstance(x, float): return utility.wrap_float(x) if isinstance(x, model.W_Object): return x if isinstance(x, str) and len(x) == 1: return utility.wrap_char(x) if isinstance(x, str): return utility.wrap_string(x) raise NotImplementedError
def at0(self, index0): from pypy.lang.smalltalk import utility if index0 < self.getliteralsize(): self.literalat0(index0) else: index0 = index0 - self.getliteralsize() return utility.wrap_int(ord(self.bytes[index0]))
def check_me(): interp, w_object = build() interp.w_active_context.push(w_object) interp.w_active_context.push(wrap_int(8)) result = interp.interpret() assert unwrap_int(result) == 34 print "check_me() ok"
def test_bc_push_rcvr_in_block(): # value1 # " (self >> #value1) byteCode " # " (self >> #value1) literals " # # ^ [ self ] value assert interpret_bc([137, 117, 200, 164, 2, 112, 125, 201, 124], fakeliterals(wrap_int(3))) is objtable.w_nil
def test_bc_x_plus_x_plus_1(): # value1 # " (self >> #value1) byteCode " # " (self >> #value1) literals " # # ^ [ :x | x + x + 1 ] value: 3 assert interpret_bc( [137, 118, 200, 164, 7, 104, 16, 16, 176, 118, 176, 125, 32, 202, 124], fakeliterals(wrap_int(3))).value == 7
def test_bc_push_rcvr_in_block(): # value1 # " (self >> #value1) byteCode " # " (self >> #value1) literals " # # ^ [ self ] value assert interpret_bc( [ 137, 117, 200, 164, 2, 112, 125, 201, 124 ], fakeliterals(wrap_int(3))) is objtable.w_nil
def test_bc_x_plus_x_plus_1(): # value1 # " (self >> #value1) byteCode " # " (self >> #value1) literals " # # ^ [ :x | x + x + 1 ] value: 3 assert interpret_bc( [ 137, 118, 200, 164, 7, 104, 16, 16, 176, 118, 176, 125, 32, 202, 124 ], fakeliterals(wrap_int(3))).value == 7
def entry_point(argv): if len(argv) > 1: n = int(argv[1]) else: n = 8 interp.w_active_context.push(w_object) interp.w_active_context.push(wrap_int(n)) result = interp.interpret() print unwrap_int(result) return 0
def fakeliteral(lit): if isinstance(lit, str): return fakesymbol(lit) elif isinstance(lit, int): return wrap_int(lit) elif isinstance(lit, list): lstlen = len(lit) res = classtable.w_Array.as_class_get_shadow().new(lstlen) for i in range(lstlen): res.storevarpointer(i, fakeliteral(lit[i])) return res return lit
def build_smalltalk_class(name, format, w_superclass=w_Object, w_classofclass=None, methods={}): if w_classofclass is None: w_classofclass = build_smalltalk_class(None, 0x94, w_superclass.w_class, w_Metaclass) w_methoddict = build_methoddict(methods) size = constants.CLASS_NAME_INDEX + 1 w_class = model.W_PointersObject(w_classofclass, size) w_class.store(constants.CLASS_SUPERCLASS_INDEX, w_superclass) w_class.store(constants.CLASS_METHODDICT_INDEX, w_methoddict) w_class.store(constants.CLASS_FORMAT_INDEX, utility.wrap_int(format)) if name is not None: w_class.store(constants.CLASS_NAME_INDEX, utility.wrap_string(name)) return w_class
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(0).as_class_get_shadow() method = model.W_CompiledMethod(len(bytecode)) method.literalsize = 1 method.bytes = bytecode method.argsize = 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:") interp.w_active_context.push(w_object) interp.w_active_context.push(wrap_int(8)) result = interp.interpret() assert unwrap_int(result) == 34
def w(any): if any is None: return objtable.w_nil if isinstance(any, str): # assume never have strings of length 1 if len(any) == 1: return utility.wrap_chr(any) else: return utility.wrap_string(any) if isinstance(any, int): return utility.wrap_int(any) if isinstance(any, bool): return utility.wrap_bool(any) if isinstance(any, float): return utility.wrap_float(any) else: raise Exception
def build_methoddict(methods): size = int(len(methods) * 1.5) w_methoddict = w_MethodDict.as_class_get_shadow().new(size) w_array = w_Array.as_class_get_shadow().new(size) for i in range(size): w_array.store(i, objtable.w_nil) w_methoddict.store(constants.METHODDICT_NAMES_INDEX+i, objtable.w_nil) w_tally = utility.wrap_int(len(methods)) w_methoddict.store(constants.METHODDICT_TALLY_INDEX, w_tally) w_methoddict.store(constants.METHODDICT_VALUES_INDEX, w_array) positions = range(size) random.shuffle(positions) for selector, w_compiledmethod in methods.items(): pos = positions.pop() w_selector = utility.wrap_string(selector) w_methoddict.store(constants.METHODDICT_NAMES_INDEX+pos, w_selector) w_array.store(pos, w_compiledmethod) #print w_methoddict._vars return w_methoddict
def build_methoddict(methods): size = int(len(methods) * 1.5) w_methoddict = w_MethodDict.as_class_get_shadow().new(size) w_array = w_Array.as_class_get_shadow().new(size) for i in range(size): w_array.store(i, objtable.w_nil) w_methoddict.store(constants.METHODDICT_NAMES_INDEX + i, objtable.w_nil) w_tally = utility.wrap_int(len(methods)) w_methoddict.store(constants.METHODDICT_TALLY_INDEX, w_tally) w_methoddict.store(constants.METHODDICT_VALUES_INDEX, w_array) positions = range(size) random.shuffle(positions) for selector, w_compiledmethod in methods.items(): pos = positions.pop() w_selector = utility.wrap_string(selector) w_methoddict.store(constants.METHODDICT_NAMES_INDEX + pos, w_selector) w_array.store(pos, w_compiledmethod) #print w_methoddict._vars return w_methoddict
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(0).as_class_get_shadow() method = model.W_CompiledMethod(len(bytecode)) method.literalsize = 1 method.bytes = bytecode method.argsize = 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:") interp.w_active_context.push(w_object) interp.w_active_context.push(wrap_int(8)) result = interp.interpret() assert unwrap_int(result) == 34
def func(interp, receiver, argument): try: res = rarithmetic.ovfcheck(op(receiver, argument)) except OverflowError: raise PrimitiveFailedError() return utility.wrap_int(res)
def at0(self, index0): from pypy.lang.smalltalk import utility return utility.wrap_int(self.getword(index0))
def literalat0(self, index0): if index0 == 0: from pypy.lang.smalltalk import utility return utility.wrap_int(self.getheader()) else: return self.literals[index0-1]
def func(interp, w_arg): import time import math return utility.wrap_int(int(math.fmod(time.time()*1000, constants.TAGGED_MAXINT/2)))
def fake_bytes_left(): return utility.wrap_int(2**20) # XXX we don't know how to do this :-(
def func(interp, w_obj): if not w_obj.shadow_of_my_class().isvariable(): raise PrimitiveFailedError() return utility.wrap_int(w_obj.primsize())
def test(): assert interpret_bc( [ 137, 119, 200, 164, 6, 105, 104, 16, 17, 176, 125, 33, 34, 240, 124 ], fakeliterals("value:value:", wrap_int(3), wrap_int(4))).value == 7
def test(): assert interpret_bc([ 137, 119, 200, 164, 6, 105, 104, 16, 17, 176, 125, 33, 34, 240, 124 ], fakeliterals("value:value:", wrap_int(3), wrap_int(4))).value == 7
def literalat0(self, index0): if index0 == 0: from pypy.lang.smalltalk import utility return utility.wrap_int(self.getheader()) else: return self.literals[index0 - 1]
def func(interp, receiver, argument): res = op(receiver, argument) return utility.wrap_int(res)
def initialize_int(self, value, reader): self.owner = reader self.value = value self.size = -1 self.w_object = utility.wrap_int(value)
def func(interp, w_arg): import time return utility.wrap_int(0x23910d6c) # HACK: too big for a small int!
def func(interp, receiver, argument): if argument == 0: raise PrimitiveFailedError() return utility.wrap_int(receiver // argument)
def func(interp, f): w_res = utility.wrap_int(int(f)) return w_res
def func(interp, w_rcvr): if isinstance(w_rcvr, model.W_SmallInteger): raise PrimitiveFailedError() return utility.wrap_int(w_rcvr.gethash())
def func(interp, w_arg): import time import math return utility.wrap_int( int(math.fmod(time.time() * 1000, constants.TAGGED_MAXINT / 2)))