Stream: wasm

Topic: getting stuck on using walrus for my intermediate language


view this post on Zulip Rizary (Jun 09 2021 at 12:46):

I use walrus library for my intermediate language. But I got stuck when I want to implement while statement. So I have this builder:

pub struct WasmBuilderVisitor {
    pub module: Module,
    pub function_builder: Stack<FunctionBuilder>,
    // BuildVisitorInner is a helper struct to keep track of functions and variables
    inner: BuildVisitorInner<JasmWasmBuild>
}

And my while implementation is:

fn visit_while(&mut self, condition: &JasmExpression, body: &Block) -> () {
    self.visit(condition);
    let mut function_builder = self.function_builder.get_mut().func_body();
    function_builder.block(None, |  done | {
        let done_id = done.id().to_owned();

        done.loop_(None, | loop_ | {
            let loop_id = loop_.id();
            &self.visit(condition);

            loop_.br_if(done_id);
            self.visits(&body.0);
            loop_.br(loop_id);
        });
    });

And I got the following error:

closure requires unique access to `self` but it is already borrowed
closure construction occurs here

I notice it is because of the loop_ function signature here:

pub fn loop_(
    &mut self,
    ty: impl Into<InstrSeqType>,
    make_loop: impl FnOnce(&mut InstrSeqBuilder<'_>)
) -> &mut Self

And it is the problem: make_loop: impl FnOnce(&mut InstrSeqBuilder<'_>). However, I feel like it is hard for me to change how my visitor build is structured. Is there any suggestion for this? If I understand correctly, since there is FnOnce, then I cannot use both loop_ and self at the same block.


Last updated: Dec 23 2024 at 12:05 UTC