Live data from Hacker News

mJS – A new approach to embedded scripting

mongoose-iot.com

1–10 of 61 posts

Re: mJS – A new approach to embedded scripting

#4
Taking this example:

    let malloc = ffi('void *malloc(int)');
    let mem = malloc(10);
How do you manipulate that memory?

How can you do the following in mJS?

    int* mem = malloc(10);
    mem[0] = 3;
    mem[1] = 7;
    mem[2] = 12;
    int *rest = mem + 3;
There are some things you can do in C without functions. How does mJS achieve this?

Re: mJS – A new approach to embedded scripting

#5
post #3

I've never worked with embedded systems, so forgive my ignorance. Why is this any more useful than just writing c code, if all the code that actually interacts with the hardware has to be written in c anyway?

I'm wondering the same thing. What does having a JS syntax bring to embedded programming?

Re: mJS – A new approach to embedded scripting

#6
post #3

I've never worked with embedded systems, so forgive my ignorance. Why is this any more useful than just writing c code, if all the code that actually interacts with the hardware has to be written in c anyway?

One possible use top of my head would be vendor customization. For example Foo Inc is manufacturing some hot IoT widget which is then sold and branded by Bar Ltd. Foo can take care of all the nitty gritty low level details and provide nice high level SDK for Bar to make their customizations.

Re: mJS – A new approach to embedded scripting

#7
post #4

Taking this example: let malloc = ffi('void *malloc(int)'); let mem = malloc(10); How do you manipulate that memory? How can you do the following in mJS? int* mem = malloc(10); mem[0] = 3; mem[1] = 7; mem[2] = 12; int *rest = mem + 3; There are some things you can do in C without functions. How does mJS achieve this?

Currently, mJS FFI is limited to functions only. And, to simple enough functions.

On how to access memory from mJS: write an accessor function!

    // C
    void setmem(uint8_t *ptr, int index, int value) {
      ptr[index] = value;
    }

    // mJS
    let setmem = ffi('void setmem(void *, int, int)');
    let malloc = ffi('void *malloc(int)');
    let mem = malloc(10);
    setmem(mem, 3, 5);

Re: mJS – A new approach to embedded scripting

#8
post #4

Taking this example: let malloc = ffi('void *malloc(int)'); let mem = malloc(10); How do you manipulate that memory? How can you do the following in mJS? int* mem = malloc(10); mem[0] = 3; mem[1] = 7; mem[2] = 12; int *rest = mem + 3; There are some things you can do in C without functions. How does mJS achieve this?

The common use case for doing that in other FFIs is not to use it in your own language, but to pass the pointer to another function. That is, you can translate

    int *buffer = malloc(10);
    SSL_read(ssl, buffer, 10);
into

    let buffer = ffi('void *malloc(int)');
    let SSL_read = ffi('int SSL_read(void *ssl, void *buf, int num)');
    let buffer = malloc(10);
    SSL_read(ssl, buffer, 10);
Your own language generally has ways to allocate memory—but does not necessarily have a way to generate a pointer, or to manually manage the lifetime of that memory.

Re: mJS – A new approach to embedded scripting

#9
post #3

I've never worked with embedded systems, so forgive my ignorance. Why is this any more useful than just writing c code, if all the code that actually interacts with the hardware has to be written in c anyway?

The low-level hardware access is handled using C, but presumably people want to write higher-level code on top of that. It doesn't seem that unusual.

Re: mJS – A new approach to embedded scripting

#10
post #3

I've never worked with embedded systems, so forgive my ignorance. Why is this any more useful than just writing c code, if all the code that actually interacts with the hardware has to be written in c anyway?

The ability to script without the need of rebuilding/reflashing the whole firmware is often fairly useful. The most value is not in JS per se, but in the ability to script.
Post reply on HN