-
Notifications
You must be signed in to change notification settings - Fork 688
Support a user context void * pointer in jerry_context_t (#1717) #1727
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
|
Nice 💯 |
zherczeg
left a comment
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.
Please also add a documentation in docs/02.API-REFERENCE.md
jerry-core/jcontext/jcontext.h
Outdated
| * allocator request is in progress */ | ||
| #endif /* JERRY_VALGRIND_FREYA */ | ||
| void *user_context; | ||
| jerry_user_context_deinit_cb user_context_deinit_cb; |
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.
These are mandatory pointers, I would put them after vm_frame_ctx_t *vm_top_context_p;
jerry-core/jerry.c
Outdated
| * Jerry engine initialization with custom user context | ||
| */ | ||
| void | ||
| jerry_init_with_user_context (jerry_init_flag_t flags, jerry_user_context_init_cb init_cb, |
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.
Full stop after each sentence. Please add a comment for each function. Coding style is in #1459.
jerry-core/jerry.c
Outdated
| } /* jerry_cleanup */ | ||
|
|
||
| /** | ||
| * Retrieve user context |
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.
Please add return value description.
tests/unit/test-user-context.c
Outdated
| static bool user_context_new_was_called = false; | ||
| static bool user_context_free_was_called = false; | ||
|
|
||
| static void *user_context_new (void) |
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.
Newline after static void.
be4afe0 to
afed69f
Compare
|
@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); |
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 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);
docs/02.API-REFERENCE.md
Outdated
|
|
||
| ```c | ||
| void * | ||
| init_user_context(void) { |
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.
init_user_context (void)
{
docs/02.API-REFERENCE.md
Outdated
| } /* init_user_context */ | ||
|
|
||
| void | ||
| free_user_context(void *context) { |
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.
free_user_context (void *context)
{|
|
||
| } /* free_user_context */ | ||
| { | ||
|
|
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.
move this empty line before the open brace
docs/02.API-REFERENCE.md
Outdated
| } /* free_user_context */ | ||
| { | ||
|
|
||
| /* init_user_context() will be called before the call below returns */ |
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 space: init_user_context ()
docs/02.API-REFERENCE.md
Outdated
|
|
||
| /* 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); |
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 indentation
docs/02.API-REFERENCE.md
Outdated
|
|
||
| /* ... */ | ||
|
|
||
| /* free_user_context() will be called before the call below returns */ |
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 space: free_user_context ()
docs/02.API-REFERENCE.md
Outdated
| jerry_get_user_context (void); | ||
| ``` | ||
|
|
||
| - return value: the pointer that was assigned during `jerry_init_with_user_context()` |
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 space before (
| /** | ||
| * Initialize Jerry engine with custom user context. | ||
| */ | ||
| void |
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 line break
jerry-core/jerryscript.h
Outdated
| * 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, |
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.
ditto
|
@zherczeg @LaszloLango I have now address all review comments so far. |
zherczeg
left a comment
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.
Please fix the _p and squash commits. LGTM after that.
jerry-core/jcontext/jcontext.h
Outdated
| 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 */ |
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.
user_context_p - this is a pointer
787dbf7 to
d6d3dea
Compare
|
@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. | ||
|
|
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 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 usejerry_init_with_user_contextinstead. -
in the
##jerry_init_with_user_context: Note:jerry_initwill be called insidejerry_init_with_user_context, anddeinit_cbwill be called in thejerry_cleanup -
in the
##jerry_cleanup: If the engine is Initialized byjerry_init_with_user_contextwithdeinit_cb, then thedeinit_cbwill be called during cleanup.
What do you think?
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.
This is a good idea.
docs/02.API-REFERENCE.md
Outdated
| { | ||
| /* 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); |
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 line break. Every parameter should be on separate line.
jerry-core/jerry.c
Outdated
| */ | ||
| void | ||
| jerry_init_with_user_context (jerry_init_flag_t flags, | ||
| jerry_user_context_init_cb init_cb, |
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 comments of the arguments.
d6d3dea to
f34219c
Compare
|
@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 gabriel.schulhof@intel.com
f34219c to
069d5cd
Compare
|
Still LGTM. |
LaszloLango
left a comment
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.
Thanks for the update. LGTM
jiangzidong
left a comment
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.
Thanks. LGTM
This modification makes it possible to initialize a context in such a
way that a
void *pointer is stored inside the context and is madeavailable 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 thevalue of the new
user_contextelement in thejerry_context_tstructure using the context allocation callback provided as the second
parameter to the new
jerry_init_with_user_context()API. The locationof 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 contextcleanup 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 gabriel.schulhof@intel.com