Skip to content

Commit

Permalink
Merge branch 'main' into bugfix_recvcleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jsccast authored Jul 12, 2021
2 parents 3c6745f + 6af572f commit 2b13c5b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
5 changes: 4 additions & 1 deletion doc/manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,10 @@ single operation. Currently the following steps are supported:
1. `redactString`: a function that compiles and adds a
redaction pattern that matches the given string
literally.
1. `Failure`: return an object representing a failure with the
argument as the failure message.
1. `match`: [Sheen](https://github.com/Comcast/sheens)'s
[pattern
matching](https://github.com/Comcast/sheens#pattern-matching)
Expand Down
12 changes: 7 additions & 5 deletions dsl/js.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ func JSExec(ctx *Ctx, src string, env map[string]interface{}) (interface{}, erro
return x, nil
}

func gojaPanic(vm *goja.Runtime, x interface{}) {
panic(vm.ToValue(x))
}

func jsExec(ctx *Ctx, src string, env map[string]interface{}) (interface{}, error) {

js := goja.New()
Expand All @@ -64,14 +68,14 @@ func jsExec(ctx *Ctx, src string, env map[string]interface{}) (interface{}, erro

js.Set("redactRegexp", func(pat string) {
if err := ctx.AddRedaction(pat); err != nil {
panic(err)
gojaPanic(js, NewBroken(err))
}
})

js.Set("redactString", func(pat string) {
pat = regexp.QuoteMeta(pat)
if err := ctx.AddRedaction(pat); err != nil {
panic(err)
gojaPanic(js, NewBroken(err))
}
})

Expand All @@ -81,9 +85,7 @@ func jsExec(ctx *Ctx, src string, env map[string]interface{}) (interface{}, erro
}
bss, err := match.Match(pat, msg, bs)
if err != nil {
// This panic is caught. (That's how we
// return errors.)
panic(js.ToValue(err.Error()))
gojaPanic(js, NewBroken(err))
}
// Strip type (match.Bindings) to enable standard
// Javascript access to the maps.
Expand Down
14 changes: 14 additions & 0 deletions dsl/js_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,18 @@ bss[0]["?x"] == "queso";
}
})

t.Run("nopanic", func(t *testing.T) {
// ToDo: Iterate over all exposed native Go functions?

src := `redactRegexp("[}");`

_, err := JSExec(ctx, src, nil)
if err == nil {
t.Fatal("should have complained (politely)")
}
if _, is := IsBroken(err); !is {
t.Fatalf("should have reported Broken instead of %T", err)
}
})

}

0 comments on commit 2b13c5b

Please sign in to comment.