Skip to content

Commit ce7a226

Browse files
author
Gabriel Schulhof
committed
Implement basic per-context module data
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 #1717 JerryScript-DCO-1.0-Signed-off-by: Gabriel Schulhof [email protected]
1 parent 574dff5 commit ce7a226

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

jerry-core/jcontext/jcontext.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@
3434
*/
3535
#define JERRY_CONTEXT_FIRST_MEMBER ecma_builtin_objects
3636

37+
#ifndef JERRY_CONTEXT_MODULE_COUNT
38+
#define JERRY_CONTEXT_MODULE_COUNT 8
39+
#endif /* ndef JERRY_CONTEXT_MODULE_COUNT */
40+
41+
typedef struct {
42+
void (*deleter)(void *);
43+
void *data;
44+
} jerry_module_data_t;
45+
3746
/**
3847
* JerryScript context
3948
*
@@ -103,6 +112,7 @@ typedef struct
103112
uint8_t valgrind_freya_mempool_request; /**< Tells whether a pool manager
104113
* allocator request is in progress */
105114
#endif /* JERRY_VALGRIND_FREYA */
115+
jerry_module_data_t module_data[JERRY_CONTEXT_MODULE_COUNT];
106116
} jerry_context_t;
107117

108118
/**

jerry-core/jerry.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2087,3 +2087,46 @@ jerry_dispatch_object_free_callback (ecma_external_pointer_t freecb_p, /**< poin
20872087

20882088
jerry_make_api_available ();
20892089
} /* jerry_dispatch_object_free_callback */
2090+
2091+
/**
2092+
* Reserve a slot in the context for a module
2093+
*
2094+
* Returns: 0 or positive on success, -1 on failure.
2095+
*/
2096+
2097+
/* This value is context-independent */
2098+
static int module_index = -1;
2099+
int
2100+
jerry_register_module(void) {
2101+
if (module_index == JERRY_CONTEXT_MODULE_COUNT - 1) {
2102+
return -1;
2103+
}
2104+
return (++module_index);
2105+
}
2106+
2107+
/**
2108+
* Retrieve the module's data from the context
2109+
*
2110+
* Returns: The module's data on success, NULL of failure.
2111+
*/
2112+
void *
2113+
jerry_get_module_data(int index) {
2114+
void *return_value = NULL;
2115+
2116+
if (index >= 0 && index < JERRY_CONTEXT_MODULE_COUNT) {
2117+
return_value = (JERRY_CONTEXT (module_data)[index]).data;
2118+
}
2119+
2120+
return return_value;
2121+
}
2122+
2123+
/**
2124+
* Set the module's data for the context
2125+
*/
2126+
void
2127+
jerry_set_module_data(int index, void *data, void (*deleter)(void *)) {
2128+
if (index >= 0 && index < JERRY_CONTEXT_MODULE_COUNT) {
2129+
(JERRY_CONTEXT (module_data)[index]).data = data;
2130+
(JERRY_CONTEXT (module_data)[index]).deleter = deleter;
2131+
}
2132+
}

jerry-core/jerryscript.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,13 @@ jerry_value_t jerry_exec_snapshot (const uint32_t *snapshot_p, size_t snapshot_s
343343
size_t jerry_parse_and_save_literals (const jerry_char_t *source_p, size_t source_size, bool is_strict,
344344
uint32_t *buffer_p, size_t buffer_size, bool is_c_format);
345345

346+
/**
347+
* Module management
348+
*/
349+
int jerry_register_module(void);
350+
void *jerry_get_module_data(int index);
351+
void jerry_set_module_data(int index, void *data, void (*deleter)(void *));
352+
346353
/**
347354
* @}
348355
*/

0 commit comments

Comments
 (0)