feat: add containsKey method to Map and validate key types in subscript operations
Implement `containsKey()` native method on Map that returns a boolean indicating whether the given key exists in the map. Add `validateKey()` helper to reject non-value-type keys (objects, lists, fibers, etc.) with a clear runtime error. Refactor `wrenMapGet()` signature to return success/failure via bool and output the value through an out-parameter, enabling both `containsKey()` and the existing subscript operator to share the same key validation and lookup logic. Update `map_subscript` to return `null` instead of crashing when a key is not found. Add test cases for `containsKey` success/failure, and runtime error tests for invalid key types in subscript getter, setter, and containsKey.
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
var map = {
|
||||
"one": 1,
|
||||
"two": 2,
|
||||
"three": 3
|
||||
}
|
||||
|
||||
IO.print(map.containsKey("one")) // expect: true
|
||||
IO.print(map.containsKey("two")) // expect: true
|
||||
IO.print(map.containsKey("three")) // expect: true
|
||||
IO.print(map.containsKey("four")) // expect: false
|
||||
IO.print(map.containsKey("five")) // expect: false
|
||||
@@ -0,0 +1 @@
|
||||
var result = {}.containsKey([]) // expect runtime error: Key must be a value type.
|
||||
@@ -0,0 +1 @@
|
||||
var result = {}[[]] // expect runtime error: Key must be a value type.
|
||||
@@ -0,0 +1 @@
|
||||
var result = {}[[]] = "value" // expect runtime error: Key must be a value type.
|
||||
Reference in New Issue
Block a user