Skip to content

Commit

Permalink
Implement basic per-context module data
Browse files Browse the repository at this point in the history
Given an expected number of modules (JERRY_CONTEXT_MODULE_COUNT) which
can be defined at compile time, reserve two pointers in each context for
module-specific data: one void * for the data, and a function pointer
for the deleter to be called when the context is discarded.

The modules receive an index into the data array when they register.
They can later use the index to store data in the context.

Re jerryscript-project#1717

JerryScript-DCO-1.0-Signed-off-by: Gabriel Schulhof [email protected]
  • Loading branch information
Gabriel Schulhof committed Apr 10, 2017
1 parent 574dff5 commit ce7a226
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
10 changes: 10 additions & 0 deletions jerry-core/jcontext/jcontext.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@
*/
#define JERRY_CONTEXT_FIRST_MEMBER ecma_builtin_objects

#ifndef JERRY_CONTEXT_MODULE_COUNT
#define JERRY_CONTEXT_MODULE_COUNT 8
#endif /* ndef JERRY_CONTEXT_MODULE_COUNT */

typedef struct {
void (*deleter)(void *);
void *data;
} jerry_module_data_t;

/**
* JerryScript context
*
Expand Down Expand Up @@ -103,6 +112,7 @@ typedef struct
uint8_t valgrind_freya_mempool_request; /**< Tells whether a pool manager
* allocator request is in progress */
#endif /* JERRY_VALGRIND_FREYA */
jerry_module_data_t module_data[JERRY_CONTEXT_MODULE_COUNT];
} jerry_context_t;

/**
Expand Down
43 changes: 43 additions & 0 deletions jerry-core/jerry.c
Original file line number Diff line number Diff line change
Expand Up @@ -2087,3 +2087,46 @@ jerry_dispatch_object_free_callback (ecma_external_pointer_t freecb_p, /**< poin

jerry_make_api_available ();
} /* jerry_dispatch_object_free_callback */

/**
* Reserve a slot in the context for a module
*
* Returns: 0 or positive on success, -1 on failure.
*/

/* This value is context-independent */
static int module_index = -1;
int
jerry_register_module(void) {
if (module_index == JERRY_CONTEXT_MODULE_COUNT - 1) {
return -1;
}
return (++module_index);
}

/**
* Retrieve the module's data from the context
*
* Returns: The module's data on success, NULL of failure.
*/
void *
jerry_get_module_data(int index) {
void *return_value = NULL;

if (index >= 0 && index < JERRY_CONTEXT_MODULE_COUNT) {
return_value = (JERRY_CONTEXT (module_data)[index]).data;
}

return return_value;
}

/**
* Set the module's data for the context
*/
void
jerry_set_module_data(int index, void *data, void (*deleter)(void *)) {
if (index >= 0 && index < JERRY_CONTEXT_MODULE_COUNT) {
(JERRY_CONTEXT (module_data)[index]).data = data;
(JERRY_CONTEXT (module_data)[index]).deleter = deleter;
}
}
7 changes: 7 additions & 0 deletions jerry-core/jerryscript.h
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,13 @@ jerry_value_t jerry_exec_snapshot (const uint32_t *snapshot_p, size_t snapshot_s
size_t jerry_parse_and_save_literals (const jerry_char_t *source_p, size_t source_size, bool is_strict,
uint32_t *buffer_p, size_t buffer_size, bool is_c_format);

/**
* Module management
*/
int jerry_register_module(void);
void *jerry_get_module_data(int index);
void jerry_set_module_data(int index, void *data, void (*deleter)(void *));

/**
* @}
*/
Expand Down

0 comments on commit ce7a226

Please sign in to comment.