Documentation
¶
Overview ¶
pkg/objects/array.go
pkg/objects/builtin.go
pkg/objects/class.go
pkg/objects/float.go
pkg/objects/function.go
pkg/objects/int.go
pkg/objects/map.go
pkg/objects/methods.go
pkg/objects/module.go Module object type for runtime module representation.
pkg/objects/object.go
pkg/objects/string.go
pkg/objects/stringbuilder.go
Index ¶
- Constants
- Variables
- func ClearInternCache()
- func IsCachedInt(val int64) bool
- func IsTruthy(obj Object) bool
- func PutBufferedFloat(obj *Float)
- func PutBufferedInt(obj *Int)
- func PutBufferedString(obj *String)
- func ReleaseArray(obj *Array)
- func ReleaseArraySlice(objs []*Array)
- func ReleaseFloat(obj *Float)
- func ReleaseFloatSlice(objs []*Float)
- func ReleaseInt(obj *Int)
- func ReleaseIntSlice(objs []*Int)
- func ReleaseMap(obj *Map)
- func ReleaseMapSlice(objs []*Map)
- func ReleaseString(obj *String)
- func ReleaseStringSlice(objs []*String)
- func ResetArrayPoolStats()
- func ResetFloatPoolStats()
- func ResetIntPoolStats()
- func ResetMapPoolStats()
- func ResetStringPoolStats()
- func SetLoadPluginImpl(fn func(path string) (Object, error)) func(path string) (Object, error)
- func SetRunCodeImpl(fn func(code string, args *Map) (Object, error)) func(code string, args *Map) (Object, error)
- func WarmArrayPool(count int)
- func WarmFloatPool(count int)
- func WarmIntPool(count int)
- func WarmMapPool(count int)
- func WarmStringPool(count int)
- type Array
- type ArrayPoolStats
- type Bool
- type Builtin
- type BuiltinFunction
- type Class
- type CompiledFunction
- type Environment
- type Error
- type Float
- type FloatPoolStats
- type Function
- type HashKey
- type Identifier
- type Instance
- type Int
- type IntPoolStats
- type Map
- type MapPair
- type MapPoolStats
- type Module
- type Null
- type Object
- type ObjectType
- type Return
- type String
- type StringBuilder
- func (sb *StringBuilder) Cap() int
- func (sb *StringBuilder) Clear()
- func (sb *StringBuilder) Grow(n int)
- func (sb *StringBuilder) HashKey() HashKey
- func (sb *StringBuilder) Inspect() string
- func (sb *StringBuilder) Len() int
- func (sb *StringBuilder) Reset()
- func (sb *StringBuilder) String() string
- func (sb *StringBuilder) ToBool() *Bool
- func (sb *StringBuilder) Type() ObjectType
- func (sb *StringBuilder) TypeTag() TypeTag
- func (sb *StringBuilder) Write(s string) int
- func (sb *StringBuilder) WriteLine(s string) int
- type StringPoolStats
- type TypeTag
Constants ¶
const IntCacheMax = 100000
IntCacheMax is the maximum cached integer value Extended to cover common loop indices and calculation results
const IntCacheMin = -1000
IntCacheMin is the minimum cached integer value
Variables ¶
var ( FLOAT_ZERO = &Float{Value: 0.0} FLOAT_ONE = &Float{Value: 1.0} FLOAT_NEG_ONE = &Float{Value: -1.0} FLOAT_TWO = &Float{Value: 2.0} FLOAT_HALF = &Float{Value: 0.5} FLOAT_TEN = &Float{Value: 10.0} FLOAT_HUNDRED = &Float{Value: 100.0} FLOAT_THOUSAND = &Float{Value: 1000.0} FLOAT_PI = &Float{Value: 3.14159265358979323846} FLOAT_E = &Float{Value: 2.71828182845904523536} )
Pre-cached common float values Unlike integers, we can only cache a small set due to the infinite nature of floats
var ( TRUE = &Bool{Value: true} FALSE = &Bool{Value: false} )
TRUE and FALSE are singleton boolean values
var ( STRING_EMPTY = &String{Value: ""} STRING_TRUE = &String{Value: "true"} STRING_FALSE = &String{Value: "false"} STRING_NULL = &String{Value: "null"} STRING_INT = &String{Value: "INT"} STRING_FLOAT = &String{Value: "FLOAT"} STRING_BOOL = &String{Value: "BOOL"} STRING_STRING = &String{Value: "STRING"} STRING_ARRAY = &String{Value: "ARRAY"} STRING_MAP = &String{Value: "MAP"} STRING_FUNC = &String{Value: "FUNCTION"} STRING_BUILTIN = &String{Value: "BUILTIN"} STRING_ERROR = &String{Value: "ERROR"} STRING_NIL = &String{Value: "nil"} STRING_ZERO = &String{Value: "0"} STRING_ONE = &String{Value: "1"} STRING_SPACE = &String{Value: " "} STRING_NEWLINE = &String{Value: "\n"} )
Pre-cached common string values
var Builtins = map[string]*Builtin{}/* 108 elements not displayed */
Builtins contains all built-in functions
var EMPTY_ARRAY = &Array{Elements: emptyElements}
Pre-cached empty array
var EMPTY_MAP = &Map{Pairs: emptyPairs}
Pre-cached empty map
var NULL = &Null{}
NULL is the singleton null value
var TypeMethods = map[ObjectType]map[string]*Builtin{
IntType: intMethods,
FloatType: floatMethods,
StringType: stringMethods,
ArrayType: arrayMethods,
MapType: mapMethods,
BoolType: boolMethods,
NullType: nullMethods,
StringBuilderType: stringBuilderMethods,
}
TypeMethods maps ObjectType -> methodName -> *Builtin
Functions ¶
func ClearInternCache ¶ added in v0.4.23
func ClearInternCache()
ClearInternCache clears the string intern cache Use with caution - this will cause all interned strings to be re-allocated
func IsCachedInt ¶ added in v0.4.23
IsCachedInt returns true if the given value is within the cached range
func PutBufferedFloat ¶ added in v0.4.23
func PutBufferedFloat(obj *Float)
PutBufferedFloat returns a temporary Float to the global buffer
func PutBufferedInt ¶ added in v0.4.23
func PutBufferedInt(obj *Int)
PutBufferedInt returns a temporary Int to the global buffer
func PutBufferedString ¶ added in v0.4.23
func PutBufferedString(obj *String)
PutBufferedString returns a temporary String to the global buffer
func ReleaseArray ¶ added in v0.4.23
func ReleaseArray(obj *Array)
ReleaseArray returns an Array object to the pool
func ReleaseArraySlice ¶ added in v0.4.23
func ReleaseArraySlice(objs []*Array)
ReleaseArraySlice returns multiple Array objects to the pool
func ReleaseFloat ¶ added in v0.4.23
func ReleaseFloat(obj *Float)
ReleaseFloat returns a Float object to the pool if it's not a cached value This should be called when the object is no longer needed
func ReleaseFloatSlice ¶ added in v0.4.23
func ReleaseFloatSlice(objs []*Float)
ReleaseFloatSlice returns multiple Float objects to the pool This is more efficient than calling ReleaseFloat multiple times
func ReleaseInt ¶ added in v0.4.23
func ReleaseInt(obj *Int)
ReleaseInt returns an Int object to the pool if it's outside the cache range This should be called when the object is no longer needed
func ReleaseIntSlice ¶ added in v0.4.23
func ReleaseIntSlice(objs []*Int)
ReleaseIntSlice returns multiple Int objects to the pool This is more efficient than calling ReleaseInt multiple times
func ReleaseMap ¶ added in v0.4.23
func ReleaseMap(obj *Map)
ReleaseMap returns a Map object to the pool
func ReleaseMapSlice ¶ added in v0.4.23
func ReleaseMapSlice(objs []*Map)
ReleaseMapSlice returns multiple Map objects to the pool
func ReleaseString ¶ added in v0.4.23
func ReleaseString(obj *String)
ReleaseString returns a String object to the pool if it's not a cached value This should be called when the object is no longer needed
func ReleaseStringSlice ¶ added in v0.4.23
func ReleaseStringSlice(objs []*String)
ReleaseStringSlice returns multiple String objects to the pool This is more efficient than calling ReleaseString multiple times
func ResetArrayPoolStats ¶ added in v0.4.23
func ResetArrayPoolStats()
ResetArrayPoolStats resets the pool statistics counters
func ResetFloatPoolStats ¶ added in v0.4.23
func ResetFloatPoolStats()
ResetFloatPoolStats resets the pool statistics counters
func ResetIntPoolStats ¶ added in v0.4.23
func ResetIntPoolStats()
ResetIntPoolStats resets the pool statistics counters
func ResetMapPoolStats ¶ added in v0.4.23
func ResetMapPoolStats()
ResetMapPoolStats resets the pool statistics counters
func ResetStringPoolStats ¶ added in v0.4.23
func ResetStringPoolStats()
ResetStringPoolStats resets the pool statistics counters
func SetLoadPluginImpl ¶ added in v0.4.19
SetLoadPluginImpl registers the loadPlugin implementation and returns the previous value
func SetRunCodeImpl ¶ added in v0.4.19
func SetRunCodeImpl(fn func(code string, args *Map) (Object, error)) func(code string, args *Map) (Object, error)
SetRunCodeImpl registers the runCode implementation and returns the previous value
func WarmArrayPool ¶ added in v0.4.23
func WarmArrayPool(count int)
WarmArrayPool pre-allocates Array objects into the pool
func WarmFloatPool ¶ added in v0.4.23
func WarmFloatPool(count int)
WarmFloatPool pre-allocates a number of Float objects into the pool This can improve performance by reducing allocations during hot paths
func WarmIntPool ¶ added in v0.4.23
func WarmIntPool(count int)
WarmIntPool pre-allocates a number of Int objects into the pool This can improve performance by reducing allocations during hot paths
func WarmMapPool ¶ added in v0.4.23
func WarmMapPool(count int)
WarmMapPool pre-allocates Map objects into the pool
func WarmStringPool ¶ added in v0.4.23
func WarmStringPool(count int)
WarmStringPool pre-allocates a number of String objects into the pool This can improve performance by reducing allocations during hot paths
Types ¶
type Array ¶
type Array struct {
Elements []Object
LastPopped Object // Used by pop() to return the popped value
}
Array represents an array value
func NewArrayWithCapacity ¶ added in v0.4.23
NewArrayWithCapacity creates a new Array with pre-allocated capacity
type ArrayPoolStats ¶ added in v0.4.23
ArrayPoolStats tracks statistics about array pool usage
func GetArrayPoolStats ¶ added in v0.4.23
func GetArrayPoolStats() ArrayPoolStats
GetArrayPoolStats returns current statistics about array pool usage
type Bool ¶
type Bool struct {
Value bool
}
Bool represents a boolean value
func (*Bool) Type ¶
func (b *Bool) Type() ObjectType
type Builtin ¶
type Builtin struct {
Fn BuiltinFunction
}
Builtin represents a built-in function
func GetMethod ¶
func GetMethod(objType ObjectType, name string) (*Builtin, bool)
GetMethod returns the builtin method for the given object type and method name
func (*Builtin) Type ¶
func (b *Builtin) Type() ObjectType
type BuiltinFunction ¶
BuiltinFunction is the type for built-in functions
type Class ¶
type Class struct {
Name string
SuperClass *Class
Methods map[string]Object // methods are CompiledFunction objects
InitMethod Object // constructor method
Fields map[string]Object // default field values
}
Class represents a class definition
func (*Class) Type ¶
func (c *Class) Type() ObjectType
type CompiledFunction ¶
CompiledFunction represents a compiled function for the VM
func (*CompiledFunction) HashKey ¶
func (cf *CompiledFunction) HashKey() HashKey
func (*CompiledFunction) Inspect ¶
func (cf *CompiledFunction) Inspect() string
func (*CompiledFunction) ToBool ¶
func (cf *CompiledFunction) ToBool() *Bool
func (*CompiledFunction) Type ¶
func (cf *CompiledFunction) Type() ObjectType
func (*CompiledFunction) TypeTag ¶
func (cf *CompiledFunction) TypeTag() TypeTag
type Environment ¶
type Environment struct {
Store map[string]Object
Outer *Environment
}
Environment represents a variable scope
func NewEnclosedEnvironment ¶
func NewEnclosedEnvironment(outer *Environment) *Environment
NewEnclosedEnvironment creates a new environment with an outer scope
type Error ¶
type Error struct {
Message string
}
Error represents a runtime error
func (*Error) Type ¶
func (e *Error) Type() ObjectType
type Float ¶
type Float struct {
Value float64
}
Float represents a floating-point value
func GetBufferedFloat ¶ added in v0.4.23
GetBufferedFloat gets a temporary Float using the global buffer
func NewFloat ¶ added in v0.4.23
NewFloat creates a new Float object, using cached values for common floats
func NewFloatSlice ¶ added in v0.4.23
NewFloatSlice creates multiple Float objects efficiently This is optimized for batch operations
type FloatPoolStats ¶ added in v0.4.23
type FloatPoolStats struct {
// CacheHits is the number of times a cached float was returned
CacheHits int64
// PoolHits is the number of times a pooled float was reused
PoolHits int64
// Created is the number of new floats allocated by the pool
Created int64
// Released is the number of floats returned to the pool
Released int64
}
FloatPoolStats tracks statistics about float pool usage
func GetFloatPoolStats ¶ added in v0.4.23
func GetFloatPoolStats() FloatPoolStats
GetFloatPoolStats returns current statistics about float pool usage
type Function ¶
type Function struct {
Parameters []*Identifier
Body interface{} // Will be *ast.BlockStatement, using interface{} to avoid import cycle
Env *Environment
Name string // Optional: for named functions
}
Function represents a user-defined function
func (*Function) Type ¶
func (f *Function) Type() ObjectType
type Identifier ¶
type Identifier struct {
Value string
}
Identifier represents an identifier (used in function parameters)
func (*Identifier) String ¶
func (i *Identifier) String() string
type Instance ¶
Instance represents an instance of a class
func (*Instance) Type ¶
func (i *Instance) Type() ObjectType
type Int ¶
type Int struct {
Value int64
}
Int represents an integer value
func GetBufferedInt ¶ added in v0.4.23
GetBufferedInt gets a temporary Int using the global buffer
func NewIntSlice ¶ added in v0.4.23
NewIntSlice creates multiple Int objects efficiently This is optimized for batch operations
type IntPoolStats ¶ added in v0.4.23
type IntPoolStats struct {
// CacheHits is the number of times a cached integer was returned
CacheHits int64
// PoolHits is the number of times a pooled integer was reused
PoolHits int64
// PoolMisses is the number of times a new integer had to be allocated
PoolMisses int64
// Created is the number of new integers allocated by the pool
Created int64
// Released is the number of integers returned to the pool
Released int64
}
IntPoolStats tracks statistics about integer pool usage
func GetIntPoolStats ¶ added in v0.4.23
func GetIntPoolStats() IntPoolStats
GetIntPoolStats returns current statistics about integer pool usage
type Map ¶
Map represents a map value
func NewMapWithCapacity ¶ added in v0.4.23
NewMapWithCapacity creates a new Map with pre-allocated capacity
func (*Map) GetSortedKeys ¶ added in v0.4.23
GetSortedKeys returns the keys in sorted order, caching the result
func (*Map) InvalidateKeysCache ¶ added in v0.4.23
func (m *Map) InvalidateKeysCache()
InvalidateKeysCache marks the sorted keys cache as invalid Call this when the map is modified
type MapPoolStats ¶ added in v0.4.23
MapPoolStats tracks statistics about map pool usage
func GetMapPoolStats ¶ added in v0.4.23
func GetMapPoolStats() MapPoolStats
GetMapPoolStats returns current statistics about map pool usage
type Module ¶
type Module struct {
// Name is the module's identifier (typically the file path)
Name string
// Exports maps exported symbol names to their values
Exports map[string]Object
// Globals holds the module's global variables state.
// This is needed so exported functions can access module-level variables.
Globals []Object
}
Module represents a loaded module with its exported symbols. Modules are created when a source file is imported and compiled, and they hold all exported values accessible to importers.
func (*Module) HashKey ¶
HashKey returns a hash key for the module. Modules are not hashable in a meaningful way, so we return a constant.
type Null ¶
type Null struct{}
Null represents the null value
func (*Null) Type ¶
func (n *Null) Type() ObjectType
type Object ¶
type Object interface {
Type() ObjectType
TypeTag() TypeTag // Fast type check without string comparison
Inspect() string
ToBool() *Bool
HashKey() HashKey
}
Object is the base interface for all values in Xxlang
type ObjectType ¶
type ObjectType string
ObjectType represents the type of an object
const ( NullType ObjectType = "NULL" IntType ObjectType = "INT" FloatType ObjectType = "FLOAT" StringType ObjectType = "STRING" BoolType ObjectType = "BOOL" ArrayType ObjectType = "ARRAY" MapType ObjectType = "MAP" FunctionType ObjectType = "FUNCTION" BuiltinType ObjectType = "BUILTIN" BytesType ObjectType = "BYTES" ClassType ObjectType = "CLASS" InstanceType ObjectType = "INSTANCE" ErrorType ObjectType = "ERROR" ReturnType ObjectType = "RETURN" ClosureType ObjectType = "CLOSURE" ModuleType ObjectType = "MODULE" StringBuilderType ObjectType = "STRING_BUILDER" )
Object types
const CompiledFunctionType ObjectType = "COMPILED_FUNCTION"
CompiledFunctionType is the type for compiled functions
type Return ¶
type Return struct {
Value Object
}
Return represents a return value (used internally)
func (*Return) Type ¶
func (r *Return) Type() ObjectType
type String ¶
type String struct {
Value string
}
String represents a string value
func GetBufferedString ¶ added in v0.4.23
GetBufferedString gets a temporary String using the global buffer
func InternBatch ¶
InternBatch pre-interns a batch of strings
func InternString ¶
InternString returns a cached *String for the given value This provides permanent caching for strings that are used often Use this for strings that will be reused many times (e.g., identifiers, keywords)
func NewString ¶ added in v0.4.23
NewString creates a new String object, using cached values for common strings
func NewStringSlice ¶ added in v0.4.23
NewStringSlice creates multiple String objects efficiently This is optimized for batch operations
type StringBuilder ¶ added in v0.4.19
type StringBuilder struct {
// contains filtered or unexported fields
}
StringBuilder is a mutable string builder for efficient string concatenation. Unlike regular string concatenation which creates a new string each time, StringBuilder uses an internal buffer to accumulate strings efficiently.
func NewStringBuilder ¶ added in v0.4.19
func NewStringBuilder() *StringBuilder
NewStringBuilder creates a new StringBuilder instance.
func (*StringBuilder) Cap ¶ added in v0.4.19
func (sb *StringBuilder) Cap() int
Cap returns the current capacity of the builder's internal buffer.
func (*StringBuilder) Clear ¶ added in v0.4.19
func (sb *StringBuilder) Clear()
Clear resets the builder, removing all content.
func (*StringBuilder) Grow ¶ added in v0.4.19
func (sb *StringBuilder) Grow(n int)
Grow grows the builder's capacity to hold at least n more bytes.
func (*StringBuilder) HashKey ¶ added in v0.4.19
func (sb *StringBuilder) HashKey() HashKey
HashKey returns a hash key for the StringBuilder.
func (*StringBuilder) Inspect ¶ added in v0.4.19
func (sb *StringBuilder) Inspect() string
Inspect returns a string representation.
func (*StringBuilder) Len ¶ added in v0.4.19
func (sb *StringBuilder) Len() int
Len returns the current length of the accumulated string.
func (*StringBuilder) Reset ¶ added in v0.4.19
func (sb *StringBuilder) Reset()
Reset is an alias for Clear.
func (*StringBuilder) String ¶ added in v0.4.19
func (sb *StringBuilder) String() string
String returns the accumulated string.
func (*StringBuilder) ToBool ¶ added in v0.4.19
func (sb *StringBuilder) ToBool() *Bool
ToBool returns true (StringBuilder is always truthy).
func (*StringBuilder) Type ¶ added in v0.4.19
func (sb *StringBuilder) Type() ObjectType
Type returns the object type.
func (*StringBuilder) TypeTag ¶ added in v0.4.19
func (sb *StringBuilder) TypeTag() TypeTag
TypeTag returns the fast type tag.
func (*StringBuilder) Write ¶ added in v0.4.19
func (sb *StringBuilder) Write(s string) int
Write appends a string to the builder.
func (*StringBuilder) WriteLine ¶ added in v0.4.19
func (sb *StringBuilder) WriteLine(s string) int
WriteLine appends a string followed by a newline to the builder.
type StringPoolStats ¶ added in v0.4.23
type StringPoolStats struct {
// CacheHits is the number of times a cached string was returned
CacheHits int64
// InternHits is the number of times an interned string was returned
InternHits int64
// PoolHits is the number of times a pooled string was reused
PoolHits int64
// Created is the number of new strings allocated by the pool
Created int64
// Released is the number of strings returned to the pool
Released int64
// Interned is the number of strings currently in the intern cache
Interned int64
}
StringPoolStats tracks statistics about string pool usage
func GetStringPoolStats ¶ added in v0.4.23
func GetStringPoolStats() StringPoolStats
GetStringPoolStats returns current statistics about string pool usage
type TypeTag ¶
type TypeTag uint8
TypeTag is a fast integer type identifier for hot path checks
const ( TagNull TypeTag = iota TagInt TagFloat TagString TagBool TagArray TagMap TagFunction TagBuiltin TagBytes TagClass TagInstance TagError TagReturn TagClosure TagModule TagStringBuilder TagUnknown )
Type tags for fast type checking (must match ObjectType order)
const TagCompiledFunction TypeTag = TagClosure // Use same tag as Closure
TagCompiledFunction is the type tag for compiled functions