Skip to content

Pass Module to callback functions#10777

Merged
kripken merged 4 commits into
emscripten-core:masterfrom
kleisauke:modularize-callback
Apr 3, 2020
Merged

Pass Module to callback functions#10777
kripken merged 4 commits into
emscripten-core:masterfrom
kleisauke:modularize-callback

Conversation

@kleisauke
Copy link
Copy Markdown
Collaborator

@kleisauke kleisauke commented Mar 26, 2020

This ensures that users can still invoke runtime elements within the
preRun function that are exported on Module (for e.g. ENV or FS)
when compiling with -s MODULARIZE=1.

Example usage:

var options = {
    preRun: [function (module) {
        module.ENV.hello = 'world';
        module.FS.createPreloadedFile('/', 'foo', 'bar', true, false);
    }]
};
Module(options).then(function (module) {

});

Note: this example requires that the code is compiled with

-s "EXTRA_EXPORTED_RUNTIME_METHODS=['FS', 'ENV']"

Resolves #9827.

This ensures that users can still invoke runtime elements within the
preRun function that are exported on Module (for e.g. "ENV" or "FS").
@welcome
Copy link
Copy Markdown

welcome Bot commented Mar 26, 2020

Thank you for submitting a pull request! If this is your first PR, make sure to add yourself to AUTHORS.

Copy link
Copy Markdown
Member

@kripken kripken left a comment

Choose a reason for hiding this comment

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

Good idea, thanks @kleisauke !

I wonder if this might be best to just do in all modes and not just modularize. By doing it all the time people can easily flip between the modes without problems (otherwise, modularize => non modularize could break). This does increase code size a tiny bit, but it's very very little, and seems worth it to me. What do you think?

@kleisauke kleisauke changed the title Pass Module to callback functions when MODULARIZE=1 Pass Module to callback functions Apr 2, 2020
@kleisauke
Copy link
Copy Markdown
Collaborator Author

Yes, that makes sense. Done with 0bd7cc9, also added some tests for this in 9ea25da. I guess that I don't have to make the same change in preamble_minimal.js?

Since it seems that with -s MINIMAL_RUNTIME=1 only the __ATEXIT__ callbacks are called.

@kripken
Copy link
Copy Markdown
Member

kripken commented Apr 3, 2020

Yes, in the minimal runtime these callbacks aren't supported yet, so nothing to do there.

Copy link
Copy Markdown
Member

@kripken kripken left a comment

Choose a reason for hiding this comment

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

Perfect, thanks @kleisauke !

I think it would also be good to document this on the site. Perhaps on https://emscripten.org/docs/api_reference/module.html or https://emscripten.org/docs/porting/connecting_cpp_and_javascript/Interacting-with-code.html? (both are under site/ in this repo)

But let's land this first, that can be a followup. Would be great if you can open a PR for that!

@kripken
Copy link
Copy Markdown
Member

kripken commented Apr 3, 2020

(Oh, also we should update the Changelog later.)

@kripken kripken merged commit 6d9681a into emscripten-core:master Apr 3, 2020
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.

ENV and IDBFS are undefined in 1.38.47-upstream & above

2 participants