Skip to content

Commit

Permalink
implement map_keys and map_vals
Browse files Browse the repository at this point in the history
  • Loading branch information
bbyalcinkaya committed Dec 19, 2024
1 parent ca5465a commit b393c0b
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 2 deletions.
30 changes: 30 additions & 0 deletions src/komet/kdist/soroban-semantics/host/map.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,36 @@ increasing order. Its primary use is alongside [`map_val_by_pos`](#map_val_by_po
orBool size(M) <=Int I
```

## map_keys

```k
rule [hostCallAux-map-keys]:
<instrs> hostCallAux("m", "7")
=> allocObject(ScVec(sortedKeys(M)))
~> returnHostVal
...
</instrs>
<hostStack> ScMap(M) : S => S </hostStack>
```

## map_vals

```k
rule [hostCallAux-map-vals]:
<instrs> hostCallAux("m", "8")
=> allocObject(ScVec(
lookupMany(M, sortedKeys(M), Void)
))
~> returnHostVal
...
</instrs>
<hostStack> ScMap(M) : S => S </hostStack>
```

## map_unpack_to_linear_memory

Writes values from a map (`ScMap`) to a specified memory address.
Expand Down
57 changes: 57 additions & 0 deletions src/tests/integration/data/map.wast
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ uploadWasm( b"test-wasm",
(import "m" "4" (func $has (param i64 i64) (result i64)))
(import "m" "5" (func $key_by_pos (param i64 i64) (result i64)))
(import "m" "6" (func $val_by_pos (param i64 i64) (result i64)))
(import "m" "7" (func $keys (param i64) (result i64)))
(import "m" "8" (func $vals (param i64) (result i64)))

(export "new" (func $new))
(export "put" (func $put))
Expand All @@ -20,6 +22,8 @@ uploadWasm( b"test-wasm",
(export "has" (func $has))
(export "key_by_pos" (func $key_by_pos))
(export "val_by_pos" (func $val_by_pos))
(export "keys" (func $keys))
(export "vals" (func $vals))
)
)

Expand Down Expand Up @@ -294,4 +298,57 @@ callTx(
Error(ErrObject, 1) ;; IndexBounds
)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; map_keys
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

callTx(
Account(b"test-caller"),
Contract(b"test-sc"),
"keys",
ListItem(ScMap(
Symbol(str("b")) |-> U32(1)
Symbol(str("a")) |-> U32(2)
)),
ScVec(
ListItem(Symbol(str("a")))
ListItem(Symbol(str("b")))
)
)

callTx(
Account(b"test-caller"),
Contract(b"test-sc"),
"keys",
ListItem(ScMap(.Map)),
ScVec(.List)
)


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; map_vals
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

callTx(
Account(b"test-caller"),
Contract(b"test-sc"),
"vals",
ListItem(ScMap(
Symbol(str("b")) |-> U32(1)
Symbol(str("a")) |-> U32(2)
)),
ScVec(
ListItem(U32(2))
ListItem(U32(1))
)
)

callTx(
Account(b"test-caller"),
Contract(b"test-sc"),
"vals",
ListItem(ScMap(.Map)),
ScVec(.List)
)

setExitCode(0)
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ impl ContainersContract {
true
}


// Iterate through the key-value pairs in the map ensuring keys are strictly increasing
pub fn test_map_iterate(env: Env, n: u32) -> bool {
let n = n % 100;

Expand All @@ -37,16 +39,25 @@ impl ContainersContract {
}
assert_eq!(map.len(), n);

// Iterate through the key-value pairs in the map, ensuring:
let vals = map.values();
let keys = map.keys();

let mut cur = 0;
for (i, x) in map {
// Keys are strictly increasing
assert_eq!(cur, i);
assert_eq!(x, -(i as i32));

cur += 1;
}

for (i, k) in keys.iter().enumerate() {
assert_eq!(k, i as u32);
}

for (i, x) in vals.iter().enumerate() {
assert_eq!(x, -(i as i32));
}

true
}
}

0 comments on commit b393c0b

Please sign in to comment.