1*4947cdc7SCole Faustpackage starlarkstruct 2*4947cdc7SCole Faust 3*4947cdc7SCole Faustimport ( 4*4947cdc7SCole Faust "fmt" 5*4947cdc7SCole Faust 6*4947cdc7SCole Faust "go.starlark.net/starlark" 7*4947cdc7SCole Faust) 8*4947cdc7SCole Faust 9*4947cdc7SCole Faust// A Module is a named collection of values, 10*4947cdc7SCole Faust// typically a suite of functions imported by a load statement. 11*4947cdc7SCole Faust// 12*4947cdc7SCole Faust// It differs from Struct primarily in that its string representation 13*4947cdc7SCole Faust// does not enumerate its fields. 14*4947cdc7SCole Fausttype Module struct { 15*4947cdc7SCole Faust Name string 16*4947cdc7SCole Faust Members starlark.StringDict 17*4947cdc7SCole Faust} 18*4947cdc7SCole Faust 19*4947cdc7SCole Faustvar _ starlark.HasAttrs = (*Module)(nil) 20*4947cdc7SCole Faust 21*4947cdc7SCole Faustfunc (m *Module) Attr(name string) (starlark.Value, error) { return m.Members[name], nil } 22*4947cdc7SCole Faustfunc (m *Module) AttrNames() []string { return m.Members.Keys() } 23*4947cdc7SCole Faustfunc (m *Module) Freeze() { m.Members.Freeze() } 24*4947cdc7SCole Faustfunc (m *Module) Hash() (uint32, error) { return 0, fmt.Errorf("unhashable: %s", m.Type()) } 25*4947cdc7SCole Faustfunc (m *Module) String() string { return fmt.Sprintf("<module %q>", m.Name) } 26*4947cdc7SCole Faustfunc (m *Module) Truth() starlark.Bool { return true } 27*4947cdc7SCole Faustfunc (m *Module) Type() string { return "module" } 28*4947cdc7SCole Faust 29*4947cdc7SCole Faust// MakeModule may be used as the implementation of a Starlark built-in 30*4947cdc7SCole Faust// function, module(name, **kwargs). It returns a new module with the 31*4947cdc7SCole Faust// specified name and members. 32*4947cdc7SCole Faustfunc MakeModule(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) { 33*4947cdc7SCole Faust var name string 34*4947cdc7SCole Faust if err := starlark.UnpackPositionalArgs(b.Name(), args, nil, 1, &name); err != nil { 35*4947cdc7SCole Faust return nil, err 36*4947cdc7SCole Faust } 37*4947cdc7SCole Faust members := make(starlark.StringDict, len(kwargs)) 38*4947cdc7SCole Faust for _, kwarg := range kwargs { 39*4947cdc7SCole Faust k := string(kwarg[0].(starlark.String)) 40*4947cdc7SCole Faust members[k] = kwarg[1] 41*4947cdc7SCole Faust } 42*4947cdc7SCole Faust return &Module{name, members}, nil 43*4947cdc7SCole Faust} 44