<commit_before>package main

import (
	"fmt"
	"net"
	"os"
	"path/filepath"
)

/*
	Parameter boundaries do not include the command or collection

	This has one parameter if Command.CollectionRequired is true
	otherwise it is considered two.
	> ADD foobar "this item"
*/
type Command struct {
	Name        string
	Parameters  []CommandParameter
	HelpMessage string
	Callback    func([]interface{}, net.Conn, *Collection)
}

var Commands map[string]Command

func init() {
	Commands = map[string]Command{}
	Commands["create"] = Command{
		Name:        "create",
		HelpMessage: "Create a new collection",
		Parameters: []CommandParameter{
			CommandParameter{
				Name:     "name",
				Type:     "collection",
				Required: true,
			},
		},
		Callback: func(params []interface{}, conn net.Conn, coll *Collection) {
			perms := os.FileMode(0700)
			os.MkdirAll(coll.DataPath, perms)
			os.MkdirAll(coll.ConfigPath, perms)
			coll.SetConfig("maxitems", 100)
			coll.SetConfig("maxage", 0)
			conn.Write([]byte("+OK\r\n"))
		},
	}
	Commands["delete"] = Command{
		Name:        "delete",
		HelpMessage: "Delete an existing collection",
		Parameters: []CommandParameter{
			CommandParameter{
				Name:     "name",
				Type:     "collection",
				Required: true,
			},
		},
		Callback: func(params []interface{}, conn net.Conn, coll *Collection) {
			os.RemoveAll(coll.BasePath)
			conn.Write([]byte("+OK\r\n"))
		},
	}

	Commands["get"] = Command{
		Name:        "get",
		HelpMessage: "Get all items including timestamps in the collection in ascending order",
		Parameters: []CommandParameter{
			CommandParameter{
				Name:     "name",
				Type:     "collection",
				Required: true,
			},
		},
		Callback: func(params []interface{}, conn net.Conn, coll *Collection) {
			files := coll.GetDataFiles()
			fmt.Fprintf(conn, "*%d\r\n", len(files))
			for _, file := range files {
				sec, nsec := splitTimestamp(filepath.Base(file))
				fmt.Fprintf(conn, "*3\r\n:%d\r\n:%d\r\n", sec, nsec)
				streamFile(file, conn)
			}
		},
	}

	Commands["gett"] = Command{
		Name:        "gett",
		HelpMessage: "Get all timestamps in the collection in ascending order",
		Parameters: []CommandParameter{
			CommandParameter{
				Name:     "name",
				Type:     "collection",
				Required: true,
			},
		},
		Callback: func(params []interface{}, conn net.Conn, coll *Collection) {
			files := coll.GetDataFiles()
			fmt.Fprintf(conn, "*%d\r\n", len(files))
			for _, file := range files {
				sec, nsec := splitTimestamp(filepath.Base(file))
				fmt.Fprintf(conn, "*2\r\n:%d\r\n:%d\r\n", sec, nsec)
			}
		},
	}

	Commands["getd"] = Command{
		Name:        "getd",
		HelpMessage: "Get all items' data in the collection in ascending order",
		Parameters: []CommandParameter{
			CommandParameter{
				Name:     "name",
				Type:     "collection",
				Required: true,
			},
		},
		Callback: func(params []interface{}, conn net.Conn, coll *Collection) {
			files := coll.GetDataFiles()
			streamFiles(files, conn)
		},
	}

	Commands["head"] = Command{
		Name:        "head",
		HelpMessage: "Get the latest item in the collection",
		Parameters: []CommandParameter{
			CommandParameter{
				Name:     "name",
				Type:     "collection",
				Required: true,
			},
		},
		Callback: func(params []interface{}, conn net.Conn, coll *Collection) {
			files := coll.GetDataFiles()
			file := files[len(files)-1]
			sec, nsec := splitTimestamp(filepath.Base(file))
			fmt.Fprintf(conn, "*3\r\n:%d\r\n:%d\r\n", sec, nsec)
			streamFile(file, conn)
		},
	}

	Commands["headt"] = Command{
		Name:        "headt",
		HelpMessage: "Get the latest item's timestamp in the collection",
		Parameters: []CommandParameter{
			CommandParameter{
				Name:     "name",
				Type:     "collection",
				Required: true,
			},
		},
		Callback: func(params []interface{}, conn net.Conn, coll *Collection) {
			files := coll.GetDataFiles()
			file := files[len(files)-1]
			sec, nsec := splitTimestamp(filepath.Base(file))
			fmt.Fprintf(conn, "*2\r\n:%d\r\n:%d\r\n", sec, nsec)
		},
	}

	Commands["headd"] = Command{
		Name:        "headd",
		HelpMessage: "Get the latest item data in the collection",
		Parameters: []CommandParameter{
			CommandParameter{
				Name:     "name",
				Type:     "collection",
				Required: true,
			},
		},
		Callback: func(params []interface{}, conn net.Conn, coll *Collection) {
			files := coll.GetDataFiles()
			streamFile(files[len(files)-1], conn)
		},
	}

	Commands["tail"] = Command{
		Name:        "tail",
		HelpMessage: "Get the oldest item in the collection",
		Parameters: []CommandParameter{
			CommandParameter{
				Name:     "name",
				Type:     "collection",
				Required: true,
			},
		},
		Callback: func(params []interface{}, conn net.Conn, coll *Collection) {
			files := coll.GetDataFiles()
			file := files[0]
			sec, nsec := splitTimestamp(filepath.Base(file))
			fmt.Fprintf(conn, "*3\r\n:%d\r\n:%d\r\n", sec, nsec)
			streamFile(file, conn)
		},
	}

	Commands["tailt"] = Command{
		Name:        "tailt",
		HelpMessage: "Get the oldest item's timestamp in the collection",
		Parameters: []CommandParameter{
			CommandParameter{
				Name:     "name",
				Type:     "collection",
				Required: true,
			},
		},
		Callback: func(params []interface{}, conn net.Conn, coll *Collection) {
			files := coll.GetDataFiles()
			file := files[0]
			sec, nsec := splitTimestamp(filepath.Base(file))
			fmt.Fprintf(conn, "*2\r\n:%d\r\n:%d\r\n", sec, nsec)
		},
	}

	Commands["taild"] = Command{
		Name:        "taild",
		HelpMessage: "Get the oldest item data in the collection",
		Parameters: []CommandParameter{
			CommandParameter{
				Name:     "name",
				Type:     "collection",
				Required: true,
			},
		},
		Callback: func(params []interface{}, conn net.Conn, coll *Collection) {
			files := coll.GetDataFiles()
			streamFile(files[0], conn)
		},
	}

	Commands["add"] = Command{
		Name:        "add",
		HelpMessage: "Add item in collection",
		Parameters: []CommandParameter{
			CommandParameter{
				Name:     "name",
				Type:     "collection",
				Required: true,
			},
			CommandParameter{
				Name:     "data",
				Type:     "binary",
				Required: false,
			},
		},
		Callback: func(params []interface{}, conn net.Conn, coll *Collection) {
			filename := timestamp()
			fullFilePath := filepath.Join(coll.DataPath, filename)
			file, err := os.Create(fullFilePath)
			if err != nil {
				conn.Write([]byte("-cannot add item\r\n"))
			} else {
				file.Chmod(os.FileMode(0700))
				if len(params) > 0 {
					file.Write(params[0].([]byte))
				}
				file.Close()
				coll.EnforceCapacity()
				conn.Write([]byte("+OK\r\n"))
			}
		},
	}

	Commands["cget"] = Command{
		Name:        "cget",
		HelpMessage: "Get configuration value",
		Parameters: []CommandParameter{
			CommandParameter{
				Name:     "name",
				Type:     "collection",
				Required: true,
			},
			CommandParameter{
				Name:     "key",
				Type:     "string",
				Required: true,
			},
		},
		Callback: func(params []interface{}, conn net.Conn, coll *Collection) {
			value, err := coll.GetConfig(params[0].(string))
			if err != nil {
				conn.Write([]byte("-Error getting configuration value\r\n"))
			} else {
				fmt.Fprintf(conn, ":%d\r\n", value)
			}
		},
	}

	Commands["cset"] = Command{
		Name:        "cset",
		HelpMessage: "Set configuration value",
		Parameters: []CommandParameter{
			CommandParameter{
				Name:     "name",
				Type:     "collection",
				Required: true,
			},
			CommandParameter{
				Name:     "key",
				Type:     "string",
				Required: true,
			},
			CommandParameter{
				Name:     "value",
				Type:     "integer",
				Required: true,
			},
		},
		Callback: func(params []interface{}, conn net.Conn, coll *Collection) {
			if err := coll.SetConfig(params[0].(string), params[1].(int64)); err != nil {
				conn.Write([]byte("-Error getting configuration value\r\n"))
			} else {
				coll.EnforceCapacity()
				fmt.Fprint(conn, "+OK\r\n")
			}
		},
	}

	Commands["ping"] = Command{
		Name:        "ping",
		HelpMessage: "Ping reccs for life",
		Parameters:  []CommandParameter{},
		Callback: func(params []interface{}, conn net.Conn, coll *Collection) {
			conn.Write([]byte("+PONG\r\n"))
		},
	}
}
<commit_msg>added EXISTS command<commit_after>package main

import (
	"fmt"
	"net"
	"os"
	"path/filepath"
)

/*
	Parameter boundaries do not include the command or collection

	This has one parameter if Command.CollectionRequired is true
	otherwise it is considered two.
	> ADD foobar "this item"
*/
type Command struct {
	Name        string
	Parameters  []CommandParameter
	HelpMessage string
	Callback    func([]interface{}, net.Conn, *Collection)
}

var Commands map[string]Command

