vm

package
v1.2.10 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2022 License: Apache-2.0 Imports: 23 Imported by: 6

Documentation

Index

Constants

View Source
const (
	ByteMask byte = 0xFF
	HighBit  byte = 0x80
)

Some masking values

View Source
const (
	StNull    structureState = iota
	StDef     structureState = iota
	StDefPlus structureState = iota
	StHandler structureState = iota
	StHndPlus structureState = iota
	StIf      structureState = iota
	StIfPlus  structureState = iota
	StIfMinus structureState = iota
	StElse    structureState = iota
	StError   structureState = iota
)

These are the states in the state machine The Plus and Minus are temporary states to adjust nesting

View Source
const DisassembleTabWidth = 2

DisassembleTabWidth is the number of spaces of indent for each tab in disassembly

View Source
const MaxListSize = 1024

MaxListSize is the max number of List elements that can result from append or extend

Variables

View Source
var DedentOpcodes map[Opcode]struct{}

DedentOpcodes is the set of opcodes which result in a formatting dedent

View Source
var DisasmHelpers = make(map[Opcode]Stringizer)

DisasmHelpers is a map for specific opcodes to override the default renderer.

EnabledOpcodes is a bitset of the opcodes that are enabled -- only these opcodes will be permitted in scripts.

View Source
var IndentOpcodes map[Opcode]struct{}

IndentOpcodes is the set of opcodes which result in a formatting indent

Functions

func DisableOpcode

func DisableOpcode(op Opcode) bool

DisableOpcode allows an opcode to be disabled at runtime; this is for use in a security situation where a vulnerability has been discovered and it's important to make sure that VMs containing this opcode can no longer run.

Note that this has global impact and cannot be reversed! This is to say that there is no equivalent "EnableOpcode" function. Once an opcode is disabled, the only way to re-enable it is to restart the application.

This operates at the level of VM validation -- a VM that is already loaded will not be affected by this operation. The function returns true if the opcode was previously enabled (i.e., if it has had an effect).

func FractionLess

func FractionLess(n1, d1, n2, d2 int64) bool

FractionLess compares two ratios of int64 values (fractions) without exceeding an int64.

func Serialize

func Serialize(name string, comment string, b []byte, w io.Writer) error

Serialize takes a stream of bytes and sends it to a Writer in ChasmBinary format

func SetMaxLengths

func SetMaxLengths(code, total int)

SetMaxLengths allows globally setting the maximum number of bytes a VM may contain.

func ToBytes

func ToBytes(n int64) []byte

ToBytes returns an array of 8 bytes encoding n as a signed value in little-endian form

func ToBytesU

func ToBytesU(n uint64) []byte

ToBytesU returns an array of 8 bytes encoding n as a uint in little-endian form

func Trace

func Trace(vm *ChaincodeVM)

Trace is a Dumper which emits the state of the stack before each instruction

Types

type Bytes

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

Bytes is a Value representing an address on the blockchain

func NewBytes

func NewBytes(ab []byte) *Bytes

NewBytes creates an Bytes

func (*Bytes) Equal

func (vt *Bytes) Equal(rhs Value) bool

Equal implements equality testing for Bytes

func (*Bytes) IsScalar

func (vt *Bytes) IsScalar() bool

IsScalar indicates if this Value is a scalar value type

func (*Bytes) IsTrue

func (vt *Bytes) IsTrue() bool

IsTrue indicates if this Value evaluates to true

func (*Bytes) Less

func (vt *Bytes) Less(rhs Value) (bool, error)

Less implements comparison for Bytes

func (*Bytes) String

func (vt *Bytes) String() string

type CachingNow

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

CachingNow is a Nower that returns the same value over and over

func NewCachingNow

func NewCachingNow(ts Timestamp) (*CachingNow, error)

NewCachingNow returns an initialized instance of a CachingNow given a Timestamp it should use

func (*CachingNow) Now

func (cn *CachingNow) Now() (Timestamp, error)

Now implements Nower for CachingNow

type CachingRand

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

CachingRand is a random number generator that picks a random number once and then returns the same value over and over

func NewCachingRand

