Skip to content

Commit b84512d

Browse files
authored
jsgen: fix direct map key access and map.len (fix #24616, fix #24605) (#24620)
1 parent bb2d605 commit b84512d

3 files changed

Lines changed: 68 additions & 16 deletions

File tree

‎vlib/builtin/js/map.js.v‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ pub:
88

99
fn (mut m map) internal_set(key JS.Any, val JS.Any) {
1010
//$if es5 {
11-
#if ('$toJS' in key) key = key.$toJS();
11+
#if (key.hasOwnProperty('$toJS')) key = key.$toJS();
1212
#if (!(key in m.val.map)) m.val.length++;
1313
#m.val.map[key] = val
1414
/*} $else {
15-
# if ('$toJS' in key) key = key.$toJS();
15+
# if (key.hasOwnProperty('$toJS')) key = key.$toJS();
1616
# m.val.m.set(key,val);
1717
}*/
1818
_ := key
@@ -22,10 +22,10 @@ fn (mut m map) internal_set(key JS.Any, val JS.Any) {
2222
fn (mut m map) internal_get(key JS.Any) JS.Any {
2323
mut val := JS.Any(unsafe { nil })
2424
//$if es5 {
25-
#if (typeof key != "string" && '$toJS' in key) key = key.$toJS();
25+
#if (typeof key != "string" && key.hasOwnProperty('$toJS')) key = key.$toJS();
2626
#val = m.val.map[key]
2727
/*} $else {
28-
# if ('$toJS' in key) key = key.$toJS();
28+
# if (key.hasOwnProperty('$toJS')) key = key.$toJS();
2929
# val = m.val.m.get(key)
3030
}*/
3131
_ := key
@@ -34,11 +34,11 @@ fn (mut m map) internal_get(key JS.Any) JS.Any {
3434

3535
#map.prototype.get = function (key) { return map_internal_get(this,key); }
3636
#map.prototype.set = function(key,val) { map_internal_set(this,key,val); }
37-
#map.prototype.has = function (key) { if (typeof key != "string" && '$toJS' in key) { key = key.$toJS() } return key in this.map; }
37+
#map.prototype.has = function (key) { if (typeof key != "string" && key.hasOwnProperty('$toJS')) { key = key.$toJS() } return key in this.map; }
3838
// Removes the mapping of a particular key from the map.
3939
@[unsafe]
4040
pub fn (mut m map) delete(key JS.Any) {
41-
#let k = '$toJS' in key ? key.$toJS() : key;
41+
#let k = key.hasOwnProperty('$toJS') ? key.$toJS() : key;
4242

4343
#if (delete m.val.map[k]) { m.val.length--; };
4444

‎vlib/v/gen/js/builtin_types.v‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ fn (mut g JsGen) gen_builtin_type_defs() {
494494
typ_name: typ_name
495495
val_name: 'map'
496496
default_value: 'new map({})'
497-
constructor: 'this.map = map; this.length = 0;'
497+
constructor: 'this.map = map; this.length = Object.keys(this.map).length;'
498498
value_of: 'this'
499499
to_string: 'this.map.toString()'
500500
eq: 'new bool(vEq(self, other))'

‎vlib/v/gen/js/tests/map.v‎

Lines changed: 61 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ fn map_any[T](items []T, cb fn (item T) bool) bool {
2424
return false
2525
}
2626

27-
fn test_map_values_method() {
28-
// testing map[int]stirng
27+
fn test_values_method() {
28+
// testing map[int]string
2929
items_1 := {
3030
1: 'item_1'
3131
2: 'item_2'
@@ -84,7 +84,7 @@ fn test_map_values_method() {
8484
})
8585
}
8686

87-
fn test_map_values_method_with_generic_constraints() {
87+
fn test_values_method_with_generic_constraints() {
8888
// test with string constraint
8989
string_map := {
9090
'first': 'hello'
@@ -106,7 +106,17 @@ fn test_map_values_method_with_generic_constraints() {
106106
assert int_result.contains(2)
107107
}
108108

109-
fn test_map_keys_method() {
109+
fn test_keys_method() {
110+
// testing map[int]string keys
111+
items_1 := {
112+
1: 'item_1'
113+
2: 'item_2'
114+
3: 'item_3'
115+
}
116+
for key in items_1.keys() {
117+
assert 'item_${key}' == items_1[key]
118+
}
119+
110120
// testing map[string]int keys
111121
items_2 := {
112122
'item_1': 1
@@ -147,7 +157,7 @@ fn test_map_keys_method() {
147157
assert point_keys.contains('unit_y')
148158
}
149159

150-
fn test_map_keys_method_with_generic_constraints() {
160+
fn test_keys_method_with_generic_constraints() {
151161
// test with string values
152162
string_map := {
153163
'first': 'hello'
@@ -176,9 +186,51 @@ fn test_map_keys_method_with_generic_constraints() {
176186
assert empty_keys.len == 0
177187
}
178188

189+
fn test_direct_map_access() {
190+
// testing map[int]string
191+
items_1 := {
192+
1: 'one'
193+
2: 'two'
194+
3: 'three'
195+
}
196+
assert items_1[1] == 'one'
197+
assert items_1[2] == 'two'
198+
assert items_1[3] == 'three'
199+
200+
// testing map[string]int
201+
items_2 := {
202+
'one': 1
203+
'two': 2
204+
'three': 3
205+
}
206+
assert items_2['one'] == 1
207+
assert items_2['two'] == 2
208+
assert items_2['three'] == 3
209+
}
210+
211+
fn test_map_len() {
212+
// testing on the fly maps
213+
items_1 := {
214+
'one': 1
215+
'two': 2
216+
}
217+
assert items_1.len == 2
218+
219+
// testing empty map length
220+
mut items_2 := map[string]int{}
221+
assert items_2.len == 0
222+
223+
// testing dynamic addition map length
224+
items_2['one'] = 1
225+
items_2['two'] = 2
226+
assert items_2.len == 2
227+
}
228+
179229
fn main() {
180-
test_map_values_method()
181-
test_map_values_method_with_generic_constraints()
182-
test_map_keys_method()
183-
test_map_keys_method_with_generic_constraints()
230+
test_values_method()
231+
test_values_method_with_generic_constraints()
232+
test_keys_method()
233+
test_keys_method_with_generic_constraints()
234+
test_direct_map_access()
235+
test_map_len()
184236
}

0 commit comments

Comments
 (0)