2019-01-11 13:27:28 +03:00
|
|
|
package runtime
|
2019-01-09 10:17:42 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-01-20 17:33:22 +03:00
|
|
|
"sync/atomic"
|
2019-01-09 10:17:42 +03:00
|
|
|
|
|
|
|
"github.com/d5/tengo/compiler"
|
2019-02-21 03:26:11 +03:00
|
|
|
"github.com/d5/tengo/compiler/source"
|
2019-01-11 13:27:28 +03:00
|
|
|
"github.com/d5/tengo/compiler/token"
|
2019-01-09 10:17:42 +03:00
|
|
|
"github.com/d5/tengo/objects"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-01-15 09:24:33 +03:00
|
|
|
// StackSize is the maximum stack size.
|
|
|
|
StackSize = 2048
|
|
|
|
|
|
|
|
// GlobalsSize is the maximum number of global variables.
|
2019-01-10 01:10:47 +03:00
|
|
|
GlobalsSize = 1024
|
2019-01-15 09:24:33 +03:00
|
|
|
|
|
|
|
// MaxFrames is the maximum number of function frames.
|
|
|
|
MaxFrames = 1024
|
2019-01-09 10:17:42 +03:00
|
|
|
)
|
|
|
|
|
2019-01-15 09:24:33 +03:00
|
|
|
// VM is a virtual machine that executes the bytecode compiled by Compiler.
|
2019-01-09 10:17:42 +03:00
|
|
|
type VM struct {
|
2019-03-18 18:15:26 +03:00
|
|
|
constants []objects.Object
|
2019-03-22 14:42:34 +03:00
|
|
|
stack [StackSize]objects.Object
|
2019-03-18 18:15:26 +03:00
|
|
|
sp int
|
|
|
|
globals []objects.Object
|
|
|
|
fileSet *source.FileSet
|
2019-03-22 14:42:34 +03:00
|
|
|
frames [MaxFrames]Frame
|
2019-03-18 18:15:26 +03:00
|
|
|
framesIndex int
|
|
|
|
curFrame *Frame
|
|
|
|
curInsts []byte
|
|
|
|
ip int
|
|
|
|
aborting int64
|
|
|
|
maxAllocs int64
|
|
|
|
allocs int64
|
|
|
|
err error
|
2019-01-09 10:17:42 +03:00
|
|
|
}
|
|
|
|
|
2019-01-15 09:24:33 +03:00
|
|
|
// NewVM creates a VM.
|
2019-03-18 18:15:26 +03:00
|
|
|
func NewVM(bytecode *compiler.Bytecode, globals []objects.Object, maxAllocs int64) *VM {
|
2019-01-09 10:17:42 +03:00
|
|
|
if globals == nil {
|
2019-03-14 11:07:19 +03:00
|
|
|
globals = make([]objects.Object, GlobalsSize)
|
2019-01-09 10:17:42 +03:00
|
|
|
}
|
|
|
|
|
2019-03-22 14:42:34 +03:00
|
|
|
v := &VM{
|
2019-03-18 18:15:26 +03:00
|
|
|
constants: bytecode.Constants,
|
|
|
|
sp: 0,
|
|
|
|
globals: globals,
|
|
|
|
fileSet: bytecode.FileSet,
|
|
|
|
framesIndex: 1,
|
|
|
|
ip: -1,
|
|
|
|
maxAllocs: maxAllocs,
|
2019-01-09 10:17:42 +03:00
|
|
|
}
|
2019-03-22 14:42:34 +03: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-01-09 10:17:42 +03:00
|
|
|
}
|
|
|
|
|
2019-01-15 09:24:33 +03:00
|
|
|
// Abort aborts the execution.
|
2019-01-09 10:17:42 +03:00
|
|
|
func (v *VM) Abort() {
|
2019-01-20 17:33:22 +03:00
|
|
|
atomic.StoreInt64(&v.aborting, 1)
|
2019-01-09 10:17:42 +03:00
|
|
|
}
|
|
|
|
|
2019-01-15 09:24:33 +03:00
|
|
|
// Run starts the execution.
|
2019-02-21 20:40:17 +03:00
|
|
|
func (v *VM) Run() (err error) {
|
2019-02-01 22:09:12 +03:00
|
|
|
// reset VM states
|
|
|
|
v.sp = 0
|
|
|
|
v.curFrame = &(v.frames[0])
|
|
|
|
v.curInsts = v.curFrame.fn.Instructions
|
|
|
|
v.framesIndex = 1
|
|
|
|
v.ip = -1
|
2019-03-07 04:20:05 +03:00
|
|
|
v.allocs = v.maxAllocs + 1
|
2019-02-01 22:09:12 +03:00
|
|
|
|
2019-03-06 00:53:14 +03:00
|
|
|
v.run()
|
2019-02-23 00:39:07 +03:00
|
|
|
|
2019-03-06 00:53:14 +03:00
|
|
|
err = v.err
|
|
|
|
if err != nil {
|
2019-03-22 14:42:34 +03:00
|
|
|
filePos := v.fileSet.Position(v.curFrame.fn.SourcePos(v.ip - 1))
|
2019-03-06 00:53:14 +03:00
|
|
|
err = fmt.Errorf("Runtime Error: %s\n\tat %s", err.Error(), filePos)
|
|
|
|
for v.framesIndex > 1 {
|
|
|
|
v.framesIndex--
|
|
|
|
v.curFrame = &v.frames[v.framesIndex-1]
|
|
|
|
|
2019-03-22 14:42:34 +03:00
|
|
|
filePos = v.fileSet.Position(v.curFrame.fn.SourcePos(v.curFrame.ip - 1))
|
2019-03-06 00:53:14 +03:00
|
|
|
err = fmt.Errorf("%s\n\tat %s", err.Error(), filePos)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *VM) run() {
|
2019-03-22 14:42:34 +03:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
if v.sp >= StackSize || v.framesIndex >= MaxFrames {
|
|
|
|
v.err = ErrStackOverflow
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.ip < len(v.curInsts)-1 {
|
|
|
|
if err, ok := r.(error); ok {
|
|
|
|
v.err = err
|
|
|
|
} else {
|
|
|
|
v.err = fmt.Errorf("panic: %v", r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-04-01 09:37:30 +03:00
|
|
|
for atomic.CompareAndSwapInt64(&v.aborting, 0, 0) {
|
2019-01-20 18:41:57 +03:00
|
|
|
v.ip++
|
2019-01-09 10:17:42 +03:00
|
|
|
|
2019-02-21 03:26:11 +03:00
|
|
|
switch v.curInsts[v.ip] {
|
2019-01-09 10:17:42 +03:00
|
|
|
case compiler.OpConstant:
|
2019-01-20 18:41:57 +03:00
|
|
|
v.ip += 2
|
2019-03-22 14:42:34 +03:00
|
|
|
cidx := int(v.curInsts[v.ip]) | int(v.curInsts[v.ip-1])<<8
|
2019-01-11 09:34:28 +03:00
|
|
|
|
2019-03-14 11:07:19 +03:00
|
|
|
v.stack[v.sp] = v.constants[cidx]
|
2019-01-14 15:32:09 +03:00
|
|
|
v.sp++
|
|
|
|
|
2019-01-09 10:17:42 +03:00
|
|
|
case compiler.OpNull:
|
2019-03-14 11:07:19 +03:00
|
|
|
v.stack[v.sp] = objects.UndefinedValue
|
2019-01-14 15:32:09 +03:00
|
|
|
v.sp++
|
|
|
|
|
2019-03-22 14:42:34 +03:00
|
|
|
case compiler.OpBinaryOp:
|
|
|
|
v.ip++
|
|
|
|
right := v.stack[v.sp-1]
|
|
|
|
left := v.stack[v.sp-2]
|
2019-01-14 15:32:09 +03:00
|
|
|
|
2019-03-22 14:42:34 +03:00
|
|
|
tok := token.Token(v.curInsts[v.ip])
|
|
|
|
res, e := left.BinaryOp(tok, right)
|
|
|
|
if e != nil {
|
|
|
|
v.sp -= 2
|
2019-01-14 15:32:09 +03:00
|
|
|
|
2019-03-22 14:42:34 +03:00
|
|
|
if e == objects.ErrInvalidOperator {
|
|
|
|
v.err = fmt.Errorf("invalid operation: %s %s %s",
|
|
|
|
left.TypeName(), tok.String(), right.TypeName())
|
|
|
|
return
|
|
|
|
}
|
2019-01-14 15:32:09 +03:00
|
|
|
|
2019-03-22 14:42:34 +03:00
|
|
|
v.err = e
|
|
|
|
return
|
|
|
|
}
|
2019-01-14 15:32:09 +03:00
|
|
|
|
2019-03-22 14:42:34 +03:00
|
|
|
v.allocs--
|
|
|
|
if v.allocs == 0 {
|
|
|
|
v.err = ErrObjectAllocLimit
|
|
|
|
return
|
|
|
|
}
|
2019-03-07 04:20:05 +03:00
|
|
|
|
2019-03-22 14:42:34 +03:00
|
|
|
v.stack[v.sp-2] = res
|
|
|
|
v.sp--
|
2019-03-07 04:20:05 +03:00
|
|
|
|
2019-01-09 10:17:42 +03:00
|
|
|
case compiler.OpEqual:
|
2019-01-14 15:32:09 +03:00
|
|
|
right := v.stack[v.sp-1]
|
|
|
|
left := v.stack[v.sp-2]
|
|
|
|
v.sp -= 2
|
|
|
|
|
2019-03-14 11:07:19 +03:00
|
|
|
if left.Equals(right) {
|
|
|
|
v.stack[v.sp] = objects.TrueValue
|
2019-01-09 10:17:42 +03:00
|
|
|
} else {
|
2019-03-14 11:07:19 +03:00
|
|
|
v.stack[v.sp] = objects.FalseValue
|
2019-01-09 10:17:42 +03:00
|
|
|
}
|
2019-01-14 15:32:09 +03:00
|
|
|
v.sp++
|
|
|
|
|
2019-01-09 10:17:42 +03:00
|
|
|
case compiler.OpNotEqual:
|
2019-01-14 15:32:09 +03:00
|
|
|
right := v.stack[v.sp-1]
|
|
|
|
left := v.stack[v.sp-2]
|
|
|
|
v.sp -= 2
|
|
|
|
|
2019-03-14 11:07:19 +03:00
|
|
|
if left.Equals(right) {
|
|
|
|
v.stack[v.sp] = objects.FalseValue
|
2019-01-09 10:17:42 +03:00
|
|
|
} else {
|
2019-03-14 11:07:19 +03:00
|
|
|
v.stack[v.sp] = objects.TrueValue
|
2019-01-09 10:17:42 +03:00
|
|
|
}
|
2019-01-14 15:32:09 +03:00
|
|
|
v.sp++
|
|
|
|
|
2019-01-09 10:17:42 +03:00
|
|
|
case compiler.OpPop:
|
2019-01-14 15:32:09 +03:00
|
|
|
v.sp--
|
|
|
|
|
2019-01-09 10:17:42 +03:00
|
|
|
case compiler.OpTrue:
|
2019-03-14 11:07:19 +03:00
|
|
|
v.stack[v.sp] = objects.TrueValue
|
2019-01-14 15:32:09 +03:00
|
|
|
v.sp++
|
|
|
|
|
2019-01-09 10:17:42 +03:00
|
|
|
case compiler.OpFalse:
|
2019-03-14 11:07:19 +03:00
|
|
|
v.stack[v.sp] = objects.FalseValue
|
2019-01-14 15:32:09 +03:00
|
|
|
v.sp++
|
|
|
|
|
2019-01-09 10:17:42 +03:00
|
|
|
case compiler.OpLNot:
|
2019-01-14 15:32:09 +03:00
|
|
|
operand := v.stack[v.sp-1]
|
|
|
|
v.sp--
|
|
|
|
|
2019-03-14 11:07:19 +03:00
|
|
|
if operand.IsFalsy() {
|
|
|
|
v.stack[v.sp] = objects.TrueValue
|
2019-01-09 10:17:42 +03:00
|
|
|
} else {
|
2019-03-14 11:07:19 +03:00
|
|
|
v.stack[v.sp] = objects.FalseValue
|
2019-01-09 10:17:42 +03:00
|
|
|
}
|
2019-01-14 15:32:09 +03:00
|
|
|
v.sp++
|
|
|
|
|
2019-01-09 10:17:42 +03:00
|
|
|
case compiler.OpBComplement:
|
2019-01-14 15:32:09 +03:00
|
|
|
operand := v.stack[v.sp-1]
|
|
|
|
v.sp--
|
2019-01-09 10:17:42 +03:00
|
|
|
|
2019-03-14 11:07:19 +03:00
|
|
|
switch x := operand.(type) {
|
2019-01-09 10:17:42 +03:00
|
|
|
case *objects.Int:
|
2019-01-14 15:32:09 +03:00
|
|
|
var res objects.Object = &objects.Int{Value: ^x.Value}
|
|
|
|
|
2019-03-07 04:20:05 +03:00
|
|
|
v.allocs--
|
|
|
|
if v.allocs == 0 {
|
|
|
|
v.err = ErrObjectAllocLimit
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-03-14 11:07:19 +03:00
|
|
|
v.stack[v.sp] = res
|
2019-01-14 15:32:09 +03:00
|
|
|
v.sp++
|
2019-01-09 10:17:42 +03:00
|
|
|
default:
|
2019-03-14 11:07:19 +03:00
|
|
|
v.err = fmt.Errorf("invalid operation: ^%s", operand.TypeName())
|
2019-03-06 00:53:14 +03:00
|
|
|
return
|
2019-01-09 10:17:42 +03:00
|
|
|
}
|
2019-01-14 15:32:09 +03:00
|
|
|
|
2019-01-09 10:17:42 +03:00
|
|
|
case compiler.OpMinus:
|
2019-01-14 15:32:09 +03:00
|
|
|
operand := v.stack[v.sp-1]
|
|
|
|
v.sp--
|
2019-01-09 10:17:42 +03:00
|
|
|
|
2019-03-14 11:07:19 +03:00
|
|
|
switch x := operand.(type) {
|
2019-01-09 10:17:42 +03:00
|
|
|
case *objects.Int:
|
2019-01-14 15:32:09 +03:00
|
|
|
var res objects.Object = &objects.Int{Value: -x.Value}
|
|
|
|
|
2019-03-07 04:20:05 +03:00
|
|
|
v.allocs--
|
|
|
|
if v.allocs == 0 {
|
|
|
|
v.err = ErrObjectAllocLimit
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-03-14 11:07:19 +03:00
|
|
|
v.stack[v.sp] = res
|
2019-01-14 15:32:09 +03:00
|
|
|
v.sp++
|
2019-01-09 10:17:42 +03:00
|
|
|
case *objects.Float:
|
2019-01-14 15:32:09 +03:00
|
|
|
var res objects.Object = &objects.Float{Value: -x.Value}
|
|
|
|
|
2019-03-07 04:20:05 +03:00
|
|
|
v.allocs--
|
|
|
|
if v.allocs == 0 {
|
|
|
|
v.err = ErrObjectAllocLimit
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-03-14 11:07:19 +03:00
|
|
|
v.stack[v.sp] = res
|
2019-01-14 15:32:09 +03:00
|
|
|
v.sp++
|
2019-01-09 10:17:42 +03:00
|
|
|
default:
|
2019-03-14 11:07:19 +03:00
|
|
|
v.err = fmt.Errorf("invalid operation: -%s", operand.TypeName())
|
2019-03-06 00:53:14 +03:00
|
|
|
return
|
2019-01-09 10:17:42 +03:00
|
|
|
}
|
2019-01-14 15:32:09 +03:00
|
|
|
|
2019-01-09 10:17:42 +03:00
|
|
|
case compiler.OpJumpFalsy:
|
2019-01-20 18:41:57 +03:00
|
|
|
v.ip += 2
|
2019-01-14 15:32:09 +03:00
|
|
|
v.sp--
|
2019-03-22 14:42:34 +03:00
|
|
|
if v.stack[v.sp].IsFalsy() {
|
|
|
|
pos := int(v.curInsts[v.ip]) | int(v.curInsts[v.ip-1])<<8
|
2019-01-20 18:41:57 +03:00
|
|
|
v.ip = pos - 1
|
2019-01-09 10:17:42 +03:00
|
|
|
}
|
2019-01-14 15:32:09 +03:00
|
|
|
|
2019-01-09 10:17:42 +03:00
|
|
|
case compiler.OpAndJump:
|
2019-01-20 18:41:57 +03:00
|
|
|
v.ip += 2
|
2019-01-09 10:17:42 +03:00
|
|
|
|
2019-03-22 14:42:34 +03:00
|
|
|
if v.stack[v.sp-1].IsFalsy() {
|
|
|
|
pos := int(v.curInsts[v.ip]) | int(v.curInsts[v.ip-1])<<8
|
2019-01-20 18:41:57 +03:00
|
|
|
v.ip = pos - 1
|
2019-01-09 10:17:42 +03:00
|
|
|
} else {
|
2019-01-14 15:32:09 +03:00
|
|
|
v.sp--
|
2019-01-09 10:17:42 +03:00
|
|
|
}
|
2019-01-14 15:32:09 +03:00
|
|
|
|
2019-01-09 10:17:42 +03:00
|
|
|
case compiler.OpOrJump:
|
2019-01-20 18:41:57 +03:00
|
|
|
v.ip += 2
|
2019-01-09 10:17:42 +03:00
|
|
|
|
2019-03-22 14:42:34 +03:00
|
|
|
if v.stack[v.sp-1].IsFalsy() {
|
2019-01-14 15:32:09 +03:00
|
|
|
v.sp--
|
2019-03-22 14:42:34 +03:00
|
|
|
} else {
|
|
|
|
pos := int(v.curInsts[v.ip]) | int(v.curInsts[v.ip-1])<<8
|
|
|
|
v.ip = pos - 1
|
2019-01-09 10:17:42 +03:00
|
|
|
}
|
2019-01-14 15:32:09 +03:00
|
|
|
|
2019-01-09 10:17:42 +03:00
|
|
|
case compiler.OpJump:
|
2019-01-20 18:41:57 +03:00
|
|
|
pos := int(v.curInsts[v.ip+2]) | int(v.curInsts[v.ip+1])<<8
|
|
|
|
v.ip = pos - 1
|
2019-01-14 15:32:09 +03:00
|
|
|
|
2019-01-09 10:17:42 +03:00
|
|
|
case compiler.OpSetGlobal:
|
2019-01-20 18:41:57 +03:00
|
|
|
v.ip += 2
|
2019-01-14 15:32:09 +03:00
|
|
|
v.sp--
|
|
|
|
|
2019-03-22 14:42:34 +03:00
|
|
|
globalIndex := int(v.curInsts[v.ip]) | int(v.curInsts[v.ip-1])<<8
|
2019-01-14 15:32:09 +03:00
|
|
|
v.globals[globalIndex] = v.stack[v.sp]
|
2019-01-09 10:17:42 +03:00
|
|
|
|
|
|
|
case compiler.OpSetSelGlobal:
|
2019-01-20 18:41:57 +03:00
|
|
|
v.ip += 3
|
2019-03-22 14:42:34 +03:00
|
|
|
globalIndex := int(v.curInsts[v.ip-1]) | int(v.curInsts[v.ip-2])<<8
|
|
|
|
numSelectors := int(v.curInsts[v.ip])
|
2019-01-09 10:17:42 +03:00
|
|
|
|
2019-01-21 10:32:58 +03:00
|
|
|
// selectors and RHS value
|
2019-03-14 11:07:19 +03:00
|
|
|
selectors := make([]objects.Object, numSelectors)
|
|
|
|
for i := 0; i < numSelectors; i++ {
|
|
|
|
selectors[i] = v.stack[v.sp-numSelectors+i]
|
|
|
|
}
|
|
|
|
|
2019-01-21 10:32:58 +03:00
|
|
|
val := v.stack[v.sp-numSelectors-1]
|
|
|
|
v.sp -= numSelectors + 1
|
2019-01-09 10:17:42 +03:00
|
|
|
|
2019-02-21 20:40:17 +03:00
|
|
|
if e := indexAssign(v.globals[globalIndex], val, selectors); e != nil {
|
2019-03-06 00:53:14 +03:00
|
|
|
v.err = e
|
|
|
|
return
|
2019-01-09 10:17:42 +03:00
|
|
|
}
|
2019-01-14 15:32:09 +03:00
|
|
|
|
2019-01-09 10:17:42 +03:00
|
|
|
case compiler.OpGetGlobal:
|
2019-01-20 18:41:57 +03:00
|
|
|
v.ip += 2
|
2019-03-22 14:42:34 +03:00
|
|
|
globalIndex := int(v.curInsts[v.ip]) | int(v.curInsts[v.ip-1])<<8
|
2019-01-09 10:17:42 +03:00
|
|
|
|
|
|
|
val := v.globals[globalIndex]
|
|
|
|
|
2019-01-14 15:32:09 +03:00
|
|
|
v.stack[v.sp] = val
|
|
|
|
v.sp++
|
|
|
|
|
2019-01-09 10:17:42 +03:00
|
|
|
case compiler.OpArray:
|
2019-01-20 18:41:57 +03:00
|
|
|
v.ip += 2
|
2019-03-22 14:42:34 +03:00
|
|
|
numElements := int(v.curInsts[v.ip]) | int(v.curInsts[v.ip-1])<<8
|
2019-01-09 10:17:42 +03:00
|
|
|
|
|
|
|
var elements []objects.Object
|
|
|
|
for i := v.sp - numElements; i < v.sp; i++ {
|
2019-03-14 11:07:19 +03:00
|
|
|
elements = append(elements, v.stack[i])
|
2019-01-09 10:17:42 +03:00
|
|
|
}
|
|
|
|
v.sp -= numElements
|
|
|
|
|
|
|
|
var arr objects.Object = &objects.Array{Value: elements}
|
|
|
|
|
2019-03-07 04:20:05 +03:00
|
|
|
v.allocs--
|
|
|
|
if v.allocs == 0 {
|
|
|
|
v.err = ErrObjectAllocLimit
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-03-14 11:07:19 +03:00
|
|
|
v.stack[v.sp] = arr
|
2019-01-14 15:32:09 +03:00
|
|
|
v.sp++
|
|
|
|
|
2019-01-09 10:17:42 +03:00
|
|
|
case compiler.OpMap:
|
2019-01-20 18:41:57 +03:00
|
|
|
v.ip += 2
|
2019-03-22 14:42:34 +03:00
|
|
|
numElements := int(v.curInsts[v.ip]) | int(v.curInsts[v.ip-1])<<8
|
2019-01-09 10:17:42 +03:00
|
|
|
|
|
|
|
kv := make(map[string]objects.Object)
|
|
|
|
for i := v.sp - numElements; i < v.sp; i += 2 {
|
2019-03-14 11:07:19 +03:00
|
|
|
key := v.stack[i]
|
|
|
|
value := v.stack[i+1]
|
2019-01-09 10:17:42 +03:00
|
|
|
kv[key.(*objects.String).Value] = value
|
|
|
|
}
|
|
|
|
v.sp -= numElements
|
|
|
|
|
2019-01-15 09:24:33 +03:00
|
|
|
var m objects.Object = &objects.Map{Value: kv}
|
2019-01-09 10:17:42 +03:00
|
|
|
|
2019-03-07 04:20:05 +03:00
|
|
|
v.allocs--
|
|
|
|
if v.allocs == 0 {
|
|
|
|
v.err = ErrObjectAllocLimit
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-03-14 11:07:19 +03:00
|
|
|
v.stack[v.sp] = m
|
2019-01-14 15:32:09 +03:00
|
|
|
v.sp++
|
|
|
|
|
2019-01-16 23:23:20 +03:00
|
|
|
case compiler.OpError:
|
|
|
|
value := v.stack[v.sp-1]
|
|
|
|
|
2019-02-21 20:40:17 +03:00
|
|
|
var e objects.Object = &objects.Error{
|
2019-03-14 11:07:19 +03:00
|
|
|
Value: value,
|
2019-01-16 23:23:20 +03:00
|
|
|
}
|
|
|
|
|
2019-03-07 04:20:05 +03:00
|
|
|
v.allocs--
|
|
|
|
if v.allocs == 0 {
|
|
|
|
v.err = ErrObjectAllocLimit
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-03-14 11:07:19 +03:00
|
|
|
v.stack[v.sp-1] = e
|
2019-01-16 23:23:20 +03:00
|
|
|
|
2019-01-25 06:31:17 +03:00
|
|
|
case compiler.OpImmutable:
|
2019-01-26 01:54:58 +03:00
|
|
|
value := v.stack[v.sp-1]
|
|
|
|
|
2019-03-14 11:07:19 +03:00
|
|
|
switch value := value.(type) {
|
2019-01-26 01:54:58 +03:00
|
|
|
case *objects.Array:
|
|
|
|
var immutableArray objects.Object = &objects.ImmutableArray{
|
|
|
|
Value: value.Value,
|
|
|
|
}
|
2019-03-07 04:20:05 +03:00
|
|
|
|
|
|
|
v.allocs--
|
|
|
|
if v.allocs == 0 {
|
|
|
|
v.err = ErrObjectAllocLimit
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-03-14 11:07:19 +03:00
|
|
|
v.stack[v.sp-1] = immutableArray
|
2019-01-26 01:54:58 +03:00
|
|
|
case *objects.Map:
|
|
|
|
var immutableMap objects.Object = &objects.ImmutableMap{
|
|
|
|
Value: value.Value,
|
|
|
|
}
|
2019-03-07 04:20:05 +03:00
|
|
|
|
|
|
|
v.allocs--
|
|
|
|
if v.allocs == 0 {
|
|
|
|
v.err = ErrObjectAllocLimit
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-03-14 11:07:19 +03:00
|
|
|
v.stack[v.sp-1] = immutableMap
|
2019-01-26 01:54:58 +03:00
|
|
|
}
|
2019-01-25 06:31:17 +03:00
|
|
|
|
2019-01-09 10:17:42 +03:00
|
|
|
case compiler.OpIndex:
|
2019-01-14 15:32:09 +03:00
|
|
|
index := v.stack[v.sp-1]
|
|
|
|
left := v.stack[v.sp-2]
|
|
|
|
v.sp -= 2
|
2019-01-09 10:17:42 +03:00
|
|
|
|
2019-03-14 11:07:19 +03:00
|
|
|
switch left := left.(type) {
|
2019-01-21 10:32:58 +03:00
|
|
|
case objects.Indexable:
|
2019-03-14 11:07:19 +03:00
|
|
|
val, e := left.IndexGet(index)
|
2019-02-21 20:40:17 +03:00
|
|
|
if e != nil {
|
2019-02-21 03:26:11 +03:00
|
|
|
|
2019-02-21 20:40:17 +03:00
|
|
|
if e == objects.ErrInvalidIndexType {
|
2019-03-14 11:07:19 +03:00
|
|
|
v.err = fmt.Errorf("invalid index type: %s", index.TypeName())
|
2019-03-06 00:53:14 +03:00
|
|
|
return
|
2019-02-21 03:26:11 +03:00
|
|
|
}
|
|
|
|
|
2019-03-06 00:53:14 +03:00
|
|
|
v.err = e
|
|
|
|
return
|
2019-01-18 12:43:46 +03:00
|
|
|
}
|
2019-01-21 10:32:58 +03:00
|
|
|
if val == nil {
|
|
|
|
val = objects.UndefinedValue
|
2019-01-18 12:43:46 +03:00
|
|
|
}
|
|
|
|
|
2019-03-14 11:07:19 +03:00
|
|
|
v.stack[v.sp] = val
|
2019-01-18 12:43:46 +03:00
|
|
|
v.sp++
|
|
|
|
|
2019-02-21 20:40:17 +03:00
|
|
|
case *objects.Error: // e.value
|
2019-03-14 11:07:19 +03:00
|
|
|
key, ok := index.(*objects.String)
|
2019-01-16 23:23:20 +03:00
|
|
|
if !ok || key.Value != "value" {
|
2019-03-06 00:53:14 +03:00
|
|
|
v.err = fmt.Errorf("invalid index on error")
|
|
|
|
return
|
2019-01-16 23:23:20 +03:00
|
|
|
}
|
|
|
|
|
2019-03-14 11:07:19 +03:00
|
|
|
v.stack[v.sp] = left.Value
|
2019-01-16 23:23:20 +03:00
|
|
|
v.sp++
|
|
|
|
|
2019-01-09 10:17:42 +03:00
|
|
|
default:
|
2019-03-06 00:53:14 +03:00
|
|
|
v.err = fmt.Errorf("not indexable: %s", left.TypeName())
|
|
|
|
return
|
2019-01-09 10:17:42 +03:00
|
|
|
}
|
2019-01-14 15:32:09 +03:00
|
|
|
|
2019-01-09 10:17:42 +03:00
|
|
|
case compiler.OpSliceIndex:
|
2019-01-14 15:32:09 +03:00
|
|
|
high := v.stack[v.sp-1]
|
|
|
|
low := v.stack[v.sp-2]
|
|
|
|
left := v.stack[v.sp-3]
|
|
|
|
v.sp -= 3
|
|
|
|
|
2019-01-29 00:09:40 +03:00
|
|
|
var lowIdx int64
|
2019-03-14 11:07:19 +03:00
|
|
|
if low != objects.UndefinedValue {
|
|
|
|
if low, ok := low.(*objects.Int); ok {
|
2019-01-29 00:09:40 +03:00
|
|
|
lowIdx = low.Value
|
|
|
|
} else {
|
2019-03-06 00:53:14 +03:00
|
|
|
v.err = fmt.Errorf("invalid slice index type: %s", low.TypeName())
|
|
|
|
return
|
2019-01-29 00:09:40 +03:00
|
|
|
}
|
2019-01-09 10:17:42 +03:00
|
|
|
}
|
|
|
|
|
2019-03-14 11:07:19 +03:00
|
|
|
switch left := left.(type) {
|
2019-01-09 10:17:42 +03:00
|
|
|
case *objects.Array:
|
2019-01-14 15:32:09 +03:00
|
|
|
numElements := int64(len(left.Value))
|
2019-02-10 03:45:21 +03:00
|
|
|
var highIdx int64
|
2019-03-14 11:07:19 +03:00
|
|
|
if high == objects.UndefinedValue {
|
2019-01-14 15:32:09 +03:00
|
|
|
highIdx = numElements
|
2019-03-14 11:07:19 +03:00
|
|
|
} else if high, ok := high.(*objects.Int); ok {
|
2019-02-10 03:45:21 +03:00
|
|
|
highIdx = high.Value
|
|
|
|
} else {
|
2019-03-06 00:53:14 +03:00
|
|
|
v.err = fmt.Errorf("invalid slice index type: %s", high.TypeName())
|
|
|
|
return
|
2019-01-14 15:32:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if lowIdx > highIdx {
|
2019-03-06 00:53:14 +03:00
|
|
|
v.err = fmt.Errorf("invalid slice index: %d > %d", lowIdx, highIdx)
|
|
|
|
return
|
2019-01-14 15:32:09 +03:00
|
|
|
}
|
|
|
|
|
2019-02-10 03:45:21 +03:00
|
|
|
if lowIdx < 0 {
|
|
|
|
lowIdx = 0
|
|
|
|
} else if lowIdx > numElements {
|
|
|
|
lowIdx = numElements
|
|
|
|
}
|
|
|
|
|
|
|
|
if highIdx < 0 {
|
|
|
|
highIdx = 0
|
|
|
|
} else if highIdx > numElements {
|
|
|
|
highIdx = numElements
|
|
|
|
}
|
|
|
|
|
2019-01-14 15:32:09 +03:00
|
|
|
var val objects.Object = &objects.Array{Value: left.Value[lowIdx:highIdx]}
|
2019-03-07 04:20:05 +03:00
|
|
|
|
|
|
|
v.allocs--
|
|
|
|
if v.allocs == 0 {
|
|
|
|
v.err = ErrObjectAllocLimit
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-03-14 11:07:19 +03:00
|
|
|
v.stack[v.sp] = val
|
2019-01-14 15:32:09 +03:00
|
|
|
v.sp++
|
2019-01-26 01:54:58 +03:00
|
|
|
|
|
|
|
case *objects.ImmutableArray:
|
|
|
|
numElements := int64(len(left.Value))
|
2019-02-10 03:45:21 +03:00
|
|
|
var highIdx int64
|
2019-03-14 11:07:19 +03:00
|
|
|
if high == objects.UndefinedValue {
|
2019-01-26 01:54:58 +03:00
|
|
|
highIdx = numElements
|
2019-03-14 11:07:19 +03:00
|
|
|
} else if high, ok := high.(*objects.Int); ok {
|
2019-02-10 03:45:21 +03:00
|
|
|
highIdx = high.Value
|
|
|
|
} else {
|
2019-03-06 00:53:14 +03:00
|
|
|
v.err = fmt.Errorf("invalid slice index type: %s", high.TypeName())
|
|
|
|
return
|
2019-01-26 01:54:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if lowIdx > highIdx {
|
2019-03-06 00:53:14 +03:00
|
|
|
v.err = fmt.Errorf("invalid slice index: %d > %d", lowIdx, highIdx)
|
|
|
|
return
|
2019-01-26 01:54:58 +03:00
|
|
|
}
|
|
|
|
|
2019-02-10 03:45:21 +03:00
|
|
|
if lowIdx < 0 {
|
|
|
|
lowIdx = 0
|
|
|
|
} else if lowIdx > numElements {
|
|
|
|
lowIdx = numElements
|
|
|
|
}
|
|
|
|
|
|
|
|
if highIdx < 0 {
|
|
|
|
highIdx = 0
|
|
|
|
} else if highIdx > numElements {
|
|
|
|
highIdx = numElements
|
|
|
|
}
|
|
|
|
|
2019-01-26 01:54:58 +03:00
|
|
|
var val objects.Object = &objects.Array{Value: left.Value[lowIdx:highIdx]}
|
|
|
|
|
2019-03-07 04:20:05 +03:00
|
|
|
v.allocs--
|
|
|
|
if v.allocs == 0 {
|
|
|
|
v.err = ErrObjectAllocLimit
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-03-14 11:07:19 +03:00
|
|
|
v.stack[v.sp] = val
|
2019-01-26 01:54:58 +03:00
|
|
|
v.sp++
|
2019-01-14 15:32:09 +03:00
|
|
|
|
2019-01-09 10:17:42 +03:00
|
|
|
case *objects.String:
|
2019-01-14 15:32:09 +03:00
|
|
|
numElements := int64(len(left.Value))
|
2019-02-10 03:45:21 +03:00
|
|
|
var highIdx int64
|
2019-03-14 11:07:19 +03:00
|
|
|
if high == objects.UndefinedValue {
|
2019-01-14 15:32:09 +03:00
|
|
|
highIdx = numElements
|
2019-03-14 11:07:19 +03:00
|
|
|
} else if high, ok := high.(*objects.Int); ok {
|
2019-02-10 03:45:21 +03:00
|
|
|
highIdx = high.Value
|
|
|
|
} else {
|
2019-03-06 00:53:14 +03:00
|
|
|
v.err = fmt.Errorf("invalid slice index type: %s", high.TypeName())
|
|
|
|
return
|
2019-01-09 10:17:42 +03:00
|
|
|
}
|
2019-01-14 15:32:09 +03:00
|
|
|
|
|
|
|
if lowIdx > highIdx {
|
2019-03-06 00:53:14 +03:00
|
|
|
v.err = fmt.Errorf("invalid slice index: %d > %d", lowIdx, highIdx)
|
|
|
|
return
|
2019-01-14 15:32:09 +03:00
|
|
|
}
|
|
|
|
|
2019-02-10 03:45:21 +03:00
|
|
|
if lowIdx < 0 {
|
|
|
|
lowIdx = 0
|
|
|
|
} else if lowIdx > numElements {
|
|
|
|
lowIdx = numElements
|
|
|
|
}
|
|
|
|
|
|
|
|
if highIdx < 0 {
|
|
|
|
highIdx = 0
|
|
|
|
} else if highIdx > numElements {
|
|
|
|
highIdx = numElements
|
|
|
|
}
|
|
|
|
|
2019-01-14 15:32:09 +03:00
|
|
|
var val objects.Object = &objects.String{Value: left.Value[lowIdx:highIdx]}
|
|
|
|
|
2019-03-07 04:20:05 +03:00
|
|
|
v.allocs--
|
|
|
|
if v.allocs == 0 {
|
|
|
|
v.err = ErrObjectAllocLimit
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-03-14 11:07:19 +03:00
|
|
|
v.stack[v.sp] = val
|
2019-01-14 15:32:09 +03:00
|
|
|
v.sp++
|
|
|
|
|
2019-01-18 12:43:46 +03:00
|
|
|
case *objects.Bytes:
|
|
|
|
numElements := int64(len(left.Value))
|
2019-02-10 03:45:21 +03:00
|
|
|
var highIdx int64
|
2019-03-14 11:07:19 +03:00
|
|
|
if high == objects.UndefinedValue {
|
2019-01-18 12:43:46 +03:00
|
|
|
highIdx = numElements
|
2019-03-14 11:07:19 +03:00
|
|
|
} else if high, ok := high.(*objects.Int); ok {
|
2019-02-10 03:45:21 +03:00
|
|
|
highIdx = high.Value
|
|
|
|
} else {
|
2019-03-06 00:53:14 +03:00
|
|
|
v.err = fmt.Errorf("invalid slice index type: %s", high.TypeName())
|
|
|
|
return
|
2019-01-18 12:43:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if lowIdx > highIdx {
|
2019-03-06 00:53:14 +03:00
|
|
|
v.err = fmt.Errorf("invalid slice index: %d > %d", lowIdx, highIdx)
|
|
|
|
return
|
2019-01-18 12:43:46 +03:00
|
|
|
}
|
|
|
|
|
2019-02-10 03:45:21 +03:00
|
|
|
if lowIdx < 0 {
|
|
|
|
lowIdx = 0
|
|
|
|
} else if lowIdx > numElements {
|
|
|
|
lowIdx = numElements
|
|
|
|
}
|
|
|
|
|
|
|
|
if highIdx < 0 {
|
|
|
|
highIdx = 0
|
|
|
|
} else if highIdx > numElements {
|
|
|
|
highIdx = numElements
|
|
|
|
}
|
|
|
|
|
2019-01-18 12:43:46 +03:00
|
|
|
var val objects.Object = &objects.Bytes{Value: left.Value[lowIdx:highIdx]}
|
|
|
|
|
2019-03-07 04:20:05 +03:00
|
|
|
v.allocs--
|
|
|
|
if v.allocs == 0 {
|
|
|
|
v.err = ErrObjectAllocLimit
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-03-14 11:07:19 +03:00
|
|
|
v.stack[v.sp] = val
|
2019-01-18 12:43:46 +03:00
|
|
|
v.sp++
|
2019-01-09 10:17:42 +03:00
|
|
|
}
|
2019-01-14 15:32:09 +03:00
|
|
|
|
2019-01-09 10:17:42 +03:00
|
|
|
case compiler.OpCall:
|
2019-01-20 18:41:57 +03:00
|
|
|
numArgs := int(v.curInsts[v.ip+1])
|
|
|
|
v.ip++
|
2019-01-09 10:17:42 +03:00
|
|
|
|
2019-03-14 11:07:19 +03:00
|
|
|
value := v.stack[v.sp-1-numArgs]
|
2019-01-14 15:32:09 +03:00
|
|
|
|
2019-02-21 03:26:11 +03:00
|
|
|
switch callee := value.(type) {
|
2019-01-14 15:32:09 +03:00
|
|
|
case *objects.Closure:
|
2019-02-21 03:26:11 +03:00
|
|
|
if numArgs != callee.Fn.NumParameters {
|
2019-03-06 00:53:14 +03:00
|
|
|
v.err = fmt.Errorf("wrong number of arguments: want=%d, got=%d",
|
2019-02-23 00:39:07 +03:00
|
|
|
callee.Fn.NumParameters, numArgs)
|
2019-03-06 00:53:14 +03:00
|
|
|
return
|
2019-01-14 15:32:09 +03:00
|
|
|
}
|
2019-02-21 03:26:11 +03:00
|
|
|
|
|
|
|
// test if it's tail-call
|
|
|
|
if callee.Fn == v.curFrame.fn { // recursion
|
|
|
|
nextOp := v.curInsts[v.ip+1]
|
2019-03-22 14:42:34 +03:00
|
|
|
if nextOp == compiler.OpReturn ||
|
2019-02-21 03:26:11 +03:00
|
|
|
(nextOp == compiler.OpPop && compiler.OpReturn == v.curInsts[v.ip+2]) {
|
|
|
|
for p := 0; p < numArgs; p++ {
|
|
|
|
v.stack[v.curFrame.basePointer+p] = v.stack[v.sp-numArgs+p]
|
|
|
|
}
|
|
|
|
v.sp -= numArgs + 1
|
|
|
|
v.ip = -1 // reset IP to beginning of the frame
|
2019-03-07 04:20:05 +03:00
|
|
|
continue
|
2019-02-21 03:26:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// update call frame
|
|
|
|
v.curFrame.ip = v.ip // store current ip before call
|
|
|
|
v.curFrame = &(v.frames[v.framesIndex])
|
|
|
|
v.curFrame.fn = callee.Fn
|
|
|
|
v.curFrame.freeVars = callee.Free
|
|
|
|
v.curFrame.basePointer = v.sp - numArgs
|
|
|
|
v.curInsts = callee.Fn.Instructions
|
|
|
|
v.ip = -1
|
|
|
|
v.framesIndex++
|
|
|
|
v.sp = v.sp - numArgs + callee.Fn.NumLocals
|
|
|
|
|
2019-01-14 15:32:09 +03:00
|
|
|
case *objects.CompiledFunction:
|
2019-02-21 03:26:11 +03:00
|
|
|
if numArgs != callee.NumParameters {
|
2019-03-06 00:53:14 +03:00
|
|
|
v.err = fmt.Errorf("wrong number of arguments: want=%d, got=%d",
|
2019-02-23 00:39:07 +03:00
|
|
|
callee.NumParameters, numArgs)
|
2019-03-06 00:53:14 +03:00
|
|
|
return
|
2019-02-21 03:26:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// test if it's tail-call
|
|
|
|
if callee == v.curFrame.fn { // recursion
|
|
|
|
nextOp := v.curInsts[v.ip+1]
|
2019-03-22 14:42:34 +03:00
|
|
|
if nextOp == compiler.OpReturn ||
|
2019-02-21 03:26:11 +03:00
|
|
|
(nextOp == compiler.OpPop && compiler.OpReturn == v.curInsts[v.ip+2]) {
|
|
|
|
for p := 0; p < numArgs; p++ {
|
|
|
|
v.stack[v.curFrame.basePointer+p] = v.stack[v.sp-numArgs+p]
|
|
|
|
}
|
|
|
|
v.sp -= numArgs + 1
|
|
|
|
v.ip = -1 // reset IP to beginning of the frame
|
2019-03-07 04:20:05 +03:00
|
|
|
continue
|
2019-02-21 03:26:11 +03:00
|
|
|
}
|
2019-01-14 15:32:09 +03:00
|
|
|
}
|
2019-02-21 03:26:11 +03:00
|
|
|
|
|
|
|
// update call frame
|
|
|
|
v.curFrame.ip = v.ip // store current ip before call
|
|
|
|
v.curFrame = &(v.frames[v.framesIndex])
|
|
|
|
v.curFrame.fn = callee
|
|
|
|
v.curFrame.freeVars = nil
|
|
|
|
v.curFrame.basePointer = v.sp - numArgs
|
|
|
|
v.curInsts = callee.Instructions
|
|
|
|
v.ip = -1
|
|
|
|
v.framesIndex++
|
|
|
|
v.sp = v.sp - numArgs + callee.NumLocals
|
|
|
|
|
2019-01-14 15:32:09 +03:00
|
|
|
case objects.Callable:
|
|
|
|
var args []objects.Object
|
|
|
|
for _, arg := range v.stack[v.sp-numArgs : v.sp] {
|
2019-03-14 11:07:19 +03:00
|
|
|
args = append(args, arg)
|
2019-01-14 15:32:09 +03:00
|
|
|
}
|
|
|
|
|
2019-02-21 20:40:17 +03:00
|
|
|
ret, e := callee.Call(args...)
|
2019-01-14 15:32:09 +03:00
|
|
|
v.sp -= numArgs + 1
|
|
|
|
|
|
|
|
// runtime error
|
2019-02-21 20:40:17 +03:00
|
|
|
if e != nil {
|
|
|
|
if e == objects.ErrWrongNumArguments {
|
2019-03-06 00:53:14 +03:00
|
|
|
v.err = fmt.Errorf("wrong number of arguments in call to '%s'",
|
2019-02-23 00:39:07 +03:00
|
|
|
value.TypeName())
|
2019-03-06 00:53:14 +03:00
|
|
|
return
|
2019-02-21 03:26:11 +03:00
|
|
|
}
|
|
|
|
|
2019-02-21 20:40:17 +03:00
|
|
|
if e, ok := e.(objects.ErrInvalidArgumentType); ok {
|
2019-03-06 00:53:14 +03:00
|
|
|
v.err = fmt.Errorf("invalid type for argument '%s' in call to '%s': expected %s, found %s",
|
2019-02-23 00:39:07 +03:00
|
|
|
e.Name, value.TypeName(), e.Expected, e.Found)
|
2019-03-06 00:53:14 +03:00
|
|
|
return
|
2019-02-21 03:26:11 +03:00
|
|
|
}
|
|
|
|
|
2019-03-06 00:53:14 +03:00
|
|
|
v.err = e
|
|
|
|
return
|
2019-01-14 15:32:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// nil return -> undefined
|
|
|
|
if ret == nil {
|
|
|
|
ret = objects.UndefinedValue
|
|
|
|
}
|
|
|
|
|
2019-03-07 04:20:05 +03:00
|
|
|
v.allocs--
|
|
|
|
if v.allocs == 0 {
|
|
|
|
v.err = ErrObjectAllocLimit
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-03-14 11:07:19 +03:00
|
|
|
v.stack[v.sp] = ret
|
2019-01-14 15:32:09 +03:00
|
|
|
v.sp++
|
2019-02-21 03:26:11 +03:00
|
|
|
|
2019-01-14 15:32:09 +03:00
|
|
|
default:
|
2019-03-06 00:53:14 +03:00
|
|
|
v.err = fmt.Errorf("not callable: %s", callee.TypeName())
|
|
|
|
return
|
2019-01-09 10:17:42 +03:00
|
|
|
}
|
2019-01-14 15:32:09 +03:00
|
|
|
|
2019-03-22 14:42:34 +03:00
|
|
|
case compiler.OpReturn:
|
|
|
|
v.ip++
|
|
|
|
var retVal objects.Object
|
|
|
|
if int(v.curInsts[v.ip]) == 1 {
|
|
|
|
retVal = v.stack[v.sp-1]
|
|
|
|
} else {
|
|
|
|
retVal = objects.UndefinedValue
|
|
|
|
}
|
2019-01-14 15:32:09 +03:00
|
|
|
//v.sp--
|
2019-01-09 10:17:42 +03:00
|
|
|
|
|
|
|
v.framesIndex--
|
2019-01-14 02:21:19 +03:00
|
|
|
v.curFrame = &v.frames[v.framesIndex-1]
|
|
|
|
v.curInsts = v.curFrame.fn.Instructions
|
2019-01-20 18:41:57 +03:00
|
|
|
v.ip = v.curFrame.ip
|
2019-01-09 10:17:42 +03:00
|
|
|
|
2019-01-14 15:32:09 +03:00
|
|
|
//v.sp = lastFrame.basePointer - 1
|
2019-03-22 14:42:34 +03:00
|
|
|
v.sp = v.frames[v.framesIndex].basePointer
|
2019-01-09 10:17:42 +03:00
|
|
|
|
2019-02-21 03:26:11 +03:00
|
|
|
// skip stack overflow check because (newSP) <= (oldSP)
|
2019-01-14 15:32:09 +03:00
|
|
|
v.stack[v.sp-1] = retVal
|
|
|
|
//v.sp++
|
|
|
|
|
2019-01-11 09:34:28 +03:00
|
|
|
case compiler.OpDefineLocal:
|
2019-01-20 18:41:57 +03:00
|
|
|
v.ip++
|
2019-03-22 14:42:34 +03:00
|
|
|
localIndex := int(v.curInsts[v.ip])
|
2019-01-11 09:34:28 +03:00
|
|
|
|
2019-01-20 18:41:57 +03:00
|
|
|
sp := v.curFrame.basePointer + localIndex
|
2019-01-11 09:34:28 +03:00
|
|
|
|
|
|
|
// local variables can be mutated by other actions
|
|
|
|
// so always store the copy of popped value
|
2019-03-14 11:07:19 +03:00
|
|
|
val := v.stack[v.sp-1]
|
2019-01-14 15:32:09 +03:00
|
|
|
v.sp--
|
|
|
|
|
2019-03-14 11:07:19 +03:00
|
|
|
v.stack[sp] = val
|
2019-01-11 09:34:28 +03:00
|
|
|
|
2019-01-09 10:17:42 +03:00
|
|
|
case compiler.OpSetLocal:
|
2019-01-20 18:41:57 +03:00
|
|
|
localIndex := int(v.curInsts[v.ip+1])
|
|
|
|
v.ip++
|
2019-01-09 10:17:42 +03:00
|
|
|
|
2019-01-20 18:41:57 +03:00
|
|
|
sp := v.curFrame.basePointer + localIndex
|
2019-01-11 09:34:28 +03: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-01-14 15:32:09 +03:00
|
|
|
val := v.stack[v.sp-1]
|
|
|
|
v.sp--
|
|
|
|
|
2019-03-14 11:07:19 +03:00
|
|
|
if obj, ok := v.stack[sp].(*objects.ObjectPtr); ok {
|
|
|
|
*obj.Value = val
|
|
|
|
val = obj
|
|
|
|
}
|
|
|
|
v.stack[sp] = val // also use a copy of popped value
|
2019-01-09 10:17:42 +03:00
|
|
|
|
|
|
|
case compiler.OpSetSelLocal:
|
2019-01-20 18:41:57 +03:00
|
|
|
localIndex := int(v.curInsts[v.ip+1])
|
|
|
|
numSelectors := int(v.curInsts[v.ip+2])
|
|
|
|
v.ip += 2
|
2019-01-09 10:17:42 +03:00
|
|
|
|
2019-01-21 10:32:58 +03:00
|
|
|
// selectors and RHS value
|
2019-03-14 11:07:19 +03:00
|
|
|
selectors := make([]objects.Object, numSelectors)
|
|
|
|
for i := 0; i < numSelectors; i++ {
|
|
|
|
selectors[i] = v.stack[v.sp-numSelectors+i]
|
|
|
|
}
|
|
|
|
|
2019-01-21 10:32:58 +03:00
|
|
|
val := v.stack[v.sp-numSelectors-1]
|
|
|
|
v.sp -= numSelectors + 1
|
2019-01-09 10:17:42 +03:00
|
|
|
|
2019-01-20 18:41:57 +03:00
|
|
|
sp := v.curFrame.basePointer + localIndex
|
2019-01-09 10:17:42 +03:00
|
|
|
|
2019-02-21 20:40:17 +03:00
|
|
|
if e := indexAssign(v.stack[sp], val, selectors); e != nil {
|
2019-03-06 00:53:14 +03:00
|
|
|
v.err = e
|
|
|
|
return
|
2019-01-09 10:17:42 +03:00
|
|
|
}
|
2019-01-14 15:32:09 +03:00
|
|
|
|
2019-01-09 10:17:42 +03:00
|
|
|
case compiler.OpGetLocal:
|
2019-01-20 18:41:57 +03:00
|
|
|
v.ip++
|
2019-03-22 14:42:34 +03:00
|
|
|
localIndex := int(v.curInsts[v.ip])
|
2019-01-09 10:17:42 +03:00
|
|
|
|
2019-01-20 18:41:57 +03:00
|
|
|
val := v.stack[v.curFrame.basePointer+localIndex]
|
2019-01-09 10:17:42 +03:00
|
|
|
|
2019-03-14 11:07:19 +03:00
|
|
|
if obj, ok := val.(*objects.ObjectPtr); ok {
|
|
|
|
val = *obj.Value
|
|
|
|
}
|
|
|
|
|
2019-01-14 15:32:09 +03:00
|
|
|
v.stack[v.sp] = val
|
|
|
|
v.sp++
|
|
|
|
|
2019-01-09 10:17:42 +03:00
|
|
|
case compiler.OpGetBuiltin:
|
2019-01-20 18:41:57 +03:00
|
|
|
v.ip++
|
2019-03-22 14:42:34 +03:00
|
|
|
builtinIndex := int(v.curInsts[v.ip])
|
2019-01-14 15:32:09 +03:00
|
|
|
|
2019-03-18 18:15:26 +03:00
|
|
|
v.stack[v.sp] = objects.Builtins[builtinIndex]
|
2019-02-21 03:26:11 +03:00
|
|
|
v.sp++
|
|
|
|
|
2019-01-09 10:17:42 +03:00
|
|
|
case compiler.OpClosure:
|
2019-01-20 18:41:57 +03:00
|
|
|
v.ip += 3
|
2019-03-22 14:42:34 +03:00
|
|
|
constIndex := int(v.curInsts[v.ip-1]) | int(v.curInsts[v.ip-2])<<8
|
|
|
|
numFree := int(v.curInsts[v.ip])
|
2019-01-09 10:17:42 +03:00
|
|
|
|
2019-02-21 03:26:11 +03:00
|
|
|
fn, ok := v.constants[constIndex].(*objects.CompiledFunction)
|
|
|
|
if !ok {
|
2019-03-06 00:53:14 +03:00
|
|
|
v.err = fmt.Errorf("not function: %s", fn.TypeName())
|
|
|
|
return
|
2019-02-21 03:26:11 +03:00
|
|
|
}
|
|
|
|
|
2019-03-14 11:07:19 +03:00
|
|
|
free := make([]*objects.ObjectPtr, numFree)
|
2019-02-21 03:26:11 +03:00
|
|
|
for i := 0; i < numFree; i++ {
|
2019-03-14 11:07:19 +03:00
|
|
|
switch freeVar := (v.stack[v.sp-numFree+i]).(type) {
|
|
|
|
case *objects.ObjectPtr:
|
|
|
|
free[i] = freeVar
|
|
|
|
default:
|
|
|
|
free[i] = &objects.ObjectPtr{Value: &v.stack[v.sp-numFree+i]}
|
|
|
|
}
|
2019-02-21 03:26:11 +03:00
|
|
|
}
|
2019-03-14 11:07:19 +03:00
|
|
|
|
2019-02-21 03:26:11 +03:00
|
|
|
v.sp -= numFree
|
|
|
|
|
2019-03-14 11:07:19 +03:00
|
|
|
var cl = &objects.Closure{
|
2019-02-21 03:26:11 +03:00
|
|
|
Fn: fn,
|
|
|
|
Free: free,
|
2019-01-09 10:17:42 +03:00
|
|
|
}
|
2019-01-14 15:32:09 +03:00
|
|
|
|
2019-03-07 04:20:05 +03:00
|
|
|
v.allocs--
|
|
|
|
if v.allocs == 0 {
|
|
|
|
v.err = ErrObjectAllocLimit
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-03-14 11:07:19 +03:00
|
|
|
v.stack[v.sp] = cl
|
2019-02-21 03:26:11 +03:00
|
|
|
v.sp++
|
|
|
|
|
2019-03-14 11:07:19 +03:00
|
|
|
case compiler.OpGetFreePtr:
|
2019-01-20 18:41:57 +03:00
|
|
|
v.ip++
|
2019-03-22 14:42:34 +03:00
|
|
|
freeIndex := int(v.curInsts[v.ip])
|
2019-01-09 10:17:42 +03:00
|
|
|
|
2019-01-14 02:21:19 +03:00
|
|
|
val := v.curFrame.freeVars[freeIndex]
|
2019-01-09 10:17:42 +03:00
|
|
|
|
2019-01-14 15:32:09 +03:00
|
|
|
v.stack[v.sp] = val
|
|
|
|
v.sp++
|
|
|
|
|
2019-03-14 11:07:19 +03:00
|
|
|
case compiler.OpGetFree:
|
|
|
|
v.ip++
|
2019-03-22 14:42:34 +03:00
|
|
|
freeIndex := int(v.curInsts[v.ip])
|
2019-03-14 11:07:19 +03:00
|
|
|
|
|
|
|
val := *v.curFrame.freeVars[freeIndex].Value
|
|
|
|
|
|
|
|
v.stack[v.sp] = val
|
|
|
|
v.sp++
|
|
|
|
|
|
|
|
case compiler.OpSetFree:
|
|
|
|
v.ip++
|
2019-03-22 14:42:34 +03:00
|
|
|
freeIndex := int(v.curInsts[v.ip])
|
2019-03-14 11:07:19 +03:00
|
|
|
|
|
|
|
*v.curFrame.freeVars[freeIndex].Value = v.stack[v.sp-1]
|
|
|
|
|
|
|
|
v.sp--
|
|
|
|
|
|
|
|
case compiler.OpGetLocalPtr:
|
|
|
|
v.ip++
|
2019-03-22 14:42:34 +03:00
|
|
|
localIndex := int(v.curInsts[v.ip])
|
2019-03-14 11:07:19 +03:00
|
|
|
|
|
|
|
sp := v.curFrame.basePointer + localIndex
|
|
|
|
val := v.stack[sp]
|
|
|
|
|
|
|
|
var freeVar *objects.ObjectPtr
|
|
|
|
if obj, ok := val.(*objects.ObjectPtr); ok {
|
|
|
|
freeVar = obj
|
|
|
|
} else {
|
|
|
|
freeVar = &objects.ObjectPtr{Value: &val}
|
|
|
|
v.stack[sp] = freeVar
|
|
|
|
}
|
|
|
|
|
|
|
|
v.stack[v.sp] = freeVar
|
|
|
|
v.sp++
|
|
|
|
|
2019-01-09 10:17:42 +03:00
|
|
|
case compiler.OpSetSelFree:
|
2019-01-20 18:41:57 +03:00
|
|
|
v.ip += 2
|
2019-03-22 14:42:34 +03:00
|
|
|
freeIndex := int(v.curInsts[v.ip-1])
|
|
|
|
numSelectors := int(v.curInsts[v.ip])
|
2019-01-09 10:17:42 +03:00
|
|
|
|
2019-01-21 10:32:58 +03:00
|
|
|
// selectors and RHS value
|
2019-03-14 11:07:19 +03:00
|
|
|
selectors := make([]objects.Object, numSelectors)
|
|
|
|
for i := 0; i < numSelectors; i++ {
|
|
|
|
selectors[i] = v.stack[v.sp-numSelectors+i]
|
|
|
|
}
|
2019-01-21 10:32:58 +03:00
|
|
|
val := v.stack[v.sp-numSelectors-1]
|
|
|
|
v.sp -= numSelectors + 1
|
2019-01-09 10:17:42 +03:00
|
|
|
|
2019-03-14 11:07:19 +03:00
|
|
|
if e := indexAssign(*v.curFrame.freeVars[freeIndex].Value, val, selectors); e != nil {
|
2019-03-06 00:53:14 +03:00
|
|
|
v.err = e
|
|
|
|
return
|
2019-01-09 10:17:42 +03:00
|
|
|
}
|
2019-01-14 15:32:09 +03:00
|
|
|
|
2019-01-09 10:17:42 +03:00
|
|
|
case compiler.OpIteratorInit:
|
|
|
|
var iterator objects.Object
|
|
|
|
|
2019-01-14 15:32:09 +03:00
|
|
|
dst := v.stack[v.sp-1]
|
|
|
|
v.sp--
|
|
|
|
|
2019-03-14 11:07:19 +03:00
|
|
|
iterable, ok := dst.(objects.Iterable)
|
2019-01-24 00:36:03 +03:00
|
|
|
if !ok {
|
2019-03-14 11:07:19 +03:00
|
|
|
v.err = fmt.Errorf("not iterable: %s", dst.TypeName())
|
2019-03-06 00:53:14 +03:00
|
|
|
return
|
2019-01-09 10:17:42 +03:00
|
|
|
}
|
|
|
|
|
2019-01-24 00:36:03 +03:00
|
|
|
iterator = iterable.Iterate()
|
|
|
|
|
2019-03-07 04:20:05 +03:00
|
|
|
v.allocs--
|
|
|
|
if v.allocs == 0 {
|
|
|
|
v.err = ErrObjectAllocLimit
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-03-14 11:07:19 +03:00
|
|
|
v.stack[v.sp] = iterator
|
2019-01-14 15:32:09 +03:00
|
|
|
v.sp++
|
|
|
|
|
2019-01-09 10:17:42 +03:00
|
|
|
case compiler.OpIteratorNext:
|
2019-01-14 15:32:09 +03:00
|
|
|
iterator := v.stack[v.sp-1]
|
|
|
|
v.sp--
|
|
|
|
|
2019-03-14 11:07:19 +03:00
|
|
|
hasMore := iterator.(objects.Iterator).Next()
|
2019-01-24 00:36:03 +03:00
|
|
|
|
|
|
|
if hasMore {
|
2019-03-14 11:07:19 +03:00
|
|
|
v.stack[v.sp] = objects.TrueValue
|
2019-01-09 10:17:42 +03:00
|
|
|
} else {
|
2019-03-14 11:07:19 +03:00
|
|
|
v.stack[v.sp] = objects.FalseValue
|
2019-01-09 10:17:42 +03:00
|
|
|
}
|
2019-01-14 15:32:09 +03:00
|
|
|
v.sp++
|
|
|
|
|
2019-01-09 10:17:42 +03:00
|
|
|
case compiler.OpIteratorKey:
|
2019-01-14 15:32:09 +03:00
|
|
|
iterator := v.stack[v.sp-1]
|
|
|
|
v.sp--
|
|
|
|
|
2019-03-14 11:07:19 +03:00
|
|
|
val := iterator.(objects.Iterator).Key()
|
2019-01-09 10:17:42 +03:00
|
|
|
|
2019-03-14 11:07:19 +03:00
|
|
|
v.stack[v.sp] = val
|
2019-01-14 15:32:09 +03:00
|
|
|
v.sp++
|
|
|
|
|
2019-01-09 10:17:42 +03:00
|
|
|
case compiler.OpIteratorValue:
|
2019-01-14 15:32:09 +03:00
|
|
|
iterator := v.stack[v.sp-1]
|
|
|
|
v.sp--
|
|
|
|
|
2019-03-14 11:07:19 +03:00
|
|
|
val := iterator.(objects.Iterator).Value()
|
2019-01-09 10:17:42 +03:00
|
|
|
|
2019-03-14 11:07:19 +03:00
|
|
|
v.stack[v.sp] = val
|
2019-01-14 15:32:09 +03:00
|
|
|
v.sp++
|
|
|
|
|
2019-01-09 10:17:42 +03:00
|
|
|
default:
|
2019-03-22 14:42:34 +03:00
|
|
|
v.err = fmt.Errorf("unknown opcode: %d", v.curInsts[v.ip])
|
|
|
|
return
|
2019-01-09 10:17:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-01 09:37:30 +03:00
|
|
|
// IsStackEmpty tests if the stack is empty or not.
|
|
|
|
func (v *VM) IsStackEmpty() bool {
|
|
|
|
return v.sp == 0
|
2019-01-09 23:39:40 +03:00
|
|
|
}
|
|
|
|
|
2019-03-14 11:07:19 +03:00
|
|
|
func indexAssign(dst, src objects.Object, selectors []objects.Object) error {
|
2019-01-09 10:17:42 +03:00
|
|
|
numSel := len(selectors)
|
|
|
|
|
2019-01-21 10:32:58 +03:00
|
|
|
for sidx := numSel - 1; sidx > 0; sidx-- {
|
2019-03-14 11:07:19 +03:00
|
|
|
indexable, ok := dst.(objects.Indexable)
|
2019-01-21 10:32:58 +03:00
|
|
|
if !ok {
|
2019-03-14 11:07:19 +03:00
|
|
|
return fmt.Errorf("not indexable: %s", dst.TypeName())
|
2019-01-21 10:32:58 +03:00
|
|
|
}
|
2019-01-09 10:17:42 +03:00
|
|
|
|
2019-03-14 11:07:19 +03:00
|
|
|
next, err := indexable.IndexGet(selectors[sidx])
|
2019-01-21 10:32:58 +03:00
|
|
|
if err != nil {
|
2019-02-21 03:26:11 +03:00
|
|
|
if err == objects.ErrInvalidIndexType {
|
2019-03-14 11:07:19 +03:00
|
|
|
return fmt.Errorf("invalid index type: %s", selectors[sidx].TypeName())
|
2019-02-21 03:26:11 +03:00
|
|
|
}
|
|
|
|
|
2019-01-21 10:32:58 +03:00
|
|
|
return err
|
|
|
|
}
|
2019-01-09 10:17:42 +03:00
|
|
|
|
2019-03-14 11:07:19 +03:00
|
|
|
dst = next
|
2019-01-21 10:32:58 +03:00
|
|
|
}
|
2019-01-09 10:17:42 +03:00
|
|
|
|
2019-03-14 11:07:19 +03:00
|
|
|
indexAssignable, ok := dst.(objects.IndexAssignable)
|
2019-01-21 10:32:58 +03:00
|
|
|
if !ok {
|
2019-03-14 11:07:19 +03:00
|
|
|
return fmt.Errorf("not index-assignable: %s", dst.TypeName())
|
2019-01-09 10:17:42 +03:00
|
|
|
}
|
|
|
|
|
2019-03-14 11:07:19 +03:00
|
|
|
if err := indexAssignable.IndexSet(selectors[0], src); err != nil {
|
2019-02-21 03:26:11 +03:00
|
|
|
if err == objects.ErrInvalidIndexValueType {
|
2019-03-14 11:07:19 +03:00
|
|
|
return fmt.Errorf("invaid index value type: %s", src.TypeName())
|
2019-02-21 03:26:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2019-01-09 10:17:42 +03:00
|
|
|
}
|