Skip to content
Snippets Groups Projects
  • Damien George's avatar
    4f3d9429
    py: Fix native functions so they run with their correct globals context. · 4f3d9429
    Damien George authored
    Prior to this commit a function compiled with the native decorator
    @micropython.native would not work correctly when accessing global
    variables, because the globals dict was not being set upon function entry.
    
    This commit fixes this problem by, upon function entry, setting as the
    current globals dict the globals dict context the function was defined
    within, as per normal Python semantics, and as bytecode does.  Upon
    function exit the original globals dict is restored.
    
    In order to restore the globals dict when an exception is raised the native
    function must guard its internals with an nlr_push/nlr_pop pair.  Because
    this push/pop is relatively expensive, in both C stack usage for the
    nlr_buf_t and CPU execution time, the implementation here optimises things
    as much as possible.  First, the compiler keeps track of whether a function
    even needs to access global variables.  Using this information the native
    emitter then generates three different kinds of code:
    
    1. no globals used, no exception handlers: no nlr handling code and no
       setting of the globals dict.
    
    2. globals used, no exception handlers: an nlr_buf_t is allocated on the
       C stack but it is not used if the globals dict is unchanged, saving
       execution time because nlr_push/nlr_pop don't need to run.
    
    3. function has exception handlers, may use globals: an nlr_buf_t is
       allocated and nlr_push/nlr_pop are always called.
    
    In the end, native functions that don't access globals and don't have
    exception handlers will run more efficiently than those that do.
    
    Fixes issue #1573.
    4f3d9429
    History
    py: Fix native functions so they run with their correct globals context.
    Damien George authored
    Prior to this commit a function compiled with the native decorator
    @micropython.native would not work correctly when accessing global
    variables, because the globals dict was not being set upon function entry.
    
    This commit fixes this problem by, upon function entry, setting as the
    current globals dict the globals dict context the function was defined
    within, as per normal Python semantics, and as bytecode does.  Upon
    function exit the original globals dict is restored.
    
    In order to restore the globals dict when an exception is raised the native
    function must guard its internals with an nlr_push/nlr_pop pair.  Because
    this push/pop is relatively expensive, in both C stack usage for the
    nlr_buf_t and CPU execution time, the implementation here optimises things
    as much as possible.  First, the compiler keeps track of whether a function
    even needs to access global variables.  Using this information the native
    emitter then generates three different kinds of code:
    
    1. no globals used, no exception handlers: no nlr handling code and no
       setting of the globals dict.
    
    2. globals used, no exception handlers: an nlr_buf_t is allocated on the
       C stack but it is not used if the globals dict is unchanged, saving
       execution time because nlr_push/nlr_pop don't need to run.
    
    3. function has exception handlers, may use globals: an nlr_buf_t is
       allocated and nlr_push/nlr_pop are always called.
    
    In the end, native functions that don't access globals and don't have
    exception handlers will run more efficiently than those that do.
    
    Fixes issue #1573.