@ -37,7 +37,7 @@ STATIC void mp_map_add_qstr(mp_map_t *map, qstr qstr, mp_obj_t value) {
mp_map_lookup ( map , MP_OBJ_NEW_QSTR ( qstr ) , MP_MAP_LOOKUP_ADD_IF_NOT_FOUND ) - > value = value ;
}
void rt _init( void ) {
void mp _init( void ) {
mp_emit_glue_init ( ) ;
// locals = globals for outer module (see Objects/frameobject.c/PyFrame_New())
@ -60,86 +60,52 @@ void rt_init(void) {
# endif
// init sys.path
// for efficiency, left to platform-specific startup code
//sys_path = mp_obj_new_list(0, NULL);
//rt_store_attr(m_sys, MP_QSTR_path, sys_path);
//mp_ sys_path = mp_obj_new_list(0, NULL);
//mp_store_attr(m_sys, MP_QSTR_path, mp_ sys_path);
}
void rt _deinit( void ) {
void mp _deinit( void ) {
mp_map_free ( map_globals ) ;
mp_map_deinit ( & map_builtins ) ;
mp_module_deinit ( ) ;
mp_emit_glue_deinit ( ) ;
}
int rt_is_true ( mp_obj_t arg ) {
DEBUG_OP_printf ( " is true %p \n " , arg ) ;
if ( arg = = mp_const_false ) {
return 0 ;
} else if ( arg = = mp_const_true ) {
return 1 ;
} else if ( arg = = mp_const_none ) {
return 0 ;
} else if ( MP_OBJ_IS_SMALL_INT ( arg ) ) {
if ( MP_OBJ_SMALL_INT_VALUE ( arg ) = = 0 ) {
return 0 ;
} else {
return 1 ;
}
} else {
mp_obj_type_t * type = mp_obj_get_type ( arg ) ;
if ( type - > unary_op ! = NULL ) {
mp_obj_t result = type - > unary_op ( RT_UNARY_OP_BOOL , arg ) ;
if ( result ! = MP_OBJ_NULL ) {
return result = = mp_const_true ;
}
}
mp_obj_t len = mp_obj_len_maybe ( arg ) ;
if ( len ! = MP_OBJ_NULL ) {
// obj has a length, truth determined if len != 0
return len ! = MP_OBJ_NEW_SMALL_INT ( 0 ) ;
} else {
// any other obj is true per Python semantics
return 1 ;
}
}
}
mp_obj_t rt_list_append ( mp_obj_t self_in , mp_obj_t arg ) {
mp_obj_t mp_list_append ( mp_obj_t self_in , mp_obj_t arg ) {
return mp_obj_list_append ( self_in , arg ) ;
}
mp_obj_t rt _load_const_dec( qstr qstr ) {
mp_obj_t mp_load_const_dec ( qstr qstr ) {
DEBUG_OP_printf ( " load '%s' \n " , qstr_str ( qstr ) ) ;
uint len ;
const byte * data = qstr_data ( qstr , & len ) ;
return mp_parse_num_decimal ( ( const char * ) data , len , true , false ) ;
}
mp_obj_t rt _load_const_str( qstr qstr ) {
mp_obj_t mp_load_const_str ( qstr qstr ) {
DEBUG_OP_printf ( " load '%s' \n " , qstr_str ( qstr ) ) ;
return MP_OBJ_NEW_QSTR ( qstr ) ;
}
mp_obj_t rt _load_const_bytes( qstr qstr ) {
mp_obj_t mp_load_const_bytes ( qstr qstr ) {
DEBUG_OP_printf ( " load b'%s' \n " , qstr_str ( qstr ) ) ;
uint len ;
const byte * data = qstr_data ( qstr , & len ) ;
return mp_obj_new_bytes ( data , len ) ;
}
mp_obj_t rt _load_name( qstr qstr ) {
mp_obj_t mp_load_name ( qstr qstr ) {
// logic: search locals, globals, builtins
DEBUG_OP_printf ( " load name %s \n " , qstr_str ( qstr ) ) ;
mp_map_elem_t * elem = mp_map_lookup ( map_locals , MP_OBJ_NEW_QSTR ( qstr ) , MP_MAP_LOOKUP ) ;
if ( elem ! = NULL ) {
return elem - > value ;
} else {
return rt _load_global( qstr ) ;
return mp_load_global ( qstr ) ;
}
}
mp_obj_t rt _load_global( qstr qstr ) {
mp_obj_t mp_load_global ( qstr qstr ) {
// logic: search globals, builtins
DEBUG_OP_printf ( " load global %s \n " , qstr_str ( qstr ) ) ;
mp_map_elem_t * elem = mp_map_lookup ( map_globals , MP_OBJ_NEW_QSTR ( qstr ) , MP_MAP_LOOKUP ) ;
@ -156,7 +122,7 @@ mp_obj_t rt_load_global(qstr qstr) {
return elem - > value ;
}
mp_obj_t rt _load_build_class( void ) {
mp_obj_t mp _load_build_class( void ) {
DEBUG_OP_printf ( " load_build_class \n " ) ;
// lookup __build_class__ in dynamic table of builtins first
mp_map_elem_t * elem = mp_map_lookup ( & map_builtins , MP_OBJ_NEW_QSTR ( MP_QSTR___build_class__ ) , MP_MAP_LOOKUP ) ;
@ -169,47 +135,47 @@ mp_obj_t rt_load_build_class(void) {
}
}
mp_obj_t rt _get_cell( mp_obj_t cell ) {
mp_obj_t mp _get_cell( mp_obj_t cell ) {
return mp_obj_cell_get ( cell ) ;
}
void rt _set_cell( mp_obj_t cell , mp_obj_t val ) {
void mp _set_cell( mp_obj_t cell , mp_obj_t val ) {
mp_obj_cell_set ( cell , val ) ;
}
void rt _store_name( qstr qstr , mp_obj_t obj ) {
void mp _store_name( qstr qstr , mp_obj_t obj ) {
DEBUG_OP_printf ( " store name %s <- %p \n " , qstr_str ( qstr ) , obj ) ;
mp_map_lookup ( map_locals , MP_OBJ_NEW_QSTR ( qstr ) , MP_MAP_LOOKUP_ADD_IF_NOT_FOUND ) - > value = obj ;
}
void rt _delete_name( qstr qstr ) {
void mp _delete_name( qstr qstr ) {
DEBUG_OP_printf ( " delete name %s \n " , qstr_str ( qstr ) ) ;
mp_map_lookup ( map_locals , MP_OBJ_NEW_QSTR ( qstr ) , MP_MAP_LOOKUP_REMOVE_IF_FOUND ) ;
}
void rt _store_global( qstr qstr , mp_obj_t obj ) {
void mp _store_global( qstr qstr , mp_obj_t obj ) {
DEBUG_OP_printf ( " store global %s <- %p \n " , qstr_str ( qstr ) , obj ) ;
mp_map_lookup ( map_globals , MP_OBJ_NEW_QSTR ( qstr ) , MP_MAP_LOOKUP_ADD_IF_NOT_FOUND ) - > value = obj ;
}
mp_obj_t rt _unary_op( int op , mp_obj_t arg ) {
mp_obj_t mp _unary_op( int op , mp_obj_t arg ) {
DEBUG_OP_printf ( " unary %d %p \n " , op , arg ) ;
if ( MP_OBJ_IS_SMALL_INT ( arg ) ) {
mp_small_int_t val = MP_OBJ_SMALL_INT_VALUE ( arg ) ;
switch ( op ) {
case RT _UNARY_OP_BOOL:
case MP _UNARY_OP_BOOL:
return MP_BOOL ( val ! = 0 ) ;
case RT _UNARY_OP_POSITIVE:
case MP _UNARY_OP_POSITIVE:
return arg ;
case RT _UNARY_OP_NEGATIVE:
case MP _UNARY_OP_NEGATIVE:
// check for overflow
if ( val = = MP_SMALL_INT_MIN ) {
return mp_obj_new_int ( - val ) ;
} else {
return MP_OBJ_NEW_SMALL_INT ( - val ) ;
}
case RT _UNARY_OP_INVERT:
case MP _UNARY_OP_INVERT:
return MP_OBJ_NEW_SMALL_INT ( ~ val ) ;
default :
assert ( 0 ) ;
@ -228,7 +194,7 @@ mp_obj_t rt_unary_op(int op, mp_obj_t arg) {
}
}
mp_obj_t rt _binary_op( int op , mp_obj_t lhs , mp_obj_t rhs ) {
mp_obj_t mp _binary_op( int op , mp_obj_t lhs , mp_obj_t rhs ) {
DEBUG_OP_printf ( " binary %d %p %p \n " , op , lhs , rhs ) ;
// TODO correctly distinguish inplace operators for mutable objects
@ -241,20 +207,20 @@ mp_obj_t rt_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
// note that list does not implement + or +=, so that inplace_concat is reached first for +=
// deal with is
if ( op = = RT _BINARY_OP_IS) {
if ( op = = MP _BINARY_OP_IS) {
return MP_BOOL ( lhs = = rhs ) ;
}
// deal with == and != for all types
if ( op = = RT _BINARY_OP_EQUAL | | op = = RT _BINARY_OP_NOT_EQUAL) {
if ( op = = MP _BINARY_OP_EQUAL | | op = = MP _BINARY_OP_NOT_EQUAL) {
if ( mp_obj_equal ( lhs , rhs ) ) {
if ( op = = RT _BINARY_OP_EQUAL) {
if ( op = = MP _BINARY_OP_EQUAL) {
return mp_const_true ;
} else {
return mp_const_false ;
}
} else {
if ( op = = RT _BINARY_OP_EQUAL) {
if ( op = = MP _BINARY_OP_EQUAL) {
return mp_const_false ;
} else {
return mp_const_true ;
@ -263,7 +229,7 @@ mp_obj_t rt_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
}
// deal with exception_match for all types
if ( op = = RT _BINARY_OP_EXCEPTION_MATCH) {
if ( op = = MP _BINARY_OP_EXCEPTION_MATCH) {
// rhs must be issubclass(rhs, BaseException)
if ( mp_obj_is_exception_type ( rhs ) ) {
// if lhs is an instance of an exception, then extract and use its type
@ -294,14 +260,14 @@ mp_obj_t rt_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
// % if lhs=MIN and rhs=-1; result always fits in machine_int_t, then handled by SMALL_INT check
// << checked explicitly
switch ( op ) {
case RT _BINARY_OP_OR:
case RT _BINARY_OP_INPLACE_OR: lhs_val | = rhs_val ; break ;
case RT _BINARY_OP_XOR:
case RT _BINARY_OP_INPLACE_XOR: lhs_val ^ = rhs_val ; break ;
case RT _BINARY_OP_AND:
case RT _BINARY_OP_INPLACE_AND: lhs_val & = rhs_val ; break ;
case RT _BINARY_OP_LSHIFT:
case RT _BINARY_OP_INPLACE_LSHIFT: {
case MP _BINARY_OP_OR:
case MP _BINARY_OP_INPLACE_OR: lhs_val | = rhs_val ; break ;
case MP _BINARY_OP_XOR:
case MP _BINARY_OP_INPLACE_XOR: lhs_val ^ = rhs_val ; break ;
case MP _BINARY_OP_AND:
case MP _BINARY_OP_INPLACE_AND: lhs_val & = rhs_val ; break ;
case MP _BINARY_OP_LSHIFT:
case MP _BINARY_OP_INPLACE_LSHIFT: {
if ( rhs_val < 0 ) {
// negative shift not allowed
nlr_jump ( mp_obj_new_exception_msg ( & mp_type_ValueError , " negative shift count " ) ) ;
@ -315,8 +281,8 @@ mp_obj_t rt_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
}
break ;
}
case RT _BINARY_OP_RSHIFT:
case RT _BINARY_OP_INPLACE_RSHIFT:
case MP _BINARY_OP_RSHIFT:
case MP _BINARY_OP_INPLACE_RSHIFT:
if ( rhs_val < 0 ) {
// negative shift not allowed
nlr_jump ( mp_obj_new_exception_msg ( & mp_type_ValueError , " negative shift count " ) ) ;
@ -325,12 +291,12 @@ mp_obj_t rt_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
lhs_val > > = rhs_val ;
}
break ;
case RT _BINARY_OP_ADD:
case RT _BINARY_OP_INPLACE_ADD: lhs_val + = rhs_val ; break ;
case RT _BINARY_OP_SUBTRACT:
case RT _BINARY_OP_INPLACE_SUBTRACT: lhs_val - = rhs_val ; break ;
case RT _BINARY_OP_MULTIPLY:
case RT _BINARY_OP_INPLACE_MULTIPLY: {
case MP _BINARY_OP_ADD:
case MP _BINARY_OP_INPLACE_ADD: lhs_val + = rhs_val ; break ;
case MP _BINARY_OP_SUBTRACT:
case MP _BINARY_OP_INPLACE_SUBTRACT: lhs_val - = rhs_val ; break ;
case MP _BINARY_OP_MULTIPLY:
case MP _BINARY_OP_INPLACE_MULTIPLY: {
// If long long type exists and is larger than machine_int_t, then
// we can use the following code to perform overflow-checked multiplication.
@ -379,25 +345,25 @@ mp_obj_t rt_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
break ;
}
case RT _BINARY_OP_FLOOR_DIVIDE:
case RT _BINARY_OP_INPLACE_FLOOR_DIVIDE:
case MP _BINARY_OP_FLOOR_DIVIDE:
case MP _BINARY_OP_INPLACE_FLOOR_DIVIDE:
{
lhs_val = python_floor_divide ( lhs_val , rhs_val ) ;
break ;
}
# if MICROPY_ENABLE_FLOAT
case RT _BINARY_OP_TRUE_DIVIDE:
case RT _BINARY_OP_INPLACE_TRUE_DIVIDE: return mp_obj_new_float ( ( mp_float_t ) lhs_val / ( mp_float_t ) rhs_val ) ;
case MP _BINARY_OP_TRUE_DIVIDE:
case MP _BINARY_OP_INPLACE_TRUE_DIVIDE: return mp_obj_new_float ( ( mp_float_t ) lhs_val / ( mp_float_t ) rhs_val ) ;
# endif
case RT _BINARY_OP_MODULO:
case RT _BINARY_OP_INPLACE_MODULO:
case MP _BINARY_OP_MODULO:
case MP _BINARY_OP_INPLACE_MODULO:
{
lhs_val = python_modulo ( lhs_val , rhs_val ) ;
break ;
}
case RT _BINARY_OP_POWER:
case RT _BINARY_OP_INPLACE_POWER:
case MP _BINARY_OP_POWER:
case MP _BINARY_OP_INPLACE_POWER:
if ( rhs_val < 0 ) {
# if MICROPY_ENABLE_FLOAT
lhs = mp_obj_new_float ( lhs_val ) ;
@ -418,10 +384,10 @@ mp_obj_t rt_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
lhs_val = ans ;
}
break ;
case RT _BINARY_OP_LESS: return MP_BOOL ( lhs_val < rhs_val ) ; break ;
case RT _BINARY_OP_MORE: return MP_BOOL ( lhs_val > rhs_val ) ; break ;
case RT _BINARY_OP_LESS_EQUAL: return MP_BOOL ( lhs_val < = rhs_val ) ; break ;
case RT _BINARY_OP_MORE_EQUAL: return MP_BOOL ( lhs_val > = rhs_val ) ; break ;
case MP _BINARY_OP_LESS: return MP_BOOL ( lhs_val < rhs_val ) ; break ;
case MP _BINARY_OP_MORE: return MP_BOOL ( lhs_val > rhs_val ) ; break ;
case MP _BINARY_OP_LESS_EQUAL: return MP_BOOL ( lhs_val < = rhs_val ) ; break ;
case MP _BINARY_OP_MORE_EQUAL: return MP_BOOL ( lhs_val > = rhs_val ) ; break ;
default : assert ( 0 ) ;
}
@ -445,7 +411,7 @@ mp_obj_t rt_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
* NOTE ` a in b ` is ` b . __contains__ ( a ) ` , hence why the generic dispatch
* needs to go below with swapped arguments
*/
if ( op = = RT _BINARY_OP_IN) {
if ( op = = MP _BINARY_OP_IN) {
mp_obj_type_t * type = mp_obj_get_type ( rhs ) ;
if ( type - > binary_op ! = NULL ) {
mp_obj_t res = type - > binary_op ( op , rhs , lhs ) ;
@ -456,8 +422,8 @@ mp_obj_t rt_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
if ( type - > getiter ! = NULL ) {
/* second attempt, walk the iterator */
mp_obj_t next = NULL ;
mp_obj_t iter = rt _getiter( rhs ) ;
while ( ( next = rt _iternext( iter ) ) ! = MP_OBJ_NULL ) {
mp_obj_t iter = mp _getiter( rhs ) ;
while ( ( next = mp _iternext( iter ) ) ! = MP_OBJ_NULL ) {
if ( mp_obj_equal ( next , lhs ) ) {
return mp_const_true ;
}
@ -491,29 +457,29 @@ generic_binary_op:
return mp_const_none ;
}
mp_obj_t rt _call_function_0( mp_obj_t fun ) {
return rt _call_function_n_kw( fun , 0 , 0 , NULL ) ;
mp_obj_t mp _call_function_0( mp_obj_t fun ) {
return mp _call_function_n_kw( fun , 0 , 0 , NULL ) ;
}
mp_obj_t rt _call_function_1( mp_obj_t fun , mp_obj_t arg ) {
return rt _call_function_n_kw( fun , 1 , 0 , & arg ) ;
mp_obj_t mp _call_function_1( mp_obj_t fun , mp_obj_t arg ) {
return mp _call_function_n_kw( fun , 1 , 0 , & arg ) ;
}
mp_obj_t rt _call_function_2( mp_obj_t fun , mp_obj_t arg1 , mp_obj_t arg2 ) {
mp_obj_t mp _call_function_2( mp_obj_t fun , mp_obj_t arg1 , mp_obj_t arg2 ) {
mp_obj_t args [ 2 ] ;
args [ 0 ] = arg1 ;
args [ 1 ] = arg2 ;
return rt _call_function_n_kw( fun , 2 , 0 , args ) ;
return mp _call_function_n_kw( fun , 2 , 0 , args ) ;
}
// wrapper that accepts n_args and n_kw in one argument
// native emitter can only pass at most 3 arguments to a function
mp_obj_t rt _call_function_n_kw_for_native( mp_obj_t fun_in , uint n_args_kw , const mp_obj_t * args ) {
return rt _call_function_n_kw( fun_in , n_args_kw & 0xff , ( n_args_kw > > 8 ) & 0xff , args ) ;
mp_obj_t mp _call_function_n_kw_for_native( mp_obj_t fun_in , uint n_args_kw , const mp_obj_t * args ) {
return mp _call_function_n_kw( fun_in , n_args_kw & 0xff , ( n_args_kw > > 8 ) & 0xff , args ) ;
}
// args contains, eg: arg0 arg1 key0 value0 key1 value1
mp_obj_t rt _call_function_n_kw( mp_obj_t fun_in , uint n_args , uint n_kw , const mp_obj_t * args ) {
mp_obj_t mp _call_function_n_kw( mp_obj_t fun_in , uint n_args , uint n_kw , const mp_obj_t * args ) {
// TODO improve this: fun object can specify its type and we parse here the arguments,
// passing to the function arrays of fixed and keyword arguments
@ -532,31 +498,31 @@ mp_obj_t rt_call_function_n_kw(mp_obj_t fun_in, uint n_args, uint n_kw, const mp
// args contains: fun self/NULL arg(0) ... arg(n_args-2) arg(n_args-1) kw_key(0) kw_val(0) ... kw_key(n_kw-1) kw_val(n_kw-1)
// if n_args==0 and n_kw==0 then there are only fun and self/NULL
mp_obj_t rt _call_method_n_kw( uint n_args , uint n_kw , const mp_obj_t * args ) {
mp_obj_t mp _call_method_n_kw( uint n_args , uint n_kw , const mp_obj_t * args ) {
DEBUG_OP_printf ( " call method (fun=%p, self=%p, n_args=%u, n_kw=%u, args=%p) \n " , args [ 0 ] , args [ 1 ] , n_args , n_kw , args ) ;
int adjust = ( args [ 1 ] = = NULL ) ? 0 : 1 ;
return rt _call_function_n_kw( args [ 0 ] , n_args + adjust , n_kw , args + 2 - adjust ) ;
return mp _call_function_n_kw( args [ 0 ] , n_args + adjust , n_kw , args + 2 - adjust ) ;
}
mp_obj_t rt _build_tuple( int n_args , mp_obj_t * items ) {
mp_obj_t mp _build_tuple( int n_args , mp_obj_t * items ) {
return mp_obj_new_tuple ( n_args , items ) ;
}
mp_obj_t rt _build_list( int n_args , mp_obj_t * items ) {
mp_obj_t mp _build_list( int n_args , mp_obj_t * items ) {
return mp_obj_new_list ( n_args , items ) ;
}
mp_obj_t rt _build_set( int n_args , mp_obj_t * items ) {
mp_obj_t mp _build_set( int n_args , mp_obj_t * items ) {
return mp_obj_new_set ( n_args , items ) ;
}
mp_obj_t rt _store_set( mp_obj_t set , mp_obj_t item ) {
mp_obj_t mp _store_set( mp_obj_t set , mp_obj_t item ) {
mp_obj_set_store ( set , item ) ;
return set ;
}
// unpacked items are stored in reverse order into the array pointed to by items
void rt _unpack_sequence( mp_obj_t seq_in , uint num , mp_obj_t * items ) {
void mp _unpack_sequence( mp_obj_t seq_in , uint num , mp_obj_t * items ) {
uint seq_len ;
if ( MP_OBJ_IS_TYPE ( seq_in , & mp_type_tuple ) | | MP_OBJ_IS_TYPE ( seq_in , & mp_type_list ) ) {
mp_obj_t * seq_items ;
@ -574,16 +540,16 @@ void rt_unpack_sequence(mp_obj_t seq_in, uint num, mp_obj_t *items) {
items [ i ] = seq_items [ num - 1 - i ] ;
}
} else {
mp_obj_t iterable = rt _getiter( seq_in ) ;
mp_obj_t iterable = mp _getiter( seq_in ) ;
for ( seq_len = 0 ; seq_len < num ; seq_len + + ) {
mp_obj_t el = rt _iternext( iterable ) ;
mp_obj_t el = mp _iternext( iterable ) ;
if ( el = = MP_OBJ_NULL ) {
goto too_short ;
}
items [ num - 1 - seq_len ] = el ;
}
if ( rt _iternext( iterable ) ! = MP_OBJ_NULL ) {
if ( mp _iternext( iterable ) ! = MP_OBJ_NULL ) {
goto too_long ;
}
}
@ -595,20 +561,20 @@ too_long:
nlr_jump ( mp_obj_new_exception_msg_varg ( & mp_type_ValueError , " too many values to unpack (expected %d) " , num ) ) ;
}
mp_obj_t rt _build_map( int n_args ) {
mp_obj_t mp _build_map( int n_args ) {
return mp_obj_new_dict ( n_args ) ;
}
mp_obj_t rt _store_map( mp_obj_t map , mp_obj_t key , mp_obj_t value ) {
mp_obj_t mp _store_map( mp_obj_t map , mp_obj_t key , mp_obj_t value ) {
// map should always be a dict
return mp_obj_dict_store ( map , key , value ) ;
}
mp_obj_t rt _load_attr( mp_obj_t base , qstr attr ) {
mp_obj_t mp _load_attr( mp_obj_t base , qstr attr ) {
DEBUG_OP_printf ( " load attr %p.%s \n " , base , qstr_str ( attr ) ) ;
// use load_method
mp_obj_t dest [ 2 ] ;
rt _load_method( base , attr , dest ) ;
mp _load_method( base , attr , dest ) ;
if ( dest [ 1 ] = = MP_OBJ_NULL ) {
// load_method returned just a normal attribute
return dest [ 0 ] ;
@ -621,7 +587,7 @@ mp_obj_t rt_load_attr(mp_obj_t base, qstr attr) {
// no attribute found, returns: dest[0] == MP_OBJ_NULL, dest[1] == MP_OBJ_NULL
// normal attribute found, returns: dest[0] == <attribute>, dest[1] == MP_OBJ_NULL
// method attribute found, returns: dest[0] == <method>, dest[1] == <self>
STATIC void rt _load_method_maybe( mp_obj_t base , qstr attr , mp_obj_t * dest ) {
STATIC void mp _load_method_maybe( mp_obj_t base , qstr attr , mp_obj_t * dest ) {
// clear output to indicate no attribute/method found yet
dest [ 0 ] = MP_OBJ_NULL ;
dest [ 1 ] = MP_OBJ_NULL ;
@ -673,10 +639,10 @@ STATIC void rt_load_method_maybe(mp_obj_t base, qstr attr, mp_obj_t *dest) {
}
}
void rt _load_method( mp_obj_t base , qstr attr , mp_obj_t * dest ) {
void mp _load_method( mp_obj_t base , qstr attr , mp_obj_t * dest ) {
DEBUG_OP_printf ( " load method %p.%s \n " , base , qstr_str ( attr ) ) ;
rt _load_method_maybe( base , attr , dest ) ;
mp _load_method_maybe( base , attr , dest ) ;
if ( dest [ 0 ] = = MP_OBJ_NULL ) {
// no attribute/method called attr
@ -690,7 +656,7 @@ void rt_load_method(mp_obj_t base, qstr attr, mp_obj_t *dest) {
}
}
void rt _store_attr( mp_obj_t base , qstr attr , mp_obj_t value ) {
void mp _store_attr( mp_obj_t base , qstr attr , mp_obj_t value ) {
DEBUG_OP_printf ( " store attr %p.%s <- %p \n " , base , qstr_str ( attr ) , value ) ;
mp_obj_type_t * type = mp_obj_get_type ( base ) ;
if ( type - > store_attr ! = NULL ) {
@ -701,7 +667,7 @@ void rt_store_attr(mp_obj_t base, qstr attr, mp_obj_t value) {
nlr_jump ( mp_obj_new_exception_msg_varg ( & mp_type_AttributeError , " '%s' object has no attribute '%s' " , mp_obj_get_type_str ( base ) , qstr_str ( attr ) ) ) ;
}
void rt _store_subscr( mp_obj_t base , mp_obj_t index , mp_obj_t value ) {
void mp _store_subscr( mp_obj_t base , mp_obj_t index , mp_obj_t value ) {
DEBUG_OP_printf ( " store subscr %p[%p] <- %p \n " , base , index , value ) ;
if ( MP_OBJ_IS_TYPE ( base , & mp_type_list ) ) {
// list store
@ -722,19 +688,19 @@ void rt_store_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t value) {
}
}
mp_obj_t rt _getiter( mp_obj_t o_in ) {
mp_obj_t mp _getiter( mp_obj_t o_in ) {
mp_obj_type_t * type = mp_obj_get_type ( o_in ) ;
if ( type - > getiter ! = NULL ) {
return type - > getiter ( o_in ) ;
} else {
// check for __iter__ method
mp_obj_t dest [ 2 ] ;
rt _load_method_maybe( o_in , MP_QSTR___iter__ , dest ) ;
mp _load_method_maybe( o_in , MP_QSTR___iter__ , dest ) ;
if ( dest [ 0 ] ! = MP_OBJ_NULL ) {
// __iter__ exists, call it and return its result
return rt _call_method_n_kw( 0 , 0 , dest ) ;
return mp _call_method_n_kw( 0 , 0 , dest ) ;
} else {
rt _load_method_maybe( o_in , MP_QSTR___getitem__ , dest ) ;
mp _load_method_maybe( o_in , MP_QSTR___getitem__ , dest ) ;
if ( dest [ 0 ] ! = MP_OBJ_NULL ) {
// __getitem__ exists, create an iterator
return mp_obj_new_getitem_iter ( dest ) ;
@ -748,17 +714,17 @@ mp_obj_t rt_getiter(mp_obj_t o_in) {
// may return MP_OBJ_NULL as an optimisation instead of raise StopIteration()
// may also raise StopIteration()
mp_obj_t rt _iternext_allow_raise( mp_obj_t o_in ) {
mp_obj_t mp _iternext_allow_raise( mp_obj_t o_in ) {
mp_obj_type_t * type = mp_obj_get_type ( o_in ) ;
if ( type - > iternext ! = NULL ) {
return type - > iternext ( o_in ) ;
} else {
// check for __next__ method
mp_obj_t dest [ 2 ] ;
rt _load_method_maybe( o_in , MP_QSTR___next__ , dest ) ;
mp _load_method_maybe( o_in , MP_QSTR___next__ , dest ) ;
if ( dest [ 0 ] ! = MP_OBJ_NULL ) {
// __next__ exists, call it and return its result
return rt _call_method_n_kw( 0 , 0 , dest ) ;
return mp _call_method_n_kw( 0 , 0 , dest ) ;
} else {
nlr_jump ( mp_obj_new_exception_msg_varg ( & mp_type_TypeError , " '%s' object is not an iterator " , mp_obj_get_type_str ( o_in ) ) ) ;
}
@ -767,19 +733,19 @@ mp_obj_t rt_iternext_allow_raise(mp_obj_t o_in) {
// will always return MP_OBJ_NULL instead of raising StopIteration() (or any subclass thereof)
// may raise other exceptions
mp_obj_t rt _iternext( mp_obj_t o_in ) {
mp_obj_t mp _iternext( mp_obj_t o_in ) {
mp_obj_type_t * type = mp_obj_get_type ( o_in ) ;
if ( type - > iternext ! = NULL ) {
return type - > iternext ( o_in ) ;
} else {
// check for __next__ method
mp_obj_t dest [ 2 ] ;
rt _load_method_maybe( o_in , MP_QSTR___next__ , dest ) ;
mp _load_method_maybe( o_in , MP_QSTR___next__ , dest ) ;
if ( dest [ 0 ] ! = MP_OBJ_NULL ) {
// __next__ exists, call it and return its result
nlr_buf_t nlr ;
if ( nlr_push ( & nlr ) = = 0 ) {
mp_obj_t ret = rt _call_method_n_kw( 0 , 0 , dest ) ;
mp_obj_t ret = mp _call_method_n_kw( 0 , 0 , dest ) ;
nlr_pop ( ) ;
return ret ;
} else {
@ -795,14 +761,14 @@ mp_obj_t rt_iternext(mp_obj_t o_in) {
}
}
mp_obj_t rt _make_raise_obj( mp_obj_t o ) {
mp_obj_t mp _make_raise_obj( mp_obj_t o ) {
DEBUG_printf ( " raise %p \n " , o ) ;
if ( mp_obj_is_exception_type ( o ) ) {
// o is an exception type (it is derived from BaseException (or is BaseException))
// create and return a new exception instance by calling o
// TODO could have an option to disable traceback, then builtin exceptions (eg TypeError)
// could have const instances in ROM which we return here instead
return rt _call_function_n_kw( o , 0 , 0 , NULL ) ;
return mp _call_function_n_kw( o , 0 , 0 , NULL ) ;
} else if ( mp_obj_is_exception_instance ( o ) ) {
// o is an instance of an exception, so use it as the exception
return o ;
@ -812,7 +778,7 @@ mp_obj_t rt_make_raise_obj(mp_obj_t o) {
}
}
mp_obj_t rt _import_name( qstr name , mp_obj_t fromlist , mp_obj_t level ) {
mp_obj_t mp _import_name( qstr name , mp_obj_t fromlist , mp_obj_t level ) {
DEBUG_printf ( " import name %s \n " , qstr_str ( name ) ) ;
// build args array
@ -827,10 +793,10 @@ mp_obj_t rt_import_name(qstr name, mp_obj_t fromlist, mp_obj_t level) {
return mp_builtin___import__ ( 5 , args ) ;
}
mp_obj_t rt _import_from( mp_obj_t module , qstr name ) {
mp_obj_t mp _import_from( mp_obj_t module , qstr name ) {
DEBUG_printf ( " import from %p %s \n " , module , qstr_str ( name ) ) ;
mp_obj_t x = rt _load_attr( module , name ) ;
mp_obj_t x = mp _load_attr( module , name ) ;
/* TODO convert AttributeError to ImportError
if ( fail ) {
( ImportError , " cannot import name %s " , qstr_str ( name ) , NULL )
@ -839,66 +805,66 @@ mp_obj_t rt_import_from(mp_obj_t module, qstr name) {
return x ;
}
void rt _import_all( mp_obj_t module ) {
void mp _import_all( mp_obj_t module ) {
DEBUG_printf ( " import all %p \n " , module ) ;
mp_map_t * map = mp_obj_module_get_globals ( module ) ;
for ( uint i = 0 ; i < map - > alloc ; i + + ) {
if ( map - > table [ i ] . key ! = MP_OBJ_NULL ) {
rt _store_name( MP_OBJ_QSTR_VALUE ( map - > table [ i ] . key ) , map - > table [ i ] . value ) ;
mp _store_name( MP_OBJ_QSTR_VALUE ( map - > table [ i ] . key ) , map - > table [ i ] . value ) ;
}
}
}
mp_map_t * rt _locals_get( void ) {
mp_map_t * mp _locals_get( void ) {
return map_locals ;
}
void rt _locals_set( mp_map_t * m ) {
DEBUG_OP_printf ( " rt _locals_set(%p)\n " , m ) ;
void mp _locals_set( mp_map_t * m ) {
DEBUG_OP_printf ( " mp _locals_set(%p)\n " , m ) ;
map_locals = m ;
}
mp_map_t * rt _globals_get( void ) {
mp_map_t * mp _globals_get( void ) {
return map_globals ;
}
void rt _globals_set( mp_map_t * m ) {
DEBUG_OP_printf ( " rt _globals_set(%p)\n " , m ) ;
void mp _globals_set( mp_map_t * m ) {
DEBUG_OP_printf ( " mp _globals_set(%p)\n " , m ) ;
map_globals = m ;
}
// these must correspond to the respective enum
void * const rt_fun_table [ RT _F_NUMBER_OF] = {
rt _load_const_dec,
rt _load_const_str,
rt _load_name,
rt _load_global,
rt _load_build_class,
rt _load_attr,
rt _load_method,
rt _store_name,
rt _store_attr,
rt _store_subscr,
rt _is_true,
rt _unary_op,
rt _binary_op,
rt _build_tuple,
rt _build_list,
rt _list_append,
rt _build_map,
rt _store_map,
rt _build_set,
rt _store_set,
rt _make_function_from_id,
rt _call_function_n_kw_for_native,
rt _call_method_n_kw,
rt _getiter,
rt _iternext,
void * const mp_fun_table [ MP _F_NUMBER_OF] = {
mp _load_const_dec,
mp _load_const_str,
mp _load_name,
mp _load_global,
mp _load_build_class,
mp _load_attr,
mp _load_method,
mp _store_name,
mp _store_attr,
mp _store_subscr,
mp_obj _is_true,
mp _unary_op,
mp _binary_op,
mp _build_tuple,
mp _build_list,
mp _list_append,
mp _build_map,
mp _store_map,
mp _build_set,
mp _store_set,
mp _make_function_from_id,
mp _call_function_n_kw_for_native,
mp _call_method_n_kw,
mp _getiter,
mp _iternext,
} ;
/*
void rt_f_vector ( rt _fun_kind_t fun_kind ) {
( rt _f_table[ fun_kind ] ) ( ) ;
void mp_f_vector ( mp _fun_kind_t fun_kind ) {
( mp _f_table[ fun_kind ] ) ( ) ;
}
*/