Skip to content
This repository was archived by the owner on Sep 15, 2022. It is now read-only.

Commit 0d2543c

Browse files
committed
Support for latest @wapc/as-guest
1 parent fa08784 commit 0d2543c

File tree

6 files changed

+156
-89
lines changed

6 files changed

+156
-89
lines changed

src/assemblyscript/constant.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export const primitives = new Set([
1616
"f32",
1717
"f64",
1818
"string",
19+
"bytes",
1920
]);
2021

2122
export const decodeFuncs = new Map<string, string>([

src/assemblyscript/handlers_visitor.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
mapArgs,
77
mapArg,
88
capitalize,
9+
isVoid,
910
} from "./helpers";
1011
import { formatComment, shouldIncludeHandler } from "../utils";
1112

@@ -35,11 +36,17 @@ export class HandlersVisitor extends BaseVisitor {
3536
} else {
3637
opVal += mapArgs(operation.parameters);
3738
}
38-
opVal += `) => ${expandType(
39-
operation.type,
40-
true,
41-
isReference(operation.annotations)
42-
)}): void {\n`;
39+
opVal += `) => `;
40+
if (isVoid(operation.type)) {
41+
opVal += `Error | null`;
42+
} else {
43+
opVal += `Result<${expandType(
44+
operation.type,
45+
true,
46+
isReference(operation.annotations)
47+
)}>`;
48+
}
49+
opVal += `): void {\n`;
4350
opVal += `${operation.name.value}Handler = handler;\n`;
4451
opVal += `register(${strQuote(operation.name.value)}, ${
4552
operation.name.value

src/assemblyscript/host_visitor.ts

Lines changed: 70 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
capitalize,
88
isVoid,
99
isObject,
10-
isBytes,
10+
write,
1111
} from "./helpers";
1212
import { camelCase, formatComment, shouldIncludeHostCall } from "../utils";
1313

@@ -23,7 +23,7 @@ export class HostVisitor extends BaseVisitor {
2323
if (context.config.hostPreamble != true) {
2424
const className = context.config.hostClassName || "Host";
2525
this.write(`
26-
import { hostCall } from "@wapc/as-guest";
26+
import { hostCall, Result } from "@wapc/as-guest";
2727
export class ${className} {
2828
binding: string;
2929
@@ -40,34 +40,61 @@ export class HostVisitor extends BaseVisitor {
4040
if (index > 0) {
4141
this.write(`, `);
4242
}
43-
this.write(
44-
`${param.name.value}: ${expandType(param.type, true, false)}`
45-
);
43+
this.write(`${param.name.value}: ${expandType(param.type, true, false)}`);
4644
});
47-
this.write(`): ${expandType(operation.type, true, false)} {\n`);
48-
49-
this.write(` `);
45+
const returnType = expandType(operation.type, true, false);
46+
this.write(`): `);
5047
const retVoid = isVoid(operation.type);
48+
if (retVoid) {
49+
this.write(`Error | null {\n`);
50+
} else {
51+
this.write(`Result<${returnType}> {\n`);
52+
}
5153

54+
this.write(` `);
5255
if (operation.parameters.length == 0) {
53-
if (!retVoid) {
54-
this.write(`const payload = `);
55-
}
5656
this.write(
57-
`hostCall(this.binding, ${strQuote(
57+
`const result = hostCall(this.binding, ${strQuote(
5858
context.namespace.name.value
5959
)}, ${strQuote(operation.name.value)}, new ArrayBuffer(0));\n`
6060
);
6161
} 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+
}
7198
} else {
7299
this.write(
73100
`const inputArgs = new ${capitalize(operation.name.value)}Args();\n`
@@ -76,52 +103,48 @@ export class HostVisitor extends BaseVisitor {
76103
const paramName = param.name.value;
77104
this.write(` inputArgs.${paramName} = ${paramName};\n`);
78105
});
79-
if (!retVoid) {
80-
this.write(`const payload = `);
81-
}
82-
this.write(`hostCall(
106+
this.write(`const result = hostCall(
83107
this.binding,
84108
${strQuote(context.namespace.name.value)},
85109
${strQuote(operation.name.value)},
86110
inputArgs.toBuffer()
87111
);\n`);
88112
}
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+
}
89120
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`);
94123
this.write(
95-
` return ${expandType(
124+
` const ret = ${expandType(
96125
operation.type,
97126
false,
98127
isReference(operation.annotations)
99128
)}.decode(decoder);\n`
100129
);
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()!)
113132
}
133+
return Result.ok(ret);\n`);
134+
} else {
135+
this.write(` const decoder = new Decoder(result.get());\n`);
114136
this.write(
115-
`${read(
116-
resultVar,
137+
`const ${read(
138+
"payload",
117139
operation.type,
118140
false,
119141
isReference(operation.annotations)
120142
)}`
121143
);
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`);
125148
}
126149
}
127150
this.write(` }\n`);

src/assemblyscript/scaffold_visitor.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export class ScaffoldVisitor extends BaseVisitor {
1818
super.visitDocumentBefore(context);
1919
const typesVisitor = new TypesVisitor(this.writer);
2020
context.document?.accept(context, typesVisitor);
21+
this.write(`import { Result } from "@wapc/as-guest";\n\n`);
2122
}
2223

2324
visitAllOperationsBefore(context: Context): void {
@@ -31,20 +32,28 @@ export class ScaffoldVisitor extends BaseVisitor {
3132
}
3233
const operation = context.operation!;
3334
this.write(`\n`);
35+
const expanded = expandType(
36+
operation.type,
37+
true,
38+
isReference(operation.annotations)
39+
);
3440
this.write(
3541
`function ${operation.name.value}(${mapArgs(
3642
operation.parameters
37-
)}): ${expandType(
38-
operation.type,
39-
true,
40-
isReference(operation.annotations)
41-
)} {\n`
43+
)}):`
4244
);
45+
if (isVoid(operation.type)) {
46+
this.write(`Error | null\n`);
47+
} else {
48+
this.write(`Result<${expanded}>`)
49+
}
50+
this.write(` {\n`);
4351
if (!isVoid(operation.type)) {
4452
const dv = defaultValueForType(operation.type);
45-
this.write(` return ${dv};`);
53+
this.write(` return Result.error<${expanded}>(new Error("not implemented"));\n`);
54+
} else {
55+
this.write(`return null\n`);
4656
}
47-
this.write(`// TODO: Provide implementation.\n`);
4857
this.write(`}\n`);
4958
}
5059