func NewCachingRand() (*CachingRand, error)

NewCachingRand returns an initialized instance of a CachingRand

func (*CachingRand) RandInt

func (dr *CachingRand) RandInt() (int64, error)

RandInt implements Randomer for CachingRand

type Chaincode

type Chaincode []Opcode

Chaincode is the type for the VM bytecode program

func ConvertToOpcodes

func ConvertToOpcodes(b []byte) Chaincode

ConvertToOpcodes accepts an array of bytes and returns a Chaincode (array of opcodes)

func MiniAsm

func MiniAsm(s string) Chaincode

MiniAsm is a mini-assembler for Chaincode. It is primarily for testing but we want it available. nolint: deadcode

func MiniAsmSafe

func MiniAsmSafe(s string) (c Chaincode, err error)

MiniAsmSafe is a mini-assembler for chaincode which does not panic on errors.

func ToChaincode

func ToChaincode(b []byte) Chaincode

ToChaincode converts a byte slice into Chaincode

func (Chaincode) Bytes

func (c Chaincode) Bytes() []byte

Bytes converts chaincode into a byte slice

func (Chaincode) CodeSize

func (code Chaincode) CodeSize() int

CodeSize calculates the size of code without counting the size of the data included in PushB opcodes. It should only be called after validateStructure.

func (*Chaincode) DecodeMsg

func (z *Chaincode) DecodeMsg(dc *msgp.Reader) (err error)

DecodeMsg implements msgp.Decodable

func (Chaincode) EncodeMsg

func (z Chaincode) EncodeMsg(en *msgp.Writer) (err error)

EncodeMsg implements msgp.Encodable

func (Chaincode) IsValid

func (c Chaincode) IsValid() error

IsValid tests if an array of opcodes is a potentially valid Chaincode program.

func (Chaincode) MarshalMsg

func (z Chaincode) MarshalMsg(b []byte) (o []byte, err error)

MarshalMsg implements msgp.Marshaler

func (Chaincode) MarshalText

func (c Chaincode) MarshalText() (text []byte, err error)

MarshalText implements encoding.TextMarshaler

func (Chaincode) Msgsize

func (z Chaincode) Msgsize() (s int)

Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message

func (*Chaincode) UnmarshalMsg

func (z *Chaincode) UnmarshalMsg(bts []byte) (o []byte, err error)

UnmarshalMsg implements msgp.Unmarshaler

func (*Chaincode) UnmarshalText

func (c *Chaincode) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler

type ChaincodeVM

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

ChaincodeVM is the reason we're here

func New

func New(bin ChasmBinary) (*ChaincodeVM, error)

New creates a new VM and loads a ChasmBinary into it (or errors)

func NewChaincode

func NewChaincode(c Chaincode) (*ChaincodeVM, error)

NewChaincode creates a new VM from the supplied chaincode

func NewEmpty

func NewEmpty() (*ChaincodeVM, error)

NewEmpty creates a new VM with a minimal empty handler

func (*ChaincodeVM) Bytes

func (vm *ChaincodeVM) Bytes() []byte

Bytes returns the []byte corresponding to the chaincode

func (*ChaincodeVM) CreateForFunc

func (vm *ChaincodeVM) CreateForFunc(funcnum int) (*ChaincodeVM, error)

CreateForFunc creates a new VM from this one that is used to run a function. We assume the function number has already been validated. and is already in an initialized state to run that function. Just call Run() on the new VM after this.

func (*ChaincodeVM) Disassemble

func (vm *ChaincodeVM) Disassemble(pc int) (string, int)

Disassemble returns a single disassembled instruction as a text string, possibly with embedded newlines, along with how many bytes it consumed.

func (*ChaincodeVM) DisassembleAll

func (vm *ChaincodeVM) DisassembleAll(w io.Writer)

DisassembleAll dumps a disassembly of the whole VM to the Writer

func (*ChaincodeVM) DisassembleLine

func (vm *ChaincodeVM) DisassembleLine(pc int) *DisassembledLine

DisassembleLine returns a single disassembled instruction as an object

func (*ChaincodeVM) DisassembleLines

