Skip to content

Commit e593721

Browse files
committed
Introduce PervasivesU, uncurried pervasives, in uncurried-always mode.
1 parent 3dc7864 commit e593721

12 files changed

+1604
-5
lines changed

jscomp/core/res_compmisc.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ let initial_env () =
4646
let initial = Env.initial_safe_string in
4747
let env =
4848
if !Clflags.nopervasives then initial
49-
else open_implicit_module "Pervasives" initial
49+
else open_implicit_module (if !Config.use_automatic_curried_application then "PervasivesU" else "Pervasives") initial
5050
in
5151
List.fold_left
5252
(fun env m -> open_implicit_module m env)

jscomp/frontend/ast_config.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ let add_signature k v =
3939
signature_config_table := Map_string.add !signature_config_table k v
4040

4141
let process_directives str =
42-
Js_config.directives := []; (* Restt: multiple calls possible e.g. with bsc from the command-line *)
42+
Js_config.directives := []; (* Reset: multiple calls possible e.g. with bsc from the command-line *)
4343
str |> List.iter(fun (item : Parsetree.structure_item) -> match item.pstr_desc with
4444
| Pstr_attribute ({ txt = "directive" },
4545
PStr [ { pstr_desc = Pstr_eval ({ pexp_desc = Pexp_constant (Pconst_string (d, _)) }, _) } ]) ->

jscomp/stdlib-406/pervasivesU.res

+319
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,319 @@
1+
/* ************************************************************************ */
2+
/* */
3+
/* OCaml */
4+
/* */
5+
/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
6+
/* */
7+
/* Copyright 1996 Institut National de Recherche en Informatique et */
8+
/* en Automatique. */
9+
/* */
10+
/* All rights reserved. This file is distributed under the terms of */
11+
/* the GNU Lesser General Public License version 2.1, with the */
12+
/* special exception on linking described in the file LICENSE. */
13+
/* */
14+
/* ************************************************************************ */
15+
16+
@@uncurriedAlways
17+
18+
/* Internal */
19+
external __unsafe_cast: 'a => 'b = "%identity"
20+
21+
/* Exceptions */
22+
23+
external raise: exn => 'a = "%raise"
24+
external raise_notrace: exn => 'a = "%raise_notrace"
25+
26+
let failwith = s => raise(Failure(s))
27+
let invalid_arg = s => raise(Invalid_argument(s))
28+
29+
exception Exit
30+
31+
/* Composition operators */
32+
33+
external \"|>": ('a, 'a => 'b) => 'b = "%revapply"
34+
external \"@@": ('a => 'b, 'a) => 'b = "%apply"
35+
36+
/* Debugging */
37+
38+
external __LOC__: string = "%loc_LOC"
39+
external __FILE__: string = "%loc_FILE"
40+
external __LINE__: int = "%loc_LINE"
41+
external __MODULE__: string = "%loc_MODULE"
42+
external __POS__: (string, int, int, int) = "%loc_POS"
43+
44+
external __LOC_OF__: 'a => (string, 'a) = "%loc_LOC"
45+
external __LINE_OF__: 'a => (int, 'a) = "%loc_LINE"
46+
external __POS_OF__: 'a => ((string, int, int, int), 'a) = "%loc_POS"
47+
48+
/* Comparisons */
49+
50+
external \"=": ('a, 'a) => bool = "%equal"
51+
external \"<>": ('a, 'a) => bool = "%notequal"
52+
external \"<": ('a, 'a) => bool = "%lessthan"
53+
external \">": ('a, 'a) => bool = "%greaterthan"
54+
external \"<=": ('a, 'a) => bool = "%lessequal"
55+
external \">=": ('a, 'a) => bool = "%greaterequal"
56+
external compare: ('a, 'a) => int = "%compare"
57+
external min: ('a, 'a) => 'a = "%bs_min"
58+
external max: ('a, 'a) => 'a = "%bs_max"
59+
external \"==": ('a, 'a) => bool = "%eq"
60+
external \"!=": ('a, 'a) => bool = "%noteq"
61+
62+
/* Boolean operations */
63+
64+
external not: bool => bool = "%boolnot"
65+
66+
external \"&&": (bool, bool) => bool = "%sequand"
67+
68+
external \"||": (bool, bool) => bool = "%sequor"
69+
70+
/* Integer operations */
71+
72+
external \"~-": int => int = "%negint"
73+
external \"~+": int => int = "%identity"
74+
external succ: int => int = "%succint"
75+
external pred: int => int = "%predint"
76+
external \"+": (int, int) => int = "%addint"
77+
external \"-": (int, int) => int = "%subint"
78+
external \"*": (int, int) => int = "%mulint"
79+
external \"/": (int, int) => int = "%divint"
80+
external mod: (int, int) => int = "%modint"
81+
82+
let abs = x =>
83+
if x >= 0 {
84+
x
85+
} else {
86+
-x
87+
}
88+
89+
external land: (int, int) => int = "%andint"
90+
external lor: (int, int) => int = "%orint"
91+
external lxor: (int, int) => int = "%xorint"
92+
93+
let lnot = x => lxor(x, -1)
94+
95+
external lsl: (int, int) => int = "%lslint"
96+
external lsr: (int, int) => int = "%lsrint"
97+
external asr: (int, int) => int = "%asrint"
98+
99+
let max_int = lsr(-1, 1)
100+
let min_int = max_int + 1
101+
102+
/* Floating-point operations */
103+
104+
external \"~-.": float => float = "%negfloat"
105+
external \"~+.": float => float = "%identity"
106+
external \"+.": (float, float) => float = "%addfloat"
107+
external \"-.": (float, float) => float = "%subfloat"
108+
external \"*.": (float, float) => float = "%mulfloat"
109+
external \"/.": (float, float) => float = "%divfloat"
110+
111+
@val @scope("Math") external \"**": (float, float) => float = "pow"
112+
@val @scope("Math") external exp: float => float = "exp"
113+
external expm1: float => float = "?expm1_float"
114+
115+
@val @scope("Math") external acos: float => float = "acos"
116+
@val @scope("Math") external asin: float => float = "asin"
117+
@val @scope("Math") external atan: float => float = "atan"
118+
@val @scope("Math") external atan2: (float, float) => float = "atan2"
119+
external hypot: (float, float) => float = "?hypot_float"
120+
121+
@val @scope("Math") external cos: float => float = "cos"
122+
@val @scope("Math") external cosh: float => float = "cosh"
123+
@val @scope("Math") external log: float => float = "log"
124+
@val @scope("Math") external log10: float => float = "log10"
125+
@val @scope("Math") external log1p: float => float = "log1p"
126+
@val @scope("Math") external sin: float => float = "sin"
127+
@val @scope("Math") external sinh: float => float = "sinh"
128+
@val @scope("Math") external sqrt: float => float = "sqrt"
129+
@val @scope("Math") external tan: float => float = "tan"
130+
@val @scope("Math") external tanh: float => float = "tanh"
131+
@val @scope("Math") external ceil: float => float = "ceil"
132+
@val @scope("Math") external floor: float => float = "floor"
133+
@val @scope("Math") external abs_float: float => float = "abs"
134+
external copysign: (float, float) => float = "?copysign_float"
135+
external mod_float: (float, float) => float = "?fmod_float"
136+
external frexp: float => (float, int) = "?frexp_float"
137+
external ldexp: (float, int) => float = "?ldexp_float"
138+
external modf: float => (float, float) = "?modf_float"
139+
external float: int => float = "%floatofint"
140+
external float_of_int: int => float = "%floatofint"
141+
external truncate: float => int = "%intoffloat"
142+
external int_of_float: float => int = "%intoffloat"
143+
144+
let infinity = 0x1p2047
145+
let neg_infinity = -0x1p2047
146+
@val @scope("Number") external nan: float = "NaN"
147+
let max_float = 1.79769313486231571e+308 /* 0x1.ffff_ffff_ffff_fp+1023 */
148+
let min_float = 2.22507385850720138e-308 /* 0x1p-1022 */
149+
let epsilon_float = 2.22044604925031308e-16 /* 0x1p-52 */
150+
151+
type fpclass =
152+
| FP_normal
153+
| FP_subnormal
154+
| FP_zero
155+
| FP_infinite
156+
| FP_nan
157+
158+
let classify_float = (x: float): fpclass =>
159+
if (%raw(`isFinite`): _ => _)(x) {
160+
if abs_float(x) >= /* 0x1p-1022 */ /* 2.22507385850720138e-308 */ min_float {
161+
FP_normal
162+
} else if x != 0. {
163+
FP_subnormal
164+
} else {
165+
FP_zero
166+
}
167+
} else if (%raw(`isNaN`): _ => _)(x) {
168+
FP_nan
169+
} else {
170+
FP_infinite
171+
}
172+
173+
/* String and byte sequence operations -- more in modules String and Bytes */
174+
175+
external string_length: string => int = "%string_length"
176+
177+
external \"^": (string, string) => string = "#string_append"
178+
/* Character operations -- more in module Char */
179+
180+
external int_of_char: char => int = "%identity"
181+
external unsafe_char_of_int: int => char = "%identity"
182+
let char_of_int = n =>
183+
if n < 0 || n > 255 {
184+
invalid_arg("char_of_int")
185+
} else {
186+
unsafe_char_of_int(n)
187+
}
188+
189+
/* Unit operations */
190+
191+
external ignore: 'a => unit = "%ignore"
192+
193+
/* Pair operations */
194+
195+
external fst: (('a, 'b)) => 'a = "%field0"
196+
external snd: (('a, 'b)) => 'b = "%field1"
197+
198+
/* References */
199+
200+
type ref<'a> = {mutable contents: 'a}
201+
external ref: 'a => ref<'a> = "%makemutable"
202+
external \"!": ref<'a> => 'a = "%bs_ref_field0"
203+
external \":=": (ref<'a>, 'a) => unit = "%bs_ref_setfield0"
204+
external incr: ref<int> => unit = "%incr"
205+
external decr: ref<int> => unit = "%decr"
206+
207+
/* Result type */
208+
209+
type result<'a, 'b> = Belt.Result.t<'a, 'b> =
210+
| Ok('a)
211+
| Error('b)
212+
213+
/* String conversion functions */
214+
external format_float: (string, float) => string = "?format_float"
215+
216+
let string_of_bool = b =>
217+
if b {
218+
"true"
219+
} else {
220+
"false"
221+
}
222+
let bool_of_string = param =>
223+
switch param {
224+
| "true" => true
225+
| "false" => false
226+
| _ => invalid_arg("bool_of_string")
227+
}
228+
229+
let bool_of_string_opt = param =>
230+
switch param {
231+
| "true" => Some(true)
232+
| "false" => Some(false)
233+
| _ => None
234+
}
235+
236+
@val external string_of_int: int => string = "String"
237+
238+
external int_of_string: string => int = "?int_of_string"
239+
240+
let int_of_string_opt = s =>
241+
/* TODO: provide this directly as a non-raising primitive. */
242+
try Some(int_of_string(s)) catch {
243+
| Failure(_) => None
244+
}
245+
246+
external string_get: (string, int) => char = "%string_safe_get"
247+
248+
let valid_float_lexem = s => {
249+
let l = string_length(s)
250+
let rec loop = i =>
251+
if i >= l {
252+
s ++ "."
253+
} else {
254+
switch string_get(s, i) {
255+
| '0' .. '9' | '-' => loop(i + 1)
256+
| _ => s
257+
}
258+
}
259+
260+
loop(0)
261+
}
262+
263+
let string_of_float = f => valid_float_lexem(format_float("%.12g", f))
264+
265+
external float_of_string: string => float = "?float_of_string"
266+
267+
let float_of_string_opt = s =>
268+
/* TODO: provide this directly as a non-raising primitive. */
269+
try Some(float_of_string(s)) catch {
270+
| Failure(_) => None
271+
}
272+
273+
/* List operations -- more in module List */
274+
275+
let rec \"@" = (l1, l2) =>
276+
switch l1 {
277+
| list{} => l2
278+
| list{hd, ...tl} => list{hd, ...\"@"(tl, l2)}
279+
}
280+
281+
/* Output functions on standard output */
282+
283+
@val @scope("console") external print_endline: string => unit = "log"
284+
let print_newline = () => print_endline("")
285+
286+
/* Output functions on standard error */
287+
288+
@val @scope("console") external prerr_endline: string => unit = "error"
289+
let prerr_newline = () => prerr_endline("")
290+
291+
let print_int = (i: int) => print_endline(string_of_int(i))
292+
let print_float = (i: float) => print_endline(string_of_float(i))
293+
let print_string = print_endline
294+
295+
/* Miscellaneous */
296+
297+
external sys_exit: int => 'a = "?sys_exit"
298+
299+
let exit_function = ref(ignore)
300+
301+
let at_exit = f => {
302+
let g = exit_function.contents
303+
exit_function :=
304+
(
305+
() => {
306+
f()
307+
g()
308+
}
309+
)
310+
}
311+
312+
let do_at_exit = () => exit_function.contents()
313+
314+
let exit = retcode => {
315+
do_at_exit()
316+
sys_exit(retcode)
317+
}
318+
319+
type int32 = int

0 commit comments

Comments
 (0)