feat: implement Map.keys, Map.values iterators and proper toString with key-value formatting

Add MapKeySequence and MapValueSequence classes in core.wren to expose iterable key and value views on Map instances. Implement native iterate_, keyIteratorValue_, and valueIteratorValue_ primitives in wren_core.c to support these sequences. Replace the stub Map.toString with a full implementation that iterates keys and formats each entry as "key: value", handling empty maps and nested maps correctly. Include comprehensive test suites for key iteration, value iteration, iterator type validation, and toString output across multiple orderings.
This commit is contained in:
Bob Nystrom
2015-01-25 18:27:38 +00:00
parent 56123c71e3
commit f093d0a42a
9 changed files with 250 additions and 4 deletions
+45
View File
@@ -0,0 +1,45 @@
var a = {"one": 1, "two": 2, "three": 3, "four": 4}.keys
// The precise numeric values aren't defined since they are indexes into the
// entry table and the hashing process isn't specified. So we just validate
// what we can assume about them.
IO.print(a.iterate(null) is Num) // expect: true
IO.print(a.iterate(null) >= 0) // expect: true
IO.print(a.iterate(0) is Num) // expect: true
IO.print(a.iterate(0) > 0) // expect: true
IO.print(a.iterate(1) is Num) // expect: true
IO.print(a.iterate(1) > 0) // expect: true
IO.print(a.iterate(2) is Num) // expect: true
IO.print(a.iterate(2) > 0) // expect: true
IO.print(a.iterate(3) is Num) // expect: true
IO.print(a.iterate(3) > 0) // expect: true
var previous = -1
var iterator = a.iterate(null)
while (iterator) {
IO.print(iterator > previous)
IO.print(iterator is Num)
previous = iterator
iterator = a.iterate(iterator)
}
// First entry:
// expect: true
// expect: true
// Second entry:
// expect: true
// expect: true
// Third entry:
// expect: true
// expect: true
// Fourth entry:
// expect: true
// expect: true
// Out of bounds.
IO.print(a.iterate(16)) // expect: false
IO.print(a.iterate(-1)) // expect: false
// Nothing to iterate in an empty map.
IO.print({}.keys.iterate(null)) // expect: false
@@ -0,0 +1,2 @@
var a = {1: 2, 3: 4}
a.keys.iterate(1.5) // expect runtime error: Iterator must be an integer.
@@ -0,0 +1,2 @@
var a = {1: 2, 3: 4}
a.keys.iterate("2") // expect runtime error: Iterator must be a number.
+27
View File
@@ -0,0 +1,27 @@
// Handle empty map.
IO.print({}.toString) // expect: {}
// Does not quote strings.
IO.print({"1": "2"}.toString) // expect: {1: 2}
// Nested maps.
IO.print({1: {2: {}}}) // expect: {1: {2: {}}}
// Calls toString on elements.
class Foo {
toString { "Foo.toString" }
}
IO.print({1: new Foo}) // expect: {1: Foo.toString}
// Since iteration order is unspecified, we don't know what order the results
// will be.
var s = {1: 2, 3: 4, 5: 6}.toString
IO.print(s == "{1: 2, 3: 4, 5: 6}" ||
s == "{1: 2, 5: 6, 3: 4}" ||
s == "{3: 4, 1: 2, 5: 6}" ||
s == "{3: 4, 5: 6, 1: 2}" ||
s == "{5: 6, 1: 2, 3: 4}" ||
s == "{5: 6, 3: 4, 1: 2}") // expect: true
// TODO: Handle maps that contain themselves.
+45
View File
@@ -0,0 +1,45 @@
var a = {"one": 1, "two": 2, "three": 3, "four": 4}.values
// The precise numeric values aren't defined since they are indexes into the
// entry table and the hashing process isn't specified. So we just validate
// what we can assume about them.
IO.print(a.iterate(null) is Num) // expect: true
IO.print(a.iterate(null) >= 0) // expect: true
IO.print(a.iterate(0) is Num) // expect: true
IO.print(a.iterate(0) > 0) // expect: true
IO.print(a.iterate(1) is Num) // expect: true
IO.print(a.iterate(1) > 0) // expect: true
IO.print(a.iterate(2) is Num) // expect: true
IO.print(a.iterate(2) > 0) // expect: true
IO.print(a.iterate(3) is Num) // expect: true
IO.print(a.iterate(3) > 0) // expect: true
var previous = -1
var iterator = a.iterate(null)
while (iterator) {
IO.print(iterator > previous)
IO.print(iterator is Num)
previous = iterator
iterator = a.iterate(iterator)
}
// First entry:
// expect: true
// expect: true
// Second entry:
// expect: true
// expect: true
// Third entry:
// expect: true
// expect: true
// Fourth entry:
// expect: true
// expect: true
// Out of bounds.
IO.print(a.iterate(16)) // expect: false
IO.print(a.iterate(-1)) // expect: false
// Nothing to iterate in an empty map.
IO.print({}.values.iterate(null)) // expect: false
@@ -0,0 +1,2 @@
var a = {1: 2, 3: 4}
a.values.iterate(1.5) // expect runtime error: Iterator must be an integer.
@@ -0,0 +1,2 @@
var a = {1: 2, 3: 4}
a.values.iterate("2") // expect runtime error: Iterator must be a number.