Skip to content
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

Support a user context void * pointer in jerry_context_t (#1717) #1727

Conversation

gabrielschulhof
Copy link
Contributor

This modification makes it possible to initialize a context in such a
way that a void * pointer is stored inside the context and is made
available via a new jerry_get_user_context() API.

The pointer is initialized via a new jerry_init_with_user_context()
API, which calls the existing jerry_init(), after which it sets the
value of the new user_context element in the jerry_context_t
structure using the context allocation callback provided as the second
parameter to the new jerry_init_with_user_context() API. The location
of the cleanup function responsible for deallocating the pointer created
by the context allocation callback is provided as the third parameter.
This location is stored in the context along with the pointer itself.

When a context is discarded via jerry_cleanup(), the user context
cleanup function is called to dispose of the pointer stored within the
context.

The semantics behind the API are such that it is now possible to choose
for each context an agent which manages arbitrary user data keyed to the
given context. The agent must be chosen at context instantiation time
and cannot be changed afterwards, remaining in effect for the lifetime
of the context.

Fixes #1717

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

@martijnthe
Copy link
Contributor

Nice 💯

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.

Please also add a documentation in docs/02.API-REFERENCE.md

@@ -103,6 +103,8 @@ typedef struct
uint8_t valgrind_freya_mempool_request; /**< Tells whether a pool manager
* allocator request is in progress */
#endif /* JERRY_VALGRIND_FREYA */
void *user_context;
jerry_user_context_deinit_cb user_context_deinit_cb;
Copy link
Member

Choose a reason for hiding this comment

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

These are mandatory pointers, I would put them after vm_frame_ctx_t *vm_top_context_p;

*/
void
jerry_init_with_user_context (jerry_init_flag_t flags, jerry_user_context_init_cb init_cb,
jerry_user_context_deinit_cb deinit_cb)
Copy link
Member

Choose a reason for hiding this comment

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

Full stop after each sentence. Please add a comment for each function. Coding style is in #1459.

} /* jerry_cleanup */

/**
* Retrieve user context
Copy link
Member

Choose a reason for hiding this comment

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

Please add return value description.

static bool user_context_new_was_called = false;
static bool user_context_free_was_called = false;

static void *user_context_new (void)
Copy link
Member

Choose a reason for hiding this comment

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

Newline after static void.

@gabrielschulhof gabrielschulhof force-pushed the 1717-user-context-support branch 3 times, most recently from be4afe0 to afed69f Compare April 11, 2017 20:30
@gabrielschulhof
Copy link
Contributor Author

@zherczeg I have addressed your review comments. Could you please take another look?

```c
void
jerry_init_with_user_context (jerry_init_flag_t flags, jerry_user_context_init_cb init_cb,
jerry_user_context_deinit_cb deinit_cb);
Copy link
Contributor

Choose a reason for hiding this comment

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

missing line break:

jerry_init_with_user_context (jerry_init_flag_t flags,
                              jerry_user_context_init_cb init_cb,
                              jerry_user_context_deinit_cb deinit_cb);


```c
void *
init_user_context(void) {
Copy link
Contributor

Choose a reason for hiding this comment

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

init_user_context (void)
{

} /* init_user_context */

