Skip to content

jsgen: fix maps are always constructed using string keys (fix #24607 #24671)#24673

Merged
spytheman merged 2 commits into
vlang:masterfrom
gchumillas:fix_maps_are_always_constructed_using_string_keys
Jun 9, 2025
Merged

jsgen: fix maps are always constructed using string keys (fix #24607 #24671)#24673
spytheman merged 2 commits into
vlang:masterfrom
gchumillas:fix_maps_are_always_constructed_using_string_keys

Conversation

@gchumillas

@gchumillas gchumillas commented Jun 8, 2025

Copy link
Copy Markdown
Contributor

Fixes #24607 and #24671

The original problem is that map keys do not preserve the prototype methods of the original key values. The trick is to store the original key values, along with their prototype, so the following V code:

m := {
	1: 'one'
	2: 'two'
	3: 'three'
}

Is translated to this JS code:

const map = {
	"map": {
		// the `key` property preserves the original key value, along with its prototype
		"1": { "val": { "str": "one" }, "key": { "val": 1 } },
		"2": { "val": { "str": "two" }, "key": { "val": 2 } },
		"3": { "val": { "str": "three" }, "key": { "val": 3 } }
	},
	"length": 3
};

Later, we can then use the "key values" for other purposes, such as returning the keys from the map.

@huly-for-github

Copy link
Copy Markdown

Connected to Huly®: V_0.6-23030

Comment thread vlib/v/gen/js/tests/map.v

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please add new test functions, instead of modifying the existing ones, unless there is a bug. That makes reviewing a lot easier.

@spytheman spytheman left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Excellent work.

@spytheman spytheman merged commit 2604fc1 into vlang:master Jun 9, 2025
74 checks passed
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.

jsgen: maps are always constructed using string keys

2 participants