engine

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2026 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var GlobalCache = &ScriptCache{files: make(map[string]*CachedScript)}

Functions

func ExecuteFastHTTPResponse

func ExecuteFastHTTPResponse(ctx context.Context, node *Node, scope *Scope) error

ExecuteFastHTTPResponse executes http.response using fast path

func FastHTTPResponse

func FastHTTPResponse(ctx context.Context, w http.ResponseWriter, status int, body interface{}) error

FastHTTPResponse provides optimized path for simple JSON responses Bypasses full node traversal and validation for common case Expected: 2-3x faster than generic http.response handler

func FastVarSet

func FastVarSet(scope *Scope, name string, value interface{})

FastVarSet provides optimized path for simple variable assignment Bypasses node execution overhead

func GetBuffer

func GetBuffer() []byte

GetBuffer retrieves a byte buffer from the pool. Always call PutBuffer when done.

func GetMap

func GetMap() map[string]interface{}

GetMap retrieves a map from the pool. Always call PutMap when done.

func PutArena

func PutArena(a *Arena)

PutArena returns an Arena to the pool after resetting it.

func PutBuffer

func PutBuffer(b []byte)

PutBuffer returns a byte buffer to the pool. The buffer's length is reset to 0 but capacity is preserved.

func PutMap

func PutMap(m map[string]interface{})

PutMap returns a map to the pool after clearing its data.

func PutScope

func PutScope(s *Scope)

PutScope returns a Scope to the pool after clearing its data. This should be called with defer immediately after GetScope(). Example:

scope := engine.GetScope()
defer engine.PutScope(scope)

func TryFastPath

func TryFastPath(ctx context.Context, node *Node, scope *Scope) (bool, error)

TryFastPath attempts to execute node using fast path Returns true if fast path was used, false if should use slow path

Types

type Arena

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

Arena provides high-performance linear memory allocation for request-scoped objects. It bypasses the Go GC for objects allocated within it by using a pre-allocated chunk.

func GetArena

func GetArena() *Arena

GetArena retrieves an Arena from the pool.

func NewArena

func NewArena(capacity int) *Arena

NewArena creates a new Arena with the specified initial capacity.

func (*Arena) Alloc

func (a *Arena) Alloc(n int) unsafe.Pointer

Alloc allocates n bytes of memory from the arena.

func (*Arena) AllocBuffer

func (a *Arena) AllocBuffer(capacity int) []byte

AllocBuffer allocates a byte buffer in the arena.

func (*Arena) AllocMap

func (a *Arena) AllocMap(capacity int) map[string]interface{}

AllocMap allocates a new map in the arena (wrapped as a standard map for compatibility). Warning: standard maps still have GC overhead. This is a bridge implementation.

func (*Arena) AllocScope

func (a *Arena) AllocScope(parent *Scope) *Scope

AllocScope allocates a new Scope in the arena.

func (*Arena) CreateValuePointer

func (a *Arena) CreateValuePointer(v interface{}) unsafe.Pointer

CreateValuePointer creates a pointer to a value in the arena.

func (*Arena) Reset

func (a *Arena) Reset()

Reset resets the arena offset to zero, effectively "freeing" all allocated objects. This is an O(1) operation and does not involve the GC.

func (*Arena) Stats

func (a *Arena) Stats() (used int, total int)

Stats returns information about the arena usage.

type CachedScript

type CachedScript struct {
	Root    *Node
	ModTime time.Time
}

type Diagnostic

type Diagnostic struct {
	Type     string `json:"type"` // "error", "warning"
	Message  string `json:"message"`
	Filename string `json:"filename"`
	Line     int    `json:"line"`
	Col      int    `json:"col"`
	Slot     string `json:"slot,omitempty"`
}

func (Diagnostic) Error

func (d Diagnostic) Error() string

type Engine

type Engine struct {
	Registry map[string]HandlerFunc
	Docs     map[string]SlotMeta // <--- Database Dokumentasi
}

func NewEngine

func NewEngine() *Engine

func (*Engine) Execute

func (e *Engine) Execute(ctx context.Context, node *Node, scope *Scope) (err error)

Execute executes a node with comprehensive panic recovery to ensure runtime immortality. Any panic from user scripts will be caught, logged, and converted to an error.

func (*Engine) GetDocumentation

func (e *Engine) GetDocumentation() map[string]SlotMeta

Helper untuk mengambil semua docs (Sorted by Name)

func (*Engine) GetSortedSlotNames

func (e *Engine) GetSortedSlotNames() []string

Helper untuk mendapatkan list nama slot yang terurut

func (*Engine) Register

func (e *Engine) Register(name string, fn HandlerFunc, meta SlotMeta)

