dicescript

package module
v0.0.0-...-b06b5f3 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2024 License: Apache-2.0 Imports: 18 Imported by: 2

README

DiceScript

Go Report Card Software License GoDoc

通用TRPG骰点脚本语言。

Simple script language for TRPG dice engine.

特性:

  • 支持整数、浮点数、字符串、数组、字典、函数,常见算符以及if和while逻辑语句
  • 支持形如d20等的trpg用骰点语法
  • 全类型可序列化(包括函数在内)
  • 对模板字符串语法做大量优化,可胜任模板引擎
  • 易于使用,方便扩展
  • 稳定可靠,极高的测试覆盖率
  • 免费并可商用
  • 可编译到JavaScript

测试页面:

https://sealdice.github.io/dicescript/

这个项目是海豹核心的骰点解释器的完全重构。 从第一次实现中吸取了很多经验和教训,并尝试做得更好。

如何使用

DiceScript指南

你可以从这里了解如何使用DiceScript进行骰点,编写自己的TRPG规则,以及如何嵌入到任何你想要的地方。

设计原则

  • 从主流语言和跑团软件中借鉴语法,如Golang/Python/JS/Fvtt/BCDice,不随意发明
  • 符合国内跑团指令的一般习惯
  • 要具有较强的配置和扩展能力,符合trpg场景的需求
  • 一定限度内容忍全角符号
  • 兼容gopherjs
  • 良好的错误提示文本
  • 支持多线程

进度

  • 基础类型 int float string null
  • 一元算符 + -
  • 二元算符 +-*/% >,>=,==,!=,<,<=,&,|,&&,||
  • 三元算符 ? :
  • 空值合并算符 ??
  • 骰点运算 - 流行语法: d20, 3d20, (4+5)d(20), 2d20k1, 2d20q1
  • 骰点运算 - fvtt语法: 2d20kl, 2d20kh, 2d20dl, 2d20dh, d20min10, d20max10
  • 骰点运算 - CoC / Fate / WoD / Double Cross
  • 骰点运算 - 自定义算符
  • 高级类型 数组array
  • 高级类型 字典dict
  • 高级类型 计算数值computed
  • 逻辑语法 if ... else ..
  • 逻辑语法 while
  • 函数支持
  • 内置函数
  • 分片语法
  • 区间数组
  • 变量支持
  • 序列化和反序列化
  • 计算过程显示
  • 角色属性对接
  • 报错信息优化
  • 线程安全
  • 变量作用域
  • 测试覆盖率 86% / 90%

更新记录

更新记录

TODO

  • 骰点运算 - 自定义算符
  • computed 的repr格式无法读入

开发

如果修改了文法,使用这个工具重新生成:

go install github.com/fy0/pigeon@latest
pigeon -nolint -optimize-parser -optimize-ref-expr-by-index -o .\roll.peg.go .\roll.peg

Documentation

Index

Constants

View Source
const IntTypeSize = strconv.IntSize / 8 // 只能为 4 或 8(32位/64位)

Variables

This section is empty.

Functions

func GetAsmText

func GetAsmText(code []ByteCode, codeIndex int) string

func RollDoubleCross

func RollDoubleCross(src *rand.PCGSource, addLine IntType, pool IntType, points IntType, mode int) (IntType, IntType, IntType, string)

func RollWoD

func RollWoD(src *rand.PCGSource, addLine IntType, pool IntType, points IntType, threshold IntType, isGE bool, mode int) (IntType, IntType, IntType, string)

RollWoD 返回: 成功数,总骰数,轮数,细节

func ValueEqual

func ValueEqual(a *VMValue, b *VMValue, autoConvert bool) bool

Types

type ArrayData

type ArrayData struct {
	List []*VMValue
}

type BufferSpan

type BufferSpan struct {
	Begin IntType
	End   IntType
	Ret   *VMValue
	Text  string

	Expr string // 如果存在Expr,用来替代等号左边的内容,不存在的话用Begin和End从原文提取
	Tag  string // 来源标记
}

type ByteCode

type ByteCode struct {
	T     CodeType
	Value any
}

func (*ByteCode) CodeString

func (code *ByteCode) CodeString() string

type CodeType

type CodeType uint8

type ComputedData

type ComputedData struct {
	Expr string

	/* 缓存数据 */
	Attrs *ValueMap
	// contains filtered or unexported fields
}

