There are already libraries/frameworks that have supported this:

* espruino (https://www.espruino.com/)

* elk (https://github.com/cesanta/elk)

* DeviceScript (Microsoft Research's now defunct effort, https://github.com/microsoft/devicescript)

And also Duktape (https://duktape.org)