-
Notifications
You must be signed in to change notification settings - Fork 267
feat(cpp): add map type support #1590
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
Open
yordis
wants to merge
9
commits into
bytecodealliance:main
Choose a base branch
from
yordis:yordis/feat-cpp-map-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+222
−21
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
92ed57a
feat(cpp): add map type support
yordis 44cad6d
fix(cpp): add operator< to wit::string for std::map key support and f…
yordis e3697ba
fix(cpp): const_cast map keys during lowering for ownership transfer
yordis 3c38452
fix(cpp): copy map keys by value instead of const_cast during lowering
yordis 235eb9e
fix(cpp): remove map runtime test until wasi-sdk supports map types
yordis 33bb552
refactor(cpp): use wit::map instead of std::map for map types
yordis 57c0514
refactor(cpp): drop redundant size_t cast in MapLower
yordis 773f612
refactor(cpp): skip guest-dealloc loop when body is empty
yordis fee6876
refactor(cpp): use static_cast for MapLower realloc result
yordis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ | |
| #include <stdlib.h> // free | ||
| #include <new> | ||
| #include <span> | ||
| #include <utility> // pair | ||
|
|
||
| namespace wit { | ||
| /// @brief Helper class to map between IDs and resources | ||
|
|
@@ -170,6 +171,70 @@ template <class T> class vector { | |
| } | ||
| }; | ||
|
|
||
| /// @brief A map stored as a contiguous array of key-value pairs in linear | ||
| /// memory, freed unconditionally using free. | ||
| /// | ||
| /// Mirrors the canonical ABI representation of `map<K, V>` (`list<tuple<K, V>>`) | ||
| /// to enable lift without per-entry tree allocation. Unlike `std::map` this | ||
| /// container provides flat iteration only — callers who need keyed lookup can | ||
| /// build their own index from `get_view()`. | ||
| template <class K, class V> class map { | ||
| public: | ||
| using entry_type = std::pair<K, V>; | ||
|
|
||
| private: | ||
| entry_type *data_; | ||
| size_t length; | ||
|
|
||
| static entry_type* empty_ptr() { return (entry_type*)alignof(entry_type); } | ||
|
|
||
| public: | ||
| map(map const &) = delete; | ||
| map(map &&b) : data_(b.data_), length(b.length) { b.data_ = nullptr; } | ||
| map &operator=(map const &) = delete; | ||
| map &operator=(map &&b) { | ||
| if (data_ && length > 0) { | ||
| for (unsigned i = 0; i < length; ++i) { data_[i].~entry_type(); } | ||
| free(data_); | ||
| } | ||
| data_ = b.data_; | ||
| length = b.length; | ||
| b.data_ = nullptr; | ||
| return *this; | ||
| } | ||
| map(entry_type *d, size_t l) : data_(d), length(l) {} | ||
| map() : data_(empty_ptr()), length() {} | ||
| entry_type const *data() const { return data_; } | ||
| entry_type *data() { return data_; } | ||
| entry_type &operator[](size_t n) { return data_[n]; } | ||
| entry_type const &operator[](size_t n) const { return data_[n]; } | ||
| size_t size() const { return length; } | ||
| bool empty() const { return !length; } | ||
| ~map() { | ||
| if (data_ && length > 0) { | ||
| for (unsigned i = 0; i < length; ++i) { data_[i].~entry_type(); } | ||
| free((void*)data_); | ||
| } | ||
| } | ||
| // WARNING: map contains uninitialized entries; caller must construct them via | ||
| // `initialize` before the map is observed or destroyed. | ||
| static map<K, V> allocate(size_t len) { | ||
| if (!len) return map<K, V>(empty_ptr(), 0); | ||
| return map<K, V>((entry_type*)malloc(sizeof(entry_type) * len), len); | ||
| } | ||
| void initialize(size_t n, entry_type&& entry) { | ||
| new ((void*)(data_ + n)) entry_type(std::move(entry)); | ||
| } | ||
| entry_type* leak() { entry_type* result = data_; data_ = nullptr; return result; } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very long line, can you run clang-format on this? |
||
| static void drop_raw(void *ptr) { if (ptr != empty_ptr()) free(ptr); } | ||
| std::span<entry_type> get_view() const { return std::span<entry_type>(data_, length); } | ||
| std::span<entry_type const> get_const_view() const { return std::span<entry_type const>(data_, length); } | ||
| entry_type* begin() { return data_; } | ||
| entry_type* end() { return data_ + length; } | ||
| entry_type const* begin() const { return data_; } | ||
| entry_type const* end() const { return data_ + length; } | ||
| }; | ||
|
|
||
| /// @brief A Resource defined within the guest (guest side) | ||
| /// | ||
| /// It registers with the host and should remain in a static location. | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could zero the length as well