2020-01-09 20:52:19 +00:00
|
|
|
package tengo
|
2019-02-23 15:39:44 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync/atomic"
|
|
|
|
|
2020-01-09 20:52:19 +00:00
|
|
|
"github.com/d5/tengo/v2/parser"
|
|
|
|
"github.com/d5/tengo/v2/token"
|
2019-02-23 15:39:44 +00:00
|
|
|
)
|
|
|
|
|
2020-01-09 20:52:19 +00:00
|
|
|
// frame represents a function call frame.
|
|
|
|
type frame struct {
|
|
|
|
fn *CompiledFunction
|
|
|
|
freeVars []*ObjectPtr
|
|
|
|
ip int
|
|
|
|
basePointer int
|
|
|
|
}
|
2019-02-23 15:39:44 +00:00
|
|
|
|
|
|
|
// VM is a virtual machine that executes the bytecode compiled by Compiler.
|
|
|
|
type VM struct {
|
2020-01-09 20:52:19 +00:00
|
|
|
constants []Object
|
|
|
|
stack [StackSize]Object
|
2019-04-06 20:18:25 +00:00
|
|
|
sp int
|
2020-01-09 20:52:19 +00:00
|
|
|
globals []Object
|
|
|
|
fileSet *parser.SourceFileSet
|
|
|
|
frames [MaxFrames]frame
|
2019-04-06 20:18:25 +00:00
|
|
|
framesIndex int
|
2020-01-09 20:52:19 +00:00
|
|
|
curFrame *frame
|
2019-04-06 20:18:25 +00:00
|
|
|
curInsts []byte
|
|
|
|
ip int
|
|
|
|
aborting int64
|
|
|
|
maxAllocs int64
|
|
|
|
allocs int64
|
|
|
|
err error
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewVM creates a VM.
|
2020-01-09 20:52:19 +00:00
|
|
|
func NewVM(
|
|
|
|
bytecode *Bytecode,
|
|
|
|
globals []Object,
|
|
|
|
maxAllocs int64,
|
|
|
|
) *VM {
|
2019-02-23 15:39:44 +00:00
|
|
|
if globals == nil {
|
2020-01-09 20:52:19 +00:00
|
|
|
globals = make([]Object, GlobalsSize)
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
2019-04-06 20:18:25 +00:00
|
|
|
v := &VM{
|
|
|
|
constants: bytecode.Constants,
|
|
|
|
sp: 0,
|
|
|
|
globals: globals,
|
|
|
|
fileSet: bytecode.FileSet,
|
|
|
|
framesIndex: 1,
|
|
|
|
ip: -1,
|
|
|
|
maxAllocs: maxAllocs,
|
2019-03-05 22:08:54 +00:00
|
|
|
}
|
2019-04-06 20:18:25 +00:00
|
|
|
v.frames[0].fn = bytecode.MainFunction
|
|
|
|
v.frames[0].ip = -1
|
|
|
|
v.curFrame = &v.frames[0]
|
|
|
|
v.curInsts = v.curFrame.fn.Instructions
|
|
|
|
return v
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Abort aborts the execution.
|
|
|
|
func (v *VM) Abort() {
|
|
|
|
atomic.StoreInt64(&v.aborting, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run starts the execution.
|
|
|
|
func (v *VM) Run() (err error) {
|
|
|
|
// reset VM states
|
|
|
|
v.sp = 0
|
|
|
|
v.curFrame = &(v.frames[0])
|
|
|
|
v.curInsts = v.curFrame.fn.Instructions
|
|
|
|
v.framesIndex = 1
|
|
|
|
v.ip = -1
|
2019-04-06 20:18:25 +00:00
|
|
|
v.allocs = v.maxAllocs + 1
|
2019-02-23 15:39:44 +00:00
|
|
|
|
2019-03-05 22:08:54 +00:00
|
|
|
v.run()
|
2019-04-06 20:18:25 +00:00
|
|
|
atomic.StoreInt64(&v.aborting, 0)
|
2019-03-05 22:08:54 +00:00
|
|
|
err = v.err
|
|
|
|
if err != nil {
|
2020-01-09 20:52:19 +00:00
|
|
|
filePos := v.fileSet.Position(
|
|
|
|
v.curFrame.fn.SourcePos(v.ip - 1))
|
2020-03-28 22:41:35 +00:00
|
|
|
err = fmt.Errorf("Runtime Error: %w\n\tat %s",
|
|
|
|
err, filePos)
|
2019-03-05 22:08:54 +00:00
|
|
|
for v.framesIndex > 1 {
|
|
|
|
v.framesIndex--
|
|
|
|
v.curFrame = &v.frames[v.framesIndex-1]
|
2020-01-09 20:52:19 +00:00
|
|
|
filePos = v.fileSet.Position(
|
|
|
|
v.curFrame.fn.SourcePos(v.curFrame.ip - 1))
|
2020-03-28 22:41:35 +00:00
|
|
|
err = fmt.Errorf("%w\n\tat %s", err, filePos)
|
2019-03-05 22:08:54 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2019-02-23 15:39:44 +00:00
|
|
|
|
2019-03-05 22:08:54 +00:00
|
|
|
func (v *VM) run() {
|
2019-04-06 20:18:25 +00:00
|
|
|
for atomic.LoadInt64(&v.aborting) == 0 {
|
2019-02-23 15:39:44 +00:00
|
|
|
v.ip++
|
|
|
|
|
|
|
|
switch v.curInsts[v.ip] {
|
2020-01-09 20:52:19 +00:00
|
|
|
case parser.OpConstant:
|
2019-02-23 15:39:44 +00:00
|
|
|
v.ip += 2
|
2019-04-06 20:18:25 +00:00
|
|
|
cidx := int(v.curInsts[v.ip]) | int(v.curInsts[v.ip-1])<<8
|
2019-02-23 15:39:44 +00:00
|
|
|
|
2019-04-06 20:18:25 +00:00
|
|
|
v.stack[v.sp] = v.constants[cidx]
|
2019-02-23 15:39:44 +00:00
|
|
|
v.sp++
|
2020-01-09 20:52:19 +00:00
|
|
|
case parser.OpNull:
|
|
|
|
v.stack[v.sp] = UndefinedValue
|
2019-02-23 15:39:44 +00:00
|
|
|
v.sp++
|
2020-01-09 20:52:19 +00:00
|
|
|
case parser.OpBinaryOp:
|
2019-04-06 20:18:25 +00:00
|
|
|
v.ip++
|
|
|
|
right := v.stack[v.sp-1]
|
|
|
|
left := v.stack[v.sp-2]
|
|
|
|
tok := token.Token(v.curInsts[v.ip])
|
|
|
|
res, e := left.BinaryOp(tok, right)
|
|
|
|
if e != nil {
|
|
|
|
v.sp -= 2
|
2020-01-09 20:52:19 +00:00
|
|
|
if e == ErrInvalidOperator {
|
2019-04-06 20:18:25 +00:00
|
|
|
v.err = fmt.Errorf("invalid operation: %s %s %s",
|
|
|
|
left.TypeName(), tok.String(), right.TypeName())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
v.err = e
|
|
|
|
return
|
|
|
|
}
|
2019-02-23 15:39:44 +00:00
|
|
|
|
2019-04-06 20:18:25 +00:00
|
|
|
v.allocs--
|
|
|
|
if v.allocs == 0 {
|
|
|
|
v.err = ErrObjectAllocLimit
|
|
|
|
return
|
|
|
|
}
|
2019-02-23 15:39:44 +00:00
|
|
|
|
2019-04-06 20:18:25 +00:00
|
|
|
v.stack[v.sp-2] = res
|
|
|
|
v.sp--
|
2020-01-09 20:52:19 +00:00
|
|
|
case parser.OpEqual:
|
2019-02-23 15:39:44 +00:00
|
|
|
right := v.stack[v.sp-1]
|
|
|
|
left := v.stack[v.sp-2]
|
|
|
|
v.sp -= 2
|
2019-04-06 20:18:25 +00:00
|
|
|
if left.Equals(right) {
|
2020-01-09 20:52:19 +00:00
|
|
|
v.stack[v.sp] = TrueValue
|
2019-02-23 15:39:44 +00:00
|
|
|
} else {
|
2020-01-09 20:52:19 +00:00
|
|
|
v.stack[v.sp] = FalseValue
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
|
|
|
v.sp++
|
2020-01-09 20:52:19 +00:00
|
|
|
case parser.OpNotEqual:
|
2019-02-23 15:39:44 +00:00
|
|
|
right := v.stack[v.sp-1]
|
|
|
|
left := v.stack[v.sp-2]
|
|
|
|
v.sp -= 2
|
2019-04-06 20:18:25 +00:00
|
|
|
if left.Equals(right) {
|
2020-01-09 20:52:19 +00:00
|
|
|
v.stack[v.sp] = FalseValue
|
2019-02-23 15:39:44 +00:00
|
|
|
} else {
|
2020-01-09 20:52:19 +00:00
|
|
|
v.stack[v.sp] = TrueValue
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
|
|
|
v.sp++
|
2020-01-09 20:52:19 +00:00
|
|
|
case parser.OpPop:
|
2019-02-23 15:39:44 +00:00
|
|
|
v.sp--
|
2020-01-09 20:52:19 +00:00
|
|
|
case parser.OpTrue:
|
|
|
|
v.stack[v.sp] = TrueValue
|
2019-02-23 15:39:44 +00:00
|
|
|
v.sp++
|
2020-01-09 20:52:19 +00:00
|
|
|
case parser.OpFalse:
|
|
|
|
v.stack[v.sp] = FalseValue
|
2019-02-23 15:39:44 +00:00
|
|
|
v.sp++
|
2020-01-09 20:52:19 +00:00
|
|
|
case parser.OpLNot:
|
2019-02-23 15:39:44 +00:00
|
|
|
operand := v.stack[v.sp-1]
|
|
|
|
v.sp--
|
2019-04-06 20:18:25 +00:00
|
|
|
if operand.IsFalsy() {
|
2020-01-09 20:52:19 +00:00
|
|
|
v.stack[v.sp] = TrueValue
|
2019-02-23 15:39:44 +00:00
|
|
|
} else {
|
2020-01-09 20:52:19 +00:00
|
|
|
v.stack[v.sp] = FalseValue
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
|
|
|
v.sp++
|
2020-01-09 20:52:19 +00:00
|
|
|
case parser.OpBComplement:
|
2019-02-23 15:39:44 +00:00
|
|
|
operand := v.stack[v.sp-1]
|
|
|
|
v.sp--
|
|
|
|
|
2019-04-06 20:18:25 +00:00
|
|
|
switch x := operand.(type) {
|
2020-01-09 20:52:19 +00:00
|
|
|
case *Int:
|
|
|
|
var res Object = &Int{Value: ^x.Value}
|
2019-04-06 20:18:25 +00:00
|
|
|
v.allocs--
|
|
|
|
if v.allocs == 0 {
|
|
|
|
v.err = ErrObjectAllocLimit
|
2019-03-05 22:08:54 +00:00
|
|
|
return
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
2019-04-06 20:18:25 +00:00
|
|
|
v.stack[v.sp] = res
|
2019-02-23 15:39:44 +00:00
|
|
|
v.sp++
|
|
|
|
default:
|
2020-01-09 20:52:19 +00:00
|
|
|
v.err = fmt.Errorf("invalid operation: ^%s",
|
|
|
|
operand.TypeName())
|
2019-03-05 22:08:54 +00:00
|
|
|
return
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
2020-01-09 20:52:19 +00:00
|
|
|
case parser.OpMinus:
|
2019-02-23 15:39:44 +00:00
|
|
|
operand := v.stack[v.sp-1]
|
|
|
|
v.sp--
|
|
|
|
|
2019-04-06 20:18:25 +00:00
|
|
|
switch x := operand.(type) {
|
2020-01-09 20:52:19 +00:00
|
|
|
case *Int:
|
|
|
|
var res Object = &Int{Value: -x.Value}
|
2019-04-06 20:18:25 +00:00
|
|
|
v.allocs--
|
|
|
|
if v.allocs == 0 {
|
|
|
|
v.err = ErrObjectAllocLimit
|
2019-03-05 22:08:54 +00:00
|
|
|
return
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
2019-04-06 20:18:25 +00:00
|
|
|
v.stack[v.sp] = res
|
2019-02-23 15:39:44 +00:00
|
|
|
v.sp++
|
2020-01-09 20:52:19 +00:00
|
|
|
case *Float:
|
|
|
|
var res Object = &Float{Value: -x.Value}
|
2019-04-06 20:18:25 +00:00
|
|
|
v.allocs--
|
|
|
|
if v.allocs == 0 {
|
|
|
|
v.err = ErrObjectAllocLimit
|
2019-03-05 22:08:54 +00:00
|
|
|
return
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
2019-04-06 20:18:25 +00:00
|
|
|
v.stack[v.sp] = res
|
2019-02-23 15:39:44 +00:00
|
|
|
v.sp++
|
|
|
|
default:
|
2020-01-09 20:52:19 +00:00
|
|
|
v.err = fmt.Errorf("invalid operation: -%s",
|
|
|
|
operand.TypeName())
|
2019-03-05 22:08:54 +00:00
|
|
|
return
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
2020-01-09 20:52:19 +00:00
|
|
|
case parser.OpJumpFalsy:
|
2019-02-23 15:39:44 +00:00
|
|
|
v.ip += 2
|
|
|
|
v.sp--
|
2019-04-06 20:18:25 +00:00
|
|
|
if v.stack[v.sp].IsFalsy() {
|
|
|
|
pos := int(v.curInsts[v.ip]) | int(v.curInsts[v.ip-1])<<8
|
2019-02-23 15:39:44 +00:00
|
|
|
v.ip = pos - 1
|
|
|
|
}
|
2020-01-09 20:52:19 +00:00
|
|
|
case parser.OpAndJump:
|
2019-02-23 15:39:44 +00:00
|
|
|
v.ip += 2
|
2019-04-06 20:18:25 +00:00
|
|
|
if v.stack[v.sp-1].IsFalsy() {
|
|
|
|
pos := int(v.curInsts[v.ip]) | int(v.curInsts[v.ip-1])<<8
|
2019-02-23 15:39:44 +00:00
|
|
|
v.ip = pos - 1
|
|
|
|
} else {
|
|
|
|
v.sp--
|
|
|
|
}
|
2020-01-09 20:52:19 +00:00
|
|
|
case parser.OpOrJump:
|
2019-02-23 15:39:44 +00:00
|
|
|
v.ip += 2
|
2019-04-06 20:18:25 +00:00
|
|
|
if v.stack[v.sp-1].IsFalsy() {
|
2019-02-23 15:39:44 +00:00
|
|
|
v.sp--
|
2019-04-06 20:18:25 +00:00
|
|
|
} else {
|
|
|
|
pos := int(v.curInsts[v.ip]) | int(v.curInsts[v.ip-1])<<8
|
|
|
|
v.ip = pos - 1
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
2020-01-09 20:52:19 +00:00
|
|
|
case parser.OpJump:
|
2019-02-23 15:39:44 +00:00
|
|
|
pos := int(v.curInsts[v.ip+2]) | int(v.curInsts[v.ip+1])<<8
|
|
|
|
v.ip = pos - 1
|
2020-01-09 20:52:19 +00:00
|
|
|
case parser.OpSetGlobal:
|
2019-02-23 15:39:44 +00:00
|
|
|
v.ip += 2
|
|
|
|
v.sp--
|
2019-04-06 20:18:25 +00:00
|
|
|
globalIndex := int(v.curInsts[v.ip]) | int(v.curInsts[v.ip-1])<<8
|
2019-02-23 15:39:44 +00:00
|
|
|
v.globals[globalIndex] = v.stack[v.sp]
|
2020-01-09 20:52:19 +00:00
|
|
|
case parser.OpSetSelGlobal:
|
2019-02-23 15:39:44 +00:00
|
|
|
v.ip += 3
|
2019-04-06 20:18:25 +00:00
|
|
|
globalIndex := int(v.curInsts[v.ip-1]) | int(v.curInsts[v.ip-2])<<8
|
|
|
|
numSelectors := int(v.curInsts[v.ip])
|
2019-02-23 15:39:44 +00:00
|
|
|
|
|
|
|
// selectors and RHS value
|
2020-01-09 20:52:19 +00:00
|
|
|
selectors := make([]Object, numSelectors)
|
2019-04-06 20:18:25 +00:00
|
|
|
for i := 0; i < numSelectors; i++ {
|
|
|
|
selectors[i] = v.stack[v.sp-numSelectors+i]
|
|
|
|
}
|
2019-02-23 15:39:44 +00:00
|
|
|
val := v.stack[v.sp-numSelectors-1]
|
|
|
|
v.sp -= numSelectors + 1
|
2020-01-09 20:52:19 +00:00
|
|
|
e := indexAssign(v.globals[globalIndex], val, selectors)
|
|
|
|
if e != nil {
|
2019-03-05 22:08:54 +00:00
|
|
|
v.err = e
|
|
|
|
return
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
2020-01-09 20:52:19 +00:00
|
|
|
case parser.OpGetGlobal:
|
2019-02-23 15:39:44 +00:00
|
|
|
v.ip += 2
|
2019-04-06 20:18:25 +00:00
|
|
|
globalIndex := int(v.curInsts[v.ip]) | int(v.curInsts[v.ip-1])<<8
|
2019-02-23 15:39:44 +00:00
|
|
|
val := v.globals[globalIndex]
|
|
|
|
v.stack[v.sp] = val
|
|
|
|
v.sp++
|
2020-01-09 20:52:19 +00:00
|
|
|
case parser.OpArray:
|
2019-02-23 15:39:44 +00:00
|
|
|
v.ip += 2
|
2019-04-06 20:18:25 +00:00
|
|
|
numElements := int(v.curInsts[v.ip]) | int(v.curInsts[v.ip-1])<<8
|
2019-02-23 15:39:44 +00:00
|
|
|
|
2020-01-09 20:52:19 +00:00
|
|
|
var elements []Object
|
2019-02-23 15:39:44 +00:00
|
|
|
for i := v.sp - numElements; i < v.sp; i++ {
|
2019-04-06 20:18:25 +00:00
|
|
|
elements = append(elements, v.stack[i])
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
|
|
|
v.sp -= numElements
|
|
|
|
|
2020-01-09 20:52:19 +00:00
|
|
|
var arr Object = &Array{Value: elements}
|
2019-04-06 20:18:25 +00:00
|
|
|
v.allocs--
|
|
|
|
if v.allocs == 0 {
|
|
|
|
v.err = ErrObjectAllocLimit
|
2019-03-05 22:08:54 +00:00
|
|
|
return
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
|
|
|
|
2019-04-06 20:18:25 +00:00
|
|
|
v.stack[v.sp] = arr
|
2019-02-23 15:39:44 +00:00
|
|
|
v.sp++
|
2020-01-09 20:52:19 +00:00
|
|
|
case parser.OpMap:
|
2019-02-23 15:39:44 +00:00
|
|
|
v.ip += 2
|
2019-04-06 20:18:25 +00:00
|
|
|
numElements := int(v.curInsts[v.ip]) | int(v.curInsts[v.ip-1])<<8
|
2020-01-09 20:52:19 +00:00
|
|
|
kv := make(map[string]Object)
|
2019-02-23 15:39:44 +00:00
|
|
|
for i := v.sp - numElements; i < v.sp; i += 2 {
|
2019-04-06 20:18:25 +00:00
|
|
|
key := v.stack[i]
|
|
|
|
value := v.stack[i+1]
|
2020-01-09 20:52:19 +00:00
|
|
|
kv[key.(*String).Value] = value
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
|
|
|
v.sp -= numElements
|
|
|
|
|
2020-01-09 20:52:19 +00:00
|
|
|
var m Object = &Map{Value: kv}
|
2019-04-06 20:18:25 +00:00
|
|
|
v.allocs--
|
|
|
|
if v.allocs == 0 {
|
|
|
|
v.err = ErrObjectAllocLimit
|
2019-03-05 22:08:54 +00:00
|
|
|
return
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
2019-04-06 20:18:25 +00:00
|
|
|
v.stack[v.sp] = m
|
2019-02-23 15:39:44 +00:00
|
|
|
v.sp++
|
2020-01-09 20:52:19 +00:00
|
|
|
case parser.OpError:
|
2019-02-23 15:39:44 +00:00
|
|
|
value := v.stack[v.sp-1]
|
2020-01-09 20:52:19 +00:00
|
|
|
var e Object = &Error{
|
2019-04-06 20:18:25 +00:00
|
|
|
Value: value,
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
2019-04-06 20:18:25 +00:00
|
|
|
v.allocs--
|
|
|
|
if v.allocs == 0 {
|
|
|
|
v.err = ErrObjectAllocLimit
|
|
|
|
return
|
|
|
|
}
|
|
|
|
v.stack[v.sp-1] = e
|
2020-01-09 20:52:19 +00:00
|
|
|
case parser.OpImmutable:
|
2019-02-23 15:39:44 +00:00
|
|
|
value := v.stack[v.sp-1]
|
2019-04-06 20:18:25 +00:00
|
|
|
switch value := value.(type) {
|
2020-01-09 20:52:19 +00:00
|
|
|
case *Array:
|
|
|
|
var immutableArray Object = &ImmutableArray{
|
2019-02-23 15:39:44 +00:00
|
|
|
Value: value.Value,
|
|
|
|
}
|
2019-04-06 20:18:25 +00:00
|
|
|
v.allocs--
|
|
|
|
if v.allocs == 0 {
|
|
|
|
v.err = ErrObjectAllocLimit
|
|
|
|
return
|
|
|
|
}
|
|
|
|
v.stack[v.sp-1] = immutableArray
|
2020-01-09 20:52:19 +00:00
|
|
|
case *Map:
|
|
|
|
var immutableMap Object = &ImmutableMap{
|
2019-02-23 15:39:44 +00:00
|
|
|
Value: value.Value,
|
|
|
|
}
|
2019-04-06 20:18:25 +00:00
|
|
|
v.allocs--
|
|
|
|
if v.allocs == 0 {
|
|
|
|
v.err = ErrObjectAllocLimit
|
|
|
|
return
|
|
|
|
}
|
|
|
|
v.stack[v.sp-1] = immutableMap
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
2020-01-09 20:52:19 +00:00
|
|
|
case parser.OpIndex:
|
2019-02-23 15:39:44 +00:00
|
|
|
index := v.stack[v.sp-1]
|
|
|
|
left := v.stack[v.sp-2]
|
|
|
|
v.sp -= 2
|
|
|
|
|
2020-01-09 20:52:19 +00:00
|
|
|
val, err := left.IndexGet(index)
|
|
|
|
if err != nil {
|
|
|
|
if err == ErrNotIndexable {
|
|
|
|
v.err = fmt.Errorf("not indexable: %s", index.TypeName())
|
2019-03-05 22:08:54 +00:00
|
|
|
return
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
2020-01-09 20:52:19 +00:00
|
|
|
if err == ErrInvalidIndexType {
|
|
|
|
v.err = fmt.Errorf("invalid index type: %s",
|
|
|
|
index.TypeName())
|
2019-03-05 22:08:54 +00:00
|
|
|
return
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
2020-01-09 20:52:19 +00:00
|
|
|
v.err = err
|
2019-03-05 22:08:54 +00:00
|
|
|
return
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
2020-01-09 20:52:19 +00:00
|
|
|
if val == nil {
|
|
|
|
val = UndefinedValue
|
|
|
|
}
|
|
|
|
v.stack[v.sp] = val
|
|
|
|
v.sp++
|
|
|
|
case parser.OpSliceIndex:
|
2019-02-23 15:39:44 +00:00
|
|
|
high := v.stack[v.sp-1]
|
|
|
|
low := v.stack[v.sp-2]
|
|
|
|
left := v.stack[v.sp-3]
|
|
|
|
v.sp -= 3
|
|
|
|
|
|
|
|
var lowIdx int64
|
2020-01-09 20:52:19 +00:00
|
|
|
if low != UndefinedValue {
|
|
|
|
if low, ok := low.(*Int); ok {
|
2019-02-23 15:39:44 +00:00
|
|
|
lowIdx = low.Value
|
|
|
|
} else {
|
2020-01-09 20:52:19 +00:00
|
|
|
v.err = fmt.Errorf("invalid slice index type: %s",
|
|
|
|
low.TypeName())
|
2019-03-05 22:08:54 +00:00
|
|
|
return
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-06 20:18:25 +00:00
|
|
|
switch left := left.(type) {
|
2020-01-09 20:52:19 +00:00
|
|
|
case *Array:
|
2019-02-23 15:39:44 +00:00
|
|
|
numElements := int64(len(left.Value))
|
|
|
|
var highIdx int64
|
2020-01-09 20:52:19 +00:00
|
|
|
if high == UndefinedValue {
|
2019-02-23 15:39:44 +00:00
|
|
|
highIdx = numElements
|
2020-01-09 20:52:19 +00:00
|
|
|
} else if high, ok := high.(*Int); ok {
|
2019-02-23 15:39:44 +00:00
|
|
|
highIdx = high.Value
|
|
|
|
} else {
|
2020-01-09 20:52:19 +00:00
|
|
|
v.err = fmt.Errorf("invalid slice index type: %s",
|
|
|
|
high.TypeName())
|
2019-03-05 22:08:54 +00:00
|
|
|
return
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
|
|
|
if lowIdx > highIdx {
|
2020-01-09 20:52:19 +00:00
|
|
|
v.err = fmt.Errorf("invalid slice index: %d > %d",
|
|
|
|
lowIdx, highIdx)
|
2019-03-05 22:08:54 +00:00
|
|
|
return
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
|
|
|
if lowIdx < 0 {
|
|
|
|
lowIdx = 0
|
|
|
|
} else if lowIdx > numElements {
|
|
|
|
lowIdx = numElements
|
|
|
|
}
|
|
|
|
if highIdx < 0 {
|
|
|
|
highIdx = 0
|
|
|
|
} else if highIdx > numElements {
|
|
|
|
highIdx = numElements
|
|
|
|
}
|
2020-01-09 20:52:19 +00:00
|
|
|
var val Object = &Array{
|
|
|
|
Value: left.Value[lowIdx:highIdx],
|
|
|
|
}
|
2019-04-06 20:18:25 +00:00
|
|
|
v.allocs--
|
|
|
|
if v.allocs == 0 {
|
|
|
|
v.err = ErrObjectAllocLimit
|
2019-03-05 22:08:54 +00:00
|
|
|
return
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
2019-04-06 20:18:25 +00:00
|
|
|
v.stack[v.sp] = val
|
2019-02-23 15:39:44 +00:00
|
|
|
v.sp++
|
2020-01-09 20:52:19 +00:00
|
|
|
case *ImmutableArray:
|
2019-02-23 15:39:44 +00:00
|
|
|
numElements := int64(len(left.Value))
|
|
|
|
var highIdx int64
|
2020-01-09 20:52:19 +00:00
|
|
|
if high == UndefinedValue {
|
2019-02-23 15:39:44 +00:00
|
|
|
highIdx = numElements
|
2020-01-09 20:52:19 +00:00
|
|
|
} else if high, ok := high.(*Int); ok {
|
2019-02-23 15:39:44 +00:00
|
|
|
highIdx = high.Value
|
|
|
|
} else {
|
2020-01-09 20:52:19 +00:00
|
|
|
v.err = fmt.Errorf("invalid slice index type: %s",
|
|
|
|
high.TypeName())
|
2019-03-05 22:08:54 +00:00
|
|
|
return
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
|
|
|
if lowIdx > highIdx {
|
2020-01-09 20:52:19 +00:00
|
|
|
v.err = fmt.Errorf("invalid slice index: %d > %d",
|
|
|
|
lowIdx, highIdx)
|
2019-03-05 22:08:54 +00:00
|
|
|
return
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
|
|
|
if lowIdx < 0 {
|
|
|
|
lowIdx = 0
|
|
|
|
} else if lowIdx > numElements {
|
|
|
|
lowIdx = numElements
|
|
|
|
}
|
|
|
|
if highIdx < 0 {
|
|
|
|
highIdx = 0
|
|
|
|
} else if highIdx > numElements {
|
|
|
|
highIdx = numElements
|
|
|
|
}
|
2020-01-09 20:52:19 +00:00
|
|
|
var val Object = &Array{
|
|
|
|
Value: left.Value[lowIdx:highIdx],
|
|
|
|
}
|
2019-04-06 20:18:25 +00:00
|
|
|
v.allocs--
|
|
|
|
if v.allocs == 0 {
|
|
|
|
v.err = ErrObjectAllocLimit
|
2019-03-05 22:08:54 +00:00
|
|
|
return
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
2019-04-06 20:18:25 +00:00
|
|
|
v.stack[v.sp] = val
|
2019-02-23 15:39:44 +00:00
|
|
|
v.sp++
|
2020-01-09 20:52:19 +00:00
|
|
|
case *String:
|
2019-02-23 15:39:44 +00:00
|
|
|
numElements := int64(len(left.Value))
|
|
|
|
var highIdx int64
|
2020-01-09 20:52:19 +00:00
|
|
|
if high == UndefinedValue {
|
2019-02-23 15:39:44 +00:00
|
|
|
highIdx = numElements
|
2020-01-09 20:52:19 +00:00
|
|
|
} else if high, ok := high.(*Int); ok {
|
2019-02-23 15:39:44 +00:00
|
|
|
highIdx = high.Value
|
|
|
|
} else {
|
2020-01-09 20:52:19 +00:00
|
|
|
v.err = fmt.Errorf("invalid slice index type: %s",
|
|
|
|
high.TypeName())
|
2019-03-05 22:08:54 +00:00
|
|
|
return
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
|
|
|
if lowIdx > highIdx {
|
2020-01-09 20:52:19 +00:00
|
|
|
v.err = fmt.Errorf("invalid slice index: %d > %d",
|
|
|
|
lowIdx, highIdx)
|
2019-03-05 22:08:54 +00:00
|
|
|
return
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
|
|
|
if lowIdx < 0 {
|
|
|
|
lowIdx = 0
|
|
|
|
} else if lowIdx > numElements {
|
|
|
|
lowIdx = numElements
|
|
|
|
}
|
|
|
|
if highIdx < 0 {
|
|
|
|
highIdx = 0
|
|
|
|
} else if highIdx > numElements {
|
|
|
|
highIdx = numElements
|
|
|
|
}
|
2020-01-09 20:52:19 +00:00
|
|
|
var val Object = &String{
|
|
|
|
Value: left.Value[lowIdx:highIdx],
|
|
|
|
}
|
2019-04-06 20:18:25 +00:00
|
|
|
v.allocs--
|
|
|
|
if v.allocs == 0 {
|
|
|
|
v.err = ErrObjectAllocLimit
|
2019-03-05 22:08:54 +00:00
|
|
|
return
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
2019-04-06 20:18:25 +00:00
|
|
|
v.stack[v.sp] = val
|
2019-02-23 15:39:44 +00:00
|
|
|
v.sp++
|
2020-01-09 20:52:19 +00:00
|
|
|
case *Bytes:
|
2019-02-23 15:39:44 +00:00
|
|
|
numElements := int64(len(left.Value))
|
|
|
|
var highIdx int64
|
2020-01-09 20:52:19 +00:00
|
|
|
if high == UndefinedValue {
|
2019-02-23 15:39:44 +00:00
|
|
|
highIdx = numElements
|
2020-01-09 20:52:19 +00:00
|
|
|
} else if high, ok := high.(*Int); ok {
|
2019-02-23 15:39:44 +00:00
|
|
|
highIdx = high.Value
|
|
|
|
} else {
|
2020-01-09 20:52:19 +00:00
|
|
|
v.err = fmt.Errorf("invalid slice index type: %s",
|
|
|
|
high.TypeName())
|
2019-03-05 22:08:54 +00:00
|
|
|
return
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
|
|
|
if lowIdx > highIdx {
|
2020-01-09 20:52:19 +00:00
|
|
|
v.err = fmt.Errorf("invalid slice index: %d > %d",
|
|
|
|
lowIdx, highIdx)
|
2019-03-05 22:08:54 +00:00
|
|
|
return
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
|
|
|
if lowIdx < 0 {
|
|
|
|
lowIdx = 0
|
|
|
|
} else if lowIdx > numElements {
|
|
|
|
lowIdx = numElements
|
|
|
|
}
|
|
|
|
if highIdx < 0 {
|
|
|
|
highIdx = 0
|
|
|
|
} else if highIdx > numElements {
|
|
|
|
highIdx = numElements
|
|
|
|
}
|
2020-01-09 20:52:19 +00:00
|
|
|
var val Object = &Bytes{
|
|
|
|
Value: left.Value[lowIdx:highIdx],
|
|
|
|
}
|
2019-04-06 20:18:25 +00:00
|
|
|
v.allocs--
|
|
|
|
if v.allocs == 0 {
|
|
|
|
v.err = ErrObjectAllocLimit
|
2019-03-05 22:08:54 +00:00
|
|
|
return
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
2019-04-06 20:18:25 +00:00
|
|
|
v.stack[v.sp] = val
|
2019-02-23 15:39:44 +00:00
|
|
|
v.sp++
|
|
|
|
}
|
2020-01-09 20:52:19 +00:00
|
|
|
case parser.OpCall:
|
2019-02-23 15:39:44 +00:00
|
|
|
numArgs := int(v.curInsts[v.ip+1])
|
2020-07-18 15:27:41 +00:00
|
|
|
spread := int(v.curInsts[v.ip+2])
|
|
|
|
v.ip += 2
|
|
|
|
|
2019-04-06 20:18:25 +00:00
|
|
|
value := v.stack[v.sp-1-numArgs]
|
2020-01-09 20:52:19 +00:00
|
|
|
if !value.CanCall() {
|
|
|
|
v.err = fmt.Errorf("not callable: %s", value.TypeName())
|
|
|
|
return
|
|
|
|
}
|
2020-07-18 15:27:41 +00:00
|
|
|
|
|
|
|
if spread == 1 {
|
|
|
|
v.sp--
|
|
|
|
switch arr := v.stack[v.sp].(type) {
|
|
|
|
case *Array:
|
|
|
|
for _, item := range arr.Value {
|
|
|
|
v.stack[v.sp] = item
|
|
|
|
v.sp++
|
|
|
|
}
|
|
|
|
numArgs += len(arr.Value) - 1
|
|
|
|
case *ImmutableArray:
|
|
|
|
for _, item := range arr.Value {
|
|
|
|
v.stack[v.sp] = item
|
|
|
|
v.sp++
|
|
|
|
}
|
|
|
|
numArgs += len(arr.Value) - 1
|
|
|
|
default:
|
|
|
|
v.err = fmt.Errorf("not an array: %s", arr.TypeName())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-09 20:52:19 +00:00
|
|
|
if callee, ok := value.(*CompiledFunction); ok {
|
2019-06-16 21:33:25 +00:00
|
|
|
if callee.VarArgs {
|
|
|
|
// if the closure is variadic,
|
|
|
|
// roll up all variadic parameters into an array
|
|
|
|
realArgs := callee.NumParameters - 1
|
|
|
|
varArgs := numArgs - realArgs
|
|
|
|
if varArgs >= 0 {
|
|
|
|
numArgs = realArgs + 1
|
2020-01-09 20:52:19 +00:00
|
|
|
args := make([]Object, varArgs)
|
2019-06-16 21:33:25 +00:00
|
|
|
spStart := v.sp - varArgs
|
|
|
|
for i := spStart; i < v.sp; i++ {
|
|
|
|
args[i-spStart] = v.stack[i]
|
|
|
|
}
|
2020-01-09 20:52:19 +00:00
|
|
|
v.stack[spStart] = &Array{Value: args}
|
2019-06-16 21:33:25 +00:00
|
|
|
v.sp = spStart + 1
|
|
|
|
}
|
|
|
|
}
|
2019-02-23 15:39:44 +00:00
|
|
|
if numArgs != callee.NumParameters {
|
2019-06-16 21:33:25 +00:00
|
|
|
if callee.VarArgs {
|
2020-01-09 20:52:19 +00:00
|
|
|
v.err = fmt.Errorf(
|
|
|
|
"wrong number of arguments: want>=%d, got=%d",
|
2019-06-16 21:33:25 +00:00
|
|
|
callee.NumParameters-1, numArgs)
|
|
|
|
} else {
|
2020-01-09 20:52:19 +00:00
|
|
|
v.err = fmt.Errorf(
|
|
|
|
"wrong number of arguments: want=%d, got=%d",
|
2019-06-16 21:33:25 +00:00
|
|
|
callee.NumParameters, numArgs)
|
|
|
|
}
|
2019-03-05 22:08:54 +00:00
|
|
|
return
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// test if it's tail-call
|
|
|
|
if callee == v.curFrame.fn { // recursion
|
|
|
|
nextOp := v.curInsts[v.ip+1]
|
2020-01-09 20:52:19 +00:00
|
|
|
if nextOp == parser.OpReturn ||
|
|
|
|
(nextOp == parser.OpPop &&
|
|
|
|
parser.OpReturn == v.curInsts[v.ip+2]) {
|
2019-02-23 15:39:44 +00:00
|
|
|
for p := 0; p < numArgs; p++ {
|
2020-01-09 20:52:19 +00:00
|
|
|
v.stack[v.curFrame.basePointer+p] =
|
|
|
|
v.stack[v.sp-numArgs+p]
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
|
|
|
v.sp -= numArgs + 1
|
|
|
|
v.ip = -1 // reset IP to beginning of the frame
|
2019-04-06 20:18:25 +00:00
|
|
|
continue
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-09 20:52:19 +00:00
|
|
|
if v.framesIndex >= MaxFrames {
|
|
|
|
v.err = ErrStackOverflow
|
|
|
|
return
|
|
|
|
}
|
2019-02-23 15:39:44 +00:00
|
|
|
|
|
|
|
// update call frame
|
|
|
|
v.curFrame.ip = v.ip // store current ip before call
|
|
|
|
v.curFrame = &(v.frames[v.framesIndex])
|
|
|
|
v.curFrame.fn = callee
|
2020-01-09 20:52:19 +00:00
|
|
|
v.curFrame.freeVars = callee.Free
|
2019-02-23 15:39:44 +00:00
|
|
|
v.curFrame.basePointer = v.sp - numArgs
|
|
|
|
v.curInsts = callee.Instructions
|
|
|
|
v.ip = -1
|
|
|
|
v.framesIndex++
|
|
|
|
v.sp = v.sp - numArgs + callee.NumLocals
|
2020-01-09 20:52:19 +00:00
|
|
|
} else {
|
|
|
|
var args []Object
|
2019-06-16 21:33:25 +00:00
|
|
|
args = append(args, v.stack[v.sp-numArgs:v.sp]...)
|
2020-01-09 20:52:19 +00:00
|
|
|
ret, e := value.Call(args...)
|
2019-02-23 15:39:44 +00:00
|
|
|
v.sp -= numArgs + 1
|
|
|
|
|
|
|
|
// runtime error
|
|
|
|
if e != nil {
|
2020-01-09 20:52:19 +00:00
|
|
|
if e == ErrWrongNumArguments {
|
|
|
|
v.err = fmt.Errorf(
|
|
|
|
"wrong number of arguments in call to '%s'",
|
2019-02-23 15:39:44 +00:00
|
|
|
value.TypeName())
|
2019-03-05 22:08:54 +00:00
|
|
|
return
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
2020-01-09 20:52:19 +00:00
|
|
|
if e, ok := e.(ErrInvalidArgumentType); ok {
|
|
|
|
v.err = fmt.Errorf(
|
|
|
|
"invalid type for argument '%s' in call to '%s': "+
|
|
|
|
"expected %s, found %s",
|
2019-02-23 15:39:44 +00:00
|
|
|
e.Name, value.TypeName(), e.Expected, e.Found)
|
2019-03-05 22:08:54 +00:00
|
|
|
return
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
2019-03-05 22:08:54 +00:00
|
|
|
v.err = e
|
|
|
|
return
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// nil return -> undefined
|
|
|
|
if ret == nil {
|
2020-01-09 20:52:19 +00:00
|
|
|
ret = UndefinedValue
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
2019-04-06 20:18:25 +00:00
|
|
|
v.allocs--
|
|
|
|
if v.allocs == 0 {
|
|
|
|
v.err = ErrObjectAllocLimit
|
2019-03-05 22:08:54 +00:00
|
|
|
return
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
2019-04-06 20:18:25 +00:00
|
|
|
v.stack[v.sp] = ret
|
2019-02-23 15:39:44 +00:00
|
|
|
v.sp++
|
|
|
|
}
|
2020-01-09 20:52:19 +00:00
|
|
|
case parser.OpReturn:
|
2019-04-06 20:18:25 +00:00
|
|
|
v.ip++
|
2020-01-09 20:52:19 +00:00
|
|
|
var retVal Object
|
2019-04-06 20:18:25 +00:00
|
|
|
if int(v.curInsts[v.ip]) == 1 {
|
|
|
|
retVal = v.stack[v.sp-1]
|
|
|
|
} else {
|
2020-01-09 20:52:19 +00:00
|
|
|
retVal = UndefinedValue
|
2019-04-06 20:18:25 +00:00
|
|
|
}
|
2019-02-23 15:39:44 +00:00
|
|
|
//v.sp--
|
|
|
|
v.framesIndex--
|
|
|
|
v.curFrame = &v.frames[v.framesIndex-1]
|
|
|
|
v.curInsts = v.curFrame.fn.Instructions
|
|
|
|
v.ip = v.curFrame.ip
|
|
|
|
//v.sp = lastFrame.basePointer - 1
|
2019-04-06 20:18:25 +00:00
|
|
|
v.sp = v.frames[v.framesIndex].basePointer
|
2019-02-23 15:39:44 +00:00
|
|
|
// skip stack overflow check because (newSP) <= (oldSP)
|
|
|
|
v.stack[v.sp-1] = retVal
|
|
|
|
//v.sp++
|
2020-01-09 20:52:19 +00:00
|
|
|
case parser.OpDefineLocal:
|
2019-02-23 15:39:44 +00:00
|
|
|
v.ip++
|
2019-04-06 20:18:25 +00:00
|
|
|
localIndex := int(v.curInsts[v.ip])
|
2019-02-23 15:39:44 +00:00
|
|
|
sp := v.curFrame.basePointer + localIndex
|
|
|
|
|
|
|
|
// local variables can be mutated by other actions
|
|
|
|
// so always store the copy of popped value
|
2019-04-06 20:18:25 +00:00
|
|
|
val := v.stack[v.sp-1]
|
2019-02-23 15:39:44 +00:00
|
|
|
v.sp--
|
2019-04-06 20:18:25 +00:00
|
|
|
v.stack[sp] = val
|
2020-01-09 20:52:19 +00:00
|
|
|
case parser.OpSetLocal:
|
2019-02-23 15:39:44 +00:00
|
|
|
localIndex := int(v.curInsts[v.ip+1])
|
|
|
|
v.ip++
|
|
|
|
sp := v.curFrame.basePointer + localIndex
|
|
|
|
|
2020-01-09 20:52:19 +00:00
|
|
|
// update pointee of v.stack[sp] instead of replacing the pointer
|
|
|
|
// itself. this is needed because there can be free variables
|
|
|
|
// referencing the same local variables.
|
2019-02-23 15:39:44 +00:00
|
|
|
val := v.stack[v.sp-1]
|
|
|
|
v.sp--
|
2020-01-09 20:52:19 +00:00
|
|
|
if obj, ok := v.stack[sp].(*ObjectPtr); ok {
|
2019-04-06 20:18:25 +00:00
|
|
|
*obj.Value = val
|
|
|
|
val = obj
|
|
|
|
}
|
|
|
|
v.stack[sp] = val // also use a copy of popped value
|
2020-01-09 20:52:19 +00:00
|
|
|
case parser.OpSetSelLocal:
|
2019-02-23 15:39:44 +00:00
|
|
|
localIndex := int(v.curInsts[v.ip+1])
|
|
|
|
numSelectors := int(v.curInsts[v.ip+2])
|
|
|
|
v.ip += 2
|
|
|
|
|
|
|
|
// selectors and RHS value
|
2020-01-09 20:52:19 +00:00
|
|
|
selectors := make([]Object, numSelectors)
|
2019-04-06 20:18:25 +00:00
|
|
|
for i := 0; i < numSelectors; i++ {
|
|
|
|
selectors[i] = v.stack[v.sp-numSelectors+i]
|
|
|
|
}
|
2019-02-23 15:39:44 +00:00
|
|
|
val := v.stack[v.sp-numSelectors-1]
|
|
|
|
v.sp -= numSelectors + 1
|
2019-06-16 21:33:25 +00:00
|
|
|
dst := v.stack[v.curFrame.basePointer+localIndex]
|
2020-01-09 20:52:19 +00:00
|
|
|
if obj, ok := dst.(*ObjectPtr); ok {
|
2019-06-16 21:33:25 +00:00
|
|
|
dst = *obj.Value
|
|
|
|
}
|
|
|
|
if e := indexAssign(dst, val, selectors); e != nil {
|
2019-03-05 22:08:54 +00:00
|
|
|
v.err = e
|
|
|
|
return
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
2020-01-09 20:52:19 +00:00
|
|
|
case parser.OpGetLocal:
|
2019-02-23 15:39:44 +00:00
|
|
|
v.ip++
|
2019-04-06 20:18:25 +00:00
|
|
|
localIndex := int(v.curInsts[v.ip])
|
2019-02-23 15:39:44 +00:00
|
|
|
val := v.stack[v.curFrame.basePointer+localIndex]
|
2020-01-09 20:52:19 +00:00
|
|
|
if obj, ok := val.(*ObjectPtr); ok {
|
2019-04-06 20:18:25 +00:00
|
|
|
val = *obj.Value
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
|
|
|
v.stack[v.sp] = val
|
|
|
|
v.sp++
|
2020-01-09 20:52:19 +00:00
|
|
|
case parser.OpGetBuiltin:
|
2019-02-23 15:39:44 +00:00
|
|
|
v.ip++
|
2019-04-06 20:18:25 +00:00
|
|
|
builtinIndex := int(v.curInsts[v.ip])
|
2020-01-09 20:52:19 +00:00
|
|
|
v.stack[v.sp] = builtinFuncs[builtinIndex]
|
2019-02-23 15:39:44 +00:00
|
|
|
v.sp++
|
2020-01-09 20:52:19 +00:00
|
|
|
case parser.OpClosure:
|
2019-02-23 15:39:44 +00:00
|
|
|
v.ip += 3
|
2019-04-06 20:18:25 +00:00
|
|
|
constIndex := int(v.curInsts[v.ip-1]) | int(v.curInsts[v.ip-2])<<8
|
|
|
|
numFree := int(v.curInsts[v.ip])
|
2020-01-09 20:52:19 +00:00
|
|
|
fn, ok := v.constants[constIndex].(*CompiledFunction)
|
2019-02-23 15:39:44 +00:00
|
|
|
if !ok {
|
2019-03-05 22:08:54 +00:00
|
|
|
v.err = fmt.Errorf("not function: %s", fn.TypeName())
|
|
|
|
return
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
2020-01-09 20:52:19 +00:00
|
|
|
free := make([]*ObjectPtr, numFree)
|
2019-02-23 15:39:44 +00:00
|
|
|
for i := 0; i < numFree; i++ {
|
2019-04-06 20:18:25 +00:00
|
|
|
switch freeVar := (v.stack[v.sp-numFree+i]).(type) {
|
2020-01-09 20:52:19 +00:00
|
|
|
case *ObjectPtr:
|
2019-04-06 20:18:25 +00:00
|
|
|
free[i] = freeVar
|
|
|
|
default:
|
2020-01-09 20:52:19 +00:00
|
|
|
free[i] = &ObjectPtr{
|
|
|
|
Value: &v.stack[v.sp-numFree+i],
|
|
|
|
}
|
2019-04-06 20:18:25 +00:00
|
|
|
}
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
2019-04-06 20:18:25 +00:00
|
|
|
v.sp -= numFree
|
2020-01-09 20:52:19 +00:00
|
|
|
cl := &CompiledFunction{
|
|
|
|
Instructions: fn.Instructions,
|
|
|
|
NumLocals: fn.NumLocals,
|
|
|
|
NumParameters: fn.NumParameters,
|
|
|
|
VarArgs: fn.VarArgs,
|
|
|
|
Free: free,
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
2019-04-06 20:18:25 +00:00
|
|
|
v.allocs--
|
|
|
|
if v.allocs == 0 {
|
|
|
|
v.err = ErrObjectAllocLimit
|
|
|
|
return
|
|
|
|
}
|
|
|
|
v.stack[v.sp] = cl
|
2019-02-23 15:39:44 +00:00
|
|
|
v.sp++
|
2020-01-09 20:52:19 +00:00
|
|
|
case parser.OpGetFreePtr:
|
2019-02-23 15:39:44 +00:00
|
|
|
v.ip++
|
2019-04-06 20:18:25 +00:00
|
|
|
freeIndex := int(v.curInsts[v.ip])
|
2019-02-23 15:39:44 +00:00
|
|
|
val := v.curFrame.freeVars[freeIndex]
|
2019-04-06 20:18:25 +00:00
|
|
|
v.stack[v.sp] = val
|
|
|
|
v.sp++
|
2020-01-09 20:52:19 +00:00
|
|
|
case parser.OpGetFree:
|
2019-04-06 20:18:25 +00:00
|
|
|
v.ip++
|
|
|
|
freeIndex := int(v.curInsts[v.ip])
|
|
|
|
val := *v.curFrame.freeVars[freeIndex].Value
|
2019-02-23 15:39:44 +00:00
|
|
|
v.stack[v.sp] = val
|
|
|
|
v.sp++
|
2020-01-09 20:52:19 +00:00
|
|
|
case parser.OpSetFree:
|
2019-04-06 20:18:25 +00:00
|
|
|
v.ip++
|
|
|
|
freeIndex := int(v.curInsts[v.ip])
|
|
|
|
*v.curFrame.freeVars[freeIndex].Value = v.stack[v.sp-1]
|
|
|
|
v.sp--
|
2020-01-09 20:52:19 +00:00
|
|
|
case parser.OpGetLocalPtr:
|
2019-04-06 20:18:25 +00:00
|
|
|
v.ip++
|
|
|
|
localIndex := int(v.curInsts[v.ip])
|
|
|
|
sp := v.curFrame.basePointer + localIndex
|
|
|
|
val := v.stack[sp]
|
2020-01-09 20:52:19 +00:00
|
|
|
var freeVar *ObjectPtr
|
|
|
|
if obj, ok := val.(*ObjectPtr); ok {
|
2019-04-06 20:18:25 +00:00
|
|
|
freeVar = obj
|
|
|
|
} else {
|
2020-01-09 20:52:19 +00:00
|
|
|
freeVar = &ObjectPtr{Value: &val}
|
2019-04-06 20:18:25 +00:00
|
|
|
v.stack[sp] = freeVar
|
|
|
|
}
|
|
|
|
v.stack[v.sp] = freeVar
|
|
|
|
v.sp++
|
2020-01-09 20:52:19 +00:00
|
|
|
case parser.OpSetSelFree:
|
2019-02-23 15:39:44 +00:00
|
|
|
v.ip += 2
|
2019-04-06 20:18:25 +00:00
|
|
|
freeIndex := int(v.curInsts[v.ip-1])
|
|
|
|
numSelectors := int(v.curInsts[v.ip])
|
2019-02-23 15:39:44 +00:00
|
|
|
|
|
|
|
// selectors and RHS value
|
2020-01-09 20:52:19 +00:00
|
|
|
selectors := make([]Object, numSelectors)
|
2019-04-06 20:18:25 +00:00
|
|
|
for i := 0; i < numSelectors; i++ {
|
|
|
|
selectors[i] = v.stack[v.sp-numSelectors+i]
|
|
|
|
}
|
2019-02-23 15:39:44 +00:00
|
|
|
val := v.stack[v.sp-numSelectors-1]
|
|
|
|
v.sp -= numSelectors + 1
|
2020-01-09 20:52:19 +00:00
|
|
|
e := indexAssign(*v.curFrame.freeVars[freeIndex].Value,
|
|
|
|
val, selectors)
|
|
|
|
if e != nil {
|
2019-03-05 22:08:54 +00:00
|
|
|
v.err = e
|
|
|
|
return
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
2020-01-09 20:52:19 +00:00
|
|
|
case parser.OpIteratorInit:
|
|
|
|
var iterator Object
|
2019-02-23 15:39:44 +00:00
|
|
|
dst := v.stack[v.sp-1]
|
|
|
|
v.sp--
|
2020-01-09 20:52:19 +00:00
|
|
|
if !dst.CanIterate() {
|
2019-04-06 20:18:25 +00:00
|
|
|
v.err = fmt.Errorf("not iterable: %s", dst.TypeName())
|
2019-03-05 22:08:54 +00:00
|
|
|
return
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
2020-01-09 20:52:19 +00:00
|
|
|
iterator = dst.Iterate()
|
2019-04-06 20:18:25 +00:00
|
|
|
v.allocs--
|
|
|
|
if v.allocs == 0 {
|
|
|
|
v.err = ErrObjectAllocLimit
|
2019-03-05 22:08:54 +00:00
|
|
|
return
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
2019-04-06 20:18:25 +00:00
|
|
|
v.stack[v.sp] = iterator
|
2019-02-23 15:39:44 +00:00
|
|
|
v.sp++
|
2020-01-09 20:52:19 +00:00
|
|
|
case parser.OpIteratorNext:
|
2019-02-23 15:39:44 +00:00
|
|
|
iterator := v.stack[v.sp-1]
|
|
|
|
v.sp--
|
2020-01-09 20:52:19 +00:00
|
|
|
hasMore := iterator.(Iterator).Next()
|
2019-02-23 15:39:44 +00:00
|
|
|
if hasMore {
|
2020-01-09 20:52:19 +00:00
|
|
|
v.stack[v.sp] = TrueValue
|
2019-02-23 15:39:44 +00:00
|
|
|
} else {
|
2020-01-09 20:52:19 +00:00
|
|
|
v.stack[v.sp] = FalseValue
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
|
|
|
v.sp++
|
2020-01-09 20:52:19 +00:00
|
|
|
case parser.OpIteratorKey:
|
2019-02-23 15:39:44 +00:00
|
|
|
iterator := v.stack[v.sp-1]
|
|
|
|
v.sp--
|
2020-01-09 20:52:19 +00:00
|
|
|
val := iterator.(Iterator).Key()
|
2019-04-06 20:18:25 +00:00
|
|
|
v.stack[v.sp] = val
|
2019-02-23 15:39:44 +00:00
|
|
|
v.sp++
|
2020-01-09 20:52:19 +00:00
|
|
|
case parser.OpIteratorValue:
|
2019-02-23 15:39:44 +00:00
|
|
|
iterator := v.stack[v.sp-1]
|
|
|
|
v.sp--
|
2020-01-09 20:52:19 +00:00
|
|
|
val := iterator.(Iterator).Value()
|
2019-04-06 20:18:25 +00:00
|
|
|
v.stack[v.sp] = val
|
2019-02-23 15:39:44 +00:00
|
|
|
v.sp++
|
2020-01-09 20:52:19 +00:00
|
|
|
case parser.OpSuspend:
|
|
|
|
return
|
2019-02-23 15:39:44 +00:00
|
|
|
default:
|
2019-04-06 20:18:25 +00:00
|
|
|
v.err = fmt.Errorf("unknown opcode: %d", v.curInsts[v.ip])
|
|
|
|
return
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-06 20:18:25 +00:00
|
|
|
// IsStackEmpty tests if the stack is empty or not.
|
|
|
|
func (v *VM) IsStackEmpty() bool {
|
|
|
|
return v.sp == 0
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
|
|
|
|
2020-01-09 20:52:19 +00:00
|
|
|
func indexAssign(dst, src Object, selectors []Object) error {
|
2019-02-23 15:39:44 +00:00
|
|
|
numSel := len(selectors)
|
|
|
|
for sidx := numSel - 1; sidx > 0; sidx-- {
|
2020-01-09 20:52:19 +00:00
|
|
|
next, err := dst.IndexGet(selectors[sidx])
|
2019-02-23 15:39:44 +00:00
|
|
|
if err != nil {
|
2020-01-09 20:52:19 +00:00
|
|
|
if err == ErrNotIndexable {
|
|
|
|
return fmt.Errorf("not indexable: %s", dst.TypeName())
|
|
|
|
}
|
|
|
|
if err == ErrInvalidIndexType {
|
|
|
|
return fmt.Errorf("invalid index type: %s",
|
|
|
|
selectors[sidx].TypeName())
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2019-04-06 20:18:25 +00:00
|
|
|
dst = next
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
|
|
|
|
2020-01-09 20:52:19 +00:00
|
|
|
if err := dst.IndexSet(selectors[0], src); err != nil {
|
|
|
|
if err == ErrNotIndexAssignable {
|
|
|
|
return fmt.Errorf("not index-assignable: %s", dst.TypeName())
|
|
|
|
}
|
|
|
|
if err == ErrInvalidIndexValueType {
|
2019-04-06 20:18:25 +00:00
|
|
|
return fmt.Errorf("invaid index value type: %s", src.TypeName())
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|