Skip to content

Commit 08dce31

Browse files
authored
Merge pull request #1078 from ejgallego/timeout
[wasm] Update interrupt patch to account for timeouts
2 parents beecdb5 + 988372c commit 08dce31

File tree

4 files changed

+100
-60
lines changed

4 files changed

+100
-60
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@
8888
- [vscode] Add config manager for handling client configuration
8989
changes in the infoview. Add configuration option for number of
9090
messages displayed (@Durbatuluk1701, #1067)
91+
- [wasm] Update interrupt patch to account for timeouts (@ejgallego,
92+
Shachar Itzhaky, #1078)
9193

9294
# coq-lsp 0.2.4: (W)Activation
9395
------------------------------

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ patch-for-js:
236236
ifndef VENDORED_SETUP
237237
git clone --depth=1 https://github.com/coq/coq.git -b $(COQ_BRANCH) $(COQ_SRC_DIR)
238238
endif
239-
cd $(COQ_SRC_DIR) && git apply $(PATCH_DIR)/0001-coq-lsp-patch.patch
239+
cd $(COQ_SRC_DIR) && git apply $(PATCH_DIR)/0001-jscoq-Interrupt-and-timeout-Rocq-patch.patch
240240
cd $(COQ_SRC_DIR) && git apply $(PATCH_DIR)/0001-jscoq-lib-system.ml-de-unix-stat.patch
241241
cd $(COQ_SRC_DIR) && git apply $(PATCH_DIR)/0001-engine-trampoline.patch
242242
cd $(COQ_SRC_DIR) && git apply $(PATCH_DIR)/0001-ocaml-4-12.patch

etc/0001-coq-lsp-patch.patch

Lines changed: 0 additions & 59 deletions
This file was deleted.
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
From d8ca76a4cc493a2fe89c53fd9e2c5fd38c9dc2fb Mon Sep 17 00:00:00 2001
2+
From: Emilio Jesus Gallego Arias <[email protected]>
3+
Date: Mon, 1 Dec 2025 00:56:30 +0100
4+
Subject: [PATCH] [jscoq] Interrupt and timeout Rocq patch
5+
6+
---
7+
lib/control.ml | 36 +++++++++++++++++++++++++++++++++---
8+
lib/dune | 4 ++++
9+
lib/jscoq_extern.c | 4 ++++
10+
3 files changed, 41 insertions(+), 3 deletions(-)
11+
create mode 100644 lib/jscoq_extern.c
12+
13+
diff --git a/lib/control.ml b/lib/control.ml
14+
index 5321357a1c..513677565f 100644
15+
--- a/lib/control.ml
16+
+++ b/lib/control.ml
17+
@@ -18,7 +18,19 @@ let enable_thread_delay = ref false
18+
19+
exception Timeout
20+
21+
+(* implemented in backend/jsoo/js_stub/interrupt.js *)
22+
+external interrupt_pending : unit -> bool = "interrupt_pending"
23+
+
24+
+let timeout_deadline : (float * (unit -> unit)) option ref = ref None
25+
+
26+
+let jscoq_event_yield () =
27+
+ if interrupt_pending() then interrupt := true;
28+
+ match !timeout_deadline with
29+
+ | Some (time, callback) -> if Unix.gettimeofday () > time then callback ();
30+
+ | None -> ()
31+
+
32+
let check_for_interrupt () =
33+
+ jscoq_event_yield ();
34+
if !interrupt then begin interrupt := false; raise Sys.Break end;
35+
if !enable_thread_delay then begin
36+
incr steps;
37+
@@ -93,11 +105,29 @@ let windows_timeout n f x =
38+
let () = killed := true in
39+
Exninfo.iraise e
40+
41+
+let unwind ~(protect:unit -> unit) f =
42+
+ try let y = f () in protect (); y
43+
+ with e -> protect (); raise e
44+
+
45+
+let jscoq_timeout n f x =
46+
+ let expired = ref false in
47+
+ timeout_deadline := Some (Unix.gettimeofday () +. n,
48+
+ fun () -> expired := true; interrupt := true);
49+
+ let protect () = jscoq_event_yield (); timeout_deadline := None;
50+
+ interrupt := false in
51+
+ let res = try Ok (unwind ~protect (fun () -> f x))
52+
+ with Sys.Break -> if !expired then Error Exninfo.null else raise Sys.Break in
53+
+ if !expired then Error Exninfo.null
54+
+ else res
55+
+
56+
type timeout = { timeout : 'a 'b. float -> ('a -> 'b) -> 'a -> ('b, Exninfo.info) result }
57+
58+
-let timeout_fun = match Sys.os_type with
59+
-| "Unix" | "Cygwin" -> { timeout = unix_timeout }
60+
-| _ -> { timeout = windows_timeout }
61+
+(* let timeout_fun = match Sys.os_type with *)
62+
+(* | "Unix" | "Cygwin" -> { timeout = unix_timeout } *)
63+
+(* | _ -> { timeout = windows_timeout } *)
64+
+
65+
+let _ = windows_timeout, unix_timeout
66+
+let timeout_fun = { timeout = jscoq_timeout }
67+
68+
let timeout_fun_ref = ref timeout_fun
69+
let set_timeout f = timeout_fun_ref := f
70+
diff --git a/lib/dune b/lib/dune
71+
index e86e9decb1..b3e2d070c9 100644
72+
--- a/lib/dune
73+
+++ b/lib/dune
74+
@@ -4,6 +4,10 @@
75+
(public_name rocq-runtime.lib)
76+
(wrapped false)
77+
(modules_without_implementation xml_datatype)
78+
+ (foreign_stubs
79+
+ (language c)
80+
+ (names jscoq_extern)
81+
+ (flags :standard (:include %{project_root}/config/dune.c_flags)))
82+
(libraries
83+
rocq-runtime.boot rocq-runtime.clib rocq-runtime.config
84+
(select instr.ml from
85+
diff --git a/lib/jscoq_extern.c b/lib/jscoq_extern.c
86+
new file mode 100644
87+
index 0000000000..7d0bb8c8bc
88+
--- /dev/null
89+
+++ b/lib/jscoq_extern.c
90+
@@ -0,0 +1,4 @@
91+
+#include <caml/mlvalues.h>
92+
+
93+
+// jsCoq Stub; actual implementation is in backend/jsoo/js_stub/interrupt.js
94+
+value interrupt_pending() { return Val_false; }
95+
--
96+
2.43.0
97+

0 commit comments

Comments
 (0)