JerryScript types

jerry_init_flag_t

Enum that contains the following elements:

  • JERRY_INIT_EMPTY - empty flag set
  • JERRY_INIT_SHOW_OPCODES - dump byte-code to log after parse
  • JERRY_INIT_SHOW_REGEXP_OPCODES - dump regexp byte-code to log after compilation
  • JERRY_INIT_MEM_STATS - dump memory statistics
  • JERRY_INIT_MEM_STATS_SEPARATE - deprecated, dump memory statistics and reset peak values after parse
  • JERRY_INIT_DEBUGGER - deprecated, an unused placeholder now

Changed in version 2.0: JERRY_INIT_MEM_STATS_SEPARATE and JERRY_INIT_DEBUGGER are now deprecated and not used internally.

jerry_type_t

Enum that contains JerryScript API value types:

  • JERRY_TYPE_NONE - no type information
  • JERRY_TYPE_UNDEFINED - undefined type
  • JERRY_TYPE_NULL - null type
  • JERRY_TYPE_BOOLEAN - boolean type
  • JERRY_TYPE_NUMBER - number type
  • JERRY_TYPE_STRING - string type
  • JERRY_TYPE_OBJECT - object type
  • JERRY_TYPE_FUNCTION - function type
  • JERRY_TYPE_ERROR - error/abort type
  • JERRY_TYPE_SYMBOL - symbol type

New in version 2.0.

jerry_error_t

Possible types of an error:

  • JERRY_ERROR_COMMON - common error
  • JERRY_ERROR_EVAL - eval error
  • JERRY_ERROR_RANGE - range error
  • JERRY_ERROR_REFERENCE - reference error
  • JERRY_ERROR_SYNTAX - syntax error
  • JERRY_ERROR_TYPE - type error
  • JERRY_ERROR_URI - URI error

There is also a special value JERRY_ERROR_NONE which is not an error type this value can only be returned by the jerry_get_error_type.

Changed in version 2.0: The JERRY_ERROR_NONE was added to be used by the jerry_get_error_type method.

jerry_feature_t

Possible compile time enabled feature types:

  • JERRY_FEATURE_CPOINTER_32_BIT - 32 bit compressed pointers
  • JERRY_FEATURE_ERROR_MESSAGES - error messages
  • JERRY_FEATURE_JS_PARSER - js-parser
  • JERRY_FEATURE_MEM_STATS - memory statistics
  • JERRY_FEATURE_PARSER_DUMP - parser byte-code dumps
  • JERRY_FEATURE_REGEXP_DUMP - regexp byte-code dumps
  • JERRY_FEATURE_SNAPSHOT_SAVE - saving snapshot files
  • JERRY_FEATURE_SNAPSHOT_EXEC - executing snapshot files
  • JERRY_FEATURE_DEBUGGER - debugging
  • JERRY_FEATURE_VM_EXEC_STOP - stopping ECMAScript execution
  • JERRY_FEATURE_JSON - JSON support
  • JERRY_FEATURE_PROMISE - promise support
  • JERRY_FEATURE_TYPEDARRAY - Typedarray support
  • JERRY_FEATURE_DATE - Date support
  • JERRY_FEATURE_REGEXP - RegExp support
  • JERRY_FEATURE_LINE_INFO - line info available
  • JERRY_FEATURE_LOGGING - logging
  • JERRY_FEATURE_SYMBOL - symbol support
  • JERRY_FEATURE_DATAVIEW - DataView support

New in version 2.0.

jerry_regexp_flags_t

RegExp object optional flags:

  • JERRY_REGEXP_FLAG_GLOBAL - global match; find all matches rather than stopping after the first match
  • JERRY_REGEXP_FLAG_IGNORE_CASE - ignore case
  • JERRY_REGEXP_FLAG_MULTILINE - multiline; treat beginning and end characters (^ and $) as working over multiple lines (i.e., match the beginning or end of each line (delimited by \n or \r), not only the very beginning or end of the whole input string)

New in version 2.0.

jerry_parse_opts_t

Option bits for jerry_parse and jerry_parse_function functions:

  • JERRY_PARSE_NO_OPTS - no options passed
  • JERRY_PARSE_STRICT_MODE - enable strict mode

New in version 2.0.

jerry_gc_mode_t

Set garbage collection operational mode

  • JERRY_GC_PRESSURE_LOW - free unused objects
  • JERRY_GC_PRESSURE_HIGH - free as much memory as possible

The difference between JERRY_GC_PRESSURE_LOW and JERRY_GC_PRESSURE_HIGH is that the former keeps memory allocated for performance improvements such as property hash tables for large objects. The latter frees all possible memory blocks but the performance may drop after the garbage collection.

New in version 2.0.

jerry_generate_snapshot_opts_t

Flags for jerry_generate_snapshot and jerry_generate_function_snapshot functions:

  • JERRY_SNAPSHOT_SAVE_STATIC - generate static snapshot (see below)
  • JERRY_SNAPSHOT_SAVE_STRICT - strict source code provided

Generate static snapshots Snapshots contain literal pools, and these literal pools contain references to constant literals (strings, numbers, etc.). When a snapshot is executed, these literals are converted to jerry values and the literal pool entries are changed to their corresponding jerry value. To support this conversion, the literals and literal pools are copied into RAM even if the JERRY_SNAPSHOT_EXEC_COPY_DATA option is passed to jerry_exec_snapshot. This non-negligible memory consumption can be avoided by using static snapshots. The literals of these snapshots are limited to magic strings and 28 bit signed integers, so their constant pools do not need to be loaded into the memory. Hence these snapshots can be executed from ROM.

Important note: The jerry_exec_snapshot function rejects static snaphots unless the JERRY_SNAPSHOT_EXEC_ALLOW_STATIC option bit is set. The caller must also ensure that the same magic strings are set by jerry_register_magic_strings when the snapshot is generated and executed. Furthermore the JERRY_SNAPSHOT_EXEC_COPY_DATA option is not allowed.

New in version 2.0.

jerry_exec_snapshot_opts_t

Flags for jerry_exec_snapshot and jerry_load_function_snapshot functions:

  • JERRY_SNAPSHOT_EXEC_COPY_DATA - copy snapshot data into memory (see below)
  • JERRY_SNAPSHOT_EXEC_ALLOW_STATIC - allow executing static snapshots

Copy snapshot data into memory

By default the snapshot buffer is expected to be present in memory until jerry_cleanup is called. For example static const buffers compiled into the application binary satisfy this requirement.

If the snapshot buffer is freed after jerry_exec_snapshot is called the JERRY_SNAPSHOT_EXEC_COPY_DATA must be passed to copy the necessary parts of the snapshot buffer into memory.

The JERRY_SNAPSHOT_EXEC_COPY_DATA option is not allowed for static snapshots.

New in version 2.0.

jerry_char_t

Summary

Jerry’s char value

Prototype

  1. typedef uint8_t jerry_char_t;

jerry_size_t

Summary

Jerry’s size

Prototype

  1. typedef uint32_t jerry_size_t;

jerry_length_t

Summary

Jerry’s length

Prototype

  1. typedef uint32_t jerry_length_t;

jerry_value_t

Summary

JerryScript value can be a boolean, number, null, object, string or undefined. The value has an error flag, that indicates whether is an error or not. Every type has an error flag not only objects. The error flag should be cleared before the value is passed as an argument, otherwise it can lead to a type error. The error objects created by API functions has the error flag set.

Returned and created values by the API functions must be freed with jerry_release_value when they are no longer needed.

Prototype

  1. typedef uint32_t jerry_value_t;

jerry_context_data_manager_t

Summary

Structure that defines how a context data item will be initialized and deinitialized. JerryScript zeroes out the memory for the item by default, and if the init_cb field is not NULL, it will be called with the pointer to the memory as an additional custom initializer. The deinit_cb (if non-NULL) is called during a call to jerry_cleanup () to run any custom deinitialization before the VM has been fully cleaned up. The finalize_cb (if non-NULL) is also called during a call to jerry_cleanup () to run any custom deinitialization after the VM has been fully cleaned up. If bytes_needed field is 0, no buffer is allocated for the manager, callback functions are called with NULL pointer.

Prototype

  1. typedef struct
  2. {
  3. /**
  4. * Callback responsible for initializing a context item, or NULL to zero out the memory. This is called lazily, the
  5. * first time jerry_get_context_data () is called with this manager.
  6. *
  7. * @param [in] data The buffer that JerryScript allocated for the manager. The buffer is zeroed out. The size is
  8. * determined by the bytes_needed field. The buffer is kept alive until jerry_cleanup () is called.
  9. */
  10. void (*init_cb) (void *data);
  11. /**
  12. * Callback responsible for deinitializing a context item, or NULL. This is called as part of jerry_cleanup (),
  13. * right *before* the VM has been cleaned up. This is a good place to release strong references to jerry_value_t's
  14. * that the manager may be holding.
  15. * Note: because the VM has not been fully cleaned up yet, jerry_object_native_info_t free_cb's can still get called
  16. * *after* all deinit_cb's have been run. See finalize_cb for a callback that is guaranteed to run *after* all
  17. * free_cb's have been run.
  18. *
  19. * @param [in] data The buffer that JerryScript allocated for the manager.
  20. */
  21. void (*deinit_cb) (void *data);
  22. /**
  23. * Callback responsible for finalizing a context item, or NULL. This is called as part of jerry_cleanup (),
  24. * right *after* the VM has been cleaned up and destroyed and jerry_... APIs cannot be called any more. At this point,
  25. * all values in the VM have been cleaned up. This is a good place to clean up native state that can only be cleaned
  26. * up at the very end when there are no more VM values around that may need to access that state.
  27. *
  28. * @param [in] data The buffer that JerryScript allocated for the manager. After returning from this callback,
  29. * the data pointer may no longer be used.
  30. */
  31. void (*finalize_cb) (void *data);
  32. /**
  33. * Number of bytes to allocate for this manager. This is the size of the buffer that JerryScript will allocate on
  34. * behalf of the manager. The pointer to this buffer is passed into init_cb, deinit_cb and finalize_cb. It is also
  35. * returned from the jerry_get_context_data () API.
  36. */
  37. size_t bytes_needed;
  38. } jerry_context_data_manager_t;

New in version 2.0.

jerry_context_alloc_t

Summary

Function type for allocating buffer for JerryScript context.

Prototype

  1. typedef void *(*jerry_context_alloc_t) (size_t size, void *cb_data_p);
  • size - allocation size
  • cb_data_p - pointer to user data

New in version 2.0.

jerry_context_t

Summary

An opaque declaration of the JerryScript context structure.

Prototype

  1. typedef struct jerry_context_t jerry_context_t;

New in version 2.0.

jerry_binary_operation_t

Enum that contains the supported binary operation types

  • JERRY_BIN_OP_EQUAL - equal comparison (==)
  • JERRY_BIN_OP_STRICT_EQUAL - strict equal comparison (===)
  • JERRY_BIN_OP_LESS - less relation (<)
  • JERRY_BIN_OP_LESS_EQUAL - less or equal relation (<=)
  • JERRY_BIN_OP_GREATER - greater relation (>)
  • JERRY_BIN_OP_GREATER_EQUAL - greater or equal relation (>=)
  • JERRY_BIN_OP_INSTANCEOF - instanceof operation
  • JERRY_BIN_OP_ADD - addition operator (+)
  • JERRY_BIN_OP_SUB - subtraction operator (-)
  • JERRY_BIN_OP_MUL - multiplication operator (*)
  • JERRY_BIN_OP_DIV - division operator (/)
  • JERRY_BIN_OP_REM - remainder operator (%)

New in version 2.0.

See also

jerry_property_descriptor_t

Summary

Description of ECMA property descriptor. This struct can be used for the jerry_define_own_property method to configure how the property should be registered.

The naming scheme is similar to the JavaScript Object.defineProperty method.

Fields should be used in pairs. That is if the is_value_defined is set to true the value field should contain the value for the property.

Prototype

  1. typedef struct
  2. {
  3. /** Is [[Value]] defined? */
  4. bool is_value_defined;
  5. /** Is [[Get]] defined? */
  6. bool is_get_defined;
  7. /** Is [[Set]] defined? */
  8. bool is_set_defined;
  9. /** Is [[Writable]] defined? */
  10. bool is_writable_defined;
  11. /** [[Writable]] */
  12. bool is_writable;
  13. /** Is [[Enumerable]] defined? */
  14. bool is_enumerable_defined;
  15. /** [[Enumerable]] */
  16. bool is_enumerable;
  17. /** Is [[Configurable]] defined? */
  18. bool is_configurable_defined;
  19. /** [[Configurable]] */
  20. bool is_configurable;
  21. /** [[Value]] */
  22. jerry_value_t value;
  23. /** [[Get]] */
  24. jerry_value_t getter;
  25. /** [[Set]] */
  26. jerry_value_t setter;
  27. } jerry_property_descriptor_t;

See also

jerry_heap_stats_t

Summary

Description of JerryScript heap memory stats. It is for memory profiling.

Prototype

  1. typedef struct
  2. {
  3. size_t version /**< the version of the stats struct */
  4. size_t size; /**< heap total size */
  5. size_t allocated_bytes; /**< currently allocated bytes */
  6. size_t peak_allocated_bytes; /**< peak allocated bytes */
  7. size_t reserved[4]; /**< padding for future extensions */
  8. } jerry_heap_stats_t;

New in version 2.0.

See also

jerry_external_handler_t

Summary

Type of an external function handler

Prototype

  1. typedef jerry_value_t (*jerry_external_handler_t) (const jerry_value_t function_obj,
  2. const jerry_value_t this_val,
  3. const jerry_value_t args_p[],
  4. const jerry_length_t args_count);
  • function_object - the JavaScript function object which was invoked.
  • this_val - the this value provided for the function call.
  • args_p - the function arguments, array of JavaScript values.
  • args_count - the number of arguments.
  • return value

See also

jerry_object_native_free_callback_t

Summary

Native free callback of an object. It is used in jerry_object_native_info_t and for external Array buffers.

Note:

  • Referred values by this method must have at least 1 reference. (Correct API usage satisfies this condition)

Prototype

  1. typedef void (*jerry_object_native_free_callback_t) (void *native_p);

New in version 2.0: Renamed from jerry_object_free_callback_t. Changed in version 2.2: API calls are once again allowed. (See note)

See also

jerry_object_native_info_t

Summary

The type information of the native pointer. It includes the free callback that will be called when associated JavaScript object is garbage collected. It can be left NULL in case it is not needed.

Typically, one would create a static const jerry_object_native_info_t for each distinct C type for which a pointer is used with jerry_set_object_native_pointer () and jerry_get_object_native_pointer (). This way, each const jerry_object_native_info_t * pointer address value itself uniquely identifies the C type of the native pointer.

See jerry_get_object_native_pointer for a best-practice code example.

Prototype

  1. typedef struct
  2. {
  3. jerry_object_native_free_callback_t free_cb;
  4. } jerry_object_native_info_t;

New in version 2.0.

See also

jerry_object_property_foreach_t

Summary

Function type used as a callback for the jerry_foreach_object_property method. A function with this type must return “true” to continue the iteration or “false” to finish the iteration on the object’s properties.

