Skip to content

Commit c2a1961

Browse files
committed
Merge pull request rescript-lang#163 from bloomberg/format_primitive_support
Format primitive support
2 parents 55436c9 + 1d8cb36 commit c2a1961

13 files changed

+528
-157
lines changed

jscomp/ext_ident.ml

+4-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,10 @@ let reserved_words =
123123
(* also reserved in ECMAScript 3 *)
124124
"abstract"; "boolean"; "byte"; "char"; "const"; "double";
125125
"final"; "float"; "goto"; "int"; "long"; "native"; "short";
126-
"synchronized"; "throws"; "transient"; "volatile";
126+
"synchronized";
127+
(* "throws"; *)
128+
(* seems to be fine, like nodejs [assert.throws] *)
129+
"transient"; "volatile";
127130

128131
(* also reserved in ECMAScript 6 *)
129132
"await";

jscomp/runtime/caml_format.d.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
export var parse_digit: (c : any) => any ;
22
export var caml_invalid_argument: (s : any) => any ;
33
export var repeat: any ;
4+
export var parse_sign_and_base: (s : any) => any ;
5+
export var caml_failwith: (s : any) => any ;
46
export var caml_format_float: any ;
57
export var caml_format_int: any ;
68
export var caml_nativeint_format: (prim : any, prim : any) => any ;
79
export var caml_int32_format: (prim : any, prim : any) => any ;
810
export var caml_float_of_string: any ;
9-
export var caml_int_of_string: any ;
10-
export var caml_int32_of_string: (prim : any) => any ;
11-
export var caml_nativeint_of_string: (prim : any) => any ;
11+
export var caml_int_of_string: (s : any) => any ;
12+
export var caml_int32_of_string: (s : any) => any ;
13+
export var caml_nativeint_of_string: (s : any) => any ;
1214

jscomp/runtime/caml_format.js

+127-74
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44
var Caml_builtin_exceptions = require("./caml_builtin_exceptions");
55
var Caml_utils = require("./caml_utils");
66

