Description
I'm not sure what would be required of JavaScript compilation but as part of rehp it had occurred to me that we could output much better string representations if the standard library of OCaml moved more of its string operations to externs:
For example the definition of ^
:
external string_length : string -> int = "%string_length"
external bytes_length : bytes -> int = "%bytes_length"
external bytes_create : int -> bytes = "caml_create_bytes"
external string_blit : string -> int -> bytes -> int -> int -> unit
= "caml_blit_string" [@@noalloc]
external bytes_blit : bytes -> int -> bytes -> int -> int -> unit
= "caml_blit_bytes" [@@noalloc]
external bytes_unsafe_to_string : bytes -> string = "%bytes_to_string"
let ( ^ ) s1 s2 =
let l1 = string_length s1 and l2 = string_length s2 in
let s = bytes_create (l1 + l2) in
string_blit s1 0 s 0 l1;
string_blit s2 0 s l1 l2;
bytes_unsafe_to_string s
If compiling using the jsoo bytecode decompiler, and when targeting PHP/Hack, we could actually just emit plain string concatenation in the output. I also believe this is possible in JavaScript as well (by implementing a toString()
on MlBytes
). But this would require intercepting the ^
which is currently not defined as an extern. It seems it could be implemented as an external without much downside.
If possible, this would improve readability of the output and improve performance of ^
.