func (vm *ChaincodeVM) DisassembleLines() []*DisassembledLine

DisassembleLines returns a structured disassembly of the whole VM Do not call this on a vm that has not been validated!

func (*ChaincodeVM) HandlerIDs

func (vm *ChaincodeVM) HandlerIDs() []int

HandlerIDs returns a sorted list of handler IDs that are defined for this VM.

func (*ChaincodeVM) History

func (vm *ChaincodeVM) History() []HistoryState

History returns the entire history of this VM's operation

func (*ChaincodeVM) IP

func (vm *ChaincodeVM) IP() int

IP fetches the current instruction pointer (aka program counter)

func (*ChaincodeVM) Init

func (vm *ChaincodeVM) Init(eventID byte, values ...Value) error

Init is called to set up the VM to run the handler for a given eventID. It can take an arbitrary list of values to push on the stack, which it pushes in order -- so if you want something on top of the stack, put it last in the argument list. If the VM doesn't have a handler for the specified eventID, and it also doesn't have a handler for event 0, then Init will return an error.

func (*ChaincodeVM) InitFromStack

func (vm *ChaincodeVM) InitFromStack(eventID byte, stk *Stack) error

InitFromStack initializes a vm with a given starting stack, which should be a new stack

func (*ChaincodeVM) MakeMutable

func (vm *ChaincodeVM) MakeMutable() *MutableChaincodeVM

MakeMutable allows this VM to be mutated externally.

This function consumes the ChaincodeVM instance on which it is defined; no references to that instance should be retained or used after this is called. To help enforce this restriction, this function zeroizes the calling vm.

This call is explicit and grep-able, so codebases which require determinism can easily prove that no script is mutable.

func (*ChaincodeVM) PreLoad

func (vm *ChaincodeVM) PreLoad(cb ChasmBinary) error

PreLoad is the validation function called before loading a VM to make sure it has a hope of loading properly

func (*ChaincodeVM) PreLoadOpcodes

func (vm *ChaincodeVM) PreLoadOpcodes(data Chaincode) error

PreLoadOpcodes acepts an array of opcodes and validates it. If it fails to validate, the VM is not modified. However, if it does validate the VM is updated with code and function tables.

func (*ChaincodeVM) Run

func (vm *ChaincodeVM) Run(debug Dumper) error

Run runs a VM from its current state until it ends

func (*ChaincodeVM) SetNow

func (vm *ChaincodeVM) SetNow(n Nower)

SetNow sets the Nower to call for this VM

func (*ChaincodeVM) SetRand

func (vm *ChaincodeVM) SetRand(r Randomer)

SetRand sets the randomer to call for this VM

func (*ChaincodeVM) Stack

func (vm *ChaincodeVM) Stack() *Stack

Stack returns the current stack of the VM

func (*ChaincodeVM) Step

func (vm *ChaincodeVM) Step(debug Dumper) error

Step executes a single instruction

func (*ChaincodeVM) String

func (vm *ChaincodeVM) String() string

String implements Stringer so we can print a VM and get something meaningful.

type ChasmBinary

type ChasmBinary struct {
	Name    string   `json:"name"`
	Comment string   `json:"comment"`
	Data    []Opcode `json:"data"`
}

ChasmBinary defines the "binary" (assembled) format of a vm

func Deserialize

func Deserialize(r io.Reader) (ChasmBinary, error)

Deserialize takes a reader and extracts a ChasmBinary from it

type DefaultNow

type DefaultNow struct{}

DefaultNow is the default time object, which returns a Timestamp based on the current time.

func NewDefaultNow

func NewDefaultNow() (*DefaultNow, error)

NewDefaultNow returns a new instance of a DefaultNow

func (*DefaultNow) Now

func (dr *DefaultNow) Now() (Timestamp, error)

Now implements Nower for DefaultNow

type DefaultRand

type DefaultRand struct{}

DefaultRand is the default random number generator, which picks a random number from 0 to MaxInt64 using crypto/rand.

func NewDefaultRand

func NewDefaultRand() (*DefaultRand, error)

NewDefaultRand returns a new instance of a DefaultRand

