-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remaining host functions for FxDAO: 5/11 #43
Changes from all commits
4723997
1c2c615
a6456c3
8842917
52409a5
0fffc08
974a509
13dcfbf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
0.1.37 | ||
0.1.38 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api" | |
|
||
[tool.poetry] | ||
name = "komet" | ||
version = "0.1.37" | ||
version = "0.1.38" | ||
description = "K tooling for the Soroban platform" | ||
authors = [ | ||
"Runtime Verification, Inc. <[email protected]>", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,86 @@ module HOST-VECTOR | |
<locals> .Map </locals> | ||
``` | ||
|
||
## vec_get | ||
|
||
```k | ||
rule [hostfun-vec-get]: | ||
<instrs> hostCall ( "v" , "1" , [ i64 i64 .ValTypes ] -> [ i64 .ValTypes ] ) | ||
=> loadObject(HostVal(INDEX)) | ||
~> loadObject(HostVal(VEC)) | ||
~> vecGet | ||
... | ||
</instrs> | ||
<locals> | ||
0 |-> < i64 > VEC | ||
1 |-> < i64 > INDEX | ||
</locals> | ||
|
||
syntax InternalInstr ::= "vecGet" [symbol(vecGet)] | ||
// ---------------------------------------------------- | ||
rule [vecGet]: | ||
<instrs> vecGet => VEC {{ I }} orDefault HostVal(0) ... </instrs> // use 'orDefault' to avoid non-total list lookup | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @bbyalcinkaya could you please elaborate on how There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is a typed and total list lookup defined here. It returns the default value if the index is out of bounds or the list item at that index is not a I could've used the normal list lookup but that would require:
|
||
<hostStack> ScVec(VEC) : U32(I) : S => S </hostStack> | ||
requires 0 <=Int I | ||
andBool I <Int size(VEC) | ||
|
||
``` | ||
|
||
## vec_len | ||
|
||
```k | ||
rule [hostfun-vec-len]: | ||
<instrs> hostCall ( "v" , "3" , [ i64 .ValTypes ] -> [ i64 .ValTypes ] ) | ||
=> loadObject(HostVal(VEC)) | ||
~> vecLen | ||
... | ||
</instrs> | ||
<locals> | ||
0 |-> < i64 > VEC | ||
</locals> | ||
|
||
syntax InternalInstr ::= "vecLen" [symbol(vecLen)] | ||
// ---------------------------------------------------- | ||
rule [vecLen]: | ||
<instrs> vecLen => toSmall(U32(size(VEC))) ... </instrs> | ||
<hostStack> ScVec(VEC) : S => S </hostStack> | ||
|
||
``` | ||
|
||
## vec_push_back | ||
|
||
Creates a new vector by appending a given item to the end of the provided vector. | ||
This function does not modify the original vector, maintaining immutability. | ||
Returns a new vector with the appended item. | ||
|
||
```k | ||
rule [hostfun-vec-push-back]: | ||
<instrs> hostCall ( "v" , "6" , [ i64 i64 .ValTypes ] -> [ i64 .ValTypes ] ) | ||
=> loadObject(HostVal(VEC)) | ||
~> vecPushBack(HostVal(ITEM)) | ||
... | ||
</instrs> | ||
<locals> | ||
0 |-> < i64 > VEC | ||
1 |-> < i64 > ITEM | ||
</locals> | ||
|
||
syntax InternalInstr ::= vecPushBack(HostVal) [symbol(vecPushBack)] | ||
// ---------------------------------------------------- | ||
rule [vecPushBack]: | ||
<instrs> vecPushBack(ITEM) | ||
=> allocObject( | ||
ScVec( | ||
VEC ListItem(rel2abs(RELS, ITEM)) | ||
) | ||
) | ||
~> returnHostVal | ||
... | ||
</instrs> | ||
<hostStack> ScVec(VEC) : S => S </hostStack> | ||
<relativeObjects> RELS </relativeObjects> | ||
``` | ||
|
||
## vec_unpack_to_linear_memory | ||
|
||
```k | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
[package] | ||
name = "test_containers" | ||
version = "0.0.0" | ||
edition = "2021" | ||
publish = false | ||
|
||
[lib] | ||
crate-type = ["cdylib"] | ||
doctest = false | ||
|
||
[dependencies] | ||
soroban-sdk = { workspace = true } | ||
|
||
[dev-dependencies] | ||
soroban-sdk = { workspace = true, features = ["testutils"] } |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#![no_std] | ||
use soroban_sdk::{contract, contractimpl, Env, Vec}; | ||
|
||
#[contract] | ||
pub struct ContainersContract; | ||
|
||
#[contractimpl] | ||
impl ContainersContract { | ||
|
||
pub fn test_vector(env: Env, n: u32) -> bool { | ||
let n = n % 100; | ||
|
||
let mut vec: Vec<u32> = Vec::new(&env); | ||
assert_eq!(vec.len(), 0); | ||
|
||
for i in 0..n { | ||
vec.push_back(i); | ||
} | ||
|
||
assert_eq!(vec.len(), n); | ||
|
||
for i in 0..n { | ||
assert_eq!(vec.get_unchecked(i), i); | ||
} | ||
|
||
true | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bbyalcinkaya, could you please elaborate a bit on the presence of
toSmall
at the end of the rewriting operation? After going through its implementation, it is not immediately clear to me why we should leave it at the K cell at the end of certain operations.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My current understanding is that it is working as some sort of a return value for this host function (as in it is returning nothing, in this case), but I'd like to double check.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ACassimiro That's correct, it is used as a return value here.
toSmall
convertsScVal
s toHostVal
s. When aHostVal
is on top of the<instrs>
cell, it is executed asi64.const _
instruction: rule.