Skip to content

Commit

Permalink
fix(es): Don't panic when wasm bytecheck faild (#9803)
Browse files Browse the repository at this point in the history
**Description:**

Currently when wasm plugin is not compatible with swc version it will panic which cause it hard to report useful error info from caller except use catch_unwind. change the behavior to not panic and let the user to decide error handling
  • Loading branch information
hardfist authored Dec 18, 2024
1 parent 9eceec4 commit c81be2e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .changeset/new-apricots-exercise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
swc: patch
swc_core: patch
---

fix: don't panic when wasm bytecheck faild
25 changes: 19 additions & 6 deletions crates/swc/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
)]

use serde::{Deserialize, Serialize};
use swc_common::errors::HANDLER;
use swc_ecma_ast::Pass;
#[cfg(feature = "plugin")]
use swc_ecma_ast::*;
Expand Down Expand Up @@ -165,15 +166,27 @@ impl Fold for RustPlugins {

#[cfg(feature = "plugin")]
fn fold_module(&mut self, n: Module) -> Module {
self.apply(Program::Module(n))
.expect("failed to invoke plugin")
.expect_module()
match self.apply(Program::Module(n)) {
Ok(program) => program.expect_module(),
Err(err) => {
HANDLER.with(|handler| {
handler.err(&err.to_string());
});
Module::default()
}
}
}

#[cfg(feature = "plugin")]
fn fold_script(&mut self, n: Script) -> Script {
self.apply(Program::Script(n))
.expect("failed to invoke plugin")
.expect_script()
match self.apply(Program::Script(n)) {
Ok(program) => program.expect_script(),
Err(err) => {
HANDLER.with(|handler| {
handler.err(&err.to_string());
});
Script::default()
}
}
}
}

0 comments on commit c81be2e

Please sign in to comment.