生成返回字符串的函数时,为啥 wasm-opt 在 wasm-pack 构建中失败?
Posted
技术标签:
【中文标题】生成返回字符串的函数时,为啥 wasm-opt 在 wasm-pack 构建中失败?【英文标题】:Why does wasm-opt fail in wasm-pack builds when generating a function returning a string?生成返回字符串的函数时,为什么 wasm-opt 在 wasm-pack 构建中失败? 【发布时间】:2021-02-06 23:22:50 【问题描述】:我正在处理Rust WASM tutorial for Conway's game of life。
文件中最简单的函数之一称为Universe.render
(它用于渲染表示游戏状态的字符串)。当我运行wasm-pack build
时,它会导致错误:
Fatal: error in validating input
Error: failed to execute `wasm-opt`: exited with exit code: 1
full command: "/home/vaer/.cache/.wasm-pack/wasm-opt-4d7a65327e9363b7/wasm-opt" "/home/vaer/src/learn-rust/wasm-game-of-life/pkg/wasm_game_of_life_bg.wasm" "-o" "/home/vaer/src/learn-rust/wasm-game-of-life/pkg/wasm_game_of_life_bg.wasm-opt.wasm" "-O"
To disable `wasm-opt`, add `wasm-opt = false` to your package metadata in your `Cargo.toml`.
如果我删除该函数,则代码构建不会出错。如果我用以下函数替换它,构建会失败并出现同样的错误:
pub fn wtf() -> String
String::from("wtf")
似乎任何返回 String
的函数都会导致此错误。为什么?
以下是我的全部代码:
mod utils;
use wasm_bindgen::prelude::*;
// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
// allocator.
#[cfg(feature = "wee_alloc")]
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
// Begin game of life impl
use std::fmt;
#[wasm_bindgen]
#[repr(u8)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Cell
Dead = 0,
Alive = 1,
#[wasm_bindgen]
pub struct Universe
width: u32,
height: u32,
cells: Vec<Cell>,
impl fmt::Display for Universe
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result
for line in self.cells.as_slice().chunks(self.width as usize)
for &cell in line
let symbol = if cell == Cell::Dead '◻' else '◼' ;
write!(f, "", symbol)?;
write!(f, "\n")?;
Ok(())
impl Universe
fn get_index(&self, row: u32, column: u32) -> usize
(row * self.width + column) as usize
fn live_neighbor_count(&self, row: u32, column: u32) -> u8
let mut count = 0;
for delta_row in [self.height - 1, 0, 1].iter().cloned()
for delta_col in [self.width - 1, 0, 1].iter().cloned()
if delta_row == 0 && delta_col == 0
continue;
let neighbor_row = (row + delta_row) % self.height;
let neighbor_col = (column + delta_col) % self.width;
let idx = self.get_index(neighbor_row, neighbor_col);
count += self.cells[idx] as u8;
count
/// Public methods, exported to javascript.
#[wasm_bindgen]
impl Universe
pub fn tick(&mut self)
let mut next = self.cells.clone();
for row in 0..self.height
for col in 0..self.width
let idx = self.get_index(row, col);
let cell = self.cells[idx];
let live_neighbors = self.live_neighbor_count(row, col);
let next_cell = match (cell, live_neighbors)
// Rule 1: Any live cell with fewer than two live neighbours
// dies, as if caused by underpopulation.
(Cell::Alive, x) if x < 2 => Cell::Dead,
// Rule 2: Any live cell with two or three live neighbours
// lives on to the next generation.
(Cell::Alive, 2) | (Cell::Alive, 3) => Cell::Alive,
// Rule 3: Any live cell with more than three live
// neighbours dies, as if by overpopulation.
(Cell::Alive, x) if x > 3 => Cell::Dead,
// Rule 4: Any dead cell with exactly three live neighbours
// becomes a live cell, as if by reproduction.
(Cell::Dead, 3) => Cell::Alive,
// All other cells remain in the same state.
(otherwise, _) => otherwise,
;
next[idx] = next_cell;
self.cells = next;
pub fn new() -> Universe
let width = 64;
let height = 64;
let cells = (0..width * height)
.map(|i|
if i % 2 == 0 || i % 7 == 0
Cell::Alive
else
Cell::Dead
)
.collect();
Universe
width,
height,
cells,
pub fn render(&self) -> String
self.to_string()
只需删除此文件底部的render
函数即可使构建成功。将render
函数替换为任何返回String
的函数会导致构建失败。为什么?
【问题讨论】:
由于这个问题只与wasm-opt
当前版本中的错误有关,是否应该关闭这个问题?
这是一个很好的问题和一个很好的答案。从 github 问题来看,该错误已经存在了一段时间,尚未修复。您的回答可能会帮助其他人。
【参考方案1】:
事实证明这不是预期的行为;而不是 it is a bug 和 wasm-pack
。
现在可以通过将以下内容添加到项目的cargo.toml
来解决此问题:
[package.metadata.wasm-pack.profile.release]
wasm-opt = ["-Oz", "--enable-mutable-globals"]
【讨论】:
以上是关于生成返回字符串的函数时,为啥 wasm-opt 在 wasm-pack 构建中失败?的主要内容,如果未能解决你的问题,请参考以下文章
为啥 PHP 的 uniqid 函数只返回 13 位而不是 14?