Prototype

  1. typedef bool (*jerry_object_property_foreach_t) (const jerry_value_t property_name,
  2. const jerry_value_t property_value,
  3. void *user_data_p);
  • property_name - a property name, this is not always a string.
  • property_value - the value for the given property.
  • user_data_p - optional user data pointer supplied via the (jerry_foreach_object_property)[#jerry_foreach_object_property] method.
  • return value
    • true, to continue the iteration
    • false, to stop the iteration

See also

jerry_objects_foreach_t

Summary

Function type used as a callback for the (jerry_objects_foreach)[#jerry_objects_foreach] method. A function with this type must return “true” to continue the iteration or “false” to finish the iteration on the object’s properties.

Prototype

  1. typedef bool (*jerry_objects_foreach_t) (const jerry_value_t object,
  2. void *user_data_p);
  • object - the current JavaScript object in the for-each iteration.
  • user_data_p - optional user data pointer supplied via the (jerry_objects_foreach)[#jerry_objects_foreach] method.
  • return value
    • true, to continue the iteration
    • false, to stop the iteration

New in version 2.0.

See also

jerry_objects_foreach_by_native_info_t

Summary

Function type used as a callback for the (jerry_objects_foreach_by_native_info)[#jerry_objects_foreach_by_native_info] method. A function with this type must return “true” to continue the iteration or “false” to finish the iteration on the object’s properties.

Prototype

  1. typedef bool (*jerry_objects_foreach_by_native_info_t) (const jerry_value_t object,
  2. void *object_data_p,
  3. void *user_data_p);
  • object - the current JavaScript object in the for-each iteration.
  • object_data_p - the current object’s native data pointer.
  • user_data_p - optional user data pointer supplied via the (jerry_objects_foreach_by_native_info)[#jerry_objects_foreach_by_native_info] method.
  • return value
    • true, to continue the iteration
    • false, to stop the iteration

New in version 2.0.

See also

jerry_vm_exec_stop_callback_t

Summary

Callback which tells whether the ECMAScript execution should be stopped. If it returns with undefined value the ECMAScript execution continues. Otherwise the result is thrown by the engine (if the error flag is not set for the returned value the engine automatically sets it). The callback function might be called again even if it threw an error. In this case the function must throw the same error again.

Prototype

  1. typedef jerry_value_t (*jerry_vm_exec_stop_callback_t) (void *user_p);

New in version 2.0.

See also

jerry_promise_state_t

Enum which describes the state of a Promise.

Possible values:

  • JERRY_PROMISE_STATE_NONE - Invalid/Unknown state (possibly called on a non-promise object).
  • JERRY_PROMISE_STATE_PENDING - Promise is in “Pending” state.
  • JERRY_PROMISE_STATE_FULFILLED - Promise is in “Fulfilled” state.
  • JERRY_PROMISE_STATE_REJECTED - Promise is in “Rejected” state.

New in version 2.2.

See also

jerry_typedarray_type_t

Enum which describes the TypedArray types. Possible values:

  • JERRY_TYPEDARRAY_UINT8 - represents the Uint8Array TypedArray
  • JERRY_TYPEDARRAY_UINT8CLAMPED - represents the Uint8ClampedArray TypedArray
  • JERRY_TYPEDARRAY_INT8 - represents the Int8Array TypedArray
  • JERRY_TYPEDARRAY_UINT16 - represents the Uint16Array TypedArray
  • JERRY_TYPEDARRAY_INT16 - represents the Int16Array TypedArray
  • JERRY_TYPEDARRAY_UINT32 - represents the Uint32Array TypedArray
  • JERRY_TYPEDARRAY_INT32 - represents the Int32Array TypedArray
  • JERRY_TYPEDARRAY_FLOAT32 - represents the Float32Array TypedArray
  • JERRY_TYPEDARRAY_FLOAT64 - represents the Float64Array TypedArray
  • JERRY_TYPEDARRAY_INVALID - represents an invalid TypedArray

API functions can return the JERRY_TYPEDARRAY_INVALID value if the TypedArray support is not in the engine.

New in version 2.0.

See also

General engine functions

jerry_init

Summary

Initializes the JerryScript engine, making it possible to run JavaScript code and perform operations on JavaScript values. This is required for almost all API functions.

Prototype

  1. void
  2. jerry_init (jerry_init_flag_t flags)

flags - combination of various engine configuration flags jerry_init_flag_t.

Example

  1. #include "jerryscript.h"
  2. int
  3. main (void)
  4. {
  5. jerry_init (JERRY_INIT_SHOW_OPCODES | JERRY_INIT_SHOW_REGEXP_OPCODES);
  6. // ...
  7. jerry_cleanup ();
  8. return 0;
  9. }

See also

jerry_cleanup

Summary

Finish JavaScript engine execution, freeing memory and JavaScript values.

Note: JavaScript values, received from engine, will be inaccessible after the cleanup.

Prototype

  1. void
  2. jerry_cleanup (void);

See also

jerry_get_context_data

Summary

Retrieve a pointer to the item stored within the current context by the given manager.

Note: Since internally the pointer to a manager’s context data item is linked to the next such pointer in a linked list, it is inadvisable to invoke too many different managers, because doing so will increase the time it takes to retrieve a manager’s context data item, degrading performance. For example, try to keep the number of managers below five.

Prototype

  1. void *
  2. jerry_get_context_data (const jerry_context_data_manager *manager_p);
  • manager_p: the manager of this context data item.
  • return value: the item created by manager_p when jerry_get_context_data () was first called, or a new item created by manager_p, which will be stored for future identical calls to jerry_get_context_data (), and which will be deinitialized using the deinit_cb callback provided by manager_p when the context will be destroyed.

New in version 2.0.

Example

  1. #include "jerryscript.h"
  2. typedef struct
  3. {
  4. int my_data1;
  5. double my_data2;
  6. char *my_data3;
  7. } my_context_data_t;
  8. /* Define how context items will be initialized. */
  9. static void
  10. my_context_data_new (void *user_data_p)
  11. {
  12. my_context_data_t *my_data_p = (my_context_data_t *) user_data_p;
  13. /*
  14. * Initialize my_data_p. JerryScript will store it on the current context and return it whenever
  15. * jerry_get_context_data () is called with a pointer to my_manager as defined below.
  16. */
  17. }
  18. /* Define how context items will be deinitialized */
  19. static void
  20. my_context_data_free (void *user_data_p)
  21. {
  22. my_context_data_t *my_data_p = ((my_context_data_t *) user_data_p);
  23. /* Perform any necessary cleanup on my_data. JerryScript will free the pointer after this function completes. */
  24. }
  25. /* Wrap the creation and destruction functions into a manager */
  26. static const jerry_context_data_manager_t my_manager =
  27. {
  28. .init_cb = my_context_data_new,
  29. .deinit_cb = my_context_data_free,
  30. .bytes_needed = sizeof (my_context_data_t)
  31. };
  32. /*
  33. * Then, in some function in your code, you can retrieve an item of type my_context_data_t from the currently active
  34. * context such that JerryScript will create and store such an item if one was not previously created
  35. */
  36. static void
  37. someplace_in_the_code (void)
  38. {
  39. my_context_data_t *my_data = (my_context_data_t *) jerry_get_context_data (&my_manager);
  40. /* Perform useful things using the data found in my_data */
  41. }

jerry_register_magic_strings

Summary

Registers an external magic string array.

Notes:

  • The strings in the array must be sorted by size at first, then lexicographically.
  • The maximum number of external magic strings is limited to 2147483648 (UINT32_MAX / 2). If there are more than 2147483648 external magic strings the extra is cropped.

Prototype

  1. void
  2. jerry_register_magic_strings (const jerry_char_t * const *ex_str_items_p,
  3. uint32_t count,
  4. const jerry_length_t *str_lengths_p);
  • ex_str_items_p - character arrays, representing external magic strings’ contents
  • count - number of elements in ext_str_items_p array
  • str_lengths_p - array of lengths for each magic string

Changed in version 2.0: The first function argument type was changed.

Example

  1. #include "jerryscript.h"
  2. int
  3. main (void)
  4. {
  5. jerry_init (JERRY_INIT_EMPTY);
  6. // must be static, because 'jerry_register_magic_strings' does not copy
  7. // the items must be sorted by size at first, then lexicographically
  8. static const jerry_char_t * const magic_string_items[] = {
  9. (const jerry_char_t *) "magicstring1",
  10. (const jerry_char_t *) "magicstring2",
  11. (const jerry_char_t *) "magicstring3"
  12. };
  13. uint32_t num_magic_string_items = (uint32_t) (sizeof (magic_string_items) / sizeof (jerry_char_t *));
  14. // must be static, because 'jerry_register_magic_strings' does not copy
  15. static const jerry_length_t magic_string_lengths[] = {
  16. 12,
  17. 12,
  18. 12
  19. };
  20. jerry_register_magic_strings (magic_string_items, num_magic_string_items, magic_string_lengths);
  21. }

See also

jerry_get_memory_stats

Summary

Get heap memory stats.

Notes:

  • The engine must be initialized with the JERRY_INIT_MEM_STATS option to allow heap statistic collections. See jerry_init
  • This API depends on a build option (JERRY_MEM_STATS) and can be checked in runtime with the JERRY_FEATURE_MEM_STATS feature enum value, see: jerry_is_feature_enabled.

Prototype

  1. bool
  2. jerry_get_memory_stats (jerry_heap_stats_t *out_stats_p);
  • out_stats_p - out parameter, that provides the heap statistics.
  • return value
    • true, if stats were written into the out_stats_p pointer.
    • false, otherwise. Usually it is because the JERRY_FEATURE_MEM_STATS feature is not enabled.

New in version 2.0.

Example

  1. jerry_init (JERRY_INIT_MEM_STATS);
  2. // ...
  3. jerry_heap_stats_t stats = {0};
  4. bool get_stats_ret = jerry_get_memory_stats (&stats);

See also

jerry_gc

Summary

Performs garbage collection.

Prototype

  1. void
  2. jerry_gc (jerry_gc_mode_t mode);

Changed in version 2.0: Added mode argument.

Example

  1. #include "jerryscript.h"
  2. int
  3. main (void)
  4. {
  5. jerry_init (JERRY_INIT_EMPTY);
  6. jerry_value_t object_value = jerry_create_object ();
  7. jerry_release_value (object_value);
  8. jerry_gc (JERRY_GC_PRESSURE_LOW);
  9. jerry_cleanup ();
  10. }

See also

Parser and executor functions

Functions to parse and run JavaScript source code.

jerry_run_simple

Summary

The simplest way to run JavaScript.

Prototype

  1. bool
  2. jerry_run_simple (const jerry_char_t *script_source_p,
  3. size_t script_source_size,
  4. jerry_init_flag_t flags);
  • script_source_p - source code, it must be a valid utf8 string.
  • script_source_size - size of source code buffer, in bytes.
  • jerry_init_flag_t - combination of various engine configuration flags
  • return value
    • true, if run was successful
    • false, otherwise

Example

  1. #include "jerryscript.h"
  2. int
  3. main (void)
  4. {
  5. const jerry_char_t script[] = "print ('Hello, World!');";
  6. jerry_run_simple (script, sizeof (script) - 1, JERRY_INIT_EMPTY);
  7. return 0;
  8. }

See also

jerry_parse

Summary

Parse script and construct an EcmaScript function. The lexical environment is set to the global lexical environment. The resource name can be used by debugging systems to provide line / backtrace info.

Note: Returned value must be freed with jerry_release_value when it is no longer needed.

Prototype

  1. jerry_value_t
  2. jerry_parse (const jerry_char_t *resource_name_p, /**< resource name (usually a file name) */
  3. size_t resource_name_length, /**< length of resource name */
  4. const jerry_char_t *source_p,
  5. size_t source_size,
  6. uint32_t parse_opts);
  • resource_name_p - resource name, usually a file name (must be a valid UTF8 string).
  • resource_name_length - size of the resource name, in bytes.
  • source_p - string, containing source code to parse (must be a valid UTF8 string).
  • source_size - size of the string, in bytes.
  • parse_opts - any combination of jerry_parse_opts_t flags.
  • return value
    • function object value, if script was parsed successfully,
    • thrown error, otherwise

Changed in version 2.0: Added resource_name_p, and resource_name_length arguments.

Example

  1. #include "jerryscript.h"
  2. int
  3. main (void)
  4. {
  5. jerry_init (JERRY_INIT_EMPTY);
  6. const jerry_char_t script[] = "print ('Hello, World!');";
  7. jerry_value_t parsed_code = jerry_parse (NULL, 0, script, sizeof (script) - 1, JERRY_PARSE_NO_OPTS);
  8. jerry_release_value (parsed_code);
  9. jerry_cleanup ();
  10. return 0;
  11. }

See also

jerry_parse_function

Summary

Parse function source code and construct an ECMAScript function. The function arguments and function body are passed as separated arguments. The lexical environment is set to the global lexical environment. The resource name (usually a file name) is also passed to this function which is used by the debugger to find the source code.

Note: The returned value must be freed with jerry_release_value when it is no longer needed.

Prototype

  1. jerry_value_t
  2. jerry_parse_function (const jerry_char_t *resource_name_p, /**< resource name (usually a file name) */
  3. size_t resource_name_length, /**< length of resource name */
  4. const jerry_char_t *arg_list_p, /**< script source */
  5. size_t arg_list_size, /**< script source size */
  6. const jerry_char_t *source_p, /**< script source */
  7. size_t source_size, /**< script source size */
  8. uint32_t parse_opts) /**< strict mode */
  • resource_name_p - resource name, usually a file name (must be a valid UTF8 string).
  • resource_name_length - size of the resource name, in bytes.
  • arg_list_p - argument list of the function (must be a valid UTF8 string).
  • arg_list_size - size of the argument list, in bytes.
  • source_p - string, containing source code to parse (must be a valid UTF8 string).
  • source_size - size of the string, in bytes.
  • parse_opts - any combination of jerry_parse_opts_t flags.
  • return value
    • function object value, if script was parsed successfully,
    • thrown error, otherwise

New in version 2.0.

Example

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "jerryscript.h"
  4. int
  5. main (void)
  6. {
  7. int return_value = 1;
  8. /* Initialize engine */
  9. jerry_init (JERRY_INIT_EMPTY);
  10. /* Parse the 'function (a,b) { return a + b; }' function */
  11. const char function_args[] = "a, b";
  12. const char function_source[] = "return a + b";
  13. jerry_value_t parsed_function = jerry_parse_function (NULL,
  14. 0,
  15. (const jerry_char_t *) function_args,
  16. strlen (function_args),
  17. (const jerry_char_t *) function_source,
  18. strlen (function_source),
  19. JERRY_PARSE_NO_OPTS);
  20. if (!jerry_value_is_error (parsed_function))
  21. {
  22. /* Run the parsed function */
  23. jerry_value_t args[] = {
  24. jerry_create_number (3),
  25. jerry_create_number (55),
  26. };
  27. jerry_size_t argc = sizeof (args) / sizeof (args[0]);
  28. jerry_value_t ret_value = jerry_call_function (parsed_function,
  29. jerry_create_undefined(),
  30. args,
  31. argc);
  32. /* Process result value */
  33. if (jerry_value_is_number (ret_value)) {
  34. double value = jerry_get_number_value (ret_value);
  35. printf ("Function result: %lf\n", value);
  36. return_value = !(value == (3 + 55));
  37. }
  38. /* Release the function arguments */
  39. for (jerry_size_t idx = 0; idx < argc; idx++) {
  40. jerry_release_value (args[idx]);
  41. }
  42. /* Returned value must be freed */
  43. jerry_release_value (ret_value);
  44. }
  45. /* Parsed function must be freed */
  46. jerry_release_value (parsed_function);
  47. /* Cleanup engine */
  48. jerry_cleanup ();
  49. return return_value;
  50. }

See also

jerry_run

Summary

Run an EcmaScript function created by jerry_parse.

Notes:

  • The code should be previously parsed with jerry_parse.
  • Returned value must be freed with jerry_release_value when it is no longer needed.

Prototype

  1. jerry_value_t
  2. jerry_run (const jerry_value_t func_val);
  • func_val - function to run
  • return value
    • result of bytecode, if run was successful
    • thrown error, otherwise

Example

  1. #include "jerryscript.h"
  2. int
  3. main (void)
  4. {
  5. const jerry_char_t script[] = "print ('Hello, World!');";
  6. /* Initialize engine */
  7. jerry_init (JERRY_INIT_EMPTY);
  8. /* Setup Global scope code */
  9. jerry_value_t parsed_code = jerry_parse (NULL, 0, script, sizeof (script) - 1, JERRY_PARSE_NO_OPTS);
  10. if (!jerry_value_is_error (parsed_code))
  11. {
  12. /* Execute the parsed source code in the Global scope */
  13. jerry_value_t ret_value = jerry_run (parsed_code);
  14. /* Returned value must be freed */
  15. jerry_release_value (ret_value);
  16. }
  17. /* Parsed source code must be freed */
  18. jerry_release_value (parsed_code);
  19. /* Cleanup engine */
  20. jerry_cleanup ();
  21. }

See also

jerry_eval

Summary

Perform JavaScript eval function call (ECMA-262 v5.1 sec-15.1.2.1).

Note: Returned value must be freed with jerry_release_value when it is no longer needed.

Prototype

  1. jerry_value_t
  2. jerry_eval (const jerry_char_t *source_p,
  3. size_t source_size,
  4. uint32_t parse_opts);
  • source_p - source code to evaluate, it must be a valid utf8 string.
  • source_size - length of the source code
  • parse_opts - any combination of jerry_parse_opts_t flags.
  • return value - result of eval, may be an error value.

Example

  1. {
  2. jerry_value_t ret_val = jerry_eval (str_to_eval,
  3. strlen (str_to_eval),
  4. JERRY_PARSE_NO_OPTS);
  5. }

See also

jerry_run_all_enqueued_jobs

Summary

Run enqueued Promise jobs until the first thrown error or until all get executed.

Note: Returned value must be freed with jerry_release_value when it is no longer needed.

Prototype

  1. jerry_value_t
  2. jerry_run_all_enqueued_jobs (void)
  • return value - result of last executed job, may be error value.

New in version 2.0.

Example

  1. #include "jerryscript.h"
  2. int
  3. main (void)
  4. {
  5. jerry_init (JERRY_INIT_EMPTY);
  6. const jerry_char_t script[] = "new Promise(function(f,r) { f('Hello, World!'); }).then(function(x) { print(x); });";
  7. jerry_value_t parsed_code = jerry_parse (NULL, 0, script, sizeof (script) - 1, JERRY_PARSE_NO_OPTS);
  8. jerry_value_t script_value = jerry_run (parsed_code);
  9. jerry_value_t job_value = jerry_run_all_enqueued_jobs ();
  10. jerry_release_value (job_value);
  11. jerry_release_value (script_value);
  12. jerry_release_value (parsed_code);
  13. jerry_cleanup ();
  14. return 0;
  15. }

Get the global context

jerry_get_global_object

Summary

Get the Global object.

Note: Returned value must be freed with jerry_release_value when it is no longer needed.

Prototype

  1. jerry_value_t
  2. jerry_get_global_object (void);
  • return value - api value of global object

Example

  1. {
  2. jerry_value_t glob_obj_val = jerry_get_global_object ();
  3. ... // Do something with global object, ex: add properties
  4. jerry_release_value (glob_obj_val);
  5. }

See also

Checker functions

Functions to check the type of an API value (jerry_value_t).

jerry_value_is_abort

Summary

Returns whether the given jerry_value_t has the error and abort value set.

Prototype

  1. bool
  2. jerry_value_is_abort (const jerry_value_t value);
  • value - api value
  • return value
    • true, if the given jerry_value_t has the error and abort value set
    • false, otherwise

New in version 2.0.

Example

  1. {
  2. jerry_value_t value;
  3. ... // create or acquire value
  4. if (jerry_value_is_abort (value))
  5. {
  6. ...
  7. }
  8. jerry_release_value (value);
  9. }

See also

jerry_value_is_array

Summary

Returns whether the given jerry_value_t is an array.

Prototype

  1. bool
  2. jerry_value_is_array (const jerry_value_t value)
  • value - api value
  • return value
    • true, if the given jerry_value_t is an array
    • false, otherwise

Example

  1. {
  2. jerry_value_t value;
  3. ... // create or acquire value
  4. if (jerry_value_is_array (value))
  5. {
  6. ...
  7. }
  8. jerry_release_value (value);
  9. }

See also

jerry_value_is_arraybuffer

Summary

Returns whether the given jerry_value_t is an ArrayBuffer object.

Notes:

  • This API depends on a build option (JERRY_ES2015_BUILTIN_TYPEDARRAY) and can be checked in runtime with the JERRY_FEATURE_TYPEDARRAY feature enum value, see: jerry_is_feature_enabled.
  • The ES2015-subset profile enables this by default.

Prototype

  1. bool
  2. jerry_value_is_arraybuffer (const jerry_value_t value)
  • value - api value to check.
  • return value
    • true, if the given jerry_value_t is an ArrayBuffer object.
    • false, otherwise

New in version 2.0.

Example

  1. {
  2. jerry_value_t value;
  3. ... // create or acquire value
  4. if (jerry_value_is_arraybuffer (value))
  5. {
  6. ...
  7. }
  8. jerry_release_value (value);
  9. }

See also

jerry_value_is_boolean

Summary

Returns whether the given jerry_value_t is a boolean value.

Prototype

  1. bool
  2. jerry_value_is_boolean (const jerry_value_t value)
  • value - api value
  • return value
    • true, if the given jerry_value_t is a boolean value
    • false, otherwise

Example

  1. {
  2. jerry_value_t value;
  3. ... // create or acquire value
  4. if (jerry_value_is_boolean (value))
  5. {
  6. ...
  7. }
  8. jerry_release_value (value);
  9. }

See also

jerry_value_is_constructor

Summary

Returns whether the given jerry_value_t is a constructor function.

Prototype

  1. bool
  2. jerry_value_is_constructor (const jerry_value_t value)
  • value - api value
  • return value
    • true, if the given jerry_value_t is a constructor
    • false, otherwise

Example

  1. {
  2. jerry_value_t value;
  3. ... // create or acquire value
  4. if (jerry_value_is_constructor (value))
  5. {
  6. ...
  7. }
  8. jerry_release_value (value);
  9. }

See also

jerry_value_is_dataview

Summary

Returns whether the given jerry_value_t is a DataView object value.

Notes:

  • This API depends on a build option (JERRY_ES2015_BUILTIN_DATAVIEW) and can be checked in runtime with the JERRY_FEATURE_DATAVIEW feature enum value, see: jerry_is_feature_enabled.
  • The ES2015-subset profile enables this by default.

Prototype

  1. bool
  2. jerry_value_is_dataview (const jerry_value_t value)
  • value - API value
  • return value
    • true, if the given jerry_value_t is a DataView object
    • false, otherwise

New in version 2.0.

Example

  1. #include "jerryscript.h"
  2. int
  3. main (void)
  4. {
  5. jerry_init (JERRY_INIT_EMPTY);
  6. jerry_value_t arraybuffer = jerry_create_arraybuffer (16);
  7. jerry_value_t dataview = jerry_create_dataview (arraybuffer, 0, 16);
  8. if (jerry_value_is_dataview (dataview))
  9. {
  10. // usage of dataview
  11. }
  12. jerry_release_value (dataview);
  13. jerry_release_value (arraybuffer);
  14. jerry_cleanup ();
  15. return 0;
  16. }

See also

jerry_value_is_error

Summary

Returns whether the given jerry_value_t is error value.

Prototype

  1. bool
  2. jerry_value_is_error (const jerry_value_t value);
  • value - api value
  • return value
    • true, if the given jerry_value_t is error value.
    • false, otherwise

New in version 2.0.

Example

  1. {
  2. jerry_value_t value;
  3. ... // create or acquire value
  4. if (jerry_value_is_error (value))
  5. {
  6. ...
  7. }
  8. jerry_release_value (value);
  9. }

See also

jerry_value_is_function

Summary

Returns whether the given jerry_value_t is a function.

Prototype

  1. bool
  2. jerry_value_is_function (const jerry_value_t value)
  • value - api value
  • return value
    • true, if the given jerry_value_t is a function
    • false, otherwise

Example

  1. {
  2. jerry_value_t value;
  3. ... // create or acquire value
  4. if (jerry_value_is_function (value))
  5. {
  6. ...
  7. }
  8. jerry_release_value (value);
  9. }

See also

jerry_value_is_number

Summary

Returns whether the given jerry_value_t is a number.

Prototype

  1. bool
  2. jerry_value_is_number (const jerry_value_t value)
  • value - api value
  • return value
    • true, if the given jerry_value_t is a number
    • false, otherwise

Example

  1. {
  2. jerry_value_t value;
  3. ... // create or acquire value
  4. if (jerry_value_is_number (value))
  5. {
  6. ...
  7. }
  8. jerry_release_value (value);
  9. }

See also

jerry_value_is_null

Summary

Returns whether the given jerry_value_t is a null value.

Prototype

  1. bool
  2. jerry_value_is_null (const jerry_value_t value)
  • value - api value
  • return value
    • true, if the given jerry_value_t is a null
    • false, otherwise

Example

  1. {
  2. jerry_value_t value;
  3. ... // create or acquire value
  4. if (jerry_value_is_null (value))
  5. {
  6. ...
  7. }
  8. jerry_release_value (value);
  9. }

See also

jerry_value_is_object

Summary

Returns whether the given jerry_value_t is an object value.

Prototype

  1. bool
  2. jerry_value_is_object (const jerry_value_t value)
  • value - api value
  • return value
    • true, if the given jerry_value_t is an object
    • false, otherwise

Example

  1. {
  2. jerry_value_t value;
  3. ... // create or acquire value
  4. if (jerry_value_is_object (value))
  5. {
  6. ...
  7. }
  8. jerry_release_value (value);
  9. }

See also

jerry_value_is_promise

Summary

Returns whether the given jerry_value_t is a promise value.

Notes:

  • This API depends on a build option (JERRY_ES2015_BUILTIN_PROMISE) and can be checked in runtime with the JERRY_FEATURE_PROMISE feature enum value, see: jerry_is_feature_enabled.
  • The ES2015-subset profile enables this by default.

Prototype

  1. bool
  2. jerry_value_is_promise (const jerry_value_t value)
  • value - api value
  • return value
    • true, if the given jerry_value_t is a promise
    • false, otherwise

New in version 2.0.

Example

  1. {
  2. jerry_value_t value;
  3. ... // create or acquire value
  4. if (jerry_value_is_promise (value))
  5. {
  6. ...
  7. }
  8. jerry_release_value (value);
  9. }

See also

jerry_value_is_string

Summary

Returns whether the given jerry_value_t is a string value.

Prototype

  1. bool
  2. jerry_value_is_string (const jerry_value_t value)
  • value - api value
  • return value
    • true, if the given jerry_value_t is a string
    • false, otherwise

Example

  1. {
  2. jerry_value_t value;
  3. ... // create or acquire value
  4. if (jerry_value_is_string (value))
  5. {
  6. ...
  7. }
  8. jerry_release_value (value);
  9. }

See also

jerry_value_is_symbol

Summary

Returns whether the given jerry_value_t is a symbol value.

Notes:

  • This API depends on a build option (JERRY_ES2015_BUILTIN_SYMBOL) and can be checked in runtime with the JERRY_FEATURE_SYMBOL feature enum value, see: jerry_is_feature_enabled.
  • The ES2015-subset profile enables this by default.

Prototype

  1. bool
  2. jerry_value_is_symbol (const jerry_value_t value)
  • value - API value
  • return value
    • true, if the given jerry_value_t is a symbol
    • false, otherwise

New in version 2.0.

Example

  1. #include "jerryscript.h"
  2. int
  3. main (void)
  4. {
  5. jerry_init (JERRY_INIT_EMPTY);
  6. jerry_value_t string_value = jerry_create_string ((const jerry_char_t *) "Symbol description string");
  7. jerry_value_t symbol_value = jerry_create_symbol (string_value);
  8. jerry_release_value (string_value);
  9. if (jerry_value_is_symbol (symbol_value))
  10. {
  11. // usage of symbol_value
  12. }
  13. jerry_release_value (symbol_value);
  14. jerry_cleanup ();
  15. return 0;
  16. }

See also

jerry_value_is_typedarray

Summary

Checks whether the given jerry_value_t is a TypedArray object or not.

Notes:

  • This API depends on a build option (JERRY_ES2015_BUILTIN_TYPEDARRAY) and can be checked in runtime with the JERRY_FEATURE_TYPEDARRAY feature enum value, see: jerry_is_feature_enabled.
  • The ES2015-subset profile enables this by default.

Prototype

  1. bool
  2. jerry_value_is_typedarray (const jerry_value_t value)
  • value - object to check
  • return value
    • true, if the given jerry_value_t is a TypedArray object.
    • false, otherwise

New in version 2.0.

Example

  1. #include "jerryscript.h"
  2. int
  3. main (void)
  4. {
  5. jerry_init (JERRY_INIT_EMPTY);
  6. jerry_value_t value = jerry_create_typedarray (JERRY_TYPEDARRAY_UINT16, 15);
  7. if (jerry_value_is_typedarray (value))
  8. {
  9. /* "value" is a typedarray. */
  10. }
  11. jerry_release_value (value);
  12. jerry_cleanup ();
  13. return 0;
  14. }

See also

jerry_value_is_undefined

Summary

Returns whether the given jerry_value_t is an undefined value.

Prototype

  1. bool
  2. jerry_value_is_undefined (const jerry_value_t value)
  • value - api value
  • return value
    • true, if the given jerry_value_t is an undefined value
    • false, otherwise

Example

  1. {
  2. jerry_value_t value;
  3. ... // create or acquire value
  4. if (jerry_value_is_undefined (value))
  5. {
  6. ...
  7. }
  8. jerry_release_value (value);
  9. }

See also

jerry_value_get_type

Summary

Returns the JavaScript type for a given value as a jerry_type_t enum value.

This is a similar operation to the ‘typeof’ operator in the standard with an exception that the ‘null’ value has its own enum value.

Prototype

  1. jerry_type_t
  2. jerry_value_get_type (const jerry_value_t value);
  • value - JavaScript value to check.
  • return value

New in version 2.0.

Example

  1. {
  2. jerry_value_t number = jerry_create_number (3.3);
  3. jerry_type_t type_info = jerry_value_get_type (number);
  4. if (type_info == JERRY_TYPE_NUMBER)
  5. {
  6. /* ... */
  7. }
  8. jerry_release_value (number);
  9. }

See also

jerry_is_feature_enabled

Summary

Returns whether the specified compile time feature is enabled.

Prototype

  1. bool
  2. jerry_is_feature_enabled (const jerry_feature_t feature);
  • feature - jerry feature
  • return value
    • true, if the given jerry_feature_t is enabled
    • false, otherwise

New in version 2.0.

Example

  1. {
  2. /* ... */
  3. jerry_feature_t feature = JERRY_FEATURE_SNAPSHOT_SAVE;
  4. if (jerry_is_feature_enabled (feature))
  5. {
  6. /* ... */
  7. }
  8. }

See also

Binary operations

jerry_binary_operation

Summary

Perform binary operation on the given operands (==, ===, <, >, etc.).

Note: Returned value must be freed with jerry_release_value when it is no longer needed.

Prototype

  1. jerry_value_t
  2. jerry_binary_operation (jerry_binary_operation_t op,
  3. const jerry_value_t lhs,
  4. const jerry_value_t rhs);
  • op - binary operation
  • lhs - left-hand side operand
  • rhs - right-hand side operand
  • return value
    • error, if argument has an error flag or operation is unsuccessful or unsupported
    • true/false, the result of the binary operation on the given operands otherwise

New in version 2.0.

Example - JERRY_BIN_OP_EQUAL

  1. {
  2. jerry_value_t value1;
  3. jerry_value_t value2;
  4. ... // create or acquire value
  5. jerry_value_t result = jerry_binary_operation (JERRY_BIN_OP_EQUAL, value1, value2)
  6. if (!jerry_value_is_error (result))
  7. {
  8. if (jerry_get_boolean_value (result))
  9. {
  10. // value1 and value2 are equal
  11. }
  12. else
  13. {
  14. // value1 and value2 are NOT equal
  15. }
  16. }
  17. else
  18. {
  19. ... // handle error
  20. }
  21. jerry_release_value (value1);
  22. jerry_release_value (value2);
  23. jerry_release_value (result);
  24. }

Example - JERRY_BIN_OP_INSTANCEOF

  1. #include "jerryscript.h"
  2. static jerry_value_t
  3. my_constructor (const jerry_value_t func_val,
  4. const jerry_value_t this_val,
  5. const jerry_value_t argv[],
  6. const jerry_length_t argc)
  7. {
  8. return jerry_create_undefined ();
  9. }
  10. int
  11. main (void)
  12. {
  13. jerry_init (JERRY_INIT_EMPTY);
  14. jerry_value_t base_obj = jerry_create_object ();
  15. jerry_value_t constructor = jerry_create_external_function (my_constructor);
  16. /* External functions does not have a prototype by default, so we need to create one */
  17. jerry_value_t prototype_str = jerry_create_string ((const jerry_char_t *) ("prototype"));
  18. jerry_release_value (jerry_set_property (constructor, prototype_str, base_obj));
  19. jerry_release_value (prototype_str);
  20. /* Construct the instance. */
  21. jerry_value_t instance_val = jerry_construct_object (constructor, NULL, 0);
  22. /* Call the API function of 'instanceof'. */
  23. jerry_value_t is_instance = jerry_binary_operation (JERRY_BIN_OP_INSTANCEOF,
  24. instance_val,
  25. constructor);
  26. if (!jerry_value_is_error (is_instance)
  27. && jerry_get_boolean_value (is_instance) == true)
  28. {
  29. /* ... */
  30. }
  31. /* Free all of the jerry values and cleanup the engine. */
  32. jerry_release_value (base_obj);
  33. jerry_release_value (constructor);
  34. jerry_release_value (instance_val);
  35. jerry_release_value (is_instance);
  36. jerry_cleanup ();
  37. return 0;
  38. }

See also

Error manipulation functions

Changed in version 2.0: The error handling and manipulation was modified and the old methods were replaced.

jerry_create_abort_from_value

Summary

Create (api) abort from a value.

This function creates an API abort value from an API value. The second argument defines whether the input value must be released or not. If it is set to true, then a jerry_release_value function will be called for the first argument, so the api value won’t be available after the call of jerry_create_abort_from_value. The second argument should be false if both value and created abort value are needed.

Prototype

  1. jerry_value_t
  2. jerry_create_abort_from_value (jerry_value_t value, bool release);
  • value - api value
  • release - raw boolean, defines whether input value must be released
  • return value - abort (api) value

New in version 2.0.

Example 1

  1. {
  2. jerry_value_t value;
  3. ... // create or acquire value
  4. jerry_value_t abort = jerry_create_abort_from_value (value, true);
  5. // using the 'value' variable after release is invalid.
  6. jerry_release_value (abort);
  7. }

Example 2

  1. {
  2. jerry_value_t value;
  3. ... // create or acquire value
  4. jerry_value_t abort = jerry_create_abort_from_value (value, false);
  5. // both 'abort' and 'value' can be used and must be released when they are no longer needed
  6. jerry_release_value (abort);
  7. jerry_release_value (value);
  8. }

See also

jerry_create_error_from_value

Summary

Create (api) error from a value.

This function creates an API error value from an API value. The second argument defines whether the input value must be released or not. If it is set to true, then a jerry_release_value function will be called for the first argument, so the api value won’t be available after the call of jerry_create_error_from_value. The second argument should be false if both value and created error value are needed.

Prototype

  1. jerry_value_t
  2. jerry_create_error_from_value (jerry_value_t value, bool release);
  • value - api value
  • release - raw boolean, defines whether input value must be released
  • return value - error (api) value

New in version 2.0.

Example 1

  1. {
  2. jerry_value_t value;
  3. ... // create or acquire value
  4. jerry_value_t error = jerry_create_error_from_value (value, true);
  5. // using the 'value' variable after release is invalid.
  6. jerry_release_value (error);
  7. }

Example 2

  1. {
  2. jerry_value_t value;
  3. ... // create or acquire value
  4. jerry_value_t error = jerry_create_error_from_value (value, false);
  5. // both 'error' and 'value' can be used and must be released when they are no longer needed
  6. jerry_release_value (error);
  7. jerry_release_value (value);
  8. }

See also

jerry_get_error_type

Summary

Returns the type of the Error object if possible.

If a non-error object is used as the input for the function the method will return JERRY_ERROR_NONE indicating that the value was not an Error object. However it is still possible that the value contains error semantics. To correctly detect if a value have error use the jerry_value_is_error method.

Prototype

  1. jerry_error_t
  2. jerry_get_error_type (const jerry_value_t value);
  • value - api value (possible error object)
  • return value
    • JERRY_ERROR_NONE if the input is not an error object
    • one of the jerry_error_t value

New in version 2.0.

Example

  1. {
  2. jerry_value_t error_obj = jerry_create_error (JERRY_ERROR_RANGE,
  3. (const jerry_char_t *) "error msg");
  4. jerry_error_t error_type = jerry_get_error_type (error_obj);
  5. // error_type is now JERRY_ERROR_RANGE.
  6. jerry_release_value (error_obj);
  7. }

See also

jerry_get_value_from_error

Summary

Get the value from an error.

Many API functions cannot be called with an error value. This function extracts the API value from an error. The second argument defines whether the input error value must be released or not. If it is set to true, then a jerry_release_value function will be called for the first argument, so the error value won’t be available after the call of jerry_get_value_from_error. The second argument should be false if both error and its represented value are needed.

Note: Returned value must be freed with jerry_release_value when it is no longer needed.

Prototype

  1. jerry_value_t
  2. jerry_get_value_from_error (jerry_value_t value, bool release)
  • value - error (api) value
  • release - raw boolean, defines whether input value must be released
  • return value - api value

New in version 2.0.

Example 1

  1. {
  2. jerry_value_t value;
  3. ... // create or acquire value
  4. jerry_value_t error = jerry_create_error_from_value (value, true);
  5. jerry_value_t value_from_error = jerry_get_value_from_error (error, true);
  6. // using the 'error' variable after release is invalid.
  7. jerry_release_value (value_from_error);
  8. }

Example 2

  1. {
  2. jerry_value_t value;
  3. ... // create or acquire value
  4. jerry_value_t error = jerry_create_error_from_value (value, true);
  5. jerry_value_t value_from_error = jerry_get_value_from_error (error, false);
  6. // both 'error' and 'value_from_error' can be used and must be released when they are no longer needed
  7. jerry_release_value (value_from_error);
  8. jerry_release_value (error);
  9. }

See also

Getter functions of ‘jerry_value_t’

Get raw data from API values.

jerry_get_boolean_value

Summary

Gets the raw bool value from a jerry_value_t.

Prototype

  1. bool
  2. jerry_get_boolean_value (const jerry_value_t value);
  • value - api value
  • return value - boolean value represented by the argument.

Example

  1. {
  2. jerry_value_t value;
  3. ... // create or acquire value
  4. if (jerry_value_is_boolean (value))
  5. {
  6. bool raw_value = jerry_get_boolean_value (value);
  7. ... // usage of raw value
  8. }
  9. jerry_release_value (value);
  10. }

See also

jerry_get_number_value

Summary

Gets the number value of the given jerry_value_t parameter as a raw double.

If the argument passed is not a number 0.0 will be returned.

Prototype

  1. double
  2. jerry_get_number_value (const jerry_value_t value);
  • value - api value
  • return value
    • the number value of the given jerry_value_t parameter as a raw double.
    • 0.0 if the api value passed is not a number.

Example

  1. {
  2. jerry_value_t value;
  3. ... // create or acquire value
  4. if (jerry_value_is_number (value))
  5. {
  6. double raw_value = jerry_get_number_value (value);
  7. ... // usage of raw value
  8. }
  9. jerry_release_value (value);
  10. }

See also

Functions for string values

jerry_get_string_size

Summary

Get the size of a string. Returns zero, if the value parameter is not a string. This is effectively the number of bytes required to store the string’s characters.

Prototype

  1. jerry_size_t
  2. jerry_get_string_size (const jerry_value_t value);
  • value - api value
  • return value - number of bytes in the buffer needed to represent the string.

Example

  1. {
  2. const jerry_char_t char_array[] = "a string";
  3. jerry_value_t string = jerry_create_string (char_array);
  4. jerry_size_t string_size = jerry_get_string_size (string);
  5. ... // usage of string_size
  6. jerry_release_value (string);
  7. }

See also

jerry_get_utf8_string_size

Summary

Get the size of an utf8-encoded string. Returns zero, if the value parameter is not a string. This is effectively the number of bytes required to store the utf8 encoded string’s characters.

Note: The difference from jerry_get_string_size is that it returns with utf-8 string size instead of the cesu-8 string size.

Prototype

  1. jerry_size_t
  2. jerry_get_utf8_string_size (const jerry_value_t value);
  • value - api value
  • return value - number of bytes in the buffer needed to represent the utf8-encoded string.

New in version 2.0.

Example

  1. {
  2. const jerry_char_t char_array[] = "a string";
  3. jerry_value_t string = jerry_create_string (char_array);
  4. jerry_size_t string_size = jerry_get_utf8_string_size (string);
  5. ... // usage of string_size
  6. jerry_release_value (string);
  7. }

See also

jerry_get_string_length

Summary

Get the length of a string. Returns zero, if the value parameter is not a string.

Notes:

  • The difference from jerry_get_string_size is that it returns the number of bytes used for the string.
  • This is not the number of bytes required to store the string.

Prototype

  1. jerry_length_t
  2. jerry_get_string_length (const jerry_value_t value);
  • value - api value
  • return value - number of characters in the string

Example

  1. {
  2. const jerry_char_t char_array[] = "a string";
  3. jerry_value_t string = jerry_create_string (char_array);
  4. jerry_length_t string_length = jerry_get_string_length (string);
  5. ... // usage of string_length
  6. jerry_release_value (string);
  7. }

See also

jerry_get_utf8_string_length

Summary

Get the length of an UTF-8 encoded string. Returns zero, if the value parameter is not a string.

Notes:

  • The difference from jerry_get_string_length is that it returns with utf-8 string length instead of the cesu-8 string length.
  • This is not the number of bytes required to store the string.

Prototype

  1. jerry_length_t
  2. jerry_get_utf8_string_length (const jerry_value_t value);
  • value - input string value
  • return value - number of characters in the string

New in version 2.0.

Example

  1. {
  2. const jerry_char_t char_array[] = "a string";
  3. jerry_value_t string = jerry_create_string_from_utf8 (char_array);
  4. jerry_length_t string_length = jerry_get_utf8_string_length (string);
  5. ... // usage of string_length
  6. jerry_release_value (string);
  7. }

See also

jerry_string_to_char_buffer

Summary

Copy the characters of a string into a specified cesu-8 buffer. The ‘\0’ character could occur in the character buffer. Returns 0, if the value parameter is not a string or the buffer is not large enough for the whole string.

Note: Does not put ‘\0’ to the end of string, the return value identifies the number of valid bytes in the output buffer.

Note: If the size of the string in jerry value is larger than the size of the target buffer, the copy will fail. To copy a substring the jerry_substring_to_char_buffer API function is recommended instead.

Prototype

  1. jerry_size_t
  2. jerry_string_to_char_buffer (const jerry_value_t value,
  3. jerry_char_t *buffer_p,
  4. jerry_size_t buffer_size);
  • value - input string value
  • buffer_p - pointer to output buffer
  • buffer_size - size of the buffer
  • return value - number of bytes, actually copied to the buffer

Example

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "jerryscript.h"
  4. int
  5. main (void)
  6. {
  7. jerry_init (JERRY_INIT_EMPTY);
  8. jerry_value_t value;
  9. // create or acquire value
  10. value = jerry_create_string ((const jerry_char_t *) "Demo string");
  11. // Read the string into a byte buffer.
  12. jerry_size_t string_size = jerry_get_string_size (value);
  13. jerry_char_t *string_buffer_p = (jerry_char_t *) malloc (sizeof (jerry_char_t) * (string_size + 1));
  14. jerry_size_t copied_bytes = jerry_string_to_char_buffer (value, string_buffer_p, string_size);
  15. string_buffer_p[copied_bytes] = '\0';
  16. jerry_release_value (value);
  17. jerry_cleanup ();
  18. printf ("Test string: %s\n", string_buffer_p);
  19. free (string_buffer_p);
  20. return 0;
  21. }

See also

jerry_string_to_utf8_char_buffer

Summary

Copy the characters of a string into a specified utf-8 buffer. The ‘\0’ character could occur in character buffer. Returns 0, if the value parameter is not a string or the buffer is not large enough for the whole string.

Note: Does not put ‘\0’ to the end of string, the return value identifies the number of valid bytes in the output buffer.

Note: If the size of the string in jerry value is larger than the size of the target buffer, the copy will fail. To copy a substring the jerry_substring_to_utf8_char_buffer API function is recommended instead.

Prototype

  1. jerry_size_t
  2. jerry_string_to_utf8_char_buffer (const jerry_value_t value,
  3. jerry_char_t *buffer_p,
  4. jerry_size_t buffer_size);
  • value - input string value
  • buffer_p - pointer to output buffer
  • buffer_size - size of the buffer
  • return value - number of bytes, actually copied to the buffer

New in version 2.0.

Example

  1. {
  2. jerry_value_t value;
  3. ... // create or acquire value
  4. jerry_size_t req_sz = jerry_get_utf8_string_size (value);
  5. jerry_char_t str_buf_p[req_sz];
  6. jerry_size_t bytes_copied = jerry_string_to_utf8_char_buffer (value, str_buf_p, req_sz);
  7. jerry_release_value (value);
  8. }

See also

jerry_substring_to_char_buffer

Summary

Copy the characters of a cesu-8 encoded substring into a specified buffer. The ‘\0’ character could occur in character buffer. Returns 0, if the value parameter is not a string. It will extract the substring between the specified start position and the end position (or the end of the string, whichever comes first).

Note: Does not put ‘\0’ to the end of string, the return value identifies the number of valid bytes in the output buffer.

Prototype

  1. jerry_size_t
  2. jerry_substring_to_char_buffer (const jerry_value_t value,
  3. jerry_length_t start_pos,
  4. jerry_length_t end_pos,
  5. jerry_char_t *buffer_p,
  6. jerry_size_t buffer_size);
  • value - input string value
  • start_pos - position of the first character
  • end_pos - position of the last character
  • buffer_p - pointer to output buffer
  • buffer_size - size of the buffer
  • return value - number of bytes, actually copied to the buffer

New in version 2.0.

Example

  1. {
  2. jerry_value_t value;
  3. ... // create or acquire value
  4. jerry_size_t req_sz = jerry_get_string_size (value);
  5. jerry_char_t str_buf_p[req_sz];
  6. jerry_length_t start_pos = 0;
  7. jerry_length_t end_pos = jerry_get_string_length (value);
  8. jerry_substring_to_char_buffer (value, start_pos, end_pos, str_buf_p, req_sz);
  9. jerry_release_value (value);
  10. }

See also

jerry_substring_to_utf8_char_buffer

Summary

Copy the characters of an utf-8 encoded substring into a specified buffer. The ‘\0’ character could occur in character buffer. Returns 0, if the value parameter is not a string. It will extract the substring between the specified start position and the end position (or the end of the string, whichever comes first).

Note: Does not put ‘\0’ to the end of string, the return value identifies the number of valid bytes in the output buffer.

Prototype

  1. jerry_size_t
  2. jerry_substring_to_utf8_char_buffer (const jerry_value_t value,
  3. jerry_length_t start_pos,
  4. jerry_length_t end_pos,
  5. jerry_char_t *buffer_p,
  6. jerry_size_t buffer_size);
  • value - input string value
  • start_pos - position of the first character
  • end_pos - position of the last character
  • buffer_p - pointer to output buffer
  • buffer_size - size of the buffer
  • return value - number of bytes, actually copied to the buffer

New in version 2.0.

Example

  1. {
  2. jerry_value_t value;
  3. ... // create or acquire value
  4. jerry_size_t req_sz = jerry_get_utf8_string_size (value);
  5. jerry_char_t str_buf_p[req_sz];
  6. jerry_length_t start_pos = 0;
  7. jerry_length_t end_pos = jerry_get_utf8_string_length (value);
  8. jerry_substring_to_utf8_char_buffer (value, start_pos, end_pos, str_buf_p, req_sz);
  9. jerry_release_value (value);
  10. }

See also

Functions for array object values

jerry_get_array_length

Summary

Get length of an array object. Returns zero, if the given parameter is not an array object.

Prototype

  1. uint32_t
  2. jerry_get_array_length (const jerry_value_t value);
  • value - input array value
  • return value - length of the given array

Example

  1. {
  2. jerry_value_t value;
  3. ... // create or acquire value
  4. uint32_t len = jerry_get_array_length (value);
  5. jerry_release_value (value);
  6. }

See also

Converters of ‘jerry_value_t’

Functions for converting API values to another value type.

jerry_value_to_boolean

Summary

Call ToBoolean operation on the api value.

Prototype

  1. bool
  2. jerry_value_to_boolean (const jerry_value_t value);
  • value - api value
  • return value
    • true, if the logical value is true
    • false, otherwise

Example

  1. {
  2. jerry_value_t value;
  3. ... // create or acquire value
  4. bool b = jerry_value_to_boolean (value);
  5. jerry_release_value (value);
  6. }

See also

jerry_value_to_number

Summary

Call ToNumber operation on the api value.

Note: Returned value must be freed with jerry_release_value when it is no longer needed.

Prototype

  1. jerry_value_t
  2. jerry_value_to_number (const jerry_value_t value);
  • value - api value
  • return value
    • converted number value, if success
    • thrown error, otherwise

Example

  1. {
  2. jerry_value_t value;
  3. ... // create or acquire value
  4. jerry_value_t number_value = jerry_value_to_number (value);
  5. jerry_release_value (number_value);
  6. jerry_release_value (value);
  7. }

See also

jerry_value_to_object

Summary

Call ToObject operation on the api value.

Note: Returned value must be freed with jerry_release_value when it is no longer needed.

Prototype

  1. jerry_value_t
  2. jerry_value_to_object (const jerry_value_t value);
  • value - api value
  • return value
    • converted object value, if success
    • thrown error, otherwise

Example

  1. {
  2. jerry_value_t value;
  3. ... // create or acquire value
  4. jerry_value_t object_value = jerry_value_to_object (value);
  5. jerry_release_value (object_value);
  6. jerry_release_value (value);
  7. }

See also

jerry_value_to_primitive

Summary

Call ToPrimitive operation on the api value.

Note: Returned value must be freed with jerry_release_value when it is no longer needed.

Prototype

  1. jerry_value_t
  2. jerry_value_to_primitive (const jerry_value_t value);
  • value - api value
  • return value
    • converted primitive value, if success
    • thrown error, otherwise

Example

  1. {
  2. jerry_value_t value;
  3. ... // create or acquire value
  4. jerry_value_t prim_value = jerry_value_to_primitive (value);
  5. jerry_release_value (prim_value);
  6. jerry_release_value (value);
  7. }

See also

jerry_value_to_string

Summary

Call the ToString ecma builtin operation on the api value.

Note: Returned value must be freed with jerry_release_value when it is no longer needed.

Prototype

  1. jerry_value_t
  2. jerry_value_to_string (const jerry_value_t value);
  • value - api value
  • return value
    • converted string value, if success
    • thrown error, otherwise

Example

  1. {
  2. jerry_value_t value;
  3. ... // create or acquire value
  4. jerry_value_t string_value = jerry_value_to_string (value);
  5. jerry_release_value (string_value);
  6. jerry_release_value (value);
  7. }

See also

Functions for promise objects

These APIs all depend on the ES2015-subset profile (or on some build options).

jerry_get_promise_result

Summary

The function returns the result of a Promise object.

Notes:

  • Returned value must be freed with jerry_release_value when it is no longer needed.
  • This API depends on a build option (JERRY_ES2015_BUILTIN_PROMISE) and can be checked in runtime with the JERRY_FEATURE_PROMISE feature enum value, see: jerry_is_feature_enabled.
  • The ES2015-subset profile enables this by default.

Prototype

  1. jerry_value_t
  2. jerry_get_promise_result (const jerry_value_t promise);
  • promise - the input Promise object.
  • return
    • The result of the Promise.
    • If the Promise is not resolved yet the result is the ‘undefined’ value.
    • A TypeError is returned if the input argument was not a Promise object or the Promise support was not built into the library.

New in version 2.2.

Example

  1. #include <jerryscript.h>
  2. static void
  3. example (void)
  4. {
  5. // acquire/create a promise object.
  6. jerry_value_t promise = jerry_create_promise ();
  7. {
  8. // prepare the argumnent for the resolve or reject.
  9. jerry_value_t argument = jerry_create_number (33);
  10. jerry_value_t is_ok = jerry_resolve_or_reject_promise (promise,
  11. argument,
  12. true);
  13. // 'is_ok' should be checked if it is an error or not.
  14. // skipped in this example
  15. jerry_release_value (is_ok);
  16. jerry_release_value (argument);
  17. }
  18. jerry_value_t promise_result = jerry_get_promise_result (promise);
  19. // 'promise_result' is now the number 33.
  20. jerry_release_value (promise_result);
  21. jerry_release_value (promise);
  22. }

See also

jerry_get_promise_state

Summary

Notes:

  • Returned value must be freed with jerry_release_value when it is no longer needed.
  • This API depends on a build option (JERRY_ES2015_BUILTIN_PROMISE) and can be checked in runtime with the JERRY_FEATURE_PROMISE feature enum value, see: jerry_is_feature_enabled.
  • The ES2015-subset profile enables this by default.

Prototype

  1. jerry_promise_state_t
  2. jerry_get_promise_state (const jerry_value_t promise);
  • promise - the input promise object.
  • return
    • jerry_promise_state_t
    • JERRY_PROMISE_STATE_NONE is returned if the input argument was not a promise object or the Promise support was not built into the library.

New in version 2.2.

Example

  1. #include <jerryscript.h>
  2. static void
  3. example (void)
  4. {
  5. // acquire/create a promise object.
  6. jerry_value_t promise = jerry_create_promise ();
  7. jerry_promise_state_t start_state = jerry_get_promise_state (promise);
  8. // a Promise have a default state of JERRY_PROMISE_STATE_PENDING
  9. {
  10. // prepare the argumnent for the resolve or reject.
  11. jerry_value_t argument = jerry_create_number (33);
  12. jerry_value_t is_ok = jerry_resolve_or_reject_promise (promise,
  13. argument,
  14. true);
  15. // 'is_ok' should be checked if it is an error or not.
  16. // skipped in this example
  17. jerry_release_value (is_ok);
  18. jerry_release_value (argument);
  19. }
  20. jerry_promise_state_t current_state = jerry_get_promise_state (promise);
  21. // at this point the Promise should be in the JERRY_PROMISE_STATE_FULFILLED state.
  22. jerry_release_value (promise);
  23. }

See also

jerry_resolve_or_reject_promise

Summary

Resolve or reject the promise with an argument.

Note:

  • Returned value must be freed with jerry_release_value when it is no longer needed.
  • This API depends on a build option (JERRY_ES2015_BUILTIN_PROMISE) and can be checked in runtime with the JERRY_FEATURE_PROMISE feature enum value, see: jerry_is_feature_enabled.
  • The ES2015-subset profile enables this by default.

Prototype

  1. jerry_value_t
  2. jerry_resolve_or_reject_promise (jerry_value_t promise,
  3. jerry_value_t argument,
  4. bool is_resolve)
  • promise - the promise value
  • argument - the argument for resolve or reject
  • is_resolve - whether the promise should be resolved or rejected
  • return value
    • undefined jerry value - resolve or reject successed
    • jerry value with error flag - otherwise

New in version 2.0.

Example

  1. {
  2. jerry_value_t promise = ... // acquire/create a promise object.
  3. ...
  4. bool is_resolve = ... // whether the promise should be resolved or rejected
  5. jerry_value_t argument = ... // prepare the argumnent for the resolve or reject.
  6. jerry_value_t is_ok = jerry_resolve_or_reject_promise (promise,
  7. argument,
  8. is_resolve);
  9. if (jerry_value_is_error (is_ok))
  10. {
  11. // handle the error.
  12. }
  13. jerry_release_value (is_ok);
  14. jerry_release_value (argument);
  15. jerry_release_value (promise);
  16. }

See also

Functions for symbols

These APIs all depend on the ES2015-subset profile (or on build options).

jerry_get_symbol_descriptive_string

Summary

Call the SymbolDescriptiveString ecma builtin operation on the API value. Based on ECMA 262 v6 19.4.3.2.1 this is in the form of Symbol(<description>).

Notes:

  • Returned value must be freed with jerry_release_value when it is no longer needed.
  • This API depends on a build option (JERRY_ES2015_BUILTIN_SYMBOL) and can be checked in runtime with the JERRY_FEATURE_SYMBOL feature enum value, see: jerry_is_feature_enabled.
  • The ES2015-subset profile enables this by default.
  • If the symbol support is not enabled an error will be returned.

Prototype

  1. jerry_value_t
  2. jerry_get_symbol_descriptive_string (const jerry_value_t value);
  • value - symbol value
  • return value
    • string value containing the symbol’s descriptive string - if success
    • thrown error, otherwise

New in version 2.0.

Example

  1. #include "jerryscript.h"
  2. int
  3. main (void)
  4. {
  5. jerry_init (JERRY_INIT_EMPTY);
  6. jerry_value_t string_value = jerry_create_string ((const jerry_char_t *) "foo");
  7. jerry_value_t symbol_value = jerry_create_symbol (string_value);
  8. jerry_release_value (string_value);
  9. jerry_value_t symbol_desc_string = jerry_get_symbol_descriptive_string (symbol_value);
  10. // usage of symbol_desc_string
  11. jerry_release_value (symbol_desc_string);
  12. jerry_release_value (symbol_value);
  13. jerry_cleanup ();
  14. return 0;
  15. }

Acquire and release API values

jerry_acquire_value

Summary

Acquires the specified Jerry API value.

Note: Returned value must be freed with jerry_release_value when it is no longer needed.

Prototype

  1. jerry_value_t
  2. jerry_acquire_value (jerry_value_t value);
  • value - api value
  • return value - acquired value that may be used outside of the engine

Example

  1. {
  2. jerry_value_t object_value = jerry_create_object ();
  3. jerry_value_t acquired_object = jerry_acquire_value (object_value);
  4. jerry_release_value (object_value);
  5. // acquired_object refers to the created object and makes it
  6. // available after the release of 'object_value'
  7. jerry_release_value (acquired_object);
  8. }

See also

jerry_release_value

Summary

Release specified Jerry API value.

Prototype

  1. void
  2. jerry_release_value (jerry_value_t value);
  • value - api value

Example

  1. {
  2. jerry_value_t object_value = jerry_create_object ();
  3. ...
  4. jerry_release_value (object_value);
  5. }

Create API values

Function for creating API values.

Note: Every created API value must be freed with jerry_release_value when it is no longer needed.

jerry_create_array

Summary

Create an array object value.

Note: Returned value must be freed with jerry_release_value when it is no longer needed.

Prototype

  1. jerry_value_t
  2. jerry_create_array (uint32_t size);
  • size - size of array;
  • return value - value of the constructed array object

Example

  1. {
  2. jerry_value_t array = jerry_create_array (10);
  3. ...
  4. jerry_release_value (array);
  5. }

See also

jerry_create_arraybuffer

Summary

Create a jerry_value_t representing an ArrayBuffer object.

Note:

  • This API depends on the ES2015-subset profile.
  • Returned value must be freed with jerry_release_value when it is no longer needed.

Prototype

  1. jerry_value_t
  2. jerry_create_arraybuffer (jerry_length_t size);
  • size - size of the ArrayBuffer to create in bytes
  • return value - the new ArrayBuffer as a jerry_value_t

New in version 2.0.

Example

  1. {
  2. jerry_value_t buffer_value = jerry_create_arraybuffer (15);
  3. ... // use the ArrayBuffer
  4. jerry_release_value (buffer_value);
  5. }

See also

jerry_create_arraybuffer_external

Summary

Creates a jerry_value_t representing an ArrayBuffer object with user specified back-buffer.

User must pass a buffer pointer which is at least size big. After the object is not needed the GC will call the free_cb so the user can release the buffer which was provided.

Note:

  • This API depends on the ES2015-subset profile.
  • Returned value must be freed with jerry_release_value when it is no longer needed.

Prototype

  1. jerry_value_t
  2. jerry_create_arraybuffer_external (const jerry_length_t size
  3. uint8_t *buffer_p,
  4. jerry_object_native_free_callback_t free_cb);
  • size - size of the buffer to use in bytes (should not be 0)
  • buffer_p - the buffer used for the Array Buffer object (should not be a null pointer)
  • free_cb - the callback function called when the object is released
  • return value
    • the new ArrayBuffer as a jerry_value_t
    • if the size is zero or buffer_p is a null pointer this will return an empty ArrayBuffer.

New in version 2.0.

Example

  1. {
  2. uint8_t buffer_p[15];
  3. jerry_value_t buffer_value = jerry_create_arraybuffer_external (15, buffer_p, NULL);
  4. ... // use the array buffer
  5. jerry_release_value (buffer_value);
  6. }

See also

jerry_create_boolean

Summary

Create a jerry_value_t representing a boolean value from the given boolean parameter.

Prototype

  1. jerry_value_t
  2. jerry_create_boolean (bool value);
  • value - raw boolean value.
  • return value - a jerry_value_t created from the given boolean argument.

Example

  1. {
  2. jerry_value_t boolean_value = jerry_create_boolean (true);
  3. ... // usage of the value
  4. jerry_release_value (boolean_value);
  5. }

See also

jerry_create_error

Summary

Create new JavaScript error object.

Important! The error_type argument must not be JERRY_ERROR_NONE. Creating an error with no error type is not valid.

Note: Returned value must be freed with jerry_release_value when it is no longer needed.

Prototype

  1. jerry_value_t
  2. jerry_create_error (jerry_error_t error_type,
  3. const jerry_char_t *message_p);
  • error_type - type of error
  • message_p - value of ‘message’ property of constructed error object
  • return value - value of the constructed error object

Example

  1. {
  2. jerry_value_t error_obj = jerry_create_error (JERRY_ERROR_TYPE,
  3. (const jerry_char_t *) "error");
  4. ... // usage of error_obj
  5. jerry_release_value (error_obj);
  6. }

See also

jerry_create_error_sz

Summary

Create new JavaScript error object.

Note: Returned value must be freed with jerry_release_value when it is no longer needed.

Prototype

  1. jerry_value_t
  2. jerry_create_error_sz (jerry_error_t error_type,
  3. const jerry_char_t *message_p,
  4. jerry_size_t message_size);
  • error_type - type of the error
  • message_p - value of ‘message’ property of the constructed error object
  • message_size - size of the message in bytes
  • return value - value of the constructed error object

Example

  1. {
  2. const jerry_char_t message[] = "error";
  3. jerry_value_t error_obj = jerry_create_error_sz (JERRY_ERROR_COMMON,
  4. message,
  5. sizeof (message) - 1);
  6. ... // usage of error_obj
  7. jerry_release_value (error_obj);
  8. }

See also

jerry_create_dataview

Summary

Create new JavaScript DataView object.

Note:

  • This API depends on the ES2015-subset profile.
  • Returned value must be freed with jerry_release_value when it is no longer needed.

Prototype

  1. jerry_value_t
  2. jerry_create_dataview (const jerry_value_t array_buffer,
  3. const jerry_length_t byte_offset,
  4. const jerry_length_t byte_length)
  • array_buffer - arrayBuffer to create DataView from
  • byte_offset - offset in bytes, to the first byte in the buffer
  • byte_length - number of elements in the byte array
  • return value
    • value of the constructed DataView object - if success
    • created error - otherwise

New in version 2.0.

Example

  1. #include "jerryscript.h"
  2. int
  3. main (void)
  4. {
  5. jerry_init (JERRY_INIT_EMPTY);
  6. jerry_value_t arraybuffer = jerry_create_arraybuffer (16);
  7. jerry_value_t dataview = jerry_create_dataview (arraybuffer, 0, 16);
  8. // usage of dataview
  9. jerry_release_value (dataview);
  10. jerry_release_value (arraybuffer);
  11. jerry_cleanup ();
  12. return 0;
  13. }

See also

jerry_create_external_function

Summary

Create an external function object.

Note: Returned value must be freed with jerry_release_value when it is no longer needed.

Prototype

  1. jerry_value_t
  2. jerry_create_external_function (jerry_external_handler_t handler_p);
  • handler_p - pointer to native handler of the function object
  • return value - value of the constructed function object

Example

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "jerryscript.h"
  4. static jerry_value_t
  5. handler (const jerry_value_t function_obj,
  6. const jerry_value_t this_val,
  7. const jerry_value_t args_p[],
  8. const jerry_length_t args_cnt)
  9. {
  10. printf ("native handler called!\n");
  11. return jerry_create_boolean (true);
  12. }
  13. int
  14. main (void)
  15. {
  16. jerry_init (JERRY_INIT_EMPTY);
  17. jerry_value_t func_val = jerry_create_external_function (handler);
  18. jerry_value_t glob_obj = jerry_get_global_object ();
  19. // after this, script can invoke the native handler through "handler_field (1, 2, 3);"
  20. jerry_value_t prop_name = jerry_create_string ((const jerry_char_t *) "handler_field");
  21. // set property and release the return value without any check
  22. jerry_release_value (jerry_set_property (glob_obj, prop_name, func_val));
  23. jerry_release_value (prop_name);
  24. jerry_release_value (func_val);
  25. jerry_release_value (glob_obj);
  26. // Test the method by calling it
  27. const char *test_src = "handler_field ();";
  28. jerry_value_t ret_val = jerry_eval ((const jerry_char_t *) test_src,
  29. strlen (test_src),
  30. JERRY_PARSE_NO_OPTS);
  31. // release the eval result
  32. jerry_release_value (ret_val);
  33. jerry_cleanup ();
  34. return 0;
  35. }

See also

jerry_create_number

Summary

Creates a jerry_value_t representing a number value.

Note: Returned value must be freed with jerry_release_value when it is no longer needed.

Prototype

  1. jerry_value_t
  2. jerry_create_number (double value);
  • value - double value from which a jerry_value_t will be created
  • return value - a jerry_value_t created from the given double argument

Example

  1. {
  2. jerry_value_t number_value = jerry_create_number (3.14);
  3. ... // usage of the value
  4. jerry_release_value (number_value);
  5. }

See also

jerry_create_number_infinity

Summary

Creates a jerry_value_t representing a positive or negative infinity value.

Note: Returned value must be freed with jerry_release_value when it is no longer needed.

Prototype

  1. jerry_value_t
  2. jerry_create_number_infinity (bool sign);
  • sign - true for negative Infinity and false for positive Infinity
  • return value - a jerry_value_t representing the infinity value

Example

  1. {
  2. jerry_value_t positive_inf_value = jerry_create_number_infinity (false);
  3. ... // usage of the positive_inf_value
  4. jerry_release_value (positive_inf_value);
  5. }

See also

jerry_create_number_nan

Summary

Creates a jerry_value_t representing a not-a-number value.

Note: Returned value must be freed with jerry_release_value when it is no longer needed.

Prototype

  1. jerry_value_t
  2. jerry_create_number_nan (void);
  • return value - a jerry_value_t representing the not-a-number value

Example

  1. {
  2. jerry_value_t nan_value = jerry_create_number_nan ();
  3. ... // usage of the nan_value
  4. jerry_release_value (nan_value);
  5. }

See also

jerry_create_null

Summary

Creates and returns a jerry_value_t with type null object.

Prototype

  1. jerry_value_t
  2. jerry_create_null (void);
  • return value - a jerry_value_t representing null.

Example

  1. {
  2. jerry_value_t null_value = jerry_create_null ();
  3. ... // usage of the value
  4. jerry_release_value (null_value);
  5. }

See also

jerry_create_object

Summary

Create new JavaScript object, like with new Object().

Note: Returned value must be freed with jerry_release_value when it is no longer needed.

Prototype

  1. jerry_value_t
  2. jerry_create_object (void);
  • return value - value of the created object

Example

  1. {
  2. jerry_value_t object_value = jerry_create_object ();
  3. ... // usage of object_value
  4. jerry_release_value (object_value);
  5. }

See also

jerry_create_promise

Summary

Create an empty promise object which can be resolved or rejected later by calling jerry_resolve_or_reject_promise.

Note:

  • This API depends on the ES2015-subset profile.
  • Returned value must be freed with jerry_release_value when it is no longer needed.

Prototype

  1. jerry_value_t
  2. jerry_create_promise (void)
  • return value - value of the newly created promise

New in version 2.0.

Example

  1. {
  2. jerry_value_t p = jerry_create_promise ();
  3. ...// usage of the promise
  4. jerry_release_value (p);
  5. }

See also

jerry_create_string

Summary

Create string from a valid CESU8 string.

Note: Returned value must be freed with jerry_release_value when it is no longer needed.

Prototype

  1. jerry_value_t
  2. jerry_create_string (const jerry_char_t *str_p);
  • str_p - non-null pointer to string
  • return value - value of the created string

Example

  1. {
  2. const jerry_char_t char_array[] = "a string";
  3. jerry_value_t string_value = jerry_create_string (char_array);
  4. ... // usage of string_value
  5. jerry_release_value (string_value);
  6. }

See also

jerry_create_string_sz

Summary

Create string from a valid CESU8 string.

Note: Returned value must be freed with jerry_release_value when it is no longer needed.

Prototype

  1. jerry_value_t
  2. jerry_create_string_sz (const jerry_char_t *str_p,
  3. jerry_size_t str_size)
  • str_p - non-null pointer to string
  • str_size - size of the string
  • return value - value of the created string

Example

  1. {
  2. const jerry_char_t char_array[] = "a string";
  3. jerry_value_t string_value = jerry_create_string_sz (char_array,
  4. sizeof (char_array) - 1);
  5. ... // usage of string_value
  6. jerry_release_value (string_value);
  7. }

See also

jerry_create_string_from_utf8

Summary

Create string from a valid UTF8 string.

Note:

Prototype

  1. jerry_value_t
  2. jerry_create_string_from_utf8 (const jerry_char_t *str_p);
  • str_p - non-null pointer to string
  • return value - value of the created string

New in version 2.0.

Example

  1. {
  2. const jerry_char_t char_array[] = "a string";
  3. jerry_value_t string_value = jerry_create_string_from_utf8 (char_array);
  4. ... // usage of string_value
  5. jerry_release_value (string_value);
  6. }

See also

jerry_create_string_sz_from_utf8

Summary

Create string from a valid UTF8 string.

Note:

Prototype

  1. jerry_value_t
  2. jerry_create_string_sz_from_utf8 (const jerry_char_t *str_p,
  3. jerry_size_t str_size)
  • str_p - non-null pointer to string
  • str_size - size of the string
  • return value - value of the created string

New in version 2.0.

Example

  1. {
  2. const jerry_char_t char_array[] = "a string";
  3. jerry_value_t string_value = jerry_create_string_sz_from_utf8 (char_array,
  4. sizeof (char_array) - 1);
  5. ... // usage of string_value
  6. jerry_release_value (string_value);
  7. }

See also

jerry_create_symbol

Summary

Create symbol from an API value.

Note:

  • The given argument is converted to string. This operation can throw an error.
  • This API depends on the ES2015-subset profile.
  • Returned value must be freed with jerry_release_value when it is no longer needed.

Prototype

  1. jerry_value_t
  2. jerry_create_symbol (const jerry_value_t value)
  • value - API value
  • return value
    • value of the created symbol, if success
    • thrown error, otherwise

New in version 2.0.

Example

  1. #include "jerryscript.h"
  2. int
  3. main (void)
  4. {
  5. jerry_init (JERRY_INIT_EMPTY);
  6. jerry_value_t string_value = jerry_create_string ((const jerry_char_t *) "Symbol description string");
  7. jerry_value_t symbol_value = jerry_create_symbol (string_value);
  8. // The description value is no longer needed
  9. jerry_release_value (string_value);
  10. // usage of symbol_value
  11. jerry_release_value (symbol_value);
  12. jerry_cleanup ();
  13. }

See also

jerry_create_regexp

Summary

Returns a jerry_value_t RegExp object or an error, if the construction of the object fails. Optional flags can be set using jerry_regexp_flags_t. These flags can be combined together with the binary OR operator or used on their own as enum values.

Note: Returned value must be freed with jerry_release_value when it is no longer needed.

Prototype

  1. jerry_value_t
  2. jerry_create_regexp (const jerry_char_t *pattern_p, uint16_t flags);
  • pattern_p - the RegExp pattern as a zero-terminated UTF-8 string
  • flags - optional flags for the RegExp object, see jerry_regexp_flags_t
  • return value - the RegExp object as a jerry_value_t

New in version 2.0.

Example

  1. {
  2. jerry_char_t pattern_p = "[cgt]gggtaaa|tttaccc[acg]";
  3. uint16_t pattern_flags = JERRY_REGEXP_FLAG_IGNORE_CASE;
  4. jerry_value_t regexp = jerry_create_regexp (pattern_p, pattern_flags);
  5. ...
  6. jerry_release_value (regexp);
  7. }

jerry_create_regexp_sz

Summary

Returns a jerry_value_t RegExp object or an error, if the construction of the object fails. Optional flags can be set using jerry_regexp_flags_t. These flags can be combined together with the binary OR operator or used on their own as enum values.

Note: Returned value must be freed with jerry_release_value when it is no longer needed.

Prototype

  1. jerry_value_t
  2. jerry_create_regexp_sz (const jerry_char_t *pattern_p, jerry_size_t pattern_size, uint16_t flags);
  • pattern_p - the RegExp pattern as a zero-terminated UTF-8 string
  • pattern_size - size of the pattern
  • flags - optional flags for the RegExp object, see jerry_regexp_flags_t
  • return value - the RegExp object as a jerry_value_t

New in version 2.0.

Example

  1. {
  2. jerry_char_t pattern_p = "[cgt]gggtaaa|tttaccc[acg]";
  3. jerry_size_t pattern_size = sizeof (pattern_p) - 1;
  4. uint16_t pattern_flags = JERRY_REGEXP_FLAG_IGNORE_CASE;
  5. jerry_value_t regexp = jerry_create_regexp_sz (pattern_p, pattern_size, pattern_flags);
  6. ...
  7. jerry_release_value (regexp);
  8. }

jerry_create_typedarray

Summary

Create a jerry_value_t representing an TypedArray object.

For the new object the type of the TypedArray (see: jerry_typedarray_type_t) and element count can be specified.

Notes:

  • Returned value must be freed with jerry_release_value when it is no longer needed.
  • This API depends on a build option (JERRY_ES2015_BUILTIN_TYPEDARRAY) and can be checked in runtime with the JERRY_FEATURE_TYPEDARRAY feature enum value, see: jerry_is_feature_enabled.
  • The ES2015-subset profile enables this by default.

Prototype

  1. jerry_value_t
  2. jerry_create_typedarray (jerry_typedarray_type_t type_name, jerry_length_t item_count);
  • type_name - type of TypedArray to create
  • item_count - number of items in the new TypedArray
  • return value - the new TypedArray as a jerry_value_t

New in version 2.0.

Example

  1. {
  2. jerry_value_t array = jerry_create_typedarray (JERRY_TYPEDARRAY_UINT16, 15);
  3. ... // use the TypedArray
  4. jerry_release_value (array);
  5. }

See also

jerry_create_typedarray_for_arraybuffer

Summary

Create a jerry_value_t representing an TypedArray object using an already existing ArrayBuffer object.

For the new object the type of the TypedArray (see: jerry_typedarray_type_t) and element count can be specified.

The developer must ensure that the ArrayBuffer has the correct length for the given type of TypedArray otherwise an error is generated.

The JavaScript equivalent of this function is: new %TypedArray%(arraybuffer) where %TypedArray% is one of the allowed TypedArray functions.

Notes:

  • Returned value must be freed with jerry_release_value when it is no longer needed.
  • This API depends on a build option (JERRY_ES2015_BUILTIN_TYPEDARRAY) and can be checked in runtime with the JERRY_FEATURE_TYPEDARRAY feature enum value, see: jerry_is_feature_enabled.
  • The ES2015-subset profile enables this by default.

Prototype

  1. jerry_value_t
  2. jerry_create_typedarray_for_arraybuffer (jerry_typedarray_type_t type_name,
  3. const jerry_value_t arraybuffer);
  • type_name - type of TypedArray to create
  • arraybuffer - the ArrayBuffer to use for the new TypedArray
  • return value
    • the new TypedArray as a jerry_value_t
    • Error if the ArrayBuffer does not have enough space for the given type of TypedArray

New in version 2.0.

Example

  1. {
  2. jerry_value_t buffer = jerry_create_array_buffer (12 * 2);
  3. jerry_value_t array = jerry_create_typedarray_for_arraybuffer (JERRY_TYPEDARRAY_UINT16, buffer);
  4. jerry_release_value (buffer);
  5. ... // use the TypedArray
  6. jerry_release_value (array);
  7. }

See also

jerry_create_typedarray_for_arraybuffer_sz

Summary

Create a jerry_value_t representing an TypedArray object using an already existing ArrayBuffer object and by specifying the byteOffset, and length properties.

For the new object the type of the TypedArray (see: jerry_typedarray_type_t) and element count can be specified.

The developer must ensure that the ArrayBuffer has the correct length for the given type of TypedArray otherwise an error is generated.

The JavaScript equivalent of this function is: new %TypedArray%(arraybuffer, byteOffset, length) where %TypedArray% is one of the allowed TypedArray functions.

Notes:

  • Returned value must be freed with jerry_release_value when it is no longer needed.
  • This API depends on a build option (JERRY_ES2015_BUILTIN_TYPEDARRAY) and can be checked in runtime with the JERRY_FEATURE_TYPEDARRAY feature enum value, see: jerry_is_feature_enabled.
  • The ES2015-subset profile enables this by default.

Prototype

  1. jerry_value_t
  2. jerry_create_typedarray_for_arraybuffer_sz (jerry_typedarray_type_t type_name,
  3. const jerry_value_t arraybuffer,
  4. jerry_length_t byte_offset,
  5. jerry_length_t length);
  • type_name - type of TypedArray to create
  • arraybuffer - the ArrayBuffer to use for the new TypedArray
  • byte_offset - start offset to use for the ArrayBuffer
  • length - number of elements to used from the ArrayBuffer (this is not the same as the byteLength)
  • return value
    • the new TypedArray as a jerry_value_t
    • Error if the ArrayBuffer does not have enough space for the given type of TypedArray

New in version 2.0.

Example

  1. {
  2. jerry_value_t buffer = jerry_create_array_buffer (12 * 2);
  3. jerry_value_t array = jerry_create_typedarray_for_arraybuffer_sz (JERRY_TYPEDARRAY_UINT16, buffer, 4, 10);
  4. jerry_release_value (buffer);
  5. ... // use the TypedArray
  6. jerry_release_value (array);
  7. }

See also

jerry_create_undefined

Summary

Creates a jerry_value_t representing an undefined value.

Prototype

  1. jerry_value_t
  2. jerry_create_undefined (void);
  • return value - value of undefined

Example

  1. {
  2. jerry_value_t undefined_value = jerry_create_undefined ();
  3. ... // usage of the value
  4. jerry_release_value (undefined_value);
  5. }

See also

General API functions of JS objects

jerry_has_property

Summary

Checks whether the object or its prototype objects have the given property.

Note: Returned value must be freed with jerry_release_value when it is no longer needed.

Prototype

  1. jerry_value_t
  2. jerry_has_property (const jerry_value_t obj_val,
  3. const jerry_value_t prop_name_val);
  • obj_val - object value
  • prop_name_val - property name
  • return value - JavaScript boolean value that evaluates to
    • true, if the property exists
    • false, otherwise

Changed in version 2.0: The return value type is now a JavaScript value and not a primitive boolean value.

Example

  1. #include "jerryscript.h"
  2. int
  3. main (void)
  4. {
  5. jerry_init (JERRY_INIT_EMPTY);
  6. jerry_value_t global_object = jerry_get_global_object ();
  7. jerry_value_t prop_name = jerry_create_string ((const jerry_char_t *) "handler_field");
  8. jerry_value_t has_prop_js = jerry_has_property (global_object, prop_name);
  9. bool has_prop = jerry_get_boolean_value (has_prop_js);
  10. jerry_release_value (has_prop_js);
  11. jerry_release_value (prop_name);
  12. jerry_release_value (global_object);
  13. jerry_cleanup ();
  14. return 0;
  15. }

See also

jerry_has_own_property

Summary

Checks whether the object has the given property.

Note: Returned value must be freed with jerry_release_value when it is no longer needed.

Prototype

  1. jerry_value_t
  2. jerry_has_own_property (const jerry_value_t obj_val,
  3. const jerry_value_t prop_name_val);
  • obj_val - object value
  • prop_name_val - property name
  • return value - JavaScript boolean value that evaluates to
    • true, if the property exists
    • false, otherwise

Changed in version 2.0: The return value type is now a JavaScript value and not a primitive boolean value.

Example

  1. #include "jerryscript.h"
  2. int
  3. main (void)
  4. {
  5. jerry_init (JERRY_INIT_EMPTY);
  6. jerry_value_t global_object = jerry_get_global_object ();
  7. jerry_value_t prop_name = jerry_create_string ((const jerry_char_t *) "handler_field");
  8. jerry_value_t has_prop_js = jerry_has_own_property (global_object, prop_name);
  9. bool has_prop = jerry_get_boolean_value (has_prop_js);
  10. jerry_release_value (has_prop_js);
  11. jerry_release_value (prop_name);
  12. jerry_release_value (global_object);
  13. jerry_cleanup ();
  14. return 0;
  15. }

See also

jerry_has_internal_property

Summary

Checks whether the object has the given internal property.

Note:

Prototype

  1. bool
  2. jerry_has_internal_property (const jerry_value_t obj_val,
  3. const jerry_value_t prop_name_val);
  • obj_val - object value
  • prop_name_val - property name
  • return value
    • true, if the property exists
    • false, otherwise

New in version 2.2.

Example

  1. #include "jerryscript.h"
  2. int
  3. main (void)
  4. {
  5. jerry_init (JERRY_INIT_EMPTY);
  6. jerry_value_t global_object = jerry_get_global_object ();
  7. jerry_value_t prop_name = jerry_create_string ((const jerry_char_t *) "hidden_property");
  8. bool has_internal_js_prop = jerry_has_internal_property (global_object, prop_name);
  9. jerry_release_value (prop_name);
  10. jerry_release_value (global_object);
  11. return 0;
  12. }

See also

jerry_delete_property

Summary

Delete a property from an object.

Prototype

  1. bool
  2. jerry_delete_property (const jerry_value_t obj_val,
  3. const jerry_value_t prop_name_val);
  • obj_val - object value
  • prop_name_val - property name
  • return value
    • true, if property was deleted successfully
    • false, otherwise

Example

  1. {
  2. jerry_value_t global_object = jerry_get_global_object ();
  3. jerry_value_t prop_name = jerry_create_string ((const jerry_char_t *) "my_prop");
  4. bool delete_result = jerry_delete_property (global_object, prop_name);
  5. /* use "delete_result" */
  6. jerry_release_value (prop_name);
  7. jerry_release_value (global_object);
  8. }

See also

jerry_delete_property_by_index

Summary

Delete indexed property from the specified object.

Prototype

  1. bool
  2. jerry_delete_property_by_index (const jerry_value_t obj_val,
  3. uint32_t index);
  • obj_val - object value
  • index - index number
  • return value
    • true, if property was deleted successfully
    • false, otherwise

New in version 2.0.

Example

  1. {
  2. jerry_value_t object;
  3. ... // create or acquire object
  4. bool delete_result = jerry_delete_property_by_index (object, 5);
  5. jerry_release_value (object);
  6. }

See also

jerry_delete_internal_property

Summary

Delete an internal property from an object.

Note: Properties which were not created with jerry_set_internal_property are excluded during the operation.

Prototype

  1. bool
  2. jerry_delete_internal_property (const jerry_value_t obj_val,
  3. const jerry_value_t prop_name_val);
  • obj_val - object value
  • prop_name_val - property name
  • return value
    • true, if property was deleted successfully
    • false, otherwise

New in version 2.2.

Example

  1. {
  2. jerry_value_t global_object = jerry_get_global_object ();
  3. jerry_value_t prop_name = jerry_create_string ((const jerry_char_t *) "hidden_property");
  4. bool delete_result = jerry_delete_internal_property (global_object, prop_name);
  5. /* use "delete_result" */
  6. jerry_release_value (prop_name);
  7. jerry_release_value (global_object);
  8. }

See also

jerry_get_property

Summary

Get value of a property to the specified object with the given name.

Note: Returned value must be freed with jerry_release_value when it is no longer needed.

Prototype

  1. jerry_value_t
  2. jerry_get_property (const jerry_value_t obj_val,
  3. const jerry_value_t prop_name_val);
  • obj_val - object value
  • prop_name_val - property name
  • return value
    • value of property, if success
    • thrown error, otherwise

Example

  1. #include "jerryscript.h"
  2. int
  3. main (void)
  4. {
  5. jerry_init (JERRY_INIT_EMPTY);
  6. jerry_value_t global_object = jerry_get_global_object ();
  7. jerry_value_t prop_name = jerry_create_string ((const jerry_char_t *) "Object");
  8. jerry_value_t prop_value = jerry_get_property (global_object, prop_name);
  9. /* use "prop_value" then release it. */
  10. jerry_release_value (prop_value);
  11. jerry_release_value (prop_name);
  12. jerry_release_value (global_object);
  13. return 0;
  14. }

See also

jerry_get_property_by_index

Summary

Get value by an index from the specified object.

Note: Returned value must be freed with jerry_release_value when it is no longer needed.

Prototype

  1. jerry_value_t
  2. jerry_get_property_by_index (const jerry_value_t obj_val,
  3. uint32_t index);
  • obj_val - object value
  • index - index number
  • return value
    • stored value on the specified index, if success
    • thrown exception, otherwise.

Example

  1. {
  2. jerry_value_t object;
  3. ... // create or acquire object
  4. jerry_value_t value = jerry_get_property_by_index (object, 5);
  5. ...
  6. jerry_release_value (value);
  7. jerry_release_value (object);
  8. }

See also

jerry_get_internal_property

Summary

Get value of an internal property to the specified object with the given name.

Note:

Prototype

  1. jerry_value_t
  2. jerry_get_internal_property (const jerry_value_t obj_val,
  3. const jerry_value_t prop_name_val);
  • obj_val - object value
  • prop_name_val - property name
  • return value
    • value of property, if the internal property exists
    • undefined value, if the, if the internal does not property exists
    • thrown error, otherwise

New in version 2.2.

Example

  1. #include "jerryscript.h"
  2. int
  3. main (void)
  4. {
  5. jerry_init (JERRY_INIT_EMPTY);
  6. jerry_value_t global_object = jerry_get_global_object ();
  7. jerry_value_t prop_name = jerry_create_string ((const jerry_char_t *) "hidden_property");
  8. jerry_value_t prop_value = jerry_get_internal_property (global_object, prop_name);
  9. /* use "prop_value" then release it. */
  10. jerry_release_value (prop_value);
  11. jerry_release_value (prop_name);
  12. jerry_release_value (global_object);
  13. return 0;
  14. }

See also

jerry_set_property

Summary

Set a property to the specified object with the given name.

Note: Returned value must be freed with jerry_release_value when it is no longer needed.

Prototype

  1. jerry_value_t
  2. jerry_set_property (const jerry_value_t obj_val,
  3. const jerry_value_t prop_name_val,
  4. const jerry_value_t value_to_set)
  • obj_val - object value
  • prop_name_val - property name
  • value_to_set - value to set
  • return value
    • true, if success
    • thrown error, otherwise

Example

  1. {
  2. jerry_value_t value_to_set;
  3. ... // create or acquire value to set
  4. jerry_value_t glob_obj = jerry_get_global_object ();
  5. jerry_value_t prop_name = jerry_create_string ((const jerry_char_t *) "my_prop");
  6. jerry_value_t set_result = jerry_set_property (glob_obj, prop_name, value_to_set);
  7. ... // check result of property set call
  8. jerry_release_value (set_result);
  9. jerry_release_value (prop_name);
  10. ...
  11. jerry_release_value (value_to_set);
  12. jerry_release_value (glob_obj);
  13. }

See also

jerry_set_property_by_index

Summary

Set indexed value in the specified object

Note: Returned value must be freed with jerry_release_value when it is no longer needed.

Prototype

  1. jerry_value_t
  2. jerry_set_property_by_index (const jerry_value_t obj_val,
  3. uint32_t index,
  4. const jerry_value_t value_to_set);
  • obj_val - object value
  • index - index number
  • value_to_set - value to set
  • return value
    • true, if field value was set successfully
    • thrown exception, otherwise

Example

  1. {
  2. jerry_value_t object;
  3. jerry_value_t value_to_set;
  4. ... // create or acquire object and value to set
  5. jerry_value_t ret_val = jerry_set_property_by_index (object, 5, value_to_set);
  6. ...
  7. jerry_release_value (value_to_set);
  8. jerry_release_value (ret_val);
  9. jerry_release_value (object);
  10. }

See also

jerry_set_internal_property

Summary

Set an internal property to the specified object with the given name.

Note:

  • The property cannot be accessed from the JavaScript context, only from the public API.
  • It is different from jerry_set_object_native_pointer in that any jerry API value can be hidden from the JavaScript context, not only native pointers.

Prototype

  1. bool
  2. jerry_set_internal_property (const jerry_value_t obj_val,
  3. const jerry_value_t prop_name_val,
  4. const jerry_value_t value_to_set)
  • obj_val - object value
  • prop_name_val - property name
  • value_to_set - value to set
  • return value
    • true, if success
    • thrown error, otherwise

New in version 2.2.

Example

  1. #include "jerryscript.h"
  2. int
  3. main (void)
  4. {
  5. jerry_init (JERRY_INIT_EMPTY);
  6. jerry_value_t global_object = jerry_get_global_object ();
  7. jerry_value_t prop_name = jerry_create_string ((const jerry_char_t *) "hidden_property");
  8. jerry_value_t value_to_set = jerry_create_number (5);
  9. bool set_result = jerry_set_internal_property (global_object, prop_name, value_to_set);
  10. /* check the result of internal property set call */
  11. jerry_release_value (value_to_set);
  12. jerry_release_value (prop_name);
  13. jerry_release_value (global_object);
  14. return 0;
  15. }

See also

jerry_init_property_descriptor_fields

Summary

Initialize property descriptor. This means that all fields in the jerry_property_descriptor_t struct will be set to zero or false depending on the field’s type.

Prototype

  1. void
  2. jerry_init_property_descriptor_fields (jerry_property_descriptor_t *prop_desc_p);
  • prop_desc_p - pointer to property descriptor

Example

  1. {
  2. jerry_property_descriptor_t prop_desc;
  3. jerry_init_property_descriptor_fields (&prop_desc);
  4. ... // usage of prop_desc
  5. jerry_free_property_descriptor_fields (&prop_desc);
  6. }

For a more complete example see jerry_define_own_property.

See also

jerry_define_own_property

Summary

Define a property to the specified object with the given name.

Note: Returned value must be freed with jerry_release_value when it is no longer needed.

Prototype

  1. jerry_value_t
  2. jerry_define_own_property (const jerry_value_t obj_val,
  3. const jerry_value_t prop_name_val,
  4. const jerry_property_descriptor_t *prop_desc_p);
  • obj_val - target object where the property should be registered
  • prop_name_val - property name
  • prop_desc_p - pointer to property descriptor
  • return value
    • true, if success
    • thrown error, otherwise

Example

Registering a simple value property via the jerry_define_own_property method:

  1. #include "jerryscript.h"
  2. int
  3. main (void)
  4. {
  5. jerry_init (JERRY_INIT_EMPTY);
  6. jerry_value_t global_obj_val = jerry_get_global_object ();
  7. // configure the property
  8. jerry_property_descriptor_t prop_desc;
  9. jerry_init_property_descriptor_fields (&prop_desc);
  10. jerry_value_t value_to_set;
  11. // create or acquire value to set
  12. // For example:
  13. value_to_set = jerry_create_number (33);
  14. // set the property descriptor fields:
  15. // set the "is_value_defined" field to "true" to indicate the "value"
  16. // field should be used during the property registration.
  17. prop_desc.is_value_defined = true;
  18. // set the "value" field to the number 33
  19. prop_desc.value = value_to_set;
  20. // add the property as "my_prop" for the global object
  21. jerry_value_t prop_name = jerry_create_string ((const jerry_char_t *) "my_prop");
  22. jerry_value_t return_value = jerry_define_own_property (global_obj_val, prop_name, &prop_desc);
  23. if (jerry_value_is_error (return_value))
  24. {
  25. // there was an error
  26. }
  27. // if there was no error at this point the global object should have a "my_prop" property
  28. jerry_release_value (return_value);
  29. jerry_release_value (prop_name);
  30. jerry_free_property_descriptor_fields (&prop_desc);
  31. jerry_release_value (global_obj_val);
  32. jerry_cleanup ();
  33. return 0;
  34. }

Registering a getter/setter property via the jerry_define_own_property method:

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "jerryscript.h"
  4. static int counter = 0;
  5. static jerry_value_t
  6. method_getter (const jerry_value_t this_obj,
  7. const jerry_value_t func_obj,
  8. const jerry_value_t args[],
  9. const jerry_length_t argc)
  10. {
  11. counter++;
  12. printf("Getter called, returning: %d\n", counter);
  13. return jerry_create_number (counter);
  14. }
  15. static jerry_value_t
  16. method_setter (const jerry_value_t this_obj,
  17. const jerry_value_t func_obj,
  18. const jerry_value_t args[],
  19. const jerry_length_t argc)
  20. {
  21. // Note: the arguments count and type should be checked
  22. // in this example it is ommitted!
  23. double new_value = jerry_get_number_value (args[0]);
  24. counter = (int) new_value;
  25. printf("Setter called, setting: %d\n", counter);
  26. return jerry_create_undefined ();
  27. }
  28. int
  29. main (void)
  30. {
  31. jerry_init (JERRY_INIT_EMPTY);
  32. jerry_value_t global_obj_val = jerry_get_global_object ();
  33. // configure the property
  34. jerry_property_descriptor_t prop_desc;
  35. jerry_init_property_descriptor_fields (&prop_desc);
  36. // set the property descriptor fields:
  37. prop_desc.is_get_defined = true;
  38. prop_desc.getter = jerry_create_external_function (method_getter);
  39. prop_desc.is_set_defined = true;
  40. prop_desc.setter = jerry_create_external_function (method_setter);
  41. // add the property as "my_prop" for the global object
  42. jerry_value_t prop_name = jerry_create_string ((const jerry_char_t *) "my_prop");
  43. jerry_value_t return_value = jerry_define_own_property (global_obj_val, prop_name, &prop_desc);
  44. if (jerry_value_is_error (return_value))
  45. {
  46. // there was an error
  47. }
  48. // if there was no error at this point the global object should have a "my_prop" property
  49. jerry_release_value (return_value);
  50. jerry_release_value (prop_name);
  51. jerry_free_property_descriptor_fields (&prop_desc);
  52. jerry_release_value (global_obj_val);
  53. // run an example js code to use the getter/setters
  54. const char *src_p = "this.my_prop; this.my_prop; this.my_prop = 4; this.my_prop";
  55. jerry_value_t eval_result = jerry_eval ((const jerry_char_t *) src_p, strlen (src_p), JERRY_PARSE_NO_OPTS);
  56. // "eval_result" is the last result of "this.my_prop" that is "5" currently.
  57. double result_number = jerry_get_number_value (eval_result);
  58. printf("output: %lf\n", result_number);
  59. jerry_cleanup ();
  60. return result_number != 5.0;
  61. }

See also

jerry_get_own_property_descriptor

Summary

Construct property descriptor from specified property.

Prototype

  1. bool
  2. jerry_get_own_property_descriptor (const jerry_value_t obj_val,
  3. const jerry_value_t prop_name_val,
  4. jerry_property_descriptor_t *prop_desc_p);
  • obj_val - object value
  • prop_name_val - property name
  • prop_desc_p - pointer to property descriptor
  • return value

Example

  1. {
  2. jerry_value_t global_obj_val = jerry_get_global_object ();
  3. jerry_property_descriptor_t prop_desc;
  4. jerry_init_property_descriptor_fields (&prop_desc);
  5. jerry_value_t prop_name = jerry_create_string ((const jerry_char_t *) "my_prop");
  6. jerry_get_own_property_descriptor (global_obj_val, prop_name, &prop_desc);
  7. jerry_release_value (prop_name);
  8. ... // usage of property descriptor
  9. jerry_free_property_descriptor_fields (&prop_desc);
  10. jerry_release_value (global_obj_val);
  11. }

See also

jerry_free_property_descriptor_fields

Summary

Free fields of property descriptor (setter, getter and value).

Prototype

  1. void
  2. jerry_free_property_descriptor_fields (const jerry_property_descriptor_t *prop_desc_p);
  • prop_desc_p - pointer to property descriptor

Example

  1. {
  2. jerry_property_descriptor_t prop_desc;
  3. jerry_init_property_descriptor_fields (&prop_desc);
  4. ... // usage of property descriptor
  5. jerry_free_property_descriptor_fields (&prop_desc);
  6. }

See also

jerry_call_function

Summary

Call function specified by a function value. Error flag must not be set for any arguments of this function. Value of this parameter should be set to undefined for non-method calls.

Note: Returned value must be freed with jerry_release_value when it is no longer needed.

Prototype

  1. jerry_value_t
  2. jerry_call_function (const jerry_value_t func_obj_val,
  3. const jerry_value_t this_val,
  4. const jerry_value_t args_p[],
  5. jerry_size_t args_count);
  • func_obj_val - the function object to call
  • this_val - object for ‘this’ binding
  • args_p - function’s call arguments
  • args_count - number of arguments
  • return value - returned jerry value of the called function

Example

  1. {
  2. jerry_value_t target_function;
  3. ... // create or get "target_function"
  4. if (jerry_value_is_function (target_function))
  5. {
  6. jerry_value_t this_val = jerry_create_undefined ();
  7. jerry_value_t ret_val = jerry_call_function (target_function, this_val, NULL, 0);
  8. if (!jerry_value_is_error (ret_val))
  9. {
  10. ... // handle return value
  11. }
  12. jerry_release_value (ret_val);
  13. jerry_release_value (this_val);
  14. }
  15. jerry_release_value (target_function);
  16. }

See also

jerry_construct_object

Summary

Construct object, invoking specified function object as constructor. Error flag must not be set for any arguments of this function.

Note: Returned value must be freed with jerry_release_value when it is no longer needed.

Prototype

  1. jerry_value_t
  2. jerry_construct_object (const jerry_value_t func_obj_val,
  3. const jerry_value_t args_p[],
  4. jerry_size_t args_count);
  • func_obj_val - function object to call
  • args_p - function’s call arguments
  • args_count - number of arguments
  • return value - returned value of the invoked constructor

Example

  1. {
  2. jerry_value_t val;
  3. ... // receiving val
  4. if (jerry_is_constructor (val))
  5. {
  6. jerry_value_t ret_val = jerry_construct_object (val, NULL, 0);
  7. if (!jerry_value_is_error (ret_val))
  8. {
  9. ... // handle return value
  10. }
  11. jerry_release_value (ret_val);
  12. }
  13. }

See also

jerry_get_object_keys

Summary

Get keys of the specified object value.

Note: Returned value must be freed with jerry_release_value when it is no longer needed.

Prototype

  1. jerry_value_t
  2. jerry_get_object_keys (const jerry_value_t obj_val);
  • obj_val - object value
  • return value
    • array object value, if success
    • thrown error, otherwise

Example

  1. {
  2. jerry_value_t object;
  3. ... // create or acquire object
  4. jerry_value_t keys_array = jerry_get_object_keys (object);
  5. ... // usage of keys_array
  6. jerry_release_value (keys_array);
  7. }

See also

jerry_get_prototype

Summary

Get the prototype of the specified object.

Note: Returned value must be freed with jerry_release_value when it is no longer needed.

Prototype

  1. jerry_value_t
  2. jerry_get_prototype (const jerry_value_t obj_val);
  • obj_val - object value
  • return value
    • object value, if success
    • null or thrown error, otherwise

Example

  1. {
  2. jerry_value_t object;
  3. ... // create or acquire object
  4. jerry_value_t prototype = jerry_get_prototype (object);
  5. ... // usage of prototype object
  6. jerry_release_value (prototype);
  7. jerry_release_value (object);
  8. }

See also

jerry_set_prototype

Summary

Set the prototype of the specified object.

Note: Returned value must be freed with jerry_release_value when it is no longer needed.

Prototype

  1. jerry_value_t
  2. jerry_set_prototype (const jerry_value_t obj_val,
  3. const jerry_value_t proto_obj_val);
  • obj_val - object value
  • proto_obj_val - prototype object value
  • return value
    • true, if success
    • thrown error, otherwise

Example

  1. {
  2. jerry_value_t object;
  3. jerry_value_t prototype;
  4. ... // create or acquire object and prototype
  5. jerry_value_t ret_val = jerry_set_prototype (object, prototype);
  6. jerry_release_value (ret_val);
  7. jerry_release_value (prototype);
  8. jerry_release_value (object);
  9. }

See also

jerry_get_object_native_pointer

Summary

Get native pointer by the given type information. The pointer and the type information are previously associated with the object by jerry_set_object_native_pointer.

Note: out_native_pointer_p can be NULL, and it means the caller doesn’t want to get the native_pointer.

Prototype

  1. bool
  2. jerry_get_object_native_pointer (const jerry_value_t obj_val,
  3. void **out_native_pointer_p,
  4. const jerry_object_native_info_t *native_info_p)
  • obj_val - object value to get native pointer from.
  • out_native_pointer_p - native pointer (output parameter).
  • native_info_p - native pointer’s type information.
  • return value
    • true, if there is native pointer associated of the specified object with the given native type info
    • false, otherwise

New in version 2.0: Changed from jerry_get_object_native_handle.

Example

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "jerryscript.h"
  5. typedef struct
  6. {
  7. char *data_p;
  8. unsigned int length;
  9. } buffer_native_object_t;
  10. typedef struct
  11. {
  12. int area;
  13. int perimeter;
  14. } shape_native_object_t;
  15. #define SECRET_INFO ((void *) 42)
  16. static void
  17. buffer_native_freecb (void *native_p)
  18. {
  19. char *data_p = ((buffer_native_object_t*)native_p)->data_p;
  20. if (data_p != NULL)
  21. {
  22. free (data_p);
  23. }
  24. free (native_p);
  25. }
  26. static void
  27. shape_native_freecb (void *native_p)
  28. {
  29. free (native_p);
  30. }
  31. static void
  32. destructor_freecb (void *native_p)
  33. {
  34. printf("Note: the object has been freed\n");
  35. }
  36. // NOTE: The address (!) of type_info acts as a way to uniquely "identify" the
  37. // C type `buffer_native_object_t *`.
  38. static const jerry_object_native_info_t buffer_obj_type_info =
  39. {
  40. .free_cb = buffer_native_freecb
  41. };
  42. // NOTE: The address (!) of type_info acts as a way to uniquely "identify" the
  43. // C type `shape_native_object_t *`.
  44. static const jerry_object_native_info_t shape_obj_type_info =
  45. {
  46. .free_cb = shape_native_freecb
  47. };
  48. // NOTE: The address (!) of type_info is the unique "identifier"
  49. static const jerry_object_native_info_t destructor_obj_type_info =
  50. {
  51. .free_cb = destructor_freecb
  52. };
  53. static void
  54. print_buffer (char *data_p,
  55. unsigned int length)
  56. {
  57. for (unsigned int i = 0; i < length; ++i)
  58. {
  59. printf("%c", data_p[i]);
  60. }
  61. printf("\n");
  62. }
  63. static void
  64. do_stuff (jerry_value_t object)
  65. {
  66. void *native_p;
  67. bool has_p = jerry_get_object_native_pointer (object, &native_p, &buffer_obj_type_info);
  68. if (!has_p)
  69. {
  70. // Process the error
  71. return;
  72. }
  73. // It is safe to cast to buffer_native_object_t * and dereference the pointer:
  74. buffer_native_object_t *buffer_p = (buffer_native_object_t *) native_p;
  75. print_buffer (buffer_p->data_p, buffer_p->length); // Usage of buffer_p
  76. bool need_shape_info = true; // implementation dependent
  77. if (need_shape_info)
  78. {
  79. has_p = jerry_get_object_native_pointer (object, &native_p, &shape_obj_type_info);
  80. if (!has_p)
  81. {
  82. // Process the error
  83. return;
  84. }
  85. // It is safe to cast to shape_native_object_t * and dereference the pointer:
  86. shape_native_object_t *shape_p = (shape_native_object_t *) native_p;
  87. printf("Area: %d\tPerimeter: %d\n", shape_p->area, shape_p->perimeter); // Usage of shape_p
  88. }
  89. bool need_secret_info = true; // implementation dependent
  90. if (need_secret_info)
  91. {
  92. has_p = jerry_get_object_native_pointer (object, &native_p, NULL);
  93. if (!has_p)
  94. {
  95. // Process the error
  96. return;
  97. }
  98. printf("Secret: %d\n", (int)((uintptr_t) native_p)); // Usage of native_p
  99. bool deleted = jerry_delete_object_native_pointer (object, NULL);
  100. if (deleted)
  101. {
  102. printf("The secret is no longer available\n");
  103. }
  104. }
  105. }
  106. int
  107. main (void)
  108. {
  109. jerry_init (JERRY_INIT_EMPTY);
  110. jerry_value_t object = jerry_create_object ();
  111. buffer_native_object_t *buffer_p = (buffer_native_object_t *) malloc (sizeof (buffer_native_object_t));
  112. buffer_p->length = 14;
  113. buffer_p->data_p = (char *) malloc (buffer_p->length * sizeof (char));
  114. memcpy (buffer_p->data_p, "My buffer data", buffer_p->length);
  115. jerry_set_object_native_pointer (object, buffer_p, &buffer_obj_type_info);
  116. shape_native_object_t *shape_p = (shape_native_object_t *) malloc (sizeof (shape_native_object_t));
  117. shape_p->area = 6;
  118. shape_p->perimeter = 12;
  119. jerry_set_object_native_pointer (object, shape_p, &shape_obj_type_info);
  120. // The native pointer can be NULL. This gives possibily to get notified via the native type info's
  121. // free callback when the object has been freed by the GC.
  122. jerry_set_object_native_pointer (object, NULL, &destructor_obj_type_info);
  123. // The native type info can be NULL as well. In this case the registered property is simply freed
  124. // when the object is freed by te GC.
  125. jerry_set_object_native_pointer (object, SECRET_INFO, NULL);
  126. do_stuff (object);
  127. jerry_release_value (object);
  128. jerry_cleanup ();
  129. return 0;
  130. }

See also

jerry_set_object_native_pointer

Summary

Set native pointer and an optional type information for the specified object. You can get them by calling jerry_get_object_native_pointer later.

Notes:

  • If a native pointer was already set for the object with the same type information, its value is updated.
  • If a non-NULL free callback is specified in the native type information, it will be called by the garbage collector when the object is freed.
  • If the object is only referenced via the “global” object (or one of it’s “child”), the free callback will be invoked during the execution of jerry_cleanup.
  • The free callback can invoke API functions.

Note: If possible do not store API values in native pointers, rather check jerry_set_internal_property.

Prototype

  1. void
  2. jerry_set_object_native_pointer (const jerry_value_t obj_val,
  3. void *native_p,
  4. const jerry_object_native_info_t *info_p)
  • obj_val - object to set native pointer in.
  • native_p - native pointer.
  • info_p - native pointer’s type information or NULL. When used, this should be a long-lived pointer, usually a pointer to a static const jerry_object_native_info_t makes most sense.

New in version 2.0: Changed from jerry_set_object_native_handle.

Example

See jerry_get_object_native_pointer for a best-practice example.

See also

jerry_delete_object_native_pointer

Summary

Delete the native pointer of the specified object associated with the given native type info.

Notes:

  • If the specified object has no matching native pointer for the given native type info the operation has no effect.
  • The method does not invoke the free callback specified in the type info. If the native pointer should be freed then one must get the native pointer first and invoke the free callback manually before calling this method.
  • This operation cannot throw an exception.

Prototype

  1. bool
  2. jerry_delete_object_native_pointer (const jerry_value_t obj_val,
  3. const jerry_object_native_info_t *info_p)
  • obj_val - object to delete native pointer from.
  • info_p - native pointer’s type information.

New in version 2.0.

Example

See jerry_get_object_native_pointer for a best-practice example.

See also

jerry_foreach_object_property

Summary

Applies the given function to every enumerable(!) property in the given object.

The “iterator” foreach_p method should return true value to continue the iteration. If the method returns false the iteration will end.

Prototype

  1. bool
  2. jerry_foreach_object_property (jerry_value_t obj_val,
  3. jerry_object_property_foreach_t foreach_p,
  4. void *user_data_p);
  • obj_val - object value
  • foreach_p - foreach function, that will be applied for each property
  • user_data_p - user data for foreach function
  • return value
    • true, if object fields traversal was performed successfully, i.e.:
      • no unhandled exceptions were thrown in object fields traversal
      • object fields traversal was stopped on callback that returned false
    • false, otherwise

Example

  1. #include <stdio.h>
  2. #include "jerryscript.h"
  3. /* Example structure used as user data for the property iteration. */
  4. struct iteration_data {
  5. int string_property_count;
  6. };
  7. /*
  8. * Example foreach function to print out property names.
  9. */
  10. static bool
  11. foreach_function (const jerry_value_t prop_name,
  12. const jerry_value_t prop_value,
  13. void *user_data_p)
  14. {
  15. if (jerry_value_is_string (prop_name)) {
  16. jerry_char_t string_buffer[128];
  17. jerry_size_t copied_bytes = jerry_substring_to_char_buffer (prop_name,
  18. 0,
  19. 127,
  20. string_buffer,
  21. 127);
  22. string_buffer[copied_bytes] = '\0';
  23. printf ("Property: %s\n", string_buffer);
  24. struct iteration_data *data = (struct iteration_data *) user_data_p;
  25. data->string_property_count++;
  26. }
  27. /* return true to continue iteration */
  28. return true;
  29. }
  30. int
  31. main (void)
  32. {
  33. jerry_init (JERRY_INIT_EMPTY);
  34. /* Construct an example object with a single property. */
  35. jerry_value_t object = jerry_create_object ();
  36. {
  37. jerry_value_t test_property = jerry_create_string ((const jerry_char_t *) "DemoProp");
  38. jerry_value_t test_value = jerry_create_number (3);
  39. /* By default all properties added to an object are enumerable. */
  40. jerry_value_t set_result = jerry_set_property (object, test_property, test_value);
  41. /* The `set_result` should be checked if it is an error or not. */
  42. jerry_release_value (set_result);
  43. jerry_release_value (test_value);
  44. jerry_release_value (test_property);
  45. }
  46. /* Iterate on the object's properties with the given user data. */
  47. struct iteration_data user_data = { 0 };
  48. bool iteration_result = jerry_foreach_object_property (object, foreach_function, &user_data);
  49. /* Check and process the `iteration_result` if required. */
  50. jerry_release_value (object);
  51. jerry_cleanup ();
  52. return user_data.string_property_count == 0;
  53. }

See also

jerry_objects_foreach

Summary

Iterate over all objects available in the engine.

The “iterator” foreach_p method should return true value to continue the search. If the method returns false the search for the object is finished.

Note: Values obtained in foreach_p must be retained using jerry_acquire_value.

Prototype

  1. bool
  2. jerry_objects_foreach (jerry_objects_foreach_t foreach_p,
  3. void *user_data_p);
  • foreach_p - function that will be invoked for each object.
  • user_data_p - User data to pass to the function.
  • return value
    • true, if the search function terminated the traversal by returning false
    • false, if the end of the list of objects was reached

New in version 2.0.

Example

  1. #include <stdio.h>
  2. #include "jerryscript.h"
  3. /* Create a custom structure to guide the search and store the result. */
  4. typedef struct
  5. {
  6. jerry_value_t property_name;
  7. jerry_value_t result;
  8. } find_my_object_info_t;
  9. /*
  10. * Find the first object with the given property.
  11. */
  12. static bool
  13. find_my_object (const jerry_value_t candidate,
  14. void *user_data_p)
  15. {
  16. find_my_object_info_t *info_p = (find_my_object_info_t *) user_data_p;
  17. /* Check if the given object has the required property. */
  18. jerry_value_t has_property = jerry_has_property (candidate, info_p->property_name);
  19. bool object_found = jerry_get_boolean_value (has_property);
  20. if (object_found)
  21. {
  22. /* We found it, so we acquire the value and record it. */
  23. info_p->result = jerry_acquire_value (candidate);
  24. }
  25. jerry_release_value (has_property);
  26. /* If the object was not found continue the search. */
  27. return !object_found;
  28. } /* find_my_object */
  29. int
  30. main (void)
  31. {
  32. int return_value = 0;
  33. /* Initialize JerryScript engine. */
  34. jerry_init (JERRY_INIT_EMPTY);
  35. /* Create the test object. */
  36. {
  37. jerry_value_t test_object = jerry_create_object ();
  38. {
  39. jerry_value_t test_property = jerry_create_string ((const jerry_char_t *) "DemoProp");
  40. jerry_value_t test_value = jerry_create_number (3);
  41. jerry_value_t set_result = jerry_set_property (test_object, test_property, test_value);
  42. /* The `set_result` should be checked if it is an error or not. */
  43. jerry_release_value (set_result);
  44. jerry_release_value (test_value);
  45. jerry_release_value (test_property);
  46. }
  47. {
  48. /* Register the test object into the global object. */
  49. jerry_value_t global_object = jerry_get_global_object ();
  50. jerry_value_t demo_property = jerry_create_string ((const jerry_char_t *) "DemoObject");
  51. jerry_value_t set_result = jerry_set_property (global_object, demo_property, test_object);
  52. /* The `set_result` should be checked if it is an error or not. */
  53. jerry_release_value (set_result);
  54. jerry_release_value (demo_property);
  55. jerry_release_value (global_object);
  56. }
  57. jerry_release_value (test_object);
  58. }
  59. /* Look up the test object base on a property name. */
  60. find_my_object_info_t search_info =
  61. {
  62. .property_name = jerry_create_string ((const jerry_char_t *) "DemoProp")
  63. };
  64. if (jerry_objects_foreach (find_my_object, &search_info))
  65. {
  66. /* The search was successful. Do something useful with search_info.result. */
  67. // ...
  68. printf ("Object found\n");
  69. /* Release the found object after we're done using it. */
  70. jerry_release_value (search_info.result);
  71. }
  72. else
  73. {
  74. /* The search has failed. */
  75. printf ("Object not found\n");
  76. return_value = 1;
  77. }
  78. jerry_release_value (search_info.property_name);
  79. /* Engine cleanup */
  80. jerry_cleanup ();
  81. return return_value;
  82. }

See also

jerry_objects_foreach_by_native_info

Summary

Iterate over all objects in the engine matching a certain native data type.

The “iterator” foreach_p method should return true value to continue the search. If the method returns false the search for the object is finished.

Note: Values obtained in foreach_p must be retained using jerry_acquire_value.

Prototype

  1. bool
  2. jerry_objects_foreach_by_native_info (const jerry_object_native_info_t *native_info_p,
  3. jerry_objects_foreach_by_native_info_t foreach_p,
  4. void *user_data_p);
  • native_info_p - native pointer’s type information.
  • foreach_p - function that will be invoked for each object.
  • return value
    • true, if the search function terminated the traversal by returning false
    • false, if the end of the list of objects was reached

New in version 2.0.

Example

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "jerryscript.h"
  4. typedef struct
  5. {
  6. int foo;
  7. bool bar;
  8. } native_obj_t;
  9. typedef struct
  10. {
  11. jerry_value_t found_object;
  12. native_obj_t *found_native_data_p;
  13. int match_foo_value;
  14. } find_object_data_t;
  15. static void native_freecb (void *native_p)
  16. {
  17. /* `native_p` was allocated via malloc. */
  18. free (native_p);
  19. } /* native_freecb */
  20. /*
  21. * NOTE: The address (!) of type_info acts as a way to uniquely "identify" the
  22. * C type `native_obj_t *`.
  23. */
  24. static const jerry_object_native_info_t native_obj_type_info =
  25. {
  26. .free_cb = native_freecb
  27. };
  28. /*
  29. * Function creating JS object that is "backed" by a `native_obj_t`.
  30. */
  31. static void
  32. add_object_with_nativeptr (int foo_value)
  33. {
  34. // construct object and native_set value:
  35. jerry_value_t test_object = jerry_create_object ();
  36. native_obj_t *native_obj_p = malloc (sizeof (*native_obj_p));
  37. native_obj_p->foo = foo_value;
  38. native_obj_p->bar = true;
  39. jerry_set_object_native_pointer (test_object, native_obj_p, &native_obj_type_info);
  40. /* Register the test object into the global object. */
  41. jerry_value_t global_object = jerry_get_global_object ();
  42. jerry_value_t demo_property = jerry_create_string ((const jerry_char_t *) "DemoObject");
  43. jerry_value_t set_result = jerry_set_property (global_object, demo_property, test_object);
  44. /* The `set_result` should be checked if it is an error or not. */
  45. jerry_release_value (set_result);
  46. jerry_release_value (demo_property);
  47. jerry_release_value (global_object);
  48. jerry_release_value (test_object);
  49. } /* create_object_with_nativeptr */
  50. /*
  51. * Example native method that searches for a JavaScript object
  52. * with a `native_obj_type_info` has the correct value.
  53. */
  54. static bool
  55. find_object (const jerry_value_t candidate, void *data_p, void *user_data_p)
  56. {
  57. find_object_data_t *find_data_p = (find_object_data_t *) user_data_p;
  58. native_obj_t *native_obj_p = (native_obj_t *) data_p;
  59. if (find_data_p->match_foo_value == native_obj_p->foo)
  60. {
  61. /* If the object was found, acquire it and store it in the user data. */
  62. find_data_p->found_object = jerry_acquire_value (candidate);
  63. find_data_p->found_native_data_p = native_obj_p;
  64. /* Stop traversing over the objects. */
  65. return false;
  66. }
  67. /* Indicate that the object was not found, so traversal must continue. */
  68. return true;
  69. } /* find_object */
  70. int
  71. main (void)
  72. {
  73. jerry_init (JERRY_INIT_EMPTY);
  74. add_object_with_nativeptr (4);
  75. add_object_with_nativeptr (3);
  76. add_object_with_nativeptr (2);
  77. find_object_data_t find_data =
  78. {
  79. .match_foo_value = 3,
  80. };
  81. if (jerry_objects_foreach_by_native_info (&native_obj_type_info, find_object, &find_data))
  82. {
  83. /* The object was found and is now stored in `find_data.found_object`. After using it, it must be released. */
  84. printf ("Object found, native foo value: %d\n", find_data.found_native_data_p->foo);
  85. jerry_release_value (find_data.found_object);
  86. }
  87. else
  88. {
  89. printf ("Object not found\n");
  90. }
  91. jerry_cleanup ();
  92. return 0;
  93. }

See also

Input validator functions

jerry_is_valid_utf8_string

Summary

Check if a given character buffer is a valid UTF-8 string.

Notes: Calling this method is safe in any time. It can be called even before engine initialization.

Prototype

  1. bool
  2. jerry_is_valid_utf8_string (const jerry_char_t *utf8_buf_p, /**< UTF-8 string */
  3. jerry_size_t buf_size) /**< string size */
  • utf8_buf_p - UTF-8 input string buffer.
  • buf_size - input string buffer size in bytes.
  • return value
    • true, if the provided string was a valid UTF-8 string.
    • false, if the string is not valid as an UTF-8 string.

New in version 2.0.

Example

  1. #include "jerryscript.h"
  2. int
  3. main (void)
  4. {
  5. const jerry_char_t script[] = "print ('Hello, World!');";
  6. const jerry_size_t script_size = sizeof (script) - 1;
  7. if (jerry_is_valid_utf8_string (script, script_size))
  8. {
  9. jerry_run_simple (script, script_size, JERRY_INIT_EMPTY);
  10. }
  11. return 0;
  12. }

See also

jerry_is_valid_cesu8_string

Summary

Check if a given character buffer is a valid CESU-8 string.

Notes: Calling this method is safe in any time. It can be called even before engine initialization.

Prototype

  1. bool
  2. jerry_is_valid_cesu8_string (const jerry_char_t *cesu8_buf_p, /**< CESU-8 string */
  3. jerry_size_t buf_size) /**< string size */
  • cesu8_buf_p - CESU-8 input string buffer.
  • buf_size - input string buffer size in bytes.
  • return value
    • true, if the provided string was a valid CESU-8 string.
    • false, if the string is not valid as a CESU-8 string.

New in version 2.0.

Example

  1. #include "jerryscript.h"
  2. int
  3. main (void)
  4. {
  5. jerry_init (JERRY_INIT_EMPTY);
  6. const jerry_char_t script[] = "Hello, World!";
  7. const jerry_size_t script_size = sizeof (script) - 1;
  8. if (jerry_is_valid_cesu8_string (script, script_size))
  9. {
  10. jerry_value_t string_value = jerry_create_string_sz (script,
  11. script_size);
  12. // usage of string_value
  13. jerry_release_value (string_value);
  14. }
  15. jerry_cleanup ();
  16. return 0;
  17. }

See also

Dynamic memory management functions

jerry_heap_alloc

Summary

Allocate memory on the engine’s heap.

Note: This function may take away memory from the executed JavaScript code. If any other dynamic memory allocation API is available (e.g., libc malloc), it should be used instead.

Prototype

  1. void *jerry_heap_alloc (size_t size);
  • size: size of the memory block.
  • return value: non-NULL pointer, if the memory is successfully allocated, NULL otherwise.

New in version 2.0.

See also

jerry_heap_free

Summary

Free memory allocated on the engine’s heap.

Prototype

  1. void jerry_heap_free (void *mem_p, size_t size);
  • mem_p: value returned by jerry_heap_alloc.
  • size: same size as passed to jerry_heap_alloc.

New in version 2.0.

See also

External context functions

jerry_create_context

Summary

Create an external JerryScript engine context.

Prototype

  1. jerry_context_t *
  2. jerry_create_context (uint32_t heap_size,
  3. jerry_context_alloc_t alloc,
  4. void *cb_data_p);
  • heap_size - requested heap size of the JerryScript context
  • alloc - function for allocation
  • cb_data_p - user data
  • return value
    • pointer to the newly created JerryScript context if success
    • NULL otherwise.

New in version 2.0.

Example

  1. #include <stdlib.h>
  2. #include <pthread.h>
  3. #include "jerryscript.h"
  4. #include "jerryscript-port.h"
  5. /* A different Thread Local Storage variable for each jerry context. */
  6. __thread jerry_context_t *tls_context;
  7. jerry_context_t *
  8. jerry_port_get_current_context (void)
  9. {
  10. /* Returns the context assigned to the thread. */
  11. return tls_context;
  12. }
  13. /* Allocate JerryScript heap for each thread. */
  14. static void *
  15. context_alloc_fn (size_t size, void *cb_data)
  16. {
  17. (void) cb_data;
  18. return malloc (size);
  19. }
  20. static void *
  21. thread_function (void *param)
  22. {
  23. tls_context = jerry_create_context (512 * 1024,
  24. context_alloc_fn,
  25. NULL);
  26. jerry_init (JERRY_INIT_EMPTY);
  27. /* Run JerryScript in the context (e.g.: jerry_parse & jerry_run) */
  28. jerry_cleanup ();
  29. /* Deallocate JerryScript context */
  30. free (tls_context);
  31. return NULL;
  32. }
  33. #define NUM_OF_THREADS 8
  34. int
  35. main (void)
  36. {
  37. pthread_t threads[NUM_OF_THREADS];
  38. /* Create the threads. */
  39. for (int i = 0; i < NUM_OF_THREADS; i++)
  40. {
  41. pthread_create (&threads[i], NULL, thread_function, (void *) (intptr_t) i);
  42. }
  43. /* Wait for the threads to complete, and release their resources. */
  44. for (int i = 0; i < NUM_OF_THREADS; i++)
  45. {
  46. pthread_join (threads[i], NULL);
  47. }
  48. return 0;
  49. }

See also

Snapshot functions

jerry_generate_snapshot

Summary

Generate snapshot from the specified source code.

Notes:

  • Returned value must be freed with jerry_release_value when it is no longer needed.
  • This API depends on a build option (JERRY_SNAPSHOT_SAVE) and can be checked in runtime with the JERRY_FEATURE_SNAPSHOT_SAVE feature enum value, see jerry_is_feature_enabled. If the feature is not enabled the function will return an error.

Prototype

  1. jerry_value_t
  2. jerry_generate_snapshot (const jerry_char_t *resource_name_p,
  3. size_t resource_name_length,
  4. const jerry_char_t *source_p,
  5. size_t source_size,
  6. uint32_t generate_snapshot_opts,
  7. uint32_t *buffer_p,
  8. size_t buffer_size);
  • resource_name_p - resource (file) name of the source code. Currently unused, the debugger may use it in the future.
  • resource_name_length - length of resource name.
  • source_p - script source, it must be a valid utf8 string.
  • source_size - script source size, in bytes.
  • generate_snapshot_opts - any combination of jerry_generate_snapshot_opts_t flags.
  • buffer_p - output buffer (aligned to 4 bytes) to save snapshot to.
  • buffer_size - the output buffer’s size in bytes.
  • return value
    • the size of the generated snapshot in bytes as number value, if it was generated succesfully (i.e. there are no syntax errors in source code, buffer size is sufficient, and snapshot support is enabled in current configuration through JERRY_SNAPSHOT_SAVE)
    • thrown error, otherwise.

New in version 2.0.

Example

  1. #include "jerryscript.h"
  2. int
  3. main (void)
  4. {
  5. jerry_init (JERRY_INIT_EMPTY);
  6. static uint32_t global_mode_snapshot_buffer[256];
  7. const jerry_char_t script_to_snapshot[] = "(function () { return 'string from snapshot'; }) ();";
  8. jerry_value_t generate_result;
  9. generate_result = jerry_generate_snapshot (NULL,
  10. 0,
  11. script_to_snapshot,
  12. sizeof (script_to_snapshot) - 1,
  13. 0,
  14. global_mode_snapshot_buffer,
  15. sizeof (global_mode_snapshot_buffer) / sizeof (uint32_t));
  16. if (!jerry_value_is_error (generate_result))
  17. {
  18. size_t snapshot_size = (size_t) jerry_get_number_value (generate_result);
  19. }
  20. jerry_release_value (generate_result);
  21. jerry_cleanup ();
  22. return 0;
  23. }

See also

jerry_generate_function_snapshot

Summary

Generate function snapshot from the specified source code with the given arguments.

The function arguments and function body are passed as separated arguments.

Notes:

  • Returned value must be freed with jerry_release_value when it is no longer needed.
  • This API depends on a build option (JERRY_SNAPSHOT_SAVE) and can be checked in runtime with the JERRY_FEATURE_SNAPSHOT_SAVE feature enum value, see jerry_is_feature_enabled. If the feature is not enabled the function will return an error.

Prototype

  1. jerry_value_t
  2. jerry_generate_function_snapshot (const jerry_char_t *resource_name_p,
  3. size_t resource_name_length,
  4. const jerry_char_t *source_p,
  5. size_t source_size,
  6. const jerry_char_t *args_p,
  7. size_t args_size,
  8. uint32_t generate_snapshot_opts,
  9. uint32_t *buffer_p,
  10. size_t buffer_size)
  • resource_name_p - resource (file) name of the source code. Currently unused, the debugger may use it in the future.
  • resource_name_length - length of resource name.
  • source_p - script source, it must be a valid utf8 string.
  • source_size - script source size, in bytes.
  • args_p - function arguments, it must be a valid utf8 string.
  • args_size - function argument size, in bytes.
  • generate_snapshot_opts - any combination of jerry_generate_snapshot_opts_t flags.
  • buffer_p - buffer (aligned to 4 bytes) to save snapshot to.
  • buffer_size - the buffer’s size in bytes.
  • return value
    • the size of the generated snapshot in bytes as number value, if it was generated succesfully (i.e. there are no syntax errors in source code, buffer size is sufficient, and snapshot support is enabled in current configuration through JERRY_SNAPSHOT_SAVE)
    • thrown error, otherwise.

New in version 2.0.

Example

  1. #include "jerryscript.h"
  2. int
  3. main (void)
  4. {
  5. jerry_init (JERRY_INIT_EMPTY);
  6. static uint32_t func_snapshot_buffer[256];
  7. const jerry_char_t args[] = "a, b";
  8. const jerry_char_t src[] = "return a + b;";
  9. jerry_value_t generate_result;
  10. generate_result = jerry_generate_function_snapshot (NULL,
  11. 0,
  12. src,
  13. sizeof (src) - 1,
  14. args,
  15. sizeof (args) - 1,
  16. 0,
  17. func_snapshot_buffer,
  18. sizeof (func_snapshot_buffer) / sizeof (uint32_t));
  19. if (!jerry_value_is_error (generate_result))
  20. {
  21. size_t snapshot_size = (size_t) jerry_get_number_value (generate_result);
  22. }
  23. jerry_release_value (generate_result);
  24. jerry_cleanup ();
  25. return 0;
  26. }

See also

jerry_exec_snapshot

Summary

Execute snapshot from the specified buffer.

Notes:

  • Returned value must be freed with jerry_release_value when it is no longer needed.
  • This API depends on a build option (JERRY_SNAPSHOT_EXEC) and can be checked in runtime with the JERRY_FEATURE_SNAPSHOT_EXEC feature enum value, see jerry_is_feature_enabled. If the feature is not enabled the function will return an error.

Prototype

  1. jerry_value_t
  2. jerry_exec_snapshot (const uint32_t *snapshot_p,
  3. size_t snapshot_size,
  4. size_t func_index,
  5. uint32_t exec_snapshot_opts);
  • snapshot_p - pointer to snapshot.
  • snapshot_size - size of snapshot in bytes.
  • func_index - index of executed function.
  • exec_snapshot_opts - any combination of jerry_exec_snapshot_opts_t flags.
  • return value
    • result of bytecode, if run was successful.
    • thrown error, otherwise (an error is reported if the snapshot execution feature is not enabled).

Changed in version 2.0: Added func_index and exec_snapshot_opts arguments. Removed the copy_bytecode last argument.

Example

  1. #include "jerryscript.h"
  2. int
  3. main (void)
  4. {
  5. static uint32_t global_mode_snapshot_buffer[256];
  6. const jerry_char_t script_to_snapshot[] = "(function () { return 'string from snapshot'; }) ();";
  7. jerry_init (JERRY_INIT_EMPTY);
  8. jerry_value_t generate_result;
  9. generate_result = jerry_generate_snapshot (NULL,
  10. 0,
  11. script_to_snapshot,
  12. sizeof (script_to_snapshot) - 1,
  13. 0,
  14. global_mode_snapshot_buffer,
  15. sizeof (global_mode_snapshot_buffer) / sizeof (uint32_t));
  16. // generate_result should be checked if it is an error or not
  17. size_t global_mode_snapshot_size = (size_t) jerry_get_number_value (generate_result);
  18. jerry_release_value (generate_result);
  19. jerry_cleanup ();
  20. jerry_init (JERRY_INIT_EMPTY);
  21. jerry_value_t res = jerry_exec_snapshot (global_mode_snapshot_buffer,
  22. global_mode_snapshot_size,
  23. 0,
  24. 0);
  25. // check the `res` value for error and process the result.
  26. jerry_release_value (res);
  27. jerry_cleanup ();
  28. return 0;
  29. }

See also

jerry_load_function_snapshot

Summary

Load the selected snapshot function from the specified buffer as a function object.

The lexical environment of the loaded function is always the global lexical environment.

Notes:

  • Returned value must be freed with jerry_release_value when it is no longer needed.
  • This API depends on a build option (JERRY_SNAPSHOT_EXEC) and can be checked in runtime with the JERRY_FEATURE_SNAPSHOT_EXEC feature enum value, see jerry_is_feature_enabled. If the feature is not enabled the function will return an error.

Prototype

  1. jerry_value_t
  2. jerry_load_function_snapshot (const uint32_t *snapshot_p,
  3. size_t snapshot_size,
  4. size_t func_index,
  5. uint32_t exec_snapshot_opts);
  • snapshot_p - pointer to snapshot.
  • snapshot_size - size of snapshot in bytes.
  • func_index - index of function to load from the snapshot.
  • exec_snapshot_opts - any combination of jerry_exec_snapshot_opts_t flags.
  • return value
    • function object built from the snapshot.
    • thrown error, otherwise.

New in version 2.0.

Example

  1. #include "jerryscript.h"
  2. int
  3. main (void)
  4. {
  5. static uint32_t snapshot_buffer[256];
  6. const jerry_char_t func_args[] = "a, b";
  7. const jerry_char_t func_src[] = "return a + b;";
  8. jerry_init (JERRY_INIT_EMPTY);
  9. jerry_value_t generate_result;
  10. generate_result = jerry_generate_function_snapshot (NULL,
  11. 0,
  12. func_src,
  13. sizeof (func_src) - 1,
  14. func_args,
  15. sizeof (func_args) - 1,
  16. false,
  17. snapshot_buffer,
  18. sizeof (snapshot_buffer) / sizeof (uint32_t));
  19. size_t snapshot_size = (size_t) jerry_get_number_value (generate_result);
  20. jerry_release_value (generate_result);
  21. jerry_cleanup ();
  22. jerry_init (JERRY_INIT_EMPTY);
  23. jerry_value_t func = jerry_load_function_snapshot (snapshot_buffer,
  24. snapshot_size,
  25. 0,
  26. 0);
  27. /* 'func' can be used now as a function object */
  28. jerry_value_t this_value = jerry_create_undefined ();
  29. jerry_value_t args[2];
  30. args[0] = jerry_create_number (1.0);
  31. args[1] = jerry_create_number (2.0);
  32. jerry_value_t res = jerry_call_function (func, this_value, args, 2);
  33. /* 'res' now contains the value 3 as a jerry_value_t */
  34. jerry_release_value (args[0]);
  35. jerry_release_value (args[1]);
  36. jerry_release_value (this_value);
  37. jerry_release_value (func);
  38. jerry_cleanup ();
  39. return 0;
  40. }

See also

jerry_get_literals_from_snapshot

Summary

Collect the used literals from the given snapshot and save them into a buffer in list or C format. None of these literals are magic strings. In C format only valid identifiers are collected.

Note:

  • This API depends on a build option (JERRY_SNAPSHOT_SAVE) and can be checked in runtime with the JERRY_FEATURE_SNAPSHOT_SAVE feature enum value, see jerry_is_feature_enabled. If the feature is not enabled the function will return zero.

Prototype

  1. size_t
  2. jerry_get_literals_from_snapshot (const uint32_t *snapshot_p,
  3. size_t snapshot_size,
  4. jerry_char_t *lit_buf_p,
  5. size_t lit_buf_size,
  6. bool is_c_format);
  • snapshot_p - input snapshot buffer.
  • snapshot_size - size of snapshot in bytes.
  • lit_buf_p - buffer to save literals to.
  • lit_buf_size - the buffer’s size.
  • is_c_format - the output format would be C-style (true) or a simple list (false).
  • return value
    • the size of the literal-list, if it was generated succesfully (i.e. the list of literals isn’t empty, and literal-save support is enabled in current configuration through JERRY_SNAPSHOT_SAVE)
    • 0 otherwise.

New in version 2.0.

Example

  1. #include <stdio.h>
  2. #include "jerryscript.h"
  3. int
  4. main (void)
  5. {
  6. jerry_init (JERRY_INIT_EMPTY);
  7. static jerry_char_t literal_buffer[256];
  8. static uint32_t snapshot_buffer[256];
  9. const jerry_char_t script_for_literal_save[] = "var obj = { a:'aa', bb:'Bb' }";
  10. jerry_value_t generate_result = jerry_generate_snapshot (NULL,
  11. 0,
  12. script_for_literal_save,
  13. sizeof (script_for_literal_save) - 1,
  14. 0,
  15. snapshot_buffer,
  16. 256);
  17. size_t snapshot_size = (size_t) jerry_get_number_value (generate_result);
  18. jerry_release_value (generate_result);
  19. const size_t literal_size = jerry_get_literals_from_snapshot (snapshot_buffer,
  20. snapshot_size,
  21. literal_buffer,
  22. 256,
  23. true);
  24. if (literal_size != 0)
  25. {
  26. FILE *literal_file_p = fopen ("literals.h", "wb");
  27. fwrite (literal_buffer, sizeof (uint8_t), literal_size, literal_file_p);
  28. fclose (literal_file_p);
  29. }
  30. jerry_cleanup ();
  31. return 0;
  32. }

See also

Miscellaneous functions

jerry_set_vm_exec_stop_callback

Summary

When JERRY_FEATURE_VM_EXEC_STOP is enabled a callback function can be specified by this function. This callback is periodically called when JerryScript executes an ECMAScript program.

If the callback returns with undefined value the ECMAScript execution continues. Otherwise the result is thrown by the engine (if the error flag is not set for the returned value the engine automatically sets it). The callback function might be called again even if it threw an error. In this case the function must throw the same error again.

To reduce the CPU overhead of constantly checking the termination condition the callback is called when a backward jump is executed or an exception is caught. Setting the frequency to a greater than 1 value reduces this overhead further. If its value is N only every Nth event (backward jump, etc.) trigger the next check.

Prototype

  1. void
  2. jerry_set_vm_exec_stop_callback (jerry_vm_exec_stop_callback_t stop_cb,
  3. void *user_p,
  4. uint32_t frequency);
  • stop_cb - periodically called callback (passing NULL disables this feature)
  • user_p - user pointer passed to the stop_cb function
  • frequency - frequency of calling the stop_cb function

New in version 2.0.

Example

  1. #include "jerryscript.h"
  2. static int countdown = 10;
  3. static jerry_value_t
  4. vm_exec_stop_callback (void *user_p)
  5. {
  6. while (countdown > 0)
  7. {
  8. countdown--;
  9. return jerry_create_undefined ();
  10. }
  11. // The error flag is added automatically.
  12. return jerry_create_string ((const jerry_char_t *) "Abort script");
  13. }
  14. int
  15. main (void)
  16. {
  17. jerry_init (JERRY_INIT_EMPTY);
  18. jerry_set_vm_exec_stop_callback (vm_exec_stop_callback, &countdown, 16);
  19. // Inifinte loop.
  20. const jerry_char_t script[] = "while(true) {}";
  21. jerry_value_t parsed_code = jerry_parse (NULL, 0, script, sizeof (script) - 1, JERRY_PARSE_NO_OPTS);
  22. jerry_release_value (jerry_run (parsed_code));
  23. jerry_release_value (parsed_code);
  24. jerry_cleanup ();
  25. }

See also

jerry_get_backtrace

Summary

Get backtrace. The backtrace is an array of strings where each string contains the position of the corresponding frame. The array length is zero if the backtrace is not available.

This function is typically called from native callbacks.

Notes:

  • Returned value must be freed with jerry_release_value when it is no longer needed.
  • This feature depends on build option (JERRY_LINE_INFO) and can be checked in runtime with the JERRY_FEATURE_LINE_INFO feature enum value, see: jerry_is_feature_enabled.

Prototype

  1. jerry_value_t
  2. jerry_get_backtrace (uint32_t max_depth);
  • max_depth - backtrace collection stops after reaching this value, 0 = unlimited
  • return value
    • a newly constructed JS array

New in version 2.0.

Example

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "jerryscript.h"
  4. static jerry_value_t
  5. backtrace_handler (const jerry_value_t function_obj,
  6. const jerry_value_t this_val,
  7. const jerry_value_t args_p[],
  8. const jerry_length_t args_count)
  9. {
  10. if (!jerry_is_feature_enabled (JERRY_FEATURE_LINE_INFO))
  11. {
  12. printf ("Line info disabled, no backtrace will be printed\n");
  13. }
  14. /* If the line info feature is disabled an empty array will be returned. */
  15. jerry_value_t backtrace_array = jerry_get_backtrace (5);
  16. uint32_t array_length = jerry_get_array_length (backtrace_array);
  17. for (uint32_t idx = 0; idx < array_length; idx++)
  18. {
  19. jerry_value_t property = jerry_get_property_by_index (backtrace_array, idx);
  20. jerry_char_t string_buffer[64];
  21. jerry_size_t copied_bytes = jerry_substring_to_char_buffer (property,
  22. 0,
  23. 63,
  24. string_buffer,
  25. 63);
  26. string_buffer[copied_bytes] = '\0';
  27. printf(" %d: %s\n", idx, string_buffer);
  28. jerry_release_value (property);
  29. }
  30. jerry_release_value (backtrace_array);
  31. return jerry_create_undefined ();
  32. } /* backtrace_handler */
  33. int
  34. main (void)
  35. {
  36. jerry_init (JERRY_INIT_EMPTY);
  37. jerry_value_t global = jerry_get_global_object ();
  38. /* Register the "dump_backtrace" method. */
  39. {
  40. jerry_value_t func = jerry_create_external_function (backtrace_handler);
  41. jerry_value_t name = jerry_create_string ((const jerry_char_t *) "backtrace");
  42. jerry_value_t result = jerry_set_property (global, name, func);
  43. jerry_release_value (result);
  44. jerry_release_value (name);
  45. jerry_release_value (func);
  46. }
  47. jerry_release_value (global);
  48. const char *source = ("function f() { g (); }\n"
  49. "function g() { h (); }\n"
  50. "function h() { backtrace (); }\n"
  51. "f ();\n");
  52. const char *resource = "demo_memoryjs";
  53. jerry_value_t program = jerry_parse ((const jerry_char_t *) resource,
  54. strlen (resource),
  55. (const jerry_char_t *) source,
  56. strlen (source),
  57. JERRY_PARSE_NO_OPTS);
  58. if (!jerry_value_is_error (program))
  59. {
  60. jerry_value_t run_result = jerry_run (program);
  61. jerry_release_value (run_result);
  62. }
  63. jerry_release_value (program);
  64. jerry_cleanup ();
  65. return 0;
  66. }

See also

jerry_get_resource_name

Summary

Get the resource name (usually a file name) of the currently executed script or the given function object.

This function is typically called from native callbacks.

Notes:

  • Returned value must be freed with jerry_release_value when it is no longer needed.
  • This feature depends on build option (JERRY_LINE_INFO) and can be checked in runtime with the JERRY_FEATURE_LINE_INFO feature enum value, see: jerry_is_feature_enabled.

Prototype

  1. jerry_value_t
  2. jerry_get_resource_name (jerry_value_t value);
  • value - api value to obtain the resource name from
  • return string value constructed from
    • the currently executed function object’s resource name, if the given value is undefined
    • resource name of the function object, if the given value is a function object
    • “”, otherwise

New in version 2.2.

Example

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "jerryscript.h"
  4. static jerry_value_t
  5. resource_name_handler (const jerry_value_t function_obj,
  6. const jerry_value_t this_val,
  7. const jerry_value_t args_p[],
  8. const jerry_length_t args_count)
  9. {
  10. jerry_value_t undefined_value = jerry_create_undefined ();
  11. jerry_value_t resource_name = jerry_get_resource_name (args_count > 0 ? args_p[0] : undefined_value);
  12. jerry_release_value (undefined_value);
  13. return resource_name;
  14. } /* resource_name_handler */
  15. int
  16. main (void)
  17. {
  18. jerry_init (JERRY_INIT_EMPTY);
  19. jerry_value_t global = jerry_get_global_object ();
  20. /* Register the "resourceName" method. */
  21. {
  22. jerry_value_t func = jerry_create_external_function (resource_name_handler);
  23. jerry_value_t name = jerry_create_string ((const jerry_char_t *) "resourceName");
  24. jerry_value_t result = jerry_set_property (global, name, func);
  25. jerry_release_value (result);
  26. jerry_release_value (name);
  27. jerry_release_value (func);
  28. }
  29. jerry_release_value (global);
  30. const jerry_char_t source[] = "function myFunction() { return resourceName() }; myFunction()";
  31. const jerry_char_t resource[] = "demo.js";
  32. jerry_value_t program = jerry_parse (resource,
  33. sizeof (resource) - 1,
  34. source,
  35. sizeof (source) - 1,
  36. JERRY_PARSE_NO_OPTS);
  37. if (!jerry_value_is_error (program))
  38. {
  39. /* `run_result` contains "demo.js" */
  40. jerry_value_t run_result = jerry_run (program);
  41. /* usage of `run_result` */
  42. jerry_release_value (run_result);
  43. }
  44. jerry_release_value (program);
  45. jerry_cleanup ();
  46. return 0;
  47. }

See also

jerry_get_new_target

Summary

Returns the current “new.target” JavaScript function at the call site.

If used outside of a native C function it will return “undefined” value.

Notes:

  • Returned value must be freed with jerry_release_value when it is no longer needed.
  • This feature depends on build option (JERRY_ES2015) and can be checked in runtime with the JERRY_FEATURE_SYMBOL feature enum value (as symbols are enabled in case of ES2015), see: jerry_is_feature_enabled.
  • If the ES2015 mode is not enabled this method will always return the “undefined” value.

Prototype

  1. jerry_value_t
  2. jerry_get_new_target (void);
  • return
    • “undefined” - if at the call site it was not a constructor call.
    • function object - if the current call site is in a constructor call.

New in version 2.2.

Example 1

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <jerryscript.h>
  4. static jerry_value_t
  5. demo_handler (const jerry_value_t func_obj_val,
  6. const jerry_value_t this_val,
  7. const jerry_value_t args_p[],
  8. const jerry_length_t args_cnt)
  9. {
  10. jerry_value_t new_target = jerry_get_new_target ();
  11. /* new_target is the "demo" JS function object */
  12. if (jerry_value_get_type (new_target) == JERRY_TYPE_FUNCTION)
  13. {
  14. printf ("This is a construct call\r\n");
  15. }
  16. jerry_release_value (new_target);
  17. return jerry_create_undefined ();
  18. }
  19. int
  20. main (int argc, char** argv)
  21. {
  22. jerry_init (JERRY_INIT_EMPTY);
  23. jerry_value_t function_val = jerry_create_external_function (demo_handler);
  24. jerry_value_t ret_val = jerry_construct_object (function_val, NULL, 0);
  25. jerry_release_value (ret_val);
  26. jerry_release_value (function_val);
  27. jerry_cleanup ();
  28. return 0;
  29. }

Example 2

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <jerryscript.h>
  4. static jerry_value_t
  5. demo_handler (const jerry_value_t func_obj_val,
  6. const jerry_value_t this_val,
  7. const jerry_value_t args_p[],
  8. const jerry_length_t args_cnt)
  9. {
  10. jerry_value_t new_target = jerry_get_new_target ();
  11. /* new_target is a JS function object */
  12. if (jerry_value_get_type (new_target) == JERRY_TYPE_FUNCTION)
  13. {
  14. printf ("This is a construct call\r\n");
  15. }
  16. jerry_release_value (new_target);
  17. return jerry_create_undefined ();
  18. }
  19. int
  20. main (int argc, char** argv)
  21. {
  22. jerry_init (JERRY_INIT_EMPTY);
  23. /* register C method */
  24. jerry_value_t global_obj_val = jerry_get_global_object ();
  25. jerry_value_t function_val = jerry_create_external_function (demo_handler);
  26. jerry_value_t function_name_val = jerry_create_string ((const jerry_char_t *) "demo");
  27. jerry_value_t result_val = jerry_set_property (global_obj_val, function_name_val, function_val);
  28. jerry_release_value (result_val);
  29. jerry_release_value (function_name_val);
  30. jerry_release_value (function_val);
  31. jerry_release_value (global_obj_val);
  32. /* Invoke C method via JS */
  33. const char *src = "new demo ()";
  34. jerry_value_t ret_val = jerry_eval ((const jerry_char_t *) src,
  35. strlen (src),
  36. JERRY_PARSE_NO_OPTS);
  37. jerry_release_value (ret_val);
  38. jerry_cleanup ();
  39. return 0;
  40. }

See also

ArrayBuffer and TypedArray functions

These APIs all depend on the ES2015-subset profile.

jerry_get_arraybuffer_byte_length

Summary

Get the byte length property of the ArrayBuffer. This is the same value which was passed to the ArrayBuffer constructor call.

Prototype

  1. jerry_length_t
  2. jerry_get_arraybuffer_byte_length (const jerry_value_t value);
  • value - ArrayBuffer object
  • return value
    • size of the ArrayBuffer in bytes
    • 0 if the value parameter is not an ArrayBuffer

New in version 2.0.

Example

  1. {
  2. jerry_value_t buffer = jerry_create_arraybuffer (15);
  3. jerry_length_t length = jerry_get_arraybuffer_byte_length (buffer);
  4. // length should be 15
  5. jerry_release_value (buffer);
  6. }

See also

jerry_arraybuffer_read

Summary

Copy the portion of the ArrayBuffer into a user provided buffer. The start offset of the read operation can be specified.

The number bytes to be read can be specified via the buf_size parameter. It is not possible to read more than the length of the ArrayBuffer.

Function returns the number of bytes read from the ArrayBuffer (and written to the buffer parameter). This value is calculated in the following way: min(array buffer length - offset, buf_size).

Prototype

  1. jerry_length_t
  2. jerry_arraybuffer_read (const jerry_value_t value,
  3. jerry_length_t offset,
  4. uint8_t *buf_p,
  5. jerry_length_t buf_size);
  • value - ArrayBuffer to read from
  • offset - start offset of the read operation
  • buf_p - buffer to read the data to
  • buf_size - maximum number of bytes to read into the buffer
  • return value
    • number of bytes written into the buffer (read from the ArrayBuffer)
    • 0 if the value is not an ArrayBuffer object
    • 0 if the buf_size is zero or there is nothing to read

New in version 2.0.

Example

  1. {
  2. uint8_t data[20];
  3. jerry_value_t buffer;
  4. // ... create the ArrayBuffer or acuiqre it from somewhere.
  5. jerry_value_t bytes_read;
  6. // read 10 bytes from the start of the ArrayBuffer.
  7. bytes_read = jerry_arraybuffer_read (buffer, 0, data, 10);
  8. // read the next 10 bytes
  9. bytes_read += jerry_arraybuffer_read (buffer, bytes_read, data + bytes_read, 10);
  10. // process the data variable
  11. jerry_release_value (buffer);
  12. }

See also

jerry_arraybuffer_write

Summary

Copy the contents of a buffer into the ArrayBuffer. The start offset of the write operation can be specified.

The number bytes to be written can be specified via the buf_size parameter. It is not possible to write more than the length of the ArrayBuffer.

Function returns the number of bytes written into the ArrayBuffer (and read from the buffer parameter). This value is calculated in the following way: min(array buffer length - offset, buf_size).

Prototype

  1. jerry_length_t
  2. jerry_arraybuffer_write (const jerry_value_t value,
  3. jerry_length_t offset,
  4. const uint8_t *buf_p,
  5. jerry_length_t buf_size);
  • value - ArrayBuffer to write to
  • offset - start offset of the write operation
  • buf_p - buffer to read the data from
  • buf_size - maximum number of bytes to write into the ArrayBuffer
  • return value
    • number of bytes written into the ArrayBuffer (read from the buffer parameter)
    • 0 if the value is not an ArrayBuffer object
    • 0 if the buf_size is zero or there is nothing to write

New in version 2.0.

Example

  1. {
  2. uint8_t data[20];
  3. // fill the data with values
  4. for (int i = 0; i < 20; i++)
  5. {
  6. data[i] = (uint8_t) (i * 2);
  7. }
  8. jerry_value_t buffer;
  9. // ... create the ArrayBuffer or acquire it from somewhere.
  10. jerry_value_t bytes_written;
  11. // write 10 bytes from to the start of the ArrayBuffer.
  12. bytes_written = jerry_arraybuffer_write (buffer, 0, data, 10);
  13. // read the next 10 bytes
  14. bytes_written += jerry_arraybuffer_write (buffer, bytes_written, data + bytes_written, 10);
  15. // use the ArrayBuffer
  16. jerry_release_value (buffer);
  17. }

See also

jerry_get_arraybuffer_pointer

Summary

The function allows access to the contents of the Array Buffer directly.

WARNING! This operation is for expert use only! The programmer must ensure that the returned memory area is used correctly. That is there is no out of bounds reads or writes. The lifetime of the underlying data buffer is managed by the ArrayBuffer value. Make sure to acquire the value with jerry_acquire_value if the data buffer is needed later.

Prototype

  1. uint8_t *
  2. jerry_get_arraybuffer_pointer (const jerry_value_t value);
  • value - Array Buffer object.
  • return value
    • pointer to the Array Buffer’s data area.
    • NULL if the value is not an Array Buffer object.

New in version 2.0.

Example

  1. {
  2. // create the ArrayBuffer
  3. jerry_value_t buffer = jerry_create_arraybuffer (16);
  4. uint8_t *const data = jerry_get_arraybuffer_pointer (buffer);
  5. for (int i = 0; i < 16; i++)
  6. {
  7. data[i] = (uint8_t) (i + 4);
  8. }
  9. // use the Array Buffer
  10. // release buffer as it is not needed after this point
  11. jerry_release_value (buffer);
  12. }

See also

jerry_is_arraybuffer_detachable

Summary

Get if the ArrayBuffer is detachable.

Prototype

  1. jerry_value_t
  2. jerry_is_arraybuffer_detachable (const jerry_value_t value);
  • value - ArrayBuffer to be detached
  • return
    • boolean value if success
    • Error otherwise

New in version 2.2.

Example

  1. {
  2. // create the ArrayBuffer
  3. jerry_value_t buffer = jerry_create_arraybuffer (16);
  4. jerry_value_t res = jerry_is_arraybuffer_detachable (buffer);
  5. bool is_detachable = jerry_get_boolean_value (res);
  6. // release buffer as it is not needed after this point
  7. jerry_release_value (res);
  8. jerry_release_value (buffer);
  9. }

See also

jerry_detach_arraybuffer

Summary

Detach the underlying data block from ArrayBuffer and set its bytelength to 0.

This operation requires the ArrayBuffer to be external that created by jerry_create_arraybuffer_external.

Prototype

  1. jerry_value_t
  2. jerry_detach_arraybuffer (const jerry_value_t value);
  • value - ArrayBuffer to be detached
  • return
    • null value if success
    • Error otherwise

New in version 2.2.

Example

  1. {
  2. uint8_t buf[1];
  3. jerry_size_t length = 1;
  4. // create the ArrayBuffer
  5. jerry_value_t buffer = jerry_create_arraybuffer (length, buf, NULL);
  6. jerry_value_t res = jerry_detach_arraybuffer (buffer);
  7. // release buffer as it is not needed after this point
  8. jerry_release_value (res);
  9. jerry_release_value (buffer);
  10. }

See also

jerry_get_dataview_buffer

Summary

Get the ArrayBuffer object used by a DataView object. Additionally returns the byteLength and byteOffset properties of the DataView object.

For the returned ArrayBuffer the jerry_release_value must be called when it is no longer needed.

Prototype

  1. jerry_value_t
  2. jerry_get_dataview_buffer (const jerry_value_t value,
  3. jerry_length_t *byteOffset,
  4. jerry_length_t *byteLength);
  • value - DataView to get the ArrayBuffer from
  • byteOffset - (Optional) returns the start offset of the ArrayBuffer for the DataView
  • byteLength - (Optional) returns the number of bytes used from the ArrayBuffer for the DataView
  • return
    • DataView object’s underlying ArrayBuffer object
    • TypeError if the value is not a DataView object

New in version 2.0.

Example

  1. #include "jerryscript.h"
  2. int
  3. main (void)
  4. {
  5. jerry_init (JERRY_INIT_EMPTY);
  6. jerry_value_t arraybuffer = jerry_create_arraybuffer (16);
  7. jerry_value_t dataview = jerry_create_dataview (arraybuffer, 0, 16);
  8. jerry_length_t byteOffset = 0;
  9. jerry_length_t byteLength = 0;
  10. jerry_value_t buffer = jerry_get_dataview_buffer (dataview, &byteOffset, &byteLength);
  11. // buffer is an ArrayBuffer object and ArrayBuffer operations can be performed on it
  12. // byteOffset is 0
  13. // byteLength is 16
  14. // usage of buffer
  15. jerry_release_value (buffer);
  16. jerry_release_value (dataview);
  17. jerry_release_value (arraybuffer);
  18. jerry_cleanup ();
  19. }

See also

jerry_get_typedarray_type

Summary

Get the type of the TypedArray.

The returned type is one of the jerry_typedarray_type_t enum value.

Prototype

  1. jerry_typedarray_type_t
  2. jerry_get_typedarray_type (jerry_value_t value);
  • value - TypedArray object to query for type.
  • return
    • the type of the TypedArray
    • JERRY_TYPEDARRAY_INVALID if the object was not a TypedArray

New in version 2.0.

Example

  1. {
  2. jerry_typedarray_type_t expected_type = JERRY_TYPEDARRAY_UINT32;
  3. jerry_value_t typedarray = jerry_create_typedarray (expected_klass, 25);
  4. jerry_typedarray_type_t type = jerry_get_typedarray_type (typedarray);
  5. // 'type' is now JERRY_TYPEDARRAY_UINT32
  6. jerry_release_value (typedarray);
  7. }

See also

jerry_get_typedarray_length

Summary

Get the element count of the TypedArray as specified during creation.

This is not the same as the byteLength property of a TypedArray object.

Prototype

  1. jerry_length_t
  2. jerry_get_typedarray_length (jerry_value_t value);
  • value - TypedArray object to query
  • return
    • length (element count) of the TypedArray object
    • 0 if the object is not a TypedArray

New in version 2.0.

Example

  1. {
  2. jerry_value_t array = jerry_create_typedarray (JERRY_TYPEDARRAY_INT32, 21);
  3. jerry_length_t element_count = jerry_get_typedarray_length (array);
  4. // element_count is now 21.
  5. jerry_release_value (array);
  6. }

See also

jerry_get_typedarray_buffer

Summary

Get the ArrayBuffer object used by a TypedArray object. Additionally returns the byteLength and byteOffset properties of the TypedArray object.

For the returned ArrayBuffer the jerry_release_value must be called.

Note: Returned value must be freed with jerry_release_value when it is no longer needed.

Prototype

  1. jerry_value_t
  2. jerry_get_typedarray_buffer (jerry_value_t value,
  3. jerry_length_t *byteOffset,
  4. jerry_length_t *byteLength);
  • value - TypedArray to get the ArrayBuffer from
  • byteOffset - (Optional) returns the start offset of the ArrayBuffer for the TypedArray
  • byteLength - (Optional) returns the number of bytes used from the ArrayBuffer for the TypedArray
  • return
    • TypedArray object’s underlying ArrayBuffer object
    • TypeError if the value is not a TypedArray object

New in version 2.0.

Example

  1. {
  2. jerry_value_t array = jerry_create_typedarray (JERRY_TYPEDARRAY_INT16, 11);
  3. jerry_length_t byteLength = 0;
  4. jerry_length_t byteOffset = 0;
  5. jerry_value_t buffer = jerry_get_typedarray_buffer (array, &byteOffset, &byteLength);
  6. // buffer is an ArrayBuffer object and ArrayBuffer operations can be performed on it
  7. // byteLength is 11 * 2 (2 as the TypedArray stores Int16 that is 2 byte elements)
  8. // byteOffset is 0
  9. jerry_release_value (buffer);
  10. jerry_release_value (array);
  11. }

See also

JSON functions

jerry_json_parse

Summary

Returns the same result as JSON.parse ecmascript function.

Prototype

  1. jerry_value_t
  2. jerry_json_parse (const jerry_char_t *string_p,
  3. jerry_size_t string_size);
  • string_p - a JSON string
  • string_size - size of the string
  • return
    • jerry_value_t containing the same as json.parse()
    • jerry_value_t containing error massage

New in version 2.0.

Example

  1. {
  2. const jerry_char_t data[] = "{\"name\": \"John\", \"age\": 5}";
  3. jerry_value_t parsed_json = jerry_json_parse (data, sizeof (data) - 1);
  4. // parsed_json now conatins all data stored in data_in_json
  5. jerry_release_value (parsed_json);
  6. }

jerry_json_stringify

Summary

Returns the same value as JSON.stringify ecmascript function.

Prototype

  1. jerry_value_t
  2. jerry_json_stringify (const jerry_value_t object_to_stringify);
  • object_to_stringify - a jerry_value_t object to stringify
  • return
    • jerry_value_t containing the same as json.stringify()
    • jerry_value_t containing error massage

New in version 2.0.

Example

  1. {
  2. jerry_value_t obj = jerry_create_object ();
  3. jerry_value_t key = jerry_create_string ((const jerry_char_t *) "name");
  4. jerry_value_t value = jerry_create_string ((const jerry_char_t *) "John");
  5. jerry_release_value (jerry_set_property (obj, key, value));
  6. jerry_value_t stringified = jerry_json_stringify (obj);
  7. //stringified now contains a json formated string
  8. jerry_release_value (obj);
  9. jerry_release_value (key);
  10. jerry_release_value (value);
  11. jerry_release_value (stringified);
  12. }