func init() {
	Commands = map[string]Command{}
	Commands["create"] = Command{
		Name:        "create",
		HelpMessage: "Create a new collection",
		Parameters: []CommandParameter{
			CommandParameter{
				Name:     "name",
				Type:     "collection",
				Required: true,
			},
		},
		Callback: func(params []interface{}, conn net.Conn, coll *Collection) {
			perms := os.FileMode(0700)
			os.MkdirAll(coll.DataPath, perms)
			os.MkdirAll(coll.ConfigPath, perms)
			coll.SetConfig("maxitems", 100)
			coll.SetConfig("maxage", 0)
			conn.Write([]byte("+OK\r\n"))
		},
	}

	Commands["delete"] = Command{
		Name:        "delete",
		HelpMessage: "Delete an existing collection",
		Parameters: []CommandParameter{
			CommandParameter{
				Name:     "name",
				Type:     "collection",
				Required: true,
			},
		},
		Callback: func(params []interface{}, conn net.Conn, coll *Collection) {
			os.RemoveAll(coll.BasePath)
			conn.Write([]byte("+OK\r\n"))
		},
	}

	Commands["exists"] = Command{
		Name:        "exists",
		HelpMessage: "Verify existence of a collection",
		Parameters: []CommandParameter{
			CommandParameter{
				Name:     "name",
				Type:     "collection",
				Required: true,
			},
		},
		Callback: func(params []interface{}, conn net.Conn, coll *Collection) {
			if _, err := os.Open(coll.BasePath); err != nil {
				conn.Write([]byte(":0\r\n"))
			} else {
				conn.Write([]byte(":1\r\n"))
			}
		},
	}

	Commands["get"] = Command{
		Name:        "get",
		HelpMessage: "Get all items including timestamps in the collection in ascending order",
		Parameters: []CommandParameter{
			CommandParameter{
				Name:     "name",
				Type:     "collection",
				Required: true,
			},
		},
		Callback: func(params []interface{}, conn net.Conn, coll *Collection) {
			files := coll.GetDataFiles()
			fmt.Fprintf(conn, "*%d\r\n", len(files))
			for _, file := range files {
				sec, nsec := splitTimestamp(filepath.Base(file))
				fmt.Fprintf(conn, "*3\r\n:%d\r\n:%d\r\n", sec, nsec)
				streamFile(file, conn)
			}
		},
	}

	Commands["gett"] = Command{
		Name:        "gett",
		HelpMessage: "Get all timestamps in the collection in ascending order",
		Parameters: []CommandParameter{
			CommandParameter{
				Name:     "name",
				Type:     "collection",
				Required: true,
			},
		},
		Callback: func(params []interface{}, conn net.Conn, coll *Collection) {
			files := coll.GetDataFiles()
			fmt.Fprintf(conn, "*%d\r\n", len(files))
			for _, file := range files {
				sec, nsec := splitTimestamp(filepath.Base(file))
				fmt.Fprintf(conn, "*2\r\n:%d\r\n:%d\r\n", sec, nsec)
			}
		},
	}

	Commands["getd"] = Command{
		Name:        "getd",
		HelpMessage: "Get all items' data in the collection in ascending order",
		Parameters: []CommandParameter{
			CommandParameter{
				Name:     "name",
				Type:     "collection",
				Required: true,
			},
		},
		Callback: func(params []interface{}, conn net.Conn, coll *Collection) {
			files := coll.GetDataFiles()
			streamFiles(files, conn)
		},
	}

	Commands["head"] = Command{
		Name:        "head",
		HelpMessage: "Get the latest item in the collection",
		Parameters: []CommandParameter{
			CommandParameter{
				Name:     "name",
				Type:     "collection",
				Required: true,
			},
		},
		Callback: func(params []interface{}, conn net.Conn, coll *Collection) {
			files := coll.GetDataFiles()
			file := files[len(files)-1]
			sec, nsec := splitTimestamp(filepath.Base(file))
			fmt.Fprintf(conn, "*3\r\n:%d\r\n:%d\r\n", sec, nsec)
			streamFile(file, conn)
		},
	}

	Commands["headt"] = Command{
		Name:        "headt",
		HelpMessage: "Get the latest item's timestamp in the collection",
		Parameters: []CommandParameter{
			CommandParameter{
				Name:     "name",
				Type:     "collection",
				Required: true,
			},
		},
		Callback: func(params []interface{}, conn net.Conn, coll *Collection) {
			files := coll.GetDataFiles()
			file := files[len(files)-1]
			sec, nsec := splitTimestamp(filepath.Base(file))
			fmt.Fprintf(conn, "*2\r\n:%d\r\n:%d\r\n", sec, nsec)
		},
	}

	Commands["headd"] = Command{
		Name:        "headd",
		HelpMessage: "Get the latest item data in the collection",
		Parameters: []CommandParameter{
			CommandParameter{
				Name:     "name",
				Type:     "collection",
				Required: true,
			},
		},
		Callback: func(params []interface{}, conn net.Conn, coll *Collection) {
			files := coll.GetDataFiles()
			streamFile(files[len(files)-1], conn)
		},
	}

	Commands["tail"] = Command{
		Name:        "tail",
		HelpMessage: "Get the oldest item in the collection",
		Parameters: []CommandParameter{
			CommandParameter{
				Name:     "name",
				Type:     "collection",
				Required: true,
			},
		},
		Callback: func(params []interface{}, conn net.Conn, coll *Collection) {
			files := coll.GetDataFiles()
			file := files[0]
			sec, nsec := splitTimestamp(filepath.Base(file))
			fmt.Fprintf(conn, "*3\r\n:%d\r\n:%d\r\n", sec, nsec)
			streamFile(file, conn)
		},
	}

	Commands["tailt"] = Command{
		Name:        "tailt",
		HelpMessage: "Get the oldest item's timestamp in the collection",
		Parameters: []CommandParameter{
			CommandParameter{
				Name:     "name",
				Type:     "collection",
				Required: true,
			},
		},
		Callback: func(params []interface{}, conn net.Conn, coll *Collection) {
			files := coll.GetDataFiles()
			file := files[0]
			sec, nsec := splitTimestamp(filepath.Base(file))
			fmt.Fprintf(conn, "*2\r\n:%d\r\n:%d\r\n", sec, nsec)
		},
	}

	Commands["taild"] = Command{
		Name:        "taild",
		HelpMessage: "Get the oldest item data in the collection",
		Parameters: []CommandParameter{
			CommandParameter{
				Name:     "name",
				Type:     "collection",
				Required: true,
			},
		},
		Callback: func(params []interface{}, conn net.Conn, coll *Collection) {
			files := coll.GetDataFiles()
			streamFile(files[0], conn)
		},
	}

	Commands["add"] = Command{
		Name:        "add",
		HelpMessage: "Add item in collection",
		Parameters: []CommandParameter{
			CommandParameter{
				Name:     "name",
				Type:     "collection",
				Required: true,
			},
			CommandParameter{
				Name:     "data",
				Type:     "binary",
				Required: false,
			},
		},
		Callback: func(params []interface{}, conn net.Conn, coll *Collection) {
			filename := timestamp()
			fullFilePath := filepath.Join(coll.DataPath, filename)
			file, err := os.Create(fullFilePath)
			if err != nil {
				conn.Write([]byte("-cannot add item\r\n"))
			} else {
				file.Chmod(os.FileMode(0700))
				if len(params) > 0 {
					file.Write(params[0].([]byte))
				}
				file.Close()
				coll.EnforceCapacity()
				conn.Write([]byte("+OK\r\n"))
			}
		},
	}

	Commands["cget"] = Command{
		Name:        "cget",
		HelpMessage: "Get configuration value",
		Parameters: []CommandParameter{
			CommandParameter{
				Name:     "name",
				Type:     "collection",
				Required: true,
			},
			CommandParameter{
				Name:     "key",
				Type:     "string",
				Required: true,
			},
		},
		Callback: func(params []interface{}, conn net.Conn, coll *Collection) {
			value, err := coll.GetConfig(params[0].(string))
			if err != nil {
				conn.Write([]byte("-Error getting configuration value\r\n"))
			} else {
				fmt.Fprintf(conn, ":%d\r\n", value)
			}
		},
	}

	Commands["cset"] = Command{
		Name:        "cset",
		HelpMessage: "Set configuration value",
		Parameters: []CommandParameter{
			CommandParameter{
				Name:     "name",
				Type:     "collection",
				Required: true,
			},
			CommandParameter{
				Name:     "key",
				Type:     "string",
				Required: true,
			},
			CommandParameter{
				Name:     "value",
				Type:     "integer",
				Required: true,
			},
		},
		Callback: func(params []interface{}, conn net.Conn, coll *Collection) {
			if err := coll.SetConfig(params[0].(string), params[1].(int64)); err != nil {
				conn.Write([]byte("-Error getting configuration value\r\n"))
			} else {
				coll.EnforceCapacity()
				fmt.Fprint(conn, "+OK\r\n")
			}
		},
	}

	Commands["ping"] = Command{
		Name:        "ping",
		HelpMessage: "Ping reccs for life",
		Parameters:  []CommandParameter{},
		Callback: func(params []interface{}, conn net.Conn, coll *Collection) {
			conn.Write([]byte("+PONG\r\n"))
		},
	}
}
<|endoftext|>


<commit_before>// Copyright 2014-2015 The project 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 main

import (
	"flag"
	"fmt"
	"go/ast"
	"go/parser"
	"go/token"
	"io"
	"os"
	"regexp"
	"strings"
	"text/template"
)

// kind of AST node
const (
	Expression = iota
	Statement
)

// go source file header
const header = `// Copyright 2014-2015 The project AUTHORS. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// DO NOT EDIT: This source file has been generated by gen/gen_ast_decoder.go

package src

`

// template for expression
const tmplExpr = `
func (dec *decoder) decode{{ .Name }}() *ast.{{ .Name }} {
	if !dec.assertNewObject() {
		return nil
	}
	if dec.isEmptyObject() {
		dec.err = errors.New("{{ .Name }} object cannot be empty")
		return nil
	}
	if dec.err != nil {
		return nil
	}
	return dec.decode{{ .Name }}Attrs()
}

func (dec *decoder) decode{{ .Name }}Attrs() *ast.{{ .Name }} {
	expr := ast.{{ .Name }}{ExprName: token.{{ .Name }}Name}
	for {
		key, err := dec.scan.nextKey()
		if err != nil {
			if err == io.EOF {
				break
			}
			dec.err = err
			return nil
		}
		if key == "" {
			dec.err = errors.New("empty key")
			return nil
		}

		{{ if .HasBasicType }}
		val, tok, err := dec.scan.nextValue()
		{{ else }}
		_, _, err = dec.scan.nextValue()
		{{ end }}
		if err != nil {
			dec.err = err
			return nil
		}

		switch key {
		{{ range $index, $field := .Fields }}
		case "{{ $field.JSONName }}":
			{{ if $field.BasicType }}
				if tok != scan{{ $field.Type }}Lit {
					dec.err = fmt.Errorf("expected '{{ $field.Type }} literal', found '%v'", tok)
					return nil
				}
				expr.{{ $field.Name }}, dec.err = dec.unmarshal{{ $field.Type }}(val)
			{{ else }}
				{{ if $field.Array }}
					dec.scan.back()
					expr.{{ $field.Name }} = dec.decode{{ $field.Type }}s()
				{{ else }}
					dec.scan.back()
					expr.{{ $field.Name }} = dec.decode{{ $field.Type }}s()
				{{ end }}
			{{ end }}
		{{ end }}
		default:
			dec.err = fmt.Errorf("unexpected value for the key '%s' of a {{.Name}} object", key)
		}

		if dec.err != nil {
			return nil
		}

		if dec.isEndObject() {
			break
		}
		if err != nil {
			return nil
		}
	}
	return &expr
}

`

// template for statements
const tmplStmt = ``

// template for "normal" types
const tmplOther = ``

const outputPath = "decode_gen.go"

type DecoderTmpl struct {
	Name   string
	Fields []Field
}

func (dt DecoderTmpl) HasBasicType() bool {
	for _, field := range dt.Fields {
		if field.BasicType {
			return true
		}
	}
	return false
}

type Field struct {
	Name      string
	JSONName  string
	Type      string
	BasicType bool
	Array     bool
}

func genExprs(w io.Writer, exprs []DecoderTmpl) error {
	if len(exprs) == 0 {
		return nil
	}

	// TODO generate decodeExpr

	t := template.Must(template.New("expressions").Parse(tmplExpr))

	for _, expr := range exprs {
		if err := t.Execute(w, expr); err != nil {
			return err
		}
	}
	return nil
}

func genStmts(w io.Writer, stmts []DecoderTmpl) error {
	if len(stmts) == 0 {
		return nil
	}

	return nil
}

func genOthers(w io.Writer, others []DecoderTmpl) error {
	if len(others) == 0 {
		return nil
	}

	return nil
}

func extractType(field *ast.Field) (typ string, array bool, basicType bool) {
	switch s := field.Type.(type) {
	case *ast.StarExpr:
		if ident, ok := s.X.(*ast.Ident); ok {
			typ = ident.String()
		}
	case *ast.ArrayType:
		switch elt := s.Elt.(type) {
		case *ast.StarExpr:
			if ident, ok := elt.X.(*ast.Ident); ok {
				typ = ident.String()
				array = true
			}
		case *ast.Ident:
			typ = elt.String()
			array = true
		}
	case *ast.Ident:
		typ = s.String()
		typ = strings.ToUpper(string(typ[0])) + typ[1:]

		switch typ {
		case "String", "Integer", "Float":
			basicType = true
		}
	}
	return
}

func extractTag(tag *ast.BasicLit) string {
	// extract JSON tag
	re := regexp.MustCompile("`json:\"([a-zA-Z0-9_]+)(,omitempty)?\"`")
	m := re.FindStringSubmatch(tag.Value)
	if len(m) < 2 {
		return ""
	}
	return m[1]
}

func warn(a ...interface{}) {
	fmt.Fprintln(os.Stderr, a...)
}

func warnf(format string, a ...interface{}) {
	warn(fmt.Sprintf(format, a...) + "\n")
}

func fatal(a ...interface{}) {
	warn(a...)
	os.Exit(1)
}

func fatalf(format string, a ...interface{}) {
	warnf(format, a...)
	os.Exit(1)
}

func main() {
	flag.Parse()

	out, err := os.Create(outputPath)
	if err != nil {
		fatal(err)
	}
	defer out.Close()

	// write file header
	out.WriteString(header)

	fset := token.NewFileSet()

	f, err := parser.ParseFile(fset, "./ast/ast.go", nil, 0)
	if err != nil {
		fatal(err)
	}

	exprs := []DecoderTmpl{}
	stmts := []DecoderTmpl{}
	others := []DecoderTmpl{}

	for _, decl := range f.Decls {
		var genDecl *ast.GenDecl
		var ok bool
		if genDecl, ok = decl.(*ast.GenDecl); !ok {
			continue
		}

		for _, spec := range genDecl.Specs {
			var typeSpec *ast.TypeSpec
			var ok bool
			if typeSpec, ok = spec.(*ast.TypeSpec); !ok {
				continue
			}

			dec := DecoderTmpl{Name: typeSpec.Name.String(), Fields: []Field{}}

			var structType *ast.StructType
			if structType, ok = typeSpec.Type.(*ast.StructType); !ok {
				continue
			}
			var kind int
			for _, field := range structType.Fields.List {
				fieldTmpl := Field{}
				// XXX: handle compositions
				if len(field.Names) == 0 {
					continue
				}
				fieldTmpl.Name = field.Names[0].String()
				fieldTmpl.JSONName = extractTag(field.Tag)
				switch fieldTmpl.JSONName {
				case "expression_name":
					kind = Expression
				case "statement_name":
					kind = Statement
				}
				if fieldTmpl.Type, fieldTmpl.Array, fieldTmpl.BasicType = extractType(field); fieldTmpl.Type == "" {
					warnf("invalid type for %s.%s", dec.Name, fieldTmpl.Name)
					continue
				}
				dec.Fields = append(dec.Fields, fieldTmpl)
			}

			switch kind {
			case Expression:
				exprs = append(exprs, dec)
			case Statement:
				stmts = append(stmts, dec)
			default:
				others = append(others, dec)
			}
		}
	}

	// generate code
	if err := genExprs(out, exprs); err != nil {
		fatal(err)
	}
	if err := genStmts(out, stmts); err != nil {
		fatal(err)
	}
	if err := genStmts(out, stmts); err != nil {
		fatal(err)
	}
}
<commit_msg>src/gen: add imports in the generated file<commit_after>// Copyright 2014-2015 The project 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 main

