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 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 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 fakesymbol(s, _cache={}): try: return _cache[s] except KeyError: result = _cache[s] = wrap_string(s) return result