BSB Developer Docs
Loading...
Searching...
No Matches
js_runner.h File Reference

Javascript app runner. More...

#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include <core/pubsub.h>

Go to the source code of this file.

Data Structures

struct  JsRunnerContextInitResult
 
struct  JsRunnerRunResult
 
struct  JsRunnerEvent
 

Macros

#define RECORD_JS_RUNNER   "js_runner"
 

Typedefs

typedef struct JsRunner JsRunner
 
typedef enum JsRunnerError JsRunnerError
 
typedef enum JsRunnerFatal JsRunnerFatal
 
typedef enum JsRunnerConsoleSeverity JsRunnerConsoleSeverity
 
typedef enum JsRunnerConsoleSeparator JsRunnerConsoleSeparator
 
typedef void(* JsRunnerConsoleOutCallback) (JsRunnerConsoleSeverity severity, const char *buf, size_t size, JsRunnerConsoleSeparator separator, void *context)
 
typedef struct JsRunnerContextHandle JsRunnerContextHandle
 
typedef struct JsRunnerContextInitResult JsRunnerContextInitResult
 
typedef struct JsRunnerExecutionHandle JsRunnerExecutionHandle
 
typedef struct JsRunnerRunResult JsRunnerRunResult
 
typedef void(* JsRunnerEventCallback) (const JsRunnerEvent *event, void *context)
 

Enumerations

enum  JsRunnerError {
  JsRunnerErrorNone = 0 , JsRunnerErrorUnknown , JsRunnerErrorFilesystem , JsRunnerErrorOutOfMemory ,
  JsRunnerErrorParseException , JsRunnerErrorInvalidAppId , JsRunnerErrorResource , JsRunnerErrorTimeout ,
  JsRunnerErrorMax
}
 
enum  JsRunnerFatal { JsRunnerFatalGeneric , JsRunnerFatalOutOfMemory }
 
enum  JsRunnerConsoleSeverity { JsRunnerConsoleSeverityLog , JsRunnerConsoleSeverityInfo , JsRunnerConsoleSeverityWarn , JsRunnerConsoleSeverityError }
 
enum  JsRunnerConsoleSeparator { JsRunnerConsoleSeparatorNone , JsRunnerConsoleSeparatorSpace , JsRunnerConsoleSeparatorNewline }
 
enum  JsRunnerEventType { JsRunnerEventTypeScriptStarted , JsRunnerEventTypeScriptFinished }
 

Functions

JsRunnerContextInitResult js_runner_context_alloc (JsRunner *instance, const char *app_id, size_t heap_size, JsRunnerConsoleOutCallback console_write_cb, void *console_write_context)
 Allocate a Javascript execution context.
 
void js_runner_context_free (JsRunnerContextHandle *handle)
 Free a Javascript execution context.
 
JsRunnerRunResult js_runner_run (JsRunnerContextHandle *handle, const char *path, JsRunnerEventCallback event_callback, void *context)
 Run a JS application.
 
JsRunnerRunResult js_runner_run_snippet (JsRunnerContextHandle *handle, const char *code, bool print_result, JsRunnerEventCallback event_callback, void *context)
 Run a JS code snippet.
 
JsRunnerError js_runner_join (JsRunnerExecutionHandle *handle, uint32_t timeout)
 Wait until JS run job completes.
 
void js_runner_abort (JsRunnerExecutionHandle *handle)
 Forcefully terminate a running JS application.
 
void js_runner_abort_all (JsRunner *instance)
 Forcefully terminate all running JS applications.
 
const char * js_runner_get_error_message (JsRunnerError error)
 Get human-readable error message corresponding to an error code.
 
FuriPubSub * js_runner_get_fatal_pubsub (JsRunner *instance)
 Get a pubsub which is triggered on jerryscript fatal errors.
 

Detailed Description

Javascript app runner.

Function Documentation

◆ js_runner_abort()

void js_runner_abort ( JsRunnerExecutionHandle * handle)

Forcefully terminate a running JS application.

The handle must be joined afterwards.

Parameters
handleexecution handle of the running script.

◆ js_runner_abort_all()

void js_runner_abort_all ( JsRunner * instance)

Forcefully terminate all running JS applications.

Parameters
instanceJsRunner instance. Can be obtained with furi_record_open().

◆ js_runner_context_alloc()

JsRunnerContextInitResult js_runner_context_alloc ( JsRunner * instance,
const char * app_id,
size_t heap_size,
JsRunnerConsoleOutCallback console_write_cb,
void * console_write_context )

Allocate a Javascript execution context.

Parameters
instanceJsRunner instance. Can be obtained with furi_record_open().
app_idJS application ID (see js_app_launcher).
heap_sizeJS heap size for the app in bytes.
console_write_cbcallback function for JS console methods (console.log, console.error, console.info). Supply NULL to disable console.
console_write_contextuser pointer passed to console_write_cb.
Returns
operation result. If error is JsRunnerErrorNone, handle is valid.

◆ js_runner_context_free()

void js_runner_context_free ( JsRunnerContextHandle * handle)

Free a Javascript execution context.

Any execution handles obtained with this context handle must be joined before calling this function.

Parameters
handlecontext handle to be freed.

◆ js_runner_get_error_message()

const char * js_runner_get_error_message ( JsRunnerError error)

Get human-readable error message corresponding to an error code.

Parameters
errorerror code
Returns
error message

◆ js_runner_get_fatal_pubsub()

FuriPubSub * js_runner_get_fatal_pubsub ( JsRunner * instance)

Get a pubsub which is triggered on jerryscript fatal errors.

Parameters
instanceJsRunner instance. Can be obtained with furi_record_open().
Returns
the pubsub. Pubsub messages can be casted into a pointer to JsRunnerFatal.

◆ js_runner_join()

JsRunnerError js_runner_join ( JsRunnerExecutionHandle * handle,
uint32_t timeout )

Wait until JS run job completes.

This function blocks until the script terminates. To forcefully terminate the script use js_runner_abort().

Parameters
handleexecution handle previously created by js_runner_run or js_runner_run_snippet.
timeoutoperation timeout in ticks.
Returns
operation result (JsRunnerErrorTimeout or JsRunnerErrorNone).

◆ js_runner_run()

JsRunnerRunResult js_runner_run ( JsRunnerContextHandle * handle,
const char * path,
JsRunnerEventCallback event_callback,
void * context )

Run a JS application.

Parameters
handlecontext handle previously created by js_runner_context_alloc.
pathentry point script path.
event_callbackfunction to be called when an event occurs.
contextuser pointer passed to the event_callback function.
Returns
operation result. If error is JsRunnerErrorNone, handle is valid.

◆ js_runner_run_snippet()

JsRunnerRunResult js_runner_run_snippet ( JsRunnerContextHandle * handle,
const char * code,
bool print_result,
JsRunnerEventCallback event_callback,
void * context )

Run a JS code snippet.

Parameters
handlecontext handle previously created by js_runner_context_alloc.
codeJS code (encoding: UTF-8).
print_resultif true, evaluation result of the code snippet is printed using the console callback (severity: log).
event_callbackfunction to be called when an event occurs.
contextuser pointer passed to the on_terminate function.
Returns
operation result. If error is JsRunnerErrorNone, handle is valid.