Skip to content

Add initial (binary only) support for compact imports proposal. NFC.#8226

Open
sbc100 wants to merge 1 commit intomainfrom
compact_imports
Open

Add initial (binary only) support for compact imports proposal. NFC.#8226
sbc100 wants to merge 1 commit intomainfrom
compact_imports

Conversation

@sbc100
Copy link
Member

@sbc100 sbc100 commented Jan 22, 2026

See https://github.com/WebAssembly/compact-import-section

This change only adds binary support. Luckily the upstream tests are devided in two seperate
files binary-compact-imports.wast and compact-imports.wast so this change only imports
one of them.

@sbc100 sbc100 force-pushed the compact_imports branch 5 times, most recently from c48fb7e to 9248800 Compare January 23, 2026 21:34
@sbc100 sbc100 requested a review from a team as a code owner March 23, 2026 23:08
@sbc100 sbc100 requested review from stevenfontanella and removed request for a team March 23, 2026 23:08
@sbc100 sbc100 changed the title [WIP] Initial support for compact imports proposal. NFC. Add initial support for compact imports proposal. NFC. Mar 23, 2026
@sbc100 sbc100 requested a review from tlively March 23, 2026 23:12
@sbc100
Copy link
Member Author

sbc100 commented Mar 23, 2026

@tlively or @stevenfontanella how about we land this and I can hand it over to one of your to do the text parse stuff which is beyond my ken.

@sbc100 sbc100 changed the title Add initial support for compact imports proposal. NFC. Add initial (binary only) support for compact imports proposal. NFC. Mar 23, 2026
@sbc100
Copy link
Member Author

sbc100 commented Mar 23, 2026

I just copied the spec test directly from the upstream repo.. is that the right way to do it, or is there some script / submodule type thing for pulling those in these days?

@stevenfontanella
Copy link
Member

I just copied the spec test directly from the upstream repo.. is that the right way to do it, or is there some script / submodule type thing for pulling those in these days?

Looks like it's not in the testsuite repo yet link (I'm not sure when this would happen). Seems ok to me to copy it over for now. Maybe add a TODO to replace it with the one in the testsuite repo once it's there?

how about we land this and I can hand it over to one of your to do the text parse stuff which is beyond my ken.

I'm happy to give it a try!

Comment on lines +1667 to +1672
void addImport(uint32_t kind, std::unique_ptr<Importable> importable);
std::unique_ptr<Importable>
readImportDetails(Name module, Name field, uint32_t kind);
std::unique_ptr<Importable> copyImportable(uint32_t kind,
Importable& details);

Copy link
Member

Choose a reason for hiding this comment

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

Importable doesn't define a virtual destructor, so IIUC we shouldn't put subclasses of Importable in a unique_ptr because deleting it is UB and will leak resources (i.e. this looks like slicing).

Maybe we can just add the virtual destructor? I'm not sure if we didn't add it on purpose to avoid a performance penalty. Or otherwise, maybe we'd want to split these out into different functions for each type of Importable or use a std::variant.

Copy link
Member Author

Choose a reason for hiding this comment

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

But I see stuff like std::unique_ptr<Function> all over the codbase.. so isn't "subclasses of Importable in a unique_ptr" just something that exists already?

Copy link
Member Author

Choose a reason for hiding this comment

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

Or did you mean we shouldn't put Importable itself in a unique_ptr?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, unique_ptr<Function> is fine because it will invoke Function's destructor, but an instance of Function in a unique_ptr<Importable> isn't ok because it will invoke Importable's destructor and leak memory (in particular Function has a vector field which has a non-trivial destructor).

It could work if we give the unique_ptr a custom deleter when constructing it, but I feel it would be better to just split these methods into different ones for functions, tables, etc.

Copy link
Member

Choose a reason for hiding this comment

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

A std::variant might also be an option here (but I don't feel strongly one way or the other).

Copy link
Member Author

Choose a reason for hiding this comment

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

I see. I guess we don't want to add virtual dtors everywhere to allow this kind of thing?

It does seem kind of backwards to create a std::variant to hold bunch of different things that all share a common supertype. Isn't that exactly what a supertype pointer/reference is supposed to be able to do.

@sbc100 sbc100 force-pushed the compact_imports branch 4 times, most recently from 5433c45 to 176e41b Compare March 24, 2026 00:47
}

template<typename T>
std::unique_ptr<Importable> copyImportable(Importable& details) {
Copy link
Member

Choose a reason for hiding this comment

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

A reasonable solution to the dtor issue might be to have this take T& and return std::unique_ptr<T>, then hoist the switch over kind into the parser code so we never deal with std::unique_ptr<Importable> at all.

So far this is binary-only.  We can add the text format support as
a followup I think.

https://github.com/WebAssembly/compact-import-section
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