Update Register agar menerima Metadata

func (*Engine) ResolveShorthandValue

func (e *Engine) ResolveShorthandValue(n *Node, scope *Scope) interface{}

ResolveShorthandValue Helper internal untuk memproses nilai pada Variable Shorthand (Exported for analysis)

func (*Engine) ValidateValueType

func (e *Engine) ValidateValueType(val interface{}, expectedType string, node *Node, slotName string) error

ValidateValueType Helper internal untuk memvalidasi tipe data (Exported for slots)

type HandlerFunc

type HandlerFunc func(ctx context.Context, node *Node, scope *Scope) error

type InputMeta

type InputMeta struct {
	Description string `json:"description"`
	Required    bool   `json:"required"`
	Type        string `json:"type,omitempty"` // e.g. "string", "int", "bool"
}

type Lexer

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

func NewLexer

func NewLexer(input string) *Lexer

func (*Lexer) GetLineInfo

func (l *Lexer) GetLineInfo() (int, int)

func (*Lexer) NextToken

func (l *Lexer) NextToken() Token

func (*Lexer) PeekToken

func (l *Lexer) PeekToken() Token

type Node

type Node struct {
	Name     string
	Value    interface{}
	Children []*Node
	Parent   *Node
	Line     int
	Col      int
	Filename string
	// contains filtered or unexported fields
}

func LoadScript

func LoadScript(path string) (*Node, error)

LoadScript membaca dan memparsing file script, atau mengambil dari cache jika belum berubah

func ParseString

func ParseString(content string, filename string) (*Node, error)

ParseString memproses string kode ZenoLang menjadi AST Node

type Scope

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

func GetScope

func GetScope() *Scope

GetScope retrieves a Scope from the pool. The returned Scope is ready to use but may contain data from previous use. Always call PutScope when done to return it to the pool.

func NewScope

func NewScope(parent *Scope) *Scope

func (*Scope) Clone

func (s *Scope) Clone() *Scope

[BARU] Clone membuat salinan scope baru (Deep Copy Level 1) Ini yang dicari oleh router.go

func (*Scope) Delete

func (s *Scope) Delete(key string)

Delete menghapus variabel dari scope current level

func (*Scope) Get

func (s *Scope) Get(key string) (interface{}, bool)

Get mengambil variabel dengan dukungan Dot Notation (user.id, form.judul)

func (*Scope) GetAll

func (s *Scope) GetAll() map[string]interface{}

GetAll mengembalikan semua variabel di scope ini dan scope parent secara rekursif

func (*Scope) GetDefault

func (s *Scope) GetDefault(key string, defaultValue interface{}) interface{}

ToMap mengonversi scope menjadi map standar (berguna untuk template rendering) GetDefault returns the value of the key if found, otherwise returns the default value.

func (*Scope) Reset

func (s *Scope) Reset()

Reset clears all variables from the scope. This is used by the object pool to safely reuse Scope instances without data leakage between requests.

func (*Scope) Set

func (s *Scope) Set(key string, val interface{})

Set menyimpan variabel dengan aman (Thread-Safe)

func (*Scope) SetParent

func (s *Scope) SetParent(parent *Scope)

SetParent updates the parent of the scope

func (*Scope) ToMap

func (s *Scope) ToMap() map[string]interface{}

type ScriptCache

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

func (*ScriptCache) ClearHandlerCache

func (c *ScriptCache) ClearHandlerCache()

ClearHandlerCache membersihkan semua cached handler dan metadata dari Node AST Fungsi ini harus dipanggil sebelum hot reload untuk mencegah panic akibat stale handlers

type SlotMeta

type SlotMeta struct {
	Description    string               `json:"description"`
	Example        string               `json:"example"` // Snippet kode .zl
	Inputs         map[string]InputMeta `json:"inputs,omitempty"`
	RequiredBlocks []string             `json:"required_blocks,omitempty"` // e.g. ["do"], ["then", "else"]
	ValueType      string               `json:"value_type,omitempty"`      // [BARU] Tipe data untuk value utama slot
}

Struct untuk menyimpan Dokumentasi Slot

type Token

type Token struct {
	Type    TokenType
	Literal string
	Line    int
	Column  int
}

type TokenType

type TokenType string
const (
	TokenIdentifier TokenType = "IDENTIFIER"
	TokenColon      TokenType = "COLON"
	TokenString     TokenType = "STRING"
	TokenLBrace     TokenType = "LBRACE"
	TokenRBrace     TokenType = "RBRACE"
	TokenEOF        TokenType = "EOF"
	TokenError      TokenType = "ERROR"
)

Jump to

Keyboard shortcuts

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