7
7
capitalize ,
8
8
isVoid ,
9
9
isObject ,
10
- isBytes ,
10
+ write ,
11
11
} from "./helpers" ;
12
12
import { camelCase , formatComment , shouldIncludeHostCall } from "../utils" ;
13
13
@@ -23,7 +23,7 @@ export class HostVisitor extends BaseVisitor {
23
23
if ( context . config . hostPreamble != true ) {
24
24
const className = context . config . hostClassName || "Host" ;
25
25
this . write ( `
26
- import { hostCall } from "@wapc/as-guest";
26
+ import { hostCall, Result } from "@wapc/as-guest";
27
27
export class ${ className } {
28
28
binding: string;
29
29
@@ -40,34 +40,61 @@ export class HostVisitor extends BaseVisitor {
40
40
if ( index > 0 ) {
41
41
this . write ( `, ` ) ;
42
42
}
43
- this . write (
44
- `${ param . name . value } : ${ expandType ( param . type , true , false ) } `
45
- ) ;
43
+ this . write ( `${ param . name . value } : ${ expandType ( param . type , true , false ) } ` ) ;
46
44
} ) ;
47
- this . write ( `): ${ expandType ( operation . type , true , false ) } {\n` ) ;
48
-
49
- this . write ( ` ` ) ;
45
+ const returnType = expandType ( operation . type , true , false ) ;
46
+ this . write ( `): ` ) ;
50
47
const retVoid = isVoid ( operation . type ) ;
48
+ if ( retVoid ) {
49
+ this . write ( `Error | null {\n` ) ;
50
+ } else {
51
+ this . write ( `Result<${ returnType } > {\n` ) ;
52
+ }
51
53
54
+ this . write ( ` ` ) ;
52
55
if ( operation . parameters . length == 0 ) {
53
- if ( ! retVoid ) {
54
- this . write ( `const payload = ` ) ;
55
- }
56
56
this . write (
57
- `hostCall(this.binding, ${ strQuote (
57
+ `const result = hostCall(this.binding, ${ strQuote (
58
58
context . namespace . name . value
59
59
) } , ${ strQuote ( operation . name . value ) } , new ArrayBuffer(0));\n`
60
60
) ;
61
61
} else if ( operation . isUnary ( ) ) {
62
- if ( ! retVoid ) {
63
- this . write ( `const payload = ` ) ;
64
- }
65
- this . write (
66
- `hostCall(this.binding, ${ strQuote (
67
- context . namespace . name . value
68
- ) } , ${ strQuote ( operation . name . value ) } , ${ operation . unaryOp ( ) . name . value
69
- } .toBuffer());\n`
70
- ) ;
62
+ const unaryParam = operation . parameters [ 0 ] ;
63
+ if ( isObject ( unaryParam . type ) ) {
64
+ this . write (
65
+ `const result = hostCall(this.binding, ${ strQuote (
66
+ context . namespace . name . value
67
+ ) } , ${ strQuote ( operation . name . value ) } , ${
68
+ operation . unaryOp ( ) . name . value
69
+ } .toBuffer());\n`
70
+ ) ;
71
+ } else {
72
+ this . write ( `const sizer = new Sizer();
73
+ ${ write (
74
+ "sizer" ,
75
+ "" ,
76
+ "" ,
77
+ unaryParam . name . value ,
78
+ unaryParam . type ,
79
+ false ,
80
+ isReference ( operation . annotations )
81
+ ) } const ua = new ArrayBuffer(sizer.length);
82
+ const encoder = new Encoder(ua);
83
+ ${ write (
84
+ "encoder" ,
85
+ "" ,
86
+ "" ,
87
+ unaryParam . name . value ,
88
+ unaryParam . type ,
89
+ false ,
90
+ isReference ( operation . annotations )
91
+ ) } `) ;
92
+ this . write (
93
+ `const result = hostCall(this.binding, ${ strQuote (
94
+ context . namespace . name . value
95
+ ) } , ${ strQuote ( operation . name . value ) } , ua);\n`
96
+ ) ;
97
+ }
71
98
} else {
72
99
this . write (
73
100
`const inputArgs = new ${ capitalize ( operation . name . value ) } Args();\n`
@@ -76,52 +103,48 @@ export class HostVisitor extends BaseVisitor {
76
103
const paramName = param . name . value ;
77
104
this . write ( ` inputArgs.${ paramName } = ${ paramName } ;\n` ) ;
78
105
} ) ;
79
- if ( ! retVoid ) {
80
- this . write ( `const payload = ` ) ;
81
- }
82
- this . write ( `hostCall(
106
+ this . write ( `const result = hostCall(
83
107
this.binding,
84
108
${ strQuote ( context . namespace . name . value ) } ,
85
109
${ strQuote ( operation . name . value ) } ,
86
110
inputArgs.toBuffer()
87
111
);\n` ) ;
88
112
}
113
+ if ( retVoid ) {
114
+ this . write ( `return result.error()\n` ) ;
115
+ } else {
116
+ this . write ( `if (!result.isOk) {
117
+ return Result.error<${ returnType } >(result.error()!);
118
+ }\n` ) ;
119
+ }
89
120
if ( ! retVoid ) {
90
- if ( isBytes ( operation . type ) ) {
91
- this . write ( ` return payload;\n` ) ;
92
- } else if ( isObject ( operation . type ) ) {
93
- this . write ( ` const decoder = new Decoder(payload);\n` ) ;
121
+ if ( isObject ( operation . type ) ) {
122
+ this . write ( ` const decoder = new Decoder(result.get());\n` ) ;
94
123
this . write (
95
- ` return ${ expandType (
124
+ ` const ret = ${ expandType (
96
125
operation . type ,
97
126
false ,
98
127
isReference ( operation . annotations )
99
128
) } .decode(decoder);\n`
100
129
) ;
101
- } else {
102
- this . write ( ` const decoder = new Decoder(payload);\n` ) ;
103
- var resultVar = "" ;
104
- if ( operation . type instanceof Optional ) {
105
- resultVar = "result" ;
106
- this . write (
107
- `var result: ${ expandType (
108
- operation . type ,
109
- true ,
110
- isReference ( operation . annotations )
111
- ) } ;\n`
112
- ) ;
130
+ this . write ( `if (decoder.error()) {
131
+ return Result.error<${ returnType } >(decoder.error()!)
113
132
}
133
+ return Result.ok(ret);\n` ) ;
134
+ } else {
135
+ this . write ( ` const decoder = new Decoder(result.get());\n` ) ;
114
136
this . write (
115
- `${ read (
116
- resultVar ,
137
+ `const ${ read (
138
+ "payload" ,
117
139
operation . type ,
118
140
false ,
119
141
isReference ( operation . annotations )
120
142
) } `
121
143
) ;
122
- if ( resultVar != "" ) {
123
- this . write ( ` return ${ resultVar } ;\n` ) ;
124
- }
144
+ this . write ( `if (decoder.error()) {
145
+ return Result.error<${ returnType } >(decoder.error()!)
146
+ }\n` ) ;
147
+ this . write ( ` return Result.ok(payload);\n` ) ;
125
148
}
126
149
}
127
150
this . write ( ` }\n` ) ;
0 commit comments