Skip to content

Commit 30cc905

Browse files
committed
Preprocessor: use the export name to name functions without id
This provides a better debugging experience since reference to these functions will now use an id rather than a number in disassembled code.
1 parent 4db0a80 commit 30cc905

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

compiler/lib-wasm/wat_preprocess.ml

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,11 @@ let type_name (t : typ) =
288288
| String -> "string"
289289
| Version -> "version"
290290

291+
let variable_is_set st nm =
292+
match StringMap.find_opt nm st.variables with
293+
| Some (Bool true) -> true
294+
| _ -> false
295+
291296
let check_type ?typ expr actual_typ =
292297
match typ with
293298
| None -> ()
@@ -520,6 +525,25 @@ and rewrite st elt =
520525
| { desc = List ({ desc = Atom "@string"; _ } :: _ :: _ :: { loc; _ } :: _); _ } ->
521526
raise
522527
(Error (position_of_loc loc, Printf.sprintf "Expecting a closing parenthesis.\n"))
528+
| { desc =
529+
List
530+
({ desc = Atom "func"; loc = _, pos }
531+
:: { desc =
532+
List
533+
[ { desc = Atom "export"; _ }
534+
; { desc = Atom export_name; loc = export_loc }
535+
]
536+
; loc = pos', _
537+
}
538+
:: l)
539+
; _
540+
}
541+
when variable_is_set st "name-wasm-functions"
542+
&& is_id ("$" ^ parse_string export_loc export_name) ->
543+
write st pos;
544+
insert st (Printf.sprintf " $%s " (parse_string export_loc export_name));
545+
skip st pos';
546+
rewrite_list st l
523547
| { desc = List l; _ } -> rewrite_list st l
524548
| _ -> ()
525549

@@ -529,12 +553,14 @@ let ocaml_version =
529553
Scanf.sscanf Sys.ocaml_version "%d.%d.%d" (fun major minor patchlevel ->
530554
Version (major, minor, patchlevel))
531555

556+
let default_settings = [ "name-wasm-functions", Bool true ]
557+
532558
let f ~variables ~filename ~contents:text =
533559
let variables =
534560
List.fold_left
535561
~f:(fun m (k, v) -> StringMap.add k v m)
536562
~init:StringMap.empty
537-
variables
563+
(default_settings @ variables)
538564
in
539565
let variables = StringMap.add "ocaml_version" ocaml_version variables in
540566
let lexbuf = Sedlexing.Utf8.from_string text in

manual/wasm_runtime.wiki

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ To form conditional expressions, the following operators are available:
6262

6363
It also provides some syntactic sugar to write strings. The expression{{{(@string "ab")}}} is expanded into {{{(array.new_fixed $bytes 2 (i32.const 97) (i32.const 98))}}}. The statement {{{(@string $s "ab")}}} is an abbreviation for {{{(global $s (ref eq) (@string "ab"))}}}.
6464

65+
To provide a better debugging experience, the function export name is used to name functions with no explicit id: {{{(func (export "foo") ...)}}}} is expanded into {{{(func $foo (export "foo") ...)}}}}.
66+
6567
== Implementing primitives ==
6668

6769
You define a primitive by exporting a Wasm function with parameters and return value of type {{{(ref eq)}}}.

runtime/wasm/jslib_js_of_ocaml.wat

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,20 @@
2727
(import "jslib" "caml_js_from_array"
2828
(func $caml_js_from_array (param (ref eq)) (result (ref eq))))
2929
(import "js" "caml_js_html_escape"
30-
(func $caml_js_html_escape (param anyref) (result anyref)))
30+
(func $caml_js_html_escape_js (param anyref) (result anyref)))
3131
(import "js" "caml_js_html_entities"
32-
(func $caml_js_html_entities (param anyref) (result anyref)))
32+
(func $caml_js_html_entities_js (param anyref) (result anyref)))
3333

3434
(type $block (array (mut (ref eq))))
3535
(type $bytes (array (mut i8)))
3636

3737
(func (export "caml_js_html_escape") (param (ref eq)) (result (ref eq))
3838
(return_call $wrap
39-
(call $caml_js_html_escape (call $unwrap (local.get 0)))))
39+
(call $caml_js_html_escape_js (call $unwrap (local.get 0)))))
4040

4141
(func (export "caml_js_html_entities") (param (ref eq)) (result (ref eq))
4242
(return_call $wrap
43-
(call $caml_js_html_entities (call $unwrap (local.get 0)))))
43+
(call $caml_js_html_entities_js (call $unwrap (local.get 0)))))
4444

4545
(@string $console "console")
4646

0 commit comments

Comments
 (0)