func (*DefaultRand) RandInt

func (dr *DefaultRand) RandInt() (int64, error)

RandInt implements Randomer for DefaultRand

type DisassembledLine

type DisassembledLine struct {
	PC       int
	Opcode   Opcode
	NumExtra int
	ArgBytes []byte
}

DisassembledLine is the data structure intended to be leveraged by a debugging API.

func (DisassembledLine) String

func (l DisassembledLine) String() string

type Dumper

type Dumper func(*ChaincodeVM)

Dumper is a pointer to a function that may be passed to Run or Step, which if it is not nil, is called before each instruction. It can be used to dump the vm, record state, etc.

type HistoryState

type HistoryState struct {
	PC    int
	Stack *Stack
}

HistoryState is a single item in the history of a VM

type Instruction

type Instruction []Opcode

Instruction is an opcode with all of its associated data bytes

type List

type List []Value

List maintains a single list object

func NewList

func NewList(vs ...Value) List

NewList creates a new, empty list.

func (List) Append

func (vt List) Append(v Value) List

Append adds a new Value to the end of a list

func (List) Equal

func (vt List) Equal(rhs Value) bool

Equal implements equality testing for List If the lists are of different lengths, they cannot be equal. If the lengths are the same, they are compared on a per-element basis and the result is the result of the first element that is not equal to its counterpart.

func (List) Extend

func (vt List) Extend(other List) List

Extend generates a new List by appending one List to the end of another

func (List) Index

func (vt List) Index(n int64) (Value, error)

Index returns the value at the given index, or error

func (List) IsScalar

func (vt List) IsScalar() bool

IsScalar indicates if this Value is a scalar value type

func (List) IsTrue

func (vt List) IsTrue() bool

IsTrue indicates if this Value evaluates to true

func (List) Len

func (vt List) Len() int64

Len returns the length of a List as an int64

func (List) Less

func (vt List) Less(rhs Value) (bool, error)

Less implements comparison for List The lists are compared on a per-element basis and the result is the result of the first element that is not equal to its counterpart. If lists are different lengths but equal for comparative lengths, the shorter is less than the longer.

func (List) Map

func (vt List) Map(f func(Value) (Value, error)) (List, error)

Map applies a function to each element of the list and returns a List of the results

func (List) Reduce

func (vt List) Reduce(f func(prev, item Value) Value, init Value) Value

Reduce applies a function to each element of the list and returns an aggregated result

func (List) String

func (vt List) String() string

type MutableChaincodeVM

type MutableChaincodeVM struct {
	ChaincodeVM
}

MutableChaincodeVM is a chaincode vm into which instructions can be injected externally at arbitrary points.

The default, normal sequence of operations is for a script to be immutable: it is created, initialized, run, and values are extracted. Determinism isn't just an accidental property; it's a designed requirement of the system.

However, there are cases, such as within debuggers, in which it is desirable to be able to run arbitrary chaincode commands during the evaluation of a chaincode script. This breaks determinism for that context, but within the debugger, that isn't such a big problem.

An instance of this type can be constructed from a normal ChaincodeVM by calling its MakeMutable method.

func (*MutableChaincodeVM) Inject

func (vm *MutableChaincodeVM) Inject(code []Opcode, debug Dumper) error

Inject runs an instruction on this VM without editing its internal state

type Nower

type Nower interface {
	Now() (Timestamp, error)
}

Nower is an interface for a type that returns the "current" time as a Timestamp The definition of "now" may be defined by context.

type Number

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

A Number is a type of Value representing an int64

func NewFalse

func NewFalse() Number

NewFalse returns a number that is considered to be false

func NewNumber

func NewNumber(n int64) Number

NewNumber creates a Number object out of an int64

func NewTrue

func NewTrue() Number

NewTrue returns a number that is considered to be true

func (Number) AsInt64

func (vt Number) AsInt64() int64

AsInt64 allows retrieving the contents of a Number object as an int64

implements Numeric

func (Number) Equal

func (vt Number) Equal(rhs Value) bool

Equal implements equality testing for Number

func (Number) IsScalar

func (vt Number) IsScalar() bool

