Handle Scope

jerryx_handle_scope

Summary It is often necessary to make the lifespan of handles shorter than the lifespan of a native method. Even though the native code could only use the most recent handle, all of the associated objects would also be kept alive since they all share the same scope.

To handle this case, JerryScript HandleScope extension provides the ability to establish a new ‘scope’ to which newly created handles will be associated. Once those handles are no longer required, the scope can be ‘closed’ and any handles associated with the scope are invalidated. The methods available to open/close scopes are jerryx_open_handle_scope and jerryx_close_handle_scope.

JerryScript only supports a single nested hierarchy of scopes. There is only one active scope at any time, and all new handles will be associated with that scope while it is active. Scopes must be closed in the reverse order from which they are opened. In addition, all scopes created within a native method must be closed before returning from that method.

Example

  1. #include "jerryscript.h"
  2. #include "jerryscript-ext/handle-scope.h"
  3. static jerry_value_t
  4. create_object (void)
  5. {
  6. jerry_value_t obj = jerry_create_object ();
  7. return obj;
  8. } /* create_object */
  9. static void
  10. test_handle_scope_val (void)
  11. {
  12. jerryx_handle_scope scope;
  13. jerryx_open_handle_scope (&scope);
  14. jerry_value_t obj = jerryx_create_handle (create_object ());
  15. jerryx_close_handle_scope (scope);
  16. // now obj has been released
  17. } /* test_handle_scope_val */
  18. int
  19. main (void)
  20. {
  21. jerry_init (JERRY_INIT_EMPTY);
  22. test_handle_scope_val ();
  23. jerry_gc (JERRY_GC_PRESSURE_LOW);
  24. jerry_cleanup ();
  25. } /* main */

jerryx_escapable_handle_scope

Summary

It is necessary in common cases that a handle has to be promote to outer scope and prevent from been garbage collected. To handle this case, a escapable handle scope has been proposed from which one object can be promoted to the outer scope. The method available to escape an object from been release at current scope is jerryx_escape_handle.

Example

  1. #include "jerryscript.h"
  2. #include "jerryscript-ext/handle-scope.h"
  3. static jerry_value_t
  4. create_object (void)
  5. {
  6. jerryx_escapable_handle_scope scope;
  7. jerryx_open_escapable_handle_scope (&scope);
  8. jerry_value_t obj = jerryx_create_handle (jerry_create_object ());
  9. jerry_value_t escaped_obj;
  10. jerryx_escape_handle(scope, obj, &escaped_obj);
  11. jerryx_close_handle_scope (scope);
  12. // escaped_obj has now been escaped to outer scope, thus not released at this point
  13. return escaped_obj;
  14. } /* create_object */
  15. static void
  16. test_handle_scope_val (void)
  17. {
  18. jerryx_handle_scope scope;
  19. jerryx_open_handle_scope (&scope);
  20. jerry_value_t obj = create_object ();
  21. jerryx_close_handle_scope (scope);
  22. // now obj has been released
  23. } /* test_handle_scope_val */
  24. int
  25. main (void)
  26. {
  27. jerry_init (JERRY_INIT_EMPTY);
  28. test_handle_scope_val ();
  29. jerry_gc (JERRY_GC_PRESSURE_LOW);
  30. jerry_cleanup ();
  31. } /* main */

See also

Pre-allocated list of handle scopes and handles

To prevent trapping into system calls frequently, a pre-allocated dedicated list mechanism has been introduced to the implementation of JerryX handle scope.

To change the size of pre-allocation list, use build definition JERRYX_HANDLE_PRELIST_SIZE and JERRYX_SCOPE_PRELIST_SIZE to alter the default value of 20.