type Context

type Context struct {
	Attrs *ValueMap
	UpCtx *Context

	NumOpCount IntType // 算力计数

	Config RollConfig // 标记
	Error  error      // 报错信息

	Ret         *VMValue // 返回值
	RestInput   string   // 剩余字符串
	Matched     string   // 匹配的字符串
	DetailSpans []BufferSpan

	IsComputedLoaded bool

	Seed    []byte          // 随机种子,16个字节,即双uint64
	RandSrc *rand.PCGSource // 根据种子生成的source

	IsRunning      bool // 是否正在运行,Run时会置为true,halt时会置为false
	CustomDiceInfo []*customDiceItem

	// 全局scope的写入回调
	GlobalValueStoreFunc func(name string, v *VMValue)
	// 全局scope的读取回调
	GlobalValueLoadFunc func(name string) *VMValue
	// 全局scope的读取后回调(返回值将覆盖之前读到的值。如果之前未读取到值curVal将为nil)
	GlobalValueLoadOverwriteFunc func(name string, curVal *VMValue) *VMValue
	// contains filtered or unexported fields
}

func NewVM

func NewVM() *Context

func (*Context) Depth

func (ctx *Context) Depth() int

func (*Context) GetAsmText

func (ctx *Context) GetAsmText() string

func (*Context) GetCurSeed

func (ctx *Context) GetCurSeed() ([]byte, error)

func (*Context) GetDetailText

func (ctx *Context) GetDetailText() string

func (*Context) GetErrorText

func (ctx *Context) GetErrorText() string

GetErrorText 主要用于js,因为ctx.Error是数组,在js那边不被当做正常的异常对象,所以会报错

func (*Context) Init

func (ctx *Context) Init()

func (*Context) IsCalculateExists

func (ctx *Context) IsCalculateExists() bool

IsCalculateExists 只有表达式被解析后,才能被调用,暂不考虑存在invoke指令的情况

func (*Context) LoadName

func (ctx *Context) LoadName(name string, isRaw bool, useHook bool) *VMValue

func (*Context) LoadNameGlobal

func (ctx *Context) LoadNameGlobal(name string, isRaw bool) *VMValue

func (*Context) LoadNameGlobalWithDetail

func (ctx *Context) LoadNameGlobalWithDetail(name string, isRaw bool, detail *BufferSpan) *VMValue

func (*Context) LoadNameLocal

func (ctx *Context) LoadNameLocal(name string, isRaw bool) *VMValue

func (*Context) LoadNameLocalWithDetail

func (ctx *Context) LoadNameLocalWithDetail(name string, isRaw bool, detail *BufferSpan) *VMValue

func (*Context) LoadNameWithDetail

func (ctx *Context) LoadNameWithDetail(name string, isRaw bool, useHook bool, detail *BufferSpan) *VMValue

func (*Context) Parse

func (ctx *Context) Parse(value string) error

func (*Context) RegCustomDice

func (ctx *Context) RegCustomDice(s string, callback func(ctx *Context, groups []string) *VMValue) error

func (*Context) Run

func (ctx *Context) Run(value string) error

Run 执行给定语句

func (*Context) RunAfterParsed

func (ctx *Context) RunAfterParsed() error

func (*Context) RunExpr

func (ctx *Context) RunExpr(value string, useUpCtxLocal bool) (*VMValue, error)

RunExpr 注: 最后不一定叫这个名字,这个函数作用是,即使当前vm被占用,也能执行语句,是为了指令hack而服务的

func (*Context) SetConfig

func (ctx *Context) SetConfig(cfg *RollConfig)

func (*Context) StackTop

func (ctx *Context) StackTop() int

func (*Context) StoreName

func (ctx *Context) StoreName(name string, v *VMValue, useHook bool)

StoreName 储存变量

func (*Context) StoreNameGlobal

func (ctx *Context) StoreNameGlobal(name string, v *VMValue)

func (*Context) StoreNameLocal

func (ctx *Context) StoreNameLocal(name string, v *VMValue)

type DictData

type DictData struct {
	Dict *ValueMap
}

type FunctionData

type FunctionData struct {
	Expr     string
	Name     string
	Params   []string
	Defaults []*VMValue

	/* 缓存数据 */
	Self *VMValue // 若存在self,即为bound method
	// contains filtered or unexported fields
}

