Javascript port - interrupts

Discussion and questions about boards that can run MicroPython but don't have a dedicated forum.
Target audience: Everyone interested in running MicroPython on other hardware.
Post Reply
Posts: 1
Joined: Tue Mar 26, 2019 10:50 pm

Javascript port - interrupts

Post by shpadoinkle » Fri Oct 30, 2020 1:40 pm

Hi guys,

I've been working on a project, where I want to run Micropython in JS. I stumbled upon the following port: ... javascript

Which seems to work out great. I did implement some further functionality through emscripten, so that I can call JS from a MicroPython script:


Code: Select all

import js
js.exec("Some string")

Code: Select all = (data) => {
	return data

Code: Select all

EM_JS(char *, callback, (const char* str), {
    if(!Module.callback) Module.callback = {};
    var returnVal = Module.callback['call'](UTF8ToString(str));
    if(!returnVal) return 0;
    const byteCount = (Module.lengthBytesUTF8(returnVal) + 1);

    const returnValPtr = Module._malloc(byteCount);
    Module.stringToUTF8(returnVal, returnValPtr, byteCount);

    return returnValPtr;

STATIC mp_obj_t js_exec(mp_obj_t code) {
    char * result = callback(mp_obj_str_get_str(code));

    return mp_obj_new_str(result, strlen(result));
STATIC MP_DEFINE_CONST_FUN_OBJ_1(js_exec_obj, js_exec);
Now, I can call JS from Python, great. What I intend to do next, is to call Python from JS. I could use the mp_js_do_str method, which would work fine. However, if I wanted to emulate some sensor data, and the user writes something like:

Code: Select all

sensorData = 10

while sensorData < 50:
I would not be able to update the sensorData with mp_js_do_str("sensorData = 60"), because the loop is blocking.

Now, how do I update sensorData if the user has written some blocking code?

My initially idea, was to write a method in main.c, that performs an interrupt. I've been looking in various other ports, to see how that would work out, but without any luck.

Post Reply