Files
gocryptotrader/gctscript/vm/gctscript.go
Ryan O'Hara-Reid e93ee83563 script: implementation of error insertion on return (#986)
* exchanges/account: shift credentials to account package and segregate funds to keys

* merge: fixes

* linter: fix

* Update exchanges/account/account.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* glorious: nits + protection for string panic

* glorious_suggestion: add method for matching keys

* linter: fix tests

* account: add protected method for credentials minimizing access, display full account details to rpc.

* linter: spelling kweeeeeeen

* accounts/portfolio: clean/check portfolio code and quickly check balances from change. Add protected method for future matching.

* accounts: theres no point in pointerising everything

* linter: ok pointerise this then...

* exchanges: fix regression add in little notes.

* glorious: nits

* Update exchanges/account/credentials.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* Update exchanges/account/credentials_test.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* Update exchanges/account/credentials_test.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* glorious: nits

* gloriously: fix glorious glorious test gloriously

* script: initial implementation of error insertion on return

* script: make script context aware(ish) and update error handle in examples

* script: add tests

* script: add syntax highlighting to readme

* Update gctscript/vm/vm.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* Update gctscript/vm/vm.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* Update gctscript/examples/exchange/account_info.gct

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* Update gctscript/examples/exchange/cancel_order.gct

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* Update gctscript/examples/verbose.gct

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* Update gctscript/modules/gct/gct_test.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* glorious: nits

* rm: bros

* scripts: handle errors in examples when they are going to use the data after fetching

* linter: fix rides again

* SCOTT_SPELL_CHECK_LINTER: fix

* gctscript: fix tests

* glorious: niiiiiiiiiiiiits

* scriptmodules/gct: standardize runtime errors

* Update gctscript/modules/gct/exchange.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* Update gctscript/modules/gct/exchange.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* Update gctscript/modules/gct/exchange.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* Update gctscript/modules/gct/exchange.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* Update gctscript/modules/gct/gct.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* Update gctscript/modules/gct/gct.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* Update gctscript/modules/gct/gct.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* Update gctscript/modules/gct/gct.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* Update gctscript/modules/gct/gct.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* Update gctscript/modules/gct/gct.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* Update gctscript/modules/gct/gct.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* glorious: nits/reverts

* go mod: tidy

Co-authored-by: Ryan O'Hara-Reid <ryan.oharareid@thrasher.io>
Co-authored-by: Scott <gloriousCode@users.noreply.github.com>
2022-08-17 14:18:53 +10:00

88 lines
2.0 KiB
Go

package vm
import (
"errors"
"fmt"
"github.com/gofrs/uuid"
"github.com/thrasher-corp/gocryptotrader/gctscript/wrappers/validator"
"github.com/thrasher-corp/gocryptotrader/log"
)
// New returns a new instance of VM
func (g *GctScriptManager) New() *VM {
if VMSCount.Len() >= int32(g.GetMaxVirtualMachines()) {
if g.config.Verbose {
log.Warnf(log.GCTScriptMgr, "GCTScript MaxVirtualMachines (%v) hit, unable to start further instances",
g.GetMaxVirtualMachines())
}
return nil
}
VMSCount.add()
vm := g.NewVM()
if vm == nil {
VMSCount.remove()
} else {
AllVMSync.Store(vm.ID, vm)
}
return vm
}
// Validate will attempt to execute a script in a test/non-live environment
// to confirm it passes requirements for execution
func (g *GctScriptManager) Validate(file string) (err error) {
validator.IsTestExecution.Store(true)
defer validator.IsTestExecution.Store(false)
tempVM := g.NewVM()
err = tempVM.Load(file)
if err != nil {
return
}
err = tempVM.Compile()
if err != nil {
return
}
return tempVM.RunCtx()
}
// ShutdownAll shutdown all
func (g *GctScriptManager) ShutdownAll() (err error) {
if g.config.Verbose {
log.Debugln(log.GCTScriptMgr, "Shutting down all Virtual Machines")
}
var shutdownErrors []error
AllVMSync.Range(func(_, v interface{}) bool {
vm, ok := v.(*VM)
if !ok {
shutdownErrors = append(shutdownErrors, errors.New("unable to type assert VM"))
return true
}
errShutdown := vm.Shutdown()
if err != nil {
shutdownErrors = append(shutdownErrors, errShutdown)
}
return true
})
if len(shutdownErrors) > 0 {
err = fmt.Errorf("failed to shutdown the following Virtual Machines: %v", shutdownErrors)
}
return err
}
// RemoveVM remove VM from list
func (g *GctScriptManager) RemoveVM(id uuid.UUID) error {
if _, ok := AllVMSync.Load(id); !ok {
return fmt.Errorf(ErrNoVMFound, id.String())
}
AllVMSync.Delete(id)
VMSCount.remove()
if g.config.Verbose {
log.Debugf(log.GCTScriptMgr, "VM %v removed from AllVMs", id)
}
return nil
}