Add C API to query the type of a JS value (#2195)

New API function:
 * jerry_value_get_type

JerryScript-DCO-1.0-Signed-off-by: Peter Gal pgal.u-szeged@partner.samsung.com
This commit is contained in:
Péter Gál
2018-02-13 13:48:07 +01:00
committed by László Langó
parent 4652c3caaf
commit d7991ae54c
7 changed files with 266 additions and 41 deletions
+51
View File
@@ -21,6 +21,8 @@
#include "jrt-bit-fields.h"
#include "vm-defines.h"
#include "ecma-function-object.h"
/** \addtogroup ecma ECMA
* @{
*
@@ -917,6 +919,55 @@ ecma_free_value_if_not_object (ecma_value_t value) /**< value description */
}
} /* ecma_free_value_if_not_object */
/**
* Get the literal id associated with the given ecma_value type.
* This operation is equivalent to the JavaScript 'typeof' operator.
*
* @returns one of the following value:
* - LIT_MAGIC_STRING_UNDEFINED
* - LIT_MAGIC_STRING_OBJECT
* - LIT_MAGIC_STRING_BOOLEAN
* - LIT_MAGIC_STRING_NUMBER
* - LIT_MAGIC_STRING_STRING
* - LIT_MAGIC_STRING_FUNCTION
*/
lit_magic_string_id_t
ecma_get_typeof_lit_id (ecma_value_t value) /**< input ecma value */
{
lit_magic_string_id_t ret_value = LIT_MAGIC_STRING__EMPTY;
if (ecma_is_value_undefined (value))
{
ret_value = LIT_MAGIC_STRING_UNDEFINED;
}
else if (ecma_is_value_null (value))
{
ret_value = LIT_MAGIC_STRING_OBJECT;
}
else if (ecma_is_value_boolean (value))
{
ret_value = LIT_MAGIC_STRING_BOOLEAN;
}
else if (ecma_is_value_number (value))
{
ret_value = LIT_MAGIC_STRING_NUMBER;
}
else if (ecma_is_value_string (value))
{
ret_value = LIT_MAGIC_STRING_STRING;
}
else
{
JERRY_ASSERT (ecma_is_value_object (value));
ret_value = ecma_op_is_callable (value) ? LIT_MAGIC_STRING_FUNCTION : LIT_MAGIC_STRING_OBJECT;
}
JERRY_ASSERT (ret_value != LIT_MAGIC_STRING__EMPTY);
return ret_value;
} /* ecma_get_typeof_lit_id */
/**
* @}
* @}
+1
View File
@@ -187,6 +187,7 @@ void ecma_value_assign_number (ecma_value_t *value_p, ecma_number_t ecma_number)
void ecma_free_value (ecma_value_t value);
void ecma_fast_free_value (ecma_value_t value);
void ecma_free_value_if_not_object (ecma_value_t value);
lit_magic_string_id_t ecma_get_typeof_lit_id (ecma_value_t value);
/* ecma-helpers-string.c */
ecma_string_t *ecma_new_ecma_string_from_utf8 (const lit_utf8_byte_t *string_p, lit_utf8_size_t string_size);