lua

package
v0.0.0-...-4fe8727 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2019 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	True  = Bool(true)
	False = Bool(false)
)
View Source
const (

	// Lua search paths.
	LUAPATH_DEFAULT = LUA_DIR + "?.lua;" + LUA_DIR + "?/init.lua;" + GO_DIR + "?.lua;" + GO_DIR + "?/init.lua;" + "./?.lua;" + "./?/init.lua"
	LUA_DIR         = ROOT_DEFAULT + "share/lua/5.3/"
	LUAPATH         = "GLUA_PATH"
	// Go search paths.
	GOPATH_DEFAULT = GO_DIR + "?.so;" + GO_DIR + "loadall.so;" + "./?.so"
	GO_DIR         = ROOT_DEFAULT + "lib/lua/5.3/"
	GOPATH         = "GLUA_GOPATH"
	// System root paths.
	ROOT_DEFAULT = "/usr/local/"
	ROOT         = "LUA_ROOT"

	// PATH_MARK is the string that marks the substitution points in a template.
	PATH_MARK = "?"

	// PATH_SEP is the character that separates templates in a path.
	PATH_SEP = ";"

	// EXEC_DIR in a Windows path is replaced by the executable's directory.
	EXEC_DIR = "!"

	// LUA_DIRSEP is the directory separator (for submodules).
	//
	// CHANGE it if your machine does not use "/" as the directory
	// separator and is not Windows.
	//
	// Windows Lua automatically uses "\".
	DIR_SEP = "/"

	// MOD_SEP (LUA_CSUBSEP/LUA_LSUBSEP) is the character that replaces dots
	// in submodule names when searching for a Go/Lua loader.
	MOD_SEP = DIR_SEP

	// LUA_IGMARK is a mark to ignore all before it when building
	// the luaopen_ function name.
	IGNORE_MARK = "-"
)

Configuration for paths

View Source
const (
	NilType    = code.NilType
	BoolType   = code.BoolType
	NumberType = code.NumberType
	StringType = code.StringType
	TableType  = code.TableType
	FuncType   = code.FuncType
	GoType     = code.GoType
	ThreadType = code.ThreadType
	IntType    = code.IntType
	FloatType  = code.FloatType
)
View Source
const EnvID = "_ENV"
View Source
const Nil = nilType(0)

Variables

This section is empty.

Functions

func ArgErr

func ArgErr(arg int, err error) error

"bad argument #arg to 'funcname' (extramsg)"

func Compare

func Compare(t *Thread, op Op, x, y Value) (bool, error)

func Equals

func Equals(t *Thread, x, y Value) (bool, error)

func ExactArgs

func ExactArgs(want int) argsCheck

func IsBool

func IsBool(v Value) bool

IsBool reports whether the Value is a Bool.

func IsFloat

func IsFloat(v Value) bool

IsFloat reports whether the Value is a Float;

That is, the value is a Number and is represented as a Float.

func IsFunction

func IsFunction(v Value) bool

IsFunction reports whether the Value implements the Function interface (i.e. *Func or *GoFunc).

func IsGoFunc

func IsGoFunc(v Value) bool

IsGoFunc reports whether the Value is a *GoFunc.

func IsGoValue

func IsGoValue(v Value) bool

IsGoValue reports whether the Value is a *GoValue.

func IsInt

func IsInt(v Value) bool

IsInt reports whether the Value is an Int;

That is, the value is a Number and is represented as an Int.

func IsNil

func IsNil(v Value) bool

IsNil reports whether the Value is nil.

func IsNumber

func IsNumber(v Value) bool

IsNumber reports whether the Value is a Number or a String convertible to a Number.

func IsString

func IsString(v Value) bool

IsString reports whether the Value is a String or a Number (which is always convertible to a string).

func IsTable

func IsTable(v Value) bool

IsTable reports whether the Value is a *Table.

func IsThread

func IsThread(v Value) bool

IsThread reports whether the Value is a *Thread.

func MaxArgs

func MaxArgs(max int) argsCheck

func MinArgs

func MinArgs(min int) argsCheck

func RangeArgs

func RangeArgs(min, max int) argsCheck

func Truth

func Truth(v Value) bool

Truth converts the Value to a Go bool value.

Returns true for any Lua value different from false and nil; otherwise it returns false.

func TypeErr

func TypeErr(arg int, typ, want string) error

"'typename' expected, got 'typename'"

func TypeName