type IntType

type IntType int // :IntType

func Roll

func Roll(src *rand.PCGSource, dicePoints IntType, mod int) IntType

func RollCoC

func RollCoC(src *rand.PCGSource, isBonus bool, diceNum IntType, mode int) (IntType, string)

func RollCommon

func RollCommon(src *rand.PCGSource, times, dicePoints IntType, diceMin, diceMax *IntType, isKeepLH, lowNum, highNum IntType, mode int) (IntType, string)

RollCommon (times)d(dicePoints)kl(lowNum) 或 (times)d(dicePoints)kh(highNum)

func RollFate

func RollFate(src *rand.PCGSource, mode int) (IntType, string)

type NativeFunctionData

type NativeFunctionData struct {
	Name     string
	Params   []string
	Defaults []*VMValue

	/* 缓存数据 */
	Self       *VMValue // 若存在self,即为bound method
	NativeFunc NativeFunctionDef
}

type NativeFunctionDef

type NativeFunctionDef func(ctx *Context, this *VMValue, params []*VMValue) *VMValue

type NativeObjectData

type NativeObjectData struct {
	Name     string
	AttrSet  func(ctx *Context, name string, v *VMValue)
	AttrGet  func(ctx *Context, name string) *VMValue
	ItemSet  func(ctx *Context, index *VMValue, v *VMValue)
	ItemGet  func(ctx *Context, index *VMValue) *VMValue
	DirFunc  func(ctx *Context) []*VMValue
	ToString func(ctx *Context) string
}

type ParserCustomData

type ParserCustomData struct {
	ParserData
}

type ParserData

type ParserData struct {
	Config RollConfig
	// contains filtered or unexported fields
}

func (*ParserData) AddAttrSet

func (p *ParserData) AddAttrSet(objName string, attr string, isRaw bool)

func (*ParserData) AddDiceDetail

func (p *ParserData) AddDiceDetail(begin IntType, end IntType)

func (*ParserData) AddFormatString

func (e *ParserData) AddFormatString(num IntType)

func (*ParserData) AddInvoke

func (e *ParserData) AddInvoke(paramsNum IntType)

func (*ParserData) AddInvokeMethod

func (e *ParserData) AddInvokeMethod(name string, paramsNum IntType)

func (*ParserData) AddLoadName

func (e *ParserData) AddLoadName(value string)

func (*ParserData) AddOp

func (e *ParserData) AddOp(operator CodeType)

func (*ParserData) AddStModify

func (e *ParserData) AddStModify(op string, text string)

func (*ParserData) AddStName

func (e *ParserData) AddStName()

func (*ParserData) AddStore

func (e *ParserData) AddStore(text string)

func (*ParserData) AddStoreComputed

func (p *ParserData) AddStoreComputed(name string, text string)

func (*ParserData) AddStoreComputedOnStack

func (p *ParserData) AddStoreComputedOnStack(text string)

func (*ParserData) AddStoreFunction

func (p *ParserData) AddStoreFunction(name string, paramsReversed []string, text string)

func (*ParserData) AddStoreGlobal

func (e *ParserData) AddStoreGlobal(text string)

func (*ParserData) AddStoreLocal

func (e *ParserData) AddStoreLocal(text string)

func (*ParserData) BreakPush

func (p *ParserData) BreakPush() error

func (*ParserData) BreakSet

func (p *ParserData) BreakSet()

func (*ParserData) CodePop

func (p *ParserData) CodePop() ([]ByteCode, int, int)

func (*ParserData) CodePush

func (p *ParserData) CodePush(textPos int)

func (*ParserData) ContinuePush

func (p *ParserData) ContinuePush() error

func (*ParserData) ContinueSet

func (p *ParserData) ContinueSet(offsetB int)

func (*ParserData) CounterAdd

func (e *ParserData) CounterAdd(offset IntType)

func (*ParserData) CounterPop

func (e *ParserData) CounterPop() IntType

func (*ParserData) CounterPush

func (e *ParserData) CounterPush()

func (*ParserData) FlagsPop

func (e *ParserData) FlagsPop()

func (*ParserData) FlagsPush

func (e *ParserData) FlagsPush()

func (*ParserData) LoopBegin

func (e *ParserData) LoopBegin()

func (*ParserData) LoopEnd

func (e *ParserData) LoopEnd()

func (*ParserData) NamePop

