1
0
forked from Mirror/wren

Functions for operating on Maps from C (#725)

new API functions for maps:
wrenSetSlotNewMap
wrenGetMapCount
wrenGetMapContainsKey
wrenGetMapValue
wrenSetMapValue
wrenRemoveMapValue
This commit is contained in:
Aviv Beeri
2020-06-14 22:45:23 +01:00
committed by GitHub
parent 344d3432b3
commit de6a312868
19 changed files with 374 additions and 7 deletions

68
test/api/maps.wren Normal file
View File

@ -0,0 +1,68 @@
class ForeignClass {
construct new() {}
}
class Maps {
foreign static newMap()
foreign static insert()
foreign static contains(map, key)
foreign static contains()
foreign static containsFalse()
foreign static count()
foreign static count(map)
foreign static remove(map)
foreign static invalidInsert(obj)
}
// map new + get/set API
var map = Maps.newMap()
System.print(map is Map) // expect: true
System.print(map.count) // expect: 0
var data = Maps.insert()
System.print(data["England"]) // expect: London
System.print(data["Empty"]) // expect: []
System.print(data[1.0]) // expect: 42
System.print(data[false]) // expect: true
System.print(data[null]) // expect: null
// remove API
var removed = Maps.remove({ "key":"value", "other":"data" })
System.print(removed) // expect: value
var removedNone = Maps.remove({})
System.print(removedNone) // expect: null
// count API
var countMap = { "key":"value", "other":"data", 4:"number key" }
System.print(Maps.count(countMap)) // expect: 3
Maps.remove(countMap) //remove using API
System.print(Maps.count(countMap)) // expect: 2
countMap.remove("other") //remove wren side
System.print(Maps.count(countMap)) // expect: 1
var countAPI = Maps.count()
System.print(countAPI) // expect: 5
//contains key API
var containsMap = { "key":"value", "other":"data", 4:"number key" }
System.print(Maps.contains(containsMap, "key")) // expect: true
System.print(Maps.contains(containsMap, "fake")) // expect: false
System.print(Maps.contains(containsMap, "other")) // expect: true
Maps.remove(containsMap) //remove using API
System.print(Maps.contains(containsMap, "key")) // expect: false
containsMap.remove("other") //remove wren side
System.print(Maps.contains(containsMap, "other")) // expect: false
System.print(Maps.contains()) // expect: true
System.print(Maps.containsFalse()) // expect: false
//
Maps.invalidInsert(ForeignClass.new()) // expect runtime error: Key must be a value type.