Add new symbol related API functions. (#2741)

New functions:
 - ecma_create_symbol
 - ecma_value_is_symbol
 - jerry_get_symbol_descriptive_string

Also improve the jerryx_handler_print to be able to print symbol values via using the jerry_get_symbol_descriptive_string API function for symbol values.

JerryScript-DCO-1.0-Signed-off-by: Robert Fancsik frobert@inf.u-szeged.hu
This commit is contained in:
Robert Fancsik
2019-02-19 07:45:50 +01:00
committed by GitHub
parent cffb299f37
commit 60bbd731e5
6 changed files with 486 additions and 15 deletions
+99 -14
View File
@@ -34,6 +34,7 @@
#include "ecma-objects-general.h"
#include "ecma-regexp-object.h"
#include "ecma-promise-object.h"
#include "ecma-symbol-object.h"
#include "ecma-typedarray-object.h"
#include "opcodes.h"
#include "jcontext.h"
@@ -770,6 +771,25 @@ jerry_value_is_string (const jerry_value_t value) /**< api value */
return ecma_is_value_string (value);
} /* jerry_value_is_string */
/**
* Check if the specified value is symbol.
*
* @return true - if the specified value is symbol,
* false - otherwise
*/
bool
jerry_value_is_symbol (const jerry_value_t value) /**< api value */
{
jerry_assert_api_available ();
#ifndef CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN
return ecma_is_value_symbol (value);
#else /* CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN */
JERRY_UNUSED (value);
return false;
#endif /* !CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN */
} /* jerry_value_is_symbol */
/**
* Check if the specified value is undefined.
*
@@ -821,6 +841,12 @@ jerry_value_get_type (const jerry_value_t value) /**< input value to check */
{
return JERRY_TYPE_STRING;
}
#ifndef CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN
case LIT_MAGIC_STRING_SYMBOL:
{
return JERRY_TYPE_SYMBOL;
}
#endif /* !CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN */
case LIT_MAGIC_STRING_FUNCTION:
{
return JERRY_TYPE_FUNCTION;
@@ -885,6 +911,9 @@ jerry_is_feature_enabled (const jerry_feature_t feature) /**< feature to check *
#ifndef CONFIG_DISABLE_ES2015_PROMISE_BUILTIN
|| feature == JERRY_FEATURE_PROMISE
#endif /* !CONFIG_DISABLE_ES2015_PROMISE_BUILTIN */
#ifndef CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN
|| feature == JERRY_FEATURE_SYMBOL
#endif /* !CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN */
#ifndef CONFIG_DISABLE_ES2015_TYPEDARRAY_BUILTIN
|| feature == JERRY_FEATURE_TYPEDARRAY
#endif /* !CONFIG_DISABLE_ES2015_TYPEDARRAY_BUILTIN */
@@ -1557,6 +1586,33 @@ jerry_create_string_sz (const jerry_char_t *str_p, /**< pointer to string */
return ecma_make_string_value (ecma_str_p);
} /* jerry_create_string_sz */
/**
* Create symbol from an api value
*
* Note:
* The given argument is converted to string. This operation can throw an error.
* returned value must be freed with jerry_release_value when it is no longer needed.
*
* @return value of the created symbol, if success
* thrown error, otherwise
*/
jerry_value_t
jerry_create_symbol (const jerry_value_t value) /**< api value */
{
jerry_assert_api_available ();
if (ecma_is_value_error_reference (value))
{
return jerry_throw (ecma_raise_type_error (ECMA_ERR_MSG (wrong_args_msg_p)));
}
#ifndef CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN
return jerry_return (ecma_op_create_symbol (&value, 1));
#else /* CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN */
return jerry_throw (ecma_raise_type_error (ECMA_ERR_MSG ("Symbol is not supported.")));
#endif /* !CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN */
} /* jerry_create_symbol */
/**
* Calculates the size of the given pattern and creates a RegExp object.
*
@@ -1873,13 +1929,13 @@ jerry_has_property (const jerry_value_t obj_val, /**< object value */
jerry_assert_api_available ();
if (!ecma_is_value_object (obj_val)
|| !ecma_is_value_string (prop_name_val))
|| !ecma_is_value_prop_name (prop_name_val))
{
return ECMA_VALUE_FALSE;
}
bool has_property = ecma_op_object_has_property (ecma_get_object_from_value (obj_val),
ecma_get_string_from_value (prop_name_val));
ecma_get_prop_name_from_value (prop_name_val));
return ecma_make_boolean_value (has_property);
} /* jerry_has_property */
@@ -1897,13 +1953,13 @@ jerry_has_own_property (const jerry_value_t obj_val, /**< object value */
jerry_assert_api_available ();
if (!ecma_is_value_object (obj_val)
|| !ecma_is_value_string (prop_name_val))
|| !ecma_is_value_prop_name (prop_name_val))
{
return ECMA_VALUE_FALSE;
}
bool has_property = ecma_op_object_has_own_property (ecma_get_object_from_value (obj_val),
ecma_get_string_from_value (prop_name_val));
ecma_get_prop_name_from_value (prop_name_val));
return ecma_make_boolean_value (has_property);
} /* jerry_has_own_property */
@@ -1921,13 +1977,13 @@ jerry_delete_property (const jerry_value_t obj_val, /**< object value */
jerry_assert_api_available ();
if (!ecma_is_value_object (obj_val)
|| !ecma_is_value_string (prop_name_val))
|| !ecma_is_value_prop_name (prop_name_val))
{
return false;
}
ecma_value_t ret_value = ecma_op_object_delete (ecma_get_object_from_value (obj_val),
ecma_get_string_from_value (prop_name_val),
ecma_get_prop_name_from_value (prop_name_val),
false);
return ecma_is_value_true (ret_value);
} /* jerry_delete_property */
@@ -1974,13 +2030,13 @@ jerry_get_property (const jerry_value_t obj_val, /**< object value */
jerry_assert_api_available ();
if (!ecma_is_value_object (obj_val)
|| !ecma_is_value_string (prop_name_val))
|| !ecma_is_value_prop_name (prop_name_val))
{
return jerry_throw (ecma_raise_type_error (ECMA_ERR_MSG (wrong_args_msg_p)));
}
jerry_value_t ret_value = ecma_op_object_get (ecma_get_object_from_value (obj_val),
ecma_get_string_from_value (prop_name_val));
ecma_get_prop_name_from_value (prop_name_val));
return jerry_return (ret_value);
} /* jerry_get_property */
@@ -2029,13 +2085,13 @@ jerry_set_property (const jerry_value_t obj_val, /**< object value */
if (ecma_is_value_error_reference (value_to_set)
|| !ecma_is_value_object (obj_val)
|| !ecma_is_value_string (prop_name_val))
|| !ecma_is_value_prop_name (prop_name_val))
{
return jerry_throw (ecma_raise_type_error (ECMA_ERR_MSG (wrong_args_msg_p)));
}
return jerry_return (ecma_op_object_put (ecma_get_object_from_value (obj_val),
ecma_get_string_from_value (prop_name_val),
ecma_get_prop_name_from_value (prop_name_val),
value_to_set,
true));
} /* jerry_set_property */
@@ -2109,7 +2165,7 @@ jerry_define_own_property (const jerry_value_t obj_val, /**< object value */
jerry_assert_api_available ();
if (!ecma_is_value_object (obj_val)
|| !ecma_is_value_string (prop_name_val))
|| !ecma_is_value_prop_name (prop_name_val))
{
return jerry_throw (ecma_raise_type_error (ECMA_ERR_MSG (wrong_args_msg_p)));
}
@@ -2189,7 +2245,7 @@ jerry_define_own_property (const jerry_value_t obj_val, /**< object value */
}
return ecma_op_object_define_own_property (ecma_get_object_from_value (obj_val),
ecma_get_string_from_value (prop_name_val),
ecma_get_prop_name_from_value (prop_name_val),
&prop_desc,
true);
} /* jerry_define_own_property */
@@ -2208,7 +2264,7 @@ jerry_get_own_property_descriptor (const jerry_value_t obj_val, /**< object val
jerry_assert_api_available ();
if (!ecma_is_value_object (obj_val)
|| !ecma_is_value_string (prop_name_val))
|| !ecma_is_value_prop_name (prop_name_val))
{
return false;
}
@@ -2216,7 +2272,7 @@ jerry_get_own_property_descriptor (const jerry_value_t obj_val, /**< object val
ecma_property_descriptor_t prop_desc;
if (!ecma_op_object_get_own_property_descriptor (ecma_get_object_from_value (obj_val),
ecma_get_string_from_value (prop_name_val),
ecma_get_prop_name_from_value (prop_name_val),
&prop_desc))
{
return false;
@@ -2715,6 +2771,35 @@ jerry_resolve_or_reject_promise (jerry_value_t promise, /**< the promise value *
#endif /* !CONFIG_DISABLE_ES2015_PROMISE_BUILTIN */
} /* jerry_resolve_or_reject_promise */
/**
* Call the SymbolDescriptiveString ecma builtin operation on the symbol value.
*
* Note:
* returned value must be freed with jerry_release_value, when it is no longer needed.
*
* @return string value containing the symbol's descriptive string - if success
* thrown error - otherwise
*/
jerry_value_t
jerry_get_symbol_descriptive_string (const jerry_value_t symbol) /**< symbol value */
{
jerry_assert_api_available ();
#ifndef CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN
if (!ecma_is_value_symbol (symbol))
{
return jerry_throw (ecma_raise_type_error (ECMA_ERR_MSG (wrong_args_msg_p)));
}
/* Note: This operation cannot throw an error */
return ecma_get_symbol_descriptive_string (symbol);
#else /* CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN */
JERRY_UNUSED (symbol);
return jerry_throw (ecma_raise_type_error (ECMA_ERR_MSG ("Symbol is not supported.")));
#endif /* !CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN */
} /** jerry_get_symbol_descriptive_string */
/**
* Validate UTF-8 string
*