forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs_of_lam_string.ml
130 lines (103 loc) · 3.59 KB
/
js_of_lam_string.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
(* OCamlScript compiler
* Copyright (C) 2015-2016 Bloomberg Finance L.P.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, with linking exception;
* either version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*)
(* Author: Hongbo Zhang *)
module E = J_helper.Exp
module A = struct
let const_char (i : char) =
E.str (String.make 1 i)
let caml_char_of_int ?comment (v : J.expression) =
E.char_of_int ?comment v
let caml_char_to_int ?comment v =
E.char_to_int ?comment v
(* string [s[i]] expects to return a [ocaml_char] *)
let ref_string e e1 =
E.string_access e e1
(* [s[i]] excepts to return a [ocaml_char]
We use normal array for [bytes]
TODO: we can use [Buffer] in the future
*)
let ref_byte e e0 =
E.char_of_int (E.access e e0)
(* {Bytes.set : bytes -> int -> char -> unit }*)
let set_byte e e0 e1 =
E.assign (E.access e e0) (E.char_to_int e1)
(*
Note that [String.fromCharCode] also works, but it only
work for small arrays, however, for {bytes_to_string} it is likely the bytes
will become big
{[
String.fromCharCode.apply(null,[87,97])
"Wa"
String.fromCharCode(87,97)
"Wa"
]}
This does not work for large arrays
{[
String.fromCharCode.apply(null, prim = Array[1048576])
Maxiume call stack size exceeded
]}
*)
let bytes_to_string e =
E.runtime_call J_helper.string "bytes_to_string" [e]
let bytes_of_string s =
E.runtime_call J_helper.string "bytes_of_string" [s]
end
(* We use module B for string compilation, once the upstream can make changes to the
patten match of range patterns, we can use module [A] which means [char] is [string] in js,
currently, it follows the same patten of ocaml, [char] is [int]
*)
module B = struct
let const_char (i : char) =
E.int ~comment:("\"" ^ Ext_string.escaped (String.make 1 i) ^ "\"")
~c:i (Char.code i)
let caml_char_of_int ?comment (v : J.expression) = v
let caml_char_to_int ?comment v = v
(* string [s[i]] expects to return a [ocaml_char] *)
let ref_string e e1 =
E.char_to_int (E.string_access e e1)
(* [s[i]] excepts to return a [ocaml_char]
We use normal array for [bytes]
TODO: we can use [Buffer] in the future
*)
let ref_byte e e0 = E.access e e0
(* {Bytes.set : bytes -> int -> char -> unit }*)
let set_byte e e0 e1 =
E.assign (E.access e e0) e1
(**
Note that [String.fromCharCode] also works, but it only
work for small arrays, however, for {bytes_to_string} it is likely the bytes
will become big
{[
String.fromCharCode.apply(null,[87,97])
"Wa"
String.fromCharCode(87,97)
"Wa"
]}
This does not work for large arrays
{[
String.fromCharCode.apply(null, prim = Array[1048576])
Maxiume call stack size exceeded
]}
*)
let bytes_to_string e =
E.runtime_call J_helper.string "bytes_to_string" [e]
let bytes_of_string s =
E.runtime_call J_helper.string "bytes_of_string" [s]
end
(* include A *)
include B