v2 / vlib / v / gen / js / str.v
159 lines · 148 sloc · 3.9 KB · da93f26fa66bcadfc15f6033d1acd1fce2d73525
Raw
1module js
2
3import v.ast
4
5fn escape_template_literal_value(s string) string {
6 return s.replace('\\', '\\\\').replace('`', '\\`').replace('$' + '{', '\\' + '$' + '{')
7}
8
9/*
10fn (mut g JsGen) gen_expr_to_string(expr ast.Expr, etype ast.Type) {
11 is_shared := etype.has_flag(.shared_f)
12 mut typ := etype
13 if is_shared {
14 typ = typ.clear_flag(.shared_f).set_nr_muls(0)
15 }
16
17 mut sym := g.table.sym(typ)
18 // when type is alias, print the aliased value
19 if mut sym.info is ast.Alias {
20 parent_sym := g.table.sym(sym.info.parent_type)
21 if parent_sym.has_method('str') {
22 typ = sym.info.parent_type
23 sym = parent_sym
24 }
25 }
26 sym_has_str_method, str_method_expects_ptr, _ := sym.str_method_info()
27 is_var_mut := expr.is_auto_deref_var()
28 if typ.has_flag(.variadic) {
29 str_fn_name := g.gen_str_for_type(typ)
30 g.write('${str_fn_name}(')
31 g.expr(expr)
32 g.write(')')
33 } else if typ == ast.string_type {
34 g.expr(expr)
35 } else if typ == ast.bool_type {
36 g.write('new string((')
37 g.expr(expr)
38 g.write(').valueOf()? "true" : "false")')
39 } else if sym.kind == .none {
40 g.write('new string("<none>")')
41 } else if sym.kind == .enum {
42 if expr !is ast.EnumVal {
43 str_fn_name := g.gen_str_for_type(typ)
44 g.write('${str_fn_name}(')
45 g.expr(expr)
46 g.write(')')
47 } else {
48 g.write('new string("')
49 g.expr(expr)
50 g.write('")')
51 }
52 } else if sym_has_str_method
53 || sym.kind in [.array, .array_fixed, .map, .struct, .multi_return, .sum_type, .interface] {
54 is_ptr := typ.is_ptr()
55 str_fn_name := g.gen_str_for_type(typ)
56 g.write('${str_fn_name}(')
57 if str_method_expects_ptr && !is_ptr {
58 g.write('new \$ref(')
59 g.expr(expr)
60 g.write(')')
61 } else if (!str_method_expects_ptr && is_ptr && !is_shared) || is_var_mut {
62 g.expr(expr)
63 g.gen_deref_ptr(etype)
64 }
65
66 g.expr(expr)
67 g.write(')')
68 } else {
69 str_fn_name := g.gen_str_for_type(typ)
70 g.write('${str_fn_name}(')
71
72 if sym.kind != .function {
73 g.expr(expr)
74 if expr.is_auto_deref_var() {
75 g.write('.val')
76 }
77 }
78 g.write(')')
79 }
80}*/
81
82fn (mut g JsGen) gen_expr_to_string(expr ast.Expr, etype ast.Type) {
83 is_shared := etype.has_flag(.shared_f)
84 mut typ := etype
85 if is_shared {
86 typ = typ.clear_flag(.shared_f).set_nr_muls(0)
87 }
88 mut sym := g.table.sym(typ)
89 // when type is alias, print the aliased value
90 if mut sym.info is ast.Alias {
91 parent_sym := g.table.sym(sym.info.parent_type)
92 if parent_sym.has_method('str') {
93 typ = sym.info.parent_type
94 sym = unsafe { parent_sym }
95 }
96 }
97 sym_has_str_method, str_method_expects_ptr, _ := sym.str_method_info()
98 if typ.has_flag(.variadic) {
99 str_fn_name := g.get_str_fn(typ)
100 g.write('${str_fn_name}(')
101 g.expr(expr)
102 g.write(')')
103 } else if typ == ast.string_type {
104 g.expr(expr)
105 } else if typ == ast.bool_type {
106 g.expr(expr)
107 g.write('.valueOf()? new string("true") : new string("false")')
108 } else if sym.kind == .none {
109 g.write('new string("<none>")')
110 } else if sym.kind == .enum {
111 if expr !is ast.EnumVal {
112 str_fn_name := g.get_str_fn(typ)
113 g.write('${str_fn_name}(')
114 g.expr(expr)
115 g.write(')')
116 } else {
117 g.write('new string("')
118 g.expr(expr)
119 g.write('")')
120 }
121 } else if sym_has_str_method
122 || sym.kind in [.array, .array_fixed, .map, .struct, .multi_return, .sum_type, .interface] {
123 is_ptr := typ.is_ptr()
124 is_var_mut := expr.is_auto_deref_var()
125 str_fn_name := g.get_str_fn(typ)
126 g.write('${str_fn_name}(')
127
128 if str_method_expects_ptr && !is_ptr {
129 g.write('new \$ref(')
130 }
131 if typ == ast.u32_type && expr is ast.CastExpr {
132 g.write('new u32(')
133 }
134
135 g.expr(expr)
136
137 if typ == ast.u32_type && expr is ast.CastExpr {
138 g.write(')')
139 }
140 if (!str_method_expects_ptr && is_ptr && !is_shared) || is_var_mut {
141 g.write('.val')
142 }
143 g.write(')')
144 if str_method_expects_ptr && !is_ptr {
145 g.write(')')
146 }
147 } else {
148 str_fn_name := g.get_str_fn(typ)
149 g.write('${str_fn_name}(')
150
151 if sym.kind != .function {
152 g.expr(expr)
153 }
154 if expr.is_auto_deref_var() {
155 g.write('.val')
156 }
157 g.write(')')
158 }
159}
160