Skip to content

Commit

Permalink
fix debug/1 function to be available only when debug/0 is defined
Browse files Browse the repository at this point in the history
  • Loading branch information
itchyny committed May 19, 2024
1 parent f694c1b commit a41a5f8
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 4 deletions.
1 change: 0 additions & 1 deletion builtin.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion builtin.jq
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ def sub($re; str; $flags):
def gsub($re; str): sub($re; str; "g");
def gsub($re; str; $flags): sub($re; str; $flags + "g");

def debug(f): (f | debug | empty), .;
def inputs:
try
repeat(input)
Expand Down
16 changes: 16 additions & 0 deletions compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -993,6 +993,22 @@ func (c *compiler) compileFunc(e *Func) error {
true,
-1,
)
case "debug":
setfork := c.lazy(func() *code {
return &code{op: opfork, v: len(c.codes)}
})
if err := c.compileQuery(e.Args[0]); err != nil {
return err
}
if err := c.compileFunc(&Func{Name: "debug"}); err != nil {
if _, ok := err.(*funcNotFoundError); ok {
err = &funcNotFoundError{e}
}
return err
}
c.append(&code{op: opbacktrack})
setfork()
return nil
default:
return c.compileCall(e.Name, e.Args)
}
Expand Down
2 changes: 0 additions & 2 deletions compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,6 @@ func BenchmarkCompile(b *testing.B) {
_, err := gojq.Compile(
query,
gojq.WithInputIter(gojq.NewIter()),
gojq.WithFunction("debug", 0, 0,
func(v any, _ []any) any { return v }),
)
if err != nil {
b.Fatal(err)
Expand Down
1 change: 1 addition & 0 deletions func.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func init() {
"builtins": argFunc0(nil),
"input": argFunc0(nil),
"modulemeta": argFunc0(nil),
"debug": argFunc1(nil),
"abs": argFunc0(funcAbs),
"length": argFunc0(funcLength),
"utf8bytelength": argFunc0(funcUtf8ByteLength),
Expand Down

0 comments on commit a41a5f8

Please sign in to comment.