go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/lucicfg/native.go (about)

     1  // Copyright 2018 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package lucicfg
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  
    21  	"go.starlark.net/starlark"
    22  	"go.starlark.net/starlarkstruct"
    23  
    24  	"go.chromium.org/luci/starlark/interpreter"
    25  )
    26  
    27  // nativeCall carries arguments for nativeFunc to avoid ridiculously long
    28  // func declarations.
    29  type nativeCall struct {
    30  	Ctx    context.Context
    31  	State  *State
    32  	Thread *starlark.Thread
    33  	Fn     *starlark.Builtin
    34  	Args   starlark.Tuple
    35  	Kwargs []starlark.Tuple
    36  }
    37  
    38  // unpack unpacks the positional arguments into corresponding variables.
    39  //
    40  // See starlark.UnpackPositionalArgs for more info.
    41  func (c *nativeCall) unpack(min int, vars ...any) error {
    42  	return starlark.UnpackPositionalArgs(c.Fn.Name(), c.Args, c.Kwargs, min, vars...)
    43  }
    44  
    45  // nativeFunc is callable from Starlark side via __native__.<name>(...).
    46  //
    47  // It gets the state of the generator and can potentially examine or mutate it.
    48  // It must not depend on any other global state.
    49  type nativeFunc func(call nativeCall) (starlark.Value, error)
    50  
    51  // All native functions exposed to the starlark code.
    52  //
    53  // Populated during init() time via declNative(...) calls.
    54  var nativeFuncs = map[string]nativeFunc{}
    55  
    56  // declNative registers a native function during init() time.
    57  //
    58  // Panics if such function has already been registered.
    59  func declNative(name string, f nativeFunc) {
    60  	if nativeFuncs[name] != nil {
    61  		panic(fmt.Sprintf("native function %q is already registered", name))
    62  	}
    63  	nativeFuncs[name] = f
    64  }
    65  
    66  // native returns a starlark struct with all registered native functions.
    67  //
    68  // This struct is put into the global starlark namespace as '__native__'. Native
    69  // functions can access the generator state through the context associated with
    70  // the starlark thread that executes them.
    71  func native(extra starlark.StringDict) starlark.Value {
    72  	dict := make(starlark.StringDict, len(nativeFuncs)+len(extra))
    73  	for name, cb := range nativeFuncs {
    74  		cb := cb
    75  		dict[name] = starlark.NewBuiltin(name, func(th *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
    76  			ctx := interpreter.Context(th)
    77  			return cb(nativeCall{
    78  				Ctx:    ctx,
    79  				State:  ctxState(ctx),
    80  				Thread: th,
    81  				Fn:     fn,
    82  				Args:   args,
    83  				Kwargs: kwargs,
    84  			})
    85  		})
    86  	}
    87  	for k, v := range extra {
    88  		dict[k] = v
    89  	}
    90  	return starlarkstruct.FromStringDict(starlark.String("__native__"), dict)
    91  }