Skip to content

Commit

Permalink
optimized assert with lazy args
Browse files Browse the repository at this point in the history
  • Loading branch information
lunfardo314 committed Nov 30, 2024
1 parent 2e2fc7c commit 21cb8dd
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 21 deletions.
2 changes: 1 addition & 1 deletion compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ func (lib *Library) expressionFromBytecode(bytecode []byte, localLib ...*LocalLi
if len(callPrefix) == 1 && arity < 0 {
return nil, nil, 0xff, fmt.Errorf("EasyFL: short embedded with vararg is not allowed")
}
Assert(arity >= 0, "EasyFL: arity >= 0")
Assertf(arity >= 0, "EasyFL: arity >= 0")

ret := &Expression{
Args: make([]*Expression, 0),
Expand Down
15 changes: 8 additions & 7 deletions eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,22 +238,23 @@ func (lib *Library) CallLocalLibrary(ctx *CallParams, libBin [][]byte, idx int)

func (lib *Library) MustEqual(source1, source2 string) {
res1, err := lib.EvalFromSource(nil, source1)
Assert(err == nil, "expression '%s' resulted in error: '%v'", source1, err)
Assertf(err == nil, "expression '%s' resulted in error: '%v'", source1, err)
res2, err := lib.EvalFromSource(nil, source2)
Assert(err == nil, "expression '%s' resulted in error: '%v'", source2, err)
Assert(bytes.Equal(res1, res2), "must be equal %s and %s: %s != %s", source1, source2, Fmt(res1), Fmt(res2))
Assertf(err == nil, "expression '%s' resulted in error: '%v'", source2, err)
Assertf(bytes.Equal(res1, res2), "must be equal %s and %s: %s != %s", source1, source2,
func() string { return Fmt(res1) }, func() string { return Fmt(res2) })
}

func (lib *Library) MustTrue(source string) {
res, err := lib.EvalFromSource(nil, source)
Assert(err == nil, "expression '%s' resulted in error: '%v'", source, err)
Assert(len(res) > 0, "expression '%s' must be true", res)
Assertf(err == nil, "expression '%s' resulted in error: '%v'", source, err)
Assertf(len(res) > 0, "expression '%s' must be true", res)
}

func (lib *Library) MustError(source string, mustContain ...string) {
_, err := lib.EvalFromSource(nil, source)
Assert(err != nil, "expression '%s' is expected to return an error", source)
Assertf(err != nil, "expression '%s' is expected to return an error", source)
if len(mustContain) > 0 {
Assert(strings.Contains(err.Error(), mustContain[0]), fmt.Sprintf("error must contain '%s' (instead got %s)", mustContain[0], err.Error()))
Assertf(strings.Contains(err.Error(), mustContain[0]), fmt.Sprintf("error must contain '%s' (instead got %s)", mustContain[0], err.Error()))
}
}
12 changes: 6 additions & 6 deletions library.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func (lib *Library) embedShortErr(sym string, requiredNumPar int, embeddedFun Em
}
codeBytes, err := lib.FunctionCallPrefixByName(sym, byte(requiredNumPar))
AssertNoError(err)
Assert(len(codeBytes) == 1, "expected short code")
Assertf(len(codeBytes) == 1, "expected short code")
}
return byte(dscr.funCode), nil
}
Expand Down Expand Up @@ -253,7 +253,7 @@ func (lib *Library) embedLongErr(sym string, requiredNumPar int, embeddedFun Emb
}
codeBytes, err := lib.FunctionCallPrefixByName(sym, byte(requiredNumPar))
AssertNoError(err)
Assert(len(codeBytes) == 2, "expected long code")
Assertf(len(codeBytes) == 2, "expected long code")
}
return dscr.funCode, nil
}
Expand Down Expand Up @@ -319,7 +319,7 @@ func (lib *Library) ExtendErr(sym string, source string) (uint16, error) {
return 0, fmt.Errorf("error while compiling '%s': %v", sym, err)
}

Assert(lib.numExtended < MaxNumExtendedGlobal, "too many extended functions")
Assertf(lib.numExtended < MaxNumExtendedGlobal, "too many extended functions")

