Skip to content

Commit 8320da2

Browse files
authored
jsgen: fix map to string fails on rune keys (fix #24637) (#24638)
1 parent bfc6d54 commit 8320da2

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

vlib/v/gen/js/builtin_types.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ fn (mut g JsGen) gen_builtin_type_defs() {
456456
value_of: 'this.val | 0'
457457
to_string: 'new string(this.val + "")'
458458
eq: 'new bool(self.valueOf() === other.valueOf())'
459-
to_jsval: '+this'
459+
to_jsval: 'String.fromCharCode(+this)'
460460
)
461461
}
462462
'f32', 'f64', 'float_literal' {

vlib/v/gen/js/js.v

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -900,11 +900,7 @@ fn (mut g JsGen) expr(node_ ast.Expr) {
900900
// TODO
901901
}
902902
ast.CharLiteral {
903-
if !node.val.is_pure_ascii() {
904-
g.write("new rune('${node.val}'.charCodeAt())")
905-
} else {
906-
g.write("new u8('${node.val}')")
907-
}
903+
g.write("new rune('${node.val}')")
908904
}
909905
ast.Comment {}
910906
ast.ComptimeCall {
@@ -1712,8 +1708,12 @@ fn (mut g JsGen) gen_for_in_stmt(it ast.ForInStmt) {
17121708

17131709
g.inc_indent()
17141710
g.writeln('let ${val} = ${tmp}.map[${tmp2}];')
1715-
g.writeln('let ${key} = ${tmp2};')
1716-
1711+
sym := g.table.sym(it.key_type)
1712+
if sym.is_number() {
1713+
g.writeln('let ${key} = new ${g.styp(it.key_type)}(+${tmp2})')
1714+
} else {
1715+
g.writeln('let ${key} = new ${g.styp(it.key_type)}(${tmp2})')
1716+
}
17171717
g.writeln('try { ')
17181718
g.inc_indent()
17191719
g.stmts(it.stmts)

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,11 +226,25 @@ fn test_map_len() {
226226
assert items_2.len == 2
227227
}
228228

229+
fn test_rune_keys() {
230+
mut m := {
231+
`!`: 2
232+
`%`: 3
233+
}
234+
assert typeof(m).name == 'map[rune]int'
235+
assert m[`!`] == 2
236+
m[`@`] = 7
237+
assert m.len == 3
238+
println(m)
239+
assert '${m}' == '{`!`: 2, `%`: 3, `@`: 7}'
240+
}
241+
229242
fn main() {
230243
test_values_method()
231244
test_values_method_with_generic_constraints()
232245
test_keys_method()
233246
test_keys_method_with_generic_constraints()
234247
test_direct_map_access()
235248
test_map_len()
249+
test_rune_keys()
236250
}

0 commit comments

Comments
 (0)