Jerry API: free callback
Current version of jerry_api_set_object_free_callback() takes two parameter
- pointer to object
- function pointer to callback.
The prototype of callback is like this:
typedef void (*jerry_object_free_callback_t) (const uintptr_t native_p);
I think it's confusing that the callback takes native pointer as its parameter, since, at least i thought, there are no link connecting the native_p to something in this set of API.
Actually we need to set the native pointer using separate API named jerry_api_set_object_native_handle() to get it work. I think it's inconsistent. To make it consistent, I think, jerry_set_object_free_callback() should take one more parameter type of native pointer. or the callback function should take jerry_api_object_t* that is being destroyed and then we can access native pointer using jerry_api_get_object_native_handle().
@ILyoan , I think you have a point. Basic idea for current implementation was reusing native handle as the target of "free", that can make users confusion. In some cases jerry_api_object_t* can have both, one native_p holding some native data and another native_p for purpose of "free".
@ruben-ayrapetyan , what do you think?
@seanshpark , @ILyoan ,
I think, we could add jerry_object_free_callback_t argument to jerry_api_set_object_native_handle.
If the argument would be NULL, no free callback would be set, otherwise - both native handle would be created and free callback would be set with one call to API.
In the case, maybe we should remove jerry_api_set_object_free_callback procedure, moving its functionality into jerry_api_set_object_native_handle.
Passing jerry_api_object_t* to callback function may be confusing - anyway API is disabled during the callback operation and, so, we couldn't query anything from the passed argument.
@ruben-ayrapetyan , what I was thinking is to separate native_handle from object_free_callback by adding data to object_free_callback so that jerry_api_set_object_free_callback becomes
bool jerry_api_set_object_free_callback (jerry_api_object_t *object_p,
jerry_object_free_callback_t freecb_p,
uintptr_t freecb_data);
changes would be like previous one with adding another property_id, ECMA_INTERNAL_PROPERTY_FREE_CB_DATA. If it's ok with you, I'll begin the work.
@seanshpark, is it correct, that in the case, jerry_api_[get/set]_object_native_handle API and ECMA_INTERNAL_PROPERTY_NATIVE_HANDLE internal property id become unnecessary and could be removed?
@ruben-ayrapetyan , jerry_api_[get/set]_object_native_handle can be used in other cases. it was added before object_free_callback and it was not intended only for object_free_callback. so it will be useful to exist.
@seanshpark , Why should we need free callback for an object, if no native handle is associated with the object? Or, why should we need several native handles for the object (one in [NATIVE_HANDLE], and another is in [FREE_CB_DATA])? If [FREE_CB_DATA] is not supposed to contain native handle, what should be stored there?
@ruben-ayrapetyan , I think you are stick to the words "native handle". please separate them. object free callback is for calling the external callback function when object is freed, when GCed. the data is what ever the callback is going to use by the callback. for IoT.js it is used to sync object lifecycle for objects that are created within external function as you know. native handle is a handle (or a pointer or a value) that object can have that can be used with any relation by JerryScript users set or get. it can be extra memory pointer, or it can be a enumeration value, or any think the programmer wants. if works "data", "handle" makes confusion, please suggest anything that popup.
@seanshpark, thank you for explanation.
Now I understand that native handle, introduced into API, is not considered just as storage for pointer to native part of object.
Our point is that we should avoid duplication of functionality as much as possible.
It seems that 'native handle' value and 'freecb_data' value can contain the same things
(both can contain some values of sizeof (uintptr_t) bytes - pointers or not).
So, both 'freecb_data' or 'native handle' can contain pointer to native object to be freed (or pointer to any other structure describing native part of the object, or even non-pointer value, if structure is not necessary).
From other view point, 'native handle' allows to connect native data with JS object, without setting free callback (may be, we don't use dynamic memory for native object parts - in the case, free callback is unnecessary), while 'freecb_data' implies necessity of setting free callback.
It seems that 'native handle' is sufficient for connection of JS and native object parts (with pointer or some other values).
So, we could merge 'free_cb_data' and 'native handle' entities, just adding jerry_object_free_callback_t argument (can be NULL) to jerry_api_set_object_native_handle, and removing separate jerry_api_set_object_free_callback interface.
@ruben-ayrapetyan, After reading the discussion, I think I get your point of view and philosophy of designing API. I agree that only one 'handle' is sufficient that could be either 'should be freed' or 'don't care'. Passing optional jerry_object_free_callback_t argument to jerry_api_set_object_native_handle seem to me consistent and clear.
@ruben-ayrapetyan , Thank you too for long description. We had a little f2f discussion and concluded follow your idea. I'll start on "adding jerry_object_free_callback_t argument (can be NULL) to jerry_api_set_object_native_handle, and removing separate jerry_api_set_object_free_callback interface." shortly.
@ILyoan , @seanshpark , it is great that we came to the same point of view. I also agree that using one API interface instead of several interdependent would be more clear and would increase consistency.
@ruben-ayrapetyan , how is the code https://github.com/seanshpark/jerryscript/commit/f079e3046a010205ee16ab2e799e9b8ea4f45960 ?
@seanshpark , the code is almost correct, except for freeing 'free callback' property:
If ecma_free_external_pointer_in_property is called, then the property itself is not freed, but its contents are. After GC would actually delete the property, it would invoke ecma_free_external_pointer_in_property in second time for the same property. So, double-deallocation of the property's contents would occur.
If instead of ecma_free_external_pointer_in_property, ecma_delete_property would be called, the code would be correct.
@seanshpark ,
There is patch (from seanshpark@f2be31f1820716f0472af8a3d199d175e9f273fb) I came to.
Could you, please, consider updating comment to the jerry_api_set_object_native_handle?
diff --git a/jerry-core/jerry.cpp b/jerry-core/jerry.cpp
index 2a7d7a1..ba6bc0f 100644
--- a/jerry-core/jerry.cpp
+++ b/jerry-core/jerry.cpp
@@ -749,20 +749,28 @@ 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 and
- * set object free callback for the specified object or NULL if not used
+ * 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 */
- jerry_object_free_callback_t freecb_p) /**< object free callback */
+ 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);
- if (freecb_p)
+ if (freecb_p != NULL)
{
ecma_create_external_pointer_property (object_p,
ECMA_INTERNAL_PROPERTY_FREE_CALLBACK,
@@ -772,9 +780,9 @@ jerry_api_set_object_native_handle (jerry_api_object_t *object_p, /**< object to
{
ecma_property_t *prop_p = ecma_find_internal_property (object_p,
ECMA_INTERNAL_PROPERTY_FREE_CALLBACK);
- if (prop_p)
+ if (prop_p != NULL)
{
- ecma_free_external_pointer_in_property (prop_p);
+ ecma_delete_property (object_p, prop_p);
}
}
} /* jerry_api_set_object_native_handle */
Great ! thank you !
Closing, da86a52fe9b25901fbb950c506c6135102637ad