IsScalar indicates if this Value is a scalar value type

func (Number) IsTrue

func (vt Number) IsTrue() bool

IsTrue indicates if this Value evaluates to true

func (Number) Less

func (vt Number) Less(rhs Value) (bool, error)

Less implements comparison for Number

func (Number) String

func (vt Number) String() string

type Numeric

type Numeric interface {
	AsInt64() int64
}

Numeric types can be expressed as integers

type Opcode

type Opcode byte

Opcode is a byte used to identify an opcode; we rely on it being a byte in some cases.

const (
	OpNop     Opcode = 0x00
	OpDrop    Opcode = 0x01
	OpDrop2   Opcode = 0x02
	OpDup     Opcode = 0x05
	OpDup2    Opcode = 0x06
	OpSwap    Opcode = 0x09
	OpOver    Opcode = 0x0c
	OpPick    Opcode = 0x0d
	OpRoll    Opcode = 0x0e
	OpTuck    Opcode = 0x0f
	OpRet     Opcode = 0x10
	OpFail    Opcode = 0x11
	OpOne     Opcode = 0x1a
	OpNeg1    Opcode = 0x1b
	OpTrue    Opcode = 0x1b
	OpMaxNum  Opcode = 0x1c
	OpMinNum  Opcode = 0x1d
	OpZero    Opcode = 0x20
	OpFalse   Opcode = 0x20
	OpPush1   Opcode = 0x21
	OpPush2   Opcode = 0x22
	OpPush3   Opcode = 0x23
	OpPush4   Opcode = 0x24
	OpPush5   Opcode = 0x25
	OpPush6   Opcode = 0x26
	OpPush7   Opcode = 0x27
	OpPush8   Opcode = 0x28
	OpPushB   Opcode = 0x2a
	OpPushT   Opcode = 0x2b
	OpNow     Opcode = 0x2c
	OpRand    Opcode = 0x2e
	OpPushL   Opcode = 0x2f
	OpAdd     Opcode = 0x40
	OpSub     Opcode = 0x41
	OpMul     Opcode = 0x42
	OpDiv     Opcode = 0x43
	OpMod     Opcode = 0x44
	OpDivMod  Opcode = 0x45
	OpMulDiv  Opcode = 0x46
	OpNot     Opcode = 0x48
	OpNeg     Opcode = 0x49
	OpInc     Opcode = 0x4a
	OpDec     Opcode = 0x4b
	OpIndex   Opcode = 0x50
	OpLen     Opcode = 0x51
	OpAppend  Opcode = 0x52
	OpExtend  Opcode = 0x53
	OpSlice   Opcode = 0x54
	OpField   Opcode = 0x60
	OpIsField Opcode = 0x61
	OpFieldL  Opcode = 0x70
	OpDef     Opcode = 0x80
	OpCall    Opcode = 0x81
	OpDeco    Opcode = 0x82
	OpEndDef  Opcode = 0x88
	OpIfZ     Opcode = 0x89
	OpIfNZ    Opcode = 0x8a
	OpElse    Opcode = 0x8e
	OpEndIf   Opcode = 0x8f
	OpSum     Opcode = 0x90
	OpAvg     Opcode = 0x91
	OpMax     Opcode = 0x92
	OpMin     Opcode = 0x93
	OpChoice  Opcode = 0x94
	OpWChoice Opcode = 0x95
	OpSort    Opcode = 0x96
	OpLookup  Opcode = 0x97
	OpHandler Opcode = 0xa0
	OpOr      Opcode = 0xb0
	OpAnd     Opcode = 0xb1
	OpXor     Opcode = 0xb2
	OpCount1s Opcode = 0xbc
	OpBNot    Opcode = 0xbf
	OpLt      Opcode = 0xc0
	OpLte     Opcode = 0xc1
	OpEq      Opcode = 0xc2
	OpGte     Opcode = 0xc3
	OpGt      Opcode = 0xc4
)

Opcodes

func (*Opcode) DecodeMsg

func (z *Opcode) DecodeMsg(dc *msgp.Reader) (err error)

DecodeMsg implements msgp.Decodable

