[python] Enable PYSTACK for emscripten platform: this "fixes" bug

probably due to gc_collect (we suspect that transpiled C does not have
access to javascript variables preventing it from collecting all
required roots and leading to deleting objects prematuraly). Enabling
PyStack reduces the use of the heap and fixes the bug.
This commit is contained in:
Émilie Feral
2020-01-31 11:44:57 +01:00
committed by Léa Saviot
parent 58acedfd34
commit de74aa7e59
3 changed files with 28 additions and 0 deletions

View File

@@ -136,6 +136,9 @@ Q(<genexpr>)
Q(<string>)
Q(<stdin>)
Q(utf-8)
#if __EMSCRIPTEN__
Q(pystack exhausted)
#endif
Q(ArithmeticError)
Q(AssertionError)
Q(AttributeError)
@@ -192,6 +195,9 @@ Q(__lt__)
Q(__main__)
Q(__module__)
Q(__name__)
#if __EMSCRIPTEN__
Q(__ne__)
#endif
Q(__new__)
Q(__next__)
Q(__path__)
@@ -355,6 +361,10 @@ Q(pop)
Q(popitem)
Q(pow)
Q(print)
#if __EMSCRIPTEN__
Q(pystack_space_exhausted)
Q(pystack_use)
#endif
Q(radians)
Q(randint)
Q(random)

View File

@@ -5,6 +5,19 @@
/* MicroPython configuration options
* We're not listing the default options as defined in mpconfig.h */
#if __EMSCRIPTEN__
// Enable a PyStack where most objects are allocated instead of always using the heap
/* This enables to allocate and free memory in a scope (thus, Python can call
* Python) but also has the collateral effect of removing bugs regarding
* garbage collection on the web simulator. Indeed, fewer objetcts are
* allocated on the heap and the garbage collection is less frequently called.
* We suspect that garbage collection failed in javascript because when
* collecting roots the transpiled C code is denied access to Javascript
* variables that can store pointers to the Python heap. The pointed objects
* are therefore erased prematurely. */
#define MICROPY_ENABLE_PYSTACK (1)
#endif
// Maximum length of a path in the filesystem
#define MICROPY_ALLOC_PATH_MAX (32)

View File

@@ -102,6 +102,11 @@ extern "C" {
}
void MicroPython::init(void * heapStart, void * heapEnd) {
#if __EMSCRIPTEN__
static mp_obj_t pystack[1024];
mp_pystack_init(pystack, &pystack[MP_ARRAY_SIZE(pystack)]);
#endif
volatile int stackTop;
void * stackTopAddress = (void *)(&stackTop);
/* We delimit the stack part that will be used by Python. The stackTop is the