void
free_user_context(void *context) {
Copy link
Contributor

Choose a reason for hiding this comment

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

free_user_context (void *context)
{


} /* free_user_context */
{

Copy link
Contributor

Choose a reason for hiding this comment

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

move this empty line before the open brace

} /* free_user_context */
{

/* init_user_context() will be called before the call below returns */
Copy link
Contributor

Choose a reason for hiding this comment

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

missing space: init_user_context ()


/* init_user_context() will be called before the call below returns */
jerry_init_with_user_context (JERRY_INIT_SHOW_OPCODES | JERRY_INIT_SHOW_REGEXP_OPCODES,
init_user_context, free_user_context);
Copy link
Contributor

Choose a reason for hiding this comment

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

wrong indentation


/* ... */

/* free_user_context() will be called before the call below returns */
Copy link
Contributor

Choose a reason for hiding this comment

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

missing space: free_user_context ()

jerry_get_user_context (void);
```

- return value: the pointer that was assigned during `jerry_init_with_user_context()`
Copy link
Contributor

Choose a reason for hiding this comment

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

missing space before (

* Initialize Jerry engine with custom user context.
*/
void
jerry_init_with_user_context (jerry_init_flag_t flags, jerry_user_context_init_cb init_cb,
Copy link
Contributor

Choose a reason for hiding this comment

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

missing line break

@@ -189,11 +199,14 @@ typedef struct
* General engine functions
*/
void jerry_init (jerry_init_flag_t flags);
void jerry_init_with_user_context (jerry_init_flag_t flags, jerry_user_context_init_cb init_cb,
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto

@gabrielschulhof
Copy link
Contributor Author

@zherczeg @LaszloLango I have now address all review comments so far.

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.

Please fix the _p and squash commits. LGTM after that.

@@ -61,6 +61,8 @@ typedef struct
ecma_lit_storage_item_t *number_list_first_p; /**< first item of the literal number list */
ecma_object_t *ecma_global_lex_env_p; /**< global lexical environment */
vm_frame_ctx_t *vm_top_context_p; /**< top (current) interpreter context */
void *user_context; /**< user-provided context-specific pointer */
Copy link
Member

Choose a reason for hiding this comment

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

user_context_p - this is a pointer

@gabrielschulhof gabrielschulhof force-pushed the 1717-user-context-support branch 2 times, most recently from 787dbf7 to d6d3dea Compare April 12, 2017 12:48
@gabrielschulhof
Copy link
Contributor Author

@LaszloLango should all be good now. Please take another look!

function calls the callback given in its `init_cb` parameter to allocate the memory for the pointer
and it stores the function pointer given in the `deinit_cb` parameter along with the pointer so that
it may be called to free the stored pointer when the context is discarded.

Copy link
Contributor

@jiangzidong jiangzidong Apr 13, 2017

Choose a reason for hiding this comment

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

Could you please describe the relationship among jerry_init_with_user_context, jerry_init and jerry_cleanup.
e.g.

  • in the ##jerry_init: Note: If a user context need to be initialized, please use jerry_init_with_user_context instead.

  • in the ##jerry_init_with_user_context: Note: jerry_init will be called inside jerry_init_with_user_context, and deinit_cb will be called in the jerry_cleanup

  • in the ##jerry_cleanup: If the engine is Initialized by jerry_init_with_user_context with deinit_cb, then the deinit_cb will be called during cleanup.

What do you think?

Copy link
Member

Choose a reason for hiding this comment

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

This is a good idea.

{
/* init_user_context () will be called before the call below returns */
jerry_init_with_user_context (JERRY_INIT_SHOW_OPCODES | JERRY_INIT_SHOW_REGEXP_OPCODES,
init_user_context, free_user_context);
Copy link
Contributor

Choose a reason for hiding this comment

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

missing line break. Every parameter should be on separate line.

void
jerry_init_with_user_context (jerry_init_flag_t flags,
jerry_user_context_init_cb init_cb,
jerry_user_context_deinit_cb deinit_cb)
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing comments of the arguments.

@gabrielschulhof gabrielschulhof force-pushed the 1717-user-context-support branch from d6d3dea to f34219c Compare April 13, 2017 12:28
@gabrielschulhof
Copy link
Contributor Author

@jiangzidong, @LaszloLango how's about now?

…-project#1717)

This modification makes it possible to initialize a context in such a
way that a `void *` pointer is stored inside the context and is made
available via a new `jerry_get_user_context()` API.

The pointer is initialized via a new `jerry_init_with_user_context()`
API, which calls the existing `jerry_init()`, after which it sets the
value of the new `user_context` element in the `jerry_context_t`
structure using the context allocation callback provided as the second
parameter to the new `jerry_init_with_user_context()` API. The location
of the cleanup function responsible for deallocating the pointer created
by the context allocation callback is provided as the third parameter.
This location is stored in the context along with the pointer itself.

When a context is discarded via `jerry_cleanup()`, the user context
cleanup function is called to dispose of the pointer stored within the
context.

The semantics behind the API are such that it is now possible to choose
for each context an agent which manages arbitrary user data keyed to the
given context. The agent must be chosen at context instantiation time
and cannot be changed afterwards, remaining in effect for the lifetime
of the context.

Fixes jerryscript-project#1717

JerryScript-DCO-1.0-Signed-off-by: Gabriel Schulhof [email protected]
@gabrielschulhof gabrielschulhof force-pushed the 1717-user-context-support branch from f34219c to 069d5cd Compare April 13, 2017 12:29
@zherczeg
Copy link
Member

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

Thanks for the update. LGTM

Copy link
Contributor

@jiangzidong jiangzidong left a comment

Choose a reason for hiding this comment

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

Thanks. LGTM

@jiangzidong jiangzidong merged commit b4a3985 into jerryscript-project:master Apr 14, 2017
@gabrielschulhof gabrielschulhof deleted the 1717-user-context-support branch April 18, 2017 21:49
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.

5 participants