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

Handle each kind of module field separately in preprocessor #5

Merged
merged 24 commits into from Jun 11, 2023

Conversation

tomstuart
Copy link
Owner

Different WebAssembly module fields can use different abbreviations, so the preprocessor ultimately needs to treat each kind of field separately. As mentioned in 4caaf54, the work of expanding inline imports & exports currently requires looking at the entire field at once and therefore prevents us from first identifying the field and then consuming it incrementally with peek and read as is done elsewhere.

This PR refactors the preprocessor’s #process_field method to do nothing except peek at the field’s first atom and call the appropriate method for handling that kind of field. This regularises the preprocessor’s implementation and will make it easier to extend the individual field-specific methods to support whatever abbreviations they need.

This does mean we have to temporarily match against the entire
S-expression instead of peeking at it, but that’ll go away once we start
consuming it incrementally. For now the priority is to get #read_list
lifted out of #process_field so that it’s operating entirely inside a
list like all the other methods do.
We were only wrapping `field` in a single-element array because it was
being passed to the #read_list helper inside #process_field, but now the
helper’s moved next to the array, the two cancel each other out.
Although this is slightly less efficient, it more directly resembles the
specification of export abbreviations (e.g. [0]) and so should be easier
to understand at a glance.

[0] https://webassembly.github.io/spec/core/text/modules.html#text-global-abbrev
Just as in #expand_inline_export, we can use #process_command to desugar
the result of expanding the module abbreviation [0].

[0] https://webassembly.github.io/spec/core/text/modules.html#id10
This makes it possible for each method to expand inline exports, which
produce multiple fields. In practice it’s only #process_function that
needs to do this, but I’ve made the same change to #process_type
and #process_import to keep all of the field-processing methods
consistent.
The spec calls these fields “function definitions” [0] and “type
definitions” [1], so we should use the same terminology here.

[0] https://webassembly.github.io/spec/core/text/modules.html#functions
[1] https://webassembly.github.io/spec/core/text/modules.html#types
This removes the need to support inline imports & exports for functions
in #process_field.
It’s handled in #process_function_definition now.
…itions

This removes the need to support inline imports & exports for any kind
of field in #process_field.
It’s entirely handled in field-specific methods now.
They’re called by both #process_function_definition
and #process_table_memory_global_definition, and the code is easier to
read if callers come before callees.
…xport

We really want to get the optional ID out of the way, but we have to
read the field kind first.
We need to do the same work to handle inline imports & exports for
function and table/memory/global definitions, so it’s convenient to move
the duplicated code into helpers.
We’d like to lift the #read out of this method, so we can’t use it
directly in a conditional. Fortunately we can check its side-effect (of
assigning to `id`) instead.
Rather than doing the reads inline when they’re needed, we read
everything together first and then build the result. The increased
similarity to #expand_inline_import makes the methods easier to compare
at a glance.
We now only need to look at the next thing we’ll read, not the entire
S-expression.
Table, memory and global definitions support different abbreviations, so
each will need its own slightly different method.
@tomstuart tomstuart merged commit 242b653 into main Jun 11, 2023
2 checks passed
@tomstuart tomstuart deleted the process-fields-separately branch June 11, 2023 16:07
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.

None yet

1 participant