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
+54
View File
@@ -766,6 +766,60 @@ jerry_value_is_undefined (const jerry_value_t value) /**< api value */
return ecma_is_value_undefined (jerry_get_arg_value (value));
} /* jerry_value_is_undefined */
/**
* Perform the base type of the JavaScript value.
*
* @return jerry_type_t value
*/
jerry_type_t
jerry_value_get_type (const jerry_value_t value) /**< input value to check */
{
jerry_assert_api_available ();
jerry_value_t argument = jerry_get_arg_value (value);
lit_magic_string_id_t lit_id = ecma_get_typeof_lit_id (argument);
JERRY_ASSERT (lit_id != LIT_MAGIC_STRING__EMPTY);
switch (lit_id)
{
case LIT_MAGIC_STRING_UNDEFINED:
{
return JERRY_TYPE_UNDEFINED;
}
case LIT_MAGIC_STRING_BOOLEAN:
{
return JERRY_TYPE_BOOLEAN;
}
case LIT_MAGIC_STRING_NUMBER:
{
return JERRY_TYPE_NUMBER;
}
case LIT_MAGIC_STRING_STRING:
{
return JERRY_TYPE_STRING;
}
case LIT_MAGIC_STRING_FUNCTION:
{
return JERRY_TYPE_FUNCTION;
}
case LIT_MAGIC_STRING_OBJECT:
{
/* Based on the ECMA 262 5.1 standard the 'null' value is an object.
* Thus we'll do an extra check for 'null' here.
*/
return ecma_is_value_null (argument) ? JERRY_TYPE_NULL : JERRY_TYPE_OBJECT;
}
default:
{
JERRY_UNREACHABLE ();
break;
}
}
return JERRY_TYPE_NONE;
} /* jerry_value_get_type */
/**
* Check if the specified feature is enabled.
*
+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);
+17
View File
@@ -286,6 +286,23 @@ bool jerry_value_is_promise (const jerry_value_t value);
bool jerry_value_is_string (const jerry_value_t value);
bool jerry_value_is_undefined (const jerry_value_t value);
/**
* JerryScript API value type information.
*/
typedef enum
{
JERRY_TYPE_NONE = 0u, /**< no type information */
JERRY_TYPE_UNDEFINED, /**< undefined type */
JERRY_TYPE_NULL, /**< null type */
JERRY_TYPE_BOOLEAN, /**< boolean type */
JERRY_TYPE_NUMBER, /**< number type */
JERRY_TYPE_STRING, /**< string type */
JERRY_TYPE_OBJECT, /**< object type */
JERRY_TYPE_FUNCTION, /**< function type */
} jerry_type_t;
jerry_type_t jerry_value_get_type (const jerry_value_t value);
/**
* Checker function of whether the specified compile feature is enabled.
*/
+2 -41
View File
@@ -92,47 +92,8 @@ opfunc_logical_not (ecma_value_t left_value) /**< left value */
ecma_value_t
opfunc_typeof (ecma_value_t left_value) /**< left value */
{
ecma_value_t ret_value = ECMA_VALUE_EMPTY;
ecma_string_t *type_str_p = NULL;
if (ecma_is_value_undefined (left_value))
{
type_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_UNDEFINED);
}
else if (ecma_is_value_null (left_value))
{
type_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_OBJECT);
}
else if (ecma_is_value_boolean (left_value))
{
type_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_BOOLEAN);
}
else if (ecma_is_value_number (left_value))
{
type_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_NUMBER);
}
else if (ecma_is_value_string (left_value))
{
type_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_STRING);
}
else
{
JERRY_ASSERT (ecma_is_value_object (left_value));
if (ecma_op_is_callable (left_value))
{
type_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_FUNCTION);
}
else
{
type_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_OBJECT);
}
}
ret_value = ecma_make_string_value (type_str_p);
return ret_value;
ecma_string_t *type_str_p = ecma_get_magic_string (ecma_get_typeof_lit_id (left_value));
return ecma_make_string_value (type_str_p);
} /* opfunc_typeof */
/**