Skip to content

Commit

Permalink
Add custom asyncify section(s)
Browse files Browse the repository at this point in the history
Names of all the asyncified export functions are now added as data of custom "asyncify" sections.

This allows to detect them in a JS wrapper and wrap into async JS functions only if necessary.

We could come up with a more condensed representation that doesn't duplicate export function names or name of "asyncify" section itself, but this would add unnecessary complexity to implementation with little benefit after Gzip / Brotli, which already take care of duplicated strings quite well.

Fixes WebAssembly#2322.
  • Loading branch information
RReverser committed Sep 4, 2019
1 parent 30cb271 commit ccb4383
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/passes/Asyncify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1150,6 +1150,32 @@ struct Asyncify : public Pass {
runner.setValidateGlobally(false);
runner.run();
}
// Mark exported asyncified functions.
//
// This creates a custom section for each function that:
// 1) Is exported and, thus, directly accessible by user.
// 2) Might directly or indirectly start an async operation (and unwind).
//
// Each such section will have a name "asyncify" and name of the export as
// its data.
//
// This gives the JavaScript side an opportunity to wrap functions into an
// `async` variant only if it's necessary, and leave others intact.
auto& userSections = module->userSections;
for (auto& exp : module->exports) {
if (exp->kind != ExternalKind::Function) {
continue;
}
auto* func = module->getFunction(exp->value);
if (!analyzer.needsInstrumentation(func)) {
continue;
}
userSections.resize(userSections.size() + 1);
auto& section = userSections.back();
section.name = BinaryConsts::UserSections::Asyncify;
auto& name = exp->name;
section.data.assign(name.c_str(), name.c_str() + name.size());
}
// Finally, add function support (that should not have been seen by
// the previous passes).
addFunctions(module);
Expand Down
1 change: 1 addition & 0 deletions src/wasm-binary.h
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ extern const char* Dylink;
extern const char* Linking;
extern const char* Producers;
extern const char* TargetFeatures;
extern const char* Asyncify;

extern const char* AtomicsFeature;
extern const char* BulkMemoryFeature;
Expand Down
1 change: 1 addition & 0 deletions src/wasm/wasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const char* Dylink = "dylink";
const char* Linking = "linking";
const char* Producers = "producers";
const char* TargetFeatures = "target_features";
const char* Asyncify = "asyncify";
const char* AtomicsFeature = "atomics";
const char* BulkMemoryFeature = "bulk-memory";
const char* ExceptionHandlingFeature = "exception-handling";
Expand Down

0 comments on commit ccb4383

Please sign in to comment.