import (
	"flag"
	"fmt"
	"go/ast"
	"go/parser"
	"go/token"
	"io"
	"os"
	"regexp"
	"strings"
	"text/template"
)

// kind of AST node
const (
	Expression = iota
	Statement
)

// go source file header
const header = `// Copyright 2014-2015 The project AUTHORS. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// DO NOT EDIT: This source file has been generated by gen/gen_ast_decoder.go

package src

import (
	"github.com/DevMine/srcanlzr/src/ast"
	"github.com/DevMine/srcanlzr/src/token"
)

`

// template for expression
const tmplExpr = `
func (dec *decoder) decode{{ .Name }}() *ast.{{ .Name }} {
	if !dec.assertNewObject() {
		return nil
	}
	if dec.isEmptyObject() {
		dec.err = errors.New("{{ .Name }} object cannot be empty")
		return nil
	}
	if dec.err != nil {
		return nil
	}
	return dec.decode{{ .Name }}Attrs()
}

func (dec *decoder) decode{{ .Name }}Attrs() *ast.{{ .Name }} {
	expr := ast.{{ .Name }}{ExprName: token.{{ .Name }}Name}
	for {
		key, err := dec.scan.nextKey()
		if err != nil {
			if err == io.EOF {
				break
			}
			dec.err = err
			return nil
		}
		if key == "" {
			dec.err = errors.New("empty key")
			return nil
		}

		{{ if .HasBasicType }}
		val, tok, err := dec.scan.nextValue()
		{{ else }}
		_, _, err = dec.scan.nextValue()
		{{ end }}
		if err != nil {
			dec.err = err
			return nil
		}

		switch key {
		{{ range $index, $field := .Fields }}
		case "{{ $field.JSONName }}":
			{{ if $field.BasicType }}
				if tok != scan{{ $field.Type }}Lit {
					dec.err = fmt.Errorf("expected '{{ $field.Type }} literal', found '%v'", tok)
					return nil
				}
				expr.{{ $field.Name }}, dec.err = dec.unmarshal{{ $field.Type }}(val)
			{{ else }}
				{{ if $field.Array }}
					dec.scan.back()
					expr.{{ $field.Name }} = dec.decode{{ $field.Type }}s()
				{{ else }}
					dec.scan.back()
					expr.{{ $field.Name }} = dec.decode{{ $field.Type }}s()
				{{ end }}
			{{ end }}
		{{ end }}
		default:
			dec.err = fmt.Errorf("unexpected value for the key '%s' of a {{.Name}} object", key)
		}

		if dec.err != nil {
			return nil
		}

		if dec.isEndObject() {
			break
		}
		if err != nil {
			return nil
		}
	}
	return &expr
}

`

// template for statements
const tmplStmt = ``

// template for "normal" types
const tmplOther = ``

const outputPath = "decode_gen.go"

type DecoderTmpl struct {
	Name   string
	Fields []Field
}

func (dt DecoderTmpl) HasBasicType() bool {
	for _, field := range dt.Fields {
		if field.BasicType {
			return true
		}
	}
	return false
}

type Field struct {
	Name      string
	JSONName  string
	Type      string
	BasicType bool
	Array     bool
}

func genExprs(w io.Writer, exprs []DecoderTmpl) error {
	if len(exprs) == 0 {
		return nil
	}

	// TODO generate decodeExpr

	t := template.Must(template.New("expressions").Parse(tmplExpr))

	for _, expr := range exprs {
		if err := t.Execute(w, expr); err != nil {
			return err
		}
	}
	return nil
}

func genStmts(w io.Writer, stmts []DecoderTmpl) error {
	if len(stmts) == 0 {
		return nil
	}

	return nil
}

func genOthers(w io.Writer, others []DecoderTmpl) error {
	if len(others) == 0 {
		return nil
	}

	return nil
}

func extractType(field *ast.Field) (typ string, array bool, basicType bool) {
	switch s := field.Type.(type) {
	case *ast.StarExpr:
		if ident, ok := s.X.(*ast.Ident); ok {
			typ = ident.String()
		}
	case *ast.ArrayType:
		switch elt := s.Elt.(type) {
		case *ast.StarExpr:
			if ident, ok := elt.X.(*ast.Ident); ok {
				typ = ident.String()
				array = true
			}
		case *ast.Ident:
			typ = elt.String()
			array = true
		}
	case *ast.Ident:
		typ = s.String()
		typ = strings.ToUpper(string(typ[0])) + typ[1:]

		switch typ {
		case "String", "Integer", "Float":
			basicType = true
		}
	}
	return
}

func extractTag(tag *ast.BasicLit) string {
	// extract JSON tag
	re := regexp.MustCompile("`json:\"([a-zA-Z0-9_]+)(,omitempty)?\"`")
	m := re.FindStringSubmatch(tag.Value)
	if len(m) < 2 {
		return ""
	}
	return m[1]
}

func warn(a ...interface{}) {
	fmt.Fprintln(os.Stderr, a...)
}

func warnf(format string, a ...interface{}) {
	warn(fmt.Sprintf(format, a...) + "\n")
}

func fatal(a ...interface{}) {
	warn(a...)
	os.Exit(1)
}

func fatalf(format string, a ...interface{}) {
	warnf(format, a...)
	os.Exit(1)
}

func main() {
	flag.Parse()

	out, err := os.Create(outputPath)
	if err != nil {
		fatal(err)
	}
	defer out.Close()

	// write file header
	out.WriteString(header)

	fset := token.NewFileSet()

	f, err := parser.ParseFile(fset, "./ast/ast.go", nil, 0)
	if err != nil {
		fatal(err)
	}

	exprs := []DecoderTmpl{}
	stmts := []DecoderTmpl{}
	others := []DecoderTmpl{}

	for _, decl := range f.Decls {
		var genDecl *ast.GenDecl
		var ok bool
		if genDecl, ok = decl.(*ast.GenDecl); !ok {
			continue
		}

		for _, spec := range genDecl.Specs {
			var typeSpec *ast.TypeSpec
			var ok bool
			if typeSpec, ok = spec.(*ast.TypeSpec); !ok {
				continue
			}

			dec := DecoderTmpl{Name: typeSpec.Name.String(), Fields: []Field{}}

			var structType *ast.StructType
			if structType, ok = typeSpec.Type.(*ast.StructType); !ok {
				continue
			}
			var kind int
			for _, field := range structType.Fields.List {
				fieldTmpl := Field{}
				// XXX: handle compositions
				if len(field.Names) == 0 {
					continue
				}
				fieldTmpl.Name = field.Names[0].String()
				fieldTmpl.JSONName = extractTag(field.Tag)
				switch fieldTmpl.JSONName {
				case "expression_name":
					kind = Expression
				case "statement_name":
					kind = Statement
				}
				if fieldTmpl.Type, fieldTmpl.Array, fieldTmpl.BasicType = extractType(field); fieldTmpl.Type == "" {
					warnf("invalid type for %s.%s", dec.Name, fieldTmpl.Name)
					continue
				}
				dec.Fields = append(dec.Fields, fieldTmpl)
			}

			switch kind {
			case Expression:
				exprs = append(exprs, dec)
			case Statement:
				stmts = append(stmts, dec)
			default:
				others = append(others, dec)
			}
		}
	}

	// generate code
	if err := genExprs(out, exprs); err != nil {
		fatal(err)
	}
	if err := genStmts(out, stmts); err != nil {
		fatal(err)
	}
	if err := genStmts(out, stmts); err != nil {
		fatal(err)
	}
}
<|endoftext|>


<commit_before>package gofetcher

import (
	"github.com/cocaine/cocaine-framework-go/cocaine"
	"github.com/ugorji/go/codec"
	"net/http"
	"io/ioutil"
	"strconv"
	"time"
	"fmt"
	"errors"
	"bytes"
	"io"
	"reflect"
)

const (
	DefaultTimeout = 5000
	DefaultFollowRedirects = true
)

type Gofetcher struct {
	logger *cocaine.Logger
}

type Cookies map[string]string

type Request struct {
	method    string
	url       string
	body	io.Reader
	timeout	int64
	cookies	Cookies
	headers http.Header
	followRedirects bool
}

type responseAndError struct{
	res *http.Response
	err error
}

type Response struct {
	httpResponse	*http.Response
	body	[]byte
	header	http.Header
	runtime time.Duration
}

func NewGofetcher() *Gofetcher {
	logger, err := cocaine.NewLogger()
	if err != nil {
		logger.Err(fmt.Sprintf("Could not initialize logger due to error: %v", err))
		return nil
	}
	gofetcher := Gofetcher{logger}
	return &gofetcher
}

func noRedirect(_ *http.Request, via []*http.Request) error {
	if len(via) > 0 {
		return errors.New("stopped after first redirect")
	}
	return nil
}

func (gofetcher *Gofetcher) performRequest(request *Request) (*Response, error) {
	var (
		err 			error
		httpRequest    *http.Request
		httpResponse   *http.Response
		requestTimeout	time.Duration = time.Duration(request.timeout) * time.Millisecond
	)
	gofetcher.logger.Info(fmt.Sprintf("Requested url: %s, method: %s, timeout: %d, headers: %v",
		request.url, request.method, request.timeout, request.headers))
	httpClient := &http.Client{}
	if request.followRedirects == false {
		httpClient.CheckRedirect = noRedirect
	}
	httpRequest, err = http.NewRequest(request.method, request.url, request.body)
	if err != nil {
		return nil, err
	}
	for name, value := range request.cookies{
		httpRequest.AddCookie(&http.Cookie{Name:name, Value:value})
	}
	httpRequest.Header = request.headers
	resultChan := make(chan responseAndError)
	started := time.Now()
	go func (){
		res, err := httpClient.Do(httpRequest)
		if err != nil {
			resultChan <- responseAndError{nil, err}
		} else {
			resultChan <- responseAndError{res, err}
		}
	}()
	// http connection stay active after timeout exceeded, cause we can't close it using current client api.
	// Read more about timeouts: https://code.google.com/p/go/issues/detail?id=3362
	//
	select {
		case result := <- resultChan:
			httpResponse, err = result.res, result.err
		case <- time.After(requestTimeout):
			err = errors.New(fmt.Sprintf("Request timeout[%s] exceeded", requestTimeout.String()))
	}
	if err != nil {
		return nil, err
	}
	defer httpResponse.Body.Close()
	body, err := ioutil.ReadAll(httpResponse.Body)
	if err != nil {
		return nil, err
	}
	runtime := time.Since(started)
	response := &Response{httpResponse: httpResponse, body: body, header: httpResponse.Header, runtime: runtime}
	return response, nil

}

// Normal methods

func parseHeaders(rawHeaders map[string]interface {}) http.Header{
	headers := make(http.Header)
	for name, values := range rawHeaders {
		for _, value := range values.([]interface {}) {
			headers.Add(name, string(value.([]uint8))) // to transform in canonical form
		}
	}
	return headers
}

func parseCookies(rawCookie map[string]interface {}) Cookies{
	cookies := Cookies{}
	for key, value := range rawCookie {
		cookies[key] = string(value.([]uint8))
	}
	return cookies
}

func parseTimeout(rawTimeout interface {}) (timeout int64) {
	// is it possible to got timeout in int64 instead of uint64?
	switch rawTimeout.(type) {
	case uint64:
		timeout = int64(rawTimeout.(uint64))
	case int64:
		timeout = rawTimeout.(int64)
	}
	return timeout
}

