Skip to content

Commit 7adb48a

Browse files
committed
reduce runtime without using js primitives
1 parent b1e1245 commit 7adb48a

26 files changed

+209
-44
lines changed

jscomp/runtime/.depend

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ caml_float.cmj : typed_array.cmj js_float.cmj caml_float.cmi
1212
caml_lexer.cmj : caml_lexer.cmi
1313
caml_parser.cmj : caml_parser.cmi
1414
caml_primitive.cmj : caml_primitive.cmi
15-
caml_format.cmj : js_nativeint.cmj js_int64.cmj js_int.cmj js_float.cmj \
16-
caml_utils.cmj bs_string.cmj caml_format.cmi
15+
caml_format.cmj : js_nativeint.cmj js_int64.cmj js_float.cmj caml_utils.cmj \
16+
bs_string.cmj caml_format.cmi
1717
caml_md5.cmj : bs_string.cmj caml_md5.cmi
1818
caml_queue.cmj : caml_queue.cmi
1919
caml_hash.cmj : js_undefined.cmj js.cmj caml_queue.cmj bs_string.cmj \

jscomp/runtime/bs_obj.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ external set_length : any -> int -> unit = "#obj_set_length"
2929
external length : any -> int = "#obj_length"
3030
external tag : any -> int = "caml_obj_tag"
3131
external set_tag : any -> int -> unit = "caml_obj_set_tag"
32-
(* external uninitialized_object : int -> int -> any = "#uninitialized_object" *)
32+
3333
external is_instance_array : any -> bool =
3434
"#is_instance_array" (* use Array.isArray instead*)
3535
external size_of_any : any -> 'a Js.undefined =

jscomp/runtime/bs_string.ml

+3-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

25-
external string_of_char : char -> string = "#string_of_char"
25+
2626
(** TODO: check with {!String.of_char}
2727
it's quite common that we have
2828
{[ Bs_string.of_char x.[0] ]}
@@ -31,8 +31,9 @@ external string_of_char : char -> string = "#string_of_char"
3131
]}
3232
*)
3333

