Skip to content

Fix jerry_get_context_data() API function #3127

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
1 change: 1 addition & 0 deletions docs/02.API-REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ for the item by default, and if the `init_cb` field is not NULL, it will be call
an additional custom initializer. The `deinit_cb` (if non-`NULL`) is called during a call to `jerry_cleanup ()` to run
any custom deinitialization *before* the VM has been fully cleaned up. The `finalize_cb` (if non-`NULL`) is also called
during a call to `jerry_cleanup ()` to run any custom deinitialization *after* the VM has been fully cleaned up.
If bytes_needed field is 0, no buffer is allocated for the manager, callback functions are called with NULL pointer.

**Prototype**

Expand Down
16 changes: 11 additions & 5 deletions jerry-core/api/jerry.c
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,8 @@ jerry_cleanup (void)
{
if (this_p->manager_p->deinit_cb)
{
this_p->manager_p->deinit_cb (JERRY_CONTEXT_DATA_HEADER_USER_DATA (this_p));
void *data = (this_p->manager_p->bytes_needed > 0) ? JERRY_CONTEXT_DATA_HEADER_USER_DATA (this_p) : NULL;
this_p->manager_p->deinit_cb (data);
}
}

Expand All @@ -223,7 +224,8 @@ jerry_cleanup (void)
next_p = this_p->next_p;
if (this_p->manager_p->finalize_cb)
{
this_p->manager_p->finalize_cb (JERRY_CONTEXT_DATA_HEADER_USER_DATA (this_p));
void *data = (this_p->manager_p->bytes_needed > 0) ? JERRY_CONTEXT_DATA_HEADER_USER_DATA (this_p) : NULL;
this_p->manager_p->finalize_cb (data);
}
jmem_heap_free_block (this_p, sizeof (jerry_context_data_header_t) + this_p->manager_p->bytes_needed);
}
Expand All @@ -249,17 +251,21 @@ jerry_get_context_data (const jerry_context_data_manager_t *manager_p)
{
if (item_p->manager_p == manager_p)
{
return JERRY_CONTEXT_DATA_HEADER_USER_DATA (item_p);
return (manager_p->bytes_needed > 0) ? JERRY_CONTEXT_DATA_HEADER_USER_DATA (item_p) : NULL;
}
}

item_p = jmem_heap_alloc_block (sizeof (jerry_context_data_header_t) + manager_p->bytes_needed);
item_p->manager_p = manager_p;
item_p->next_p = JERRY_CONTEXT (context_data_p);
JERRY_CONTEXT (context_data_p) = item_p;
ret = JERRY_CONTEXT_DATA_HEADER_USER_DATA (item_p);

memset (ret, 0, manager_p->bytes_needed);
if (manager_p->bytes_needed > 0)
{
ret = JERRY_CONTEXT_DATA_HEADER_USER_DATA (item_p);
memset (ret, 0, manager_p->bytes_needed);
}

if (manager_p->init_cb)
{
manager_p->init_cb (ret);
Expand Down
50 changes: 47 additions & 3 deletions tests/unit-core/test-context-data.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@
static bool test_context_data1_new_called = false;
static bool test_context_data2_new_called = false;
static bool test_context_data3_new_called = false;
static bool test_context_data4_new_called = false;
static bool test_context_data1_free_called = false;
static bool test_context_data2_free_called = false;
static bool test_context_data4_free_called = false;
static bool test_context_data1_finalize_called = false;
static bool test_context_data4_finalize_called = false;

/* Context item 1 */
const char *string1 = "item1";
Expand Down Expand Up @@ -84,13 +87,12 @@ static const jerry_context_data_manager_t manager2 =
};

/* Context item 3 */
const char *string3 = "item3";

static void
test_context_data3_new (void *user_data_p)
{
JERRY_UNUSED (user_data_p);
test_context_data3_new_called = true;
*((const char **) user_data_p) = string3;
} /* test_context_data3_new */

static const jerry_context_data_manager_t manager3 =
Expand All @@ -102,6 +104,41 @@ static const jerry_context_data_manager_t manager3 =
.bytes_needed = 0,
};

/* Context item 4 */

static void
test_context_data4_new (void *user_data_p)
{
test_context_data4_new_called = true;
TEST_ASSERT (user_data_p == NULL);
} /* test_context_data4_new */


static void
test_context_data4_free (void *user_data_p)
{
test_context_data4_free_called = true;
TEST_ASSERT (user_data_p == NULL);
TEST_ASSERT (!test_context_data4_finalize_called);
} /* test_context_data4_free */

static void
test_context_data4_finalize (void *user_data_p)
{
TEST_ASSERT (!test_context_data4_finalize_called);
test_context_data4_finalize_called = true;
TEST_ASSERT (user_data_p == NULL);
} /* test_context_data4_finalize */

static const jerry_context_data_manager_t manager4 =
{
.init_cb = test_context_data4_new,
.deinit_cb = test_context_data4_free,
.finalize_cb = test_context_data4_finalize,
.bytes_needed = 0
};


int
main (void)
{
Expand All @@ -111,19 +148,26 @@ main (void)

TEST_ASSERT (!strcmp (*((const char **) jerry_get_context_data (&manager1)), "item1"));
TEST_ASSERT (!strcmp (*((const char **) jerry_get_context_data (&manager2)), "item2"));
TEST_ASSERT (!strcmp (*((const char **) jerry_get_context_data (&manager3)), "item3"));
TEST_ASSERT (jerry_get_context_data (&manager3) == NULL);
TEST_ASSERT (jerry_get_context_data (&manager4) == NULL);

TEST_ASSERT (test_context_data1_new_called);
TEST_ASSERT (test_context_data2_new_called);
TEST_ASSERT (test_context_data3_new_called);
TEST_ASSERT (test_context_data4_new_called);

TEST_ASSERT (!test_context_data1_free_called);
TEST_ASSERT (!test_context_data2_free_called);
TEST_ASSERT (!test_context_data4_free_called);

jerry_cleanup ();

TEST_ASSERT (test_context_data1_free_called);
TEST_ASSERT (test_context_data2_free_called);
TEST_ASSERT (test_context_data4_free_called);

TEST_ASSERT (test_context_data1_finalize_called);
TEST_ASSERT (test_context_data4_finalize_called);

return 0;
} /* main */