Introduce new jerry-ext methods to ease property registration (#2715)
New methods
* jerryx_set_properties: Allows multiple property registration for via a single call
* jerryx_set_property_str: Allows property registration without the need to
create the property name JS value. The property name can be directly passed
as a `const char*` value and must be zero terminated.
* jerryx_get_property_str: Allows getting a property value on a given object.
The property name can be directly passed as a `const char*` value.
* jerryx_has_property_str: Allows checking if a property exists on a given object.
JerryScript-DCO-1.0-Signed-off-by: Peter Gal pgal.u-szeged@partner.samsung.com
This commit is contained in:
committed by
Robert Fancsik
parent
3e12738037
commit
c64ee882da
@@ -40,3 +40,147 @@ jerryx_handler_register_global (const jerry_char_t *name_p, /**< name of the fun
|
||||
|
||||
return result_val;
|
||||
} /* jerryx_handler_register_global */
|
||||
|
||||
/**
|
||||
* Set multiple properties on a target object.
|
||||
*
|
||||
* The properties are an array of (name, property value) pairs and
|
||||
* this list must end with a (NULL, 0) entry.
|
||||
*
|
||||
* Notes:
|
||||
* - Each property value in the input array is released after a successful property registration.
|
||||
* - The property name must be a zero terminated UTF-8 string.
|
||||
* - There should be no '\0' (NULL) character in the name excluding the string terminator.
|
||||
* - The method `jerryx_release_property_entry` must be called if there is any failed registration
|
||||
* to release the values in the entries array.
|
||||
*
|
||||
* @return `jerryx_register_result` struct - if everything is ok with the (undefined, property entry count) values.
|
||||
* In case of error the (error object, registered property count) pair.
|
||||
*/
|
||||
jerryx_register_result
|
||||
jerryx_set_properties (const jerry_value_t target_object, /**< target object */
|
||||
const jerryx_property_entry entries[]) /**< array of method entries */
|
||||
{
|
||||
#define JERRYX_SET_PROPERTIES_RESULT(VALUE, IDX) ((jerryx_register_result) { VALUE, IDX })
|
||||
uint32_t idx = 0;
|
||||
for (; ((entries + idx) != NULL) && (entries[idx].name != NULL); idx++)
|
||||
{
|
||||
const jerryx_property_entry *entry = &entries[idx];
|
||||
|
||||
jerry_value_t prop_name = jerry_create_string_from_utf8 ((const jerry_char_t *) entry->name);
|
||||
jerry_value_t result = jerry_set_property (target_object, prop_name, entry->value);
|
||||
|
||||
jerry_release_value (prop_name);
|
||||
|
||||
// By API definition:
|
||||
// The jerry_set_property returns TRUE if there is no problem
|
||||
// and error object if there is any problem.
|
||||
// Thus there is no need to check if the boolean value is false or not.
|
||||
if (!jerry_value_is_boolean (result))
|
||||
{
|
||||
return JERRYX_SET_PROPERTIES_RESULT (result, idx);
|
||||
}
|
||||
|
||||
jerry_release_value (entry->value);
|
||||
jerry_release_value (result);
|
||||
}
|
||||
|
||||
return JERRYX_SET_PROPERTIES_RESULT (jerry_create_undefined (), idx);
|
||||
#undef JERRYX_SET_PROPERTIES_RESULT
|
||||
} /* jerryx_set_properties */
|
||||
|
||||
/**
|
||||
* Release all jerry_value_t in a jerryx_property_entry array based on
|
||||
* a previous jerryx_set_properties call.
|
||||
*
|
||||
* In case of a successful registration it is safe to call this method.
|
||||
*/
|
||||
void
|
||||
jerryx_release_property_entry (const jerryx_property_entry entries[], /**< list of property entries */
|
||||
const jerryx_register_result register_result) /**< previous result of registration */
|
||||
{
|
||||
for (uint32_t idx = register_result.registered;
|
||||
((entries + idx) != NULL) && (entries[idx].name != NULL);
|
||||
idx++)
|
||||
{
|
||||
jerry_release_value (entries[idx].value);
|
||||
}
|
||||
} /* jerryx_release_property_entry */
|
||||
|
||||
/**
|
||||
* Set a property to a specified value with a given name.
|
||||
*
|
||||
* Notes:
|
||||
* - The operation performed is the same as what the `jerry_set_property` method.
|
||||
* - The property name must be a zero terminated UTF-8 string.
|
||||
* - There should be no '\0' (NULL) character in the name excluding the string terminator.
|
||||
* - Returned value must be freed with jerry_release_value, when it is no longer needed.
|
||||
*
|
||||
* @return true value - if the operation was successful
|
||||
* thrown error - otherwise
|
||||
*/
|
||||
jerry_value_t
|
||||
jerryx_set_property_str (const jerry_value_t target_object, /**< target object */
|
||||
const char *name, /**< property name */
|
||||
const jerry_value_t value) /**< value to set */
|
||||
{
|
||||
jerry_value_t property_name_val = jerry_create_string_from_utf8 ((const jerry_char_t *) name);
|
||||
jerry_value_t result_val = jerry_set_property (target_object, property_name_val, value);
|
||||
|
||||
jerry_release_value (property_name_val);
|
||||
|
||||
return result_val;
|
||||
} /* jerryx_set_property_str */
|
||||
|
||||
/**
|
||||
* Get a property value of a specified object.
|
||||
*
|
||||
* Notes:
|
||||
* - The operation performed is the same as what the `jerry_get_property` method.
|
||||
* - The property name must be a zero terminated UTF-8 string.
|
||||
* - There should be no '\0' (NULL) character in the name excluding the string terminator.
|
||||
* - Returned value must be freed with jerry_release_value, when it is no longer needed.
|
||||
*
|
||||
* @return jerry_value_t - the property value
|
||||
*/
|
||||
jerry_value_t
|
||||
jerryx_get_property_str (const jerry_value_t target_object, /**< target object */
|
||||
const char *name) /**< property name */
|
||||
{
|
||||
jerry_value_t prop_name = jerry_create_string_from_utf8 ((const jerry_char_t *) name);
|
||||
jerry_value_t result_val = jerry_get_property (target_object, prop_name);
|
||||
jerry_release_value (prop_name);
|
||||
|
||||
return result_val;
|
||||
} /* jerryx_get_property_str */
|
||||
|
||||
/**
|
||||
* Check if a property exists on an object.
|
||||
*
|
||||
* Notes:
|
||||
* - The operation performed is the same as what the `jerry_has_property` method.
|
||||
* - The property name must be a zero terminated UTF-8 string.
|
||||
* - There should be no '\0' (NULL) character in the name excluding the string terminator.
|
||||
*
|
||||
* @return true - if the property exists on the given object.
|
||||
* false - if there is no such property or there was an error accessing the property.
|
||||
*/
|
||||
bool
|
||||
jerryx_has_property_str (const jerry_value_t target_object, /**< target object */
|
||||
const char *name) /**< property name */
|
||||
{
|
||||
bool has_property = false;
|
||||
|
||||
jerry_value_t prop_name = jerry_create_string_from_utf8 ((const jerry_char_t *) name);
|
||||
jerry_value_t has_prop_val = jerry_has_property (target_object, prop_name);
|
||||
|
||||
if (!jerry_value_is_error (has_prop_val))
|
||||
{
|
||||
has_property = jerry_get_boolean_value (has_prop_val);
|
||||
}
|
||||
|
||||
jerry_release_value (has_prop_val);
|
||||
jerry_release_value (prop_name);
|
||||
|
||||
return has_property;
|
||||
} /* jerryx_has_property_str */
|
||||
|
||||
@@ -45,6 +45,56 @@ jerry_value_t jerryx_handler_gc (const jerry_value_t func_obj_val, const jerry_v
|
||||
jerry_value_t jerryx_handler_print (const jerry_value_t func_obj_val, const jerry_value_t this_p,
|
||||
const jerry_value_t args_p[], const jerry_length_t args_cnt);
|
||||
|
||||
/**
|
||||
* Struct used by the `jerryx_set_functions` method to
|
||||
* register multiple methods for a given object.
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
const char *name; /**< name of the property to add */
|
||||
jerry_value_t value; /**< value of the property */
|
||||
} jerryx_property_entry;
|
||||
|
||||
#define JERRYX_PROPERTY_NUMBER(NAME, NUMBER) (jerryx_property_entry) { NAME, jerry_create_number (NUMBER) }
|
||||
#define JERRYX_PROPERTY_STRING(NAME, STR) \
|
||||
(jerryx_property_entry) { NAME, jerry_create_string_from_utf8 ((const jerry_char_t *) STR) }
|
||||
#define JERRYX_PROPERTY_STRING_SZ(NAME, STR, SIZE) \
|
||||
(jerryx_property_entry) { NAME, jerry_create_string_sz_from_utf8 ((const jerry_char_t *) STR, SIZE) }
|
||||
#define JERRYX_PROPERTY_BOOLEAN(NAME, VALUE) (jerryx_property_entry) { NAME, jerry_create_boolean (VALUE) }
|
||||
#define JERRYX_PROPERTY_FUNCTION(NAME, FUNC) (jerryx_property_entry) { NAME, jerry_create_external_function (FUNC) }
|
||||
#define JERRYX_PROPERTY_UNDEFINED(NAME) (jerryx_property_entry) { NAME, jerry_create_undefined() }
|
||||
#define JERRYX_PROPERTY_LIST_END() (jerryx_property_entry) { NULL, 0 }
|
||||
|
||||
/**
|
||||
* Stores the result of property register operation.
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
jerry_value_t result; /**< result of property registraion (undefined or error object) */
|
||||
uint32_t registered; /**< number of successfully registered methods */
|
||||
} jerryx_register_result;
|
||||
|
||||
jerryx_register_result
|
||||
jerryx_set_properties (const jerry_value_t target_object,
|
||||
const jerryx_property_entry entries[]);
|
||||
|
||||
void
|
||||
jerryx_release_property_entry (const jerryx_property_entry entries[],
|
||||
const jerryx_register_result register_result);
|
||||
|
||||
jerry_value_t
|
||||
jerryx_set_property_str (const jerry_value_t target_object,
|
||||
const char *name,
|
||||
const jerry_value_t value);
|
||||
|
||||
jerry_value_t
|
||||
jerryx_get_property_str (const jerry_value_t target_object,
|
||||
const char *name);
|
||||
|
||||
bool
|
||||
jerryx_has_property_str (const jerry_value_t target_object,
|
||||
const char *name);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif /* __cplusplus */
|
||||
|
||||
Reference in New Issue
Block a user