Skip to content

Commit 64518c5

Browse files
committed
add string test, prepare utf16le encoding support
1 parent f472b2b commit 64518c5

9 files changed

+98
-47
lines changed

jscomp/others/js_json.ml

+11-9
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,20 @@ type _ kind =
3434
| Null : Js_types.null_val kind
3535

3636
let reify_type (type a) (x : 'a) : (a kind * a ) =
37-
if Js.typeof x = "string" then
38-
(Obj.magic String, Obj.magic x) else
37+
(if Js.typeof x = "string" then
38+
Obj.magic String else
3939
if Js.typeof x = "number" then
40-
(Obj.magic Number, Obj.magic x ) else
41-
if Js.typeof x = "boolean" then (* which one is faster, save [Js.typeof] or not *)
42-
(Obj.magic Boolean, Obj.magic x) else
43-
if (Obj.magic x) == Js.null then (* providing a universal function *)
44-
(Obj.magic Null, Obj.magic x) else
40+
Obj.magic Number else
41+
if Js.typeof x = "boolean" then
42+
(* which one is faster, save [Js.typeof] or not *)
43+
Obj.magic Boolean else
44+
if (Obj.magic x) == Js.null then
45+
(* providing a universal function *)
46+
Obj.magic Null else
4547
if Js.to_bool @@ Js_array.isArray x then
46-
(Obj.magic Array, Obj.magic x )
48+
Obj.magic Array
4749
else
48-
(Obj.magic Object, Obj.magic x )
50+
Obj.magic Object ), Obj.magic x
4951

5052
let test (type a) (x : 'a) (v : a kind) : bool =
5153
match v with

jscomp/others/js_string.ml

+2-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ external charAt : int -> t = "" [@@bs.send.pipe: t]
5151

5252
external charCodeAt : int -> float = "" [@@bs.send.pipe: t]
5353
(** type it as [float] due to that it may return NAN *)
54-
external concat : t -> t = "" [@@bs.send.pipe: t]
54+
external concat : t array -> t = ""
55+
[@@bs.send.pipe: t] [@@bs.splice]
5556

5657
external indexOf : t -> int = "" [@@bs.send.pipe: t]
5758
external indexOf_from : t -> int -> int = "indexOf" [@@bs.send.pipe: t]

jscomp/test/.depend

+8
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,8 @@ js_json_test.cmj : ../runtime/js.cmj
364364
js_json_test.cmx : ../runtime/js.cmx
365365
js_obj_test.cmj : mt.cmi
366366
js_obj_test.cmx : mt.cmx
367+
js_string_test.cmj : mt.cmi ../runtime/js.cmj
368+
js_string_test.cmx : mt.cmx ../runtime/js.cmx
367369
js_val.cmj :
368370
js_val.cmx :
369371
jsoo_400_test.cmj : ../stdlib/string.cmi mt.cmi
@@ -600,6 +602,8 @@ string_test.cmj : ../stdlib/string.cmi mt.cmi ../stdlib/list.cmi \
600602
ext_string.cmj ../stdlib/bytes.cmi
601603
string_test.cmx : ../stdlib/string.cmx mt.cmx ../stdlib/list.cmx \
602604
ext_string.cmx ../stdlib/bytes.cmx
605+
string_unicode_test.cmj : ../runtime/js.cmj
606+
string_unicode_test.cmx : ../runtime/js.cmx
603607
stringmatch_test.cmj : ../stdlib/string.cmi
604608
stringmatch_test.cmx : ../stdlib/string.cmx
605609
swap_test.cmj : ../stdlib/printf.cmi mt.cmi ../stdlib/list.cmi \
@@ -1192,6 +1196,8 @@ js_json_test.cmo : ../runtime/js.cmo
11921196
js_json_test.cmj : ../runtime/js.cmj
11931197
js_obj_test.cmo : mt.cmi
11941198
js_obj_test.cmj : mt.cmj
1199+
js_string_test.cmo : mt.cmi ../runtime/js.cmo
1200+
js_string_test.cmj : mt.cmj ../runtime/js.cmj
11951201
js_val.cmo :
11961202
js_val.cmj :
11971203
jsoo_400_test.cmo : ../stdlib/string.cmi mt.cmi
@@ -1428,6 +1434,8 @@ string_test.cmo : ../stdlib/string.cmi mt.cmi ../stdlib/list.cmi \
14281434
ext_string.cmo ../stdlib/bytes.cmi
14291435
string_test.cmj : ../stdlib/string.cmj mt.cmj ../stdlib/list.cmj \
14301436
ext_string.cmj ../stdlib/bytes.cmj
1437+
string_unicode_test.cmo : ../runtime/js.cmo
1438+
string_unicode_test.cmj : ../runtime/js.cmj
14311439
stringmatch_test.cmo : ../stdlib/string.cmi
14321440
stringmatch_test.cmj : ../stdlib/string.cmj
14331441
swap_test.cmo : ../stdlib/printf.cmi mt.cmi ../stdlib/list.cmi \

jscomp/test/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ OTHERS := literals a test_ari test_export2 test_internalOO test_obj_simple_ffi t
6767
ppx_this_obj_field single_module_alias \
6868
sexpm sexpm_test sexp js_json_test array_subtle_test \
6969
bytes_split_gpr_743_test module_splice_test hello.foo \
70-
ffi_splice_test node_path_test
70+
ffi_splice_test node_path_test js_string_test string_unicode_test
7171

7272

7373

jscomp/test/js_string_test.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'use strict';
2+
3+
var Mt = require("./mt");
4+
var Block = require("../../lib/js/block");
5+
6+
var suites = [/* [] */0];
7+
8+
var test_id = [0];
9+
10+
function eq(loc, _z) {
11+
var y = _z[1];
12+
var x = _z[0];
13+
console.log(_z);
14+
test_id[0] = test_id[0] + 1 | 0;
15+
suites[0] = /* :: */[
16+
/* tuple */[
17+
loc + (" id " + test_id[0]),
18+
function () {
19+
return /* Eq */Block.__(0, [
20+
x,
21+
y
22+
]);
23+
}
24+
],
25+
suites[0]
26+
];
27+
return /* () */0;
28+
}
29+
30+
eq('File "js_string_test.ml", line 13, characters 5-12', /* tuple */[
31+
"012",
32+
"0".concat("1", "2")
33+
]);
34+
35+
Mt.from_pair_suites("js_string_test.ml", suites[0]);
36+
37+
exports.suites = suites;
38+
exports.test_id = test_id;
39+
exports.eq = eq;
40+
/* Not a pure module */

jscomp/test/js_string_test.ml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
let suites : Mt.pair_suites ref = ref []
3+
let test_id = ref 0
4+
5+
let eq loc ((x, y) as _z) =
6+
Js.log _z;
7+
incr test_id ;
8+
suites :=
9+
(loc ^" id " ^ (string_of_int !test_id), (fun _ -> Mt.Eq(x,y))) :: !suites
10+
11+
12+
let () =
13+
eq __LOC__ ("012", "0" |> Js.String.concat [|"1";"2"|])
14+
15+
let () =
16+
Mt.from_pair_suites __FILE__ !suites

jscomp/test/string_unicode_test.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
'use strict';
2+
3+
4+
console.log("\xe4\xbd\xa0\xe5\xa5\xbd");
5+
6+
/* Not a pure module */

jscomp/test/string_unicode_test.ml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
Js.log "你好"

lib/js/js_json.js

+12-36
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,18 @@
22

33

44
function reify_type(x) {
5-
if (typeof x === "string") {
6-
return /* tuple */[
7-
/* String */0,
8-
x
9-
];
10-
}
11-
else if (typeof x === "number") {
12-
return /* tuple */[
13-
/* Number */1,
14-
x
15-
];
16-
}
17-
else if (typeof x === "boolean") {
18-
return /* tuple */[
19-
/* Boolean */4,
20-
x
21-
];
22-
}
23-
else if (x === null) {
24-
return /* tuple */[
25-
/* Null */5,
26-
x
27-
];
28-
}
29-
else if (Array.isArray(x)) {
30-
return /* tuple */[
31-
/* Array */3,
32-
x
33-
];
34-
}
35-
else {
36-
return /* tuple */[
37-
/* Object */2,
38-
x
39-
];
40-
}
5+
return /* tuple */[
6+
typeof x === "string" ? /* String */0 : (
7+
typeof x === "number" ? /* Number */1 : (
8+
typeof x === "boolean" ? /* Boolean */4 : (
9+
x === null ? /* Null */5 : (
10+
Array.isArray(x) ? /* Array */3 : /* Object */2
11+
)
12+
)
13+
)
14+
),
15+
x
16+
];
4117
}
4218

4319
function test(x, v) {

0 commit comments

Comments
 (0)