@@ -1079,6 +1079,7 @@ fn (mut g JsGen) gen_for_c_stmt(it ast.ForCStmt) {
1079
1079
g.write ('; ' )
1080
1080
}
1081
1081
if it .has_cond {
1082
+ g.write ('+' ) // convert to number or boolean
1082
1083
g.expr (it .cond)
1083
1084
}
1084
1085
g.write ('; ' )
@@ -1174,6 +1175,7 @@ fn (mut g JsGen) gen_for_stmt(it ast.ForStmt) {
1174
1175
if it .is_inf {
1175
1176
g.write ('true' )
1176
1177
} else {
1178
+ g.write ('+' ) // convert expr to number or boolean
1177
1179
g.expr (it .cond)
1178
1180
}
1179
1181
g.writeln (') {' )
@@ -1325,6 +1327,7 @@ fn (mut g JsGen) gen_array_init_expr(it ast.ArrayInit) {
1325
1327
// offering similar performance
1326
1328
g.write ('new array(' )
1327
1329
g.inc_indent ()
1330
+
1328
1331
if it .has_len {
1329
1332
t1 := g.new_tmp_var ()
1330
1333
t2 := g.new_tmp_var ()
@@ -1349,6 +1352,31 @@ fn (mut g JsGen) gen_array_init_expr(it ast.ArrayInit) {
1349
1352
g.writeln ('return $t1 ;' )
1350
1353
g.dec_indent ()
1351
1354
g.write ('})()' )
1355
+ } else if it .is_fixed && it .exprs.len == 1 {
1356
+ // [100]byte codegen
1357
+ t1 := g.new_tmp_var ()
1358
+ t2 := g.new_tmp_var ()
1359
+ g.writeln ('(function() {' )
1360
+ g.inc_indent ()
1361
+ g.writeln ('const $t1 = [];' )
1362
+ g.write ('for (let $t2 = 0; $t2 < ' )
1363
+ g.expr (it .exprs[0 ])
1364
+ g.writeln ('; $t2 ++) {' )
1365
+ g.inc_indent ()
1366
+ g.write ('${t1} .push(' )
1367
+ if it .has_default {
1368
+ g.expr (it .default_expr)
1369
+ } else {
1370
+ // Fill the array with the default values for its type
1371
+ t := g.to_js_typ_val (it .elem_type)
1372
+ g.write (t)
1373
+ }
1374
+ g.writeln (');' )
1375
+ g.dec_indent ()
1376
+ g.writeln ('};' )
1377
+ g.writeln ('return $t1 ;' )
1378
+ g.dec_indent ()
1379
+ g.write ('})()' )
1352
1380
} else {
1353
1381
g.gen_array_init_values (it .exprs)
1354
1382
}
@@ -1549,7 +1577,9 @@ fn (mut g JsGen) gen_if_expr(node ast.IfExpr) {
1549
1577
g.write (' : ' )
1550
1578
}
1551
1579
if i < node.branches.len - 1 || ! node.has_else {
1580
+ g.write ('(' )
1552
1581
g.expr (branch.cond)
1582
+ g.write (')' )
1553
1583
g.write ('.valueOf()' )
1554
1584
g.write (' ? ' )
1555
1585
}
@@ -1570,15 +1600,19 @@ fn (mut g JsGen) gen_if_expr(node ast.IfExpr) {
1570
1600
if '$branch.cond ' == 'js' {
1571
1601
g.write ('true' )
1572
1602
} else {
1603
+ g.write ('(' )
1573
1604
g.expr (branch.cond)
1605
+ g.write (')' )
1574
1606
g.write ('.valueOf()' )
1575
1607
}
1576
1608
g.writeln (') {' )
1577
1609
}
1578
1610
}
1579
1611
} else if i < node.branches.len - 1 || ! node.has_else {
1580
1612
g.write ('} else if (' )
1613
+ g.write ('(' )
1581
1614
g.expr (branch.cond)
1615
+ g.write (')' )
1582
1616
g.write ('.valueOf()' )
1583
1617
g.writeln (') {' )
1584
1618
} else if i == node.branches.len - 1 && node.has_else {
@@ -1608,7 +1642,7 @@ fn (mut g JsGen) gen_index_expr(expr ast.IndexExpr) {
1608
1642
if expr.index is ast.RangeExpr {
1609
1643
g.expr (expr.left)
1610
1644
if expr.left_type.is_ptr () {
1611
- g.write ('.val ' )
1645
+ g.write ('.valueOf() ' )
1612
1646
}
1613
1647
g.write ('.slice(' )
1614
1648
if expr.index.has_low {
@@ -1622,7 +1656,7 @@ fn (mut g JsGen) gen_index_expr(expr ast.IndexExpr) {
1622
1656
} else {
1623
1657
g.expr (expr.left)
1624
1658
if expr.left_type.is_ptr () {
1625
- g.write ('.val ' )
1659
+ g.write ('.valueOf() ' )
1626
1660
}
1627
1661
g.write ('.length' )
1628
1662
}
@@ -1649,7 +1683,7 @@ fn (mut g JsGen) gen_index_expr(expr ast.IndexExpr) {
1649
1683
g.write ('new byte(' )
1650
1684
g.expr (expr.left)
1651
1685
if expr.left_type.is_ptr () {
1652
- g.write ('.val ' )
1686
+ g.write ('.valueOf() ' )
1653
1687
}
1654
1688
g.write ('.str.charCodeAt(' )
1655
1689
g.expr (expr.index)
@@ -1659,10 +1693,10 @@ fn (mut g JsGen) gen_index_expr(expr ast.IndexExpr) {
1659
1693
// TODO Does this cover all cases?
1660
1694
g.expr (expr.left)
1661
1695
if expr.left_type.is_ptr () {
1662
- g.write ('.val ' )
1696
+ g.write ('.valueOf() ' )
1663
1697
}
1664
1698
g.write ('.arr' )
1665
- g.write ('[' )
1699
+ g.write ('[+ ' )
1666
1700
g.cast_stack << ast.int_type_idx
1667
1701
g.expr (expr.index)
1668
1702
g.cast_stack.delete_last ()
@@ -1682,7 +1716,10 @@ fn (mut g JsGen) gen_infix_expr(it ast.InfixExpr) {
1682
1716
if it .op == .eq || it .op == .ne {
1683
1717
// Shallow equatables
1684
1718
if l_sym.kind in js.shallow_equatables && r_sym.kind in js.shallow_equatables {
1719
+ // wrap left expr in parens so binary operations will work correctly.
1720
+ g.write ('(' )
1685
1721
g.expr (it .left)
1722
+ g.write (')' )
1686
1723
g.write ('.eq(' )
1687
1724
g.cast_stack << int (l_sym.kind)
1688
1725
g.expr (it .right)
@@ -1713,7 +1750,7 @@ fn (mut g JsGen) gen_infix_expr(it ast.InfixExpr) {
1713
1750
} else if r_sym.kind == .string {
1714
1751
g.write ('.str.includes(' )
1715
1752
} else {
1716
- g.write ('.arr. includes(' )
1753
+ g.write ('.\$ includes(' )
1717
1754
}
1718
1755
g.expr (it .left)
1719
1756
if l_sym.kind == .string {
@@ -1728,14 +1765,15 @@ fn (mut g JsGen) gen_infix_expr(it ast.InfixExpr) {
1728
1765
is_arithmetic := it .op in [token.Kind.plus, .minus, .mul, .div, .mod]
1729
1766
mut needs_cast := is_arithmetic && it .left_type != it .right_type
1730
1767
mut greater_typ := 0
1731
- if needs_cast {
1768
+ // todo(playX): looks like this cast is always required to perform .eq operation on types.
1769
+ if true || needs_cast {
1732
1770
greater_typ = g.greater_typ (it .left_type, it .right_type)
1733
1771
if g.cast_stack.len > 0 {
1734
1772
needs_cast = g.cast_stack.last () != greater_typ
1735
1773
}
1736
1774
}
1737
1775
1738
- if needs_cast {
1776
+ if true || needs_cast {
1739
1777
if g.ns.name == 'builtin' {
1740
1778
g.write ('new ' )
1741
1779
}
@@ -1746,7 +1784,7 @@ fn (mut g JsGen) gen_infix_expr(it ast.InfixExpr) {
1746
1784
g.write (' $it.op ' )
1747
1785
g.expr (it .right)
1748
1786
1749
- if needs_cast {
1787
+ if true || needs_cast {
1750
1788
g.cast_stack.delete_last ()
1751
1789
g.write (')' )
1752
1790
}
0 commit comments