From b3ae24a904eeabcb6cb3294efa485320c6b18585 Mon Sep 17 00:00:00 2001 From: Tim Voronov Date: Fri, 1 Nov 2024 17:49:03 -0400 Subject: [PATCH] Updated bench and output value --- pkg/compiler/compiler_eq_test.go | 12 +++++++++++- pkg/compiler/compiler_math_test.go | 12 ++---------- pkg/compiler/setup_test.go | 14 ++++++++++++-- pkg/runtime/vm.go | 4 ++-- 4 files changed, 27 insertions(+), 15 deletions(-) diff --git a/pkg/compiler/compiler_eq_test.go b/pkg/compiler/compiler_eq_test.go index 29038611..797dd66c 100644 --- a/pkg/compiler/compiler_eq_test.go +++ b/pkg/compiler/compiler_eq_test.go @@ -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 { diff --git a/pkg/compiler/compiler_math_test.go b/pkg/compiler/compiler_math_test.go index 8bb808f6..a2812190 100644 --- a/pkg/compiler/compiler_math_test.go +++ b/pkg/compiler/compiler_math_test.go @@ -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" @@ -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 { diff --git a/pkg/compiler/setup_test.go b/pkg/compiler/setup_test.go index fd9bff24..c8fe22ee 100644 --- a/pkg/compiler/setup_test.go +++ b/pkg/compiler/setup_test.go @@ -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) { @@ -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) diff --git a/pkg/runtime/vm.go b/pkg/runtime/vm.go index 85c1241f..6a564c2d 100644 --- a/pkg/runtime/vm.go +++ b/pkg/runtime/vm.go @@ -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] { @@ -497,5 +497,5 @@ loop: } } - return frame.Operands.Pop().MarshalJSON() + return frame.Operands.Pop(), nil }