34+
(*ATT: this relies on we encode `char' as int *)
3435
external of_char : char -> string = "String.fromCharCode"
35-
[@@bs.val]
36+
[@@bs.val]
3637
external toUpperCase : string -> string = "toUpperCase" [@@bs.send]
3738
external of_int : int -> base:int -> string = "toString" [@@bs.send]
3839
external of_nativeint : nativeint -> base:int -> string = "toString" [@@bs.send]

jscomp/runtime/caml_array.ml

+3-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@
2424

2525

2626

27-
external new_uninitialized : int -> 'a array = "#create_array"
28-
external append : 'a array -> 'a array -> 'a array = "#array_append"
27+
external new_uninitialized : int -> 'a array = "Array" [@@bs.new]
28+
external append : 'a array -> 'a array -> 'a array = "concat" [@@bs.send]
29+
2930
external make : int -> 'a -> 'a array = "caml_make_vect"
3031

3132

jscomp/runtime/caml_array.mli

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525

2626

2727

28-
external new_uninitialized : int -> 'a array = "#create_array"
29-
external append : 'a array -> 'a array -> 'a array = "#array_append"
28+
external new_uninitialized : int -> 'a array = "Array" [@@bs.new]
29+
external append : 'a array -> 'a array -> 'a array = "concat" [@@bs.send]
3030
external make : int -> 'a -> 'a array = "caml_make_vect"
3131

3232
val caml_array_sub : 'a array -> int -> int -> 'a array

jscomp/runtime/caml_format.ml

+9-9
Original file line numberDiff line numberDiff line change
@@ -406,29 +406,29 @@ let caml_int64_format fmt x =
406406
ref (Int64.add quotient_l c ) in
407407
let modulus = ref d in
408408
s :=
409-
Bs_string.string_of_char
409+
Bs_string.of_char
410410
cvtbl.[ Int64.to_int !modulus] ^ !s ;
411411

412412
while !quotient <> 0L do
413413
let a, b = Js_int64.div_mod (!quotient) wbase in
414414
quotient := a;
415415
modulus := b;
416-
s := Bs_string.string_of_char cvtbl.[Int64.to_int !modulus] ^ !s ;
416+
s := Bs_string.of_char cvtbl.[Int64.to_int !modulus] ^ !s ;
417417
done;
418418
end
419419
else
420420
let a, b = Js_int64.div_mod x wbase in
421421
let quotient = ref a in
422422
let modulus = ref b in
423423
s :=
424-
Bs_string.string_of_char
424+
Bs_string.of_char
425425
cvtbl.[ Int64.to_int !modulus] ^ !s ;
426426

427427
while !quotient <> 0L do
428428
let a, b = Js_int64.div_mod (!quotient) wbase in
429429
quotient := a;
430430
modulus := b;
431-
s := Bs_string.string_of_char cvtbl.[Int64.to_int !modulus] ^ !s ;
431+
s := Bs_string.of_char cvtbl.[Int64.to_int !modulus] ^ !s ;
432432
done
433433

434434
| Dec ->
@@ -454,29 +454,29 @@ let caml_int64_format fmt x =
454454
e) in
455455
let modulus = ref f in
456456
s :=
457-
Bs_string.string_of_char
457+
Bs_string.of_char
458458
cvtbl.[Int64.to_int !modulus] ^ !s ;
459459

460460
while !quotient <> 0L do
461461
let a, b = Js_int64.div_mod (!quotient) wbase in
462462
quotient := a;
463463
modulus := b;
464-
s := Bs_string.string_of_char cvtbl.[Int64.to_int !modulus] ^ !s ;
464+
s := Bs_string.of_char cvtbl.[Int64.to_int !modulus] ^ !s ;
465465
done;
466466

467467
else
468468
let a, b = Js_int64.div_mod x wbase in
469469
let quotient = ref a in
470470
let modulus = ref b in
471471
s :=
472-
Bs_string.string_of_char
472+
Bs_string.of_char
473473
cvtbl.[ Int64.to_int !modulus] ^ !s ;
474474

475475
while !quotient <> 0L do
476476
let a, b = Js_int64.div_mod (!quotient) wbase in
477477
quotient := a;
478478
modulus := b;
479-
s := Bs_string.string_of_char cvtbl.[Int64.to_int !modulus] ^ !s ;
479+
s := Bs_string.of_char cvtbl.[Int64.to_int !modulus] ^ !s ;
480480
done;
481481
end;
482482
if f.prec >= 0 then
@@ -531,7 +531,7 @@ let caml_format_float fmt x =
531531
let prec = if prec <> 0 then prec else 1 in
532532
s := Js_float.to_exponential x (prec - 1);
533533
let j = Bs_string.index_of !s "e" in
534-
let exp = Js_int.from_any @@ Bs_string.slice_rest !s (j + 1) in
534+
let exp = int_of_float @@ Js_float.of_any @@ Bs_string.slice_rest !s (j + 1) in
535535
if exp < -4 || x >= 1e21 ||Bs_string.length (Js_float.toFixed x 0) > prec then
536536
let i = ref (j - 1) in
537537
while !s.[!i] = '0' do

jscomp/runtime/caml_string.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626

2727

28-
external new_uninitialized : int -> bytes = "#create_array"
28+
external new_uninitialized : int -> bytes = "Array" [@@bs.new]
2929
external to_int_array : bytes -> int array = "%identity"
3030
external of_int_array : int array -> bytes = "%identity"
3131

jscomp/runtime/js_float.ml

+3-1
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,7 @@ external max : float -> float -> float = "Math.max"
5252

5353
external random : unit -> float = "Math.random"
5454
[@@bs.val ]
55-
external of_any : 'a -> float = "#anything_to_number"
55+
56+
external of_any : 'a -> float = "Number"
57+
[@@bs.val]
5658

jscomp/runtime/js_int.ml

+7-1
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,10 @@
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

25-
external from_any : 'a -> int = "#anything_to_number" (* + conversion*)
25+
26+
(** If we use number, we need coerce to int32 by adding `|0`,
27+
otherwise `+0` can be wrong.
28+
Most JS API is float oriented, it may overflow int32 or
29+
comes with [NAN]
30+
*)
31+
(* + conversion*)

jscomp/syntax/ast_core_type.ml

+4
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ let is_user_bool (ty : t) =
104104
| Ptyp_constr({txt = Lident "bool"},[]) -> true
105105
| _ -> false
106106

107+
let is_user_int (ty : t) =
108+
match ty.ptyp_desc with
109+
| Ptyp_constr({txt = Lident "int"},[]) -> true
110+
| _ -> false
107111

108112
let is_optional_label l =
109113
String.length l > 0 && l.[0] = '?'

jscomp/syntax/ast_core_type.mli

+2
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ val is_user_option : t -> bool
7777

7878
val is_user_bool : t -> bool
7979

80+
val is_user_int : t -> bool
81+
8082
val is_optional_label : string -> bool
8183

8284
(**

jscomp/test/.depend

+3-2
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ class_repr.cmj : ../stdlib/oo.cmj
9999
class_setter_getter.cmj : ../runtime/js.cmj class_setter_getter.cmi
100100
class_test.cmj : mt.cmj
101101
class_type_ffi_test.cmj : ../runtime/js.cmj
102+
compare_test.cmj :
102103
complex_if_test.cmj : mt.cmj ../stdlib/bytes.cmj
103104
complex_test.cmj : mt.cmj ../stdlib/complex.cmj
104105
complex_while_loop.cmj :
@@ -241,8 +242,8 @@ int64_test.cmj : ../stdlib/pervasives.cmj ../stdlib/nativeint.cmj mt.cmj \
241242
int_hashtbl_test.cmj : mt.cmj ../stdlib/list.cmj ../stdlib/hashtbl.cmj \
242243
../stdlib/array.cmj
243244
int_map.cmj : ../stdlib/map.cmj
244-
int_overflow_test.cmj : ../stdlib/string.cmj mt.cmj ../stdlib/int32.cmj \
245-
../stdlib/char.cmj
245+
int_overflow_test.cmj : ../stdlib/string.cmj mt.cmj ../runtime/js_float.cmj \
246+
../stdlib/int32.cmj ../stdlib/char.cmj
246247
io_test.cmj : ../runtime/js.cmj
247248
js_array_test.cmj : mt.cmj ../runtime/js.cmj
248249
js_bool_test.cmj : mt.cmj ../runtime/js.cmj

jscomp/test/Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ OTHERS := literals a test_ari test_export2 test_internalOO test_obj_simple_ffi t
112112
test\
113113
undef_regression2_test\
114114
js_global_test\
115-
bang_primitive
115+
bang_primitive\
116+
compare_test
116117

117118

118119
# bs_uncurry_test

jscomp/test/bang_primitive.js

+8
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,13 @@ function test(x, y) {
1212
];
1313
}
1414

15+
function f(x, _) {
16+
return /* tuple */[
17+
String.fromCharCode.apply(null,x),
18+
0
19+
];
20+
}
21+
1522
exports.test = test;
23+
exports.f = f;
1624
/* No side effect */

jscomp/test/bang_primitive.ml

+15-1
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,18 @@ let test x y=
2727
ge x y,
2828
eq x y,
2929
neq x y
30-
30+
31+
(*
32+
external append : 'a array -> 'a array -> 'a array = "#array_append"
33+
34+
35+
let f x y = append x y
36+
*)
37+
38+
external of_small_int_array : int array -> string = "#string_of_small_int_array"
39+
(* external string_of_char : char -> string = "#string_of_char" *)
40+
(* string_of_char y *)
41+
42+
let f x y =
43+
of_small_int_array x, 0
44+

jscomp/test/compare_test.js

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
'use strict';
2+
3+
var Caml_obj = require("../../lib/js/caml_obj");
4+
5+
function compare(x, y) {
6+
switch (x) {
7+
case 0 :
8+
return +(y === /* A */0);
9+
case 1 :
10+
return +(y === /* B */1);
11+
case 2 :
12+
return +(y === /* C */2);
13+
14+
}
15+
}
16+
17+
function compare2(x, y) {
18+
switch (x) {
19+
case 0 :
20+
if (y !== 0) {
21+
return /* false */0;
22+
}
23+
else {
24+
return /* true */1;
25+
}
26+
case 1 :
27+
if (y !== 1) {
28+
return /* false */0;
29+
}
30+
else {
31+
return /* true */1;
32+
}
33+
case 2 :
34+
if (y >= 2) {
35+
return /* true */1;
36+
}
37+
else {
38+
return /* false */0;
39+
}
40+
41+
}
42+
}
43+
44+
var compare3 = Caml_obj.caml_equal;
45+
46+
exports.compare = compare;
47+
exports.compare2 = compare2;
48+
exports.compare3 = compare3;
49+
/* No side effect */

jscomp/test/compare_test.ml

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
type t =
2+
| A
3+
| B
4+
| C
5+
6+
let compare (x : t) (y : t) =
7+
match x with
8+
| A -> y = A
9+
| B -> y = B
10+
| C -> y = C
11+
12+
13+
14+
(* There is a downside in this way of writing
15+
if I comment [C,C] there will be no warning
16+
*)
17+
let compare2 (x : t) (y : t) =
18+
match x,y with
19+
| A,A
20+
| B,B
21+
| C,C
22+
-> true
23+
| (A|B|C),_ -> false
24+
25+
let compare3 (x : t) (y : t) =
26+
match x with
27+
| A -> x = y (* still polymoprhic equal *)
28+
| B -> x = y
29+
| C -> x = y

jscomp/test/float_test.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,13 @@ var param$8 = Caml_float.caml_expm1_float(0);
222222

223223
Mt_global.collect_eq(test_id, suites, 'File "float_test.ml", line 59, characters 5-12', param$8, 0);
224224

225-
var param$9 = Caml_float.caml_expm1_float(2);
225+
var param$9 = Number("3.0");
226226

227-
Mt_global.collect_approx(test_id, suites, 'File "float_test.ml", line 60, characters 9-16', param$9, 6.38905609893065);
227+
Mt_global.collect_eq(test_id, suites, 'File "float_test.ml", line 60, characters 5-12', param$9, 3.0);
228+
229+
var param$10 = Caml_float.caml_expm1_float(2);
230+
231+
Mt_global.collect_approx(test_id, suites, 'File "float_test.ml", line 61, characters 9-16', param$10, 6.38905609893065);
228232

229233
var match$4 = Caml_float.caml_modf_float(32.3);
230234

jscomp/test/float_test.ml

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ let () =
5757
eq __LOC__ (copysign (3.) (0.)) 3.;
5858
eq __LOC__ (log10 10.) 1.;
5959
eq __LOC__ (expm1 0.) 0. ;
60+
eq __LOC__ (Js_float.of_any "3.0") 3.0;
6061
approx __LOC__ (expm1 2.) 6.38905609893065
6162
;;
6263

0 commit comments

Comments
 (0)