if lib.existsFunction(sym) {
return 0, errors.New("repeating symbol '" + sym + "'")
Expand All @@ -344,7 +344,7 @@ func (lib *Library) ExtendErr(sym string, source string) (uint16, error) {
// sanity check
codeBytes, err := lib.FunctionCallPrefixByName(sym, byte(numParam))
AssertNoError(err)
Assert(len(codeBytes) == 2, "expected long code")
Assertf(len(codeBytes) == 2, "expected long code")
}

return dscr.funCode, nil
Expand Down Expand Up @@ -457,7 +457,7 @@ func (lib *Library) functionByCode(funCode uint16, localLib ...*LocalLibrary) (E
func (fi *funInfo) callPrefix(numArgs byte) ([]byte, error) {
var ret []byte
if fi.IsShort {
Assert(fi.FunCode > LastEmbeddedReserved, "internal inconsistency: fi.FunCode must be > %d", LastEmbeddedReserved)
Assertf(fi.FunCode > LastEmbeddedReserved, "internal inconsistency: fi.FunCode must be > %d", LastEmbeddedReserved)
ret = []byte{byte(fi.FunCode)}
} else {
if fi.NumParams < 0 {
Expand All @@ -477,7 +477,7 @@ func (fi *funInfo) callPrefix(numArgs byte) ([]byte, error) {
ret = make([]byte, 2)
binary.BigEndian.PutUint16(ret, u16)
} else {
Assert(fi.FunCode <= FirstLocalFunCode+255 && FirstLocalFunCode <= fi.FunCode, "fi.FunCode <= FirstLocalFunCode+255 && FirstLocalFunCode <= fi.FunCode")
Assertf(fi.FunCode <= FirstLocalFunCode+255 && FirstLocalFunCode <= fi.FunCode, "fi.FunCode <= FirstLocalFunCode+255 && FirstLocalFunCode <= fi.FunCode")
// local function call 3 bytes
u16 := (uint16(firstByte) << 8) | FirstLocalFunCode
ret = make([]byte, 3)
Expand Down
4 changes: 2 additions & 2 deletions local_library.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (lib *Library) CompileLocalLibrary(source string) ([][]byte, error) {
return nil, fmt.Errorf("error while compiling '%s': %v", pf.Sym, err)
}

Assert(len(lib.funByName) <= 255, "a local library can contain uo to 255 functions")
Assertf(len(lib.funByName) <= 255, "a local library can contain uo to 255 functions")

if lib.existsFunction(pf.Sym, libLoc) {
return nil, errors.New("repeating symbol '" + pf.Sym + "'")
Expand Down Expand Up @@ -77,7 +77,7 @@ func (lib *Library) LocalLibraryFromBytes(bin [][]byte) (*LocalLibrary, error) {
if maxParam != 0xff {
numParams = int(maxParam) + 1
}
Assert(numParams <= 15, "numParams <= 15")
Assertf(numParams <= 15, "numParams <= 15")
dscr := &funDescriptor{
sym: sym,
funCode: uint16(FirstLocalFunCode + i),
Expand Down
4 changes: 2 additions & 2 deletions serde.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ func (fd *funDescriptor) write(w io.Writer) {
_ = binary.Write(w, binary.BigEndian, np)

// function name
Assert(len(fd.sym) < 256, "EasyFL: len(fd.sym)<256")
Assertf(len(fd.sym) < 256, "EasyFL: len(fd.sym)<256")
_, _ = w.Write([]byte{byte(len(fd.sym))})
_, _ = w.Write([]byte(fd.sym))
Assert(len(fd.bytecode) < 256*256, "EasyFL: len(fd.bytecode)<256*256")
Assertf(len(fd.bytecode) < 256*256, "EasyFL: len(fd.bytecode)<256*256")
// bytecode (nil for embedded)
_ = binary.Write(w, binary.BigEndian, uint16(len(fd.bytecode)))
_, _ = w.Write(fd.bytecode)
Expand Down
41 changes: 38 additions & 3 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,49 @@ func RequireErrorWith(t *testing.T, err error, s string) {
require.Contains(t, err.Error(), s)
}

func Assert(cond bool, format string, args ...interface{}) {
func Assertf(cond bool, format string, args ...interface{}) {
if !cond {
panic(fmt.Sprintf("assertion failed:: "+format, args...))
panic(fmt.Errorf("assertion failed:: "+format, evalLazyArgs(args...)...))
}
}

func evalLazyArgs(args ...any) []any {
ret := make([]any, len(args))
for i, arg := range args {
switch funArg := arg.(type) {
case func() any:
ret[i] = funArg()
case func() string:
ret[i] = funArg()
case func() bool:
ret[i] = funArg()
case func() int:
ret[i] = funArg()
case func() byte:
ret[i] = funArg()
case func() uint:
ret[i] = funArg()
case func() uint16:
ret[i] = funArg()
case func() uint32:
ret[i] = funArg()
case func() uint64:
ret[i] = funArg()
case func() int16:
ret[i] = funArg()
case func() int32:
ret[i] = funArg()
case func() int64:
ret[i] = funArg()
default:
ret[i] = arg
}
}
return ret
}

func AssertNoError(err error) {
Assert(err == nil, "error: %v", err)
Assertf(err == nil, "error: %v", err)
}

func Hex(data []byte) string {
Expand Down

0 comments on commit 21cb8dd

Please sign in to comment.