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

Utility functions for working with JS values. More...

#include "js_runner_types.h"
#include <furi/core/string.h>
#include <toolbox/sized_buffer.h>

Go to the source code of this file.

Macros

#define JS_CHECK_ARGS_COUNT(n)
 
#define JS_CHECK_INSTANCE()
 
#define JS_CHECK_CONSTRUCTOR()
 
#define JS_CHECK_ARG_IS_FUNCTION(arg)
 
#define JS_CHECK_ARG_IS_STRING(arg)
 

Functions

void js_check_and_free (jerry_value_t value)
 Check if value is not an exception and free it.
 
void js_set_constructor_prototype (jerry_value_t constructor, jerry_value_t prototype)
 Set a prototype object to be used with a constructor.
 
jerry_value_t js_object_construct (const char *name, const jerry_value_t args[], const jerry_length_t args_count)
 Construct an object given the class name.
 
void js_set_property (jerry_value_t object, const char *name, jerry_value_t property)
 Set a property in a JS object.
 
void js_set_method (jerry_value_t object, const char *name, jerry_external_handler_t handler)
 Set a method in a JS object.
 
void js_set_method_sym (jerry_value_t object, jerry_well_known_symbol_t key, jerry_external_handler_t handler)
 Set a method in a JS object using a well-known symbol as a key.
 
void js_set_property_sym (jerry_value_t object, jerry_well_known_symbol_t key, jerry_value_t property)
 Set a property in a JS object using a well-known symbol as a key.
 
void js_set_property_getset (jerry_value_t object, const char *name, jerry_external_handler_t getter, jerry_external_handler_t setter)
 Set a property defined by its getter and setter in a JS object.
 
bool js_object_has_property (jerry_value_t object, const char *key)
 Test if an object has a property with given name.
 
jerry_value_t js_object_get_nested_property (jerry_value_t object, const char *const keys[], size_t nesting_count)
 Get a value of a property nested under several layers.
 
jerry_value_t js_iterator_result (bool done, jerry_value_t value)
 Create a return value of an Iterator's next() method.
 
char * js_string_to_c_string (jerry_value_t value)
 If value is a JS string, return its UTF8 representation.
 
char * js_value_to_c_string (jerry_value_t value)
 Convert any value to a character string.
 
FuriString * js_string_to_furi_string (jerry_value_t value)
 If value is a JS string, return its UTF8 representation.
 
jerry_value_t js_utf8_string (const FuriString *s)
 Create a JS string by decoding a UTF-8 FuriString.
 
bool js_value_to_integer (jerry_value_t value, int *result)
 Convert a JS value to an integer.
 
void js_copy_property (jerry_value_t dst, jerry_value_t src, const char *key)
 Copy a property (if it exists) from one object to another.
 
bool js_is_instance_of (jerry_value_t obj, const char *constructor_name)
 Check if an object is an instance of a given constructor (JS instanceof operation).
 
jerry_value_t js_rejected_promise (const char *msg)
 Create and reject a Promise with a message.
 
jerry_value_t js_rejected_promise_from_exception (jerry_value_t exception)
 Create and reject a Promise with a message extracted from an exception.
 
void js_reject_promise_with_exception (jerry_value_t promise, jerry_value_t exception)
 Reject a Promise with a message extracted from an exception.
 
bool js_exception_is_null (jerry_value_t exception)
 Check if the exception holds a null as the value.
 
FuriString * js_get_exception_string (jerry_value_t exception)
 Create a string out of a JS exception.
 
void js_log_exception (const char *tag, const char *msg, jerry_value_t exception)
 Log exception message with ERROR severity.
 
jerry_value_t js_arraybuffer_from_byte_array (ByteArray_t *array)
 Create an external ArrayBuffer referencing ByteArray's data.
 
jerry_value_t js_arraybuffer_from_sized_buffer (SizedBuffer buffer)
 Create an external ArrayBuffer referencing SizedBuffer's data.
 

Detailed Description

Utility functions for working with JS values.

Macro Definition Documentation

◆ JS_CHECK_ARG_IS_FUNCTION

#define JS_CHECK_ARG_IS_FUNCTION ( arg)
Value:
do { \
if(!jerry_value_is_function(arg)) { \
return jerry_throw_sz(JERRY_ERROR_TYPE, "Function argument required"); \
} \
} while(false)

◆ JS_CHECK_ARG_IS_STRING

#define JS_CHECK_ARG_IS_STRING ( arg)
Value:
do { \
if(!jerry_value_is_string(arg)) { \
return jerry_throw_sz(JERRY_ERROR_TYPE, "String argument required"); \
} \
} while(false)

◆ JS_CHECK_ARGS_COUNT