func TypeName(v Value) string

func ValidArgs

func ValidArgs(kinds ...code.Type) argsCheck

Types

type Bool

type Bool bool

func (Bool) String

func (v Bool) String() string

func (Bool) Type

func (v Bool) Type(t *Thread) Type

type Callable

type Callable interface {
	// contains filtered or unexported methods
}

func ToCallable

func ToCallable(v Value) Callable

ToCallable converts the Value to a Callable value (either Lua or Go function).

On success, returns the function; otherwise nil.

type Config

type Config struct {
	Stdlib func(*Thread) error
	Import Importer
	GoPath Path
	Path   Path
	Trace  bool
	NoEnv  bool
}

type Constant

type Constant interface {
	Value
	// contains filtered or unexported methods
}

type Context

type Context interface {
	GoLoader(*Thread, string, string) (Value, error)
	Searchers() *Table
	Preload() *Table
	Globals() *Table
	Values() *Table
	Loaded() *Table
	Config() *Config
}

type Debug

type Debug interface {
	// Options(what string) Debug
	CurrentLine() int
	SourceID() string
	ShortSrc() string
	ParamN() int
	UpVarN() int
	Name() string
	Kind() string
	Span() (int, int)
	Vararg() bool
	Tailcall() bool
	Where() string
}

type Error

type Error interface {
	error
	// contains filtered or unexported methods
}

type Float

type Float float64

func ToFloat

func ToFloat(v Value) (Float, bool)

ToFloat converts the Value to a Float.

On success, returns the Float and true; otherwise 0.0 and false.

func (Float) String

func (v Float) String() string

func (Float) Type

func (v Float) Type(t *Thread) Type

type Func

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

A Func represents a Lua function value.

func LoadFile

func LoadFile(t *Thread, file string) (*Func, error)

func (*Func) String

func (cls *Func) String() string

closure represents a closure value embedded into a callable values.

func (*Func) Type

func (v *Func) Type(t *Thread) Type

type Function

type Function interface {
	Callable
}

func ToFunction

func ToFunction(v Value) Function

ToFunction converts the Value to a Function (either Lua or Go function).

On success, returns the function; otherwise nil.

type GoFunc

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

GoFunc represents a Go builtin function value.

func Closure

func Closure(impl func(*Thread, Tuple) ([]Value, error), vars ...Value) *GoFunc

func NewGoFunc

func NewGoFunc(name string, impl func(*Thread, Tuple) ([]Value, error), vars ...Value) *GoFunc

NewGoFunc creates and returns a new *GoFunc.

func ToGoFunc

func ToGoFunc(v Value) *GoFunc

ToGoFunc converts the Value to a *GoFunc.

On success, returns the *GoFunc; otherwise nil.

func WithArgs

func WithArgs(check argsCheck, fn *GoFunc) *GoFunc

func (*GoFunc) String

func (cls *GoFunc) String() string

closure represents a closure value embedded into a callable values.

func (*GoFunc) Type

func (v *GoFunc) Type(t *Thread) Type

type GoValue

type GoValue struct {
	Value interface{}
	// contains filtered or unexported fields
}

func ToGoValue

func ToGoValue(v Value) *GoValue

ToGoValue converts the Value to a *GoValue.

On success, returns the *GoValue; otherwise nil.

func (*GoValue) String

func (v *GoValue) String() string

func (*GoValue) Type

func (v *GoValue) Type(t *Thread) Type

type HasMeta

type HasMeta interface {
	SetMeta(*Table)
	Meta() *Table
}

type Importer

type Importer interface {
	Import(*Thread, string) error
}

type Int

type Int int64

func Length

func Length(t *Thread, x Value) (Int, error)

func ToInt

func ToInt(v Value) (Int, bool)

ToInt converts the Value to a Int.

On success, returns the Int and true; otherwise 0 and false.

func (Int) String

func (v Int) String() string

func (Int) Type

func (v Int) Type(t *Thread) Type

type Library

type Library struct {
	Funcs []*GoFunc
	Open  Loader
	Name  string
}

type Loader

type Loader func(*Thread) (Value, error)
func Search(ls *Thread, module string) (Loader, error)

type Number

type Number interface {
	Constant
	// contains filtered or unexported methods
}

func ToNumber

func ToNumber(v Value) Number

ToNumber converts the Value to a Number.

On success, returns the Number; otherwise nil.

type Op

