-
-
Notifications
You must be signed in to change notification settings - Fork 682
feat: Map support constructor initialization with MapInitialEntries #2941
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
feat: Map support constructor initialization with MapInitialEntries #2941
Conversation
- Added new type alias `MapInitialEntries<K, V>` = { key: K, value: V }[]
- Map constructor now accepts:
• An array of { key, value } objects
• Any array typed as MapInitialEntries<K,V>
- This allows populating a Map in one step with strong typing.
- Example:
let myMap = new Map<string, i32>([
{ key: "a", value: 1 },
{ key: "b", value: 2 },
]);
let init: MapInitialEntries<string, i32> = [
{ key: "x", value: 10 },
{ key: "y", value: 20 },
];
let anotherMap = new Map<string, i32>(init);
std/assembly/map.ts
Outdated
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.
pebrianz
commented
Aug 23, 2025
the test rt/flags failed to compile, when pass type v128 to the map. it need simd to be enabled. Do you have any suggestion?
For update all fixtures, try to run ASC_FEATURES="*" && npm run test:compiler -- --create
@CountBleck
CountBleck
left a comment
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.
This seems acceptable, since we don't have tuples or iterables/iterators. (If/when we do add support for tuples, we would replace this mechanism as a breaking change.) I don't know if we should be concerned about the size increase in symbol.release.wat.
See additional comments below.
std/assembly/map.ts
Outdated
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.
This should be nullable, and the default argument should be null instead of []. Tip: if you wrap the entire constructor body with if (initialEntries) {, you can avoid doing initialEntries!.
(I wonder if this change could be beneficial for code that never uses this feature, by making it easier for Binaryen to optimize)
std/assembly/map.ts
Outdated
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.
plz revert this cached entriesLength var and use instead initialEntries.length
std/assembly/map.ts
Outdated
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.
plz cache initialEntries.length into var
std/assembly/map.ts
Outdated
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.
Just use early return:
This will reduce one level of ident
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.
My bad for suggesting that
...nd use cache initialEntries length
This PR has been automatically marked as stale because it has not had recent activity. It will be closed in one week if no further activity occurs. Thank you for your contributions!
This PR has been automatically closed due to lack of recent activity, but feel free to reopen it as long as you merge in the main branch afterwards.
CountBleck
commented
Dec 29, 2025
Just gonna keep this open so it doesn't get buried too quickly
Uh oh!
There was an error while loading. Please reload this page.
MapInitialEntries<K, V>= { key: K, value: V }[]• An array of { key, value } objects
• Any array typed as MapInitialEntries<K,V>
Fixes #2940