func (e *ParserData) NamePop() string

func (*ParserData) NamePush

func (e *ParserData) NamePush(test string)

func (*ParserData) OffsetJmpSetX

func (e *ParserData) OffsetJmpSetX(offsetA int, offsetB int, rev bool)

func (*ParserData) OffsetPopAndSet

func (e *ParserData) OffsetPopAndSet()

func (*ParserData) OffsetPopN

func (e *ParserData) OffsetPopN(num int)

func (*ParserData) OffsetPush

func (e *ParserData) OffsetPush()

func (*ParserData) PushArray

func (e *ParserData) PushArray(value IntType)

func (*ParserData) PushDict

func (e *ParserData) PushDict(value IntType)

func (*ParserData) PushFloatNumber

func (e *ParserData) PushFloatNumber(value string)

func (*ParserData) PushGlobal

func (e *ParserData) PushGlobal()

func (*ParserData) PushIntNumber

func (e *ParserData) PushIntNumber(value string)

func (*ParserData) PushNull

func (e *ParserData) PushNull()

func (*ParserData) PushStr

func (e *ParserData) PushStr(value string)

func (*ParserData) PushThis

func (e *ParserData) PushThis()

func (*ParserData) WriteCode

func (e *ParserData) WriteCode(T CodeType, value any)

type RollConfig

type RollConfig struct {
	EnableDiceWoD         bool // 启用WOD骰子语法,即XaYmZkNqM,X个数,Y加骰线,Z面数,N阈值(>=),M阈值(<=)
	EnableDiceCoC         bool // 启用COC骰子语法,即bX/pX奖惩骰
	EnableDiceFate        bool // 启用Fate骰语法,即fX
	EnableDiceDoubleCross bool // 启用双十字骰语法,即XcY

	DisableBitwiseOp bool // 禁用位运算,用于st,如 &a=1d4
	DisableStmts     bool // 禁用语句语法(如if while等),仅允许表达式
	DisableNDice     bool // 禁用Nd语法,即只能2d6这样写,不能写2d

	// 如果返回值为true,那么跳过剩下的储存流程。如果overwrite不为nil,对v进行覆盖。
	// 另注: 钩子函数中含有ctx的原因是可能在函数中进行调用,此时ctx会发生变化
	HookFuncValueStore func(ctx *Context, name string, v *VMValue) (overwrite *VMValue, solved bool)
	// 如果overwrite不为nil,将结束值加载并使用overwrite值。如果为nil,将以newName为key进行加载
	HookFuncValueLoad func(ctx *Context, name string) (newName string, overwrite *VMValue)
	// 读取后回调(返回值将覆盖之前读到的值。如果之前未读取到值curVal将为nil),用户需要在里面调用doCompute保证结果正确
	HookFuncValueLoadOverwrite func(ctx *Context, name string, curVal *VMValue, doCompute func(curVal *VMValue) *VMValue, detail *BufferSpan) *VMValue

	// st回调,注意val和extra都经过clone,可以放心储存
	CallbackSt           func(_type string, name string, val *VMValue, extra *VMValue, op string, detail string) // st回调
	CustomMakeDetailFunc func(ctx *Context, details []BufferSpan, dataBuffer []byte) string                      // 自定义计算过程

	ParseExprLimit      uint64  // 解析算力限制,防止构造特殊语句进行DOS攻击,0为无限,建议值1000万
	OpCountLimit        IntType // 算力限制,超过这个值会报错,0为无限,建议值30000
	DefaultDiceSideExpr string  // 默认骰子面数

	PrintBytecode bool // 执行时打印字节码
	IgnoreDiv0    bool // 当div0时暂不报错

	DiceMinMode bool // 骰子以最小值结算,用于获取下界
	DiceMaxMode bool // 以最大值结算 获取上界
	// contains filtered or unexported fields
}

type StInfo

type StInfo struct {
	Op   string
	Text string
}

type Stats

