def cms_list(cms, mark, env, cont): from pycket.interpreter import return_value from pycket.prims.general import map_loop if isinstance(mark, values.W_ContinuationMarkKey): func = CMKSetToListHandler(mark.get_cmk) marks = cms.cont.get_marks(imp.get_base_object(mark)) return map_loop(func, [marks], env, cont) marks = cms.cont.get_marks(mark) return return_value(marks, env, cont)
def impersonate_struct(args): if len(args) == 1 and isinstance(args[0], values_struct.W_RootStruct): return args[0] args, prop_keys, prop_vals = unpack_properties(args, "impersonate-struct") if len(args) < 1: raise SchemeException("impersonate-struct: arity mismatch") struct, args = args[0], args[1:] if args and isinstance(args[0], values_struct.W_StructType): args = args[1:] if len(args) % 2 != 0: raise SchemeException("impersonate-struct: arity mismatch") base = imp.get_base_object(struct) if not isinstance(base, values_struct.W_RootStruct): raise SchemeException("impersonate-struct: not given struct") struct_type = base.struct_type() assert isinstance(struct_type, values_struct.W_StructType) # Consider storing immutables in an easier form in the structs implementation immutables = struct_type.immutables all_false = True count = len(args) / 2 overrides = [None] * count handlers = [None] * count for i in range(count): ovr = args[i * 2] hnd = args[i * 2 + 1] overrides[i] = ovr handlers[i] = hnd if not imp.valid_struct_proc(ovr): raise SchemeException("impersonate-struct: not given valid field accessor") elif (isinstance(ovr, values_struct.W_StructFieldMutator) and ovr.field in immutables): raise SchemeException("impersonate-struct: cannot impersonate immutable field") elif (isinstance(ovr, values_struct.W_StructFieldAccessor) and ovr.field in immutables): raise SchemeException("impersonate-struct: cannot impersonate immutable field") if hnd is not values.w_false: all_false = False if not hnd.iscallable() and hnd is not values.w_false: raise SchemeException("impersonate-struct: supplied hander is not a procedure") if all_false and not prop_keys: return struct return imp.make_struct_proxy(imp.W_ImpStruct, struct, overrides, handlers, prop_keys, prop_vals)
def call(self, struct): from pycket.impersonators import get_base_object struct = get_base_object(struct) if isinstance(struct, W_RootStruct): struct_type = struct.struct_type() while isinstance(struct_type, W_StructType): if struct_type is self.type: return values.w_true struct_type = struct_type.super return values.w_false
def vector2immutablevector(v): from pycket.impersonators import get_base_object # XXX: does not properly handle chaperones v = get_base_object(v) if v.immutable(): return v assert type(v) is values_vector.W_Vector len = v.length() data = [None] * len for i in range(len): data[i] = v.ref(i) return values_vector.W_Vector.fromelements(data, immutable=True)
def cms_first(cms, mark, missing, env, cont): from pycket.interpreter import return_value if cms is values.w_false: the_cont = cont elif isinstance(cms, values.W_ContinuationMarkSet): the_cont = cms.cont else: raise SchemeException("Expected #f or a continuation-mark-set") is_cmk = isinstance(mark, values.W_ContinuationMarkKey) m = imp.get_base_object(mark) if is_cmk else mark v = the_cont.get_mark_first(m) val = v if v is not None else missing if is_cmk: return mark.get_cmk(val, env, cont) return return_value(val, env, cont)
def copy_vector(v, env, cont): from pycket.interpreter import return_value if isinstance(v, values_vector.W_Vector): return return_value(v._make_copy(immutable=True), env, cont) len = v.length() if not len: vector = values_vector.W_Vector.fromelements([]) return return_value(vector, env, cont) # Do a little peeking to provide a hint to the strategy base = imp.get_base_object(v) assert isinstance(base, values_vector.W_Vector) data = values_vector.W_Vector.fromelement(base.ref(0), len) return copy_vector_loop(v, data, len, 0, env, cont)
def chaperone_struct(args): from pycket.prims.struct_structinfo import struct_info if len(args) == 1 and isinstance(args[0], values_struct.W_RootStruct): return args[0] args, prop_keys, prop_vals = unpack_properties(args, "chaperone-struct") if len(args) < 1: raise SchemeException("chaperone-struct: arity mismatch") struct, args = args[0], args[1:] if args and isinstance(args[0], values_struct.W_StructType): args = args[1:] if len(args) % 2 != 0: raise SchemeException("chaperone-struct: arity mismatch") base = imp.get_base_object(struct) if not isinstance(base, values_struct.W_RootStruct): raise SchemeException("chaperone-struct: not given struct") all_false = True count = len(args) / 2 overrides = [None] * count handlers = [None] * count for i in range(count): ovr = args[i * 2] hnd = args[i * 2 + 1] overrides[i] = ovr handlers[i] = hnd if not imp.valid_struct_proc(ovr) and ovr is not struct_info: raise SchemeException( "chaperone-struct: not given valid field accessor") if hnd is not values.w_false: all_false = False if not hnd.iscallable() and hnd is not values.w_false: raise SchemeException( "chaperone-struct: supplied hander is not a procedure") if all_false and not prop_keys: return struct return imp.make_struct_proxy(imp.W_ChpStruct, struct, overrides, handlers, prop_keys, prop_vals)
def copy_vector(v, env, cont): from pycket.interpreter import return_value if isinstance(v, values_vector.W_Vector): return return_value(v._make_copy(immutable=True), env, cont) len = v.length() if not len: vector = values_vector.W_Vector.fromelements([]) return return_value(vector, env, cont) # Do a little peeking to provide a hint to the strategy base = imp.get_base_object(v) assert isinstance(base, values_vector.W_Vector) data = values_vector.W_Vector.fromelement(base.ref(0), len, strategy=base.get_strategy()) return copy_vector_loop(v, data, len, 0, env, cont)
def chaperone_struct(args): from pycket.prims.struct_structinfo import struct_info if len(args) == 1 and isinstance(args[0], values_struct.W_RootStruct): return args[0] args, prop_keys, prop_vals = unpack_properties(args, "chaperone-struct") if len(args) < 1: raise SchemeException("chaperone-struct: arity mismatch") struct, args = args[0], args[1:] if args and isinstance(args[0], values_struct.W_StructType): args = args[1:] if len(args) % 2 != 0: raise SchemeException("chaperone-struct: arity mismatch") base = imp.get_base_object(struct) if not isinstance(base, values_struct.W_RootStruct): raise SchemeException("chaperone-struct: not given struct") all_false = True count = len(args) / 2 overrides = [None] * count handlers = [None] * count for i in range(count): ovr = args[i * 2] hnd = args[i * 2 + 1] overrides[i] = ovr handlers[i] = hnd if not imp.valid_struct_proc(ovr) and ovr is not struct_info: raise SchemeException("chaperone-struct: not given valid field accessor") if hnd is not values.w_false: all_false = False if not hnd.iscallable() and hnd is not values.w_false: raise SchemeException("chaperone-struct: supplied hander is not a procedure") if all_false and not prop_keys: return struct return imp.make_struct_proxy(imp.W_ChpStruct, struct, overrides, handlers, prop_keys, prop_vals)
def cms_first(cms, key, missing, prompt_tag, env, cont): from pycket.interpreter import return_value is_cmk = isinstance(key, values.W_ContinuationMarkKey) m = imp.get_base_object(key) if is_cmk else key if prompt_tag is values.w_default_continuation_prompt_tag and \ (key is values.break_enabled_key or key is values.parameterization_key): prompt_tag = values.w_root_continuation_prompt_tag if cms is values.w_false: the_cont = cont v = cont.get_mark_first(m, upto=[prompt_tag]) elif isinstance(cms, values.W_ContinuationMarkSet): the_cont = cms.cont v = cont.get_mark_first(m, upto=[prompt_tag, cms.prompt_tag]) else: raise SchemeException("Expected #f or a continuation-mark-set") val = v if v is not None else missing if is_cmk: return key.get_cmk(val, env, cont) return return_value(val, env, cont)
def unsafe_struct_set(v, k, val): v = imp.get_base_object(v) assert isinstance(v, values_struct.W_Struct) assert 0 <= k.value < v.struct_type().total_field_cnt return v._set(k.value, val)
def unsafe_struct_ref(v, k): v = imp.get_base_object(v) assert isinstance(v, values_struct.W_Struct) assert 0 <= k.value <= v.struct_type().total_field_count return v._ref(k.value)
def unsafe_struct_ref(v, k): v = imp.get_base_object(v) assert isinstance(v, values_struct.W_Struct) assert 0 <= k.value <= v.struct_type().total_field_cnt return v._ref(k.value)
def impersonate_struct(args): if len(args) == 1 and isinstance(args[0], values_struct.W_RootStruct): return args[0] args, prop_keys, prop_vals = unpack_properties(args, "impersonate-struct") if len(args) < 1: raise SchemeException("impersonate-struct: arity mismatch") struct, args = args[0], args[1:] if args and isinstance(args[0], values_struct.W_StructType): args = args[1:] if len(args) % 2 != 0: raise SchemeException("impersonate-struct: arity mismatch") base = imp.get_base_object(struct) if not isinstance(base, values_struct.W_RootStruct): raise SchemeException("impersonate-struct: not given struct") struct_type = base.struct_type() assert isinstance(struct_type, values_struct.W_StructType) # Consider storing immutables in an easier form in the structs implementation immutables = struct_type.immutables all_false = True count = len(args) / 2 overrides = [None] * count handlers = [None] * count for i in range(count): ovr = args[i * 2] hnd = args[i * 2 + 1] overrides[i] = ovr handlers[i] = hnd if not imp.valid_struct_proc(ovr): raise SchemeException( "impersonate-struct: not given valid field accessor") elif (isinstance(ovr, values_struct.W_StructFieldMutator) and ovr.field in immutables): raise SchemeException( "impersonate-struct: cannot impersonate immutable field") elif (isinstance(ovr, values_struct.W_StructFieldAccessor) and ovr.field in immutables): raise SchemeException( "impersonate-struct: cannot impersonate immutable field") if hnd is not values.w_false: all_false = False if not hnd.iscallable() and hnd is not values.w_false: raise SchemeException( "impersonate-struct: supplied hander is not a procedure") if all_false and not prop_keys: return struct return imp.make_struct_proxy(imp.W_ImpStruct, struct, overrides, handlers, prop_keys, prop_vals)