-
Notifications
You must be signed in to change notification settings - Fork 675
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
Support a user context void * pointer in jerry_context_t (#1717) #1727
Conversation
Nice 💯 |
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
@@ -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; |
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
*/ | ||
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.
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? |
docs/02.API-REFERENCE.md
Outdated
```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)
{
docs/02.API-REFERENCE.md
Outdated
|
||
} /* 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 (
jerry-core/jerry.c
Outdated
* 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, |
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
@@ -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, |
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. |
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
@@ -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 */ |
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! |
docs/02.API-REFERENCE.md
Outdated
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_context
instead. -
in the
##jerry_init_with_user_context
: Note:jerry_init
will be called insidejerry_init_with_user_context
, anddeinit_cb
will be called in thejerry_cleanup
-
in the
##jerry_cleanup
: If the engine is Initialized byjerry_init_with_user_context
withdeinit_cb
, then thedeinit_cb
will 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, | ||
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 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 [email protected]
f34219c
to
069d5cd
Compare
Still 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.
Thanks for the update. 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.
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_context
element in thejerry_context_t
structure 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 [email protected]