Autorelease values



Macro for const jerry_value_t for which jerry_value_free() 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.


#include "jerryscript.h"
#include "jerryscript-ext/autorelease.h"

static void
foo (bool enable)
  JERRYX_AR_VALUE_T bar = jerry_string_sz ("...");

  if (enable)
    JERRYX_AR_VALUE_T baz = jerry_current_realm ();

    /* bar and baz can now be used. */

     * jerry_value_free (baz) and jerry_value_free (bar) is called automatically before
     * returning, because `baz` and `bar` go out of scope.

   * jerry_value_free (bar) is called automatically when the function returns,
   * because `bar` goes out of scope.

See also