#define JS_CHECK_ARGS_COUNT ( n)
Value:
do { \
if(args_count < n) { \
return jerry_throw_sz(JERRY_ERROR_TYPE, "At least " #n " arguments required"); \
} \
} while(false)

◆ JS_CHECK_CONSTRUCTOR

#define JS_CHECK_CONSTRUCTOR ( )
Value:
do { \
if(!jerry_value_is_object(call_info->this_value)) { \
return jerry_throw_sz( \
JERRY_ERROR_TYPE, "Class constructor cannot be invoked without 'new'"); \
} \
} while(false)

◆ JS_CHECK_INSTANCE

#define JS_CHECK_INSTANCE ( )
Value:
do { \
if(!instance) { \
return jerry_throw_sz(JERRY_ERROR_TYPE, "Invalid \"this\" value"); \
} \
} while(false)

Function Documentation

◆ js_arraybuffer_from_byte_array()

jerry_value_t js_arraybuffer_from_byte_array ( ByteArray_t * array)

Create an external ArrayBuffer referencing ByteArray's data.

Ownership of the ByteArray is transferred to jerryscript.

Parameters
arraypointer to ByteArray (heap-allocated).
Returns
a JS ArrayBuffer object.

◆ js_arraybuffer_from_sized_buffer()

jerry_value_t js_arraybuffer_from_sized_buffer ( SizedBuffer buffer)

Create an external ArrayBuffer referencing SizedBuffer's data.

Ownership of the buffer is transferred to jerryscript.

Parameters
bufferdata to build an ArrayBuffer from.
Returns
a JS ArrayBuffer object.

◆ js_check_and_free()

void js_check_and_free ( jerry_value_t value)

Check if value is not an exception and free it.

Parameters
valuevalue to check and free

◆ js_exception_is_null()

bool js_exception_is_null ( jerry_value_t exception)

Check if the exception holds a null as the value.

Parameters
exceptionJS exception. This value is not freed.
Returns
true if the exception holds null, false otherwise

◆ js_get_exception_string()

FuriString * js_get_exception_string ( jerry_value_t exception)

Create a string out of a JS exception.

Parameters
exceptionJS exception. This value is not freed.
Returns
exception string or NULL if conversion failed.

◆ js_iterator_result()

jerry_value_t js_iterator_result ( bool done,
jerry_value_t value )

Create a return value of an Iterator's next() method.

The object has two properties: done and value.

Parameters
donevalue for the done property
valuecontents of the value property (freed).

◆ js_object_construct()

jerry_value_t js_object_construct ( const char * name,
const jerry_value_t args[],
const jerry_length_t args_count )

Construct an object given the class name.

args can be NULL if the constructor takes no arguments, in which case args_count must be 0.

Parameters
nameclass name as a zero-terminated string
argsarray of arguments to pass to the constructor
args_countnumber of arguments passed to the constructor
Returns
constructed object value

◆ js_object_get_nested_property()

jerry_value_t js_object_get_nested_property ( jerry_value_t object,
const char *const keys[],
size_t nesting_count )

Get a value of a property nested under several layers.

Example: get object.foo.bar.baz

const char* const keys[] = {"foo", "bar", "baz"}; jerry_value_t baz_value = js_object_get_nested_property(object, keys, 3);

Parameters
objectroot object.
keysarray of property names for each nesting level.
nesting_countnumber of entries in the keys array.
Returns
the value of the nested property or undefined if any property lookup failed. Exceptions thrown by any member lookup are propagated.

◆ js_reject_promise_with_exception()

void js_reject_promise_with_exception ( jerry_value_t promise,
jerry_value_t exception )

Reject a Promise with a message extracted from an exception.

Parameters
promisepromise (freed)
exceptionexception (freed)

◆ js_rejected_promise_from_exception()

jerry_value_t js_rejected_promise_from_exception ( jerry_value_t exception)

Create and reject a Promise with a message extracted from an exception.

Parameters
exceptionexception (freed)

◆ js_set_constructor_prototype()

void js_set_constructor_prototype ( jerry_value_t constructor,
jerry_value_t prototype )

Set a prototype object to be used with a constructor.

Parameters
constructorConstructor function (not freed)
prototypePrototype value to set (freed)

◆ js_set_method()

void js_set_method ( jerry_value_t object,
const char * name,
jerry_external_handler_t handler )

Set a method in a JS object.

Parameters
objectObject (not freed)
nameMethod name
handlerhandler Method handler

◆ js_set_method_sym()

void js_set_method_sym ( jerry_value_t object,
jerry_well_known_symbol_t key,
jerry_external_handler_t handler )

Set a method in a JS object using a well-known symbol as a key.

Parameters
objectObject (not freed)
keyMethod name
handlerhandler Method handler

◆ js_set_property()

void js_set_property ( jerry_value_t object,
const char * name,
jerry_value_t property )

Set a property in a JS object.

Parameters
objectObject (not freed)
nameProperty name
propertyProperty value to set (freed)

◆ js_set_property_getset()

void js_set_property_getset ( jerry_value_t object,
const char * name,
jerry_external_handler_t getter,
jerry_external_handler_t setter )

Set a property defined by its getter and setter in a JS object.

Parameters
objectObject (not freed)
nameProperty name
gettergetter callback (can be NULL)
settergetter callback (can be NULL)

◆ js_set_property_sym()

void js_set_property_sym ( jerry_value_t object,
jerry_well_known_symbol_t key,
jerry_value_t property )

Set a property in a JS object using a well-known symbol as a key.

Parameters
objectObject (not freed)
keyProperty name
propertyProperty value to set (freed)

◆ js_string_to_c_string()

char * js_string_to_c_string ( jerry_value_t value)

If value is a JS string, return its UTF8 representation.

Returns
a heap-allocated string or NULL if value is not a JS string.

◆ js_string_to_furi_string()

FuriString * js_string_to_furi_string ( jerry_value_t value)

If value is a JS string, return its UTF8 representation.

Returns
a new string or NULL if value is not a JS string.

◆ js_utf8_string()

jerry_value_t js_utf8_string ( const FuriString * s)

Create a JS string by decoding a UTF-8 FuriString.

Parameters
ssource string
Returns
JS string

◆ js_value_to_c_string()

char * js_value_to_c_string ( jerry_value_t value)

Convert any value to a character string.

If the value is already a string, it is used as-is, otherwise it will be converted using the toString method.

Returns
a heap-allocated string or NULL if an exception has occurred.

◆ js_value_to_integer()

bool js_value_to_integer ( jerry_value_t value,
int * result )

Convert a JS value to an integer.

Usual JS rules are applied.

Parameters
valueThe value.
[out]resultResult placeholder.
Returns
true if conversion was successful, false if value could not be converted to an integer.