From 251c4d589f20f50b0d2e035c032fc1134d406c65 Mon Sep 17 00:00:00 2001 From: Wen Yuxiang Date: Wed, 15 Apr 2026 01:26:07 +0800 Subject: [PATCH] make abort panic with msg under js target --- abort/abort.mbt | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/abort/abort.mbt b/abort/abort.mbt index c2e2a63519..83ba681f2f 100644 --- a/abort/abort.mbt +++ b/abort/abort.mbt @@ -23,7 +23,7 @@ /// /// Returns a value of type `T`. However, this function never actually returns a /// value as it always causes a panic. -#cfg(not(target="native")) +#cfg(not(any(target="native", target="js"))) pub fn[T] abort(msg : String) -> T { let _ = msg panic_impl() @@ -50,5 +50,29 @@ pub fn[T] abort(msg : String) -> T { panic_impl() } +///| +/// Aborts the program with an error message. Always causes a panic, regardless +/// of the message provided. +/// +/// Parameters: +/// +/// * `message` : A string containing the error message to be displayed when +/// aborting. +/// +/// Returns a value of type `T`. However, this function never actually returns a +/// value as it always causes a panic. +#cfg(target="js") +pub fn[T] abort(msg : String) -> T { + panic_with_msg(msg) + // This line is unreachable, but it still needs to exist to satisfy the + // return type and force the compiler to generate $PanicError. + panic_impl() +} + +///| +#cfg(target="js") +extern "js" fn panic_with_msg(msg : String) -> Unit = + #|(msg) => { throw new $PanicError(msg); } + ///| fn[T] panic_impl() -> T = "%panic"