0xMiden / 0xMiden/compiler

`self` mutability in the account methods in the script

Abierto
#802 4 comentarios 0 reacciones 1 asignado Reclamado por @greenhat Ver en GitHub
Lenguaje dominante
Rust
Estrellas
115
Forks
84
Merge medio
1 d 8 h
PR fusionados (30 d)
15

Descripción

### 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.

Guía de contribución

Abrir la guía de contribución

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.