Lazily create a linked list of context items (#1833)

This approach has the benefit that it does not require any *a priori*
initialization, and that each context pointer is identified by the way
in which it was created. Additionally, retrieving the context pointer
now requires that the entity responsible for creating/destroying it
(the manager) be given. Since managers are stored in global static
const structures, they should not normally be visible across source
files, and thus there should be no danger that a context item will be
retrieved by the wrong manager and thus cast into the wrong data type.

Since the items are stored in a linked list, their number will be
limited to exactly as many as are needed for a given context, with the
caveat that storing too many on a context will cause slow retrieval.

Thanks @mhdawson for the idea!

Fixes https://github.com/jerryscript-project/jerryscript/issues/1845

JerryScript-DCO-1.0-Signed-off-by: Gabriel Schulhof gabriel.schulhof@intel.com
This commit is contained in:
Gabriel "_|Nix|_" Schulhof
2017-05-24 15:10:18 +03:00
committed by Zoltan Herczeg
parent 29f57ec58f
commit de53adbf88
6 changed files with 233 additions and 185 deletions
+76 -94
View File
@@ -101,6 +101,25 @@ created by API functions has the error flag set.
typedef uint32_t jerry_value_t;
```
## jerry_context_data_manager_t
**Summary**
Structure that defines how a context data item will be initialized and deinitialized. JerryScript zeroes out the memory
for the item by default, and if the `init_cb` field is not NULL, it will be called with the pointer to the memory as
an additional custom initializer.
**Prototype**
```c
typedef struct
{
void (*init_cb) (void *); /**< callback responsible for initializing a context item, or NULL */
void (*deinit_cb) (void *); /**< callback responsible for deinitializing a context item */
size_t bytes_needed; /**< number of bytes to allocate for this manager */
} jerry_context_data_manager_t;
```
## jerry_property_descriptor_t
**Summary**
@@ -264,9 +283,7 @@ typedef jerry_value_t (*jerry_vm_exec_stop_callback_t) (void *user_p);
**Summary**
Initializes the JerryScript engine, making it possible to run JavaScript code and perform operations
on JavaScript values. See also [jerry_init_with_user_context](#jerry_init_with_user_context) if you
wish to initialize the JerryScript engine in such a way that its context contains a custom pointer
which you can later retrieve using [jerry_get_user_context](#jerry_get_user_context).
on JavaScript values.
**Prototype**
@@ -299,91 +316,13 @@ jerry_init (jerry_init_flag_t flags)
**See also**
- [jerry_cleanup](#jerry_cleanup)
- [jerry_init_with_user_context](#jerry_init_with_user_context)
## jerry_init_with_user_context
**Summary**
Calls [jerry_init](#jerry_init) to initialize the JerryScript engine, thereby making it possible
to run JavaScript code and perform operations on JavaScript values. In addition to the first
parameter this function accepts two more parameters with which it allows the caller to store a
`void *` pointer inside the context being initialized with `jerry_init ()`. The 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 `jerry_cleanup ()` is later called to dispose of the context.
**Prototype**
```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);
```
`flags` - combination of various engine configuration flags:
- `JERRY_INIT_EMPTY` - no flags, just initialize in default configuration.
- `JERRY_INIT_SHOW_OPCODES` - print compiled byte-code.
- `JERRY_INIT_SHOW_REGEXP_OPCODES` - print compiled regexp byte-code.
- `JERRY_INIT_MEM_STATS` - dump memory statistics.
- `JERRY_INIT_MEM_STATS_SEPARATE` - dump memory statistics and reset peak values after parse.
- `JERRY_INIT_DEBUGGER` - enable all features required by debugging.
`init_cb` - a function pointer that will be called to allocate the custom pointer.
`deinit_cb` - a function pointer that will be called when the custom pointer must be freed.
**Example**
```c
void *
init_user_context (void)
{
void *return_value;
/* allocate and initialize return_value */
return return_value;
} /* init_user_context */
void
free_user_context (void *context)
{
/* free the value allocated above */
} /* free_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);
/* ... */
/* free_user_context () will be called before the call below returns */
jerry_cleanup ();
}
```
**See also**
- [jerry_cleanup](#jerry_cleanup)
- [jerry_get_user_context](#jerry_get_user_context)
## jerry_cleanup
**Summary**
Finish JavaScript engine execution, freeing memory and JavaScript values. If the context was
initialized with `jerry_init_with_user_context ()` and a `deinit_cb` was provided, then it will
be called to free the memory at the custom pointer which was associated with the context being
cleaned up.
Finish JavaScript engine execution, freeing memory and JavaScript values.
*Note*: JavaScript values, received from engine, will be inaccessible after the cleanup.
@@ -397,38 +336,81 @@ jerry_cleanup (void);
**See also**
- [jerry_init](#jerry_init)
- [jerry_init_with_user_context](#jerry_init_with_user_context)
## jerry_get_user_context
## jerry_get_context_data
**Summary**
Retrieve the pointer stored within the current context.
Retrieve a pointer to the item stored within the current context by the given manager.
*Note*: Since internally the pointer to a manager's context data item is linked to the next such pointer in a linked
list, it is inadvisable to invoke too many different managers, because doing so will increase the time it takes
to retrieve a manager's context data item, degrading performance. For example, try to keep the number of
managers below five.
**Prototype**
```c
void *
jerry_get_user_context (void);
jerry_get_context_data (const jerry_context_data_manager *manager_p);
```
- return value: the pointer that was assigned during `jerry_init_with_user_context ()`
- `manager_p`: the manager of this context data item.
- return value: the item created by `manager_p` when `jerry_get_context_data ()` was first called, or a new item created
by `manager_p`, which will be stored for future identical calls to `jerry_get_context_data ()`, and which will be
deinitialized using the `deinit_cb` callback provided by `manager_p` when the context will be destroyed.
**Example**
```c
typedef struct
{
/* ... */
my_context *custom_data = (my_context *) jerry_get_user_context ();
/* ... */
int my_data1;
double my_data2;
char *my_data3;
} my_context_data_t;
/* Define how context items will be initialized. */
static void
my_context_data_new (void *user_data_p)
{
my_context_data_t *my_data_p = (my_context_data_t *) user_data_p;
/*
* Initialize my_data_p. JerryScript will store it on the current context and return it whenever
* jerry_get_context_data () is called with a pointer to my_manager as defined below.
*/
}
/* Define how context items will be deinitialized */
static void
my_context_data_free (void *user_data_p)
{
my_context_data_t *my_data_p = ((my_context_data_t *) user_data_p);
/* Perform any necessary cleanup on my_data. JerryScript will free the pointer after this function completes. */
}
/* Wrap the creation and destruction functions into a manager */
static const jerry_context_data_manager_t my_manager =
{
.init_cb = my_context_data_new,
.deinit_cb = my_context_data_free,
.bytes_needed = sizeof (my_context_data_t)
};
/*
* Then, in some function in your code, you can retrieve an item of type my_context_data_t from the currently active
* context such that JerryScript will create and store such an item if one was not previously created
*/
void someplace_in_the_code (void)
{
my_context_data_t *my_data = (my_context_data_t *) jerry_get_context_data (&my_manager);
/* Perform useful things using the data found in my_data */
}
```
**See also**
- [jerry_init_with_user_context](#jerry_init_with_user_context)
- [jerry_cleanup](#jerry_cleanup)
## jerry_register_magic_strings