func (gofetcher *Gofetcher) parseRequest(method string, requestBody []byte) (request *Request){
	var (
		mh codec.MsgpackHandle
		h = &mh
		timeout int64=DefaultTimeout
		cookies Cookies
		headers = make(http.Header)
		followRedirects bool=DefaultFollowRedirects
		body *bytes.Buffer
	)
	mh.MapType = reflect.TypeOf(map[string]interface{}(nil))
	var res []interface{}
	codec.NewDecoderBytes(requestBody, h).Decode(&res)
	url := string(res[0].([]uint8))
	switch {
	case method == "GET" || method == "HEAD" || method == "DELETE":
		if len(res) > 1 {
			timeout = parseTimeout(res[1])
		}
		if len(res) > 2 {
			cookies = parseCookies(res[2].(map[string]interface{}))
		}
		if len(res) > 3 {
			headers = parseHeaders(res[3].(map[string]interface{}))
		}
		if len(res) > 4 {
			followRedirects = res[4].(bool)
		}
	case method == "POST" || method == "PUT" || method == "PATCH":
		if len(res) > 1 {
			body = bytes.NewBuffer(res[1].([]byte))
		}
		if len(res) > 2 {
			timeout = parseTimeout(res[2])
		}
		if len(res) > 3 {
			cookies = parseCookies(res[3].(map[string]interface{}))
		}
		if len(res) > 4 {
			headers = parseHeaders(res[4].(map[string]interface{}))
		}
		if len(res) > 5 {
			followRedirects = res[5].(bool)
		}
	}

	request = &Request{method: method, url:url, timeout:timeout,
		followRedirects: followRedirects,
		cookies:cookies, headers:headers}
	if body != nil {
		request.body = body
	}
	return request
}

func (gofetcher *Gofetcher) writeResponse(response *cocaine.Response, request *Request, resp *Response, err error){
	if err != nil {
		response.Write([]interface{}{false, err.Error(), 0, http.Header{}})
		gofetcher.logger.Err(fmt.Sprintf("Error occured: %v, while downloading %s",
			err.Error(), request.url))
	} else{
		response.Write([]interface{}{true, resp.body, resp.httpResponse.StatusCode, resp.header})
		gofetcher.logger.Info(fmt.Sprintf("Response code: %d, url: %s, runtime: %v",
			resp.httpResponse.StatusCode, request.url, resp.runtime))
		if resp.httpResponse.StatusCode >= 500 {
			gofetcher.logger.Err(fmt.Sprintf("Response code: %d , url: %s, runtime: %v",
				resp.httpResponse.StatusCode, request.url, resp.runtime))
		}

	}
}

func (gofetcher *Gofetcher) handler(method string, request *cocaine.Request, response *cocaine.Response){
	defer response.Close()
	requestBody := <- request.Read()
	httpRequest := gofetcher.parseRequest(method, requestBody)
	resp, err := gofetcher.performRequest(httpRequest)
	gofetcher.writeResponse(response, httpRequest, resp, err)
}

func (gofetcher *Gofetcher) GetHandler(method string) func(request *cocaine.Request, response *cocaine.Response) {
	return func (request *cocaine.Request, response *cocaine.Response){
		gofetcher.handler(method, request, response)
	}
}

// Http methods

func (gofetcher *Gofetcher) writeHttpResponse(response *cocaine.Response, statusCode int,
	data interface{}, headers cocaine.Headers) {
	response.Write(cocaine.WriteHead(statusCode, headers))
	response.Write(data)
	gofetcher.logger.Info("Http response writed")

}

func (gofetcher *Gofetcher) HttpProxy(request *cocaine.Request, response *cocaine.Response){
	defer response.Close()
	var (
		timeout int64 = DefaultTimeout
	)
	req, err := cocaine.UnpackProxyRequest(<-request.Read())
	if err != nil {
		gofetcher.writeHttpResponse(response, 500, "Could not unpack request to http request",
			cocaine.Headers{{"Content-Type", "text/html"}})
		gofetcher.logger.Err("Could not unpack request to http request")

	} else {
		url := req.FormValue("url")
		timeoutArg := req.FormValue("timeout")
		if timeoutArg != "" {
			tout, _ := strconv.Atoi(timeoutArg)
			timeout = int64(tout)
		}
		httpRequest := Request{method:req.Method, url:url, timeout:timeout,
			followRedirects:DefaultFollowRedirects, headers: req.Header, body: req.Body}
		resp, err := gofetcher.performRequest(&httpRequest)
		if err != nil {
			gofetcher.writeHttpResponse(response, 500, err.Error(), cocaine.Headers{{"Content-Type", "text/html"}})
			gofetcher.logger.Err("Gofetcher error: " + err.Error())

		} else {
			gofetcher.writeHttpResponse(response, 200, resp.body, cocaine.HttpHeaderToCocaineHeader(resp.header))
		}
	}
}

func (gofetcher *Gofetcher) HttpEcho(request *cocaine.Request, response *cocaine.Response){
	req, err := cocaine.UnpackProxyRequest(<-request.Read())
	if err != nil {
		gofetcher.writeHttpResponse(response, 500, "Could not unpack request to http request",
			cocaine.Headers{{"Content-Type", "text/html"}})
	} else {
		text := req.FormValue("text")
		gofetcher.writeHttpResponse(response, 200, text, cocaine.Headers{{"Content-Type", "text/html"}})
	}
}
<commit_msg>fix goroutines leak<commit_after>package gofetcher

import (
	"github.com/cocaine/cocaine-framework-go/cocaine"
	"github.com/ugorji/go/codec"
	"net/http"
	"io/ioutil"
	"strconv"
	"time"
	"fmt"
	"errors"
	"bytes"
	"io"
	"reflect"
)

const (
	DefaultTimeout = 5000
	DefaultFollowRedirects = true
)

type Gofetcher struct {
	logger *cocaine.Logger
}

type Cookies map[string]string

type Request struct {
	method    string
	url       string
	body	io.Reader
	timeout	int64
	cookies	Cookies
	headers http.Header
	followRedirects bool
}

type responseAndError struct{
	res *http.Response
	err error
}

type Response struct {
	httpResponse	*http.Response
	body	[]byte
	header	http.Header
	runtime time.Duration
}

func NewGofetcher() *Gofetcher {
	logger, err := cocaine.NewLogger()
	if err != nil {
		logger.Err(fmt.Sprintf("Could not initialize logger due to error: %v", err))
		return nil
	}
	gofetcher := Gofetcher{logger}
	return &gofetcher
}

func noRedirect(_ *http.Request, via []*http.Request) error {
	if len(via) > 0 {
		return errors.New("stopped after first redirect")
	}
	return nil
}

func (gofetcher *Gofetcher) performRequest(request *Request) (*Response, error) {
	var (
		err 			error
		httpRequest    *http.Request
		httpResponse   *http.Response
		requestTimeout	time.Duration = time.Duration(request.timeout) * time.Millisecond
	)
	gofetcher.logger.Info(fmt.Sprintf("Requested url: %s, method: %s, timeout: %d, headers: %v",
		request.url, request.method, request.timeout, request.headers))
	httpClient := &http.Client{}
	if request.followRedirects == false {
		httpClient.CheckRedirect = noRedirect
	}
	httpRequest, err = http.NewRequest(request.method, request.url, request.body)
	if err != nil {
		return nil, err
	}
	for name, value := range request.cookies{
		httpRequest.AddCookie(&http.Cookie{Name:name, Value:value})
	}
	httpRequest.Header = request.headers
	resultChan := make(chan responseAndError)
	started := time.Now()
	go func (){
		res, err := httpClient.Do(httpRequest)
		if err != nil {
			resultChan <- responseAndError{nil, err}
		} else {
			resultChan <- responseAndError{res, err}
		}
	}()
	// http connection stay active after timeout exceeded, cause we can't close it using current client api.
	// Read more about timeouts: https://code.google.com/p/go/issues/detail?id=3362
	//
	select {
	case result := <- resultChan:
		httpResponse, err = result.res, result.err
	case <- time.After(requestTimeout):
		err = errors.New(fmt.Sprintf("Request timeout[%s] exceeded", requestTimeout.String()))
		go func(){
			// close httpResponse when it ready
			result := <- resultChan
			if result.res != nil {
				result.res.Body.Close()
			}
		}()
	}
	if err != nil {
		return nil, err
	}
	defer httpResponse.Body.Close()
	body, err := ioutil.ReadAll(httpResponse.Body)
	if err != nil {
		return nil, err
	}
	runtime := time.Since(started)
	response := &Response{httpResponse: httpResponse, body: body, header: httpResponse.Header, runtime: runtime}
	return response, nil

}

// Normal methods

func parseHeaders(rawHeaders map[string]interface {}) http.Header{
	headers := make(http.Header)
	for name, values := range rawHeaders {
		for _, value := range values.([]interface {}) {
			headers.Add(name, string(value.([]uint8))) // to transform in canonical form
		}
	}
	return headers
}

func parseCookies(rawCookie map[string]interface {}) Cookies{
	cookies := Cookies{}
	for key, value := range rawCookie {
		cookies[key] = string(value.([]uint8))
	}
	return cookies
}

func parseTimeout(rawTimeout interface {}) (timeout int64) {
	// is it possible to got timeout in int64 instead of uint64?
	switch rawTimeout.(type) {
	case uint64:
		timeout = int64(rawTimeout.(uint64))
	case int64:
		timeout = rawTimeout.(int64)
	}
	return timeout
}

func (gofetcher *Gofetcher) parseRequest(method string, requestBody []byte) (request *Request){
	var (
		mh codec.MsgpackHandle
		h = &mh
		timeout int64=DefaultTimeout
		cookies Cookies
		headers = make(http.Header)
		followRedirects bool=DefaultFollowRedirects
		body *bytes.Buffer
	)
	mh.MapType = reflect.TypeOf(map[string]interface{}(nil))
	var res []interface{}
	codec.NewDecoderBytes(requestBody, h).Decode(&res)
	url := string(res[0].([]uint8))
	switch {
	case method == "GET" || method == "HEAD" || method == "DELETE":
		if len(res) > 1 {
			timeout = parseTimeout(res[1])
		}
		if len(res) > 2 {
			cookies = parseCookies(res[2].(map[string]interface{}))
		}
		if len(res) > 3 {
			headers = parseHeaders(res[3].(map[string]interface{}))
		}
		if len(res) > 4 {
			followRedirects = res[4].(bool)
		}
	case method == "POST" || method == "PUT" || method == "PATCH":
		if len(res) > 1 {
			body = bytes.NewBuffer(res[1].([]byte))
		}
		if len(res) > 2 {
			timeout = parseTimeout(res[2])
		}
		if len(res) > 3 {
			cookies = parseCookies(res[3].(map[string]interface{}))
		}
		if len(res) > 4 {
			headers = parseHeaders(res[4].(map[string]interface{}))
		}
		if len(res) > 5 {
			followRedirects = res[5].(bool)
		}
	}

	request = &Request{method: method, url:url, timeout:timeout,
		followRedirects: followRedirects,
		cookies:cookies, headers:headers}
	if body != nil {
		request.body = body
	}
	return request
}

func (gofetcher *Gofetcher) writeResponse(response *cocaine.Response, request *Request, resp *Response, err error){
	if err != nil {
		response.Write([]interface{}{false, err.Error(), 0, http.Header{}})
		gofetcher.logger.Err(fmt.Sprintf("Error occured: %v, while downloading %s",
			err.Error(), request.url))
	} else{
		response.Write([]interface{}{true, resp.body, resp.httpResponse.StatusCode, resp.header})
		gofetcher.logger.Info(fmt.Sprintf("Response code: %d, url: %s, runtime: %v",
			resp.httpResponse.StatusCode, request.url, resp.runtime))
		if resp.httpResponse.StatusCode >= 500 {
			gofetcher.logger.Err(fmt.Sprintf("Response code: %d , url: %s, runtime: %v",
				resp.httpResponse.StatusCode, request.url, resp.runtime))
		}

	}
}

func (gofetcher *Gofetcher) handler(method string, request *cocaine.Request, response *cocaine.Response){
	defer response.Close()
	requestBody := <- request.Read()
	httpRequest := gofetcher.parseRequest(method, requestBody)
	resp, err := gofetcher.performRequest(httpRequest)
	gofetcher.writeResponse(response, httpRequest, resp, err)
}

func (gofetcher *Gofetcher) GetHandler(method string) func(request *cocaine.Request, response *cocaine.Response) {
	return func (request *cocaine.Request, response *cocaine.Response){
		gofetcher.handler(method, request, response)
	}
}

// Http methods

func (gofetcher *Gofetcher) writeHttpResponse(response *cocaine.Response, statusCode int,
	data interface{}, headers cocaine.Headers) {
	response.Write(cocaine.WriteHead(statusCode, headers))
	response.Write(data)
	gofetcher.logger.Info("Http response writed")

}

