Skip to content

Latest commit

 

History

History
442 lines (343 loc) · 13.5 KB

objects.md

File metadata and controls

442 lines (343 loc) · 13.5 KB

Object Types

Table of Contents

slim Objects

In slim, all object types (both runtime types and user types) must implement Object interface.

Object Interface

TypeName() string

TypeName method should return the name of the type. Type names are not directly used by the runtime (except when it reports a run-time error), but, it is generally a good idea to keep it short but distinguishable from other types.

String() string

String method should return a string representation of the underlying value. The value returned by String method will be used whenever string formatting for the value is required, most commonly when being converted into String value.

BinaryOp(op token.Token, rhs Object) (res Object, err error)

In slim, a type can overload binary operators (+, -, *, /, %, &, |, ^, &^, >>, <<, >, >=; note that < and <= operators are not overloadable as they're simply implemented by switching left-hand side and right-hand side of >/>= operator) by implementing BinaryOp method. BinaryOp method takes the operator op and the right-hand side object rhs, and, should return a resulting value res.

Error value vs runtime error

If BinaryOp method returns an error err (the second return value), it will be treated as a run-time error, which will halt the execution (VM.Run() error) and will return the error to the user. All runtime type implementations, for example, will return an ErrInvalidOperator error when the given operator is not supported by the type.

Alternatively the method can return an Error value as its result res (the first return value), which will not halt the runtime and will be treated like any other values. As a dynamically typed language, the receiver (another expression or statement) can determine how to translate Error value returned from binary operator expression.

IsFalsy() bool

IsFalsy method should return true if the underlying value is considered to be falsy.

Equals(o Object) bool

Equals method should return true if the underlying value is considered to be equal to the underlying value of another object o. When comparing values of different types, the runtime does not guarantee or force anything, but, it's generally a good idea to make the result consistent. For example, a custom integer type may return true when comparing against String value, but, it should return the same result for the same inputs.

Copy() Object

Copy method should return a new copy of the object. Builtin function copy uses this method to copy values. Default implementation of all runtime types return a deep-copy values, but, it's not a requirement by the runtime.

IndexGet(index Object) (value Object, err error)

IndexGet should take an index Object and return a result Object or an error for indexable objects. Indexable is an object that can take an index and return an object. If a type is indexable, its values support dot selector (value = object.index) and indexer (value = object[index]) syntax.

If Object is not indexable, ErrNotIndexable should be returned as error. If nil is returned as value, it will be converted to Undefined value by the runtime.

If IndexGet returns an error (err), the VM will treat it as a run-time error and ignore the returned value.

Array and Map implementation forces the type of index Object to be Int and String respectively, but, it's not a required behavior of the VM. It is completely okay to take various index types as long as it is consistent.

By convention, Array or Array-like types and Map or Map-like types return Undefined value when the key does not exist. But, again, this is not a required behavior.

IndexSet(index, value Object) error

IndexSet should take an index Object and a value Object for index assignable objects. Index assignable is an object that can take an index and a value on the left-hand side of the assignment statement. If a type is index assignable, its values support assignment using dot selector (object.index = value) and indexer (object[index] = value) in the assignment statements.

If Object is not index assignable, ErrNotIndexAssignable should be returned as error. If an error is returned, it will be treated as a run-time error.

Array and Map implementation forces the type of index Object to be Int and String respectively, but, it's not a required behavior of the VM. It is completely okay to take various index types as long as it is consistent.

Callable Objects

If the type is Callable, its values can be invoked as if they were functions. Two functions need to be implemented for Callable objects.

CanCall() bool

CanCall should return whether the Object can be called. When this function returns true, the Object is considered Callable.

Call(args ...Object) (ret Object, err error)

Call should take an arbitrary number of arguments and return a return value and/or an error, which the VM will consider as a run-time error.

Iterable Objects

If a type is iterable, its values can be used in for-in statements (for key, value in object { ... }). Two functions need to be implemented for Iterable Objects

CanIterate() bool

CanIterate should return whether the Object can be Iterated.

Iterate() Iterator

The Iterate method should return another object that implements Iterator interface.

Iterator Interface

Next() bool

Next method should return true if there are more elements to iterate. When used with for-in statements, the compiler uses Key and Value methods to populate the current element's key (or index) and value from the object that this iterator represents. The runtime will stop iterating in for-in statement when this method returns false.

Key() Object

Key method should return a key (or an index) Object for the current element of the underlying object. It should return the same value until Next method is called again. By convention, iterators for the map or map-like objects returns the String key, and, iterators for array or array-like objects returns the Int ndex. But, it's not a requirement by the VM.

Value() Object

Value method should return a value Object for the current element of the underlying object. It should return the same value until Next method is called again.

Runtime Object Types

These are the basic types slim runtime supports out of the box:

See Runtime Types for more details on these runtime types.

User Object Types

Users can easily extend and add their own types by implementing the same Object interface and the default ObjectImpl implementation. slim runtime will treat them in the same way as its runtime types with no performance overhead.

Here's an example user type implementation, StringArray:

type StringArray struct {
    slim.ObjectImpl
    Value []string
}

func (o *StringArray) String() string {
    return strings.Join(o.Value, ", ")
}

func (o *StringArray) BinaryOp(op token.Token, rhs slim.Object) (slim.Object, error) {
    if rhs, ok := rhs.(*StringArray); ok {
        switch op {
        case token.Add:
            if len(rhs.Value) == 0 {
                return o, nil
            }
            return &StringArray{Value: append(o.Value, rhs.Value...)}, nil
        }
    }

    return nil, slim.ErrInvalidOperator
}

func (o *StringArray) IsFalsy() bool {
    return len(o.Value) == 0
}

func (o *StringArray) Equals(x slim.Object) bool {
    if x, ok := x.(*StringArray); ok {
        if len(o.Value) != len(x.Value) {
            return false
        }

        for i, v := range o.Value {
            if v != x.Value[i] {
                return false
            }
        }

        return true
    }

    return false
}

func (o *StringArray) Copy() slim.Object {
    return &StringArray{
        Value: append([]string{}, o.Value...),
    }
}

func (o *StringArray) TypeName() string {
    return "string-array"
}

You can use a user type via either Script.Add or by directly manipulating the symbol table and the global variables. Here's an example code to add StringArray to the script:

// script that uses 'my_list'
s := slim.NewScript([]byte(`
    fmt := import("fmt")
    fmt.println(my_list, ", three")
`))

s.SetImports(stdlib.GetModuleMap("fmt"))
myList := &StringArray{Value: []string{"one", "two"}}
s.Add("my_list", myList)  // add StringArray value 'my_list'
s.Run()                   // prints "one, two, three"

It can also implement IndexGet and IndexSet:

func (o *StringArray) IndexGet(index slim.Object) (slim.Object, error) {
    intIdx, ok := index.(*slim.Int)
    if ok {
        if intIdx.Value >= 0 && intIdx.Value < int64(len(o.Value)) {
            return &slim.String{Value: o.Value[intIdx.Value]}, nil
        }

        return nil, slim.ErrIndexOutOfBounds
    }

    strIdx, ok := index.(*slim.String)
    if ok {
        for vidx, str := range o.Value {
            if strIdx.Value == str {
                return &slim.Int{Value: int64(vidx)}, nil
            }
        }

        return slim.UndefinedValue, nil
    }

    return nil, slim.ErrInvalidIndexType
}

func (o *StringArray) IndexSet(index, value slim.Object) error {
    strVal, ok := slim.ToString(value)
    if !ok {
        return slim.ErrInvalidIndexValueType
    }

    intIdx, ok := index.(*slim.Int)
    if ok {
        if intIdx.Value >= 0 && intIdx.Value < int64(len(o.Value)) {
            o.Value[intIdx.Value] = strVal
            return nil
        }

        return slim.ErrIndexOutOfBounds
    }

    return slim.ErrInvalidIndexType
}

If we implement CanCall and Call:

func (o *StringArray) CanCall() bool {
    return true
}

func (o *StringArray) Call(args ...slim.Object) (ret slim.Object, err error) {
    if len(args) != 1 {
        return nil, slim.ErrWrongNumArguments
    }

    s1, ok := slim.ToString(args[0])
    if !ok {
        return nil, slim.ErrInvalidArgumentType{
            Name:     "first",
            Expected: "string",
            Found:    args[0].TypeName(),
        }
    }

    for i, v := range o.Value {
        if v == s1 {
            return &slim.Int{Value: int64(i)}, nil
        }
    }

    return slim.UndefinedValue, nil
}

Then it can be "invoked":

s := slim.NewScript([]byte(`
    print(my_list("two"))
`))

myList := &StringArray{Value: []string{"one", "two", "three"}}
s.Add("my_list", myList)  // add StringArray value 'my_list'
s.Run()                   // prints "1" (index of "two")

We can also make StringArray iterable:

func (o *StringArray) CanIterate() bool {
    return true
}

func (o *StringArray) Iterate() slim.Iterator {
    return &StringArrayIterator{
        strArr: o,
    }
}

type StringArrayIterator struct {
    slim.ObjectImpl
    strArr *StringArray
    idx    int
}

func (i *StringArrayIterator) TypeName() string {
    return "string-array-iterator"
}

func (i *StringArrayIterator) Next() bool {
    i.idx++
    return i.idx <= len(i.strArr.Value)
}

func (i *StringArrayIterator) Key() slim.Object {
    return &slim.Int{Value: int64(i.idx - 1)}
}

func (i *StringArrayIterator) Value() slim.Object {
    return &slim.String{Value: i.strArr.Value[i.idx-1]}
}

ObjectImpl

ObjectImpl represents a default Object Implementation. To defined a new value type, one can embed ObjectImpl in their type declarations to avoid implementing all non-significant methods. TypeName() and String() methods still need to be implemented.