@@ -130,7 +139,7 @@ class TypesVisitor extends BaseVisitor {
130139

131140
if (this.hasObjects || this.hasOperations) {
132141
const packageName = context.config.package || "./module";
133-
this.write(` } from "${packageName}";\n\n`);
142+
this.write(` } from "${packageName}";\n`);
134143
}
135144
}
136145
}

src/assemblyscript/wrappers_visitor.ts

Lines changed: 53 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
isObject,
1010
mapArgs,
1111
varAccessArg,
12-
isBytes,
12+
read,
1313
} from "./helpers";
1414
import { shouldIncludeHandler } from "../utils";
1515

@@ -26,46 +26,73 @@ export class WrappersVisitor extends BaseVisitor {
2626
this.write(
2727
`var ${operation.name.value}Handler: (${mapArgs(
2828
operation.parameters
29-
)}) => ${expandType(
30-
operation.type,
31-
true,
32-
isReference(operation.annotations)
33-
)};\n`
29+
)}) => `
3430
);
31+
if (isVoid(operation.type)) {
32+
this.write(`Error | null;\n`);
33+
} else {
34+
this.write(
35+
`Result<${expandType(
36+
operation.type,
37+
true,
38+
isReference(operation.annotations)
39+
)}>;\n`
40+
);
41+
}
3542
this
36-
.write(`function ${operation.name.value}Wrapper(payload: ArrayBuffer): ArrayBuffer {
43+
.write(`function ${operation.name.value}Wrapper(payload: ArrayBuffer): Result<ArrayBuffer> {
3744
const decoder = new Decoder(payload)\n`);
3845
if (operation.isUnary()) {
39-
this.write(`const request = new ${expandType(
40-
operation.unaryOp().type,
41-
false,
42-
isReference(operation.annotations)
43-
)}();
46+
const unaryParam = operation.parameters[0];
47+
if (isObject(unaryParam.type)) {
48+
this.write(`const request = new ${expandType(
49+
operation.unaryOp().type,
50+
false,
51+
isReference(operation.annotations)
52+
)}();
4453
request.decode(decoder);\n`);
45-
this.write(isVoid(operation.type) ? "" : "const response = ");
46-
this.write(`${operation.name.value}Handler(request);\n`);
54+
this.write(isVoid(operation.type) ? "" : "const result = ");
55+
this.write(`${operation.name.value}Handler(request);\n`);
56+
} else {
57+
this.write(`const ${read("val", unaryParam.type, false, false)}`);
58+
this.write(isVoid(operation.type) ? "" : "const result = ");
59+
this.write(`${operation.name.value}Handler(val);\n`);
60+
}
4761
} else {
48-
this.write(`const inputArgs = new ${capitalize(
49-
operation.name.value
50-
)}Args();
51-
inputArgs.decode(decoder);\n`);
52-
this.write(isVoid(operation.type) ? "" : "const response = ");
62+
if (operation.parameters.length > 0) {
63+
this.write(`const inputArgs = new ${capitalize(
64+
operation.name.value
65+
)}Args();
66+
inputArgs.decode(decoder);
67+
if (decoder.error()) {
68+
return Result.error<ArrayBuffer>(decoder.error()!)
69+
}\n`);
70+
}
5371
this.write(
54-
`${operation.name.value}Handler(${varAccessArg(
72+
`const result = ${operation.name.value}Handler(${varAccessArg(
5573
"inputArgs",
5674
operation.parameters
5775
)});\n`
5876
);
77+
if (isVoid(operation.type)) {
78+
this.write(`if (result) {
79+
return Result.error<ArrayBuffer>(result);
80+
}\n`);
81+
} else {
82+
this.write(`if (!result.isOk) {
83+
return Result.error<ArrayBuffer>(result.error()!);
84+
}\n`);
85+
}
86+
}
87+
if (!isVoid(operation.type)) {
88+
this.write(`const response = result.get();\n`);
5989
}
6090
if (isVoid(operation.type)) {
6191
this.visitWrapperBeforeReturn(context);
62-
this.write(`return new ArrayBuffer(0);\n`);
63-
} else if (isBytes(operation.type)) {
64-
this.visitWrapperBeforeReturn(context);
65-
this.write(`return response;\n`);
92+
this.write(`return Result.ok(new ArrayBuffer(0));\n`);
6693
} else if (isObject(operation.type)) {
6794
this.visitWrapperBeforeReturn(context);
68-
this.write(`return response.toBuffer();\n`);
95+
this.write(`return Result.ok(response.toBuffer());\n`);
6996
} else {
7097
this.write(`const sizer = new Sizer();\n`);
7198
this.write(
@@ -79,7 +106,7 @@ export class WrappersVisitor extends BaseVisitor {
79106
isReference(operation.annotations)
80107
)};\n`);
81108
this.visitWrapperBeforeReturn(context);
82-
this.write(`return ua;\n`);
109+
this.write(`return Result.ok(ua);\n`);
83110
}
84111
this.write(`}\n\n`);
85112
}

templates/assemblyscript/package.json.tmpl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
"author": "{{.author}}",
66
"scripts": {
77
"test": "echo \"Error: no test specified\" && exit 1",
8-
"build": "asc assembly/index.ts -b build/{{.name}}.wasm --use abort=assembly/index/abort --optimize"
8+
"build": "asc assembly/index.ts -o build/{{.name}}.wasm --use abort=assembly/index/abort --optimize"
99
},
1010
"license": "ISC",
1111
"dependencies": {
12-
"@wapc/as-guest": "^v0.2.1",
12+
"@wapc/as-guest": "^v0.3.1",
1313
"@wapc/as-msgpack": "^0.1.14"
1414
},
1515
"devDependencies": {
16-
"assemblyscript": "^0.17.1"
16+
"assemblyscript": "^0.20.13"
1717
}
1818
}

0 commit comments

Comments
 (0)