native

package
v0.15.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 15, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package native lets Go code define Kukel modules: functions, constants and native classes, whose objects carry a Go value and whose member functions are written in Go. A program uses one with "use name;", like a module written in Kukel, and reaches its names as name::x.

A module registers itself (Register, usually from an init function); the loader, the compiler and the VM look modules up by name. Native classes have no data members: everything goes through member functions. A Kukel class can derive from a native class that has a constructor; its objects then hold the Go value too, once the derived constructor has called the native one.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Global

func Global(qualified string) (value.Value, bool)

Global returns what a qualified name of a registered module stands for at run time: a function ("json::encode"), a static member function ("time::Time::parse") or a class ("time::Time"). The VM links the global slots of a program with it.

func Names

func Names() []string

Names returns the names of the registered modules, sorted.

func Omit added in v0.12.0

func Omit(name, tags string)

Omit records that module name is not in this kukel because of tags.

func Omitted added in v0.12.0

func Omitted(name string) (string, bool)

Omitted gives the build tags that left module name out, if they did.

func Prepare

func Prepare(m *Module)

Prepare readies m for use without registering it: for a module that belongs to one program (Config.Modules). Register prepares a module itself.

func Register

func Register(m *Module)

Register adds module m. It panics on a second module with the same name (a programming error).

Types

type Args

type Args struct {
	Name string // as in messages: "json::encode", "time::Time::format"
	V    []value.Value
}

Args are the arguments of a call of a native function, for checking them with messages like those of the built-ins.

func Check

func Check(name string, args []value.Value, min, max int) (Args, error)

Check checks the number of arguments: min to max (max < 0: no limit).

func (Args) Errorf

func (a Args) Errorf(kind string, format string, args ...any) error

Errorf returns a Kukel exception of the given kind (value.ValueError, value.TypeError, ...), prefixed with the function's name.

func (Args) Has

func (a Args) Has(i int) bool

Has reports whether optional argument i was passed.

func (Args) Int

func (a Args) Int(i int) (int64, error)

Int returns argument i, which must be an int.

func (Args) Number

func (a Args) Number(i int) (float64, error)

Number returns argument i, an int or a float, as a float.

func (Args) Object

func (a Args) Object(i int, c *Class) (any, error)

Object returns the Go value of argument i, which must be an object of class c or of a class derived from it.

func (Args) String

func (a Args) String(i int) (string, error)

String returns argument i, which must be a string.

func (Args) TypeError

func (a Args) TypeError(i int, want string) error

TypeError is the error for argument i of the wrong type.

type Class

type Class struct {
	Name    string
	New     value.NativeMethod
	Methods map[string]value.NativeMethod
	Statics map[string]Func
	Show    func(o *value.ObjectObj) string
	// contains filtered or unexported fields
}

Class is a native class. Methods and Statics are its instance and static member functions. New is its constructor (for "new name::C(...)"): it sets this.State; without one, objects only come from the module's functions (Class.Object). Show formats an object for str() and println; without it an object shows as "name::C{}".

func (*Class) ClassObj

func (c *Class) ClassObj() *value.ClassObj

ClassObj is the run-time class.

func (*Class) Is added in v0.9.0

func (c *Class) Is(v value.Value) bool

Is reports whether v is an object of class c, or of a class derived from it, with its Go value set up.

func (*Class) Object

func (c *Class) Object(state any) value.Value

Object makes a new object of class c holding state.

func (*Class) QualifiedName

func (c *Class) QualifiedName() string

QualifiedName is "module::Class".

type Config

type Config struct {
	Modules        []*Module       // this program's own modules (prepared), found before the registered ones
	AllowedModules map[string]bool // registered modules the program may use; nil: all
	DeniedBuiltins map[string]bool // built-ins the program may not use
	Path           []string        // directories where "use" looks for name.kkl after the using file's directory
	NoFileModules  bool            // "use" never reads .kkl files (modules written in Go only)
}

Config is what one program may use beyond its own code: modules written for it (by a Go program that embeds Kukel), which of the registered modules and built-ins are allowed, and where "use" finds modules written in Kukel. The compiler, the loader and the VM (also of the program's workers) follow it. A nil *Config allows everything and finds Kukel modules only next to the files that use them.

func (*Config) BuiltinDenied

func (c *Config) BuiltinDenied(name string) bool

BuiltinDenied reports whether the program may not use built-in name.

func (*Config) Disallowed

func (c *Config) Disallowed(name string) bool

Disallowed reports whether name is a registered module the program may not use (for a clearer message than "not found").

func (*Config) Global

func (c *Config) Global(qualified string) (value.Value, bool)

Global is the run-time value of a qualified name of a module the program may use (see the function Global).

func (*Config) Lookup

func (c *Config) Lookup(name string) (*Module, bool)

Lookup finds module name: one of the program's own, else a registered module if it is allowed.

type Func

type Func func(env value.Env, args []value.Value) (value.Value, error)

Func is a function written in Go, as for the built-ins.

type Module

type Module struct {
	Name    string
	Doc     string // one line: what the module is for
	Funcs   map[string]Func
	Consts  map[string]value.Value // nil, booleans, numbers and strings
	Classes []*Class
	// contains filtered or unexported fields
}

Module is a module written in Go.

func Lookup

func Lookup(name string) (*Module, bool)

Lookup returns the module called name.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL