/
dictionary.go
101 lines (89 loc) · 3.08 KB
/
dictionary.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package gowasm
import (
"bytes"
"fmt"
"io"
"strings"
"text/template"
"github.com/gowebapi/webidl-bind/types"
)
const dictionaryTmplInput = `
{{define "header"}}
// dictionary: {{.Dict.Basic.Idl}}
type {{.Dict.Basic.Def}} struct {
{{range .Members}} {{.Name.Def}} {{.Type.VarOut}}
{{end}}
}
// JSValue is allocating a new javascript object and copy
// all values
func (_this * {{.Dict.Basic.Def}} ) JSValue() js.Value {
out := js.Global().Get("Object").New()
{{.To}}
return out
}
// {{.Dict.Basic.Def}}FromJS is allocating a new
// {{.Dict.Basic.Def}} object and copy all values in the value javascript object.
func {{.Dict.Basic.Def}}FromJS(value js.Value) {{.Type.Output}} {
var out {{.Dict.Basic.Def}}
{{.From}}
return {{if .Type.Pointer}}&{{end}} out
}
{{end}}
`
var dictionaryTmpl = template.Must(template.New("dictionary").Parse(dictionaryTmplInput))
type dictionaryData struct {
Dict *types.Dictionary
Members []*dictionaryMember
Required []*dictionaryMember
HaveReq bool
ReqParamLine string
From string
To string
Type *types.TypeInfo
}
type dictionaryMember struct {
Name types.MethodName
Type *types.TypeInfo
Ref types.TypeRef
fromIn, fromOut string
toIn, toOut string
}
func writeDictionary(dst io.Writer, value types.Type) error {
dict := value.(*types.Dictionary)
data := &dictionaryData{
Dict: dict,
}
data.Type, _ = dict.DefaultParam()
var to, from bytes.Buffer
var reqParam []string
for idx, mi := range dict.Members {
mo := &dictionaryMember{
Name: *mi.Name(),
}
mo.Type, mo.Ref = mi.Type.DefaultParam()
data.Members = append(data.Members, mo)
if mi.Required {
data.HaveReq = true
reqParam = append(reqParam, fmt.Sprint(mi.Name().Internal, " ", mo.Type.Input))
data.Required = append(data.Required, mo)
}
mo.fromIn, mo.fromOut = setupVarName("value.Get(\"@name@\")", idx, mo.Name.Idl, false), setupVarName("value%d", idx, mo.Name.Def, false)
mo.toIn, mo.toOut = setupVarName("_this.@name@", idx, mo.Name.Def, false), setupVarName("value%d", idx, mo.Name.Def, false)
from.WriteString(inoutParamStart(mo.Ref, mo.Type, mo.fromOut, mo.fromIn, idx, useOut, inoutFromTmpl))
from.WriteString(inoutGetToFromWasm(mo.Ref, mo.Type, mo.fromOut, mo.fromIn, idx, useOut, inoutFromTmpl))
from.WriteString(inoutParamEnd(mo.Type, "", inoutFromTmpl))
from.WriteString(fmt.Sprintf("\n\tout.%s = value%d\n", mo.Name.Def, idx))
to.WriteString(inoutParamStart(mo.Ref, mo.Type, mo.toOut, mo.toIn, idx, useOut, inoutToTmpl))
to.WriteString(inoutGetToFromWasm(mo.Ref, mo.Type, mo.toOut, mo.toIn, idx, useOut, inoutToTmpl))
to.WriteString(inoutParamEnd(mo.Type, "", inoutToTmpl))
to.WriteString(fmt.Sprintf("\n\tout.Set(\"%s\", value%d)\n", mi.Name().Idl, idx))
}
varFrom := inoutDictionaryVariableStart(data, useOut, inoutFromTmpl)
varTo := inoutDictionaryVariableStart(data, useOut, inoutToTmpl)
data.ReqParamLine = strings.Join(reqParam, ", ")
data.From, data.To = varFrom+from.String(), varTo+to.String()
if err := dictionaryTmpl.ExecuteTemplate(dst, "header", data); err != nil {
return err
}
return nil
}