Using micropython as a scripting API #9607
-
I want to write a program that can run arbitrary user-written code using APIs exposed from the program (things that micropython code can call to interact with it). For these security reasons, I'd want to run the code directly as bytecode, and only run a set number of instructions before stopping each iteration. Native code, hardware interaction libraries and other things would have to be removed. Is this possible without significant modification to the library (as a port)? (this is for a game engine) |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 12 replies
-
@AlexApps99 Yes this is definitely possible. Probably the only thing that isn't supported is running a fixed number of instructions, although this is something you could add. Are you running this on a PC or on an embedded microcontroller? What language is your game engine written in? |
Beta Was this translation helpful? Give feedback.
-
Basically what you want is what is being implemented in #9529 which is a way to embed MicroPython into an existing piece of C/C++ software. We plan to add more examples and hopefully merge it soon, but there should be enough to play around with. Regarding the fixed number of instructions, I realised you can actually do something very close to this using the VM loop hook: https://github.com/micropython/micropython/blob/master/py/mpconfig.h#L588 -- basically you can have the VM run your custom function run after jump instructions and you can implement your own counter and raise an exception as necessary. Obviously not quite the same as every instruction, but should solve the same problem? |
Beta Was this translation helpful? Give feedback.
-
As a side note, the “run a fixed/bounded number of instructions” thing could also be useful in the WebAssembly port, where I’m currently struggling with the lack of that. JavaScript has a tendency to insist that user code run in short chunks and return to the event loop in between, while code written for microcontrollers (in MicroPython or not) tends to be infinitely-long-running. I thought I could get around that by running MicroPython in a web worker thread, but no, web workers don't deal with infinite loops well either because the message queues needed to communicate to them also work by delivering events and cannot be polled from deep in a user-code callstack, as the VM loop hook would allow. (Emscripten has an “asyncify” mechanism to have the C code suspended deep in a call stack and still return to the event loop on the JavaScript side, but I haven’t gotten it to work reliably with the VM loop hook yet.) |
Beta Was this translation helpful? Give feedback.
Basically what you want is what is being implemented in #9529 which is a way to embed MicroPython into an existing piece of C/C++ software.
We plan to add more examples and hopefully merge it soon, but there should be enough to play around with.
Regarding the fixed number of instructions, I realised you can actually do something very close to this using the VM loop hook: https://github.com/micropython/micropython/blob/master/py/mpconfig.h#L588 -- basically you can have the VM run your custom function run after jump instructions and you can implement your own counter and raise an exception as necessary. Obviously not quite the same as every instruction, but should solve the same problem?