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

Commit 143f0d4

Browse files
committed
Misc TinyGo fixes
1 parent 6821408 commit 143f0d4

12 files changed

+86
-43
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@wapc/widl-codegen",
3-
"version": "0.0.10",
3+
"version": "0.0.11",
44
"description": "Flexible code generation using WIDL",
55
"keywords": [
66
"webassembly",

src/assemblyscript/helpers.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ export function isVoid(t: Type): boolean {
439439
* Determines if a node is a bytes node
440440
* @param t Node that is a Type node
441441
*/
442-
export function isBytes(t: Type): boolean {
442+
export function isBytes(t: Type): boolean {
443443
if (t.isKind(Kind.Named)) {
444444
return (t as Named).name.value == "bytes";
445445
}

src/assemblyscript/host_visitor.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export class HostVisitor extends BaseVisitor {
9494
context.namespace.name.value
9595
)}, ${strQuote(operation.name.value)}, ua);\n`
9696
);
97-
}
97+
}
9898
} else {
9999
this.write(
100100
`const inputArgs = new ${capitalize(operation.name.value)}Args();\n`

src/assemblyscript/scaffold_visitor.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,19 @@ export class ScaffoldVisitor extends BaseVisitor {
3838
isReference(operation.annotations)
3939
);
4040
this.write(
41-
`function ${operation.name.value}(${mapArgs(
42-
operation.parameters
43-
)}):`
41+
`function ${operation.name.value}(${mapArgs(operation.parameters)}):`
4442
);
4543
if (isVoid(operation.type)) {
4644
this.write(`Error | null\n`);
4745
} else {
48-
this.write(`Result<${expanded}>`)
46+
this.write(`Result<${expanded}>`);
4947
}
5048
this.write(` {\n`);
5149
if (!isVoid(operation.type)) {
5250
const dv = defaultValueForType(operation.type);
53-
this.write(` return Result.error<${expanded}>(new Error("not implemented"));\n`);
51+
this.write(
52+
` return Result.error<${expanded}>(new Error("not implemented"));\n`
53+
);
5454
} else {
5555
this.write(`return null\n`);
5656
}

src/go/host_visitor.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,9 @@ export class HostVisitor extends BaseVisitor {
7272
);
7373
}
7474
if (operation.isUnary()) {
75-
this.write(`inputPayload, err := msgpack.Marshal(${operation.parameters[0].type.isKind(Kind.Optional) ? "" : "&"
76-
}${operation.parameters[0].name.value})
75+
this.write(`inputPayload, err := msgpack.Marshal(${
76+
operation.parameters[0].type.isKind(Kind.Optional) ? "" : "&"
77+
}${operation.parameters[0].name.value})
7778
if err != nil {
7879
return ret, err
7980
}\n`);

src/rust/host_visitor.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,11 @@ pub struct ${className} {
117117
if (!retVoid) {
118118
this.write(`.map(|vec| {
119119
let resp = deserialize::<${expandType(
120-
operation.type,
121-
undefined,
122-
true,
123-
isReference(operation.annotations)
124-
)}>(vec.as_ref()).unwrap();
120+
operation.type,
121+
undefined,
122+
true,
123+
isReference(operation.annotations)
124+
)}>(vec.as_ref()).unwrap();
125125
resp
126126
})\n`);
127127
} else {

src/tinygo/constant.ts

+19
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,25 @@ export const primitives = new Set([
2626
"f32",
2727
"f64",
2828
"string",
29+
"bytes",
30+
]);
31+
32+
export const codecFuncs = new Map<string, string>([
33+
["ID", "StringToBytes"],
34+
["bool", "BoolToBytes"],
35+
["string", "StringToBytes"],
36+
["datetime", "StringToBytes"],
37+
["i8", "Int8ToBytes"],
38+
["u8", "Uint8ToBytes"],
39+
["i16", "Int16ToBytes"],
40+
["u16", "Uint16ToBytes"],
41+
["i32", "Int32ToBytes"],
42+
["u32", "Uint32ToBytes"],
43+
["i64", "Int64ToBytes"],
44+
["u64", "Uint64ToBytes"],
45+
["f32", "Float32ToBytes"],
46+
["f64", "Float64ToBytes"],
47+
["bytes", "ByteArraToBytesy"],
2948
]);
3049

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

src/tinygo/helpers.ts

+13-12
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,8 @@ export function read(
281281
variable == "item" ||
282282
variable == "key" ||
283283
variable == "value" ||
284-
variable == "ret"
284+
variable == "ret" ||
285+
variable == "request"
285286
) {
286287
if (errorHandling) {
287288
prefix = variable + ", err := ";
@@ -308,6 +309,9 @@ export function read(
308309
}
309310
if (prevOptional) {
310311
return `nonNil, err = ${decodeFn}
312+
if err != nil {
313+
return err
314+
}
311315
${prefix}${namedNode.name.value == "bytes" ? "" : "&"}nonNil\n`;
312316
}
313317
return `${prefix}${decodeFn}\n`;
@@ -412,17 +416,14 @@ export function read(
412416
switch (true) {
413417
case optNode.type.isKind(Kind.ListType):
414418
case optNode.type.isKind(Kind.MapType):
415-
return (
416-
prefix +
417-
read(
418-
typeInstRef,
419-
variable,
420-
false,
421-
defaultVal,
422-
optNode.type,
423-
true,
424-
isReference
425-
)
419+
return read(
420+
typeInstRef,
421+
variable,
422+
false,
423+
defaultVal,
424+
optNode.type,
425+
true,
426+
isReference
426427
);
427428
}
428429
let optCode = "";

src/tinygo/host_visitor.ts

+14-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Context, Writer, BaseVisitor, Optional } from "@wapc/widl/ast";
1+
import { Context, Writer, BaseVisitor, Optional, Named } from "@wapc/widl/ast";
22
import {
33
expandType,
44
read,
@@ -12,6 +12,7 @@ import {
1212
defaultValueForType,
1313
} from "./helpers";
1414
import { formatComment, shouldIncludeHostCall } from "../utils";
15+
import { codecFuncs } from "./constant";
1516

1617
export class HostVisitor extends BaseVisitor {
1718
constructor(writer: Writer) {
@@ -82,10 +83,18 @@ func New${className}(binding string) *${className} {
8283
)}, ${strQuote(operation.name.value)}, []byte{})\n`
8384
);
8485
} else if (operation.isUnary()) {
85-
this.write(`inputBytes, err := msgpack.ToBytes(&${
86-
operation.unaryOp().name.value
87-
})
88-
if err != nil {
86+
const unaryParam = operation.unaryOp();
87+
if (isObject(unaryParam.type)) {
88+
this.write(
89+
`inputBytes, err := msgpack.ToBytes(&${unaryParam.name.value})\n`
90+
);
91+
} else {
92+
const codecFunc = codecFuncs.get((unaryParam.type as Named).name.value);
93+
this.write(
94+
`inputBytes, err := msgpack.${codecFunc}(${unaryParam.name.value})\n`
95+
);
96+
}
97+
this.write(`if err != nil {
8998
return ${defaultValWithComma}err
9099
}\n`);
91100
if (!retVoid) {

src/tinygo/wrappers_visitor.ts

+20-7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
mapArgs,
1111
varAccessArg,
1212
uncapitalize,
13+
read,
1314
} from "./helpers";
1415
import { shouldIncludeHandler } from "../utils";
1516

@@ -75,13 +76,25 @@ export class WrapperFuncsVisitor extends BaseVisitor {
7576
this.write(`decoder := msgpack.NewDecoder(payload)\n`);
7677
}
7778
if (operation.isUnary()) {
78-
this.write(`var request ${expandType(
79-
operation.unaryOp().type,
80-
undefined,
81-
false,
82-
isReference(operation.annotations)
83-
)}
84-
request.Decode(&decoder)\n`);
79+
const unaryParam = operation.parameters[0];
80+
if (isObject(unaryParam.type)) {
81+
this.write(`var request ${expandType(
82+
operation.unaryOp().type,
83+
undefined,
84+
false,
85+
isReference(operation.annotations)
86+
)}
87+
if err := request.Decode(&decoder); err != nil {
88+
return nil, err
89+
}\n`);
90+
} else {
91+
this.write(
92+
`${read(false, "request", true, "", unaryParam.type, false, false)}`
93+
);
94+
this.write(`if err != nil {
95+
return nil, err
96+
}\n`);
97+
}
8598
this.write(isVoid(operation.type) ? "err := " : "response, err := ");
8699
this.write(`${uncapitalize(operation.name.value)}Handler(request)\n`);
87100
} else {

templates/tinygo/Makefile.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ codegen:
99

1010
build:
1111
mkdir -p build
12-
tinygo build -o build/{{.name}}.wasm -target wasm -no-debug pkg/main.go
12+
tinygo build -o build/{{.name}}.wasm -target wasi -no-debug pkg/main.go
1313

1414
clean:
1515
rm -Rf build

templates/tinygo/go.mod.tmpl

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
module {{.module}}
22

3-
go 1.15
3+
go 1.17
44

55
require (
6-
github.com/wapc/tinygo-msgpack v0.1.2
7-
github.com/wapc/wapc-guest-tinygo v0.3.0
6+
github.com/wapc/tinygo-msgpack v0.1.3
7+
github.com/wapc/wapc-guest-tinygo v0.3.1
88
)

0 commit comments

Comments
 (0)