luci: Index | Files

package interpreter

import ""

Package interpreter contains basic skylark interpreter with some features that make it useful for non-trivial scripts:

* Scripts can load other script from the file system.
* There's one designated "interpreter root" directory that can be used to
  load scripts given their path relative to this root. For example,
  load("//some/", ...).
* Scripts can load protobuf message descriptors (built into the interpreter
  binary) and instantiate protobuf messages defined there.
* Scripts have access to some built-in skylark modules (aka 'stdlib'),
  supplied by whoever instantiates Interpreter.
* All symbols from "" stdlib module are available globally to all
  non-stdlib scripts.

Additionally, all script have access to some predefined symbols:

`proto`, a library with protobuf helpers, see skylarkproto.ProtoLib.

def struct(**kwargs):
  """Returns an object resembling namedtuple from Python.

  kwargs will become attributes of the returned object.
  See also skylarkstruct package.

def fail(msg):
  """Aborts the script execution with an error message."""

def mutable(value=None):
  """Returns an object with get and set methods.

  Allows modules to explicitly declare that they wish to keep some
  mutable (non-frozen) state, which can be modified after the module has
  loaded (e.g. from an exported function).

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

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

    A string with its compact JSON serialization.


Package Files

fail.go interpreter.go mutable.go to_json.go


var ErrNoStdlibModule = errors.New("no such stdlib module")

ErrNoStdlibModule should be returned by the stdlib loader function if the requested module doesn't exist.

type Interpreter Uses

type Interpreter struct {
    // Root is a path to a root directory with scripts.
    // Scripts will be able to load other scripts from this directory using
    // "//path/rel/to/root" syntax in load(...).
    // Default is ".".
    Root string

    // Predeclared is a dict with predeclared symbols that are available globally.
    // They are available when loading stdlib and when executing user scripts.
    Predeclared skylark.StringDict

    // Usercode knows how to load source code files from the file system.
    // It takes a path relative to Root and returns its content (or an error).
    // Used by ExecFile and by load("<path>") statements inside skylark scripts.
    // The default implementation justs uses io.ReadFile.
    Usercode func(path string) (string, error)

    // Stdlib is a set of scripts that constitute a builtin standard library.
    // It is defined as function that takes a script path and returns its body
    // or ErrNoStdlibModule error if there's no such stdlib module.
    // The stdlib is preloaded before execution of other scripts. The stdlib
    // loading starts with script that can load other stdlib scripts
    // using load("builtin:<path>", ...).
    // All globals of script that do not start with '_' will become
    // available as globals in all user scripts.
    Stdlib func(path string) (string, error)

    // Logger is called by skylark's print(...) function.
    // The callback takes the position in the skylark source code where print(...)
    // was called and the message it received.
    // The default implementation just prints the message to stderr.
    Logger func(file string, line int, message string)
    // contains filtered or unexported fields

Interpreter knows how to load skylark scripts that can load other skylark scripts, instantiate protobuf messages (with descriptors compiled into the interpreter binary) and have access to some built-in standard library.

func (*Interpreter) ExecFile Uses

func (intr *Interpreter) ExecFile(path string) (skylark.StringDict, error)

ExecFile executes a skylark script file in an environment that has all builtin symbols and all stdlib symbols.

Returns the global dict of the executed script.

func (*Interpreter) Init Uses

func (intr *Interpreter) Init() error

Init initializes the interpreter and loads the stdlib.

Registers most basic built in symbols first (like 'struct' and 'fail'). Then executes '' from stdlib, which may define more symbols or override already defined ones. Initializes intr.Usercode and intr.Logger if they are not set.

Whatever symbols end up in the global dict of stdlib script will become available as global symbols in all scripts executed via ExecFile (or transitively loaded by them).

Package interpreter imports 12 packages (graph). Updated 2018-08-21. Refresh now. Tools for package owners.