Documentation
¶
Overview ¶
Package libutil holds the small helpers shared by Scriptling's standard and extension libraries. It depends only on the object and errors packages, so a library can use it without linking any other library.
Index ¶
- func AttachSubLibraries(registrar object.LibraryRegistrar, parent *object.Library, attrs ...string)
- func CancelledError(ctx context.Context) object.Object
- func EnvFromContext(ctx context.Context) *object.Environment
- func ErrorJSONBody(message string) string
- func ModuleRef(env *object.Environment, funcName string) string
- func NewAliasedLibrary(lib *object.Library, name string) *object.Library
- func RegisterCleanup(fn func())
- func RegisterSubLibrary(registrar object.LibraryRegistrar, lib *object.Library)
- func RunCleanups()
- type LibraryLookup
- type NativeBudget
- type ResultList
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AttachSubLibraries ¶
func AttachSubLibraries(registrar object.LibraryRegistrar, parent *object.Library, attrs ...string)
AttachSubLibraries sets, on parent, an attribute for each of the named sub-libraries (parent.Name()+"."+attr) that is already registered with registrar. parent must have a non-nil constants map and should be a library built for this registrar alone, since attaching mutates it. A registrar that cannot look libraries up attaches nothing.
func CancelledError ¶
CancelledError reports a cancelled context as the evaluator does, nil when the context is live.
func EnvFromContext ¶
func EnvFromContext(ctx context.Context) *object.Environment
EnvFromContext returns the environment of the call ctx belongs to, or a fresh empty environment when the context carries none.
func ErrorJSONBody ¶
ErrorJSONBody renders {"error": <message>} as a JSON body with proper escaping. Protocol endpoints report failures through it, so an error (or a user message) containing quotes or control characters must not be spliced into the body with Sprintf, which would emit malformed JSON.
func ModuleRef ¶
func ModuleRef(env *object.Environment, funcName string) string
ModuleRef derives the "module.function" reference for a decorated function defined in env. It uses __name__, falling back to the base name of __file__ when __name__ is "__main__" (as in a setup script). It returns "" when neither gives a module name.
func NewAliasedLibrary ¶
NewAliasedLibrary re-names a library so it can be imported under more than one module name. The functions and constants are shared by reference.
func RegisterCleanup ¶
func RegisterCleanup(fn func())
RegisterCleanup registers a function to be called by RunCleanups. Libraries that hold process-wide state (open sockets, cluster memberships) register one so a host resetting the runtime releases that state, without the library having to import the runtime library to say so.
func RegisterSubLibrary ¶
func RegisterSubLibrary(registrar object.LibraryRegistrar, lib *object.Library)
RegisterSubLibrary registers lib with registrar and, when the parent library (lib's name up to its last dot) is already registered there, sets the sub-library's dict as the parent's attribute named after the last component of lib's name. Parent libraries registered later pick up already-registered sub-libraries with AttachSubLibraries, so the two can be registered in either order. A registrar that cannot look libraries up only gets lib registered; `import parent.sub` still works then.
func RunCleanups ¶
func RunCleanups()
RunCleanups calls every registered cleanup function, in registration order. The functions stay registered, so each call runs them all again; they manage their own synchronisation and are called without any lock held.
Types ¶
type LibraryLookup ¶
LibraryLookup is implemented by registrars that can return a library already registered with them (scriptling.Scriptling does). Sub-library registration uses it to expose a sub-library as an attribute of its parent library, so `import parent as p; p.sub.fn()` works without a separate `import parent.sub`.
type NativeBudget ¶
type NativeBudget struct {
// contains filtered or unexported fields
}
NativeBudget bounds data a library gathers natively before it becomes script values: the bytes gathered so far are checked against the memory left every nativeCheckBytes, along with the call's cancellation. One budget is shared by every goroutine gathering for the same call, so what they hold between them is what is checked.
func NewNativeBudget ¶
func NewNativeBudget(ctx context.Context, what string) *NativeBudget
NewNativeBudget starts a budget for the call ctx belongs to. what names the data being gathered in the error raised when it does not fit.
type ResultList ¶
type ResultList struct {
// contains filtered or unexported fields
}
ResultList builds a list for a script, checking each growth of its backing array against the value limit and the memory left, and the call's cancellation every resultCheckEvery items.
func NewResultList ¶
NewResultList starts a list expected to hold about hint items. The hint is checked up front; a hint too large to admit is an error before anything is built.