<s> from pycket import values <EOL> from pycket . cont import continuation <EOL> from pycket . error import SchemeException <EOL> from pycket . hidden_classes import make_map_type , make_caching_map_type <EOL> from pycket . prims . expose import make_call_method <EOL> from rpython . rlib import jit <EOL> from rpython . rlib . objectmodel import specialize <EOL> @ jit . unroll_safe <EOL> def lookup_property ( obj , prop ) : <EOL> while obj . is_proxy ( ) : <EOL> val = obj . get_property ( prop ) <EOL> if val is not None : <EOL> return val <EOL> obj = obj . get_proxied ( ) <EOL> return None <EOL> class Counter ( object ) : <EOL> __attrs__ = [ '<STR_LIT:value>' ] <EOL> def __init__ ( self ) : <EOL> self . value = <NUM_LIT:0> <EOL> def inc ( self ) : <EOL> self . value += <NUM_LIT:1> <EOL> def add_impersonator_counts ( cls ) : <EOL> cls . impersonators = Counter ( ) <EOL> cls . chaperones = Counter ( ) <EOL> old_init = cls . __init__ <EOL> def counting_init ( self , * args ) : <EOL> old_init ( self , * args ) <EOL> if self . is_impersonator ( ) : <EOL> cls . impersonators . inc ( ) <EOL> elif self . is_chaperone ( ) : <EOL> cls . chaperones . inc ( ) <EOL> else : <EOL> assert False <EOL> counting_init . __name__ = old_init . __name__ <EOL> cls . __init__ = counting_init <EOL> return cls <EOL> @ continuation <EOL> def check_chaperone_results ( args , env , cont , vals ) : <EOL> assert vals . num_values ( ) >= args . num_values ( ) <EOL> return check_chaperone_results_loop ( vals , args , <NUM_LIT:0> , env , cont ) <EOL> @ jit . unroll_safe <EOL> def check_chaperone_results_loop ( vals , args , idx , env , cont ) : <EOL> from pycket . interpreter import return_multi_vals <EOL> from pycket . prims . equal import equal_func_unroll_n , EqualInfo <EOL> num_vals = args . num_values ( ) <EOL> while idx < num_vals and vals . get_value ( idx ) is None and args . get_value ( idx ) is None : <EOL> idx += <NUM_LIT:1> <EOL> if idx >= num_vals : <EOL> return return_multi_vals ( vals , env , cont ) <EOL> info = EqualInfo . CHAPERONE_SINGLETON <EOL> unroll_n_times = <NUM_LIT:2> <EOL> return equal_func_unroll_n ( vals . get_value ( idx ) , args . get_value ( idx ) , info , env , <EOL> catch_equal_cont ( vals , args , idx , env , cont ) , unroll_n_times ) <EOL> @ continuation <EOL> def catch_equal_cont ( vals , args , idx , env , cont , _vals ) : <EOL> from pycket . interpreter import check_one_val <EOL> val = check_one_val ( _vals ) <EOL> if val is values . w_false : <EOL> raise SchemeException ( "<STR_LIT>" ) <EOL> return check_chaperone_results_loop ( vals , args , idx + <NUM_LIT:1> , env , cont ) <EOL> @ continuation <EOL> def impersonate_reference_cont ( f , args , app , env , cont , _vals ) : <EOL> old = _vals . get_all_values ( ) <EOL> return f . call_with_extra_info ( args + old , env , cont , app ) <EOL> @ continuation <EOL> @ jit . unroll_safe <EOL> def chaperone_reference_cont ( f , args , app , env , cont , _vals ) : <EOL> args_size = args . num_values ( ) <EOL> vals_size = _vals . num_values ( ) <EOL> all_args = [ None ] * ( args_size + vals_size ) <EOL> idx = <NUM_LIT:0> <EOL> for i in range ( args_size ) : <EOL> all_args [ idx ] = args . get_value ( i ) <EOL> idx += <NUM_LIT:1> <EOL> for i in range ( vals_size ) : <EOL> all_args [ idx ] = _vals . get_value ( i ) <EOL> idx += <NUM_LIT:1> <EOL> return f . call_with_extra_info ( all_args , env , check_chaperone_results ( _vals , env , cont ) , app ) <EOL> @ jit . unroll_safe <EOL> def get_base_object ( x ) : <EOL> while x . is_proxy ( ) : <EOL> x = x . get_base ( ) <EOL> return x <EOL> EMPTY_PROPERTY_MAP = make_caching_map_type ( "<STR_LIT>" ) . EMPTY <EOL> @ jit . unroll_safe <EOL> @ specialize . argtype ( <NUM_LIT:1> ) <EOL> def make_property_map ( prop_keys , map ) : <EOL> if not prop_keys : <EOL> return map <EOL> for key in prop_keys : <EOL> map = map . add_attribute ( key ) <EOL> return map <EOL> @ jit . unroll_safe <EOL> @ specialize . argtype ( <NUM_LIT:1> ) <EOL> def make_specialized_property_map ( prop_keys , map ) : <EOL> if not prop_keys : <EOL> return map <EOL> for key in prop_keys : <EOL> map = map . add_dynamic_attribute ( key ) <EOL> return map <EOL> class ProxyMixin ( object ) : <EOL> EMPTY_MAP = make_map_type ( "<STR_LIT>" ) . EMPTY <EOL> _immutable_fields_ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> @ jit . unroll_safe <EOL> def init_proxy ( self , inner , prop_keys , prop_vals ) : <EOL> assert not prop_keys and not prop_vals or len ( prop_keys ) == len ( prop_vals ) <EOL> self . inner = inner <EOL> self . base = inner . get_base ( ) <EOL> self . property_map = make_property_map ( prop_keys , ProxyMixin . EMPTY_MAP ) <EOL> self . property_storage = prop_vals [ : ] if prop_vals is not None else None <EOL> if self . property_map is not ProxyMixin . EMPTY_MAP : <EOL> assert self . property_map . storage_size ( ) == len ( prop_vals ) <EOL> def get_property_index ( self , index ) : <EOL> return self . property_storage [ index ] <EOL> def iterprops ( self ) : <EOL> return self . property_map . iteritems ( ) <EOL> def property_count ( self ) : <EOL> return self . property_map . storage_size ( ) <EOL> def get_proxied ( self ) : <EOL> return self . inner <EOL> def get_base ( self ) : <EOL> return self . base <EOL> def is_proxy ( self ) : <EOL> return True <EOL> def get_property ( self , prop , default = None ) : <EOL> return self . property_map . lookup ( prop , self , default ) <EOL> def immutable ( self ) : <EOL> return self . base . immutable ( ) <EOL> def tostring ( self ) : <EOL> return self . base . tostring ( ) <EOL> class InlineProxyMixin ( object ) : <EOL> _immutable_fields_ = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> def init_proxy ( self , inner , property_map ) : <EOL> self . inner = inner <EOL> self . base = inner . get_base ( ) <EOL> self . property_map = property_map <EOL> def get_storage_index ( self , idx ) : <EOL> return self . _get_list ( idx ) <EOL> def get_proxied ( self ) : <EOL> return self . inner <EOL> def get_base ( self ) : <EOL> return self . base <EOL> def is_proxy ( self ) : <EOL> return True <EOL> def get_property ( self , prop , default = None ) : <EOL> return self . property_map . lookup ( prop , self , default = None ) <EOL> def immutable ( self ) : <EOL> return get_base_object ( self . base ) . immutable ( ) <EOL> def tostring ( self ) : <EOL> return get_base_object ( self . base ) . tostring ( ) <EOL> class ChaperoneMixin ( object ) : <EOL> def is_chaperone ( self ) : <EOL> return True <EOL> class ImpersonatorMixin ( object ) : <EOL> def is_impersonator ( self ) : <EOL> return True <EOL> class W_ImpPropertyDescriptor ( values . W_Object ) : <EOL> errorname = "<STR_LIT>" <EOL> _immutable_fields_ = [ "<STR_LIT:name>" ] <EOL> def __init__ ( self , name ) : <EOL> self . name = name <EOL> def tostring ( self ) : <EOL> return "<STR_LIT>" <EOL> class W_ImpPropertyFunction ( values . W_Procedure ) : <EOL> _immutable_fields_ = [ "<STR_LIT>" ] <EOL> def __init__ ( self , descriptor ) : <EOL> self . descriptor = descriptor <EOL> class W_ImpPropertyPredicate ( W_ImpPropertyFunction ) : <EOL> errorname = "<STR_LIT>" <EOL> @ make_call_method ( [ values . W_Object ] ) <EOL> def call ( self , obj ) : <EOL> return values . W_Bool . make ( lookup_property ( obj , self . descriptor ) is not None ) <EOL> class W_ImpPropertyAccessor ( W_ImpPropertyFunction ) : <EOL> errorname = "<STR_LIT>" <EOL> @ make_call_method ( [ values . W_Object ] ) <EOL> def call ( self , obj ) : <EOL> return lookup_property ( obj , self . descriptor ) <EOL> w_impersonator_prop_application_mark = W_ImpPropertyDescriptor ( "<STR_LIT>" ) </s>
