0xMiden / 0xMiden/compiler

`self` mutability in the account methods in the script

Đang mở
#802 4 bình luận 0 reaction 1 người được giao Được @greenhat nhận Xem trên GitHub
Ngôn ngữ chính
Rust
Star
115
Fork
84
Merge trung bình
1 ngày 8 giờ
Pull request đã merge (30 ngày)
15

Mô tả

### Problem

We don't have the information regarding the mutability of `self` in the account methods in the script.

### Context

All the account components with bindings generated as free functions in the modules are gathered under the `Account` as struct methods.

### Why `self` mutability info missing?

TLDR; The bindings are generated from the WIT interface which has no notion of `self` (it's just a bunch of functions) and therefore its mutability.

Let's take the `basic-wallet` example.

From the user's code with proper `&mut self` at https://github.com/0xMiden/compiler/blob/adfb610b8b4c1747ece077a6128855348efacafb/examples/basic-wallet/src/lib.rs?plain=1#L14-L38

The `#[component]` macro generates the WIT interface:

```wit
// This file is auto-generated by the `#[component]` macro.
// Do not edit this file manually.

package miden:basic-wallet@0.1.0;

use miden:base/core-types@1.0.0;

interface basic-wallet {
use core-types.{asset, note-idx};

receive-asset: func(asset: asset);
move-asset-to-note: func(asset: asset, note-idx: note-idx);
}

world basic-wallet-world {
export basic-wallet;
}
```

From which the `wit-bindgen::generate!` macro generates bindings (free functions in a module):

```rust
pub mod bindings {
pub mod miden {
pub mod basic_wallet {
pub mod basic_wallet {
pub fn receive_asset(asset: ::miden::Asset) -> () {
...
}
pub fn move_asset_to_note(asset: ::miden::Asset, note_idx: ::miden::NoteIdx) -> () {
...
}
}
}
}
```

Then, we generate methods for the `Account` from the functions above.

There is no notion of `self` in the WIT and therefore in the generated bindings.

### Solutions

Explore the Wasm CM resources to represent an account component. See an intro to resources at https://component-model.bytecodealliance.org/language-support/using-wit-resources/rust.html

The resource has `self` in the generated Rust bindings and has `borrow` as well as `write` and `read` attributes in the WIT for the resource methods.

AFAIR, the Wasm CM resource uses `call_indirect` in the compiled code so it might be the time for us to implement it.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.