Fix VM memory allocation optimizations by reducing what we allocate on the heap
This commit is contained in:
@@ -6,44 +6,44 @@ type Integer struct {
|
||||
Value int64
|
||||
}
|
||||
|
||||
func (i *Integer) Bool() bool {
|
||||
func (i Integer) Bool() bool {
|
||||
return i.Value != 0
|
||||
}
|
||||
|
||||
func (i *Integer) Type() Type {
|
||||
func (i Integer) Type() Type {
|
||||
return IntegerType
|
||||
}
|
||||
|
||||
func (i *Integer) Inspect() string {
|
||||
func (i Integer) Inspect() string {
|
||||
return fmt.Sprintf("%d", i.Value)
|
||||
}
|
||||
|
||||
func (i *Integer) Clone() Object {
|
||||
return &Integer{Value: i.Value}
|
||||
func (i Integer) Clone() Object {
|
||||
return Integer{Value: i.Value}
|
||||
}
|
||||
|
||||
func (i *Integer) String() string {
|
||||
func (i Integer) String() string {
|
||||
return i.Inspect()
|
||||
}
|
||||
|
||||
func (i *Integer) Add(other Object) (Object, error) {
|
||||
func (i Integer) Add(other Object) (Object, error) {
|
||||
if !AssertTypes(other, IntegerType) {
|
||||
return nil, NewBinaryOpError(i, other, "+")
|
||||
}
|
||||
return &Integer{i.Value + other.(*Integer).Value}, nil
|
||||
return Integer{i.Value + other.(Integer).Value}, nil
|
||||
}
|
||||
|
||||
func (i *Integer) Sub(other Object) (Object, error) {
|
||||
func (i Integer) Sub(other Object) (Object, error) {
|
||||
if !AssertTypes(other, IntegerType) {
|
||||
return nil, NewBinaryOpError(i, other, "-")
|
||||
}
|
||||
return &Integer{i.Value - other.(*Integer).Value}, nil
|
||||
return Integer{i.Value - other.(Integer).Value}, nil
|
||||
}
|
||||
|
||||
func (i *Integer) Mul(other Object) (Object, error) {
|
||||
func (i Integer) Mul(other Object) (Object, error) {
|
||||
switch other.Type() {
|
||||
case IntegerType:
|
||||
return &Integer{i.Value * other.(*Integer).Value}, nil
|
||||
return Integer{i.Value * other.(Integer).Value}, nil
|
||||
case StringType:
|
||||
return other.(Mul).Mul(i)
|
||||
case ArrayType:
|
||||
@@ -53,69 +53,69 @@ func (i *Integer) Mul(other Object) (Object, error) {
|
||||
}
|
||||
}
|
||||
|
||||
func (i *Integer) Div(other Object) (Object, error) {
|
||||
func (i Integer) Div(other Object) (Object, error) {
|
||||
if !AssertTypes(other, IntegerType) {
|
||||
return nil, NewBinaryOpError(i, other, "/")
|
||||
}
|
||||
return &Integer{i.Value / other.(*Integer).Value}, nil
|
||||
return Integer{i.Value / other.(Integer).Value}, nil
|
||||
}
|
||||
|
||||
func (i *Integer) Mod(other Object) (Object, error) {
|
||||
func (i Integer) Mod(other Object) (Object, error) {
|
||||
if !AssertTypes(other, IntegerType) {
|
||||
return nil, NewBinaryOpError(i, other, "%")
|
||||
}
|
||||
return &Integer{i.Value % other.(*Integer).Value}, nil
|
||||
return Integer{i.Value % other.(Integer).Value}, nil
|
||||
}
|
||||
|
||||
func (i *Integer) BitwiseOr(other Object) (Object, error) {
|
||||
func (i Integer) BitwiseOr(other Object) (Object, error) {
|
||||
if !AssertTypes(other, IntegerType) {
|
||||
return nil, NewBinaryOpError(i, other, "|")
|
||||
}
|
||||
return &Integer{i.Value | other.(*Integer).Value}, nil
|
||||
return Integer{i.Value | other.(Integer).Value}, nil
|
||||
}
|
||||
|
||||
func (i *Integer) BitwiseXor(other Object) (Object, error) {
|
||||
func (i Integer) BitwiseXor(other Object) (Object, error) {
|
||||
if !AssertTypes(other, IntegerType) {
|
||||
return nil, NewBinaryOpError(i, other, "^")
|
||||
}
|
||||
return &Integer{i.Value ^ other.(*Integer).Value}, nil
|
||||
return Integer{i.Value ^ other.(Integer).Value}, nil
|
||||
}
|
||||
|
||||
func (i *Integer) BitwiseAnd(other Object) (Object, error) {
|
||||
func (i Integer) BitwiseAnd(other Object) (Object, error) {
|
||||
if !AssertTypes(other, IntegerType) {
|
||||
return nil, NewBinaryOpError(i, other, "&")
|
||||
return nil, NewBinaryOpError(i, other, "")
|
||||
}
|
||||
return &Integer{i.Value & other.(*Integer).Value}, nil
|
||||
return Integer{i.Value & other.(Integer).Value}, nil
|
||||
}
|
||||
|
||||
func (i *Integer) BitwiseNot() Object {
|
||||
return &Integer{^i.Value}
|
||||
func (i Integer) BitwiseNot() Object {
|
||||
return Integer{^i.Value}
|
||||
}
|
||||
|
||||
func (i *Integer) LeftShift(other Object) (Object, error) {
|
||||
func (i Integer) LeftShift(other Object) (Object, error) {
|
||||
if !AssertTypes(other, IntegerType) {
|
||||
return nil, NewBinaryOpError(i, other, "<<")
|
||||
}
|
||||
return &Integer{i.Value << other.(*Integer).Value}, nil
|
||||
return Integer{i.Value << other.(Integer).Value}, nil
|
||||
}
|
||||
|
||||
func (i *Integer) RightShift(other Object) (Object, error) {
|
||||
func (i Integer) RightShift(other Object) (Object, error) {
|
||||
if !AssertTypes(other, IntegerType) {
|
||||
return nil, NewBinaryOpError(i, other, ">>")
|
||||
}
|
||||
return &Integer{i.Value >> other.(*Integer).Value}, nil
|
||||
return Integer{i.Value >> other.(Integer).Value}, nil
|
||||
}
|
||||
|
||||
func (i *Integer) LogicalNot() Object {
|
||||
return &Boolean{!i.Bool()}
|
||||
func (i Integer) LogicalNot() Object {
|
||||
return Boolean{!i.Bool()}
|
||||
}
|
||||
|
||||
func (i *Integer) Negate() Object {
|
||||
return &Integer{-i.Value}
|
||||
func (i Integer) Negate() Object {
|
||||
return Integer{-i.Value}
|
||||
}
|
||||
|
||||
func (i *Integer) Compare(other Object) int {
|
||||
if obj, ok := other.(*Integer); ok {
|
||||
func (i Integer) Compare(other Object) int {
|
||||
if obj, ok := other.(Integer); ok {
|
||||
switch {
|
||||
case i.Value < obj.Value:
|
||||
return -1
|
||||
|
||||
Reference in New Issue
Block a user