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:
+33
-2
@@ -89,8 +89,39 @@ class List is Sequence {
|
||||
}
|
||||
|
||||
class Map {
|
||||
// TODO: Implement this.
|
||||
toString { "{}" }
|
||||
keys { new MapKeySequence(this) }
|
||||
values { new MapValueSequence(this) }
|
||||
|
||||
toString {
|
||||
var first = true
|
||||
var result = "{"
|
||||
|
||||
for (key in keys) {
|
||||
if (!first) result = result + ", "
|
||||
first = false
|
||||
result = result + key.toString + ": " + this[key].toString
|
||||
}
|
||||
|
||||
return result + "}"
|
||||
}
|
||||
}
|
||||
|
||||
class MapKeySequence is Sequence {
|
||||
new(map) {
|
||||
_map = map
|
||||
}
|
||||
|
||||
iterate(n) { _map.iterate_(n) }
|
||||
iteratorValue(iterator) { _map.keyIteratorValue_(iterator) }
|
||||
}
|
||||
|
||||
class MapValueSequence is Sequence {
|
||||
new(map) {
|
||||
_map = map
|
||||
}
|
||||
|
||||
iterate(n) { _map.iterate_(n) }
|
||||
iteratorValue(iterator) { _map.valueIteratorValue_(iterator) }
|
||||
}
|
||||
|
||||
class Range is Sequence {}
|
||||
|
||||
Reference in New Issue
Block a user