luci: go.chromium.org/luci/starlark/builtins Index | Files

package builtins

import "go.chromium.org/luci/starlark/builtins"

Package builtins is a collection of potentially useful Starlark builtins.

Index

Package Files

doc.go fail.go stacktrace.go struct.go to_json.go

Variables

var (
    // Struct is struct(**kwargs) builtin.
    //
    //  def struct(**kwargs):
    //    """Returns an immutable object with fields set to given values."""
    Struct = starlark.NewBuiltin("struct", starlarkstruct.Make)

    // GenStruct is genstruct(name) builtin.
    //
    //  def genstruct(name):
    //    """Returns a callable constructor for "branded" struct instances."""
    GenStruct = starlark.NewBuiltin("genstruct", func(_ *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
        var name string
        if err := starlark.UnpackArgs("genstruct", args, kwargs, "name", &name); err != nil {
            return nil, err
        }
        return &ctor{name: name}, nil
    })

    // Ctor is ctor(obj) builtin.
    //
    //  def ctor(obj):
    //    """Returns a constructor used to construct this struct or None."""
    Ctor = starlark.NewBuiltin("ctor", func(_ *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
        var obj starlark.Value
        if err := starlark.UnpackArgs("ctor", args, kwargs, "obj", &obj); err != nil {
            return nil, err
        }
        if st, ok := obj.(*starlarkstruct.Struct); ok {
            return st.Constructor(), nil
        }
        return starlark.None, nil
    })
)
var Fail = starlark.NewBuiltin("fail", func(_ *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
    if len(kwargs) != 0 {
        return nil, fmt.Errorf("'fail' doesn't accept kwargs")
    }
    if len(args) != 1 {
        return nil, fmt.Errorf("'fail' got %d arguments, wants 1", len(args))
    }
    msg, ok := starlark.AsString(args[0])
    if !ok {
        msg = args[0].String()
    }

    return nil, errors.New(msg)
})

Fail is fail(msg) builtin.

def fail(msg):
  """Aborts the script execution with an error message."""
var Stacktrace = starlark.NewBuiltin("stacktrace", func(th *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
    skip := starlark.MakeInt(0)
    if err := starlark.UnpackArgs("stacktrace", args, kwargs, "skip?", &skip); err != nil {
        return nil, err
    }
    switch lvl, err := starlark.AsInt32(skip); {
    case err != nil:
        return nil, fmt.Errorf("stacktrace: bad 'skip' value %s - %s", skip, err)
    case lvl < 0:
        return nil, fmt.Errorf("stacktrace: bad 'skip' value %d - must be non-negative", lvl)
    default:
        return CaptureStacktrace(th, lvl)
    }
})

Stacktrace is stacktrace(...) builtin.

def stacktrace(skip=0):
  """Capture and returns a stack trace of the caller.

  A captured stacktrace is an opaque object that can be stringified to get a
  nice looking trace (e.g. for error messages).

  Args:
    skip: how many inner most frames to skip.
  """
var ToJSON = starlark.NewBuiltin("to_json", func(_ *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
    var v starlark.Value
    if err := starlark.UnpackPositionalArgs(fn.Name(), args, kwargs, 1, &v); err != nil {
        return nil, err
    }
    obj, err := toGoNative(v, visitingSet{})
    if err != nil {
        return nil, err
    }
    blob, err := json.Marshal(obj)
    if err != nil {
        return nil, err
    }
    return starlark.String(blob), nil
})

ToJSON is to_json(value) builtin.

def to_json(value):
  """Serializes a value to compact JSON.

  Doesn't support integers that do not fit int64. Fails if the value being
  converted has cycles.

  Args:
    value: a starlark value: scalars, lists, tuples, dicts containing only
      starlark values.

  Returns:
    A string with its compact JSON serialization.
  """

type CapturedStacktrace Uses

type CapturedStacktrace struct {
    // contains filtered or unexported fields
}

CapturedStacktrace represents a stack trace returned by stacktrace(...).

At the present time it can only be stringified (via str(...) in Starlark or via .String() in Go).

func CaptureStacktrace Uses

func CaptureStacktrace(th *starlark.Thread, skip int) (*CapturedStacktrace, error)

CaptureStacktrace captures thread's stack trace, skipping some number of innermost frames.

Returns an error if the stack is not deep enough to skip the requested number of frames.

func (*CapturedStacktrace) Freeze Uses

func (*CapturedStacktrace) Freeze()

Freeze is part of starlark.Value interface.

func (*CapturedStacktrace) Hash Uses

func (*CapturedStacktrace) Hash() (uint32, error)

Hash is part of starlark.Value interface.

func (*CapturedStacktrace) String Uses

func (s *CapturedStacktrace) String() string

String is part of starlark.Value interface.

Renders the stack trace as string.

func (*CapturedStacktrace) Truth Uses

func (*CapturedStacktrace) Truth() starlark.Bool

Truth is part of starlark.Value interface.

func (*CapturedStacktrace) Type Uses

func (*CapturedStacktrace) Type() string

Type is part of starlark.Value interface.

Package builtins imports 6 packages (graph) and is imported by 2 packages. Updated 2018-12-14. Refresh now. Tools for package owners.