From 3111635806a7fdf098f3da0200ed01c43d5704b2 Mon Sep 17 00:00:00 2001
From: Cheng Shao <terrorjack@type.dance>
Date: Sun, 16 Mar 2025 12:09:43 +0000
Subject: [PATCH] testsuite: add test for wasm promise.throwTo() logic

This commit adds a test case to test the wasm backend
promise.throwTo() logic.

(cherry picked from commit 7f80455ee45e70d142bbc69478b9a8db43082187)
(cherry picked from commit bbbaba0eb74bfa398a3a2274271182016fae5841)
---
 testsuite/tests/jsffi/all.T         |  2 ++
 testsuite/tests/jsffi/cancel.hs     | 12 ++++++++++++
 testsuite/tests/jsffi/cancel.mjs    | 13 +++++++++++++
 testsuite/tests/jsffi/cancel.stdout |  1 +
 4 files changed, 28 insertions(+)
 create mode 100644 testsuite/tests/jsffi/cancel.hs
 create mode 100644 testsuite/tests/jsffi/cancel.mjs
 create mode 100644 testsuite/tests/jsffi/cancel.stdout

diff --git a/testsuite/tests/jsffi/all.T b/testsuite/tests/jsffi/all.T
index aa84d3d3423..d482766abe1 100644
--- a/testsuite/tests/jsffi/all.T
+++ b/testsuite/tests/jsffi/all.T
@@ -11,6 +11,8 @@ setTestOpts([
   extra_ways(['compacting_gc', 'nonmoving', 'sanity'])
 ])
 
+test('cancel', [], compile_and_run, ['-optl-Wl,--export=setTimeout'])
+
 test('gameover', [], compile_and_run, ['-optl-Wl,--export=testJSException,--export=testHSException'])
 
 test('http', [], compile_and_run, ['-optl-Wl,--export=main'])
diff --git a/testsuite/tests/jsffi/cancel.hs b/testsuite/tests/jsffi/cancel.hs
new file mode 100644
index 00000000000..41b35c803e2
--- /dev/null
+++ b/testsuite/tests/jsffi/cancel.hs
@@ -0,0 +1,12 @@
+module Test where
+
+import Control.Exception
+
+foreign import javascript safe "new Promise(res => setTimeout(res, $1))"
+  js_setTimeout :: Int -> IO ()
+
+setTimeout :: Int -> IO ()
+setTimeout t = evaluate =<< js_setTimeout t
+
+foreign export javascript "setTimeout"
+  setTimeout :: Int -> IO ()
diff --git a/testsuite/tests/jsffi/cancel.mjs b/testsuite/tests/jsffi/cancel.mjs
new file mode 100644
index 00000000000..87528153596
--- /dev/null
+++ b/testsuite/tests/jsffi/cancel.mjs
@@ -0,0 +1,13 @@
+export default async (__exports) => {
+  const test = new Promise((res) => {
+    const p = __exports.setTimeout(114514);
+    p.throwTo("1919810");
+    p.catch((err) => {
+      console.log(`${err}`.split("\n")[0]);
+      res();
+    });
+  });
+
+  await test;
+  process.exit();
+};
diff --git a/testsuite/tests/jsffi/cancel.stdout b/testsuite/tests/jsffi/cancel.stdout
new file mode 100644
index 00000000000..4d34c8f9d93
--- /dev/null
+++ b/testsuite/tests/jsffi/cancel.stdout
@@ -0,0 +1 @@
+RuntimeError: JSException "1919810"
-- 
GitLab