type Stats struct {
	// ExprCnt counts the number of expressions processed during parsing
	// This value is compared to the maximum number of expressions allowed
	// (set by the MaxExpressions option).
	ExprCnt uint64

	// ChoiceAltCnt is used to count for each ordered choice expression,
	// which alternative is used how may times.
	// These numbers allow to optimize the order of the ordered choice expression
	// to increase the performance of the parser
	//
	// The outer key of ChoiceAltCnt is composed of the exprType of the rule as well
	// as the line and the column of the ordered choice.
	// The inner key of ChoiceAltCnt is the number (one-based) of the matching alternative.
	// For each alternative the number of matches are counted. If an ordered choice does not
	// match, a special counter is incremented. The exprType of this counter is set with
	// the parser option Statistics.
	// For an alternative to be included in ChoiceAltCnt, it has to match at least once.
	ChoiceAltCnt map[string]map[string]int
}

Stats stores some statistics, gathered during parsing

type VMDictValue

type VMDictValue VMValue

func NewDictVal

func NewDictVal(data *ValueMap) *VMDictValue

func NewDictValWithArray

func NewDictValWithArray(arr ...*VMValue) (*VMDictValue, error)

func NewDictValWithArrayMust

func NewDictValWithArrayMust(arr ...*VMValue) *VMDictValue

func (*VMDictValue) Load

func (d *VMDictValue) Load(key string) (value *VMValue, ok bool)

Load value为变量的值,ok代表是否找到变量

func (*VMDictValue) Range

func (d *VMDictValue) Range(callback func(key string, value *VMValue) bool)

func (*VMDictValue) Store

func (d *VMDictValue) Store(key string, value *VMValue)

func (*VMDictValue) ToString

func (d *VMDictValue) ToString() string

func (*VMDictValue) V

func (d *VMDictValue) V() *VMValue

type VMValue

type VMValue struct {
	TypeId VMValueType `json:"t"`
	Value  any         `json:"v"`
}

func NewArrayVal

func NewArrayVal(values ...*VMValue) *VMValue

func NewArrayValRaw

func NewArrayValRaw(data []*VMValue) *VMValue

func NewComputedVal

func NewComputedVal(expr string) *VMValue

func NewComputedValRaw

func NewComputedValRaw(computed *ComputedData) *VMValue

func NewFloatVal

func NewFloatVal(i float64) *VMValue

func NewFunctionValRaw

func NewFunctionValRaw(computed *FunctionData) *VMValue

func NewIntVal

func NewIntVal(i IntType) *VMValue

func NewNativeFunctionVal

func NewNativeFunctionVal(data *NativeFunctionData) *VMValue

func NewNativeObjectVal

func NewNativeObjectVal(data *NativeObjectData) *VMValue

func NewNullVal

func NewNullVal() *VMValue

func NewStrVal

func NewStrVal(s string) *VMValue

func VMValueFromJSON

func VMValueFromJSON(data []byte) (*VMValue, error)

func (*VMValue) ArrayFuncKeepBase

func (v *VMValue) ArrayFuncKeepBase(ctx *Context, pickNum IntType, orderType int) (isAllInt bool, ret float64)

func (*VMValue) ArrayFuncKeepHigh

func (v *VMValue) ArrayFuncKeepHigh(ctx *Context, pickNum IntType) (isAllInt bool, ret float64)

func (*VMValue) ArrayFuncKeepLow

func (v *VMValue) ArrayFuncKeepLow(ctx *Context, pickNum IntType) (isAllInt bool, ret float64)

func (*VMValue) ArrayItemGet

func (v *VMValue) ArrayItemGet(ctx *Context, index IntType) *VMValue

func (*VMValue) ArrayItemSet

func (v *VMValue) ArrayItemSet(ctx *Context, index IntType, val *VMValue) bool

func (*VMValue) ArrayRepeatTimesEx

func (v *VMValue) ArrayRepeatTimesEx(ctx *Context, times *VMValue) *VMValue

func (*VMValue) AsBool

func (v *VMValue) AsBool() bool

func (*VMValue) AsDictKey

func (v *VMValue) AsDictKey() (string, error)

func (*VMValue) AttrGet

func (v *VMValue) AttrGet(ctx *Context, name string) *VMValue

AttrGet 如果返回nil 说明不支持 . 取属性

func (*VMValue) AttrSet

func (v *VMValue) AttrSet(ctx *Context, name string, val *VMValue) *VMValue

func (*VMValue) Clone

func (v *VMValue) Clone() *VMValue

func (*VMValue) ComputedExecute

func (v *VMValue) ComputedExecute(ctx *Context, detail *BufferSpan) *VMValue

func (*VMValue) FuncInvoke

func (v *VMValue) FuncInvoke(ctx *Context, params []*VMValue) *VMValue