func (gofetcher *Gofetcher) HttpProxy(request *cocaine.Request, response *cocaine.Response){
	defer response.Close()
	var (
		timeout int64 = DefaultTimeout
	)
	req, err := cocaine.UnpackProxyRequest(<-request.Read())
	if err != nil {
		gofetcher.writeHttpResponse(response, 500, "Could not unpack request to http request",
			cocaine.Headers{{"Content-Type", "text/html"}})
		gofetcher.logger.Err("Could not unpack request to http request")

	} else {
		url := req.FormValue("url")
		timeoutArg := req.FormValue("timeout")
		if timeoutArg != "" {
			tout, _ := strconv.Atoi(timeoutArg)
			timeout = int64(tout)
		}
		httpRequest := Request{method:req.Method, url:url, timeout:timeout,
			followRedirects:DefaultFollowRedirects, headers: req.Header, body: req.Body}
		resp, err := gofetcher.performRequest(&httpRequest)
		if err != nil {
			gofetcher.writeHttpResponse(response, 500, err.Error(), cocaine.Headers{{"Content-Type", "text/html"}})
			gofetcher.logger.Err("Gofetcher error: " + err.Error())

		} else {
			gofetcher.writeHttpResponse(response, 200, resp.body, cocaine.HttpHeaderToCocaineHeader(resp.header))
		}
	}
}

func (gofetcher *Gofetcher) HttpEcho(request *cocaine.Request, response *cocaine.Response){
	req, err := cocaine.UnpackProxyRequest(<-request.Read())
	if err != nil {
		gofetcher.writeHttpResponse(response, 500, "Could not unpack request to http request",
			cocaine.Headers{{"Content-Type", "text/html"}})
	} else {
		text := req.FormValue("text")
		gofetcher.writeHttpResponse(response, 200, text, cocaine.Headers{{"Content-Type", "text/html"}})
	}
}
<|endoftext|>


<commit_before>package main

import (
	"github.com/cocaine/cocaine-framework-go/cocaine"
	"github.com/ugorji/go/codec"
	"net/http"
	"io/ioutil"
	"strconv"
	"time"
	"fmt"
	"errors"
	"bytes"
	"io"
	"reflect"
)

const (
	DefaultTimeout = 5000
	DefaultFollowRedirects = true
)

type Cookies map[string]string

type Request struct {
	method    string
	url       string
	body	io.Reader
	timeout	int64
	cookies	Cookies
	headers http.Header
	followRedirects bool
}

type responseAndError struct{
	res *http.Response
	err error
}

type Response struct {
	httpResponse	*http.Response
	body	[]byte
	header	http.Header
}

func noRedirect(_ *http.Request, via []*http.Request) error {
	if len(via) > 0 {
		return errors.New("stopped after first redirect")
	}
	return nil
}

func performRequest(request *Request) (*Response, error) {
	var (
		err 			error
		httpRequest    *http.Request
		httpResponse   *http.Response
		requestTimeout	time.Duration = time.Duration(request.timeout) * time.Millisecond
	)
	logger.Info(fmt.Sprintf("Request url %s, method %s, timeout %d", request.url, request.method, request.timeout))
	httpClient := &http.Client{}
	if request.followRedirects == false {
		httpClient.CheckRedirect = noRedirect
	}
	httpRequest, err = http.NewRequest(request.method, request.url, request.body)
	if err != nil {
		return nil, err
	}
	resultChan := make(chan responseAndError)
	go func (){
		res, err := httpClient.Do(httpRequest)
		if err != nil {
			resultChan <- responseAndError{nil, err}
		} else {
			resultChan <- responseAndError{res, err}
		}
	}()
	// http connection stay active after timeout exceeded, cause we can't close it using current client api.
	// Read more about timeouts: https://code.google.com/p/go/issues/detail?id=3362
	//
	select {
		case result := <- resultChan:
			httpResponse, err = result.res, result.err
		case <- time.After(requestTimeout):
			err = errors.New(fmt.Sprintf("Request timeout[%s] exceeded", requestTimeout.String()))
	}
	if err != nil {
		return nil, err
	}
	defer httpResponse.Body.Close()
	body, err := ioutil.ReadAll(httpResponse.Body)
	if err != nil {
		return nil, err
	}
	response := &Response{httpResponse: httpResponse, body: body, header: httpResponse.Header}
	return response, nil

}

// Normal methods

func parseHeaders(rawHeaders map[string]interface {}) http.Header{
	headers := make(http.Header)
	for name, values := range rawHeaders {
		for _, value := range values.([]interface {}) {
			headers.Add(name, string(value.([]uint8))) // to transform in canonical form
		}
	}
	return headers
}

func parseCookies(rawCookie map[string]interface {}) Cookies{
	cookies := Cookies{}
	for key, value := range rawCookie {
		cookies[key] = string(value.([]uint8))
	}
	return cookies
}

func parseTimeout(rawTimeout interface {}) (timeout int64) {
	// is it possible to got timeout in int64 instead of uint64?
	switch rawTimeout.(type) {
	case uint64:
		timeout = int64(rawTimeout.(uint64))
	case int64:
		timeout = rawTimeout.(int64)
	}
	return timeout
}

func parseRequest(method string, requestBody []byte) (request *Request){
	var (
		mh codec.MsgpackHandle
		h = &mh
		timeout int64=DefaultTimeout
		cookies Cookies
		headers = make(http.Header)
		followRedirects bool=DefaultFollowRedirects
		body *bytes.Buffer
	)
	mh.MapType = reflect.TypeOf(map[string]interface{}(nil))
	var res []interface{}
	codec.NewDecoderBytes(requestBody, h).Decode(&res)
	url := string(res[0].([]uint8))
	switch {
	case method == "GET" || method == "HEAD" || method == "DELETE":
		if len(res) > 1 {
			timeout = parseTimeout(res[1])
		}
		if len(res) > 2 {
			cookies = parseCookies(res[2].(map[string]interface{}))
		}
		if len(res) > 3 {
			headers = parseHeaders(res[3].(map[string]interface{}))
		}
		if len(res) > 4 {
			followRedirects = res[4].(bool)
		}
	case method == "POST" || method == "PUT" || method == "PATCH":
		if len(res) > 1 {
			body = bytes.NewBuffer(res[1].([]byte))
		}
		if len(res) > 2 {
			timeout = parseTimeout(res[2])
		}
		if len(res) > 3 {
			cookies = parseCookies(res[3].(map[string]interface{}))
		}
		if len(res) > 4 {
			headers = parseHeaders(res[4].(map[string]interface{}))
		}
		if len(res) > 5 {
			followRedirects = res[5].(bool)
		}
	}

	request = &Request{method: method, url:url, timeout:timeout,
		followRedirects: followRedirects,
		cookies:cookies, headers:headers}
	if body != nil {
		request.body = body
	}
	return request
}

func writeResponse(response *cocaine.Response, resp *Response, err error){
	if err != nil {
		response.Write([]interface{}{false, err.Error(), 0, http.Header{}})
	} else{
		response.Write([]interface{}{true, resp.body, resp.httpResponse.StatusCode, resp.header})
	}
	response.Close()
}

func Handler(method string, request *cocaine.Request, response *cocaine.Response){
	requestBody := <- request.Read()
	httpRequest := parseRequest(method, requestBody)
	resp, err := performRequest(httpRequest)
	writeResponse(response, resp, err)
}

func GetHandler(method string) func(request *cocaine.Request, response *cocaine.Response) {
	return func (request *cocaine.Request, response *cocaine.Response){
		Handler(method, request, response)
	}
}

// Http methods

func writeHttpResponse(response *cocaine.Response, statusCode int, data interface{} , headers cocaine.Headers) {
	response.Write(cocaine.WriteHead(statusCode, headers))
	response.Write(data)
	response.Close()
}

func HttpProxy(request *cocaine.Request, response *cocaine.Response){
	logger.Info("Http handler requested")
	var (
		timeout int64 = DefaultTimeout
	)
	req := cocaine.UnpackProxyRequest(<-request.Read())
	url := req.FormValue("url")
	timeoutArg := req.FormValue("timeout")
	if timeoutArg != "" {
		tout, _ := strconv.Atoi(timeoutArg)
		timeout = int64(tout)
	}
	httpRequest := Request{method:req.Method, url:url, timeout:timeout,
		followRedirects:DefaultFollowRedirects, headers: req.Header, body: req.Body}
	resp, err := performRequest(&httpRequest)
	if err != nil {
		writeHttpResponse(response, 500, err.Error(), cocaine.Headers{{"Content-Type", "text/html"}})
	} else {
		writeHttpResponse(response, 200, resp.body, cocaine.HttpHeaderToCocaineHeader(resp.header))
	}
}
<commit_msg>add cookie and header<commit_after>package main

import (
	"github.com/cocaine/cocaine-framework-go/cocaine"
	"github.com/ugorji/go/codec"
	"net/http"
	"io/ioutil"
	"strconv"
	"time"
	"fmt"
	"errors"
	"bytes"
	"io"
	"reflect"
)

const (
	DefaultTimeout = 5000
	DefaultFollowRedirects = true
)

type Cookies map[string]string

type Request struct {
	method    string
	url       string
	body	io.Reader
	timeout	int64
	cookies	Cookies
	headers http.Header
	followRedirects bool
}

type responseAndError struct{
	res *http.Response
	err error
}

type Response struct {
	httpResponse	*http.Response
	body	[]byte
	header	http.Header
}

func noRedirect(_ *http.Request, via []*http.Request) error {
	if len(via) > 0 {
		return errors.New("stopped after first redirect")
	}
	return nil
}

func performRequest(request *Request) (*Response, error) {
	var (
		err 			error
		httpRequest    *http.Request
		httpResponse   *http.Response
		requestTimeout	time.Duration = time.Duration(request.timeout) * time.Millisecond
	)
	logger.Info(fmt.Sprintf("Request url %s, method %s, timeout %d", request.url, request.method, request.timeout))
	httpClient := &http.Client{}
	if request.followRedirects == false {
		httpClient.CheckRedirect = noRedirect
	}
	httpRequest, err = http.NewRequest(request.method, request.url, request.body)
	if err != nil {
		return nil, err
	}
	for name, value := range request.cookies{
		httpRequest.AddCookie(&http.Cookie{Name:name, Value:value})
	}
	httpRequest.Header = request.headers
	resultChan := make(chan responseAndError)
	go func (){
		res, err := httpClient.Do(httpRequest)
		if err != nil {
			resultChan <- responseAndError{nil, err}
		} else {
			resultChan <- responseAndError{res, err}
		}
	}()
	// http connection stay active after timeout exceeded, cause we can't close it using current client api.
	// Read more about timeouts: https://code.google.com/p/go/issues/detail?id=3362
	//
	select {
		case result := <- resultChan:
			httpResponse, err = result.res, result.err
		case <- time.After(requestTimeout):
			err = errors.New(fmt.Sprintf("Request timeout[%s] exceeded", requestTimeout.String()))
	}
	if err != nil {
		return nil, err
	}
	defer httpResponse.Body.Close()
	body, err := ioutil.ReadAll(httpResponse.Body)
	if err != nil {
		return nil, err
	}
	response := &Response{httpResponse: httpResponse, body: body, header: httpResponse.Header}
	return response, nil

}

// Normal methods

func parseHeaders(rawHeaders map[string]interface {}) http.Header{
	headers := make(http.Header)
	for name, values := range rawHeaders {
		for _, value := range values.([]interface {}) {
			headers.Add(name, string(value.([]uint8))) // to transform in canonical form
		}
	}
	return headers
}

func parseCookies(rawCookie map[string]interface {}) Cookies{
	cookies := Cookies{}
	for key, value := range rawCookie {
		cookies[key] = string(value.([]uint8))
	}
	return cookies
}

func parseTimeout(rawTimeout interface {}) (timeout int64) {
	// is it possible to got timeout in int64 instead of uint64?
	switch rawTimeout.(type) {
	case uint64:
		timeout = int64(rawTimeout.(uint64))
	case int64:
		timeout = rawTimeout.(int64)
	}
	return timeout
}

func parseRequest(method string, requestBody []byte) (request *Request){
	var (
		mh codec.MsgpackHandle
		h = &mh
		timeout int64=DefaultTimeout
		cookies Cookies
		headers = make(http.Header)
		followRedirects bool=DefaultFollowRedirects
		body *bytes.Buffer
	)
	mh.MapType = reflect.TypeOf(map[string]interface{}(nil))
	var res []interface{}
	codec.NewDecoderBytes(requestBody, h).Decode(&res)
	url := string(res[0].([]uint8))
	switch {
	case method == "GET" || method == "HEAD" || method == "DELETE":
		if len(res) > 1 {
			timeout = parseTimeout(res[1])
		}
		if len(res) > 2 {
			cookies = parseCookies(res[2].(map[string]interface{}))
		}
		if len(res) > 3 {
			headers = parseHeaders(res[3].(map[string]interface{}))
		}
		if len(res) > 4 {
			followRedirects = res[4].(bool)
		}
	case method == "POST" || method == "PUT" || method == "PATCH":
		if len(res) > 1 {
			body = bytes.NewBuffer(res[1].([]byte))
		}
		if len(res) > 2 {
			timeout = parseTimeout(res[2])
		}
		if len(res) > 3 {
			cookies = parseCookies(res[3].(map[string]interface{}))
		}
		if len(res) > 4 {
			headers = parseHeaders(res[4].(map[string]interface{}))
		}
		if len(res) > 5 {
			followRedirects = res[5].(bool)
		}
	}

	request = &Request{method: method, url:url, timeout:timeout,
		followRedirects: followRedirects,
		cookies:cookies, headers:headers}
	if body != nil {
		request.body = body
	}
	return request
}

func writeResponse(response *cocaine.Response, resp *Response, err error){
	if err != nil {
		response.Write([]interface{}{false, err.Error(), 0, http.Header{}})
	} else{
		response.Write([]interface{}{true, resp.body, resp.httpResponse.StatusCode, resp.header})
	}
	response.Close()
}

func Handler(method string, request *cocaine.Request, response *cocaine.Response){
	requestBody := <- request.Read()
	httpRequest := parseRequest(method, requestBody)
	resp, err := performRequest(httpRequest)
	writeResponse(response, resp, err)
}

func GetHandler(method string) func(request *cocaine.Request, response *cocaine.Response) {
	return func (request *cocaine.Request, response *cocaine.Response){
		Handler(method, request, response)
	}
}

// Http methods

func writeHttpResponse(response *cocaine.Response, statusCode int, data interface{} , headers cocaine.Headers) {
	response.Write(cocaine.WriteHead(statusCode, headers))
	response.Write(data)
	response.Close()
}

func HttpProxy(request *cocaine.Request, response *cocaine.Response){
	logger.Info("Http handler requested")
	var (
		timeout int64 = DefaultTimeout
	)
	req := cocaine.UnpackProxyRequest(<-request.Read())
	url := req.FormValue("url")
	timeoutArg := req.FormValue("timeout")
	if timeoutArg != "" {
		tout, _ := strconv.Atoi(timeoutArg)
		timeout = int64(tout)
	}
	httpRequest := Request{method:req.Method, url:url, timeout:timeout,
		followRedirects:DefaultFollowRedirects, headers: req.Header, body: req.Body}
	resp, err := performRequest(&httpRequest)
	if err != nil {
		writeHttpResponse(response, 500, err.Error(), cocaine.Headers{{"Content-Type", "text/html"}})
	} else {
		writeHttpResponse(response, 200, resp.body, cocaine.HttpHeaderToCocaineHeader(resp.header))
	}
}
<|endoftext|>


<commit_before>// Copyright 2009 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 runtime

import (
	"unsafe"
)

type slice struct {
	array unsafe.Pointer
	len   int
	cap   int
}

// maxElems is a lookup table containing the maximum capacity for a slice.
// The index is the size of the slice element.
var maxElems = [...]uintptr{
	^uintptr(0),
	_MaxMem / 1, _MaxMem / 2, _MaxMem / 3, _MaxMem / 4,
	_MaxMem / 5, _MaxMem / 6, _MaxMem / 7, _MaxMem / 8,
	_MaxMem / 9, _MaxMem / 10, _MaxMem / 11, _MaxMem / 12,
	_MaxMem / 13, _MaxMem / 14, _MaxMem / 15, _MaxMem / 16,
	_MaxMem / 17, _MaxMem / 18, _MaxMem / 19, _MaxMem / 20,
	_MaxMem / 21, _MaxMem / 22, _MaxMem / 23, _MaxMem / 24,
	_MaxMem / 25, _MaxMem / 26, _MaxMem / 27, _MaxMem / 28,
	_MaxMem / 29, _MaxMem / 30, _MaxMem / 31, _MaxMem / 32,
}

// maxSliceCap returns the maximum capacity for a slice.
func maxSliceCap(elemsize uintptr) uintptr {
	if elemsize < uintptr(len(maxElems)) {
		return maxElems[elemsize]
	}
	return _MaxMem / elemsize
}

// TODO: take uintptrs instead of int64s?
func makeslice(et *_type, len64, cap64 int64) slice {
	// NOTE: The len > maxElements check here is not strictly necessary,
	// but it produces a 'len out of range' error instead of a 'cap out of range' error
	// when someone does make([]T, bignumber). 'cap out of range' is true too,
	// but since the cap is only being supplied implicitly, saying len is clearer.
	// See issue 4085.
	maxElements := maxSliceCap(et.size)
	len := int(len64)
	if len64 < 0 || int64(len) != len64 || uintptr(len) > maxElements {
		panic(errorString("makeslice: len out of range"))
	}

	cap := int(cap64)
	if cap < len || int64(cap) != cap64 || uintptr(cap) > maxElements {
		panic(errorString("makeslice: cap out of range"))
	}

	p := mallocgc(et.size*uintptr(cap), et, true)
	return slice{p, len, cap}
}

// growslice handles slice growth during append.
// It is passed the slice element type, the old slice, and the desired new minimum capacity,
// and it returns a new slice with at least that capacity, with the old data
// copied into it.
// The new slice's length is set to the old slice's length,
// NOT to the new requested capacity.
// This is for codegen convenience. The old slice's length is used immediately
// to calculate where to write new values during an append.
// TODO: When the old backend is gone, reconsider this decision.
// The SSA backend might prefer the new length or to return only ptr/cap and save stack space.
func growslice(et *_type, old slice, cap int) slice {
	if raceenabled {
		callerpc := getcallerpc(unsafe.Pointer(&et))
		racereadrangepc(old.array, uintptr(old.len*int(et.size)), callerpc, funcPC(growslice))
	}
	if msanenabled {
		msanread(old.array, uintptr(old.len*int(et.size)))
	}

	if et.size == 0 {
		if cap < old.cap {
			panic(errorString("growslice: cap out of range"))
		}
		// append should not create a slice with nil pointer but non-zero len.
		// We assume that append doesn't need to preserve old.array in this case.
		return slice{unsafe.Pointer(&zerobase), old.len, cap}
	}

	newcap := old.cap
	doublecap := newcap + newcap
	if cap > doublecap {
		newcap = cap
	} else {
		if old.len < 1024 {
			newcap = doublecap
		} else {
			for newcap < cap {
				newcap += newcap / 4
			}
		}
	}

	var lenmem, capmem uintptr
	const ptrSize = unsafe.Sizeof((*byte)(nil))
	switch et.size {
	case 1:
		lenmem = uintptr(old.len)
		capmem = roundupsize(uintptr(newcap))
		newcap = int(capmem)
	case ptrSize:
		lenmem = uintptr(old.len) * ptrSize
		capmem = roundupsize(uintptr(newcap) * ptrSize)
		newcap = int(capmem / ptrSize)
	default:
		lenmem = uintptr(old.len) * et.size
		capmem = roundupsize(uintptr(newcap) * et.size)
		newcap = int(capmem / et.size)
	}

	if cap < old.cap || uintptr(newcap) > maxSliceCap(et.size) {
		panic(errorString("growslice: cap out of range"))
	}

	var p unsafe.Pointer
	if et.kind&kindNoPointers != 0 {
		p = mallocgc(capmem, nil, false)
		memmove(p, old.array, lenmem)
		memclr(add(p, lenmem), capmem-lenmem)
	} else {
		// Note: can't use rawmem (which avoids zeroing of memory), because then GC can scan uninitialized memory.
		p = mallocgc(capmem, et, true)
		if !writeBarrier.enabled {
			memmove(p, old.array, lenmem)
		} else {
			for i := uintptr(0); i < lenmem; i += et.size {
				typedmemmove(et, add(p, i), add(old.array, i))
			}
		}
	}

	return slice{p, old.len, newcap}
}

func slicecopy(to, fm slice, width uintptr) int {
	if fm.len == 0 || to.len == 0 {
		return 0
	}

	n := fm.len
	if to.len < n {
		n = to.len
	}

	if width == 0 {
		return n
	}

	if raceenabled {
		callerpc := getcallerpc(unsafe.Pointer(&to))
		pc := funcPC(slicecopy)
		racewriterangepc(to.array, uintptr(n*int(width)), callerpc, pc)
		racereadrangepc(fm.array, uintptr(n*int(width)), callerpc, pc)
	}
	if msanenabled {
		msanwrite(to.array, uintptr(n*int(width)))
		msanread(fm.array, uintptr(n*int(width)))
	}

	size := uintptr(n) * width
	if size == 1 { // common case worth about 2x to do here
		// TODO: is this still worth it with new memmove impl?
		*(*byte)(to.array) = *(*byte)(fm.array) // known to be a byte pointer
	} else {
		memmove(to.array, fm.array, size)
	}
	return n
}

func slicestringcopy(to []byte, fm string) int {
	if len(fm) == 0 || len(to) == 0 {
		return 0
	}

	n := len(fm)
	if len(to) < n {
		n = len(to)
	}

	if raceenabled {
		callerpc := getcallerpc(unsafe.Pointer(&to))
		pc := funcPC(slicestringcopy)
		racewriterangepc(unsafe.Pointer(&to[0]), uintptr(n), callerpc, pc)
	}
	if msanenabled {
		msanwrite(unsafe.Pointer(&to[0]), uintptr(n))
	}

	memmove(unsafe.Pointer(&to[0]), stringStructOf(&fm).str, uintptr(n))
	return n
}
<commit_msg>runtime: don't call memmove if there's no work to do<commit_after>// Copyright 2009 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 runtime

import (
	"unsafe"
)

type slice struct {
	array unsafe.Pointer
	len   int
	cap   int
}

// maxElems is a lookup table containing the maximum capacity for a slice.
// The index is the size of the slice element.
var maxElems = [...]uintptr{
	^uintptr(0),
	_MaxMem / 1, _MaxMem / 2, _MaxMem / 3, _MaxMem / 4,
	_MaxMem / 5, _MaxMem / 6, _MaxMem / 7, _MaxMem / 8,
	_MaxMem / 9, _MaxMem / 10, _MaxMem / 11, _MaxMem / 12,
	_MaxMem / 13, _MaxMem / 14, _MaxMem / 15, _MaxMem / 16,
	_MaxMem / 17, _MaxMem / 18, _MaxMem / 19, _MaxMem / 20,
	_MaxMem / 21, _MaxMem / 22, _MaxMem / 23, _MaxMem / 24,
	_MaxMem / 25, _MaxMem / 26, _MaxMem / 27, _MaxMem / 28,
	_MaxMem / 29, _MaxMem / 30, _MaxMem / 31, _MaxMem / 32,
}

// maxSliceCap returns the maximum capacity for a slice.
func maxSliceCap(elemsize uintptr) uintptr {
	if elemsize < uintptr(len(maxElems)) {
		return maxElems[elemsize]
	}
	return _MaxMem / elemsize
}

// TODO: take uintptrs instead of int64s?
func makeslice(et *_type, len64, cap64 int64) slice {
	// NOTE: The len > maxElements check here is not strictly necessary,
	// but it produces a 'len out of range' error instead of a 'cap out of range' error
	// when someone does make([]T, bignumber). 'cap out of range' is true too,
	// but since the cap is only being supplied implicitly, saying len is clearer.
	// See issue 4085.
	maxElements := maxSliceCap(et.size)
	len := int(len64)
	if len64 < 0 || int64(len) != len64 || uintptr(len) > maxElements {
		panic(errorString("makeslice: len out of range"))
	}

	cap := int(cap64)
	if cap < len || int64(cap) != cap64 || uintptr(cap) > maxElements {
		panic(errorString("makeslice: cap out of range"))
	}

	p := mallocgc(et.size*uintptr(cap), et, true)
	return slice{p, len, cap}
}

// growslice handles slice growth during append.
// It is passed the slice element type, the old slice, and the desired new minimum capacity,
// and it returns a new slice with at least that capacity, with the old data
// copied into it.
// The new slice's length is set to the old slice's length,
// NOT to the new requested capacity.
// This is for codegen convenience. The old slice's length is used immediately
// to calculate where to write new values during an append.
// TODO: When the old backend is gone, reconsider this decision.
// The SSA backend might prefer the new length or to return only ptr/cap and save stack space.
func growslice(et *_type, old slice, cap int) slice {
	if raceenabled {
		callerpc := getcallerpc(unsafe.Pointer(&et))
		racereadrangepc(old.array, uintptr(old.len*int(et.size)), callerpc, funcPC(growslice))
	}
	if msanenabled {
		msanread(old.array, uintptr(old.len*int(et.size)))
	}

	if et.size == 0 {
		if cap < old.cap {
			panic(errorString("growslice: cap out of range"))
		}
		// append should not create a slice with nil pointer but non-zero len.
		// We assume that append doesn't need to preserve old.array in this case.
		return slice{unsafe.Pointer(&zerobase), old.len, cap}
	}

	newcap := old.cap
	doublecap := newcap + newcap
	if cap > doublecap {
		newcap = cap
	} else {
		if old.len < 1024 {
			newcap = doublecap
		} else {
			for newcap < cap {
				newcap += newcap / 4
			}
		}
	}

	var lenmem, capmem uintptr
	const ptrSize = unsafe.Sizeof((*byte)(nil))
	switch et.size {
	case 1:
		lenmem = uintptr(old.len)
		capmem = roundupsize(uintptr(newcap))
		newcap = int(capmem)
	case ptrSize:
		lenmem = uintptr(old.len) * ptrSize
		capmem = roundupsize(uintptr(newcap) * ptrSize)
		newcap = int(capmem / ptrSize)
	default:
		lenmem = uintptr(old.len) * et.size
		capmem = roundupsize(uintptr(newcap) * et.size)
		newcap = int(capmem / et.size)
	}

	if cap < old.cap || uintptr(newcap) > maxSliceCap(et.size) {
		panic(errorString("growslice: cap out of range"))
	}

	var p unsafe.Pointer
	if et.kind&kindNoPointers != 0 {
		p = mallocgc(capmem, nil, false)
		memmove(p, old.array, lenmem)
		memclr(add(p, lenmem), capmem-lenmem)
	} else {
		// Note: can't use rawmem (which avoids zeroing of memory), because then GC can scan uninitialized memory.
		p = mallocgc(capmem, et, true)
		if !writeBarrier.enabled {
			if lenmem > 0 {
				memmove(p, old.array, lenmem)
			}
		} else {
			for i := uintptr(0); i < lenmem; i += et.size {
				typedmemmove(et, add(p, i), add(old.array, i))
			}
		}
	}

	return slice{p, old.len, newcap}
}

