Documentation
¶
Overview ¶
Package luacode provides a Lua parser that produces virtual machine code. See Parse for more details.
Provenance ¶
This package is a hand-written conversion of Lua 5.4.7 to Go, specifically borrowing from:
- lcode.c
- lparser.c
- lopcodes.h
- lobject.h (for Proto)
- ldump.c
- lundump.c
Ideally, this package should continue to resemble upstream so that improvements in Lua can be easily ported over.
Lua License ¶
Copyright (C) 1994-2024 Lua.org, PUC-Rio.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Index ¶
- Constants
- Variables
- func AllArithmeticOperators() iter.Seq[ArithmeticOperator]
- func FloatToInteger(n float64, mode FloatToIntegerMode) (_ int64, ok bool)
- func SignedArg(arg uint8) int16
- func ToSignedArg(i int64) (_ uint8, ok bool)
- type ArithmeticOperator
- type FloatToIntegerMode
- type Instruction
- func (i Instruction) ArgA() uint8
- func (i Instruction) ArgAx() uint32
- func (i Instruction) ArgB() uint8
- func (i Instruction) ArgBx() int32
- func (i Instruction) ArgC() uint8
- func (i Instruction) IsInTop() bool
- func (i Instruction) IsOutTop() bool
- func (i Instruction) J() int32
- func (i Instruction) K() bool
- func (i Instruction) OpCode() OpCode
- func (i Instruction) String() string
- func (i Instruction) WithArgA(a uint8) (_ Instruction, ok bool)
- func (i Instruction) WithArgC(c uint8) (_ Instruction, ok bool)
- func (i Instruction) WithK(k bool) (_ Instruction, ok bool)
- type LineInfo
- type LocalVariable
- type OpCode
- type OpMode
- type Prototype
- type Source
- type TagMethod
- type UpvalueDescriptor
- type Value
- func (v Value) Bool() (_ bool, isBool bool)
- func (v Value) Equal(v2 Value) bool
- func (v Value) Float64() (_ float64, isNumber bool)
- func (v Value) IdenticalTo(v2 Value) bool
- func (v Value) Int64(mode FloatToIntegerMode) (_ int64, ok bool)
- func (v Value) IsBoolean() bool
- func (v Value) IsInteger() bool
- func (v Value) IsNil() bool
- func (v Value) IsNumber() bool
- func (v Value) IsString() bool
- func (v Value) String() string
- func (v Value) Unquoted() (s string, isString bool)
- type VariableKind
Constants ¶
const MultiReturn = -1
MultiReturn is the sentinel that indicates that an arbitrary number of result values are accepted.
const Signature = "\x1bLua"
Signature is the magic header for a binary (pre-compiled) Lua chunk. Data with this prefix can be loaded in with *Prototype.UnmarshalBinary.
Variables ¶
var ( // ErrDivideByZero is returned by [Arithmetic] // when an integer division by zero occurs. ErrDivideByZero = errors.New("attempt to divide by zero") // ErrNotNumbers is returned by [Arithmetic] // when an operand is not a number. ErrNotNumber = errors.New("arithmetic on non-number") // ErrNotInteger is returned by [Arithmetic] // when performing integer-only arithmetic // and an operand is a number but not an integer. ErrNotInteger = errors.New("number has no integer representation") )
Arithmetic errors.
Functions ¶
func AllArithmeticOperators ¶
func AllArithmeticOperators() iter.Seq[ArithmeticOperator]
AllArithmeticOperators returns an iterator over all the valid arithmetic operators.
func FloatToInteger ¶
func FloatToInteger(n float64, mode FloatToIntegerMode) (_ int64, ok bool)
FloatToInteger attempts to convert a floating-point number to an integer, rounding according to the given mode.
func SignedArg ¶
SignedArg converts an ABCInstruction argument into a signed integer.
Equivalent to `sC2int` in upstream Lua.
func ToSignedArg ¶
ToSignedArg converts an integer into a signed ABCInstruction argument. ok is true if and only if the integer is within the range.
Equivalent to `int2sC` in upstream Lua with an extra `fitsC` check.
Types ¶
type ArithmeticOperator ¶
type ArithmeticOperator int
ArithmeticOperator is the subset of Lua operators that operate on numbers.
const ( Add ArithmeticOperator = 1 + iota Subtract Multiply Modulo Power Divide IntegerDivide BitwiseAnd BitwiseOr BitwiseXOR ShiftLeft ShiftRight UnaryMinus BitwiseNot )
Defined ArithmeticOperator values. Can be iterated with AllArithmeticOperators.
func (ArithmeticOperator) IsBinary ¶
func (op ArithmeticOperator) IsBinary() bool
IsBinary reports whether the operator uses two values.
func (ArithmeticOperator) IsIntegral ¶
func (op ArithmeticOperator) IsIntegral() bool
IsIntegral reports whether the operator only operates on integral values.
func (ArithmeticOperator) IsUnary ¶
func (op ArithmeticOperator) IsUnary() bool
IsUnary reports whether the operator only uses one value.
func (ArithmeticOperator) String ¶
func (i ArithmeticOperator) String() string
func (ArithmeticOperator) TagMethod ¶
func (op ArithmeticOperator) TagMethod() TagMethod
TagMethod returns the metamethod name for the given operator. TagMethod panics if op is not a valid arithmetic operator.
type FloatToIntegerMode ¶
type FloatToIntegerMode int
FloatToIntegerMode is an enumeration of rounding modes for FloatToInteger.
const ( // OnlyIntegral does not perform rounding // and only accepts integral values. OnlyIntegral FloatToIntegerMode = iota // Floor rounds to the greatest integer value less than or equal to the number. Floor // Ceil rounds to the least integer value greater than or equal to the number. Ceil )
Rounding modes.
type Instruction ¶
type Instruction uint32
Instruction is a single virtual machine instruction.
func ABCInstruction ¶
func ABCInstruction(op OpCode, a, b, c uint8, k bool) Instruction
ABCInstruction returns a new OpModeABC Instruction with the given arguments. ABCInstruction panics if the OpCode given does not return OpModeABC from OpCode.OpMode.
func ABxInstruction ¶
func ABxInstruction(op OpCode, a uint8, bx int32) Instruction
ABxInstruction returns a new OpModeABx Instruction with the given arguments. ABxInstruction panics if the OpCode given does not return OpModeABx from OpCode.OpMode.
func ExtraArgument ¶
func ExtraArgument(ax uint32) Instruction
ExtraArgument returns an OpExtraArg Instruction. ExtraArgument panics if given an argument that is too large.
func JInstruction ¶
func JInstruction(op OpCode, j int32) Instruction
JInstruction returns a new OpModeJ (jump) Instruction with the given offset relative to the end of the instruction. JInstruction panics if the OpCode given does not return OpModeJ from OpCode.OpMode.
func (Instruction) ArgA ¶
func (i Instruction) ArgA() uint8
ArgA returns the first (A) argument of an OpModeABC, OpModeABx, or OpModeAsBx instruction.
func (Instruction) ArgAx ¶
func (i Instruction) ArgAx() uint32
ArgAx returns the argument passed to ExtraArgument.
func (Instruction) ArgB ¶
func (i Instruction) ArgB() uint8
ArgB returns the second (B) argument of an OpModeABC instruction.
func (Instruction) ArgBx ¶
func (i Instruction) ArgBx() int32
ArgBx returns the second (Bx) argument of an OpModeABC, OpModeABx, or OpModeAsBx instruction.
func (Instruction) ArgC ¶
func (i Instruction) ArgC() uint8
ArgC returns the third (C) argument of an OpModeABC instruction.
func (Instruction) IsInTop ¶
func (i Instruction) IsInTop() bool
IsInTop reports whether the instruction uses the stack top from the previous instruction.
Equivalent to `isIT` in upstream Lua.
func (Instruction) IsOutTop ¶
func (i Instruction) IsOutTop() bool
IsOutTop reports whether the instruction sets the stack top for the next instruction.
Equivalent to `isOT` in upstream Lua.
func (Instruction) J ¶
func (i Instruction) J() int32
J returns the jump offset (relative to the end of the instruction) for a OpModeJ instruction.
func (Instruction) K ¶
func (i Instruction) K() bool
K returns the k flag of an OpModeABC instruction.
func (Instruction) OpCode ¶
func (i Instruction) OpCode() OpCode
OpCode returns the instruction's type.
func (Instruction) String ¶
func (i Instruction) String() string
String decodes the instruction and formats it in a manner similar to luac -l.
func (Instruction) WithArgA ¶
func (i Instruction) WithArgA(a uint8) (_ Instruction, ok bool)
WithArgA returns a copy of i with its first (A) argument changed to the given value, or i unchanged if i doesn't have an A instruction.
func (Instruction) WithArgC ¶
func (i Instruction) WithArgC(c uint8) (_ Instruction, ok bool)
WithArgC returns a copy of i with its third (C) argument changed to the given value, or i unchanged if OpCode.OpMode is not OpModeABC.
func (Instruction) WithK ¶
func (i Instruction) WithK(k bool) (_ Instruction, ok bool)
WithK returns a copy of i with its K argument changed to the given value. or i unchanged if OpCode.OpMode is not OpModeABC.
type LineInfo ¶
type LineInfo struct {
// contains filtered or unexported fields
}
LineInfo is a sequence of line numbers. The zero value is an empty sequence.
The underlying data structure is optimized for a sequence of integers where the difference between adjacent values is relatively small (|Δ| < 128).
func CollectLineInfo ¶
CollectLineInfo collects values from seq into a new LineInfo and returns it.
func (LineInfo) All ¶
All returns an iterator over the sequence's line numbers. (The index is the instruction address.)
type LocalVariable ¶
type LocalVariable struct { Name string // StartPC is the first instruction in the [Prototype.Code] slice // where the variable is active. StartPC int // EndPC is the first instruction in the [Prototype.Code] slice // where the variable is dead. EndPC int }
LocalVariable is a description of a local variable in Prototype used for debug information.
type OpCode ¶
type OpCode uint8
OpCode is an enumeration of Instruction types.
const ( // A B R[A] := R[B] OpMove OpCode = 0 // MOVE // A sBx R[A] := sBx OpLoadI OpCode = 1 // LOADI // A sBx R[A] := (lua_Number)sBx OpLoadF OpCode = 2 // LOADF // A Bx R[A] := K[Bx] OpLoadK OpCode = 3 // LOADK // A R[A] := K[extra arg] OpLoadKX OpCode = 4 // LOADKX // A R[A] := false OpLoadFalse OpCode = 5 // LOADFALSE // A R[A] := false; pc++ ( OpLFalseSkip OpCode = 6 // LFALSESKIP // A R[A] := true OpLoadTrue OpCode = 7 // LOADTRUE // OpLoadNil sets registers R[A] through R[A+B] (inclusive) to nil. // // A B R[A], R[A+1], ..., R[A+B] := nil OpLoadNil OpCode = 8 // LOADNIL // A B R[A] := UpValue[B] OpGetUpval OpCode = 9 // GETUPVAL // A B UpValue[B] := R[A] OpSetUpval OpCode = 10 // SETUPVAL // A B C R[A] := UpValue[B][K[C]:string] OpGetTabUp OpCode = 11 // GETTABUP // A B C R[A] := R[B][R[C]] OpGetTable OpCode = 12 // GETTABLE // A B C R[A] := R[B][C] OpGetI OpCode = 13 // GETI // A B C R[A] := R[B][K[C]:string] OpGetField OpCode = 14 // GETFIELD // A B C UpValue[A][K[B]:string] := RK(C) OpSetTabUp OpCode = 15 // SETTABUP // A B C R[A][R[B]] := RK(C) OpSetTable OpCode = 16 // SETTABLE // A B C R[A][B] := RK(C) OpSetI OpCode = 17 // SETI // A B C R[A][K[B]:string] := RK(C) OpSetField OpCode = 18 // SETFIELD // OpNewTable creates a new table and stores it in R[A]. // C is a hint that integer ("array") keys [1,C] are expected; // 2^B is a hint as to how many non-"array" keys are expected. // OpNewTable is always followed by an [OpExtraArg]. // If k is set, then the array size is // the extra argument multiplied by 256 plus C. // // A B C k R[A] := {} OpNewTable OpCode = 19 // NEWTABLE // A B C R[A+1] := R[B]; R[A] := R[B][RK(C):string] OpSelf OpCode = 20 // SELF // A B sC R[A] := R[B] + sC OpAddI OpCode = 21 // ADDI // A B C R[A] := R[B] + K[C]:number OpAddK OpCode = 22 // ADDK // A B C R[A] := R[B] - K[C]:number OpSubK OpCode = 23 // SUBK // A B C R[A] := R[B] * K[C]:number OpMulK OpCode = 24 // MULK // A B C R[A] := R[B] % K[C]:number OpModK OpCode = 25 // MODK // A B C R[A] := R[B] ^ K[C]:number OpPowK OpCode = 26 // POWK // A B C R[A] := R[B] / K[C]:number OpDivK OpCode = 27 // DIVK // A B C R[A] := R[B] // K[C]:number OpIDivK OpCode = 28 // IDIVK // A B C R[A] := R[B] & K[C]:integer OpBAndK OpCode = 29 // BANDK // A B C R[A] := R[B] | K[C]:integer OpBOrK OpCode = 30 // BORK // A B C R[A] := R[B] ~ K[C]:integer OpBXORK OpCode = 31 // BXORK // A B sC R[A] := R[B] >> sC OpSHRI OpCode = 32 // SHRI // A B sC R[A] := sC << R[B] OpSHLI OpCode = 33 // SHLI // A B C R[A] := R[B] + R[C] OpAdd OpCode = 34 // ADD // A B C R[A] := R[B] - R[C] OpSub OpCode = 35 // SUB // A B C R[A] := R[B] * R[C] OpMul OpCode = 36 // MUL // A B C R[A] := R[B] % R[C] OpMod OpCode = 37 // MOD // A B C R[A] := R[B] ^ R[C] OpPow OpCode = 38 // POW // A B C R[A] := R[B] / R[C] OpDiv OpCode = 39 // DIV // A B C R[A] := R[B] // R[C] OpIDiv OpCode = 40 // IDIV // A B C R[A] := R[B] & R[C] OpBAnd OpCode = 41 // BAND // A B C R[A] := R[B] | R[C] OpBOr OpCode = 42 // BOR // A B C R[A] := R[B] ~ R[C] OpBXOR OpCode = 43 // BXOR // A B C R[A] := R[B] << R[C] OpSHL OpCode = 44 // SHL // A B C R[A] := R[B] >> R[C] OpSHR OpCode = 45 // SHR // A B C call C metamethod over R[A] and R[B] ( OpMMBin OpCode = 46 // MMBIN // A sB C k call C metamethod over R[A] and sB OpMMBinI OpCode = 47 // MMBINI // A B C k call C metamethod over R[A] and K[B] OpMMBinK OpCode = 48 // MMBINK // A B R[A] := -R[B] OpUNM OpCode = 49 // UNM // A B R[A] := ~R[B] OpBNot OpCode = 50 // BNOT // A B R[A] := not R[B] OpNot OpCode = 51 // NOT // A B R[A] := #R[B] (length operator) OpLen OpCode = 52 // LEN // A B R[A] := R[A].. ... ..R[A + B - 1] OpConcat OpCode = 53 // CONCAT // A close all upvalues >= R[A] OpClose OpCode = 54 // CLOSE // A mark variable A "to be closed" OpTBC OpCode = 55 // TBC // sJ pc += sJ OpJMP OpCode = 56 // JMP // A B k if ((R[A] == R[B]) ~= k) then pc++ OpEQ OpCode = 57 // EQ // A B k if ((R[A] < R[B]) ~= k) then pc++ OpLT OpCode = 58 // LT // A B k if ((R[A] <= R[B]) ~= k) then pc++ OpLE OpCode = 59 // LE // A B k if ((R[A] == K[B]) ~= k) then pc++ OpEQK OpCode = 60 // EQK // A sB k if ((R[A] == sB) ~= k) then pc++ OpEQI OpCode = 61 // EQI // A sB k if ((R[A] < sB) ~= k) then pc++ OpLTI OpCode = 62 // LTI // A sB k if ((R[A] <= sB) ~= k) then pc++ OpLEI OpCode = 63 // LEI // A sB k if ((R[A] > sB) ~= k) then pc++ OpGTI OpCode = 64 // GTI // A sB k if ((R[A] >= sB) ~= k) then pc++ OpGEI OpCode = 65 // GEI // A k if (not R[A] == k) then pc++ OpTest OpCode = 66 // TEST // A B k if (not R[B] == k) then pc++ else R[A] := R[B] ( OpTestSet OpCode = 67 // TESTSET // OpCall calls a function. // // - R[A] is the function to call. // When the function returns, R[A] is also where the first result will be stored. // - B is the number of arguments to pass to the function plus one. // If B is zero, this indicates to use all values after R[A] on the stack // as arguments. // - C is the number of expected results plus one. // If C is zero, then all results from the function will be pushed onto the stack, // starting at R[A]. OpCall OpCode = 68 // CALL // OpTailCall calls a function as the return of the function, // replacing the stack entry of the calling function. // // - R[A] is the function to call. // - B is the number of arguments to pass to the function plus one. // If B is zero, this indicates to use all values after R[A] on the stack // as arguments. // - C > 0 means the calling function is vararg, // so that any effects of [OpVarargPrep] must be corrected before returning; // in this case, (C - 1) is its number of fixed parameters. // - k should be true if there are upvalues that need to be closed. // (The language does not permit tail calls in blocks with to-be-closed variables in scope.) OpTailCall OpCode = 69 // TAILCALL // OpReturn instructs control flow to return to the function's caller. // // - R[A] is the first result to return. // - B is the number of results to return. // If B is zero, then return up to 'top'. // - C > 0 means the function is vararg, // so that any effects of [OpVarargPrep] must be corrected before returning; // in this case, (C - 1) is its number of fixed parameters. // - k should be true if there are upvalues and/or to-be-closed variables that need to be closed. OpReturn OpCode = 70 // RETURN // OpReturn0 instructs control flow to return to the function's caller // with zero results. // This instruction cannot be used in a function // that is variadic, // has variables referenced by its functions, // or has to-be-closed variables. OpReturn0 OpCode = 71 // RETURN0 // OpReturn1 instructs control flow to return to the function's caller // with a single result stored in R[A]. // This instruction cannot be used in a function // that is variadic, // has variables referenced by its functions, // or has to-be-closed variables. OpReturn1 OpCode = 72 // RETURN1 // A Bx update counters; if loop continues then pc-=Bx; OpForLoop OpCode = 73 // FORLOOP // A Bx <check values and prepare counters>; if not to run then pc+=Bx+1; OpForPrep OpCode = 74 // FORPREP // A Bx create upvalue for R[A + 3]; pc+=Bx OpTForPrep OpCode = 75 // TFORPREP // A C R[A+4], ... ,R[A+3+C] := R[A](R[A+1], R[A+2]); OpTForCall OpCode = 76 // TFORCALL // A Bx if R[A+2] ~= nil then { R[A]=R[A+2]; pc -= Bx } OpTForLoop OpCode = 77 // TFORLOOP // OpSetList sets the elements [C+1,C+B] // of the table in R[A] // to the registers [A+1,A+B]. // "Raw" sets are used; no metamethods are called. // If k is set, then C is augmented with an [OpExtraArg] instruction: // the starting index to set in the table is // the extra argument multiplied by 256, plus C plus 1. // // A B C k R[A][C+i] := R[A+i], 1 <= i <= B OpSetList OpCode = 78 // SETLIST // A Bx R[A] := closure(KPROTO[Bx]) OpClosure OpCode = 79 // CLOSURE // A C R[A], R[A+1], ..., R[A+C-2] = vararg OpVararg OpCode = 80 // VARARG // OpVarargPrep instructs the first A arguments to remain in registers // and the rest to be moved into storage for access with [OpVararg]. OpVarargPrep OpCode = 81 // VARARGPREP // Ax extra (larger) argument for previous opcode OpExtraArg OpCode = 82 // EXTRAARG )
Defined OpCode values.
func (OpCode) ArithmeticOperator ¶
func (op OpCode) ArithmeticOperator() (_ ArithmeticOperator, ok bool)
ArithmeticOperator returns the ArithmeticOperator that the instruction represents.
func (OpCode) IsMetamethod ¶
IsMetamethod reports whether the instruction calls a metamethod.
Equivalent to `testMMMode` in upstream Lua.
func (OpCode) IsTest ¶
IsTest reports whether the instruction is a test. In a valid program, the next instruction will be a jump.
Equivalent to `testTMode` in upstream Lua.
func (OpCode) OpMode ¶
OpMode returns the format of an Instruction that uses the opcode.
Equivalent to `getOpMode` in upstream Lua.
func (OpCode) SetsA ¶
SetsA reports whether an Instruction that uses the opcode would change the value of the register given in Instruction.ArgA.
Equivalent to `testAMode` in upstream Lua.
type Prototype ¶
type Prototype struct { // NumParams is the number of fixed (named) parameters. NumParams uint8 IsVararg bool // MaxStackSize is the number of registers needed by this function. MaxStackSize uint8 Constants []Value Code []Instruction Functions []*Prototype Upvalues []UpvalueDescriptor Source Source // LocalVariables is a list of the function's local variables in declaration order. // It is guaranteed that LocalVariables[i].StartPC <= LocalVariables[i+1].StartPC. LocalVariables []LocalVariable LineInfo LineInfo LineDefined int LastLineDefined int }
Prototype represents a parsed function.
func Parse ¶
func Parse(name Source, r io.ByteScanner) (*Prototype, error)
Parse converts a Lua source file into virtual machine bytecode.
func (*Prototype) IsMainChunk ¶
IsMainChunk reports whether the prototype represents a parsed source file (as opposed to a function inside a file).
func (*Prototype) LocalName ¶
LocalName returns the name of the local variable the given register represents during the execution of the given instruction, or the empty string if the register does not represent a local variable (or the debug information has been stripped).
func (*Prototype) MarshalBinary ¶
MarshalBinary marshals the function as a precompiled chunk in the same format as luac 5.4.
func (*Prototype) StripDebug ¶
StripDebug returns a copy of a Prototype with the debug information removed.
type Source ¶
type Source string
Source is a description of a chunk that created a Prototype. The zero value describes an empty literal string.
func AbstractSource ¶
AbstractSource returns a Source from a user-dependent description. The description can be retrieved later using Source.Abstract.
The underlying string in an abstract source starts with "=".
func FilenameSource ¶
FilenameSource returns a Source for a filesystem path. The path can be retrieved later using Source.Filename.
The underlying string in a filename source starts with "@".
func LiteralSource ¶
LiteralSource returns a Source for the given literal string. Because the type for a Source is determined by the first byte, if s starts with one of those symbols (which cannot occur in a syntactically valid Lua source file), then LiteralSource returns an AbstractSource with a condensed version of the string.
func (Source) Abstract ¶
Abstract returns the user-dependent description of the source provided to AbstractSource.
func (Source) Filename ¶
Filename returns the file name of the chunk provided to FilenameSource.
func (Source) Literal ¶
Literal returns the string provided to LiteralSource.
type TagMethod ¶
type TagMethod uint8
TagMethod is an enumeration of built-in metamethods.
const ( TagMethodIndex TagMethod = 0 // __index TagMethodNewIndex TagMethod = 1 // __newindex TagMethodGC TagMethod = 2 // __gc TagMethodMode TagMethod = 3 // __mode TagMethodLen TagMethod = 4 // __len // TagMethodEQ is the equality (==) operation. // TagMethodEQ is the last tag method with fast access. TagMethodEQ TagMethod = 5 // __eq TagMethodAdd TagMethod = 6 // __add TagMethodSub TagMethod = 7 // __sub TagMethodMul TagMethod = 8 // __mul TagMethodMod TagMethod = 9 // __mod TagMethodPow TagMethod = 10 // __pow TagMethodDiv TagMethod = 11 // __div TagMethodIDiv TagMethod = 12 // __idiv TagMethodBAnd TagMethod = 13 // __band TagMethodBOr TagMethod = 14 // __bor TagMethodBXOR TagMethod = 15 // __bxor TagMethodSHL TagMethod = 16 // __shl TagMethodSHR TagMethod = 17 // __shr TagMethodUNM TagMethod = 18 // __unm TagMethodBNot TagMethod = 19 // __bnot TagMethodLT TagMethod = 20 // __lt TagMethodLE TagMethod = 21 // __le TagMethodConcat TagMethod = 22 // __concat TagMethodCall TagMethod = 23 // __call TagMethodClose TagMethod = 24 // __close )
Metamethods.
func (TagMethod) ArithmeticOperator ¶
func (tm TagMethod) ArithmeticOperator() (_ ArithmeticOperator, ok bool)
ArithmeticOperator returns the ArithmeticOperator that the metamethod represents (if applicable).
type UpvalueDescriptor ¶
type UpvalueDescriptor struct { Name string // InStack is true if the upvalue refers to a local variable // in the containing function. // Otherwise, the upvalue refers to an upvalue in the containing function. InStack bool // Index is the index of the local variable or upvalue // to initialize the upvalue to. // Its interpretation depends on the value of InStack. Index uint8 Kind VariableKind }
UpvalueDescriptor describes an upvalue in a Prototype.
type Value ¶
type Value struct {
// contains filtered or unexported fields
}
Value is a subset of Lua values that can be used as constants: nil, booleans, floats, integers, and strings. The zero value is nil.
func Arithmetic ¶
func Arithmetic(op ArithmeticOperator, p1, p2 Value) (Value, error)
Arithmetic performs an arithmetic or bitwise operation. If the operator is unary, p1 is used and p2 is ignored. Arithmetic may return an error that wraps one of ErrDivideByZero, ErrNotNumber, or ErrNotInteger.
Equivalent to `luaO_rawarith` in upstream Lua.
func FloatValue ¶
FloatValue converts a floating-point number to a Value.
func (Value) Bool ¶
Bool reports whether the value tests true in Lua and whether the value is a boolean.
func (Value) Equal ¶
Equal returns whether two values are equivalent according to Lua equality.
func (Value) Float64 ¶
Float64 returns the value as a floating-point number and reports whether the value is a number. No coercion occurs.
func (Value) IdenticalTo ¶
IdenticalTo reports whether two values represent the same value. This is mostly the same as Value.Equal, but will report true for two NaNs, for example.
func (Value) Int64 ¶
func (v Value) Int64(mode FloatToIntegerMode) (_ int64, ok bool)
Int64 returns the value as an integer and reports whether the value is a number. If the value is a floating point number and cannot be converted to an integer according to the mode, then ok will be false. No other coercion occurs.
type VariableKind ¶
type VariableKind uint8
const ( RegularVariable VariableKind = 0 LocalConst VariableKind = 1 ToClose VariableKind = 2 CompileTimeConstant VariableKind = 3 )