Skip to content

Commit d926d69

Browse files
authored
generalize raw (#5125)
* generalize raw * fix CI
1 parent 2e983f7 commit d926d69

20 files changed

+12489
-29573
lines changed

Diff for: jscomp/frontend/ast_attributes.ml

+2-1
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,8 @@ let bs_get_arity : attr
354354

355355
let bs_set : attr
356356
= {txt = "bs.set"; loc = locg}, Ast_payload.empty
357-
357+
let internal_expansive : attr
358+
= {txt = "internal.expansive"; loc = locg}, Ast_payload.empty
358359
let bs_return_undefined : attr
359360
=
360361
{txt = "bs.return"; loc = locg },

Diff for: jscomp/frontend/ast_attributes.mli

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,5 +105,5 @@ val bs_get_index : attr
105105
val bs_get_arity : attr
106106
val bs_set : attr
107107
val bs_return_undefined : attr
108-
108+
val internal_expansive : attr
109109
(* val deprecated : string -> attr *)

Diff for: jscomp/frontend/ast_exp_handle_external.ml

+7-2
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,21 @@ let handle_debugger loc (payload : Ast_payload.t) =
7575

7676

7777
let handle_raw ~kind loc payload =
78+
let is_function = ref false in
7879
begin match Ast_payload.raw_as_string_exp_exn
79-
~kind payload with
80+
~kind ~is_function payload with
8081
| None ->
8182
Location.raise_errorf ~loc
8283
"bs.raw can only be applied to a string"
8384
| Some exp ->
8485
{ exp with pexp_desc = Ast_external_mk.local_external_apply
8586
loc ~pval_prim:["#raw_expr"]
8687
~pval_type:(Typ.arrow Nolabel (Typ.any ()) (Typ.any ()))
87-
[exp]}
88+
[exp];
89+
pexp_attributes = if !is_function then
90+
Ast_attributes.internal_expansive :: exp.pexp_attributes
91+
else exp.pexp_attributes
92+
}
8893
end
8994
let handle_raw_structure loc payload =
9095
begin match Ast_payload.raw_as_string_exp_exn

Diff for: jscomp/frontend/ast_external_mk.ml

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
2-
*
2+
* Copyright (C) 2017 - Hongbo Zhang, Authors of ReScript
33
* This program is free software: you can redistribute it and/or modify
44
* it under the terms of the GNU Lesser General Public License as published by
55
* the Free Software Foundation, either version 3 of the License, or
@@ -82,7 +82,7 @@ let local_external_obj loc
8282
pexp_loc = loc} : Parsetree.expression) args ~loc
8383
)
8484

85-
let local_extern_cont loc
85+
let local_extern_cont_to_obj loc
8686
?(pval_attributes=[])
8787
~pval_prim
8888
~pval_type
@@ -109,4 +109,4 @@ let local_extern_cont loc
109109
loc};
110110
pexp_attributes = [] ;
111111
pexp_loc = loc}
112-
)
112+
)