func (Opcode) EncodeMsg

func (z Opcode) EncodeMsg(en *msgp.Writer) (err error)

EncodeMsg implements msgp.Encodable

func (Opcode) MarshalMsg

func (z Opcode) MarshalMsg(b []byte) (o []byte, err error)

MarshalMsg implements msgp.Marshaler

func (Opcode) Msgsize

func (z Opcode) Msgsize() (s int)

Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message

func (Opcode) String

func (i Opcode) String() string

func (*Opcode) UnmarshalMsg

func (z *Opcode) UnmarshalMsg(bts []byte) (o []byte, err error)

UnmarshalMsg implements msgp.Unmarshaler

type Randomer

type Randomer interface {
	RandInt() (int64, error)
}

Randomer is an interface for a type that generates "random" integers (which may vary depending on context)

type RunState

type RunState byte

RunState is the current run state of the VM

const (
	RsNotReady RunState = iota
	RsReady    RunState = iota
	RsRunning  RunState = iota
	RsComplete RunState = iota
	RsError    RunState = iota
)

These are runstate constants

type RuntimeError

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

RuntimeError is returned when the vm encounters an error during execution

func (RuntimeError) Error

func (e RuntimeError) Error() string

func (RuntimeError) PC

func (e RuntimeError) PC(pc int) RuntimeError

PC sets the program counter value for an error

type Stack

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

Stack implements the runtime stack for our VM

func NewStack

func NewStack() *Stack

NewStack creates a new, empty Stack

func (*Stack) Clone

func (st *Stack) Clone() *Stack

Clone makes a snapshot copy of a stack

func (*Stack) Depth

func (st *Stack) Depth() int

Depth returns the depth of the stack

func (*Stack) Get

func (st *Stack) Get(n int) (Value, error)

Get retrieves the item at index n and returns it

func (*Stack) InsertAt

func (st *Stack) InsertAt(n int, v Value) error

InsertAt inserts a value between positions N and N-1 of the stack, counting from the top. If N is 0, this is equivalent to Push.

func (*Stack) Peek

func (st *Stack) Peek() (Value, error)

Peek retrieves the top value and returns it

func (*Stack) Pop

func (st *Stack) Pop() (Value, error)

Pop removes the top value and returns it

func (*Stack) PopAsInt64

func (st *Stack) PopAsInt64() (int64, error)

PopAsInt64 retrieves the top entry on the stack as an int64 or errors

func (*Stack) PopAsList

func (st *Stack) PopAsList() (List, error)

PopAsList retrieves the top entry on the stack as a List or errors

func (*Stack) PopAsListOfStructs

func (st *Stack) PopAsListOfStructs(ix int) (List, error)

PopAsListOfStructs retrieves the top entry on the stack as a List, and then checks to make sure that every element in the list is a Struct and that all of the structs in the list are compatible. If ix >= 0, it also verifies that the struct has a numeric field at the given offset. If anything isn't true, it errors.

func (*Stack) PopAsStruct

func (st *Stack) PopAsStruct() (*Struct, error)

PopAsStruct retrieves the top entry on the stack as a Struct or errors

func (*Stack) PopAt

func (st *Stack) PopAt(n int) (Value, error)

PopAt removes the nth value and returns it

func (*Stack) Push

func (st *Stack) Push(v Value) error

Push puts a value on top of the stack

func (*Stack) String

func (st *Stack) String() string

String renders a stack with one line per value

func (*Stack) TopN

func (st *Stack) TopN(n int) (*Stack, error)

TopN copies the top N items on the stack and returns a new stack with them

type Stringizer

type Stringizer func(op Opcode, extra []Opcode) string

Stringizer is used to override the default behavior of the disassembler for specific opcodes.

type Struct

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

Struct maintains a single struct object; it maintains a map of byte ids to fields

func NewStruct

func NewStruct() *Struct

NewStruct creates a new, empty struct.

func NewTestStruct

func NewTestStruct(vs ...Value) *Struct

NewTestStruct creates a new struct with an arbitrary list of fields. The fields will be created with an index in order beginning at 0. This is really only intended for testing.