func (*VMValue) FuncInvokeNative

func (v *VMValue) FuncInvokeNative(ctx *Context, params []*VMValue) *VMValue

func (*VMValue) FuncInvokeRaw

func (v *VMValue) FuncInvokeRaw(ctx *Context, params []*VMValue, useUpCtxLocal bool) *VMValue

func (*VMValue) GetSlice

func (v *VMValue) GetSlice(ctx *Context, a IntType, b IntType, step IntType) *VMValue

func (*VMValue) GetSliceEx

func (v *VMValue) GetSliceEx(ctx *Context, a *VMValue, b *VMValue) *VMValue

func (*VMValue) GetTypeName

func (v *VMValue) GetTypeName() string

func (*VMValue) ItemGet

func (v *VMValue) ItemGet(ctx *Context, index *VMValue) *VMValue

func (*VMValue) ItemSet

func (v *VMValue) ItemSet(ctx *Context, index *VMValue, val *VMValue) bool

func (*VMValue) Length

func (v *VMValue) Length(ctx *Context) IntType

func (*VMValue) MustReadArray

func (v *VMValue) MustReadArray() *ArrayData

func (*VMValue) MustReadDictData

func (v *VMValue) MustReadDictData() *DictData

func (*VMValue) MustReadFloat

func (v *VMValue) MustReadFloat() float64

func (*VMValue) MustReadInt

func (v *VMValue) MustReadInt() IntType

func (*VMValue) OpAdd

func (v *VMValue) OpAdd(ctx *Context, v2 *VMValue) *VMValue

func (*VMValue) OpBitwiseAnd

func (v *VMValue) OpBitwiseAnd(ctx *Context, v2 *VMValue) *VMValue

func (*VMValue) OpBitwiseOr

func (v *VMValue) OpBitwiseOr(ctx *Context, v2 *VMValue) *VMValue

func (*VMValue) OpCompEQ

func (v *VMValue) OpCompEQ(ctx *Context, v2 *VMValue) *VMValue

func (*VMValue) OpCompGE

func (v *VMValue) OpCompGE(ctx *Context, v2 *VMValue) *VMValue

func (*VMValue) OpCompGT

func (v *VMValue) OpCompGT(ctx *Context, v2 *VMValue) *VMValue

func (*VMValue) OpCompLE

func (v *VMValue) OpCompLE(ctx *Context, v2 *VMValue) *VMValue

func (*VMValue) OpCompLT

func (v *VMValue) OpCompLT(ctx *Context, v2 *VMValue) *VMValue

func (*VMValue) OpCompNE

func (v *VMValue) OpCompNE(ctx *Context, v2 *VMValue) *VMValue

func (*VMValue) OpDivide

func (v *VMValue) OpDivide(ctx *Context, v2 *VMValue) *VMValue

func (*VMValue) OpModulus

func (v *VMValue) OpModulus(ctx *Context, v2 *VMValue) *VMValue

func (*VMValue) OpMultiply

func (v *VMValue) OpMultiply(ctx *Context, v2 *VMValue) *VMValue

func (*VMValue) OpNegation

func (v *VMValue) OpNegation() *VMValue

func (*VMValue) OpNullCoalescing

func (v *VMValue) OpNullCoalescing(ctx *Context, v2 *VMValue) *VMValue

func (*VMValue) OpPositive

func (v *VMValue) OpPositive() *VMValue

func (*VMValue) OpPower

func (v *VMValue) OpPower(ctx *Context, v2 *VMValue) *VMValue

func (*VMValue) OpSub

func (v *VMValue) OpSub(ctx *Context, v2 *VMValue) *VMValue

func (*VMValue) ReadArray

func (v *VMValue) ReadArray() (*ArrayData, bool)

func (*VMValue) ReadComputed

func (v *VMValue) ReadComputed() (*ComputedData, bool)

func (*VMValue) ReadDictData

func (v *VMValue) ReadDictData() (*DictData, bool)

func (*VMValue) ReadFloat

func (v *VMValue) ReadFloat() (float64, bool)

func (*VMValue) ReadFunctionData

func (v *VMValue) ReadFunctionData() (*FunctionData, bool)

func (*VMValue) ReadInt

func (v *VMValue) ReadInt() (IntType, bool)

func (*VMValue) ReadNativeFunctionData

