Skip to content

Commit 2ce8593

Browse files
committed
Compiler: fix for 12430
1 parent bfedc8a commit 2ce8593

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

compiler/lib-dynlink/js_of_ocaml_compiler_dynlink.ml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,36 @@ type bytecode_sections =
1212

1313
external get_bytecode_sections : unit -> bytecode_sections = "jsoo_get_bytecode_sections"
1414

15+
let normalize_bytecode code =
16+
match Ocaml_version.v with
17+
| `V4_08
18+
| `V4_09
19+
| `V4_10
20+
| `V4_11
21+
| `V4_12
22+
| `V4_13
23+
| `V4_14
24+
| `V5_00
25+
| `V5_01
26+
| `V5_02 -> code
27+
| `V5_03 ->
28+
(* starting with ocaml 5.3. The toplevel no longer append [RETURN 1] *)
29+
let { Instr.opcode; _ } = Instr.find RETURN in
30+
let len = String.length code in
31+
let b = Bytes.create (len + 8) in
32+
Bytes.blit_string ~src:code ~src_pos:0 ~dst:b ~dst_pos:0 ~len;
33+
Bytes.set_int32_le b len (Int32.of_int opcode);
34+
Bytes.set_int32_le b (len + 4) 1l;
35+
Bytes.to_string b
36+
1537
let () =
1638
let global = J.pure_js_expr "globalThis" in
1739
Config.Flag.set "use-js-string" (Jsoo_runtime.Sys.Config.use_js_string ());
1840
Config.Flag.set "effects" (Jsoo_runtime.Sys.Config.effects ());
1941
(* this needs to stay synchronized with toplevel.js *)
2042
let toplevel_compile (s : string) (debug : Instruct.debug_event list array) :
2143
unit -> J.t =
44+
let s = normalize_bytecode s in
2245
let prims = Array.of_list (Ocaml_compiler.Symtable.all_primitives ()) in
2346
let b = Buffer.create 100 in
2447
let fmt = Pretty_print.to_buffer b in

compiler/lib/instr.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,11 @@ let ops =
367367
in
368368
ops
369369

370+
let find i =
371+
match Array.find_opt ~f:(fun { code; _ } -> Poly.(i = code)) ops with
372+
| None -> assert false
373+
| Some x -> x
374+
370375
let get code i = Char.code code.[i]
371376

372377
let getu code pc =

compiler/lib/instr.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,8 @@ type desc =
197197
; opcode : int
198198
}
199199

200+
val find : t -> desc
201+
200202
val get_instr_exn : string -> int -> desc
201203

202204
val gets : string -> int -> int

0 commit comments

Comments
 (0)