Sha256: 888654ba6c95ee44b0041dd211889ae7a96f1f51e40f09b7246e28c1c497be51

Contents?: true

Size: 1.8 KB

Versions: 13

Compression:

Stored size: 1.8 KB

Contents

use crate::token::{Id, Index};
use crate::Error;
use std::collections::HashMap;

#[derive(Default)]
pub struct Namespace<'a> {
    names: HashMap<Id<'a>, u32>,
    count: u32,
}

impl<'a> Namespace<'a> {
    pub fn register(&mut self, name: Option<Id<'a>>, desc: &str) -> Result<u32, Error> {
        let index = self.alloc();
        if let Some(name) = name {
            if let Some(_prev) = self.names.insert(name, index) {
                return Err(Error::new(
                    name.span(),
                    format!("duplicate {} identifier", desc),
                ));
            }
        }
        Ok(index)
    }

    pub fn alloc(&mut self) -> u32 {
        let index = self.count;
        self.count += 1;
        index
    }

    pub fn register_specific(&mut self, name: Id<'a>, index: u32, desc: &str) -> Result<(), Error> {
        if let Some(_prev) = self.names.insert(name, index) {
            return Err(Error::new(
                name.span(),
                format!(
                    "duplicate identifier: duplicate {desc} named `{}`",
                    name.name()
                ),
            ));
        }
        Ok(())
    }

    pub fn resolve(&self, idx: &mut Index<'a>, desc: &str) -> Result<u32, Error> {
        let id = match idx {
            Index::Num(n, _) => return Ok(*n),
            Index::Id(id) => id,
        };
        if let Some(&n) = self.names.get(id) {
            *idx = Index::Num(n, id.span());
            return Ok(n);
        }
        Err(resolve_error(*id, desc))
    }
}

pub fn resolve_error(id: Id<'_>, ns: &str) -> Error {
    assert!(
        !id.is_gensym(),
        "symbol generated by `wast` itself cannot be resolved {:?}",
        id
    );
    Error::new(
        id.span(),
        format!("unknown {ns}: failed to find name `${}`", id.name()),
    )
}

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
wasmtime-29.0.0 ./ext/cargo-vendor/wast-224.0.0/src/names.rs
wasmtime-28.0.0 ./ext/cargo-vendor/wast-223.0.0/src/names.rs
wasmtime-27.0.0 ./ext/cargo-vendor/wast-220.0.0/src/names.rs
wasmtime-26.0.0 ./ext/cargo-vendor/wast-219.0.1/src/names.rs
wasmtime-25.0.2 ./ext/cargo-vendor/wast-218.0.0/src/names.rs
wasmtime-25.0.1 ./ext/cargo-vendor/wast-217.0.0/src/names.rs
wasmtime-25.0.0 ./ext/cargo-vendor/wast-217.0.0/src/names.rs
wasmtime-24.0.0 ./ext/cargo-vendor/wast-216.0.0/src/names.rs
wasmtime-23.0.2 ./ext/cargo-vendor/wast-215.0.0/src/names.rs
wasmtime-22.0.0 ./ext/cargo-vendor/wast-209.0.1/src/names.rs
wasmtime-21.0.1 ./ext/cargo-vendor/wast-209.0.1/src/names.rs
wasmtime-20.0.2 ./ext/cargo-vendor/wast-209.0.1/src/names.rs
wasmtime-20.0.0 ./ext/cargo-vendor/wast-208.0.1/src/names.rs