Help us shape the future of Go! Take the 2026 Go Developer Survey

native

package
v0.38.0 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2026 License: MIT Imports: 13 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

View Source
var MaxSocketPath = func() int {
	if runtime.GOOS == "linux" || runtime.GOOS == "android" {
		return 107
	}
	return 103
}()

MaxSocketPath is the longest path of a Unix socket the system takes, in bytes: a longer one fails with just "invalid argument".

Functions

func Duration added in v0.27.0

func Duration(seconds float64) time.Duration

Duration converts seconds (not negative) to a duration. Beyond the longest duration, about 292 years, it gives the longest: as good as no limit.

func Flush added in v0.27.0

func Flush(env value.Env)

Flush writes out what the program's standard output and standard error hold in a buffer (kukel buffers standard output; the writers of a Go program that embeds Kukel may not buffer). A function that waits, for time, a message or an event, calls it first: what the program wrote before the wait is seen during it.

func Generator added in v0.34.0

func Generator(env value.Env, name string, next func() (value.Value, bool, error)) (value.Value, error)

Generator makes a generator (Q86) whose values come from next: it gives the next value, or false at the end; an error ends the generator too. for-each walks it as one written in Kukel, so that a member function iter() returning it makes the objects of a native class iterable. name is how it shows ("fs::File::iter").

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 IOError added in v0.31.0

func IOError(a Args, err error) error

IOError is the IOError for err, worded by IOMessage.

func IOMessage added in v0.31.0

func IOMessage(err error) string

IOMessage is what went wrong with a file, without Go's name of the operation: "nope.kkl: no such file or directory" for "open nope.kkl: no such file or directory", and "from -> to: ..." for a rename. Module fs words its IOErrors so; other modules that read files follow it.

func Names

func Names() []string

Names returns the names of the registered modules, sorted.

func NetMessage added in v0.31.0

func NetMessage(err error) string

NetMessage is what went wrong in reaching or listening on a network address, without Go's words around it: "connection refused" for "dial tcp 127.0.0.1:1: connect: connection refused", "host x not found" for a name that cannot be looked up. The caller names the address.

func Omit added in v0.12.0

func Omit(name, why string)

Omit records that module name is not in this kukel, and why: "it was built with the build tag nozmq", "it was built without the build tag tk".

func Omitted added in v0.12.0

func Omitted(name string) (string, bool)

Omitted gives why module name is not in this kukel, if a build tag left it out.

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 Program added in v0.33.1

func Program(env value.Env) string

Program is what SetProgram set, or "".

func Register

func Register(m *Module)

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

func SetProgram added in v0.33.1

func SetProgram(env value.Env, path string)

SetProgram sets what program() gives in the interpreter of env: the path of the program's file as it was given (kukel run a.kkl: "a.kkl").

func SetTerminal added in v0.38.0

func SetTerminal(env value.Env, in, out *os.File)

SetTerminal tells the interpreter of env that its standard input and output are the files in and out of the process (kukel run and exec), so that module term may make them raw and ask the size of the terminal they are, if they are one. An interpreter without it (one a Go program makes) has no terminal: term reads keys from its standard input as it is, and changes no terminal.

func Terminal added in v0.38.0

func Terminal(env value.Env) (in, out *os.File)

Terminal gives what SetTerminal set, or nil files.

func TypeName added in v0.31.0

func TypeName(v value.Value) string

TypeName names what v is, for a message: its kind ("int", "vector"), or for an object its class ("geo::Point").

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) NotSetUp added in v0.31.0

func (a Args) NotSetUp(what string, c *Class, v value.Value) error

NotSetUp is the ValueError for v, an object of a class derived from c whose constructor did not call c's; what says where v is ("argument 2", "element 1 of argument 1").

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) Seconds added in v0.27.0

func (a Args) Seconds(i int, what string) (time.Duration, error)

Seconds returns argument i, a number of seconds that is not negative, as a duration; what names it in the message for another number ("a usable timeout"). See Duration for very long times.

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: it names the kind of the argument, or for an object its class.

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) Derived added in v0.31.0

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

Derived reports whether v is an object of class c, or of a class derived from it, set up or not (see NotSetUp).

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.

type Waiter added in v0.31.0

type Waiter struct {
	// contains filtered or unexported fields
}

Waiter is a wait that Wait began; Done ends it.

func Wait added in v0.31.0

func Wait(env value.Env) Waiter

Wait is called by a function that begins to wait, for time, a message, an event or input: it flushes output (see Flush), and the Done of what it gives tells when the wait ended, so that a profile of running time (kukel profile -cpu) can leave the wait out:

defer native.Wait(env).Done()

When nothing profiles the program, it costs no more than Flush.

func (Waiter) Done added in v0.31.0

func (w Waiter) Done()

Done ends the wait (a zero Waiter: nothing).

Jump to

Keyboard shortcuts

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