func slicecopy(to, fm slice, width uintptr) int {
	if fm.len == 0 || to.len == 0 {
		return 0
	}

	n := fm.len
	if to.len < n {
		n = to.len
	}

	if width == 0 {
		return n
	}

	if raceenabled {
		callerpc := getcallerpc(unsafe.Pointer(&to))
		pc := funcPC(slicecopy)
		racewriterangepc(to.array, uintptr(n*int(width)), callerpc, pc)
		racereadrangepc(fm.array, uintptr(n*int(width)), callerpc, pc)
	}
	if msanenabled {
		msanwrite(to.array, uintptr(n*int(width)))
		msanread(fm.array, uintptr(n*int(width)))
	}

	size := uintptr(n) * width
	if size == 1 { // common case worth about 2x to do here
		// TODO: is this still worth it with new memmove impl?
		*(*byte)(to.array) = *(*byte)(fm.array) // known to be a byte pointer
	} else {
		memmove(to.array, fm.array, size)
	}
	return n
}

func slicestringcopy(to []byte, fm string) int {
	if len(fm) == 0 || len(to) == 0 {
		return 0
	}

	n := len(fm)
	if len(to) < n {
		n = len(to)
	}

	if raceenabled {
		callerpc := getcallerpc(unsafe.Pointer(&to))
		pc := funcPC(slicestringcopy)
		racewriterangepc(unsafe.Pointer(&to[0]), uintptr(n), callerpc, pc)
	}
	if msanenabled {
		msanwrite(unsafe.Pointer(&to[0]), uintptr(n))
	}

	memmove(unsafe.Pointer(&to[0]), stringStructOf(&fm).str, uintptr(n))
	return n
}
<|endoftext|>


<commit_before>// 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 runtime

import "unsafe"

// Declarations for runtime services implemented in C or assembly.

const ptrSize = 4 << (^uintptr(0) >> 63) // unsafe.Sizeof(uintptr(0)) but an ideal const
const regSize = 4 << (^uintreg(0) >> 63) // unsafe.Sizeof(uintreg(0)) but an ideal const

// Should be a built-in for unsafe.Pointer?
//go:nosplit
func add(p unsafe.Pointer, x uintptr) unsafe.Pointer {
	return unsafe.Pointer(uintptr(p) + x)
}

// n must be a power of 2
func roundup(p unsafe.Pointer, n uintptr) unsafe.Pointer {
	delta := -uintptr(p) & (n - 1)
	return unsafe.Pointer(uintptr(p) + delta)
}

// in runtime.c
func getg() *g
func acquirem() *m
func releasem(mp *m)
func gomcache() *mcache
func readgstatus(*g) uint32 // proc.c

// mcall switches from the g to the g0 stack and invokes fn(g),
// where g is the goroutine that made the call.
// mcall saves g's current PC/SP in g->sched so that it can be restored later.
// It is up to fn to arrange for that later execution, typically by recording
// g in a data structure, causing something to call ready(g) later.
// mcall returns to the original goroutine g later, when g has been rescheduled.
// fn must not return at all; typically it ends by calling schedule, to let the m
// run other goroutines.
//
// mcall can only be called from g stacks (not g0, not gsignal).
//go:noescape
func mcall(fn func(*g))

// onM switches from the g to the g0 stack and invokes fn().
// When fn returns, onM switches back to the g and returns,
// continuing execution on the g stack.
// If arguments must be passed to fn, they can be written to
// g->m->ptrarg (pointers) and g->m->scalararg (non-pointers)
// before the call and then consulted during fn.
// Similarly, fn can pass return values back in those locations.
// If fn is written in Go, it can be a closure, which avoids the need for
// ptrarg and scalararg entirely.
// After reading values out of ptrarg and scalararg it is conventional
// to zero them to avoid (memory or information) leaks.
//
// If onM is called from a g0 stack, it invokes fn and returns,
// without any stack switches.
//
// If onM is called from a gsignal stack, it crashes the program.
// The implication is that functions used in signal handlers must
// not use onM.
//
// NOTE(rsc): We could introduce a separate onMsignal that is
// like onM but if called from a gsignal stack would just run fn on
// that stack. The caller of onMsignal would be required to save the
// old values of ptrarg/scalararg and restore them when the call
// was finished, in case the signal interrupted an onM sequence
// in progress on the g or g0 stacks. Until there is a clear need for this,
// we just reject onM in signal handling contexts entirely.
//
//go:noescape
func onM(fn func())

// onMsignal is like onM but is allowed to be used in code that
// might run on the gsignal stack. Code running on a signal stack
// may be interrupting an onM sequence on the main stack, so
// if the onMsignal calling sequence writes to ptrarg/scalararg,
// it must first save the old values and then restore them when
// finished. As an exception to the rule, it is fine not to save and
// restore the values if the program is trying to crash rather than
// return from the signal handler.
// Once all the runtime is written in Go, there will be no ptrarg/scalararg
// and the distinction between onM and onMsignal (and perhaps mcall)
// can go away.
//
// If onMsignal is called from a gsignal stack, it invokes fn directly,
// without a stack switch. Otherwise onMsignal behaves like onM.
//
//go:noescape
func onM_signalok(fn func())

func badonm() {
	gothrow("onM called from signal goroutine")
}

// C functions that run on the M stack.
// Call using mcall.
func gosched_m(*g)
func park_m(*g)
func recovery_m(*g)

// More C functions that run on the M stack.
// Call using onM.
func mcacheRefill_m()
func largeAlloc_m()
func gc_m()
func scavenge_m()
func setFinalizer_m()
func removeFinalizer_m()
func markallocated_m()
func unrollgcprog_m()
func unrollgcproginplace_m()
func setgcpercent_m()
func setmaxthreads_m()
func ready_m()
func deferproc_m()
func goexit_m()
func startpanic_m()
func dopanic_m()
func readmemstats_m()
func writeheapdump_m()

// memclr clears n bytes starting at ptr.
// in memclr_*.s
//go:noescape
func memclr(ptr unsafe.Pointer, n uintptr)

// memmove copies n bytes from "from" to "to".
// in memmove_*.s
//go:noescape
func memmove(to unsafe.Pointer, from unsafe.Pointer, n uintptr)

func starttheworld()
func stoptheworld()
func newextram()
func lockOSThread()
func unlockOSThread()

// exported value for testing
var hashLoad = loadFactor

// in asm_*.s
func fastrand1() uint32

// in asm_*.s
//go:noescape
func memeq(a, b unsafe.Pointer, size uintptr) bool

// noescape hides a pointer from escape analysis.  noescape is
// the identity function but escape analysis doesn't think the
// output depends on the input.  noescape is inlined and currently
// compiles down to a single xor instruction.
// USE CAREFULLY!
//go:nosplit
func noescape(p unsafe.Pointer) unsafe.Pointer {
	x := uintptr(p)
	return unsafe.Pointer(x ^ 0)
}

func entersyscall()
func reentersyscall(pc uintptr, sp unsafe.Pointer)
func entersyscallblock()
func exitsyscall()

func cgocallback(fn, frame unsafe.Pointer, framesize uintptr)
func gogo(buf *gobuf)
func gosave(buf *gobuf)
func read(fd int32, p unsafe.Pointer, n int32) int32
func close(fd int32) int32
func mincore(addr unsafe.Pointer, n uintptr, dst *byte) int32

//go:noescape
func jmpdefer(fv *funcval, argp uintptr)
func exit1(code int32)
func asminit()
func setg(gg *g)
func exit(code int32)
func breakpoint()
func nanotime() int64
func usleep(usec uint32)

// careful: cputicks is not guaranteed to be monotonic!  In particular, we have
// noticed drift between cpus on certain os/arch combinations.  See issue 8976.
func cputicks() int64

func mmap(addr unsafe.Pointer, n uintptr, prot, flags, fd int32, off uint32) unsafe.Pointer
func munmap(addr unsafe.Pointer, n uintptr)
func madvise(addr unsafe.Pointer, n uintptr, flags int32)
func reflectcall(fn, arg unsafe.Pointer, n uint32, retoffset uint32)
func osyield()
func procyield(cycles uint32)
func cgocallback_gofunc(fv *funcval, frame unsafe.Pointer, framesize uintptr)
func readgogc() int32
func purgecachedstats(c *mcache)
func gostringnocopy(b *byte) string
func goexit()

//go:noescape
func write(fd uintptr, p unsafe.Pointer, n int32) int32

//go:noescape
func cas(ptr *uint32, old, new uint32) bool

//go:noescape
func casp(ptr *unsafe.Pointer, old, new unsafe.Pointer) bool

//go:noescape
func casuintptr(ptr *uintptr, old, new uintptr) bool

//go:noescape
func atomicstoreuintptr(ptr *uintptr, new uintptr)

//go:noescape
func atomicloaduintptr(ptr *uintptr) uintptr

//go:noescape
func atomicloaduint(ptr *uint) uint

//go:noescape
func setcallerpc(argp unsafe.Pointer, pc uintptr)

// getcallerpc returns the program counter (PC) of its caller's caller.
// getcallersp returns the stack pointer (SP) of its caller's caller.
// For both, the argp must be a pointer to the caller's first function argument.
// The implementation may or may not use argp, depending on
// the architecture.
//
// For example:
//
//	func f(arg1, arg2, arg3 int) {
//		pc := getcallerpc(unsafe.Pointer(&arg1))
//		sp := getcallerpc(unsafe.Pointer(&arg2))
//	}
//
// These two lines find the PC and SP immediately following
// the call to f (where f will return).
//
// The call to getcallerpc and getcallersp must be done in the
// frame being asked about. It would not be correct for f to pass &arg1
// to another function g and let g call getcallerpc/getcallersp.
// The call inside g might return information about g's caller or
// information about f's caller or complete garbage.
//
// The result of getcallersp is correct at the time of the return,
// but it may be invalidated by any subsequent call to a function
// that might relocate the stack in order to grow or shrink it.
// A general rule is that the result of getcallersp should be used
// immediately and can only be passed to nosplit functions.

//go:noescape
func getcallerpc(argp unsafe.Pointer) uintptr

//go:noescape
func getcallersp(argp unsafe.Pointer) uintptr

//go:noescape
func asmcgocall(fn, arg unsafe.Pointer)

//go:noescape
func asmcgocall_errno(fn, arg unsafe.Pointer) int32

//go:noescape
func open(name *byte, mode, perm int32) int32

//go:noescape
func gotraceback(*bool) int32

const _NoArgs = ^uintptr(0)

func newstack()
func newproc()
func morestack()
func mstart()
func rt0_go()

// return0 is a stub used to return 0 from deferproc.
// It is called at the very end of deferproc to signal
// the calling Go function that it should not jump
// to deferreturn.
// in asm_*.s
func return0()

// thunk to call time.now.
func timenow() (sec int64, nsec int32)

