console: handle undefined + null in console funcs (#21160)

This commit is contained in:
Guillaume Ballet
2020-06-02 18:06:22 +02:00
committed by GitHub
parent d98c42c0e3
commit 5f6f5e345e
2 changed files with 58 additions and 8 deletions

View File

@ -233,11 +233,12 @@ func (b *bridge) UnlockAccount(call jsre.Call) (goja.Value, error) {
if len(call.Arguments) < 1 {
return nil, fmt.Errorf("usage: unlockAccount(account, [ password, duration ])")
}
account := call.Argument(0)
// Make sure we have an account specified to unlock.
if call.Argument(0).ExportType().Kind() != reflect.String {
if goja.IsUndefined(account) || goja.IsNull(account) || account.ExportType().Kind() != reflect.String {
return nil, fmt.Errorf("first argument must be the account to unlock")
}
account := call.Argument(0)
// If password is not given or is the null value, prompt the user for it.
var passwd goja.Value
@ -285,10 +286,10 @@ func (b *bridge) Sign(call jsre.Call) (goja.Value, error) {
passwd = call.Argument(2)
)
if message.ExportType().Kind() != reflect.String {
if goja.IsUndefined(message) || message.ExportType().Kind() != reflect.String {
return nil, fmt.Errorf("first argument must be the message to sign")
}
if account.ExportType().Kind() != reflect.String {
if goja.IsUndefined(account) || account.ExportType().Kind() != reflect.String {
return nil, fmt.Errorf("second argument must be the account to sign with")
}
@ -317,10 +318,11 @@ func (b *bridge) Sleep(call jsre.Call) (goja.Value, error) {
if nArgs := len(call.Arguments); nArgs < 1 {
return nil, fmt.Errorf("usage: sleep(<number of seconds>)")
}
if !isNumber(call.Argument(0)) {
sleepObj := call.Argument(0)
if goja.IsUndefined(sleepObj) || goja.IsNull(sleepObj) || !isNumber(sleepObj) {
return nil, fmt.Errorf("usage: sleep(<number of seconds>)")
}
sleep := call.Argument(0).ToFloat()
sleep := sleepObj.ToFloat()
time.Sleep(time.Duration(sleep * float64(time.Second)))
return call.VM.ToValue(true), nil
}
@ -338,13 +340,13 @@ func (b *bridge) SleepBlocks(call jsre.Call) (goja.Value, error) {
return nil, fmt.Errorf("usage: sleepBlocks(<n blocks>[, max sleep in seconds])")
}
if nArgs >= 1 {
if !isNumber(call.Argument(0)) {
if goja.IsNull(call.Argument(0)) || goja.IsUndefined(call.Argument(0)) || !isNumber(call.Argument(0)) {
return nil, fmt.Errorf("expected number as first argument")
}
blocks = call.Argument(0).ToInteger()
}
if nArgs >= 2 {
if !isNumber(call.Argument(1)) {
if goja.IsNull(call.Argument(1)) || goja.IsUndefined(call.Argument(1)) || !isNumber(call.Argument(1)) {
return nil, fmt.Errorf("expected number as second argument")
}
sleep = call.Argument(1).ToInteger()