Skip to content

Commit

Permalink
Updated bench and output value
Browse files Browse the repository at this point in the history
  • Loading branch information
ziflex committed Nov 1, 2024
1 parent cd3d36e commit b3ae24a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
12 changes: 11 additions & 1 deletion pkg/compiler/compiler_eq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,17 @@ func TestEqualityOperators(t *testing.T) {

out, err := vm.Run(context.Background(), p)

return string(out), err
if err != nil {
return "", err
}

j, err := out.MarshalJSON()

if err != nil {
return "", err
}

return string(j), err
}

type UseCase struct {
Expand Down
12 changes: 2 additions & 10 deletions pkg/compiler/compiler_math_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package compiler_test

import (
"context"
j "encoding/json"
"github.com/MontFerret/ferret/pkg/runtime/values"
"testing"

runtime2 "github.com/MontFerret/ferret/pkg/runtime"
Expand All @@ -23,15 +23,7 @@ func TestMathOperators(t *testing.T) {
return 0, err
}

var res int

err = j.Unmarshal(out, &res)

if err != nil {
return 0, err
}

return res, err
return int(values.ToInt(out)), nil
}

type UseCase struct {
Expand Down
14 changes: 12 additions & 2 deletions pkg/compiler/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ type UseCase struct {
func Run(p *runtime.Program, opts ...runtime.EnvironmentOption) ([]byte, error) {
vm := runtime.NewVM(opts...)

return vm.Run(context.Background(), p)
out, err := vm.Run(context.Background(), p)

if err != nil {
return nil, err
}

return out.MarshalJSON()
}

func Exec(p *runtime.Program, raw bool, opts ...runtime.EnvironmentOption) (any, error) {
Expand Down Expand Up @@ -128,9 +134,13 @@ func RunBenchmarkWith(b *testing.B, c *compiler.Compiler, expression string, opt
runtime.WithFunctions(c.Functions().Unwrap()),
}
options = append(options, opts...)
vm := runtime.NewVM(opts...)
ctx := context.Background()

b.ResetTimer()

for n := 0; n < b.N; n++ {
_, e := Run(prog, opts...)
_, e := vm.Run(ctx, prog)

if e != nil {
panic(e)
Expand Down
4 changes: 2 additions & 2 deletions pkg/runtime/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func NewVM(opts ...EnvironmentOption) *VM {
return vm
}

func (vm *VM) Run(ctx context.Context, program *Program) ([]byte, error) {
func (vm *VM) Run(ctx context.Context, program *Program) (core.Value, error) {
tryCatch := func(pos int) bool {
for _, pair := range program.CatchTable {
if pos >= pair[0] && pos <= pair[1] {
Expand Down Expand Up @@ -497,5 +497,5 @@ loop:
}
}

return frame.Operands.Pop().MarshalJSON()
return frame.Operands.Pop(), nil
}

0 comments on commit b3ae24a

Please sign in to comment.