Skip to content

Add new API function #1161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
48 changes: 48 additions & 0 deletions jerry-core/jerry.c
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,40 @@ 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.
*
* @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 +791,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_get_object_type (object_p) == ECMA_OBJECT_TYPE_ARRAY);
} /* 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