vm

package
v0.0.0-...-c72f791 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2024 License: GPL-3.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// CostCall is the cost of the function calling
	CostCall = 50
	// CostContract is the cost of the contract calling
	CostContract = 100
	// CostExtend is the cost of the extend function calling
	CostExtend = 10
)
View Source
const (
	ContractError     = "Contract"
	JSONMarshallError = "JSONMarshall"
	ConversionError   = "Conversion"
	VMErr             = "VM"
)
View Source
const (
	ExtendParentContract   = `parent_contract`   // parent contract name
	ExtendOriginalContract = `original_contract` // original contract name
	ExtendThisContract     = `this_contract`     // current contract name
	ExtendTimeLimit        = `time_limit`        // time limit for contract execution
	ExtendGenBlock         = `gen_block`         // true then we check the time limit
	ExtendTxCost           = `txcost`            // maximum cost limit of the transaction
	ExtendStack            = `stack`             // name of the contract stack

	ExtendSc = `sc` // implements the Stacker interface of struct
	ExtendRt = `rt` // runtime of the contract

	ExtendResult = `result` // result of the contract
)
View Source
const (
	// ShiftContractId is the offset of identifiers
	ShiftContractId = 5000

	// MaxArrayIndex is the maximum index of an array
	MaxArrayIndex = 1000000

	// MaxMapCount is the maximum length of map
	MaxMapCount = 100000

	// MaxCallDepth is the maximum call depth of functions
	MaxCallDepth = 1000

	// MemoryLimit is the maximum memory limit of VM
	MemoryLimit = 128 << 20 // 128 MB
)
View Source
const MaxErrLen = 150

MaxErrLen is the maximum length of error, over which it will be truncated.

View Source
const (
	// TagOptional is the tag of the optional parameter in the contract.
	TagOptional = "optional"
)

Variables

View Source
var (
	ErrMemoryLimit = errors.New("memory limit exceeded")
	ErrVMTimeLimit = errors.New(`time limit exceeded`)
)
View Source
var ErrStackUnderFlow = errors.New("stack under flow")

ErrStackUnderFlow is an error that is returned when the stack is underflow.

Functions

func CalcChecksum

func CalcChecksum(input []byte) uint64

CalcChecksum is calculates checksum, returns crc64 sum.

func CallContract

func CallContract(rt *Runtime, state uint32, name string, params *compiler.Map) (any, error)

CallContract executes the name contract in the state with specified parameters.

func CompileEval

func CompileEval(vm *VM, src string, prefix uint32) error

CompileEval compiles the source code and loads the byte-code into the virtual machine.

func ContractBaseCost

func ContractBaseCost(cb *compiler.CodeBlock) int64

ContractBaseCost returns the base cost of the contract.

func ExecContract

func ExecContract(rt *Runtime, name, fields string, params ...any) (any, error)

ExecContract runs the name contract where fields contains the list of parameters and params are the values of parameters.

func GetContractById

func GetContractById(vm *VM, id int32) *compiler.ContractInfo

GetContractById returns the contract with the specified id.

func GetSettings

func GetSettings(rt *Runtime, cntname, name string) (any, error)

GetSettings returns the value of the setting of the contract.

func MemoryUsage

func MemoryUsage(rt *Runtime) int64

MemoryUsage returns the memory usage of the runtime.

func ObjectExists

func ObjectExists(vm *VM, name string, state uint32) bool

ObjectExists checks if the object with the specified name exists in the virtual machine.

func ParseName

func ParseName(in string) (id int64, name string)

ParseName gets a state identifier and the name of the contract or table from the full name like @[id]name

func ReleaseSmartVMObjects

func ReleaseSmartVMObjects()

func RollbackSmartVMObjects

func RollbackSmartVMObjects()

func Run

func Run(vm *VM, block *compiler.CodeBlock, extend map[string]any) (ret []any, err error)

Run executes the code block with the specified extended variables.

func RunContractById

func RunContractById(vm *VM, id int32, methods []string, extend map[string]any) error

RunContractById executes the contract with the specified id and methods.

func RunContractByName

func RunContractByName(vm *VM, name string, methods []string, extend map[string]any) error

RunContractByName executes the contract with the specified name and methods.

func SavepointSmartVMObjects

func SavepointSmartVMObjects()

func StrToInt64

func StrToInt64(s string) int64

StrToInt64 converts string to int64

func ValueToDecimal

func ValueToDecimal(v any) (ret decimal.Decimal, err error)

ValueToDecimal converts interface (string, float64, Decimal or int64) to Decimal

func ValueToFloat

func ValueToFloat(v any) (ret float64, err error)

ValueToFloat converts interface (string, float64 or int64) to float64

func ValueToInt