// in asm_*.s
// not called directly; definitions here supply type information for traceback.
func call16(fn, arg unsafe.Pointer, n, retoffset uint32)
func call32(fn, arg unsafe.Pointer, n, retoffset uint32)
func call64(fn, arg unsafe.Pointer, n, retoffset uint32)
func call128(fn, arg unsafe.Pointer, n, retoffset uint32)
func call256(fn, arg unsafe.Pointer, n, retoffset uint32)
func call512(fn, arg unsafe.Pointer, n, retoffset uint32)
func call1024(fn, arg unsafe.Pointer, n, retoffset uint32)
func call2048(fn, arg unsafe.Pointer, n, retoffset uint32)
func call4096(fn, arg unsafe.Pointer, n, retoffset uint32)
func call8192(fn, arg unsafe.Pointer, n, retoffset uint32)
func call16384(fn, arg unsafe.Pointer, n, retoffset uint32)
func call32768(fn, arg unsafe.Pointer, n, retoffset uint32)
func call65536(fn, arg unsafe.Pointer, n, retoffset uint32)
func call131072(fn, arg unsafe.Pointer, n, retoffset uint32)
func call262144(fn, arg unsafe.Pointer, n, retoffset uint32)
func call524288(fn, arg unsafe.Pointer, n, retoffset uint32)
func call1048576(fn, arg unsafe.Pointer, n, retoffset uint32)
func call2097152(fn, arg unsafe.Pointer, n, retoffset uint32)
func call4194304(fn, arg unsafe.Pointer, n, retoffset uint32)
func call8388608(fn, arg unsafe.Pointer, n, retoffset uint32)
func call16777216(fn, arg unsafe.Pointer, n, retoffset uint32)
func call33554432(fn, arg unsafe.Pointer, n, retoffset uint32)
func call67108864(fn, arg unsafe.Pointer, n, retoffset uint32)
func call134217728(fn, arg unsafe.Pointer, n, retoffset uint32)
func call268435456(fn, arg unsafe.Pointer, n, retoffset uint32)
func call536870912(fn, arg unsafe.Pointer, n, retoffset uint32)
func call1073741824(fn, arg unsafe.Pointer, n, retoffset uint32)
<commit_msg>runtime: fix getcallersp documentation<commit_after>// 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 runtime

import "unsafe"

// Declarations for runtime services implemented in C or assembly.

const ptrSize = 4 << (^uintptr(0) >> 63) // unsafe.Sizeof(uintptr(0)) but an ideal const
const regSize = 4 << (^uintreg(0) >> 63) // unsafe.Sizeof(uintreg(0)) but an ideal const

// Should be a built-in for unsafe.Pointer?
//go:nosplit
func add(p unsafe.Pointer, x uintptr) unsafe.Pointer {
	return unsafe.Pointer(uintptr(p) + x)
}

// n must be a power of 2
func roundup(p unsafe.Pointer, n uintptr) unsafe.Pointer {
	delta := -uintptr(p) & (n - 1)
	return unsafe.Pointer(uintptr(p) + delta)
}

// in runtime.c
func getg() *g
func acquirem() *m
func releasem(mp *m)
func gomcache() *mcache
func readgstatus(*g) uint32 // proc.c

// mcall switches from the g to the g0 stack and invokes fn(g),
// where g is the goroutine that made the call.
// mcall saves g's current PC/SP in g->sched so that it can be restored later.
// It is up to fn to arrange for that later execution, typically by recording
// g in a data structure, causing something to call ready(g) later.
// mcall returns to the original goroutine g later, when g has been rescheduled.
// fn must not return at all; typically it ends by calling schedule, to let the m
// run other goroutines.
//
// mcall can only be called from g stacks (not g0, not gsignal).
//go:noescape
func mcall(fn func(*g))

// onM switches from the g to the g0 stack and invokes fn().
// When fn returns, onM switches back to the g and returns,
// continuing execution on the g stack.
// If arguments must be passed to fn, they can be written to
// g->m->ptrarg (pointers) and g->m->scalararg (non-pointers)
// before the call and then consulted during fn.
// Similarly, fn can pass return values back in those locations.
// If fn is written in Go, it can be a closure, which avoids the need for
// ptrarg and scalararg entirely.
// After reading values out of ptrarg and scalararg it is conventional
// to zero them to avoid (memory or information) leaks.
//
// If onM is called from a g0 stack, it invokes fn and returns,
// without any stack switches.
//
// If onM is called from a gsignal stack, it crashes the program.
// The implication is that functions used in signal handlers must
// not use onM.
//
// NOTE(rsc): We could introduce a separate onMsignal that is
// like onM but if called from a gsignal stack would just run fn on
// that stack. The caller of onMsignal would be required to save the
// old values of ptrarg/scalararg and restore them when the call
// was finished, in case the signal interrupted an onM sequence
// in progress on the g or g0 stacks. Until there is a clear need for this,
// we just reject onM in signal handling contexts entirely.
//
//go:noescape
func onM(fn func())

// onMsignal is like onM but is allowed to be used in code that
// might run on the gsignal stack. Code running on a signal stack
// may be interrupting an onM sequence on the main stack, so
// if the onMsignal calling sequence writes to ptrarg/scalararg,
// it must first save the old values and then restore them when
// finished. As an exception to the rule, it is fine not to save and
// restore the values if the program is trying to crash rather than
// return from the signal handler.
// Once all the runtime is written in Go, there will be no ptrarg/scalararg
// and the distinction between onM and onMsignal (and perhaps mcall)
// can go away.
//
// If onMsignal is called from a gsignal stack, it invokes fn directly,
// without a stack switch. Otherwise onMsignal behaves like onM.
//
//go:noescape
func onM_signalok(fn func())

func badonm() {
	gothrow("onM called from signal goroutine")
}

// C functions that run on the M stack.
// Call using mcall.
func gosched_m(*g)
func park_m(*g)
func recovery_m(*g)

// More C functions that run on the M stack.
// Call using onM.
func mcacheRefill_m()
func largeAlloc_m()
func gc_m()
func scavenge_m()
func setFinalizer_m()
func removeFinalizer_m()
func markallocated_m()
func unrollgcprog_m()
func unrollgcproginplace_m()
func setgcpercent_m()
func setmaxthreads_m()
func ready_m()
func deferproc_m()
func goexit_m()
func startpanic_m()
func dopanic_m()
func readmemstats_m()
func writeheapdump_m()

// memclr clears n bytes starting at ptr.
// in memclr_*.s
//go:noescape
func memclr(ptr unsafe.Pointer, n uintptr)

// memmove copies n bytes from "from" to "to".
// in memmove_*.s
//go:noescape
func memmove(to unsafe.Pointer, from unsafe.Pointer, n uintptr)

func starttheworld()
func stoptheworld()
func newextram()
func lockOSThread()
func unlockOSThread()

// exported value for testing
var hashLoad = loadFactor

// in asm_*.s
func fastrand1() uint32

// in asm_*.s
//go:noescape
func memeq(a, b unsafe.Pointer, size uintptr) bool

// noescape hides a pointer from escape analysis.  noescape is
// the identity function but escape analysis doesn't think the
// output depends on the input.  noescape is inlined and currently
// compiles down to a single xor instruction.
// USE CAREFULLY!
//go:nosplit
func noescape(p unsafe.Pointer) unsafe.Pointer {
	x := uintptr(p)
	return unsafe.Pointer(x ^ 0)
}

func entersyscall()
func reentersyscall(pc uintptr, sp unsafe.Pointer)
func entersyscallblock()
func exitsyscall()

func cgocallback(fn, frame unsafe.Pointer, framesize uintptr)
func gogo(buf *gobuf)
func gosave(buf *gobuf)
func read(fd int32, p unsafe.Pointer, n int32) int32
func close(fd int32) int32
func mincore(addr unsafe.Pointer, n uintptr, dst *byte) int32

//go:noescape
func jmpdefer(fv *funcval, argp uintptr)
func exit1(code int32)
func asminit()
func setg(gg *g)
func exit(code int32)
func breakpoint()
func nanotime() int64
func usleep(usec uint32)

// careful: cputicks is not guaranteed to be monotonic!  In particular, we have
// noticed drift between cpus on certain os/arch combinations.  See issue 8976.
func cputicks() int64

func mmap(addr unsafe.Pointer, n uintptr, prot, flags, fd int32, off uint32) unsafe.Pointer
func munmap(addr unsafe.Pointer, n uintptr)
func madvise(addr unsafe.Pointer, n uintptr, flags int32)
func reflectcall(fn, arg unsafe.Pointer, n uint32, retoffset uint32)
func osyield()
func procyield(cycles uint32)
func cgocallback_gofunc(fv *funcval, frame unsafe.Pointer, framesize uintptr)
func readgogc() int32
func purgecachedstats(c *mcache)
func gostringnocopy(b *byte) string
func goexit()

//go:noescape
func write(fd uintptr, p unsafe.Pointer, n int32) int32

//go:noescape
func cas(ptr *uint32, old, new uint32) bool

//go:noescape
func casp(ptr *unsafe.Pointer, old, new unsafe.Pointer) bool

//go:noescape
func casuintptr(ptr *uintptr, old, new uintptr) bool

//go:noescape
func atomicstoreuintptr(ptr *uintptr, new uintptr)

//go:noescape
func atomicloaduintptr(ptr *uintptr) uintptr

//go:noescape
func atomicloaduint(ptr *uint) uint

//go:noescape
func setcallerpc(argp unsafe.Pointer, pc uintptr)

// getcallerpc returns the program counter (PC) of its caller's caller.
// getcallersp returns the stack pointer (SP) of its caller's caller.
// For both, the argp must be a pointer to the caller's first function argument.
// The implementation may or may not use argp, depending on
// the architecture.
//
// For example:
//
//	func f(arg1, arg2, arg3 int) {
//		pc := getcallerpc(unsafe.Pointer(&arg1))
//		sp := getcallersp(unsafe.Pointer(&arg1))
//	}
//
// These two lines find the PC and SP immediately following
// the call to f (where f will return).
//
// The call to getcallerpc and getcallersp must be done in the
// frame being asked about. It would not be correct for f to pass &arg1
// to another function g and let g call getcallerpc/getcallersp.
// The call inside g might return information about g's caller or
// information about f's caller or complete garbage.
//
// The result of getcallersp is correct at the time of the return,
// but it may be invalidated by any subsequent call to a function
// that might relocate the stack in order to grow or shrink it.
// A general rule is that the result of getcallersp should be used
// immediately and can only be passed to nosplit functions.

//go:noescape
func getcallerpc(argp unsafe.Pointer) uintptr

//go:noescape
func getcallersp(argp unsafe.Pointer) uintptr

//go:noescape
func asmcgocall(fn, arg unsafe.Pointer)

//go:noescape
func asmcgocall_errno(fn, arg unsafe.Pointer) int32

//go:noescape
func open(name *byte, mode, perm int32) int32

//go:noescape
func gotraceback(*bool) int32

const _NoArgs = ^uintptr(0)

func newstack()
func newproc()
func morestack()
func mstart()
func rt0_go()

// return0 is a stub used to return 0 from deferproc.
// It is called at the very end of deferproc to signal
// the calling Go function that it should not jump
// to deferreturn.
// in asm_*.s
func return0()

// thunk to call time.now.
func timenow() (sec int64, nsec int32)

// in asm_*.s
// not called directly; definitions here supply type information for traceback.
func call16(fn, arg unsafe.Pointer, n, retoffset uint32)
func call32(fn, arg unsafe.Pointer, n, retoffset uint32)
func call64(fn, arg unsafe.Pointer, n, retoffset uint32)
func call128(fn, arg unsafe.Pointer, n, retoffset uint32)
func call256(fn, arg unsafe.Pointer, n, retoffset uint32)
func call512(fn, arg unsafe.Pointer, n, retoffset uint32)
func call1024(fn, arg unsafe.Pointer, n, retoffset uint32)
func call2048(fn, arg unsafe.Pointer, n, retoffset uint32)
func call4096(fn, arg unsafe.Pointer, n, retoffset uint32)
func call8192(fn, arg unsafe.Pointer, n, retoffset uint32)
func call16384(fn, arg unsafe.Pointer, n, retoffset uint32)
func call32768(fn, arg unsafe.Pointer, n, retoffset uint32)
func call65536(fn, arg unsafe.Pointer, n, retoffset uint32)
func call131072(fn, arg unsafe.Pointer, n, retoffset uint32)
func call262144(fn, arg unsafe.Pointer, n, retoffset uint32)
func call524288(fn, arg unsafe.Pointer, n, retoffset uint32)
func call1048576(fn, arg unsafe.Pointer, n, retoffset uint32)
func call2097152(fn, arg unsafe.Pointer, n, retoffset uint32)
func call4194304(fn, arg unsafe.Pointer, n, retoffset uint32)
func call8388608(fn, arg unsafe.Pointer, n, retoffset uint32)
func call16777216(fn, arg unsafe.Pointer, n, retoffset uint32)
func call33554432(fn, arg unsafe.Pointer, n, retoffset uint32)
func call67108864(fn, arg unsafe.Pointer, n, retoffset uint32)
func call134217728(fn, arg unsafe.Pointer, n, retoffset uint32)
func call268435456(fn, arg unsafe.Pointer, n, retoffset uint32)
func call536870912(fn, arg unsafe.Pointer, n, retoffset uint32)
func call1073741824(fn, arg unsafe.Pointer, n, retoffset uint32)
<|endoftext|>