Skip to content

Fix object free callback. #32

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 1 commit into from
May 12, 2015
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
6 changes: 2 additions & 4 deletions jerry-core/jerry-api.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,8 @@ extern EXTERN_C
bool jerry_api_get_object_native_handle (jerry_api_object_t *object_p, uintptr_t* out_handle_p);

extern EXTERN_C
void jerry_api_set_object_native_handle (jerry_api_object_t *object_p, uintptr_t handle);

extern EXTERN_C
bool jerry_api_set_object_free_callback (jerry_api_object_t *object_p,
void jerry_api_set_object_native_handle (jerry_api_object_t *object_p,
uintptr_t handle,
jerry_object_free_callback_t freecb_p);

extern EXTERN_C
Expand Down
55 changes: 26 additions & 29 deletions jerry-core/jerry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -749,46 +749,43 @@ jerry_api_get_object_native_handle (jerry_api_object_t *object_p, /**< object to
} /* jerry_api_get_object_native_handle */

/**
* Set native handle for the specified object
* Set native handle and, optionally, free callback for the specified object
*
* Note:
* If native handle was already set for the object, its value is updated.
*
* Note:
* If free callback is specified, it is set to be called upon specified JS-object is freed (by GC).
*
* Otherwise, if NULL is specified for free callback pointer, free callback is not created
* and, if a free callback was added earlier for the object, it is removed.
*/
void
jerry_api_set_object_native_handle (jerry_api_object_t *object_p, /**< object to set handle in */
uintptr_t handle) /**< handle value */
uintptr_t handle, /**< handle value */
jerry_object_free_callback_t freecb_p) /**< object free callback or NULL */
{
jerry_assert_api_available ();

ecma_create_external_pointer_property (object_p,
ECMA_INTERNAL_PROPERTY_NATIVE_HANDLE,
handle);
} /* jerry_api_set_object_native_handle */

/**
* Set object free callback for the specified object
*
* @return true - if callback was set successfully,
* false - otherwise (there is no native handle, associated with the object).
*/
bool
jerry_api_set_object_free_callback (jerry_api_object_t *object_p, /**< object to set callback for */
jerry_object_free_callback_t freecb_p) /**< object free callback */
{
jerry_assert_api_available ();

uintptr_t handle_value;
bool is_native_handle_associated = jerry_api_get_object_native_handle (object_p,
&handle_value);

if (!is_native_handle_associated)
if (freecb_p != NULL)
{
return false;
ecma_create_external_pointer_property (object_p,
ECMA_INTERNAL_PROPERTY_FREE_CALLBACK,
(uintptr_t) freecb_p);
}

ecma_create_external_pointer_property (object_p,
ECMA_INTERNAL_PROPERTY_FREE_CALLBACK,
(uintptr_t) freecb_p);

return true;
} /* jerry_api_set_object_free_callback */
else
{
ecma_property_t *prop_p = ecma_find_internal_property (object_p,
ECMA_INTERNAL_PROPERTY_FREE_CALLBACK);
if (prop_p != NULL)
{
ecma_delete_property (object_p, prop_p);
}
}
} /* jerry_api_set_object_native_handle */

/**
* Invoke function specified by a function object
Expand Down
9 changes: 5 additions & 4 deletions tests/unit/test_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,9 @@ handler_construct (const jerry_api_object_t *function_obj_p,

jerry_api_set_object_field_value (this_p->v_object, "value_field", &args_p [0]);

jerry_api_set_object_native_handle (this_p->v_object, (uintptr_t) 0x0012345678abcdefull);

bool is_set = jerry_api_set_object_free_callback (this_p->v_object, handler_construct_freecb);
assert (is_set);
jerry_api_set_object_native_handle (this_p->v_object,
(uintptr_t) 0x0012345678abcdefull,
handler_construct_freecb);

return true;
} /* handler_construct */
Expand Down Expand Up @@ -349,5 +348,7 @@ main (void)

jerry_cleanup ();

assert(test_api_is_free_callback_was_called);

return 0;
}