Damien George
4f3d9429b5
py: Fix native functions so they run with their correct globals context.
...
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 .
2018-09-13 22:47:20 +10:00
..
2017-10-19 18:57:26 +11:00
2018-08-16 13:45:24 +10:00
2018-08-16 13:45:24 +10:00
2017-12-08 19:07:00 +11:00
2016-12-09 22:50:58 +11:00
2018-08-16 13:45:24 +10:00
2018-08-16 13:45:24 +10:00
2018-08-16 13:45:24 +10:00
2018-08-16 13:45:24 +10:00
2018-08-16 13:45:24 +10:00
2018-08-16 13:45:24 +10:00
2018-08-17 14:53:58 +10:00
2018-08-17 14:53:58 +10:00
2017-10-05 10:49:44 +11:00
2017-10-10 10:37:38 +11:00
2017-10-04 12:37:50 +11:00
2017-11-16 13:17:51 +11:00
2017-08-17 16:19:35 +10:00
2018-06-27 14:54:40 +10:00
2017-10-04 12:37:50 +11:00
2018-07-08 21:31:09 +10:00
2018-02-20 18:00:44 +11:00
2018-09-13 22:47:20 +10:00
2017-07-31 18:35:40 +10:00
2018-08-16 13:56:36 +10:00
2018-05-23 00:35:16 +10:00
2018-05-23 00:22:47 +10:00
2018-08-02 14:17:24 +10:00
2018-02-14 18:41:17 +11:00
2017-07-31 18:35:40 +10:00
2016-12-09 21:23:17 +11:00
2018-09-04 14:31:28 +10:00
2018-09-13 22:47:20 +10:00
2018-09-04 14:31:28 +10:00
2018-09-04 14:31:28 +10:00
2018-09-13 22:47:20 +10:00
2018-09-04 14:31:28 +10:00
2018-03-01 17:00:02 +11:00
2017-07-31 18:35:40 +10:00
2017-07-31 18:35:40 +10:00
2017-07-31 18:35:40 +10:00
2018-08-14 16:11:21 +10:00
2018-06-12 11:55:29 +10:00
2018-06-22 17:00:29 +10:00
2018-06-12 12:17:43 +10:00
2017-07-31 18:35:40 +10:00
2018-05-10 23:14:23 +10:00
2018-03-16 23:54:06 +11:00
2018-02-22 12:48:51 +11:00
2018-07-09 14:40:02 +10:00
2018-08-02 14:17:24 +10:00
2018-05-18 23:31:00 +10:00
2017-11-15 11:56:58 +11:00
2018-06-12 13:53:43 +10:00
2017-07-31 18:35:40 +10:00
2018-06-20 15:57:10 +10:00
2017-08-30 21:02:00 +10:00
2018-02-21 22:39:25 +11:00
2017-08-30 21:02:00 +10:00
2018-06-12 12:29:26 +10:00
2017-10-10 16:01:04 +11:00
2018-04-04 14:24:03 +10:00
2017-09-01 11:11:09 +10:00
2018-06-20 15:57:10 +10:00
2017-12-11 13:49:09 +11:00
2017-02-22 12:58:11 +11:00
2018-09-11 14:51:52 +10:00
2018-05-01 15:53:25 +10:00
2017-07-31 18:35:40 +10:00
2017-12-07 10:31:14 +02:00
2017-07-31 18:35:40 +10:00
2017-07-31 18:35:40 +10:00
2018-06-12 15:06:11 +10:00
2017-07-18 11:57:39 +10:00
2018-05-21 12:48:26 +10:00
2017-12-29 14:05:48 +11:00
2018-09-13 22:47:20 +10:00
2017-12-29 22:24:53 +11:00
2017-12-29 22:24:46 +11:00
2017-12-28 16:46:30 +11:00
2018-04-27 15:10:42 +10:00
2017-12-29 22:24:46 +11:00
2018-05-15 11:17:28 +10:00
2017-12-28 16:46:30 +11:00
2017-10-04 12:37:50 +11:00
2018-07-08 23:45:05 +10:00
2018-09-11 15:10:10 +10:00
2018-06-18 13:40:53 +10:00
2017-07-31 18:35:40 +10:00
2017-10-04 12:37:50 +11:00
2017-12-11 13:49:09 +11:00
2017-07-31 18:35:40 +10:00
2017-07-31 18:35:40 +10:00
2017-10-04 12:37:50 +11:00
2018-05-11 13:44:50 +10:00
2018-07-08 22:27:05 +10:00
2017-07-31 18:35:40 +10:00
2018-03-17 00:31:40 +11:00
2018-03-17 00:31:40 +11:00
2017-07-31 18:35:40 +10:00
2018-05-21 12:49:56 +10:00
2018-08-02 14:17:24 +10:00
2018-07-10 16:33:57 +10:00
2018-07-10 16:33:57 +10:00
2017-07-31 18:35:40 +10:00
2017-10-04 12:37:50 +11:00
2018-04-05 01:11:26 +10:00
2018-04-05 01:11:26 +10:00
2018-03-02 11:01:24 +11:00
2017-09-18 00:06:43 +03:00
2017-10-24 22:39:36 +11:00
2018-03-13 14:03:15 +11:00
2017-07-31 18:35:40 +10:00
2018-07-08 22:27:39 +10:00
2018-02-20 17:56:58 +11:00
2017-11-20 09:30:06 +02:00
2017-11-20 09:30:06 +02:00
2017-10-04 12:37:50 +11:00
2017-12-12 16:53:44 +11:00
2017-10-04 12:37:50 +11:00
2017-10-04 12:37:50 +11:00
2018-02-14 23:17:06 +11:00
2017-10-04 12:37:50 +11:00
2017-12-19 14:01:19 +11:00
2017-10-04 12:37:50 +11:00
2017-10-04 12:37:50 +11:00
2018-07-30 12:46:47 +10:00
2017-11-16 13:53:04 +11:00
2018-04-10 13:41:32 +10:00
2017-06-09 17:33:01 +03:00
2018-02-14 18:19:22 +11:00
2017-10-04 12:37:50 +11:00
2017-08-29 13:16:30 +10:00
2018-08-02 14:17:24 +10:00
2018-06-08 11:48:25 +10:00
2017-07-31 18:35:40 +10:00
2017-11-24 14:48:23 +11:00
2017-12-29 13:44:26 +11:00
2017-07-31 18:35:40 +10:00
2018-06-12 12:17:43 +10:00
2017-07-31 18:35:40 +10:00
2017-07-31 18:35:40 +10:00
2017-07-31 18:35:40 +10:00
2018-02-14 18:41:17 +11:00
2016-11-16 18:13:50 +11:00
2018-09-08 00:07:23 +10:00
2018-02-19 00:26:14 +11:00
2017-12-11 13:49:09 +11:00
2017-11-29 17:01:39 +11:00
2018-02-19 16:12:44 +11:00
2018-02-19 00:26:14 +11:00
2018-06-06 14:28:23 +10:00
2017-03-14 11:52:05 +11:00
2018-05-18 15:23:02 +10:00
2017-07-31 18:35:40 +10:00
2017-07-18 11:57:39 +10:00
2018-09-13 22:47:20 +10:00
2017-12-05 00:38:41 +02:00
2018-09-11 00:38:31 +10:00
2018-09-13 22:47:20 +10:00
2017-03-20 15:20:26 +11:00
2017-07-31 18:35:40 +10:00
2017-07-31 18:35:40 +10:00
2017-10-04 12:37:50 +11:00
2017-10-05 10:49:44 +11:00
2017-07-31 18:35:40 +10:00
2017-12-11 22:39:12 +11:00
2017-12-11 13:49:09 +11:00
2017-07-31 18:35:40 +10:00
2018-08-14 17:36:08 +10:00
2018-08-14 17:36:08 +10:00
2018-02-14 18:19:22 +11:00
2017-09-06 16:43:09 +10:00
2018-09-03 13:08:16 +10:00
2017-10-05 10:49:44 +11:00
2017-09-21 20:29:41 +10:00
2017-09-26 11:59:11 +10:00