Skip to content

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

Merged

Conversation

gabrielschulhof
Copy link
Contributor

JerryScript-DCO-1.0-Signed-off-by: Gabriel Schulhof [email protected]

My use case here is the creation of weak references to objects.

Copy link
Member

@zherczeg zherczeg left a 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.

if (find_data_p->match_data_p == data_p)
{

// If the object was found, acquire it and store it in the user data.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this newline here.

Copy link
Contributor Author

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.

Copy link
Member

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?

Copy link
Contributor Author

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,
Copy link
Member

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.

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 &&
Copy link
Member

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.

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),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong && and alignment.

@gabrielschulhof
Copy link
Contributor Author

@zherczeg I have fixed the issues you mentioned.

@gabrielschulhof
Copy link
Contributor Author

@zherczeg I have now removed that newline as well.

@martijnthe
Copy link
Contributor

Ooooh, this is useful. Thanks for adding!

@zherczeg
Copy link
Member

zherczeg commented Mar 9, 2018

One more question: why the test is called test-weak-reference.c ? Shouldn't be a test-traverse-objects.c?

@gabrielschulhof
Copy link
Contributor Author

gabrielschulhof commented Mar 9, 2018

@zherczeg it's called that because it also illustrates how one would implement a weak reference.

@zherczeg
Copy link
Member

The name would be ok for an example, but this is a test which tests a particular API function. What do you think?

{
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
Copy link
Contributor

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_ts 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.

Copy link
Contributor Author

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.

@gabrielschulhof
Copy link
Contributor Author

@zherczeg You're right. I will rename it.

@gabrielschulhof
Copy link
Contributor Author

@zherczeg I have renamed the file and I have exposed the more basic traversal as @martijnthe requested.

**Prototype**

```c
bool jerry_objects_foreach (jerry_objects_
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got interrupted? ;)

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;
Copy link
Contributor

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?

@martijnthe
Copy link
Contributor

I have exposed the more basic traversal as @martijnthe requested.

Thanks!

@gabrielschulhof
Copy link
Contributor Author

@martijnthe I have finished the documentation and unpacked the return value in the callback.

};

return jerry_objects_foreach (jerry_objects_foreach_by_native_info_callback, &iteration_info);
} /* jerry_objects_foreach_by_native_info */
Copy link
Member

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);
/**
*
Copy link
Member

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]
@gabrielschulhof
Copy link
Contributor Author

@zherczeg I have addressed your review comments.

@gabrielschulhof
Copy link
Contributor Author

Copy link
Member

@zherczeg zherczeg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

@LaszloLango LaszloLango left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants