@@ -24,7 +24,6 @@ import (
24
24
"fmt"
25
25
"math/big"
26
26
"strings"
27
- "sync"
28
27
29
28
"github.com/ethereum/evmc/bindings/go/evmc"
30
29
"github.com/ethereum/go-ethereum/common"
@@ -42,77 +41,51 @@ type EVMC struct {
42
41
readOnly bool // The readOnly flag (TODO: Try to get rid of it).
43
42
}
44
43
45
- // EVMCModule represents the loaded EVMC module.
46
- type EVMCModule struct {
47
- instance * evmc.Instance // The EVMC VM instance.
48
- once sync.Once // The mutex protecting EVMC VM instance creation.
49
- }
50
-
51
44
var (
52
- evmModule EVMCModule
53
- ewasmModule EVMCModule
45
+ evmModule * evmc. Instance
46
+ ewasmModule * evmc. Instance
54
47
)
55
48
56
- // InitEVMC initializes the EVMC modules.
57
- func InitEVMC (evmConfig string , ewasmConfig string ) {
58
- if evmConfig != "" {
59
- initEVMC (& evmModule , evmc .CapabilityEVM1 , evmConfig )
60
- }
61
- if ewasmConfig != "" {
62
- initEVMC (& ewasmModule , evmc .CapabilityEWASM , ewasmConfig )
63
- }
49
+ func InitEVMCEVM (config string ) {
50
+ evmModule = initEVMC (evmc .CapabilityEVM1 , config )
64
51
}
65
52
66
- func initEVMC (module * EVMCModule , cap evmc.Capability , config string ) {
67
- module .once .Do (func () {
68
- options := strings .Split (config , "," )
69
- path := options [0 ]
53
+ func InitEVMCEwasm (config string ) {
54
+ ewasmModule = initEVMC (evmc .CapabilityEWASM , config )
55
+ }
70
56
71
- if path == "" {
72
- panic ( "EVMC VM path not provided, set --vm.(evm|ewasm)=/path/to/vm " )
73
- }
57
+ func initEVMC ( cap evmc. Capability , config string ) * evmc. Instance {
58
+ options := strings . Split ( config , ", " )
59
+ path := options [ 0 ]
74
60
75
- var err error
76
- module .instance , err = evmc .Load (path )
77
- if err != nil {
78
- panic (err .Error ())
79
- }
80
- log .Info ("EVMC VM loaded" , "name" , module .instance .Name (), "version" , module .instance .Version (), "path" , path )
81
-
82
- // Set options before checking capabilities.
83
- for _ , option := range options [1 :] {
84
- if idx := strings .Index (option , "=" ); idx >= 0 {
85
- name := option [:idx ]
86
- value := option [idx + 1 :]
87
- err := module .instance .SetOption (name , value )
88
- if err == nil {
89
- log .Info ("EVMC VM option set" , "name" , name , "value" , value )
90
- } else {
91
- log .Warn ("EVMC VM option setting failed" , "name" , name , "error" , err )
92
- }
93
- }
94
- }
61
+ if path == "" {
62
+ panic ("EVMC VM path not provided, set --vm.(evm|ewasm)=/path/to/vm" )
63
+ }
95
64
96
- if ! module .instance .HasCapability (cap ) {
97
- panic (fmt .Errorf ("The EVMC module %s does not have requested capability %d" , path , cap ))
65
+ instance , err := evmc .Load (path )
66
+ if err != nil {
67
+ panic (err .Error ())
68
+ }
69
+ log .Info ("EVMC VM loaded" , "name" , instance .Name (), "version" , instance .Version (), "path" , path )
70
+
71
+ // Set options before checking capabilities.
72
+ for _ , option := range options [1 :] {
73
+ if idx := strings .Index (option , "=" ); idx >= 0 {
74
+ name := option [:idx ]
75
+ value := option [idx + 1 :]
76
+ err := instance .SetOption (name , value )
77
+ if err == nil {
78
+ log .Info ("EVMC VM option set" , "name" , name , "value" , value )
79
+ } else {
80
+ log .Warn ("EVMC VM option setting failed" , "name" , name , "error" , err )
81
+ }
98
82
}
99
- })
100
- }
101
-
102
- // NewEVMC creates new EVMC-based VM execution context.
103
- func NewEVMC (cap evmc.Capability , config string , env * EVM ) * EVMC {
104
- var module * EVMCModule
105
-
106
- switch cap {
107
- case evmc .CapabilityEVM1 :
108
- module = & evmModule
109
- case evmc .CapabilityEWASM :
110
- module = & ewasmModule
111
- default :
112
- panic (fmt .Errorf ("EVMC: Unknown capability %d" , cap ))
113
83
}
114
84
115
- return & EVMC {module .instance , env , cap , false }
85
+ if ! instance .HasCapability (cap ) {
86
+ panic (fmt .Errorf ("The EVMC module %s does not have requested capability %d" , path , cap ))
87
+ }
88
+ return instance
116
89
}
117
90
118
91
// hostContext implements evmc.HostContext interface.
0 commit comments