Diff for: jscomp/frontend/ast_external_mk.mli

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
2-
*
1+
(* Copyright (C) 2015 - 2016 Bloomberg Finance L.P.
2+
* Copyright (C) 2017 - Hongbo Zhang, Authors of ReScript
33
* This program is free software: you can redistribute it and/or modify
44
* it under the terms of the GNU Lesser General Public License as published by
55
* the Free Software Foundation, either version 3 of the License, or
@@ -55,7 +55,7 @@ val local_external_obj :
5555

5656

5757

58-
val local_extern_cont :
58+
val local_extern_cont_to_obj :
5959
Location.t ->
6060
?pval_attributes:Parsetree.attributes ->
6161
pval_prim:string list ->

Diff for: jscomp/frontend/ast_payload.ml

+11-3
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ let is_single_int (x : t ) : int option =
7070

7171

7272
let raw_as_string_exp_exn
73-
~(kind: Js_raw_info.raw_kind)
74-
(x : t ) : _ option =
73+
~(kind: Js_raw_info.raw_kind) ?(is_function)
74+
(x : t ) : Parsetree.expression option =
7575
match x with (** TODO also need detect empty phrase case *)
7676
| PStr [ {
7777
pstr_desc =
@@ -82,15 +82,23 @@ let raw_as_string_exp_exn
8282
;
8383
pexp_loc = loc} as e ,_);
8484
_}] ->
85+
8586
Bs_flow_ast_utils.check_flow_errors ~loc ~offset:(Bs_flow_ast_utils.flow_deli_offset deli) (match kind with
8687
| Raw_re
8788
| Raw_exp ->
88-
let (_loc,e),errors = Parser_flow.parse_expression (Parser_env.init_env None str) false in
89+
let ((_loc,e) as prog),errors = Parser_flow.parse_expression (Parser_env.init_env None str) false in
8990
if kind = Raw_re then
9091
(match e with
9192
| Literal {value = RegExp _} -> ()
9293
| _ -> Location.raise_errorf ~loc "Syntax error: a valid JS regex literal expected"
9394
);
95+
(match is_function with
96+
| Some is_function ->
97+
begin match Classify_function.classify_exp prog with
98+
| Js_function {arity= _; _} -> is_function := true
99+
| _ -> ()
100+
end
101+
| None -> ());
94102
errors
95103
| Raw_program ->
96104
snd (Parser_flow.parse_program false None str)

Diff for: jscomp/frontend/ast_payload.mli

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ val is_single_int : t -> int option
4545
(** Convert %raw into expression *)
4646
val raw_as_string_exp_exn :
4747
kind: Js_raw_info.raw_kind ->
48+
?is_function:bool ref ->
4849
t ->
4950
Parsetree.expression option
5051

Diff for: jscomp/frontend/ast_util.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ let ocaml_obj_as_js_object
225225
label.Asttypes.txt
226226
label_type acc
227227
) in
228-
Ast_external_mk.local_extern_cont
228+
Ast_external_mk.local_extern_cont_to_obj
229229
loc
230230
~pval_prim:(Ast_external_process.pval_prim_of_labels labels)
231231
(fun e ->

Diff for: jscomp/frontend/classify_function.mli

+2
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,7 @@ val classify :
2828
string ->
2929
Js_raw_info.exp
3030

31+
val classify_exp :
32+
(Loc.t, Loc.t) Flow_ast.Expression.t -> Js_raw_info.exp
3133
val classify_stmt :
3234
string -> Js_raw_info.stmt

Diff for: jscomp/snapshot.ninja

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ OCAML_SRC_TOOLS=$native_ocaml_path/tools
1515
includes = -I js_parser -I stubs -I ext -I common -I frontend -I depends -I core -I super_errors -I outcome_printer -I bsb -I ounit -I ounit_tests -I napkin -I main
1616
SNAP=../lib/$snapshot_path
1717

18-
o snapshot: phony $SNAP/whole_compiler.ml $SNAP/bsb_helper.ml $SNAP/bsb.ml $SNAP/unstable/all_ounit_tests.ml
18+
o snapshot: phony $SNAP/whole_compiler.ml $SNAP/bsb_helper.ml $SNAP/unstable/all_ounit_tests.ml
1919
# $SNAP/bspp.ml
2020
o $SNAP/whole_compiler.ml: bspack | ./bin/bspack.exe $LTO
2121
flags = ${releaseMode} -D BS_ONLY=true -bs-MD -module-alias Config=Config_whole_compiler -bs-exclude-I config -I $OCAML_SRC_UTILS -I $OCAML_SRC_PARSING -I $OCAML_SRC_TYPING -I $OCAML_SRC_BYTECOMP -I $OCAML_SRC_DRIVER $includes
@@ -26,9 +26,9 @@ o $SNAP/bsb_helper.ml: bspack | ./bin/bspack.exe $LTO
2626
flags = -bs-MD ${releaseMode} -I stubs -I common -I ext -I frontend -I depends -I bsb_helper -I main
2727
main = Bsb_helper_main
2828

29-
o $SNAP/bsb.ml: bspack | ./bin/bspack.exe $LTO
30-
flags = -D BS_MIN_LEX_DEPS=true -bs-MD ${releaseMode} -I $OCAML_SRC_UTILS -I $OCAML_SRC_PARSING -I stubs -I common -I ext -I frontend -I depends -I bsb -I ext -I main
31-
main = Bsb_main
29+
# o $SNAP/bsb.ml: bspack | ./bin/bspack.exe $LTO
30+
# flags = -D BS_MIN_LEX_DEPS=true -bs-MD ${releaseMode} -I $OCAML_SRC_UTILS -I $OCAML_SRC_PARSING -I stubs -I common -I ext -I frontend -I depends -I bsb -I ext -I main
31+
# main = Bsb_main
3232

3333
o $SNAP/rescript.ml: bspack | ./bin/bspack.exe $LTO
3434
flags = -D BS_MIN_LEX_DEPS=true -bs-MD ${releaseMode} -I $OCAML_SRC_UTILS -I $OCAML_SRC_PARSING -I stubs -I common -I ext -I frontend -I depends -I bsb -I ext -I main

Diff for: jscomp/test/build.ninja

+1
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,7 @@ o test/poly_empty_array.cmi test/poly_empty_array.cmj : cc test/poly_empty_array
480480
o test/poly_type.cmi test/poly_type.cmj : cc test/poly_type.ml | $stdlib
481481
o test/poly_variant_test.cmj : cc_cmi test/poly_variant_test.ml | test/mt.cmj test/poly_variant_test.cmi $stdlib
482482
o test/poly_variant_test.cmi : cc test/poly_variant_test.mli | $stdlib
483+
o test/polymorphic_raw_test.cmi test/polymorphic_raw_test.cmj : cc test/polymorphic_raw_test.ml | test/mt.cmj $stdlib
483484
o test/polymorphism_test.cmj : cc_cmi test/polymorphism_test.ml | test/polymorphism_test.cmi $stdlib
484485
o test/polymorphism_test.cmi : cc test/polymorphism_test.mli | $stdlib
485486
o test/polyvar_convert.cmi test/polyvar_convert.cmj : cc test/polyvar_convert.ml | $stdlib

Diff for: jscomp/test/polymorphic_raw_test.js

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
'use strict';
2+
3+
var Mt = require("./mt.js");
4+
5+
var suites = {
6+
contents: /* [] */0
7+
};
8+
9+
var test_id = {
10+
contents: 0
11+
};
12+
13+
function eq(loc, x, y) {
14+
test_id.contents = test_id.contents + 1 | 0;
15+
suites.contents = {
16+
hd: [
17+
loc + (" id " + String(test_id.contents)),
18+
(function (param) {
19+
return {
20+
TAG: /* Eq */0,
21+
_0: x,
22+
_1: y
23+
};
24+
})
25+
],
26+
tl: suites.contents
27+
};
28+
29+
}
30+
31+
var f = ((a) => typeof a);
32+
33+
var a = f(3);
34+
35+
var b = f("3");
36+
37+
eq("File \"polymorphic_raw_test.ml\", line 22, characters 6-13", a, "number");
38+
39+
eq("File \"polymorphic_raw_test.ml\", line 23, characters 6-13", b, "string");
40+
41+
Mt.from_pair_suites("polymorphic_raw_test.ml", suites.contents);
42+
43+
exports.suites = suites;
44+
exports.test_id = test_id;
45+
exports.eq = eq;
46+
exports.f = f;
47+
exports.a = a;
48+
exports.b = b;
49+
/* a Not a pure module */

Diff for: jscomp/test/polymorphic_raw_test.ml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
[@@@config {
2+
flags = [|
3+
4+
|]
5+
}]
6+
7+
let suites : Mt.pair_suites ref = ref []
8+
let test_id = ref 0
9+
let eq loc x y =
10+
incr test_id ;
11+
suites :=
12+
(loc ^" id " ^ (string_of_int !test_id), (fun _ -> Mt.Eq(x,y))) :: !suites
13+
14+
15+
16+
17+
let f : _ -> string = [%raw{| (a) => typeof a |}]
18+
19+
let a = f 3
20+
let b = f "3"
21+
22+
;; eq __LOC__ a "number"
23+
;; eq __LOC__ b "string"
24+
;; Mt.from_pair_suites __FILE__ !suites

0 commit comments

Comments
 (0)