func NewTupleStruct

func NewTupleStruct(vs ...Value) *Struct

NewTupleStruct creates a new struct with an arbitrary list of fields. The fields will be created with an index in order beginning at 0.

func (*Struct) Equal

func (vt *Struct) Equal(rhs Value) bool

Equal implements comparison for Struct. If rhs is not a Struct, errors. If the two structs have different values for validFields, then the result is false. If they have the same field set, they are compared field by field in numeric order and the result is the result from the first element that is not equal to its counterpart.

func (*Struct) Get

func (vt *Struct) Get(ix byte) (Value, error)

Get retrieves the field at a given index

func (*Struct) Indices

func (vt *Struct) Indices() []byte

Indices returns the list of indices that are defined in this struct.

func (*Struct) IsCompatible

func (vt *Struct) IsCompatible(other *Struct) bool

IsCompatible returns true if the other struct list of validFields is equal to the receiver's list.

func (*Struct) IsScalar

func (vt *Struct) IsScalar() bool

IsScalar indicates if this Value is a scalar value type

func (*Struct) IsTrue

func (vt *Struct) IsTrue() bool

IsTrue indicates if this Value evaluates to true

func (*Struct) Len

func (vt *Struct) Len() int

Len returns the number of fields in this struct

func (*Struct) Less

func (vt *Struct) Less(rhs Value) (bool, error)

Less implements comparison for Struct. If rhs is not a Struct, errors. If the two structs have different values for validFields, then the result is the result of comparing the new validFields objects. If they have the same field set, they are compared field by field in numeric order and the result is the result from the first element that is not equal to its counterpart.

func (*Struct) SafeSet

func (vt *Struct) SafeSet(ix byte, v Value) (*Struct, error)

SafeSet assigns a value to a field at index ix, only if the there is no field already at that index.

func (*Struct) Set

func (vt *Struct) Set(ix byte, v Value) *Struct

Set assigns a value to a field at index ix and returns it.

func (*Struct) String

func (vt *Struct) String() string

type Timestamp

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

Timestamp is a Value type representing duration since the epoch

func NewTimestamp

func NewTimestamp(t types.Timestamp) Timestamp

NewTimestamp returns a timestamp taken from a ndaumath/pkg/types.Timestamp struct

func NewTimestampFromInt

func NewTimestampFromInt(n int64) Timestamp

NewTimestampFromInt creates a timestamp from an int64 representation of one

func NewTimestampFromTime

func NewTimestampFromTime(t time.Time) (Timestamp, error)

NewTimestampFromTime returns a timestamp taken from a time.Time struct in Go.

func ParseTimestamp

func ParseTimestamp(s string) (Timestamp, error)

ParseTimestamp creates a timestamp from an ISO-3933 string

func (Timestamp) AsInt64

func (vt Timestamp) AsInt64() int64

AsInt64 implements Numeric

func (Timestamp) Equal

func (vt Timestamp) Equal(rhs Value) bool

Equal implements equality testing for Timestamp

func (Timestamp) IsScalar

func (vt Timestamp) IsScalar() bool

IsScalar indicates if this Value is a scalar value type

func (Timestamp) IsTrue

func (vt Timestamp) IsTrue() bool

IsTrue indicates if this Value evaluates to true

func (Timestamp) Less

func (vt Timestamp) Less(rhs Value) (bool, error)

Less implements comparison for Timestamp

func (Timestamp) String

func (vt Timestamp) String() string

func (Timestamp) T

func (vt Timestamp) T() int64

T returns the timestamp as a int64 duration in uSec since the start of epoch.

type ValidationError

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

ValidationError is returned when the code is invalid and cannot be loaded or run

func (ValidationError) Error

func (e ValidationError) Error() string

type Value

type Value interface {
	Equal(rhs Value) bool
	Less(rhs Value) (bool, error)
	IsScalar() bool
	String() string
	IsTrue() bool
}

Value objects are what is managed by the VM

type ValueError

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

ValueError is the error type when value conflicts arise

func (ValueError) Error

func (e ValueError) Error() string

Jump to

Keyboard shortcuts

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