Add jerryscript-ext/autorelease.h with JERRYX_AR_VALUE_T (#1874)
JerryScript-DCO-1.0-Signed-off-by: Martijn The martijn.the@intel.com
This commit is contained in:
committed by
Zidong Jiang
parent
940f1f89c9
commit
b947f56fd0
@@ -0,0 +1,54 @@
|
||||
/* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef JERRYX_AUTORELEASE_H
|
||||
#define JERRYX_AUTORELEASE_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif /* __cplusplus */
|
||||
|
||||
#include "autorelease.impl.h"
|
||||
|
||||
/*
|
||||
* Macro for `const jerry_value_t` for which jerry_release_value () is
|
||||
* automatically called when the variable goes out of scope.
|
||||
*
|
||||
* Example usage:
|
||||
* static void foo (bool enable)
|
||||
* {
|
||||
* JERRYX_AR_VALUE_T bar = jerry_create_string (...);
|
||||
*
|
||||
* if (enable) {
|
||||
* JERRYX_AR_VALUE_T baz = jerry_get_global_object ();
|
||||
*
|
||||
* // ...
|
||||
*
|
||||
* // jerry_release_value (baz) and jerry_release_value (bar) is called automatically before
|
||||
* // returning, because `baz` and `bar` go out of scope.
|
||||
* return;
|
||||
* }
|
||||
*
|
||||
* // jerry_release_value (bar) is called automatically when the function returns,
|
||||
* // because `bar` goes out of scope.
|
||||
* }
|
||||
*/
|
||||
#define JERRYX_AR_VALUE_T __JERRYX_AR_VALUE_T_IMPL
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif /* __cplusplus */
|
||||
#endif /* !JERRYX_AUTORELEASE_H */
|
||||
@@ -0,0 +1,40 @@
|
||||
/* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef JERRYX_AUTORELEASE_IMPL_H
|
||||
#define JERRYX_AUTORELEASE_IMPL_H
|
||||
|
||||
#include "jerryscript.h"
|
||||
|
||||
#ifdef __GNUC__
|
||||
/*
|
||||
* Calls jerry_release_value (*value).
|
||||
* The GCC __cleanup__ function must take a pointer to the variable to clean up.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
static inline void
|
||||
jerryx_autorelease_cleanup (const jerry_value_t *value) /**< jerry value */
|
||||
{
|
||||
jerry_release_value (*value);
|
||||
} /* jerryx_autorelease_cleanup */
|
||||
|
||||
#define __JERRYX_AR_VALUE_T_IMPL const jerry_value_t __attribute__ ((__cleanup__(jerryx_autorelease_cleanup)))
|
||||
#else /* !__GNUC__ */
|
||||
/* TODO: for other compilers */
|
||||
#error "No autorelease implementation for your compiler!"
|
||||
#endif /* __GNUC__ */
|
||||
|
||||
#endif /* !JERRYX_AUTORELEASE_IMPL_H */
|
||||
Reference in New Issue
Block a user