-
Notifications
You must be signed in to change notification settings - Fork 682
Add an API to traverse objects by their associated native data #2236
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
Add an API to traverse objects by their associated native data #2236
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good patch. Some style issues.
docs/02.API-REFERENCE.md
Outdated
if (find_data_p->match_data_p == data_p) | ||
{ | ||
|
||
// If the object was found, acquire it and store it in the user data. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this newline here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no newline there. Github added it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand. There is a newline in line 4648 above the comment. Why github would add it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, that newline. I thought you meant the one between "user" and "data". I'll remove it.
@@ -211,6 +211,10 @@ typedef bool (*jerry_object_property_foreach_t) (const jerry_value_t property_na | |||
const jerry_value_t property_value, | |||
void *user_data_p); | |||
|
|||
typedef bool (*jerry_native_info_foreach_t) (const jerry_value_t object, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No comment for this type.
jerry-core/api/jerry.c
Outdated
if (!ecma_is_lexical_environment (iter_p)) | ||
{ | ||
native_pointer_p = ecma_get_native_pointer_value (iter_p, LIT_INTERNAL_MAGIC_STRING_NATIVE_POINTER); | ||
if (native_pointer_p != NULL && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wrong place of &&
. Should go the the second line.
jerry-core/api/jerry.c
Outdated
native_pointer_p = ecma_get_native_pointer_value (iter_p, LIT_INTERNAL_MAGIC_STRING_NATIVE_POINTER); | ||
if (native_pointer_p != NULL && | ||
((const jerry_object_native_info_t *) native_pointer_p->u.info_p) == native_info_p && | ||
!foreach_p (ecma_make_object_value (iter_p), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wrong &&
and alignment.
36e7808
to
2d3707c
Compare
@zherczeg I have fixed the issues you mentioned. |
2d3707c
to
4404d1f
Compare
@zherczeg I have now removed that newline as well. |
Ooooh, this is useful. Thanks for adding! |
One more question: why the test is called test-weak-reference.c ? Shouldn't be a test-traverse-objects.c? |
@zherczeg it's called that because it also illustrates how one would implement a weak reference. |
The name would be ok for an example, but this is a test which tests a particular API function. What do you think? |
jerry-core/api/jerry.c
Outdated
{ | ||
native_pointer_p = ecma_get_native_pointer_value (iter_p, LIT_INTERNAL_MAGIC_STRING_NATIVE_POINTER); | ||
if (native_pointer_p != NULL | ||
&& ((const jerry_object_native_info_t *) native_pointer_p->u.info_p) == native_info_p |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In our project we have various jerry_object_native_info_t
s where the nativestructures they represent are subclasses of one another.
Say I have base class B
and subclass S1
and S2
, with the current API proposal, if I want to traverse all objects of the base class, I would have to call jerry_traverse_objects_by_native_info
3x, with the jerry_object_native_info_t *
of B
of S1
and of S2
.
That makes me wonder whether it would make sense to split this function into a more primitive bool jerry_traverse_objects(jerry_native_info_foreach_t foreach_p, void *user_data_p)
(and perhaps call it ...foreach...
instead?)
jerry_traverse_objects_by_native_info
could be implemented on top of that as a convenience function for the simple case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK. I'll do it that way.
@zherczeg You're right. I will rename it. |
4404d1f
to
c0474f7
Compare
@zherczeg I have renamed the file and I have exposed the more basic traversal as @martijnthe requested. |
docs/02.API-REFERENCE.md
Outdated
**Prototype** | ||
|
||
```c | ||
bool jerry_objects_foreach (jerry_objects_ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got interrupted? ;)
jerry-core/api/jerry.c
Outdated
return native_pointer_p != NULL | ||
&& ((const jerry_object_native_info_t *) native_pointer_p->u.info_p) == iteration_info_p->native_info_p | ||
? iteration_info_p->foreach_p (value, native_pointer_p->data_p, iteration_info_p->user_data_p) | ||
: true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you write this such that it's a bit easier to grasp?
Thanks! |
c0474f7
to
dcb1b8a
Compare
@martijnthe I have finished the documentation and unpacked the return value in the callback. |
jerry-core/api/jerry.c
Outdated
}; | ||
|
||
return jerry_objects_foreach (jerry_objects_foreach_by_native_info_callback, &iteration_info); | ||
} /* jerry_objects_foreach_by_native_info */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe my impression but using the other function to implement this actually increase complexity rather than decrease it. I would just duplicate the code.
@@ -210,6 +210,18 @@ typedef jerry_value_t (*jerry_vm_exec_stop_callback_t) (void *user_p); | |||
typedef bool (*jerry_object_property_foreach_t) (const jerry_value_t property_name, | |||
const jerry_value_t property_value, | |||
void *user_data_p); | |||
/** | |||
* |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing comment.
JerryScript-DCO-1.0-Signed-off-by: Gabriel Schulhof [email protected]
dcb1b8a
to
3bb63dc
Compare
@zherczeg I have addressed your review comments. |
Could you please restart https://travis-ci.org/jerryscript-project/jerryscript/jobs/353479244? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
JerryScript-DCO-1.0-Signed-off-by: Gabriel Schulhof [email protected]
My use case here is the creation of weak references to objects.