func ValueToInt(v any) (ret int64, err error)

ValueToInt converts interface (string or int64) to int64

Types

type ExtFuncErr

type ExtFuncErr struct {
	Name  string
	Value any
}

ExtFuncErr represents error of external function

func (ExtFuncErr) Error

func (e ExtFuncErr) Error() string

type GlobalVm

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

type Runtime

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

Runtime is needed for the execution of the byte-code

func NewRuntime

func NewRuntime(vm *VM, extend map[string]any, cost int64) *Runtime

NewRuntime creates a new Runtime for the virtual machine

func (*Runtime) CostRemain

func (rt *Runtime) CostRemain() int64

CostRemain return the remain cost of the execution.

func (*Runtime) CostUsed

func (rt *Runtime) CostUsed() int64

CostUsed return the used cost of the execution.

func (*Runtime) Run

func (rt *Runtime) Run(block *compiler.CodeBlock) (ret []any, err error)

Run executes a block of code and returns the result and any error that occurred.

func (*Runtime) RunCode

func (rt *Runtime) RunCode(block *compiler.CodeBlock) (status int, err error)

RunCode executes a block of code and returns the status and any error that occurred.

func (*Runtime) SetCost

func (rt *Runtime) SetCost(cost int64)

SetCost sets the max cost of the execution.

func (*Runtime) SubCost

func (rt *Runtime) SubCost(cost int64) error

SubCost subtracts the cost of the execution.

type Stack

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

Stack is a stack that contains elements of any type.

func (*Stack) CheckDepth

func (s *Stack) CheckDepth(min int) error

CheckDepth checks if the stack has at least min elements.

func (*Stack) Stack

func (s *Stack) Stack() []any

Stack returns the stack as a slice.

type Stacker

type Stacker interface {
	AppendStack(fn string) error
	PopStack(fn string)
}

Stacker represents interface for working with call stack

type VM

type VM struct {
	*compiler.CodeBlock
	// a function returns the cost of executing an external golang function.
	ExtCost func(string) int64
	// if the function is in the list, the first of result must be a int64 that the cost of executing.
	FuncCallsDB map[string]struct{}
	// extern mode of compilation. an inter flag indicating whether a contract is an external contract.
	// It is set to true when a VM is created. Contracts called are not displayed
	// when the code is compiled. In other words, it allows to call the contract code
	// determined in the future;
	Extern compiler.IgnoreLevel
	// id of the first contract in the VM.
	ShiftContract int64
	// contains filtered or unexported fields
}

VM is the main type of the virtual machine.

func GetVM

func GetVM() *VM

GetVM is returning smart vm.

func NewVM

func NewVM() *VM

NewVM creates a new virtual machine.

func (*VM) AppendPreVar

func (vm *VM) AppendPreVar(preVar []string) *VM

AppendPreVar appends the predeclared variables to the virtual machine.

func (*VM) Call

func (vm *VM) Call(name string, extend map[string]any) (ret []any, err error)

Call executes the name object with the specified params and extended variables and functions.

func (*VM) Compile

func (vm *VM) Compile(input []rune, conf *compiler.CompConfig) error

Compile compiles a source code and loads the byte-code into the virtual machine.

func (*VM) CompileEval

func (vm *VM) CompileEval(input string, state uint32) error

CompileEval compiles the source code and stores it in the evals map.

func (*VM) EvalIf

func (vm *VM) EvalIf(input string, state uint32, extend map[string]any) (bool, error)

EvalIf runs the conditional expression. It compiles the source code before that if that's necessary.

func (*VM) FlushBlock

func (vm *VM) FlushBlock(root *compiler.CodeBlock)

FlushBlock loads the compiled CodeBlock into the virtual machine.

func (*VM) FlushExtern

func (vm *VM) FlushExtern()

FlushExtern switches off the extern mode of the compilation.

func (*VM) MergeCompConfig

func (vm *VM) MergeCompConfig(conf *compiler.CompConfig) *compiler.CompConfig

MergeCompConfig merges the virtual machine configuration with the compiler configuration.

func (*VM) SetExtendCost

func (vm *VM) SetExtendCost(ext func(string) int64) *VM

SetExtendCost sets the cost of calling extended obj in vm.

func (*VM) SetFuncCallsDB

func (vm *VM) SetFuncCallsDB(funcCallsDB map[string]struct{}) *VM

SetFuncCallsDB Set up functions that can edit the database in vm.

type VMError

type VMError struct {
	Type   string `json:"type"`
	Err    any    `json:"error"`
	Line   int    `json:"line"`
	Column int    `json:"column"`
}

VMError represents error of VM

func (VMError) Error

func (e VMError) Error() string

Jump to

Keyboard shortcuts

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