Re:mJS – A new approach to embedded scripting
by drozd on Wednesday January 25, 2017 @01:57PM
(#13484668)
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
by geofft on Wednesday January 25, 2017 @02:01PM
(#13484720)
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.
How can you do the following in mJS?
There are some things you can do in C without functions. How does mJS achieve this?