func (v *VMValue) ReadNativeFunctionData() (*NativeFunctionData, bool)

func (*VMValue) ReadNativeObjectData

func (v *VMValue) ReadNativeObjectData() (*NativeObjectData, bool)

func (*VMValue) ReadString

func (v *VMValue) ReadString() (string, bool)

func (*VMValue) SetSlice

func (v *VMValue) SetSlice(ctx *Context, a, b, step IntType, val *VMValue) bool

func (*VMValue) SetSliceEx

func (v *VMValue) SetSliceEx(ctx *Context, a *VMValue, b *VMValue, val *VMValue) bool

func (*VMValue) ToJSON

func (v *VMValue) ToJSON() ([]byte, error)

func (*VMValue) ToJSONRaw

func (v *VMValue) ToJSONRaw(save map[*VMValue]bool) ([]byte, error)

func (*VMValue) ToRepr

func (v *VMValue) ToRepr() string

func (*VMValue) ToString

func (v *VMValue) ToString() string

func (*VMValue) UnmarshalJSON

func (v *VMValue) UnmarshalJSON(input []byte) error

type VMValueType

type VMValueType int
const (
	VMTypeInt            VMValueType = 0
	VMTypeFloat          VMValueType = 1
	VMTypeString         VMValueType = 2
	VMTypeNull           VMValueType = 4
	VMTypeComputedValue  VMValueType = 5
	VMTypeArray          VMValueType = 6
	VMTypeDict           VMValueType = 7
	VMTypeFunction       VMValueType = 8
	VMTypeNativeFunction VMValueType = 9
	VMTypeNativeObject   VMValueType = 10
)

type ValueMap

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

Map is like a Go map[interface{}]interface{} but is safe for concurrent use by multiple goroutines without additional locking or coordination. Loads, stores, and deletes run in amortized constant time.

The Map type is specialized. Most code should use a plain Go map instead, with separate locking or coordination, for better type safety and to make it easier to maintain other invariants along with the map content.

The Map type is optimized for two common use cases: (1) when the entry for a given key is only ever written once but read many times, as in caches that only grow, or (2) when multiple goroutines read, write, and overwrite entries for disjoint sets of keys. In these two cases, use of a Map may significantly reduce lock contention compared to a Go map paired with a separate Mutex or RWMutex.

The zero Map is empty and ready for use. A Map must not be copied after first use.

func (*ValueMap) Clear

func (m *ValueMap) Clear()

func (*ValueMap) Delete

func (m *ValueMap) Delete(key string)

Delete deletes the value for a key.

func (*ValueMap) Length

func (m *ValueMap) Length() int

func (*ValueMap) Load

func (m *ValueMap) Load(key string) (value *VMValue, ok bool)

Load returns the value stored in the map for a key, or nil if no value is present. The ok result indicates whether value was found in the map.

func (*ValueMap) LoadAndDelete

func (m *ValueMap) LoadAndDelete(key string) (value *VMValue, loaded bool)

LoadAndDelete deletes the value for a key, returning the previous value if any. The loaded result reports whether the key was present.

func (*ValueMap) LoadOrStore

func (m *ValueMap) LoadOrStore(key string, value *VMValue) (actual *VMValue, loaded bool)

LoadOrStore returns the existing value for the key if present. Otherwise, it stores and returns the given value. The loaded result is true if the value was loaded, false if stored.

func (*ValueMap) MustLoad

func (m *ValueMap) MustLoad(key string) *VMValue

func (*ValueMap) Range

func (m *ValueMap) Range(f func(key string, value *VMValue) bool)

Range calls f sequentially for each key and value present in the map. If f returns false, range stops the iteration.

Range does not necessarily correspond to any consistent snapshot of the Map's contents: no key will be visited more than once, but if the value for any key is stored or deleted concurrently (including by f), Range may reflect any mapping for that key from any point during the Range call. Range does not block other methods on the receiver; even f itself may call any method on m.

Range may be O(N) with the number of elements in the map even if f returns false after a constant number of calls.

func (*ValueMap) Store

func (m *ValueMap) Store(key string, value *VMValue)

Store sets the value for a key.

func (*ValueMap) ToJSON

func (m *ValueMap) ToJSON() ([]byte, error)

func (*ValueMap) UnmarshalJSON

func (m *ValueMap) UnmarshalJSON(input []byte) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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