starlight-go / starlight

a go wrapper for google's starlark embedded python language
MIT License
300 stars 29 forks source link

Panic in makeStarFn #15

Open jazzy-crane opened 5 years ago

jazzy-crane commented 5 years ago

Some toy code that reproduces this:

package main

import (
    "fmt"
    "github.com/starlight-go/starlight"
)

func main() {
    env := map[string]interface{}{"my_callout": my_callout}

    _, err := starlight.Eval([]byte(`
my_callout("Hello World!") # this is fine
my_callout(True) # this panics
`), env, nil)

    if err != nil {
        fmt.Println("Error executing starlight script", err)
        return
    }
}

func my_callout(s string) {
    fmt.Println("my_callout:", s)
}

results in:

my_callout: Hello World!
panic: reflect.Value.Convert: value of type bool cannot be converted to type string

goroutine 1 [running]:
reflect.Value.Convert(0x58c060, 0x725cc1, 0x81, 0x608620, 0x58dde0, 0x1, 0x0, 0x0)
        C:/Go/src/reflect/value.go:2351 +0x22b
vendor/github.com/starlight-go/starlight/convert.makeStarFn.func1(0xc000076a20, 0xc0000769f0, 0xc000050390, 0x1, 0x1, 0x0, 0x0, 0x0, 0x50, 0x5b4540, ...)
        vendor/github.com/starlight-go/starlight/convert/conv.go:294 +0x469
vendor/go.starlark.net/starlark.(*Builtin).CallInternal(0xc0000769f0, 0xc000076a20, 0xc000050390, 0x1, 0x1, 0x0, 0x0, 0x0, 0x606600, 0x725cc0, ...)
        vendor/go.starlark.net/starlark/value.go:585 +0x92
vendor/go.starlark.net/starlark.Call(0xc000076a20, 0x606400, 0xc0000769f0, 0xc000050390, 0x1, 0x1, 0x0, 0x0, 0x0, 0x606600, ...)
        vendor/go.starlark.net/starlark/eval.go:910 +0x142
vendor/go.starlark.net/starlark.call(0xc000076a20, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc0000aa000, 0xc000087a9c, 0xc000087bb0, ...)
        vendor/go.starlark.net/starlark/interp.go:292 +0x4121
vendor/go.starlark.net/starlark.(*Function).CallInternal(0xc0000aa000, 0xc000076a20, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x5b7060, 0xc0000aa000, ...)
        vendor/go.starlark.net/starlark/interp.go:39 +0x1c8
vendor/go.starlark.net/starlark.Call(0xc000076a20, 0x606480, 0xc0000aa000, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, ...)
        vendor/go.starlark.net/starlark/eval.go:910 +0x142
vendor/go.starlark.net/starlark.(*Program).Init(0xc00008a020, 0xc000076a20, 0xc0000769c0, 0xc00005a460, 0xc000050220, 0xc0000a2060)
        vendor/go.starlark.net/starlark/eval.go:314 +0xa1
vendor/go.starlark.net/starlark.ExecFile(0xc000076a20, 0x5c952d, 0x8, 0x58b8a0, 0xc00005a460, 0xc0000769c0, 0x20, 0x598900, 0x30001)
        vendor/go.starlark.net/starlark/eval.go:258 +0xf4
vendor/github.com/starlight-go/starlight.Eval(0x58b8a0, 0xc00005a460, 0xc000087e48, 0x0, 0xc000087f00, 0x40b643, 0xc00005c410)
        vendor/github.com/starlight-go/starlight/starlight.go:41 +0x161
main.main()
        starlight-panic/main.go:11 +0x16f

I suspect this is easily fixed by checking !val.Type().ConvertibleTo(argT) and returning error if it's not convertible

jazzy-crane commented 5 years ago

same applies for makeVariadicStarFn

jazzy-crane commented 5 years ago

Have opened a PR - https://github.com/starlight-go/starlight/pull/16