github.com/arnodel/golua@v0.0.0-20230215163904-e0b5347eaaa1/runtime/gofunction.go (about)

     1  package runtime
     2  
     3  type GoFunctionFunc func(*Thread, *GoCont) (Cont, error)
     4  
     5  // A GoFunction is a callable value implemented by a native Go function.
     6  type GoFunction struct {
     7  	f           GoFunctionFunc
     8  	safetyFlags ComplianceFlags
     9  	name        string
    10  	nArgs       int
    11  	hasEtc      bool
    12  }
    13  
    14  var _ Callable = (*GoFunction)(nil)
    15  
    16  // NewGoFunction returns a new GoFunction.
    17  func NewGoFunction(f GoFunctionFunc, name string, nArgs int, hasEtc bool) *GoFunction {
    18  	return &GoFunction{
    19  		f:      f,
    20  		name:   name,
    21  		nArgs:  nArgs,
    22  		hasEtc: hasEtc,
    23  	}
    24  }
    25  
    26  // Continuation implements Callable.Continuation.
    27  func (f *GoFunction) Continuation(t *Thread, next Cont) Cont {
    28  	return NewGoCont(t, f, next)
    29  }
    30  
    31  // SolemnlyDeclareCompliance adds compliance flags to f.  See quotas.md for
    32  // details about compliance flags.
    33  func (f *GoFunction) SolemnlyDeclareCompliance(flags ComplianceFlags) {
    34  	if flags >= complyflagsLimit {
    35  		// User is trying to register a safety flag that is not (yet) defined.
    36  		// This is a sign this function is not called solemnly enough!
    37  		panic("Invalid safety flags")
    38  	}
    39  	f.safetyFlags |= flags
    40  }
    41  
    42  // SolemnlyDeclareCompliance is a convenience function that adds the same set of
    43  // compliance flags to a number of functions.  See quotas.md for details about
    44  // compliance flags.
    45  func SolemnlyDeclareCompliance(flags ComplianceFlags, fs ...*GoFunction) {
    46  	for _, f := range fs {
    47  		f.SolemnlyDeclareCompliance(flags)
    48  	}
    49  }