Autorelease values

JERRYX_AR_VALUE_T

Summary

Macro for const jerry_value_t for which jerry_release_value() is automatically called when the variable goes out of scope.

Note: The macro depends on compiler support. For GCC and LLVM/clang, the macro is implemented using the __cleanup__ variable attribute. For other compilers, no support has been added yet.

Example

  1. #include "jerryscript.h"
  2. #include "jerryscript-ext/autorelease.h"
  3. static void
  4. foo (bool enable)
  5. {
  6. JERRYX_AR_VALUE_T bar = jerry_create_string ((const jerry_char_t *) "...");
  7. if (enable)
  8. {
  9. JERRYX_AR_VALUE_T baz = jerry_get_global_object ();
  10. /* bar and baz can now be used. */
  11. /*
  12. * jerry_release_value (baz) and jerry_release_value (bar) is called automatically before
  13. * returning, because `baz` and `bar` go out of scope.
  14. */
  15. return;
  16. }
  17. /*
  18. * jerry_release_value (bar) is called automatically when the function returns,
  19. * because `bar` goes out of scope.
  20. */
  21. }

See also