type Op int
const (
	OpNone Op = iota

	// binary ops
	OpAdd
	OpSub
	OpMul
	OpMod
	OpPow
	OpDivF
	OpDivI
	OpBand
	OpBor
	OpBxor
	OpShl
	OpShr
	OpConcat
	OpEq
	OpLt
	OpLe
	OpNe
	OpGt
	OpGe
	OpAnd
	OpOr

	// unary ops
	OpMinus
	OpBnot
	OpNot
	OpLen
)

func (Op) String

func (op Op) String() string

type Path

type Path string

@path templates

A typical path is a list of directories wherein to search for a given file. The path used by require is a list of "templates", each of them specifying an alternative way to transform a module name (the argument to require) into a file name. Each template in the path is a file name containing optional question marks. For each template, require substitutes the module name for each question mark and checks whether there is a file with the resulting name; if not, it goes to the next template. The templates in a path are separated by semicolons, a character seldom used for file names in most operating systems.

For instance, consider the path: "?;?.lua;c:\windows\?;/usr/local/lua/?/?.lua"

With this path, the call require"sql" will try to open the following Lua files:

sql
sql.lua
c:\windows\sql
/usr/local/lua/sql/sql.lua

The path require uses to search for Lua files is aways the current value of the variable "package.path". When the module package is initialized, it sets the variable with the value of the environment variable LUA_PATH_5_3; if this environment variable is undefined, Lua tries the environment variable LUA_PATH. If both are undefined, Lua uses a compiled-defined default path (-E prevents the use of these environment variables and forces the default).

When using the value of an environment variable, Lua substitues the default path for any substring ";;". For instance, if we set LUA_PATH_5_3 to "mydir/?.lua;;", the final path will be the template "mydir/?.lua" followed by the default path.

Path is a template (or list of templates separated by ';') used to search for Lua and Go packages.

func (Path) Search

func (tpl Path) Search(name, sep, dirsep string) (string, error)

Search searches for the given name in the given path.

