Fix VM memory allocation optimizations by reducing what we allocate on the heap
This commit is contained in:
@@ -10,59 +10,59 @@ type String struct {
|
||||
Value string
|
||||
}
|
||||
|
||||
func (s *String) Len() int {
|
||||
func (s String) Len() int {
|
||||
return utf8.RuneCountInString(s.Value)
|
||||
}
|
||||
|
||||
func (s *String) Bool() bool {
|
||||
func (s String) Bool() bool {
|
||||
return s.Value != ""
|
||||
}
|
||||
|
||||
func (s *String) Type() Type {
|
||||
func (s String) Type() Type {
|
||||
return StringType
|
||||
}
|
||||
|
||||
func (s *String) Inspect() string {
|
||||
func (s String) Inspect() string {
|
||||
return fmt.Sprintf("%#v", s.Value)
|
||||
}
|
||||
|
||||
func (s *String) Clone() Object {
|
||||
return &String{Value: s.Value}
|
||||
func (s String) Clone() Object {
|
||||
return String{Value: s.Value}
|
||||
}
|
||||
|
||||
func (s *String) String() string {
|
||||
func (s String) String() string {
|
||||
return s.Value
|
||||
}
|
||||
|
||||
func (s *String) Add(other Object) (Object, error) {
|
||||
func (s String) Add(other Object) (Object, error) {
|
||||
if !AssertTypes(other, StringType) {
|
||||
return nil, NewBinaryOpError(s, other, "+")
|
||||
}
|
||||
return &String{s.Value + other.(*String).Value}, nil
|
||||
return String{s.Value + other.(String).Value}, nil
|
||||
}
|
||||
|
||||
func (s *String) Mul(other Object) (Object, error) {
|
||||
func (s String) Mul(other Object) (Object, error) {
|
||||
if !AssertTypes(other, IntegerType) {
|
||||
return nil, NewBinaryOpError(s, other, "*")
|
||||
}
|
||||
return &String{strings.Repeat(s.Value, int(other.(*Integer).Value))}, nil
|
||||
return String{strings.Repeat(s.Value, int(other.(Integer).Value))}, nil
|
||||
}
|
||||
|
||||
func (s *String) Get(index Object) (Object, error) {
|
||||
func (s String) Get(index Object) (Object, error) {
|
||||
if !AssertTypes(index, IntegerType) {
|
||||
return nil, fmt.Errorf("invalid type for string index, expected Integer got %s", index.Type())
|
||||
}
|
||||
|
||||
i := int(index.(*Integer).Value)
|
||||
i := int(index.(Integer).Value)
|
||||
if i < 0 || i >= len(s.Value) {
|
||||
return &String{}, nil
|
||||
return String{}, nil
|
||||
}
|
||||
|
||||
return &String{string(s.Value[i])}, nil
|
||||
return String{string(s.Value[i])}, nil
|
||||
}
|
||||
|
||||
func (s *String) Compare(other Object) int {
|
||||
if obj, ok := other.(*String); ok {
|
||||
func (s String) Compare(other Object) int {
|
||||
if obj, ok := other.(String); ok {
|
||||
switch {
|
||||
case s.Value < obj.Value:
|
||||
return -1
|
||||
|
||||
Reference in New Issue
Block a user