File: bind.go

package info (click to toggle)
golang-golang-x-mobile 0.0~git20251126.5c265dc-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,836 kB
  • sloc: objc: 1,536; java: 1,489; ansic: 1,159; xml: 365; asm: 34; sh: 14; makefile: 5
file content (59 lines) | stat: -rw-r--r-- 1,290 bytes parent folder | download
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
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Package bind implements a code generator for gobind.
//
// See the documentation on the gobind command for usage details
// and the list of currently supported types.
// (http://godoc.org/golang.org/x/mobile/cmd/gobind)
package bind

// TODO(crawshaw): slice support
// TODO(crawshaw): channel support

import (
	"bytes"
	"go/format"
	"go/token"
	"go/types"
	"io"
)

const gobindPreamble = "// Code generated by gobind. DO NOT EDIT.\n\n"

type (
	GeneratorConfig struct {
		Writer io.Writer
		Fset   *token.FileSet
		Pkg    *types.Package
		AllPkg []*types.Package
	}

	fileType int
)

// GenGo generates a Go stub to support foreign language APIs.
func GenGo(conf *GeneratorConfig) error {
	buf := new(bytes.Buffer)
	g := &goGen{
		Generator: &Generator{
			Printer: &Printer{Buf: buf, IndentEach: []byte("\t")},
			Fset:    conf.Fset,
			AllPkg:  conf.AllPkg,
			Pkg:     conf.Pkg,
		},
	}
	g.Init()
	if err := g.gen(); err != nil {
		return err
	}
	src := buf.Bytes()
	srcf, err := format.Source(src)
	if err != nil {
		conf.Writer.Write(src) // for debugging
		return err
	}
	_, err = conf.Writer.Write(srcf)
	return err
}