Skip to content

Commit

Permalink
Add new API function
Browse files Browse the repository at this point in the history
Implemented 'jerry_is_array' and 'jerry_get_array_length' API functions.

JerryScript-DCO-1.0-Signed-off-by: László Langó [email protected]
  • Loading branch information
LaszloLango committed Jun 22, 2016
1 parent 7f153c7 commit 880fad7
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
2 changes: 2 additions & 0 deletions jerry-core/jerry-api.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ jerry_string_t *jerry_create_string_sz (const jerry_char_t *, jerry_size_t);
*/
bool jerry_set_array_index_value (jerry_object_t *, jerry_length_t, jerry_value_t);
bool jerry_get_array_index_value (jerry_object_t *, jerry_length_t, jerry_value_t *);
uint32_t jerry_get_array_length (const jerry_object_t *);

/**
* Functions of 'jerry_string_t'
Expand All @@ -196,6 +197,7 @@ jerry_size_t jerry_string_to_char_buffer (const jerry_string_t *, jerry_char_t *
/**
* General API functions of JS objects
*/
bool jerry_is_array (const jerry_object_t *);
bool jerry_is_constructor (const jerry_object_t *);
bool jerry_is_function (const jerry_object_t *);
bool jerry_add_object_field (jerry_object_t *, const jerry_char_t *, jerry_size_t, const jerry_value_t, bool);
Expand Down
49 changes: 49 additions & 0 deletions jerry-core/jerry.c
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,41 @@ jerry_get_array_index_value (jerry_object_t *array_obj_p, /**< array object */
return false;
} /* jerry_get_array_index_value */

/**
* Get length of an array object
*
* Note:
* Returns 0, if the given parameter is not an array object or
* cannot retrieve the length.
*
* @return length of the given array
*/
uint32_t
jerry_get_array_length (const jerry_object_t *object_p)
{
jerry_assert_api_available ();

if (!jerry_is_array (object_p))
{
return 0;
}

jerry_length_t length = 0;
ecma_string_t *magic_string_length_p = ecma_get_magic_string (LIT_MAGIC_STRING_LENGTH);

ecma_value_t len_value = ecma_op_object_get ((jerry_object_t *) object_p, magic_string_length_p);
ecma_deref_ecma_string (magic_string_length_p);

if (ecma_is_value_number (len_value))
{
length = ecma_number_to_uint32 (ecma_get_number_from_value (len_value));
}

ecma_free_value (len_value);

return length;
} /* jerry_get_array_length */

/**
* Create an error object
*
Expand Down Expand Up @@ -757,6 +792,20 @@ jerry_dispatch_object_free_callback (ecma_external_pointer_t freecb_p, /**< poin
jerry_make_api_available ();
} /* jerry_dispatch_object_free_callback */

/**
* Check if the specified object is an array object.
*
* @return true - if the specified object is an array object,
* false - otherwise.
*/
bool
jerry_is_array (const jerry_object_t *object_p) /**< an object */
{
jerry_assert_api_available ();

return (ecma_object_get_class_name ((jerry_object_t *) object_p) == LIT_MAGIC_STRING_ARRAY_UL);
} /* jerry_is_array */

/**
* Check if the specified object is a function object.
*
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/test-api.c
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,8 @@ main (void)

// Test: Array Object API
jerry_object_t *array_obj_p = jerry_create_array_object (10);
JERRY_ASSERT (jerry_is_array (array_obj_p));
JERRY_ASSERT (jerry_get_array_length (array_obj_p) == 10);

jerry_value_t v_in = jerry_create_number_value (10.5);
jerry_set_array_index_value (array_obj_p, 5, v_in);
Expand Down

0 comments on commit 880fad7

Please sign in to comment.