Repository: goplus/gop
Pull request #466 (2020-07-16 10:45:15)

fix  #465
map bug - get value if not exists will panic

codecov-commenter: # [Codecov](https://codecov.io/gh/goplus/gop/pull/466?src=pr&el=h1) Report
> Merging [#466](https://codecov.io/gh/goplus/gop/pull/466?src=pr&el=desc) into [master](https://codecov.io/gh/goplus/gop/commit/fb040431d19dedc90a7d2bc674de2049b92c3ed6&el=desc) will **increase** coverage by `0.00%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/goplus/gop/pull/466/graphs/tree.svg?width=650&height=150&src=pr&token=4qln9Wk3Pn)](https://codecov.io/gh/goplus/gop/pull/466?src=pr&el=tree)

```diff
@@           Coverage Diff           @@
##           master     #466   +/-   ##
=======================================
  Coverage   88.71%   88.71%           
=======================================
  Files          37       37           
  Lines        9456     9459    +3     
=======================================
+ Hits         8389     8392    +3     
  Misses        790      790           
  Partials      277      277           
```


| [Impacted Files](https://codecov.io/gh/goplus/gop/pull/466?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [exec/bytecode/goinstr.go](https://codecov.io/gh/goplus/gop/pull/466/diff?src=pr&el=tree#diff-ZXhlYy9ieXRlY29kZS9nb2luc3RyLmdv) | `94.47% <100.00%> (+0.04%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/goplus/gop/pull/466?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/goplus/gop/pull/466?src=pr&el=footer). Last update [fb04043...a84cd55](https://codecov.io/gh/goplus/gop/pull/466?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/exec/bytecode/goinstr.go b/exec/bytecode/goinstr.go
index 57ac6557..44b49039 100644
--- a/exec/bytecode/goinstr.go
+++ b/exec/bytecode/goinstr.go
@@ -247,7 +247,11 @@ func execMapIndex(i Instr, p *Context) {
 		v.SetMapIndex(key, reflect.ValueOf(p.data[n-3]))
 		p.PopN(3)
 	default: // mapData $key $mapIndex
-		p.Ret(2, v.MapIndex(key).Interface())
+		value := v.MapIndex(key)
+		if !value.IsValid() {
+			value = reflect.Zero(v.Type().Elem())
+		}
+		p.Ret(2, value.Interface())
 	}
 }
 


Test patch:
diff --git a/exec/bytecode/goinstr_test.go b/exec/bytecode/goinstr_test.go
index f6d3ea97..52400c16 100644
--- a/exec/bytecode/goinstr_test.go
+++ b/exec/bytecode/goinstr_test.go
@@ -75,21 +75,31 @@ func TestMap(t *testing.T) {
 }
 
 func TestMapIndex(t *testing.T) {
+	m := NewVar(reflect.MapOf(TyString, TyFloat64), "")
 	code := newBuilder().
+		DefineVar(m).
 		Push("Hello").
 		Push(3.2).
 		Push("xsw").
 		Push(1.0).
 		MakeMap(reflect.MapOf(TyString, TyFloat64), 2).
+		StoreVar(m).
+		LoadVar(m).
+		Push("go+").
+		MapIndex().
+		LoadVar(m).
 		Push("xsw").
 		MapIndex().
 		Resolve()
 
 	ctx := NewContext(code)
 	ctx.Exec(0, code.Len())
-	if v := checkPop(ctx); v != 1.0 {
+	if v := ctx.Get(-1); v != 1.0 {
 		t.Fatal("{`Hello`: 3.2, `xsw`: 1.0}[`xsw`] != 1.0, ret =", v)
 	}
+	if v := ctx.Get(-2); v != 0.0 {
+		t.Fatal("{`Hello`: 3.2, `xsw`: 1.0}[`go+`] != 1.0, ret =", v)
+	}
 }
 
 func TestSetMapIndex(t *testing.T) {
diff --git a/exec/golang/testdata/05.maplit/maplit.gop b/exec/golang/testdata/05.maplit/maplit.gop
index 67b120a4..a72c6c3c 100644
--- a/exec/golang/testdata/05.maplit/maplit.gop
+++ b/exec/golang/testdata/05.maplit/maplit.gop
@@ -4,6 +4,7 @@ delete(x, "Hello")
 println("x:", x)
 delete(x, "hi")
 println("x:", x)
+println(x["Hello"])
 
 y := {"Hello": 1, "xsw": "Go+"} // map[string]interface{}
 println("y:", y)