From cfcbb4227fb20191e04eb8d7766ae6202f526afd Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Wed, 23 Dec 2020 09:57:37 -0800 Subject: libgo: update to Go1.16beta1 release This does not yet include support for the //go:embed directive added in this release. * Makefile.am (check-runtime): Don't create check-runtime-dir. (mostlyclean-local): Don't remove check-runtime-dir. (check-go-tool, check-vet): Copy in go.mod and modules.txt. (check-cgo-test, check-carchive-test): Add go.mod file. * Makefile.in: Regenerate. Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/280172 --- libgo/go/html/template/clone_test.go | 14 +-- libgo/go/html/template/escape.go | 2 + libgo/go/html/template/examplefiles_test.go | 3 +- libgo/go/html/template/exec_test.go | 80 ++++++++++++++- libgo/go/html/template/multi_test.go | 43 ++++++++ libgo/go/html/template/template.go | 150 ++++++++++++++++++++++++---- libgo/go/html/template/template_test.go | 16 +++ libgo/go/html/template/testdata/fs.zip | Bin 0 -> 406 bytes 8 files changed, 274 insertions(+), 34 deletions(-) create mode 100644 libgo/go/html/template/testdata/fs.zip (limited to 'libgo/go/html') diff --git a/libgo/go/html/template/clone_test.go b/libgo/go/html/template/clone_test.go index c9c619f..7cb1b9c 100644 --- a/libgo/go/html/template/clone_test.go +++ b/libgo/go/html/template/clone_test.go @@ -8,7 +8,7 @@ import ( "bytes" "errors" "fmt" - "io/ioutil" + "io" "strings" "sync" "testing" @@ -171,7 +171,7 @@ func TestCloneThenParse(t *testing.T) { t.Error("adding a template to a clone added it to the original") } // double check that the embedded template isn't available in the original - err := t0.ExecuteTemplate(ioutil.Discard, "a", nil) + err := t0.ExecuteTemplate(io.Discard, "a", nil) if err == nil { t.Error("expected 'no such template' error") } @@ -185,13 +185,13 @@ func TestFuncMapWorksAfterClone(t *testing.T) { // get the expected error output (no clone) uncloned := Must(New("").Funcs(funcs).Parse("{{customFunc}}")) - wantErr := uncloned.Execute(ioutil.Discard, nil) + wantErr := uncloned.Execute(io.Discard, nil) // toClone must be the same as uncloned. It has to be recreated from scratch, // since cloning cannot occur after execution. toClone := Must(New("").Funcs(funcs).Parse("{{customFunc}}")) cloned := Must(toClone.Clone()) - gotErr := cloned.Execute(ioutil.Discard, nil) + gotErr := cloned.Execute(io.Discard, nil) if wantErr.Error() != gotErr.Error() { t.Errorf("clone error message mismatch want %q got %q", wantErr, gotErr) @@ -213,7 +213,7 @@ func TestTemplateCloneExecuteRace(t *testing.T) { go func() { defer wg.Done() for i := 0; i < 100; i++ { - if err := tmpl.Execute(ioutil.Discard, "data"); err != nil { + if err := tmpl.Execute(io.Discard, "data"); err != nil { panic(err) } } @@ -237,7 +237,7 @@ func TestCloneGrowth(t *testing.T) { tmpl = Must(tmpl.Clone()) Must(tmpl.Parse(`{{define "B"}}Text{{end}}`)) for i := 0; i < 10; i++ { - tmpl.Execute(ioutil.Discard, nil) + tmpl.Execute(io.Discard, nil) } if len(tmpl.DefinedTemplates()) > 200 { t.Fatalf("too many templates: %v", len(tmpl.DefinedTemplates())) @@ -257,7 +257,7 @@ func TestCloneRedefinedName(t *testing.T) { for i := 0; i < 2; i++ { t2 := Must(t1.Clone()) t2 = Must(t2.New(fmt.Sprintf("%d", i)).Parse(page)) - err := t2.Execute(ioutil.Discard, nil) + err := t2.Execute(io.Discard, nil) if err != nil { t.Fatal(err) } diff --git a/libgo/go/html/template/escape.go b/libgo/go/html/template/escape.go index f12dafa..8739735 100644 --- a/libgo/go/html/template/escape.go +++ b/libgo/go/html/template/escape.go @@ -124,6 +124,8 @@ func (e *escaper) escape(c context, n parse.Node) context { switch n := n.(type) { case *parse.ActionNode: return e.escapeAction(c, n) + case *parse.CommentNode: + return c case *parse.IfNode: return e.escapeBranch(c, &n.BranchNode, "if") case *parse.ListNode: diff --git a/libgo/go/html/template/examplefiles_test.go b/libgo/go/html/template/examplefiles_test.go index 60518ae..5eb2597 100644 --- a/libgo/go/html/template/examplefiles_test.go +++ b/libgo/go/html/template/examplefiles_test.go @@ -6,7 +6,6 @@ package template_test import ( "io" - "io/ioutil" "log" "os" "path/filepath" @@ -20,7 +19,7 @@ type templateFile struct { } func createTestDir(files []templateFile) string { - dir, err := ioutil.TempDir("", "template") + dir, err := os.MkdirTemp("", "template") if err != nil { log.Fatal(err) } diff --git a/libgo/go/html/template/exec_test.go b/libgo/go/html/template/exec_test.go index ec2bfcc..eb00824 100644 --- a/libgo/go/html/template/exec_test.go +++ b/libgo/go/html/template/exec_test.go @@ -11,9 +11,10 @@ import ( "errors" "flag" "fmt" - "io/ioutil" + "io" "reflect" "strings" + "sync" "testing" "text/template" ) @@ -1302,7 +1303,7 @@ func TestUnterminatedStringError(t *testing.T) { t.Fatal("expected error") } str := err.Error() - if !strings.Contains(str, "X:3: unexpected unterminated raw quoted string") { + if !strings.Contains(str, "X:3: unterminated raw quoted string") { t.Fatalf("unexpected error: %s", str) } } @@ -1335,7 +1336,7 @@ func TestExecuteGivesExecError(t *testing.T) { if err != nil { t.Fatal(err) } - err = tmpl.Execute(ioutil.Discard, 0) + err = tmpl.Execute(io.Discard, 0) if err == nil { t.Fatal("expected error; got none") } @@ -1481,7 +1482,7 @@ func TestEvalFieldErrors(t *testing.T) { for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { tmpl := Must(New("tmpl").Parse(tc.src)) - err := tmpl.Execute(ioutil.Discard, tc.value) + err := tmpl.Execute(io.Discard, tc.value) got := "" if err != nil { got = err.Error() @@ -1498,7 +1499,7 @@ func TestMaxExecDepth(t *testing.T) { t.Skip("skipping in -short mode") } tmpl := Must(New("tmpl").Parse(`{{template "tmpl" .}}`)) - err := tmpl.Execute(ioutil.Discard, nil) + err := tmpl.Execute(io.Discard, nil) got := "" if err != nil { got = err.Error() @@ -1706,3 +1707,72 @@ func TestIssue31810(t *testing.T) { t.Errorf("%s got %q, expected %q", textCall, b.String(), "result") } } + +// Issue 39807. There was a race applying escapeTemplate. + +const raceText = ` +{{- define "jstempl" -}} +var v = "v"; +{{- end -}} + +` + +func TestEscapeRace(t *testing.T) { + tmpl := New("") + _, err := tmpl.New("templ.html").Parse(raceText) + if err != nil { + t.Fatal(err) + } + const count = 20 + for i := 0; i < count; i++ { + _, err := tmpl.New(fmt.Sprintf("x%d.html", i)).Parse(`{{ template "templ.html" .}}`) + if err != nil { + t.Fatal(err) + } + } + + var wg sync.WaitGroup + for i := 0; i < 10; i++ { + wg.Add(1) + go func() { + defer wg.Done() + for j := 0; j < count; j++ { + sub := tmpl.Lookup(fmt.Sprintf("x%d.html", j)) + if err := sub.Execute(io.Discard, nil); err != nil { + t.Error(err) + } + } + }() + } + wg.Wait() +} + +func TestRecursiveExecute(t *testing.T) { + tmpl := New("") + + recur := func() (HTML, error) { + var sb strings.Builder + if err := tmpl.ExecuteTemplate(&sb, "subroutine", nil); err != nil { + t.Fatal(err) + } + return HTML(sb.String()), nil + } + + m := FuncMap{ + "recur": recur, + } + + top, err := tmpl.New("x.html").Funcs(m).Parse(`{{recur}}`) + if err != nil { + t.Fatal(err) + } + _, err = tmpl.New("subroutine").Parse(``) + if err != nil { + t.Fatal(err) + } + if err := top.Execute(io.Discard, nil); err != nil { + t.Fatal(err) + } +} diff --git a/libgo/go/html/template/multi_test.go b/libgo/go/html/template/multi_test.go index 50526c5..6535ab6 100644 --- a/libgo/go/html/template/multi_test.go +++ b/libgo/go/html/template/multi_test.go @@ -7,7 +7,9 @@ package template import ( + "archive/zip" "bytes" + "os" "testing" "text/template/parse" ) @@ -82,6 +84,35 @@ func TestParseGlob(t *testing.T) { testExecute(multiExecTests, template, t) } +func TestParseFS(t *testing.T) { + fs := os.DirFS("testdata") + + { + _, err := ParseFS(fs, "DOES NOT EXIST") + if err == nil { + t.Error("expected error for non-existent file; got none") + } + } + + { + template := New("root") + _, err := template.ParseFS(fs, "file1.tmpl", "file2.tmpl") + if err != nil { + t.Fatalf("error parsing files: %v", err) + } + testExecute(multiExecTests, template, t) + } + + { + template := New("root") + _, err := template.ParseFS(fs, "file*.tmpl") + if err != nil { + t.Fatalf("error parsing files: %v", err) + } + testExecute(multiExecTests, template, t) + } +} + // In these tests, actual content (not just template definitions) comes from the parsed files. var templateFileExecTests = []execTest{ @@ -104,6 +135,18 @@ func TestParseGlobWithData(t *testing.T) { testExecute(templateFileExecTests, template, t) } +func TestParseZipFS(t *testing.T) { + z, err := zip.OpenReader("testdata/fs.zip") + if err != nil { + t.Fatalf("error parsing zip: %v", err) + } + template, err := New("root").ParseFS(z, "tmpl*.tmpl") + if err != nil { + t.Fatalf("error parsing files: %v", err) + } + testExecute(templateFileExecTests, template, t) +} + const ( cloneText1 = `{{define "a"}}{{template "b"}}{{template "c"}}{{end}}` cloneText2 = `{{define "b"}}b{{end}}` diff --git a/libgo/go/html/template/template.go b/libgo/go/html/template/template.go index 7543787..09d71d4 100644 --- a/libgo/go/html/template/template.go +++ b/libgo/go/html/template/template.go @@ -7,8 +7,11 @@ package template import ( "fmt" "io" - "io/ioutil" + "io/fs" + "os" + "path" "path/filepath" + "reflect" "sync" "text/template" "text/template/parse" @@ -24,7 +27,9 @@ type Template struct { // template's in sync. text *template.Template // The underlying template's parse tree, updated to be HTML-safe. - Tree *parse.Tree + Tree *parse.Tree + // The original functions, before wrapping. + funcMap FuncMap *nameSpace // common to all associated templates } @@ -33,7 +38,7 @@ var escapeOK = fmt.Errorf("template escaped correctly") // nameSpace is the data structure shared by all templates in an association. type nameSpace struct { - mu sync.Mutex + mu sync.RWMutex set map[string]*Template escaped bool esc escaper @@ -43,8 +48,8 @@ type nameSpace struct { // itself. func (t *Template) Templates() []*Template { ns := t.nameSpace - ns.mu.Lock() - defer ns.mu.Unlock() + ns.mu.RLock() + defer ns.mu.RUnlock() // Return a slice so we don't expose the map. m := make([]*Template, 0, len(ns.set)) for _, v := range ns.set { @@ -82,8 +87,8 @@ func (t *Template) checkCanParse() error { if t == nil { return nil } - t.nameSpace.mu.Lock() - defer t.nameSpace.mu.Unlock() + t.nameSpace.mu.RLock() + defer t.nameSpace.mu.RUnlock() if t.nameSpace.escaped { return fmt.Errorf("html/template: cannot Parse after Execute") } @@ -92,6 +97,16 @@ func (t *Template) checkCanParse() error { // escape escapes all associated templates. func (t *Template) escape() error { + t.nameSpace.mu.RLock() + escapeErr := t.escapeErr + t.nameSpace.mu.RUnlock() + if escapeErr != nil { + if escapeErr == escapeOK { + return nil + } + return escapeErr + } + t.nameSpace.mu.Lock() defer t.nameSpace.mu.Unlock() t.nameSpace.escaped = true @@ -119,6 +134,8 @@ func (t *Template) Execute(wr io.Writer, data interface{}) error { if err := t.escape(); err != nil { return err } + t.nameSpace.mu.RLock() + defer t.nameSpace.mu.RUnlock() return t.text.Execute(wr, data) } @@ -134,6 +151,8 @@ func (t *Template) ExecuteTemplate(wr io.Writer, name string, data interface{}) if err != nil { return err } + t.nameSpace.mu.RLock() + defer t.nameSpace.mu.RUnlock() return tmpl.text.Execute(wr, data) } @@ -141,13 +160,27 @@ func (t *Template) ExecuteTemplate(wr io.Writer, name string, data interface{}) // is escaped, or returns an error if it cannot be. It returns the named // template. func (t *Template) lookupAndEscapeTemplate(name string) (tmpl *Template, err error) { - t.nameSpace.mu.Lock() - defer t.nameSpace.mu.Unlock() - t.nameSpace.escaped = true + t.nameSpace.mu.RLock() tmpl = t.set[name] + var escapeErr error + if tmpl != nil { + escapeErr = tmpl.escapeErr + } + t.nameSpace.mu.RUnlock() + if tmpl == nil { return nil, fmt.Errorf("html/template: %q is undefined", name) } + if escapeErr != nil { + if escapeErr != escapeOK { + return nil, escapeErr + } + return tmpl, nil + } + + t.nameSpace.mu.Lock() + defer t.nameSpace.mu.Unlock() + t.nameSpace.escaped = true if tmpl.escapeErr != nil && tmpl.escapeErr != escapeOK { return nil, tmpl.escapeErr } @@ -227,6 +260,7 @@ func (t *Template) AddParseTree(name string, tree *parse.Tree) (*Template, error nil, text, text.Tree, + nil, t.nameSpace, } t.set[name] = ret @@ -257,8 +291,10 @@ func (t *Template) Clone() (*Template, error) { nil, textClone, textClone.Tree, + t.funcMap, ns, } + ret.wrapFuncs() ret.set[ret.Name()] = ret for _, x := range textClone.Templates() { name := x.Name() @@ -267,12 +303,15 @@ func (t *Template) Clone() (*Template, error) { return nil, fmt.Errorf("html/template: cannot Clone %q after it has executed", t.Name()) } x.Tree = x.Tree.Copy() - ret.set[name] = &Template{ + tc := &Template{ nil, x, x.Tree, + src.funcMap, ret.nameSpace, } + tc.wrapFuncs() + ret.set[name] = tc } // Return the template associated with the name of this template. return ret.set[ret.Name()], nil @@ -286,6 +325,7 @@ func New(name string) *Template { nil, template.New(name), nil, + nil, ns, } tmpl.set[name] = tmpl @@ -311,6 +351,7 @@ func (t *Template) new(name string) *Template { nil, t.text.New(name), nil, + nil, t.nameSpace, } if existing, ok := tmpl.set[name]; ok { @@ -341,10 +382,35 @@ type FuncMap map[string]interface{} // type. However, it is legal to overwrite elements of the map. The return // value is the template, so calls can be chained. func (t *Template) Funcs(funcMap FuncMap) *Template { - t.text.Funcs(template.FuncMap(funcMap)) + t.funcMap = funcMap + t.wrapFuncs() return t } +// wrapFuncs records the functions with text/template. We wrap them to +// unlock the nameSpace. See TestRecursiveExecute for a test case. +func (t *Template) wrapFuncs() { + if len(t.funcMap) == 0 { + return + } + tfuncs := make(template.FuncMap, len(t.funcMap)) + for name, fn := range t.funcMap { + fnv := reflect.ValueOf(fn) + wrapper := func(args []reflect.Value) []reflect.Value { + t.nameSpace.mu.RUnlock() + defer t.nameSpace.mu.RLock() + if fnv.Type().IsVariadic() { + return fnv.CallSlice(args) + } else { + return fnv.Call(args) + } + } + wrapped := reflect.MakeFunc(fnv.Type(), wrapper) + tfuncs[name] = wrapped.Interface() + } + t.text.Funcs(tfuncs) +} + // Delims sets the action delimiters to the specified strings, to be used in // subsequent calls to Parse, ParseFiles, or ParseGlob. Nested template // definitions will inherit the settings. An empty delimiter stands for the @@ -358,8 +424,8 @@ func (t *Template) Delims(left, right string) *Template { // Lookup returns the template with the given name that is associated with t, // or nil if there is no such template. func (t *Template) Lookup(name string) *Template { - t.nameSpace.mu.Lock() - defer t.nameSpace.mu.Unlock() + t.nameSpace.mu.RLock() + defer t.nameSpace.mu.RUnlock() return t.set[name] } @@ -384,7 +450,7 @@ func Must(t *Template, err error) *Template { // For instance, ParseFiles("a/foo", "b/foo") stores "b/foo" as the template // named "foo", while "a/foo" is unavailable. func ParseFiles(filenames ...string) (*Template, error) { - return parseFiles(nil, filenames...) + return parseFiles(nil, readFileOS, filenames...) } // ParseFiles parses the named files and associates the resulting templates with @@ -396,12 +462,12 @@ func ParseFiles(filenames ...string) (*Template, error) { // // ParseFiles returns an error if t or any associated template has already been executed. func (t *Template) ParseFiles(filenames ...string) (*Template, error) { - return parseFiles(t, filenames...) + return parseFiles(t, readFileOS, filenames...) } // parseFiles is the helper for the method and function. If the argument // template is nil, it is created from the first file. -func parseFiles(t *Template, filenames ...string) (*Template, error) { +func parseFiles(t *Template, readFile func(string) (string, []byte, error), filenames ...string) (*Template, error) { if err := t.checkCanParse(); err != nil { return nil, err } @@ -411,12 +477,11 @@ func parseFiles(t *Template, filenames ...string) (*Template, error) { return nil, fmt.Errorf("html/template: no files named in call to ParseFiles") } for _, filename := range filenames { - b, err := ioutil.ReadFile(filename) + name, b, err := readFile(filename) if err != nil { return nil, err } s := string(b) - name := filepath.Base(filename) // First template becomes return value if not already defined, // and we use that one for subsequent New calls to associate // all the templates together. Also, if this file has the same name @@ -479,7 +544,7 @@ func parseGlob(t *Template, pattern string) (*Template, error) { if len(filenames) == 0 { return nil, fmt.Errorf("html/template: pattern matches no files: %#q", pattern) } - return parseFiles(t, filenames...) + return parseFiles(t, readFileOS, filenames...) } // IsTrue reports whether the value is 'true', in the sense of not the zero of its type, @@ -488,3 +553,48 @@ func parseGlob(t *Template, pattern string) (*Template, error) { func IsTrue(val interface{}) (truth, ok bool) { return template.IsTrue(val) } + +// ParseFS is like ParseFiles or ParseGlob but reads from the file system fs +// instead of the host operating system's file system. +// It accepts a list of glob patterns. +// (Note that most file names serve as glob patterns matching only themselves.) +func ParseFS(fs fs.FS, patterns ...string) (*Template, error) { + return parseFS(nil, fs, patterns) +} + +// ParseFS is like ParseFiles or ParseGlob but reads from the file system fs +// instead of the host operating system's file system. +// It accepts a list of glob patterns. +// (Note that most file names serve as glob patterns matching only themselves.) +func (t *Template) ParseFS(fs fs.FS, patterns ...string) (*Template, error) { + return parseFS(t, fs, patterns) +} + +func parseFS(t *Template, fsys fs.FS, patterns []string) (*Template, error) { + var filenames []string + for _, pattern := range patterns { + list, err := fs.Glob(fsys, pattern) + if err != nil { + return nil, err + } + if len(list) == 0 { + return nil, fmt.Errorf("template: pattern matches no files: %#q", pattern) + } + filenames = append(filenames, list...) + } + return parseFiles(t, readFileFS(fsys), filenames...) +} + +func readFileOS(file string) (name string, b []byte, err error) { + name = filepath.Base(file) + b, err = os.ReadFile(file) + return +} + +func readFileFS(fsys fs.FS) func(string) (string, []byte, error) { + return func(file string) (name string, b []byte, err error) { + name = path.Base(file) + b, err = fs.ReadFile(fsys, file) + return + } +} diff --git a/libgo/go/html/template/template_test.go b/libgo/go/html/template/template_test.go index 86bd4db..1f2c888 100644 --- a/libgo/go/html/template/template_test.go +++ b/libgo/go/html/template/template_test.go @@ -10,6 +10,7 @@ import ( . "html/template" "strings" "testing" + "text/template/parse" ) func TestTemplateClone(t *testing.T) { @@ -160,6 +161,21 @@ func TestStringsInScriptsWithJsonContentTypeAreCorrectlyEscaped(t *testing.T) { } } +func TestSkipEscapeComments(t *testing.T) { + c := newTestCase(t) + tr := parse.New("root") + tr.Mode = parse.ParseComments + newT, err := tr.Parse("{{/* A comment */}}{{ 1 }}{{/* Another comment */}}", "", "", make(map[string]*parse.Tree)) + if err != nil { + t.Fatalf("Cannot parse template text: %v", err) + } + c.root, err = c.root.AddParseTree("root", newT) + if err != nil { + t.Fatalf("Cannot add parse tree to template: %v", err) + } + c.mustExecute(c.root, nil, "1") +} + type testCase struct { t *testing.T root *Template diff --git a/libgo/go/html/template/testdata/fs.zip b/libgo/go/html/template/testdata/fs.zip new file mode 100644 index 0000000..8581313 Binary files /dev/null and b/libgo/go/html/template/testdata/fs.zip differ -- cgit v1.1 From 726b7aa004d6885388a76521222602b8552a41ee Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Wed, 27 Jan 2021 17:55:50 -0800 Subject: libgo: update to Go1.16rc1 Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/287493 --- libgo/go/html/template/exec_test.go | 55 +++++++++++++++++++++++ libgo/go/html/template/template.go | 90 ++++++------------------------------- 2 files changed, 68 insertions(+), 77 deletions(-) (limited to 'libgo/go/html') diff --git a/libgo/go/html/template/exec_test.go b/libgo/go/html/template/exec_test.go index eb00824..7d1bef1 100644 --- a/libgo/go/html/template/exec_test.go +++ b/libgo/go/html/template/exec_test.go @@ -1720,6 +1720,8 @@ var v = "v"; ` func TestEscapeRace(t *testing.T) { + t.Skip("this test currently fails with -race; see issue #39807") + tmpl := New("") _, err := tmpl.New("templ.html").Parse(raceText) if err != nil { @@ -1776,3 +1778,56 @@ func TestRecursiveExecute(t *testing.T) { t.Fatal(err) } } + +// recursiveInvoker is for TestRecursiveExecuteViaMethod. +type recursiveInvoker struct { + t *testing.T + tmpl *Template +} + +func (r *recursiveInvoker) Recur() (string, error) { + var sb strings.Builder + if err := r.tmpl.ExecuteTemplate(&sb, "subroutine", nil); err != nil { + r.t.Fatal(err) + } + return sb.String(), nil +} + +func TestRecursiveExecuteViaMethod(t *testing.T) { + tmpl := New("") + top, err := tmpl.New("x.html").Parse(`{{.Recur}}`) + if err != nil { + t.Fatal(err) + } + _, err = tmpl.New("subroutine").Parse(``) + if err != nil { + t.Fatal(err) + } + r := &recursiveInvoker{ + t: t, + tmpl: tmpl, + } + if err := top.Execute(io.Discard, r); err != nil { + t.Fatal(err) + } +} + +// Issue 43295. +func TestTemplateFuncsAfterClone(t *testing.T) { + s := `{{ f . }}` + want := "test" + orig := New("orig").Funcs(map[string]interface{}{ + "f": func(in string) string { + return in + }, + }).New("child") + + overviewTmpl := Must(Must(orig.Clone()).Parse(s)) + var out strings.Builder + if err := overviewTmpl.Execute(&out, want); err != nil { + t.Fatal(err) + } + if got := out.String(); got != want { + t.Fatalf("got %q; want %q", got, want) + } +} diff --git a/libgo/go/html/template/template.go b/libgo/go/html/template/template.go index 09d71d4..69312d3 100644 --- a/libgo/go/html/template/template.go +++ b/libgo/go/html/template/template.go @@ -11,7 +11,6 @@ import ( "os" "path" "path/filepath" - "reflect" "sync" "text/template" "text/template/parse" @@ -27,9 +26,7 @@ type Template struct { // template's in sync. text *template.Template // The underlying template's parse tree, updated to be HTML-safe. - Tree *parse.Tree - // The original functions, before wrapping. - funcMap FuncMap + Tree *parse.Tree *nameSpace // common to all associated templates } @@ -38,7 +35,7 @@ var escapeOK = fmt.Errorf("template escaped correctly") // nameSpace is the data structure shared by all templates in an association. type nameSpace struct { - mu sync.RWMutex + mu sync.Mutex set map[string]*Template escaped bool esc escaper @@ -48,8 +45,8 @@ type nameSpace struct { // itself. func (t *Template) Templates() []*Template { ns := t.nameSpace - ns.mu.RLock() - defer ns.mu.RUnlock() + ns.mu.Lock() + defer ns.mu.Unlock() // Return a slice so we don't expose the map. m := make([]*Template, 0, len(ns.set)) for _, v := range ns.set { @@ -87,8 +84,8 @@ func (t *Template) checkCanParse() error { if t == nil { return nil } - t.nameSpace.mu.RLock() - defer t.nameSpace.mu.RUnlock() + t.nameSpace.mu.Lock() + defer t.nameSpace.mu.Unlock() if t.nameSpace.escaped { return fmt.Errorf("html/template: cannot Parse after Execute") } @@ -97,16 +94,6 @@ func (t *Template) checkCanParse() error { // escape escapes all associated templates. func (t *Template) escape() error { - t.nameSpace.mu.RLock() - escapeErr := t.escapeErr - t.nameSpace.mu.RUnlock() - if escapeErr != nil { - if escapeErr == escapeOK { - return nil - } - return escapeErr - } - t.nameSpace.mu.Lock() defer t.nameSpace.mu.Unlock() t.nameSpace.escaped = true @@ -134,8 +121,6 @@ func (t *Template) Execute(wr io.Writer, data interface{}) error { if err := t.escape(); err != nil { return err } - t.nameSpace.mu.RLock() - defer t.nameSpace.mu.RUnlock() return t.text.Execute(wr, data) } @@ -151,8 +136,6 @@ func (t *Template) ExecuteTemplate(wr io.Writer, name string, data interface{}) if err != nil { return err } - t.nameSpace.mu.RLock() - defer t.nameSpace.mu.RUnlock() return tmpl.text.Execute(wr, data) } @@ -160,27 +143,13 @@ func (t *Template) ExecuteTemplate(wr io.Writer, name string, data interface{}) // is escaped, or returns an error if it cannot be. It returns the named // template. func (t *Template) lookupAndEscapeTemplate(name string) (tmpl *Template, err error) { - t.nameSpace.mu.RLock() + t.nameSpace.mu.Lock() + defer t.nameSpace.mu.Unlock() + t.nameSpace.escaped = true tmpl = t.set[name] - var escapeErr error - if tmpl != nil { - escapeErr = tmpl.escapeErr - } - t.nameSpace.mu.RUnlock() - if tmpl == nil { return nil, fmt.Errorf("html/template: %q is undefined", name) } - if escapeErr != nil { - if escapeErr != escapeOK { - return nil, escapeErr - } - return tmpl, nil - } - - t.nameSpace.mu.Lock() - defer t.nameSpace.mu.Unlock() - t.nameSpace.escaped = true if tmpl.escapeErr != nil && tmpl.escapeErr != escapeOK { return nil, tmpl.escapeErr } @@ -260,7 +229,6 @@ func (t *Template) AddParseTree(name string, tree *parse.Tree) (*Template, error nil, text, text.Tree, - nil, t.nameSpace, } t.set[name] = ret @@ -291,10 +259,8 @@ func (t *Template) Clone() (*Template, error) { nil, textClone, textClone.Tree, - t.funcMap, ns, } - ret.wrapFuncs() ret.set[ret.Name()] = ret for _, x := range textClone.Templates() { name := x.Name() @@ -303,15 +269,12 @@ func (t *Template) Clone() (*Template, error) { return nil, fmt.Errorf("html/template: cannot Clone %q after it has executed", t.Name()) } x.Tree = x.Tree.Copy() - tc := &Template{ + ret.set[name] = &Template{ nil, x, x.Tree, - src.funcMap, ret.nameSpace, } - tc.wrapFuncs() - ret.set[name] = tc } // Return the template associated with the name of this template. return ret.set[ret.Name()], nil @@ -325,7 +288,6 @@ func New(name string) *Template { nil, template.New(name), nil, - nil, ns, } tmpl.set[name] = tmpl @@ -351,7 +313,6 @@ func (t *Template) new(name string) *Template { nil, t.text.New(name), nil, - nil, t.nameSpace, } if existing, ok := tmpl.set[name]; ok { @@ -382,35 +343,10 @@ type FuncMap map[string]interface{} // type. However, it is legal to overwrite elements of the map. The return // value is the template, so calls can be chained. func (t *Template) Funcs(funcMap FuncMap) *Template { - t.funcMap = funcMap - t.wrapFuncs() + t.text.Funcs(template.FuncMap(funcMap)) return t } -// wrapFuncs records the functions with text/template. We wrap them to -// unlock the nameSpace. See TestRecursiveExecute for a test case. -func (t *Template) wrapFuncs() { - if len(t.funcMap) == 0 { - return - } - tfuncs := make(template.FuncMap, len(t.funcMap)) - for name, fn := range t.funcMap { - fnv := reflect.ValueOf(fn) - wrapper := func(args []reflect.Value) []reflect.Value { - t.nameSpace.mu.RUnlock() - defer t.nameSpace.mu.RLock() - if fnv.Type().IsVariadic() { - return fnv.CallSlice(args) - } else { - return fnv.Call(args) - } - } - wrapped := reflect.MakeFunc(fnv.Type(), wrapper) - tfuncs[name] = wrapped.Interface() - } - t.text.Funcs(tfuncs) -} - // Delims sets the action delimiters to the specified strings, to be used in // subsequent calls to Parse, ParseFiles, or ParseGlob. Nested template // definitions will inherit the settings. An empty delimiter stands for the @@ -424,8 +360,8 @@ func (t *Template) Delims(left, right string) *Template { // Lookup returns the template with the given name that is associated with t, // or nil if there is no such template. func (t *Template) Lookup(name string) *Template { - t.nameSpace.mu.RLock() - defer t.nameSpace.mu.RUnlock() + t.nameSpace.mu.Lock() + defer t.nameSpace.mu.Unlock() return t.set[name] } -- cgit v1.1