metamethod

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package api defines the tag method (metamethod) system.

Lua supports 25 metamethods that customize object behavior. The fasttm optimization caches metamethod absence in the table's flags byte.

Reference: .analysis/07-runtime-infrastructure.md §5

Tag method (metamethod) lookup functions.

Provides GetTM and GetTMByObj for metamethod resolution. CallTM and TryBinTM require the VM call machinery and will be implemented in Phase 7 (vm).

Reference: lua-master/ltm.c

Index

Constants

View Source
const (
	// MaskFlags is the bitmask for fast-access metamethods (bits 0-5).
	MaskFlags byte = 0x3F

	// BitDummy is bit 6 in table flags, indicating the hash part uses dummynode.
	BitDummy byte = 0x40
)

--------------------------------------------------------------------------- fasttm cache constants

The first 6 metamethods (TM_INDEX through TM_EQ) are cached in the table's flags byte. A set bit means "metamethod is ABSENT". ---------------------------------------------------------------------------

Variables

View Source
var TMNames = [TM_N]string{
	"__index", "__newindex", "__gc", "__mode", "__len", "__eq",
	"__add", "__sub", "__mul", "__mod", "__pow", "__div", "__idiv",
	"__band", "__bor", "__bxor", "__shl", "__shr",
	"__unm", "__bnot", "__lt", "__le",
	"__concat", "__call", "__close",
}

TMNames maps TMS values to their Lua string names.

Functions

func GetTM

func GetTM(mt *table.Table, event TMS, tmName *object.LuaString) object.TValue

GetTM looks up a metamethod in the given metatable. Returns the metamethod TValue, or Nil if not found. For events <= TM_EQ, uses the fasttm cache on the table's Flags.

func GetTMByObj

func GetTMByObj(g *state.GlobalState, obj object.TValue, event TMS) object.TValue

GetTMByObj looks up a metamethod for the given value. Returns the metamethod TValue, or Nil if not found.

func ObjTypeName

func ObjTypeName(g *state.GlobalState, obj object.TValue) string

ObjTypeName returns the type name for a Lua value. Checks __name metamethod for tables and userdata.

Types

type TMS

type TMS int

--------------------------------------------------------------------------- TMS is the tag method selector (metamethod index). ---------------------------------------------------------------------------

const (
	TM_INDEX    TMS = iota // __index        (0) — fast access
	TM_NEWINDEX            // __newindex     (1) — fast access
	TM_GC                  // __gc           (2) — fast access
	TM_MODE                // __mode         (3) — fast access
	TM_LEN                 // __len          (4) — fast access
	TM_EQ                  // __eq           (5) — fast access (last)
	TM_ADD                 // __add          (6)
	TM_SUB                 // __sub
	TM_MUL                 // __mul
	TM_MOD                 // __mod
	TM_POW                 // __pow
	TM_DIV                 // __div
	TM_IDIV                // __idiv
	TM_BAND                // __band
	TM_BOR                 // __bor
	TM_BXOR                // __bxor
	TM_SHL                 // __shl
	TM_SHR                 // __shr
	TM_UNM                 // __unm          (18)
	TM_BNOT                // __bnot         (19)
	TM_LT                  // __lt           (20)
	TM_LE                  // __le           (21)
	TM_CONCAT              // __concat       (22)
	TM_CALL                // __call         (23)
	TM_CLOSE               // __close        (24)
	TM_N                   // total count = 25
)

Jump to

Keyboard shortcuts

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