Skip to content
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

PoC: Implement basic per-context module data #1723

Conversation

gabrielschulhof
Copy link
Contributor

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

@martijnthe is this like what you had in mind?
@zherczeg note that when JERRY_CONTEXT_MODULE_COUNT is defined as 1 then this is exactly the single pointer case.

@gabrielschulhof
Copy link
Contributor Author

... and calling jerry_register_module() from a library constructor would make module registration complete, very clean, and in line with what Node.js does (#1722).

*/

/* This value is context-independent */
static int module_index = -1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does "context-independent" means all context share the same index? if so, Why?

IMO, the index should be in jerry_context, and each context can have different modules.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the index counts the modules that were registered in the process not the instances of the modules that were started in a context.

I originally had the index in the context but I realized that jerry_register_module() will only be called once for each module, no matter how many contexts are later created.

Copy link
Contributor Author

@gabrielschulhof gabrielschulhof Apr 10, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, like, slot 0 belongs to canvas and slot 1 belongs to sqlite, etc. When you start a new context, then slot 0 in that context will belong to the instance of canvas loaded into that context, and slot 1 in that context will belong to sqlite loaded into that context, and the same for every subsequent context.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess I should add a test with two trivial modules to test the data handholding.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, in the proposal that arose from #1717, module indices are global (and assigned once per boot-up).
That means that if multiple contexts use different sets of modules, there will be unused slots in the module_data array. I think this is probably acceptable.

The alternative is to add another per-context module registration step and another level of indirection. That would also add extra memory, so I'm not convinced that at the bottom line this would be better compared to having a couple unused slots.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, it makes sense.

*/
int jerry_register_module(void);
void *jerry_get_module_data(int index);
void jerry_set_module_data(int index, void *data, void (*deleter)(void *));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you know the module index in a module?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

global static, assigned only once when the constructor gets called (implemented ideally via __attribute__((constructor)) but an exported function that is called from main() will work too).

@gabrielschulhof
Copy link
Contributor Author

Man vera is hard to find - not part of Fedora, at any rate ...

@gabrielschulhof
Copy link
Contributor Author

OK, I dunno how to fix the rest of the linting errors 🤷

@@ -103,6 +113,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];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My guess is we'll want to have a "feature-flag" (#define / #ifdef) around all of this so that people still have the option to go for the most bare bones config possible.

*/

/* This value is context-independent */
static int module_index = -1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, in the proposal that arose from #1717, module indices are global (and assigned once per boot-up).
That means that if multiple contexts use different sets of modules, there will be unused slots in the module_data array. I think this is probably acceptable.

The alternative is to add another per-context module registration step and another level of indirection. That would also add extra memory, so I'm not convinced that at the bottom line this would be better compared to having a couple unused slots.

@zherczeg
Copy link
Member

I think native module management should not be part of JerryScript, which is an ECMAScript engine. This should be done in the utility library, where Standard Modules could use a nice generic interface. This could be one implementation of the interface. What do you think?

@zherczeg
Copy link
Member

@gabrielschulhof please check the discussion in #1717

@gabrielschulhof
Copy link
Contributor Author

@zherczeg so ... move to a new jerry-utilities/module?

@zherczeg
Copy link
Member

In #1716 @martijnthe proposed 3 places for such utilities. Perhaps you could also vote.

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]
@gabrielschulhof gabrielschulhof force-pushed the context-module-list branch 2 times, most recently from e7126c9 to ebf07cf Compare April 11, 2017 08:38
@LaszloLango
Copy link
Contributor

Is this PR still valid?

@gabrielschulhof gabrielschulhof deleted the context-module-list branch August 22, 2017 13:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants