-
Notifications
You must be signed in to change notification settings - Fork 1
/
stack_table.go
61 lines (50 loc) · 1.4 KB
/
stack_table.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package mm
import (
"fmt"
"github.com/assetcloud/plugin/plugin/dapp/evm/executor/vm/params"
)
type (
// StackValidationFunc 校验栈中数据是否满足计算要求
StackValidationFunc func(*Stack) error
)
// MakeStackFunc 栈校验的通用逻辑封装(主要就是检查栈的深度和空间是否够用)
func MakeStackFunc(pop, push int) StackValidationFunc {
return func(stack *Stack) error {
if err := stack.Require(pop); err != nil {
return err
}
if stack.Len()+push-pop > int(params.StackLimit) {
return fmt.Errorf("stack limit reached %d (%d)", stack.Len(), params.StackLimit)
}
return nil
}
}
// MakeDupStackFunc 创建栈大小计算方法对象
func MakeDupStackFunc(n int) StackValidationFunc {
return MakeStackFunc(n, n+1)
}
// MakeSwapStackFunc 创建栈大小计算方法对象
func MakeSwapStackFunc(n int) StackValidationFunc {
return MakeStackFunc(n, n)
}
func minSwapStack(n int) int {
return minStack(n, n)
}
func maxSwapStack(n int) int {
return maxStack(n, n)
}
func minDupStack(n int) int {
return minStack(n, n+1)
}
func maxDupStack(n int) int {
return maxStack(n, n+1)
}
func maxStack(pop, push int) int {
return int(params.StackLimit) + pop - push
}
func minStack(pops, push int) int {
return pops
}