def func(interp, w_arg, w_rcvr): w_arg_class = w_arg.getclass() w_rcvr_class = w_rcvr.getclass() # We should fail if: # 1. Rcvr or arg are SmallIntegers # XXX this is wrong too if (w_arg_class == classtable.w_SmallInteger or w_rcvr_class == classtable.w_SmallInteger): raise PrimitiveFailedError() # 2. Rcvr is an instance of a compact class and argument isn't # or vice versa XXX we don't have to fail here, but for squeak it's a problem # 3. Format of rcvr is different from format of argument raise PrimitiveNotYetWrittenError() # XXX needs to work in the shadows if w_arg_class.format != w_rcvr_class.format: raise PrimitiveFailedError() # Fail when argument class is fixed and rcvr's size differs from the # size of an instance of the arg if w_arg_class.instsize() != w_rcvr_class.instsize(): raise PrimitiveFailedError() w_rcvr.w_class = w_arg.w_class
def func(interp, argument_count): # argument_count does NOT include the receiver. # This means that for argument_count == 3 the stack looks like: # 3 2 1 Top # Rcvr | Arg 0 | Arg1 | Arg 2 # frame = interp.w_active_context # Validate that we have a block on the stack and that it received # the proper number of arguments: w_block_ctx = frame.peek(argument_count) if not isinstance(w_block_ctx, model.W_BlockContext): raise PrimitiveFailedError() exp_arg_cnt = w_block_ctx.expected_argument_count() if argument_count != exp_arg_cnt: # exp_arg_cnt doesn't count self raise PrimitiveFailedError() # Initialize the block stack with the arguments that were # pushed. Also pop the receiver. block_args = frame.pop_and_return_n(exp_arg_cnt) w_block_ctx.push_all(block_args) frame.pop() finalize_block_ctx(interp, w_block_ctx, frame)
def func(interp, w_rcvr): # XXX we might want to disable this check if not w_rcvr.getclass(interp.space).is_same_object( interp.space.classtable['w_Process']): raise PrimitiveFailedError() wrapper.ProcessWrapper(interp.space, w_rcvr).suspend(interp) return w_rcvr
def func(interp, w_rcvr): # XXX we might want to disable this check if not w_rcvr.getclass(interp.space).is_same_object( interp.space.classtable['w_Semaphore']): raise PrimitiveFailedError() wrapper.SemaphoreWrapper(interp.space, w_rcvr).wait(interp) return w_rcvr
def wrapped(interp, argument_count_m1): argument_count = argument_count_m1 + 1 # to account for the rcvr frame = interp.w_active_context assert argument_count == len_unwrap_spec if len(frame.stack) < len_unwrap_spec: raise PrimitiveFailedError() args = () for i, spec in unrolling_unwrap_spec: index = -len_unwrap_spec + i w_arg = frame.stack[index] if spec is int: args += (utility.unwrap_int(w_arg), ) elif spec is index1_0: args += (utility.unwrap_int(w_arg) - 1, ) elif spec is float: args += (utility.unwrap_float(w_arg), ) elif spec is object: args += (w_arg, ) elif spec is str: args += (w_arg.as_string(), ) elif spec is char: args += (unwrap_char(w_arg), ) else: raise NotImplementedError("unknown unwrap_spec %s" % (spec, )) w_result = func(interp, *args) frame.pop_n(len_unwrap_spec) # only if no exception occurs! if not no_result: assert w_result is not None interp.w_active_context.push(w_result)
def func(interp, w_obj, n0, w_val): val = interp.space.unwrap_char(w_val) n0 = assert_valid_index(interp.space, n0, w_obj) if not (isinstance(w_obj, model.W_CompiledMethod) or isinstance(w_obj, model.W_BytesObject)): raise PrimitiveFailedError() w_obj.setchar(n0, val) return w_val
def func(interp, w_block_ctx, w_args): if not isinstance(w_block_ctx, model.W_BlockContext): raise PrimitiveFailedError() exp_arg_cnt = w_block_ctx.expected_argument_count() # Check that our arguments have pointers format and the right size: if w_args.getclass() != classtable.w_Array: raise PrimitiveFailedError() if w_args.size() != exp_arg_cnt: raise PrimitiveFailedError() # Push all the items from the array for i in range(exp_arg_cnt): w_block_ctx.push(w_args.fetchvarpointer(i)) # XXX Check original logic. Image does not test this anyway # because falls back to value + internal implementation finalize_block_ctx(interp, w_block_ctx, interp.w_active_context)
def func(interp, w_block_ctx, w_args): assert isinstance(w_block_ctx, model.W_PointersObject) s_block_ctx = w_block_ctx.as_blockcontext_get_shadow(interp.space) exp_arg_cnt = s_block_ctx.expected_argument_count() # Check that our arguments have pointers format and the right size: if not w_args.getclass(interp.space).is_same_object(interp.space.w_Array): raise PrimitiveFailedError() if w_args.size() != exp_arg_cnt: raise PrimitiveFailedError() assert isinstance(w_args, model.W_PointersObject) # Push all the items from the array for i in range(exp_arg_cnt): s_block_ctx.push(w_args.at0(interp.space, i)) # XXX Check original logic. Image does not test this anyway # because falls back to value + internal implementation finalize_block_ctx(interp, s_block_ctx, interp.w_active_context())
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 func(interp, argument_count): # argument_count does NOT include the receiver. # This means that for argument_count == 3 the stack looks like: # 3 2 1 Top # Rcvr | Arg 0 | Arg1 | Arg 2 # frame = interp.s_active_context() # Validate that we have a block on the stack and that it received # the proper number of arguments: w_block_ctx = frame.peek(argument_count) # XXX need to check this since VALUE is called on all sorts of objects. if not w_block_ctx.getclass(interp.space).is_same_object( interp.space.w_BlockContext): raise PrimitiveFailedError() assert isinstance(w_block_ctx, model.W_PointersObject) s_block_ctx = w_block_ctx.as_blockcontext_get_shadow(interp.space) exp_arg_cnt = s_block_ctx.expected_argument_count() if argument_count != exp_arg_cnt: # exp_arg_cnt doesn't count self raise PrimitiveFailedError() # Initialize the block stack with the arguments that were # pushed. Also pop the receiver. block_args = frame.pop_and_return_n(exp_arg_cnt) # Reset stack of blockcontext to [] s_block_ctx.reset_stack() s_block_ctx.push_all(block_args) frame.pop() finalize_block_ctx(interp, s_block_ctx, frame.w_self())
def func(interp, w_rcvr, w_new): if w_rcvr.size() != w_new.size(): raise PrimitiveFailedError w_lefts = [] w_rights = [] for i in range(w_rcvr.size()): w_left = w_rcvr.at0(interp.space, i) w_right = w_new.at0(interp.space, i) if w_left.become(w_right): w_lefts.append(w_left) w_rights.append(w_right) else: for i in range(len(w_lefts)): w_lefts[i].become(w_rights[i]) raise PrimitiveFailedError() return w_rcvr
def func(interp, w_context, argcnt): frame = interp.w_active_context # From B.B.: If receiver is a MethodContext, then it becomes # the new BlockContext's home context. Otherwise, the home # context of the receiver is used for the new BlockContext. # Note that in our impl, MethodContext.w_home == self if not isinstance(w_context, model.W_ContextPart): raise PrimitiveFailedError() w_method_context = w_context.w_home # The block bytecodes are stored inline: so we skip past the # byteodes to invoke this primitive to find them (hence +2) initialip = frame.pc + 2 w_new_context = model.W_BlockContext(w_method_context, None, argcnt, initialip) return w_new_context
def wrapped(interp, argument_count_m1): argument_count = argument_count_m1 + 1 # to account for the rcvr frame = interp.w_active_context() s_frame = frame.as_context_get_shadow(interp.space) assert argument_count == len_unwrap_spec if len(s_frame.stack()) < len_unwrap_spec: raise PrimitiveFailedError() args = () for i, spec in unrolling_unwrap_spec: index = len_unwrap_spec - 1 - i w_arg = s_frame.peek(index) if spec is int: args += (interp.space.unwrap_int(w_arg), ) elif spec is index1_0: args += (interp.space.unwrap_int(w_arg) - 1, ) elif spec is float: args += (interp.space.unwrap_float(w_arg), ) elif spec is object: args += (w_arg, ) elif spec is str: assert isinstance(w_arg, model.W_BytesObject) args += (w_arg.as_string(), ) elif spec is char: args += (unwrap_char(w_arg), ) else: raise NotImplementedError("unknown unwrap_spec %s" % (spec, )) w_result = func(interp, *args) # After calling primitive, reload context-shadow in case it # needs to be updated new_s_frame = interp.s_active_context() frame.as_context_get_shadow(interp.space).pop_n( len_unwrap_spec) # only if no exception occurs! if not no_result: assert w_result is not None new_s_frame.push(w_result)
def assert_valid_index(n0, w_obj): if not 0 <= n0 < w_obj.size(): raise PrimitiveFailedError() # return the index, since from here on the annotator knows that # n0 cannot be negative return n0
def func(interp, w_cls, size): assert isinstance(w_cls, model.W_PointersObject) s_class = w_cls.as_class_get_shadow(interp.space) if not s_class.isvariable(): raise PrimitiveFailedError() return s_class.new(size)
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_cls, size): s_class = w_cls.as_class_get_shadow() if not s_class.isvariable(): raise PrimitiveFailedError() return s_class.new(size)
def func(interp, w_rcvr, n0, w_value): if not isinstance(w_rcvr, model.W_CompiledMethod): raise PrimitiveFailedError() #assert_bounds(n0, 0, len(w_rcvr.literals)) w_rcvr.literalatput0(n0, w_value) return w_value
def func(interp, w_rcvr, n0): if not isinstance(w_rcvr, model.W_CompiledMethod): raise PrimitiveFailedError() return w_rcvr.literalat0(n0)
def func(interp, w_obj): if not w_obj.shadow_of_my_class().isvariable(): raise PrimitiveFailedError() return utility.wrap_int(w_obj.primsize())
def func(interp, argcount): raise PrimitiveFailedError()
def func(interp, receiver, argument): try: res = rarithmetic.ovfcheck(op(receiver, argument)) except OverflowError: raise PrimitiveFailedError() return utility.wrap_int(res)
def assert_bounds(n0, minimum, maximum): if not minimum <= n0 < maximum: raise PrimitiveFailedError()
def func(interp, receiver, argument): if argument == 0: raise PrimitiveFailedError() return utility.wrap_int(receiver // argument)
def func(interp, receiver, argument): if argument == 0: raise PrimitiveFailedError() return interp.space.wrap_int(receiver % argument)