<commit_before>package handlers

import (
	"github.com/yuin/gopher-lua"
	"math/big"
)

type Balance struct {
	Security *Security
	Amount   *big.Rat
}

const luaBalanceTypeName = "balance"

func luaRegisterBalances(L *lua.LState) {
	mt := L.NewTypeMetatable(luaBalanceTypeName)
	L.SetGlobal("balance", mt)
	L.SetField(mt, "__index", L.NewFunction(luaBalance__index))
	L.SetField(mt, "__tostring", L.NewFunction(luaBalance__tostring))
	L.SetField(mt, "__eq", L.NewFunction(luaBalance__eq))
	L.SetField(mt, "__lt", L.NewFunction(luaBalance__lt))
	L.SetField(mt, "__le", L.NewFunction(luaBalance__le))
	L.SetField(mt, "__add", L.NewFunction(luaBalance__add))
	L.SetField(mt, "__sub", L.NewFunction(luaBalance__sub))
	L.SetField(mt, "__mul", L.NewFunction(luaBalance__mul))
	L.SetField(mt, "__div", L.NewFunction(luaBalance__div))
	L.SetField(mt, "__unm", L.NewFunction(luaBalance__unm))
	L.SetField(mt, "__metatable", lua.LString("protected"))
}

func BalanceToLua(L *lua.LState, balance *Balance) *lua.LUserData {
	ud := L.NewUserData()
	ud.Value = balance
	L.SetMetatable(ud, L.GetTypeMetatable(luaBalanceTypeName))
	return ud
}

// Checks whether the first lua argument is a *LUserData with *Balance and returns this *Balance.
func luaCheckBalance(L *lua.LState, n int) *Balance {
	ud := L.CheckUserData(n)
	if balance, ok := ud.Value.(*Balance); ok {
		return balance
	}
	L.ArgError(n, "balance expected")
	return nil
}

func luaWeakCheckBalance(L *lua.LState, n int) *Balance {
	v := L.Get(n)
	if ud, ok := v.(*lua.LUserData); ok {
		if balance, ok := ud.Value.(*Balance); ok {
			return balance
		}
		L.ArgError(n, "balance expected")
	}
	return nil
}

func luaGetBalanceOperands(L *lua.LState, n int, m int) (*Balance, *Balance) {
	bn := luaWeakCheckBalance(L, n)
	bm := luaWeakCheckBalance(L, m)

	if bn != nil && bm != nil {
		return bn, bm
	} else if bn != nil {
		nm := L.CheckNumber(m)
		var balance Balance
		var rat big.Rat
		balance.Security = bn.Security
		balance.Amount = rat.SetFloat64(float64(nm))
		if balance.Amount == nil {
			L.ArgError(n, "non-finite float invalid for operand to balance arithemetic")
			return nil, nil
		}
		return bn, &balance
	} else if bm != nil {
		nn := L.CheckNumber(n)
		var balance Balance
		var rat big.Rat
		balance.Security = bm.Security
		balance.Amount = rat.SetFloat64(float64(nn))
		if balance.Amount == nil {
			L.ArgError(n, "non-finite float invalid for operand to balance arithemetic")
			return nil, nil
		}
		return bm, &balance
	}
	L.ArgError(n, "balance expected")
	return nil, nil
}

func luaBalance__index(L *lua.LState) int {
	a := luaCheckBalance(L, 1)
	field := L.CheckString(2)

	switch field {
	case "Security", "security":
		L.Push(SecurityToLua(L, a.Security))
	case "Amount", "amount":
		float, _ := a.Amount.Float64()
		L.Push(lua.LNumber(float))
	default:
		L.ArgError(2, "unexpected balance attribute: "+field)
	}

	return 1
}<commit_msg>Lua balances: Fix order of operands pulled in<commit_after>package handlers

import (
	"github.com/yuin/gopher-lua"
	"math/big"
)

type Balance struct {
	Security *Security
	Amount   *big.Rat
}

const luaBalanceTypeName = "balance"

func luaRegisterBalances(L *lua.LState) {
	mt := L.NewTypeMetatable(luaBalanceTypeName)
	L.SetGlobal("balance", mt)
	L.SetField(mt, "__index", L.NewFunction(luaBalance__index))
	L.SetField(mt, "__tostring", L.NewFunction(luaBalance__tostring))
	L.SetField(mt, "__eq", L.NewFunction(luaBalance__eq))
	L.SetField(mt, "__lt", L.NewFunction(luaBalance__lt))
	L.SetField(mt, "__le", L.NewFunction(luaBalance__le))
	L.SetField(mt, "__add", L.NewFunction(luaBalance__add))
	L.SetField(mt, "__sub", L.NewFunction(luaBalance__sub))
	L.SetField(mt, "__mul", L.NewFunction(luaBalance__mul))
	L.SetField(mt, "__div", L.NewFunction(luaBalance__div))
	L.SetField(mt, "__unm", L.NewFunction(luaBalance__unm))
	L.SetField(mt, "__metatable", lua.LString("protected"))
}

func BalanceToLua(L *lua.LState, balance *Balance) *lua.LUserData {
	ud := L.NewUserData()
	ud.Value = balance
	L.SetMetatable(ud, L.GetTypeMetatable(luaBalanceTypeName))
	return ud
}

// Checks whether the first lua argument is a *LUserData with *Balance and returns this *Balance.
func luaCheckBalance(L *lua.LState, n int) *Balance {
	ud := L.CheckUserData(n)
	if balance, ok := ud.Value.(*Balance); ok {
		return balance
	}
	L.ArgError(n, "balance expected")
	return nil
}

func luaWeakCheckBalance(L *lua.LState, n int) *Balance {
	v := L.Get(n)
	if ud, ok := v.(*lua.LUserData); ok {
		if balance, ok := ud.Value.(*Balance); ok {
			return balance
		}
		L.ArgError(n, "balance expected")
	}
	return nil
}

func luaGetBalanceOperands(L *lua.LState, m int, n int) (*Balance, *Balance) {
	bm := luaWeakCheckBalance(L, m)
	bn := luaWeakCheckBalance(L, n)

	if bm != nil && bn != nil {
		return bm, bn
	} else if bm != nil {
		nn := L.CheckNumber(n)
		var balance Balance
		var rat big.Rat
		balance.Security = bm.Security
		balance.Amount = rat.SetFloat64(float64(nn))
		if balance.Amount == nil {
			L.ArgError(n, "non-finite float invalid for operand to balance arithemetic")
			return nil, nil
		}
		return bm, &balance
	} else if bn != nil {
		nm := L.CheckNumber(m)
		var balance Balance
		var rat big.Rat
		balance.Security = bn.Security
		balance.Amount = rat.SetFloat64(float64(nm))
		if balance.Amount == nil {
			L.ArgError(m, "non-finite float invalid for operand to balance arithemetic")
			return nil, nil
		}
		return &balance, bn
	}
	L.ArgError(m, "balance expected")
	return nil, nil
}

func luaBalance__index(L *lua.LState) int {
	a := luaCheckBalance(L, 1)
	field := L.CheckString(2)

	switch field {
	case "Security", "security":
		L.Push(SecurityToLua(L, a.Security))
	case "Amount", "amount":
		float, _ := a.Amount.Float64()
		L.Push(lua.LNumber(float))
	default:
		L.ArgError(2, "unexpected balance attribute: "+field)
	}

	return 1
}
{'fraction_non_alphanumeric': 0.14351535836177473, 'fraction_numerical': 0.00477815699658703, 'mean_word_length': 4.550189393939394, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4206144', 'n_tokens_mistral': 2310, 'n_tokens_neox': 2255, 'n_words': 519}