ocsigen / js_of_ocaml

Compiler from OCaml to Javascript.
http://ocsigen.org/js_of_ocaml/
Other
943 stars 185 forks source link

[FEATURE REQUEST] Ability to impede sharing constants #1619

Closed nojb closed 1 month ago

nojb commented 1 month ago

Using gen_js_api, if we try to bind require (from Node) with

let _ = Ojs.apply (Ojs.variable "require") [|Ojs.string_to_js "foo"|]
let _ = Ojs.apply (Ojs.variable "require") [|Ojs.string_to_js "foo"|]

the compiler shares the constants "foo":

//# 6 ".main.eobjs/jsoo/dune__exe__Main.cmo.js"
(function
  (globalThis){
   "use strict";
   var runtime = globalThis.jsoo_runtime, cst_foo = "foo";
   require(cst_foo);
   require(cst_foo);
   var Dune_exe_Main = [0];
   runtime.caml_register_global(4, Dune_exe_Main, "Dune__exe__Main");
   return;
  }
  (globalThis));

However, since require is a special form, it would be better if we could avoid sharing it and simply inline the string argument "foo" in each callsite. (The same sharing happens if we use the string "foo" in two places anywhere in the file.)

Is there a way to achieve this? If not, could some way to achieve this be added (perhaps overloading the meaning of Sys.opaque_identity when compiling to JS?)

See https://github.com/LexiFi/gen_js_api/issues/66, https://github.com/LexiFi/gen_js_api/pull/68, and https://github.com/LexiFi/gen_js_api/pull/174 for some related background.

nojb commented 1 month ago

It is not clear this is the right way to go about it, so closing! Sorry for the noise.