StaticArrayBuffer
and StaticBuffer
Hello World Example
Print "Hello World"
to console, we do it by executing write
system call to 1
file
descriptor which is STDOUT
:
var StaticBuffer = StaticBuffer; var sbuf = StaticBuffer; // r - readable, w - writable, e - executable sbuf; // Hello World!
How it works
StaticArrayBuffer
and StaticBuffer
extend ArrayBuffer
and Buffer
classes and
provide such extra functionality:
- Allocate executable memory.
- Execute the machine code in
StaticBuffer
using.call()
method. - Change protection to backing memory.
- Promise that actual data in memory will never be moved by runtime.
Memory protection flags:
'r'
- readable'w'
- writable'e'
- executable
Usage
var StaticArrayBuffer = StaticArrayBuffer;var StaticBuffer = StaticArrayBuffer;
StaticArrayBuffer
inherits from ArrayBuffer
and provides the following
extra functionality:
var sab = 1024 'rwe';StaticArrayBuffer; // true // Call mathine code stored in `StaticArrayBuffer` at offset `offset`,// providing `number[]` arguments to that machine code using standard// calling conventions supported by your system, currently supports // up to 10 arguments.var offset = 0;var args = 1 2 3;sab; // Change protection of the memory block.sab; // Get the actual address pointer of the memory that backs the `StaticArrayBuffer`.offset = 0;var addr = sab; // [number, number] // Free the memory of the `StaticArrayBuffer`, after that your `StaticArrayBuffer`// should not be used.sab;sab = null;
StaticBuffer
inherits from Buffer
and
is backed by StaticArrayBuffer
instead of ArrayBuffer
that Buffer
uses. It provides
the following extra functionality:
var sbuf = 1024 'rwe';// orvar offset = 10 len = 10;sbuf = sab offset len;// or use StaticBuffer.from() syntax StaticBuffer; // true sbuf;sbuf; sbufbuffer;sbuf = null;