File tree Expand file tree Collapse file tree 2 files changed +54
-1
lines changed Expand file tree Collapse file tree 2 files changed +54
-1
lines changed Original file line number Diff line number Diff line change @@ -37,7 +37,52 @@ pub fn exit(c int) {
37
37
JS.process.exit (c)
38
38
}
39
39
40
+ pub fn unwrap (opt any) any {
41
+ o := & Option (opt)
42
+ if o.not_ok {
43
+ panic (o.error)
44
+ return o.error
45
+ }
46
+ return opt
47
+ }
48
+
40
49
pub fn panic (s string ) {
41
50
eprintln ('V panic: $s ' )
42
51
exit (1 )
43
52
}
53
+
54
+
55
+ struct Option {
56
+ not_ok bool
57
+ is_none bool
58
+ error string
59
+ ecode int
60
+ data any
61
+ }
62
+
63
+ pub fn (o Option) str () string {
64
+ if ! o.not_ok {
65
+ return 'Option{ ok }'
66
+ }
67
+ if o.is_none {
68
+ return 'Option{ none }'
69
+ }
70
+ return 'Option{ error: "${o.error} " }'
71
+ }
72
+
73
+ pub fn error (s string ) Option {
74
+ return Option{
75
+ not_ok: true
76
+ is_none: false
77
+ error: s
78
+ }
79
+ }
80
+
81
+ pub fn error_with_code (s string , code int ) Option {
82
+ return Option{
83
+ not_ok: true
84
+ is_none: false
85
+ error: s
86
+ ecode: code
87
+ }
88
+ }
Original file line number Diff line number Diff line change @@ -1175,6 +1175,10 @@ fn (mut g JsGen) gen_call_expr(it ast.CallExpr) {
1175
1175
} else {
1176
1176
name = g.js_name (it .name)
1177
1177
}
1178
+ call_return_is_optional := it .return_type.has_flag (.optional)
1179
+ if call_return_is_optional {
1180
+ g.write ('builtin.unwrap(' )
1181
+ }
1178
1182
g.expr (it .left)
1179
1183
if it .is_method { // foo.bar.baz()
1180
1184
sym := g.table.get_type_symbol (it .receiver_type)
@@ -1224,7 +1228,11 @@ fn (mut g JsGen) gen_call_expr(it ast.CallExpr) {
1224
1228
g.write (', ' )
1225
1229
}
1226
1230
}
1227
- g.write (')' )
1231
+ if call_return_is_optional {
1232
+ g.write ('))' )
1233
+ } else {
1234
+ g.write (')' )
1235
+ }
1228
1236
}
1229
1237
1230
1238
fn (mut g JsGen) gen_ident (node ast.Ident) {
You can’t perform that action at this time.
0 commit comments