A path is a string containing a sequence of templates separated by semicolons. For each template, the function replaces each interrogation mark (if any) in the template with a copy of name wherein all occurences of sep (a dot, by default) were replaced by rep (the system's directory separator, by default), and then tries to ope nthe resulting file name.

For instance, if the path is the string:

"./?.lua;./.lc;/usr/local/?/init.lua"

The search for the name "foo.bar" will try to open the files (in order):

"./foo/bar.lua"
"./foo/bar.lc"
"/usr/local/foo/bar/init.lua"

Returns the resulting name of the first file that it can open in read mode (after closing the file), or "" and the error if none succeeds (this error message lists all the file names it tried to open). func SearchPath(ls *Thread, name, path, sep, dirsep string) (string, error) {

type String

type String string

func ToString

func ToString(v Value) (s String, ok bool)

ToString converts the Value to a String.

On success, returns the String and true; otherwise "" and false.

func (String) String

func (v String) String() string

func (String) Type

func (v String) Type(t *Thread) Type

type Table

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

func NewTable

func NewTable() *Table

func NewTableFromMap

func NewTableFromMap(kvs map[string]Value) *Table

func NewTableSize

func NewTableSize(arrN, kvsN int) *Table

func ToTable

func ToTable(v Value) *Table

ToTable converts the Value to a *Table.

On success, returns the *Table; otherwise nil.

func (*Table) Get

func (t *Table) Get(k Value) Value

func (*Table) Length

func (t *Table) Length() Int

func (*Table) Meta

func (t *Table) Meta() *Table

func (*Table) Next

func (t *Table) Next(key Value) (k, v Value, ok bool)

func (*Table) Set

func (t *Table) Set(k, v Value)

func (*Table) SetMeta

func (t *Table) SetMeta(meta *Table)

func (*Table) Slice

func (t *Table) Slice() (slice []Value)

func (*Table) String

func (t *Table) String() string

func (*Table) Type

func (v *Table) Type(t *Thread) Type

type Thread

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

func Init

func Init(config *Config) (*Thread, error)

func Must

func Must(ls *Thread, err error) *Thread

func ToThread

func ToThread(v Value) *Thread

ToThread converts the Value to a *Thread.

On success, returns the *Thread; otherwise nil.

func (*Thread) Call

func (t *Thread) Call(fv Value, args ...Value) ([]Value, error)

func (*Thread) CallN

func (t *Thread) CallN(fv Value, args []Value, want int) ([]Value, error)

func (*Thread) Caller

func (t *Thread) Caller(level int) Debug

func (*Thread) Context

func (t *Thread) Context() Context

func (*Thread) Exec

func (t *Thread) Exec(chunk *code.Chunk, args ...Value) ([]Value, error)

func (*Thread) ExecN

func (t *Thread) ExecN(chunk *code.Chunk, args []Value, want int) ([]Value, error)

func (*Thread) Global

func (t *Thread) Global(name string) Value

func (*Thread) Globals

func (t *Thread) Globals() *Table

func (*Thread) Index

func (t *Thread) Index(tbl, key Value) (Value, error)

func (*Thread) IsMainThread

func (t *Thread) IsMainThread() bool

func (*Thread) Load

func (t *Thread) Load(chunk *code.Chunk) *Func

func (*Thread) Require

func (t *Thread) Require(lib Library, global bool) (Value, error)

func (*Thread) SetGlobal

func (t *Thread) SetGlobal(name string, global Value) *Thread

func (*Thread) String

func (t *Thread) String() string

func (*Thread) Type

func (v *Thread) Type(t *Thread) Type

func (*Thread) TypeOf

func (t *Thread) TypeOf(obj Value) Type

type Tuple

type Tuple []Value

func (*Tuple) Arg

func (args *Tuple) Arg(arg int) Value

func (*Tuple) Bool

func (args *Tuple) Bool(arg int) (Bool, error)

func (*Tuple) BoolOpt

func (args *Tuple) BoolOpt(arg int, opt Bool) Bool

func (*Tuple) Check

func (args *Tuple) Check(ls *Thread, check argsCheck) error

func (*Tuple) Float

func (args *Tuple) Float(arg int) (Float, error)

func (*Tuple) FloatOpt

func (args *Tuple) FloatOpt(arg int, opt Float) Float

func (*Tuple) GoFunc

func (args *Tuple) GoFunc(arg int) (*GoFunc, error)

func (*Tuple) Int

func (args *Tuple) Int(arg int) (Int, error)

func (*Tuple) IntOpt

func (args *Tuple) IntOpt(arg int, opt Int) Int

func (*Tuple) Number

func (args *Tuple) Number(arg int) (Number, error)

func (*Tuple) NumberOpt

func (args *Tuple) NumberOpt(arg int, opt Number) Number

func (*Tuple) String

func (args *Tuple) String(arg int) (String, error)

func (*Tuple) StringOpt

func (args *Tuple) StringOpt(arg int, opt String) String

func (*Tuple) Table

func (args *Tuple) Table(arg int) (*Table, error)

func (*Tuple) Thread

func (args *Tuple) Thread(arg int) (*Thread, error)

type Type

type Type interface {
	SetMeta(funcs *Table) *Table
	Method(name string) Callable
	Kind() code.Type
	Name() string
}

type Uint

type Uint uint64

func (Uint) String

func (v Uint) String() string

type Value

type Value interface {
	String() string
	Type(*Thread) Type
	// contains filtered or unexported methods
}

func Binary

func Binary(t *Thread, op Op, x, y Value) (Value, error)

func Require

func Require(ls *Thread, module string) (Value, error)

@require(module)

1: Check in package.loaded whether "module" is already loaded.

If so, return corresponding value. Therefore, once a module
is loaded, other calls requiring the same module simply return
the same value, without running any code again.

2: Otherwise, searches for a Lua file with the module name (this

search is guided by "package.path"). If it finds such a file,
it loads it with LoadFile. The result is a function that we
call a "Loader".

Loader: A function that, when called, loads the module.

3: Otherwise, searches for a Go library with that name (this search is

   guided by "package.gopath"). If it finds a Go library, it loads it
	  with the low-level function "package.loadlib", looking for a function
	  called "luaopen_modname". The loader in this case is the result of
	  "loadlib", which is the Go function "luaopen_modname" represented as
	  a Lua function.

4: No matter whether the module was found in a Lua file or a Go library,

require now has a loader for it. To finally load the module, require
calls the loader with two arguments: the module name and the name
of the file where it got the loader (most modules just ignore these
arguments).

If the loader returns any value, require returns this value and stores
it in the "package.loaded" table, to return the same value in future
calls for this same module. IF the loader returns no value, and the
table entry "package.loaded[module]" is still empty, require behaves
as if the module returned true. Without this correction, a subsequent
call to require would run the module again.

func Unary

func Unary(t *Thread, op Op, x Value) (Value, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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