2014-07-31 19:09:13 +00:00
|
|
|
// 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.
|
|
|
|
//
|
2015-05-13 20:33:55 +00:00
|
|
|
// 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)
|
2014-12-09 03:07:24 +00:00
|
|
|
package bind // import "golang.org/x/mobile/bind"
|
2014-07-31 19:09:13 +00:00
|
|
|
|
|
|
|
// TODO(crawshaw): slice support
|
|
|
|
// TODO(crawshaw): channel support
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"go/format"
|
|
|
|
"go/token"
|
|
|
|
"io"
|
|
|
|
|
2014-11-09 21:55:57 +00:00
|
|
|
"golang.org/x/tools/go/types"
|
2014-07-31 19:09:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// GenJava generates a Java API from a Go package.
|
|
|
|
func GenJava(w io.Writer, fset *token.FileSet, pkg *types.Package) error {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
g := &javaGen{
|
|
|
|
printer: &printer{buf: buf, indentEach: []byte(" ")},
|
|
|
|
fset: fset,
|
|
|
|
pkg: pkg,
|
|
|
|
}
|
|
|
|
if err := g.gen(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err := io.Copy(w, buf)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenGo generates a Go stub to support foreign language APIs.
|
|
|
|
func GenGo(w io.Writer, fset *token.FileSet, pkg *types.Package) error {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
g := &goGen{
|
|
|
|
printer: &printer{buf: buf, indentEach: []byte("\t")},
|
|
|
|
fset: fset,
|
|
|
|
pkg: pkg,
|
|
|
|
}
|
|
|
|
if err := g.gen(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
src := buf.Bytes()
|
|
|
|
srcf, err := format.Source(src)
|
|
|
|
if err != nil {
|
|
|
|
w.Write(src) // for debugging
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = w.Write(srcf)
|
|
|
|
return err
|
|
|
|
}
|
2015-06-05 19:24:08 +00:00
|
|
|
|
|
|
|
// GenObjc generates the Objective-C API from a Go package.
|
|
|
|
func GenObjc(w io.Writer, fset *token.FileSet, pkg *types.Package, isHeader bool) error {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
g := &objcGen{
|
|
|
|
printer: &printer{buf: buf, indentEach: []byte("\t")},
|
|
|
|
fset: fset,
|
|
|
|
pkg: pkg,
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
if isHeader {
|
|
|
|
err = g.genH()
|
|
|
|
} else {
|
|
|
|
err = g.genM()
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = io.Copy(w, buf)
|
|
|
|
return err
|
|
|
|
}
|