Skip to content

Commit 4856456

Browse files
committed
WIP
1 parent cd951da commit 4856456

File tree

5 files changed

+78
-20
lines changed

5 files changed

+78
-20
lines changed

compiler/lib/js_output.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -741,8 +741,9 @@ struct
741741
let n =
742742
match k with
743743
| { async = false; generator = false } -> n
744-
| { async = false; generator = true } -> n ^ "*"
745-
| { async = true; generator = true | false } -> assert false
744+
| { async = false; generator = true } -> "*" ^ n
745+
| { async = true; generator = false } -> "async " ^ n
746+
| { async = true; generator = true } -> "async* " ^ n
746747
in
747748
function_declaration f "" PP.string (Some n) l b loc'
748749
| PropertySpread e ->

compiler/lib/js_parser.mly

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -787,8 +787,12 @@ property_name_and_value:
787787
| T_SET name=id args=call_signature "{" b=function_body "}" { PropertySet(name,args,b) }
788788
| name=id args=call_signature "{" b=function_body "}" {
789789
PropertyMethod(name, (None, {async = false; generator = false}, args, b, p $symbolstartpos)) }
790-
| name=id "*" args=call_signature "{" b=function_body "}" {
790+
| T_ASYNC name=id args=call_signature "{" b=function_body "}" {
791+
PropertyMethod(name, (None, {async = true; generator = false}, args, b, p $symbolstartpos)) }
792+
| "*" name=id args=call_signature "{" b=function_body "}" {
791793
PropertyMethod(name, (None, {async = false; generator = true}, args, b, p $symbolstartpos)) }
794+
| T_ASYNC "*" name=id args=call_signature "{" b=function_body "}" {
795+
PropertyMethod(name, (None, {async = true; generator = true}, args, b, p $symbolstartpos)) }
792796
| "[" p=assignment_expr "]" ":" e=assignment_expr { PropertyComputed (p,e) }
793797
(*----------------------------*)
794798
(* function call *)

compiler/tests-compiler/js_parser_printer.ml

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@
2020
open Js_of_ocaml_compiler.Stdlib
2121
open Js_of_ocaml_compiler
2222

23-
let print ?(report = false) ~compact source =
23+
let print ?(report = false) ?(invalid = false) ~compact source =
24+
let stdout = Util.check_javascript_source source in
25+
(match invalid, stdout with
26+
| false, _ -> print_endline stdout
27+
| true, "" -> print_endline "invalid file but node --check didn't complain"
28+
| true, _ -> ());
2429
let buffer = Buffer.create (String.length source) in
2530
let pp = Pretty_print.to_buffer buffer in
2631
Pretty_print.set_compact pp compact;
@@ -29,7 +34,14 @@ let print ?(report = false) ~compact source =
2934
let parsed = Parse_js.parse lexed in
3035
Config.Flag.enable "debuginfo";
3136
let _ = Js_output.program pp parsed in
32-
print_endline (Buffer.contents buffer)
37+
let s = Buffer.contents buffer in
38+
print_endline s;
39+
let stdout = Util.check_javascript_source s in
40+
(match invalid, stdout with
41+
| false, _ -> print_endline stdout
42+
| true, "" -> print_endline "invalid file but node --check didn't complain"
43+
| true, _ -> ());
44+
print_endline stdout
3345
with Parse_js.Parsing_error pi as e ->
3446
if report
3547
then
@@ -132,7 +144,7 @@ let%expect_test "ops" =
132144
~compact:false
133145
{|
134146
a += a;
135-
b ||= b;
147+
b ||= true;
136148
c **= b ** 2;
137149
1 ** 2;
138150
(-1) ** 2;
@@ -154,7 +166,7 @@ let%expect_test "ops" =
154166
[%expect
155167
{|
156168
/*<< 2 4>>*/ a += a;
157-
/*<< 3 4>>*/ b ||= b;
169+
/*<< 3 4>>*/ b ||= true;
158170
/*<< 4 4>>*/ c **= b ** 2;
159171
/*<< 5 4>>*/ 1 ** 2;
160172
/*<< 6 4>>*/ (- 1) ** 2;
@@ -308,7 +320,9 @@ let%expect_test "get/set property" =
308320
set prop(x) { return x == 2 },
309321
a : 4,
310322
b() { return 5},
311-
e*() { return 5},
323+
*e() { return 5},
324+
async e() { return 5},
325+
async* e() { return 5},
312326
["field" + 1]: 3
313327
};
314328

@@ -322,11 +336,14 @@ let%expect_test "get/set property" =
322336
set prop(x){ /*<< 4 21>>*/ return x == 2},
323337
a:4,
324338
b(){ /*<< 6 13>>*/ return 5 /*<< 6 7>>*/ },
325-
e*(){ /*<< 7 14>>*/ return 5 /*<< 7 7>>*/ },
339+
*e(){ /*<< 7 14>>*/ return 5 /*<< 7 7>>*/ },
340+
async e(){ /*<< 8 19>>*/ return 5 /*<< 8 7>>*/ },
341+
async* e(){ /*<< 9 20>>*/ return 5 /*<< 9 7>>*/ },
326342
["field" + 1]:3}; |}]
327343