7+
function caml_failwith(s) {
8+
throw [
9+
Caml_builtin_exceptions.failure,
10+
s
11+
];
12+
}
13+
714
function caml_invalid_argument(s) {
815
throw [
916
Caml_builtin_exceptions.invalid_argument,
@@ -36,75 +43,125 @@ function parse_digit(c) {
3643
}
3744
}
3845

39-
40-
41-
/**
42-
*
43-
* @param {string} s
44-
* @returns {number[]}
45-
*/
46-
function $$parse_sign_and_base(s) {
47-
var i = 0;
48-
var len = s.length;
49-
var base = 10;
50-
var sign = (len > 0 && s.charCodeAt(0) == 45) ? (i++, -1) : 1;
51-
if (i + 1 < len && s.charCodeAt(i) == 48)
52-
switch (s.charCodeAt(i + 1)) {
53-
case 120:
54-
case 88:
55-
base = 16;
56-
i += 2;
57-
break;
58-
case 111:
59-
case 79:
60-
base = 8;
61-
i += 2;
62-
break;
63-
case 98:
64-
case 66:
65-
base = 2;
66-
i += 2;
67-
break;
46+
function parse_sign_and_base(s) {
47+
var sign = 1;
48+
var base = 10;
49+
var i = 0;
50+
if (s[i] === "-") {
51+
sign = -1;
52+
++ i;
53+
}
54+
var match = s.charCodeAt(i);
55+
var match$1 = s.charCodeAt(i + 1);
56+
if (match === 48) {
57+
if (match$1 >= 89) {
58+
if (match$1 !== 98) {
59+
if (match$1 !== 111) {
60+
if (match$1 === 120) {
61+
base = 16;
62+
i += 2;
63+
}
64+
65+
}
66+
else {
67+
base = 8;
68+
i += 2;
69+
}
70+
}
71+
else {
72+
base = 2;
73+
i += 2;
74+
}
75+
}
76+
else if (match$1 !== 66) {
77+
if (match$1 !== 79) {
78+
if (match$1 >= 88) {
79+
base = 16;
80+
i += 2;
6881
}
69-
return [i, sign, base];
82+
83+
}
84+
else {
85+
base = 8;
86+
i += 2;
87+
}
88+
}
89+
else {
90+
base = 2;
91+
i += 2;
92+
}
93+
}
94+
return /* tuple */[
95+
i,
96+
sign,
97+
base
98+
];
7099
}
71100

72-
73-
/**
74-
*
75-
* @param {string} s
76-
* @returns {number}
77-
*/
78-
function $$caml_int_of_string(s) {
79-
var _a = $$parse_sign_and_base(s), i = _a[0], sign = _a[1], base = _a[2];
80-
var len = s.length;
81-
var threshold = -1 >>> 0;
82-
var c = (i < len) ? s.charCodeAt(i) : 0;
83-
var d = parse_digit(c);
84-
if (d < 0 || d >= base)
85-
caml_failwith("int_of_string");
86-
var res = d;
87-
for (i++; i < len; i++) {
88-
c = s.charCodeAt(i);
89-
if (c == 95)
90-
continue;
91-
d = parse_digit(c);
92-
if (d < 0 || d >= base)
93-
break;
94-
res = base * res + d;
95-
if (res > threshold)
96-
caml_failwith("int_of_string");
97-
}
98-
if (i != len)
99-
caml_failwith("int_of_string");
100-
// For base different from 10, we expect an unsigned representation,
101-
// hence any value of 'res' (less than 'threshold') is acceptable.
102-
// But we have to convert the result back to a signed integer.
103-
res = sign * res;
104-
if ((base == 10) && ((res | 0) != res))
105-
/* Signed representation expected, allow -2^(nbits-1) to 2^(nbits-1) - 1 */
106-
caml_failwith("int_of_string");
107-
return res | 0;
101+
function caml_int_of_string(s) {
102+
var match = parse_sign_and_base(s);
103+
var base = match[2];
104+
var i = match[0];
105+
var threshold = (-1 >>> 0);
106+
var len = s.length;
107+
var c = i < len ? s.charCodeAt(i) : /* "\000" */0;
108+
var d = parse_digit(c);
109+
if (d < 0 || d >= base) {
110+
throw [
111+
Caml_builtin_exceptions.failure,
112+
"int_of_string"
113+
];
114+
}
115+
var aux = function (_acc, _k) {
116+
while(true) {
117+
var k = _k;
118+
var acc = _acc;
119+
if (k === len) {
120+
return acc;
121+
}
122+
else {
123+
var a = s.charCodeAt(k);
124+
if (a === /* "_" */95) {
125+
_k = k + 1;
126+
continue ;
127+
128+
}
129+
else {
130+
var v = parse_digit(a);
131+
if (v < 0 || v >= base) {
132+
throw [
133+
Caml_builtin_exceptions.failure,
134+
"int_of_string"
135+
];
136+
}
137+
else {
138+
var acc$1 = base * acc + v;
139+
if (acc$1 > threshold) {
140+
throw [
141+
Caml_builtin_exceptions.failure,
142+
"int_of_string"
143+
];
144+
}
145+
else {
146+
_k = k + 1;
147+
_acc = acc$1;
148+
continue ;
149+
150+
}
151+
}
152+
}
153+
}
154+
};
155+
};
156+
var res = match[1] * aux(d, i + 1);
157+
var or_res = res | 0;
158+
if (base === 10 && res !== or_res) {
159+
throw [
160+
Caml_builtin_exceptions.failure,
161+
"int_of_string"
162+
];
163+
}
164+
return or_res;
108165
}
109166

110167

@@ -402,10 +459,6 @@ function caml_int32_format(prim, prim$1) {
402459
return $$caml_format_int(prim, prim$1);
403460
}
404461

405-
function caml_int32_of_string(prim) {
406-
return $$caml_int_of_string(prim);
407-
}
408-
409462
var repeat = Caml_utils.repeat;
410463

411464
function caml_format_float(prim, prim$1) {
@@ -420,15 +473,15 @@ function caml_float_of_string(prim) {
420473
return $$caml_float_of_string(prim);
421474
}
422475

423-
function caml_int_of_string(prim) {
424-
return $$caml_int_of_string(prim);
425-
}
476+
var caml_int32_of_string = caml_int_of_string;
426477

427-
var caml_nativeint_of_string = caml_int32_of_string;
478+
var caml_nativeint_of_string = caml_int_of_string;
428479

429480
exports.parse_digit = parse_digit;
430481
exports.caml_invalid_argument = caml_invalid_argument;
431482
exports.repeat = repeat;
483+
exports.parse_sign_and_base = parse_sign_and_base;
484+
exports.caml_failwith = caml_failwith;
432485
exports.caml_format_float = caml_format_float;
433486
exports.caml_format_int = caml_format_int;
434487
exports.caml_nativeint_format = caml_nativeint_format;

0 commit comments

Comments
 (0)