Skip to content

Commit

Permalink
internal/pkg: make CallCtxt.Decimal support *adt.Vertex
Browse files Browse the repository at this point in the history
The cue.Value.Decimal method currently makes the assumption that
its function argument is an *adt.Num. This means that the "Validator"
form of the builtin math.MultipleOf(n) will not work, because in that
case the first argument will be an *adt.Vertex.

This changes the explicit interface convertion to instead use the
cue.value.Decimal method, which will work in all cases.

Fixes #2927

Signed-off-by: Noam Dolovich <noam.tzvi.dolovich@gmail.com>
Change-Id: I35f780efde92a6523548ed04e1e7a6a960b191ce
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1184631
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
  • Loading branch information
NoamTD authored and mvdan committed Apr 19, 2024
1 parent a1dec7d commit 243df63
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
8 changes: 5 additions & 3 deletions internal/pkg/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ import (
"io"
"math/big"

"github.com/cockroachdb/apd/v3"

"cuelang.org/go/cue"
"cuelang.org/go/cue/token"
"cuelang.org/go/internal/core/adt"
"cuelang.org/go/internal/value"
"github.com/cockroachdb/apd/v3"
)

// CallCtxt is passed to builtin implementations that need to use a cue.Value. This is an internal type. Its interface may change.
Expand Down Expand Up @@ -129,11 +130,12 @@ func (c *CallCtxt) uintValue(i, bits int, typ string) uint64 {

func (c *CallCtxt) Decimal(i int) *apd.Decimal {
x := value.Make(c.ctx, c.args[i])
if _, err := x.MantExp(nil); err != nil {
res, err := x.Decimal()
if err != nil {
c.invalidArgType(c.args[i], i, "Decimal", err)
return nil
}
return &c.args[i].(*adt.Num).X
return res
}

func (c *CallCtxt) Float64(i int) float64 {
Expand Down
9 changes: 7 additions & 2 deletions pkg/math/testdata/round.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ mul3: math.MultipleOf(100, 1.00001)
mul4: math.MultipleOf(1, 1)
mul5: math.MultipleOf(5, 2.5)
mul6: math.MultipleOf(100e100, 10)
mul7: 10
mul7: math.MultipleOf(2)
mul8: 9 & math.MultipleOf(3)

r0: math.Round(2.5)
r1: math.Round(-2.5)
Expand All @@ -36,9 +39,9 @@ Errors:
mul2: error in call to math.MultipleOf: division by zero:
./in.cue:6:7
floorE1: too many arguments in call to math.Floor (have 2, want 1):
./in.cue:17:10
./in.cue:20:10
floorE2: cannot use "foo" (type string) as number in argument 1 to math.Floor:
./in.cue:18:21
./in.cue:21:21

Result:
mul0: true
Expand All @@ -49,6 +52,8 @@ mul3: false
mul4: true
mul5: true
mul6: true
mul7: 10
mul8: 9
r0: 3
r1: -3
r2: 2
Expand Down

0 comments on commit 243df63

Please sign in to comment.