Skip to content

Commit

Permalink
js: change codegen for match statement, speedup string.split_into_lin…
Browse files Browse the repository at this point in the history
…es (#12157)
  • Loading branch information
playXE committed Oct 12, 2021
1 parent cfc56b2 commit 22962dd
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 25 deletions.
17 changes: 5 additions & 12 deletions vlib/builtin/js/string.js.v
Original file line number Diff line number Diff line change
Expand Up @@ -755,18 +755,11 @@ pub fn (s string) split_into_lines() []string {
if s.len == 0 {
return res
}
mut start := 0
mut end := 0
for i := 0; i < s.len; i++ {
if s[i] == 10 {
end = if i > 0 && s[i - 1] == 13 { i - 1 } else { i }
res << if start == end { '' } else { s[start..end] }
start = i + 1
}
}
if start < s.len {
res << s[start..]
}
#res.arr.arr = s.str.split("\n")
#if (res.arr.arr[res.arr.arr.length-1] == "") res.arr.arr.pop();
#res.arr.len = new int(res.arr.arr.length);
#res.arr.cap = new int(res.arr.arr.length);

return res
}

Expand Down
2 changes: 1 addition & 1 deletion vlib/strings/builder_test.js.v
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import strings

type MyInt = int

const maxn = 1000
const maxn = 100000

fn test_sb() {
mut sb := strings.new_builder(100)
Expand Down
63 changes: 51 additions & 12 deletions vlib/v/gen/js/js.v
Original file line number Diff line number Diff line change
Expand Up @@ -1995,42 +1995,79 @@ fn (mut g JsGen) match_expr_classic(node ast.MatchExpr, is_expr bool, cond_var M
}
match type_sym.kind {
.array {
g.write('vEq(')
ptr_typ := g.gen_array_equality_fn(node.cond_type)

g.write('${ptr_typ}_arr_eq(')
g.match_cond(cond_var)
g.write(',')
g.expr(expr)
g.write(')')
g.write(').val')
}
.array_fixed {
g.write('vEq(')
ptr_typ := g.gen_fixed_array_equality_fn(node.cond_type)

g.write('${ptr_typ}_arr_eq(')
g.match_cond(cond_var)
g.write(',')
g.expr(expr)
g.write(')')
g.write(').val')
}
.map {
g.write('vEq(')
ptr_typ := g.gen_map_equality_fn(node.cond_type)

g.write('${ptr_typ}_map_eq(')
g.match_cond(cond_var)
g.write(',')
g.expr(expr)
g.write(')')
g.write(').val')
}
.string {
g.write('vEq(')
g.match_cond(cond_var)
g.write(',')
g.write('.str === ')
g.expr(expr)
g.write(')')
g.write('.str')
}
.struct_ {
g.write('vEq(')
ptr_typ := g.gen_struct_equality_fn(node.cond_type)

g.write('${ptr_typ}_struct_eq(')
g.match_cond(cond_var)
g.write(',')
g.expr(expr)
g.write(').val')
}
.sum_type {
ptr_typ := g.gen_sumtype_equality_fn(node.cond_type)

g.write('${ptr_typ}_sumtype_eq(')
g.match_cond(cond_var)
g.write(',')
g.expr(expr)
g.write(')')
g.write(').val')
}
.alias {
ptr_typ := g.gen_alias_equality_fn(node.cond_type)

g.write('${ptr_typ}_alias_eq(')
g.match_cond(cond_var)
g.write(',')
g.expr(expr)
g.write(').val')
}
else {
if expr is ast.RangeExpr {
has_operator_overloading := g.table.type_has_method(type_sym,
'==')
if has_operator_overloading {
left := g.unwrap(node.cond_type)
g.write(g.typ(left.unaliased.set_nr_muls(0)))
g.write('__eq(')
g.match_cond(cond_var)
g.gen_deref_ptr(node.cond_type)
g.write(',')
g.expr(expr)
g.write(')')
g.write('.valueOf()')
} else if expr is ast.RangeExpr {
// if type is unsigned and low is 0, check is unneeded
mut skip_low := false
if expr.low is ast.IntegerLiteral {
Expand Down Expand Up @@ -2144,6 +2181,7 @@ fn (mut g JsGen) stmts_with_tmp_var(stmts []ast.Stmt, tmp_var string) {
if g.inside_ternary {
g.write('(')
}
prev := g.inside_ternary
for i, stmt in stmts {
if i == stmts.len - 1 && tmp_var != '' {
if g.inside_if_optional {
Expand Down Expand Up @@ -2175,6 +2213,7 @@ fn (mut g JsGen) stmts_with_tmp_var(stmts []ast.Stmt, tmp_var string) {
}
}
g.dec_indent()
g.inside_ternary = prev
if g.inside_ternary {
g.write(')')
}
Expand Down

0 comments on commit 22962dd

Please sign in to comment.