Skip to content
Permalink
Browse files

reflect: Reimplement mapiter functions

  • Loading branch information...
hajimehoshi committed Mar 5, 2019
1 parent 35239cf commit 2ea661bd5f0f7513a6da8500975d502cd5f7ad8b
Showing with 36 additions and 19 deletions.
  1. +1 −1 compiler/gopherjspkg/fs_vfsdata.go
  2. +5 −5 compiler/natives/fs_vfsdata.go
  3. +15 −13 compiler/natives/src/reflect/reflect.go
  4. +15 −0 tests/misc_test.go

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -553,6 +553,17 @@ type mapIter struct {
last *js.Object
}

func (iter *mapIter) skipUntilValidKey() {
for iter.i < iter.keys.Length() {
k := iter.keys.Index(iter.i)
if iter.m.Get(k.String()) != js.Undefined {
break
}
// The key is already deleted. Move on the next item.
iter.i++
}
}

func mapiterinit(t *rtype, m unsafe.Pointer) unsafe.Pointer {
return unsafe.Pointer(&mapIter{t, js.InternalObject(m), js.Global.Call("$keys", js.InternalObject(m)), 0, nil})
}
@@ -563,8 +574,8 @@ func mapiterkey(it unsafe.Pointer) unsafe.Pointer {
if iter.last != nil {
kv = iter.last
} else {
// Compare the index and the size of the actual key set, and check if the iterator is already exhausted.
if iter.i >= js.Global.Call("$keys", iter.m).Length() {
iter.skipUntilValidKey()
if iter.i == iter.keys.Length() {
return nil
}
k := iter.keys.Index(iter.i)
@@ -582,7 +593,8 @@ func mapitervalue(it unsafe.Pointer) unsafe.Pointer {
if iter.last != nil {
kv = iter.last
} else {
if iter.i >= js.Global.Call("$keys", iter.m).Length() {
iter.skipUntilValidKey()
if iter.i == iter.keys.Length() {
return nil
}
k := iter.keys.Index(iter.i)
@@ -596,16 +608,6 @@ func mapiternext(it unsafe.Pointer) {
iter := (*mapIter)(it)
iter.last = nil
iter.i++

// Skip iter until the key gets valid.
for iter.i < iter.keys.Length() {
k := iter.keys.Index(iter.i)
if iter.m.Get(k.String()) != js.Undefined {
break
}
// The key is already deleted.
iter.i++
}
}

func maplen(m unsafe.Pointer) int {
@@ -695,3 +695,18 @@ func TestInterfaceConversionRuntimeError(t *testing.T) {
e := (interface{})(0)
_ = e.(I)
}

func TestReflectMapIterationAndDelete(t *testing.T) {
m := map[string]int{
"one": 1,
"two": 2,
"three": 3,
}
iter := reflect.ValueOf(m).MapRange()
for iter.Next() {
delete(m, iter.Key().String())
}
if got, want := len(m), 0; got != want {
t.Fatalf("got %d, want %d", got, want)
}
}

0 comments on commit 2ea661b

Please sign in to comment.
You can’t perform that action at this time.