Smart.js is an embeddable JavaScript engine for C/C++. It is specifically suited embedded environments with constrained resources, like smart devices, telemetry probes, etс. JavaScript interface makes development extremely fast and effortless. Smart.js features include:
- Network, File, Database, Crypto, OS API
- Simple and powerful V7 C/C++ API allows to easily export existing C/C++ functionality into JavaScript
- Industry-standard security: native SSL/TLS support
- Tiny footpint of V7 Embedded JavaScript Engine and Net Skeleton makes Smart.js fit the most constrained environments
- Smart.js is cross-platform and works on Windows, MacOS, iOS, UNIX/Linux, Android, QNX, eCos and many other platforms
On MacOS or UNIX/Linux, start terminal and execute following commands:
$ git clone https://github.com/cesanta/Smart.js.git
$ cd Smart.js
$ make run
while (true) {
var val = MyApp.getTemperature(); // Do some measurement
var msg = JSON.stringify({ temperature: val }); // Wrap it to JSON
Net.connect_and_send('ssl://mysite.com:443',
'POST /api/data HTTP/1.0\n\n', msg); // Send it
Std.sleep(10); // Sleep 10 seconds before the next cycle
}
var em = Net.EventManager(); // Start async event manager
var ws = Net.HttpServer(); // Create web server instance
var sock = Net.bind('ssl://443:cert.pem'); // Create SSL listening socket
Net.ns_add(em, sock, ws); // Attach web server to the listening socket
while (true) {
Net.ns_poll(em, 10); // Event loop. Poll interval is 10 seconds
// Iterate over all websocket connections, push measurement to each
var temperature = MyApp.getTemperature();
Net.ns_broadcast(em, ws, function(conn) {
if (conn.is_websocket) conn.send_websocket(temperature);
});
}
var em = Net.EventManager()
var ws = Net.HttpServer({ document_root: '/var/www' });
var sock = Net.bind('tcp://80')
Net.ns_add(em, sock, ws)
while (true) {
Net.ns_poll(em, 10)
}
- Crypto.md5(str) -> (string) hash
- Calculates and returns 16-byte MD5 hash of a string parameter `str`
- Crypto.md5_hex(str) -> (string) hex_hash
- Calculates and returns 32-byte hex-stringified MD5 hash of a string parameter `str`
- Crypto.sha1(str) -> (string) hash
- Calculates and returns 20-byte SHA-1 hash of a string parameter `str`
- Crypto.sha1_hex(str) -> (string) hex_hash
- Calculates and returns 40-byte hex-stringified SHA-1 hash of a string parameter `str`
- Sqlite3.exec(sql, param1, ..., callback) -> (number) last_insert_id
-
Executes SQL statement with given parameters,
calling callback function for each row. Return: last insert ID
Example: `var id = Sqlite3.exec('INSERT INTO log (msg) VALUES(?);', msg)` - File.fopen(file_name, mode) -> (number) file_stream
- Opens a file `file_name` with given `mode`. Valid values for `mode` are the same as for the [fopen](http://www.unix.com/man-page/posix/3/fopen/) POSIX call. Return value: numeric file stream, or negative OS error.
- File.fwrite(fp, data_str) -> (number) bytes_written
- Writes dat to a file stream. Return value: number of bytes written.
- File.fread(fp, num_bytes) -> (string) data
- Reads a data from file stream. Return: string with data. Upon EOF, empty string is returned.
- File.fclose(fp)
- Closes opened file stream. Return value: none. Note: failure to close will leak file descriptors.
- File.fseek(fp, offset, whence) -> (number) file_position
- Sets file position. Valid numeric values for `whence` are the same as for the `fseek` POSIX call. Return: result numeric position on success, or negative OS error code on failure.
- Std.exec(binary_path, arg1, arg2, ...) -> (number) exit_code
- Executes a given binary with arguments. Returns numeric exit code.
- Std.sleep(seconds)
- Sleeps for a given number of seconds. Seconds could be fractional, e.g. `0.1` for 1/10 of a second. Return value: none.
- Net.bind(address) -> (number) listening_socket
- Creates and returns a listening socket bound to `address`. Format of `address` string is as follows: `[PROTO://][IP:]PORT[:SSL_CERT][CA_CERT]`. PROTO could be `tcp://`, `udp://` or `ssl://`. If omitted, then `tcp://` is assumed by default. IP could be an IP address or host name, in which case listening socket will be bound to a specific interface. If IP is omitted, then socket is bound to `INADDR_ANY`. PORT is a port number. For `ssl://` protocol, SSL_CERT specifies server certificate, which should be a file in PEM format with both SSL certificate and private key file concatenated. If CA_CERT is specified, then server will request client certificate (so called two-way SSL).
- Net.connect(address) -> (number) connected_socket
- Creates and returns a client socket to a specified address. The format of `address` is the same as for the `Net.bind()` function, where IP address is required. IP could be a host name. Optional SSL_CERT is a client certificate, in case if server configured for two-way SSL. If CA_CERT is specified, it is a CA certificate to verify server's identity.
- Net.send(sock, data) -> (number) num_bytes
- Writes data to a socket. Returns number of bytes written, or negative OS error in case of error.
- Net.recv(sock, num_bytes) -> (string) data
- Reads not more then `num_bytes` bytes of data data from a socket. Return: a string, an empty string on error.
- Net.connect_and_send(address, data) -> (number) num_bytes
- Wrapper around `Net.connect()` + `Net.send()` and `Net.close()`. Connects to `address` and sends `data` there. This function blocks until all requested data is sent to the `address` or error occurs. Return: either total number of bytes sent, or negative OS error.
- Net.close(sock)
- Closes opened socket.
- Net.EventManager() -> (object) event_manager
- Creates an asyncronous non-blocking event manager.
- Net.ns_poll(em, poll_timeout) -> (number) current_timestamp
- Performs one iteration of the EventManager event loop, calling IO functions when necessary. Return: current timestamp.
This software is released under commercial and GNU GPL v.2 open source licenses. The GPLv2 open source License does not generally permit incorporating this software into non-open source programs. For those customers who do not wish to comply with the GPLv2 open source license requirements, Cesanta Software offers a full, royalty-free commercial license and professional support without any of the GPL restrictions.