File tree Expand file tree Collapse file tree 3 files changed +30
-0
lines changed
Expand file tree Collapse file tree 3 files changed +30
-0
lines changed Original file line number Diff line number Diff line change @@ -12,13 +12,36 @@ type bytecode_sections =
1212
1313external 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+
1537let () =
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
Original file line number Diff line number Diff 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+
370375let get code i = Char. code code.[i]
371376
372377let getu code pc =
Original file line number Diff line number Diff line change @@ -197,6 +197,8 @@ type desc =
197197 ; opcode : int
198198 }
199199
200+ val find : t -> desc
201+
200202val get_instr_exn : string -> int -> desc
201203
202204val gets : string -> int -> int
You can’t perform that action at this time.
0 commit comments