328344
let%expect_test "error reporting" =
329-
(try print ~compact:false {|
345+
(try
346+
print ~invalid:true ~compact:false {|
330347
var x = 2;
331348
{
332349
var = 5;
@@ -404,7 +421,12 @@ let check_vs_string s toks =
404421
in
405422
loop 0 0 toks
406423

407-
let parse_print_token ?(extra = false) s =
424+
let parse_print_token ?(invalid = false) ?(extra = false) s =
425+
let stdout = Util.check_javascript_source s in
426+
(match invalid, stdout with
427+
| false, _ -> print_endline stdout
428+
| true, "" -> print_endline "invalid file but node --check didn't complain"
429+
| true, _ -> ());
408430
let lex = Parse_js.Lexer.of_string s in
409431
let _p, tokens =
410432
try Parse_js.parse' lex
@@ -446,6 +468,7 @@ let%expect_test "tokens" =
446468

447469
let%expect_test "invalid ident" =
448470
parse_print_token
471+
~invalid:true
449472
{|
450473
var \uD83B\uDE62 = 42; // invalid surrogate escape sequence
451474
var \u{1F42B} = 2; // U+1F42B is not a valid id
@@ -473,7 +496,7 @@ let%expect_test "string" =
473496
5: 4:var, 8:a, 10:=, 12:"munpi\207\128\207\128\207\128qtex", 26:;, |}]
474497

475498
let%expect_test "multiline string" =
476-
parse_print_token {|
499+
parse_print_token ~invalid:true {|
477500
42;
478501
"
479502
";
@@ -497,7 +520,7 @@ let%expect_test "multiline string" =
497520
3: 4:" ",
498521
4: 5:;,
499522
5: 4:42, 0:;, |}];
500-
parse_print_token {|
523+
parse_print_token ~invalid:true {|
501524
42;
502525
"
503526
@@ -588,7 +611,7 @@ let%expect_test "virtual semicolon" =
588611
return 2
589612
return
590613
2
591-
614+
a:while(true){
592615
continue;
593616
continue a
594617
continue
@@ -598,7 +621,7 @@ let%expect_test "virtual semicolon" =
598621
break a
599622
break
600623
a
601-
624+
}
602625
throw 2;
603626
throw 2
604627

@@ -618,6 +641,7 @@ let%expect_test "virtual semicolon" =
618641
3: 4:return, 11:2, 0:; (virtual),
619642
4: 4:return, 0:; (virtual),
620643
5: 4:2, 0:; (virtual),
644+
6: 0:a (identifier), 1::, 2:while, 7:(, 8:true, 12:), 13:{,
621645
7: 4:continue, 12:;,
622646
8: 4:continue, 13:a (identifier), 0:; (virtual),
623647
9: 4:continue, 0:; (virtual),
@@ -626,6 +650,7 @@ let%expect_test "virtual semicolon" =
626650
13: 4:break, 10:a (identifier), 0:; (virtual),
627651
14: 4:break, 0:; (virtual),
628652
15: 4:a (identifier), 0:; (virtual),
653+
16: 0:},
629654
17: 4:throw, 10:2, 11:;,
630655
18: 4:throw, 10:2, 0:; (virtual),
631656
20: 4:{, 6:1, 0:; (virtual),

compiler/tests-compiler/util/util.ml

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ let channel_to_string c_in =
172172
(try loop () with End_of_file -> ());
173173
Buffer.contents buffer
174174

175-
let exec_to_string_exn ~fail ~cmd =
175+
let exec_to_string_exn ?input ~fail ~cmd () =
176176
let build_path_prefix_map = "BUILD_PATH_PREFIX_MAP=" in
177177
let cwd = Sys.getcwd () in
178178
let build_path =
@@ -204,7 +204,12 @@ let exec_to_string_exn ~fail ~cmd =
204204
| WSTOPPED i ->
205205
Format.sprintf "%s\nprocess stopped with signal number %d\n %s\n" std_out i cmd
206206
in
207-
let ((proc_in, _, proc_err) as proc_full) = Unix.open_process_full cmd env in
207+
let ((proc_in, oc, proc_err) as proc_full) = Unix.open_process_full cmd env in
208+
(match input with
209+
| None -> ()
210+
| Some s ->
211+
output_string oc s;
212+
close_out oc);
208213
let results = channel_to_string proc_in in
209214
let results' = channel_to_string proc_err in
210215
let exit_status = Unix.close_process_full proc_full in
@@ -229,11 +234,22 @@ let run_javascript file =
229234
exec_to_string_exn
230235
~fail:false
231236
~cmd:(Format.sprintf "%s %s" node (Filetype.path_of_js_file file))
237+
()
238+
239+
let check_javascript file =
240+
exec_to_string_exn
241+
~fail:false
242+
~cmd:(Format.sprintf "%s --check %s" node (Filetype.path_of_js_file file))
243+
()
244+
245+
let check_javascript_source source =
246+
exec_to_string_exn ~input:source ~fail:false ~cmd:(Format.sprintf "%s --check" node) ()
232247

233248
let run_bytecode file =
234249
exec_to_string_exn
235250
~fail:false
236251
~cmd:(Format.sprintf "%s %s" ocamlrun (Filetype.path_of_bc_file file))
252+
()
237253

238254
let swap_extention filename ~ext =
239255
Format.sprintf "%s.%s" (Filename.remove_extension filename) ext
@@ -296,11 +312,16 @@ let compile_to_javascript
296312
in
297313
let cmd = Format.sprintf "%s %s %s -o %s" compiler_location extra_args file out_file in
298314

299-
let stdout = exec_to_string_exn ~fail:true ~cmd in
315+
let stdout = exec_to_string_exn ~fail:true ~cmd () in
300316
print_string stdout;
301317
(* this print shouldn't do anything, so if
302318
something weird happens, we'll get the results here *)
303-
Filetype.js_file_of_path out_file
319+
let jsfile = Filetype.js_file_of_path out_file in
320+
let stdout = check_javascript jsfile in
321+
print_string stdout;
322+
(* this print shouldn't do anything, so if
323+
something weird happens, we'll get the results here *)
324+
jsfile
304325

305326
let jsoo_minify ?(flags = []) ~pretty file =
306327
let file = Filetype.path_of_js_file file in
@@ -312,7 +333,7 @@ let jsoo_minify ?(flags = []) ~pretty file =
312333
in
313334
let cmd = Format.sprintf "%s %s %s -o %s" compiler_location extra_args file out_file in
314335

315-
let stdout = exec_to_string_exn ~fail:true ~cmd in
336+
let stdout = exec_to_string_exn ~fail:true ~cmd () in
316337
print_string stdout;
317338
(* this print shouldn't do anything, so if
318339
something weird happens, we'll get the results here *)
@@ -356,6 +377,7 @@ let compile_ocaml_to_cmo ?(debug = true) file =
356377
(if debug then "-g" else "")
357378
file
358379
out_file)
380+
()
359381
in
360382
print_string stdout;
361383
Filetype.cmo_file_of_path out_file
@@ -374,6 +396,7 @@ let compile_ocaml_to_bc ?(debug = true) ?(unix = false) file =
374396
(if unix then "-I +unix unix.cma" else "")
375397
file
376398
out_file)
399+
()
377400
in
378401
print_string stdout;
379402
Filetype.bc_file_of_path out_file
@@ -389,6 +412,7 @@ let compile_lib list name =
389412
ocamlc
390413
(String.concat ~sep:" " (List.map ~f:Filetype.path_of_cmo_file list))
391414
out_file)
415+
()
392416
in
393417
print_string stdout;
394418
Filetype.cmo_file_of_path out_file

compiler/tests-compiler/util/util.mli

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ val extract_sourcemap : Filetype.js_file -> Js_of_ocaml_compiler.Source_map.t op
5757

5858
val run_javascript : Filetype.js_file -> string
5959

60+
val check_javascript : Filetype.js_file -> string
61+
62+
val check_javascript_source : string -> string
63+
6064
val expression_to_string : ?compact:bool -> Javascript.expression -> string
6165

6266
val print_file : string -> unit

0 commit comments

Comments
 (0)