Skip to content

Commit

Permalink
Compiler: modernize js ast
Browse files Browse the repository at this point in the history
  • Loading branch information
hhugo committed Jan 24, 2023
1 parent 6fcdcf1 commit 77f06f9
Show file tree
Hide file tree
Showing 25 changed files with 2,365 additions and 1,182 deletions.
81 changes: 41 additions & 40 deletions compiler/lib/driver.ml
Original file line number Diff line number Diff line change
Expand Up @@ -208,33 +208,31 @@ let gen_missing js missing =
let prim = Utf8_string.of_string_exn prim in
let p = ident prim in
( p
, Some
( ECond
( EBin
( NotEqEq
, EDot (EVar (ident Constant.global_object_), prim)
, EVar (ident_s "undefined") )
, EDot (EVar (ident Constant.global_object_), prim)
, EFun
( None
, []
, [ ( Statement
(Expression_statement
(ECall
( EVar (ident_s "caml_failwith")
, [ ( EBin
( Plus
, EStr prim
, EStr
(Utf8_string.of_string_exn
" not implemented") )
, `Not_spread )
]
, N )))
, N )
]
, N ) )
, N ) )
, ( ECond
( EBin
( NotEqEq
, dot (EVar (ident Constant.global_object_)) prim
, EVar (ident_s "undefined") )
, dot (EVar (ident Constant.global_object_)) prim
, EFun
( None
, { async = false; generator = false }
, []
, [ ( Statement
(Expression_statement
(call
(EVar (ident_s "caml_failwith"))
[ EBin
( Plus
, EStr prim
, EStr (Utf8_string.of_string_exn " not implemented")
)
]
N))
, N )
]
, N ) )
, N ) )
:: acc)
missing
[]
Expand All @@ -247,7 +245,7 @@ let gen_missing js missing =
warn "You can prevent the generation of dummy implementations with ";
warn "the commandline option '--disable genprim'@.";
report_missing_primitives missing);
(Statement (Variable_statement miss), N) :: js
(Statement (variable_declaration miss), N) :: js

let mark_start_of_generated_code = Debug.find ~even_if_quiet:true "mark-runtime-gen"

Expand Down Expand Up @@ -302,15 +300,15 @@ let link ~standalone ~linkall (js : Javascript.source_elements) : Linker.output
let all =
List.map all ~f:(fun name ->
let name = Utf8_string.of_string_exn name in
PNI name, EVar (ident name))
Property (PNI name, EVar (ident name)))
in
( Statement
(Expression_statement
(EBin
( Eq
, EDot
( EVar (ident Constant.global_object_)
, Utf8_string.of_string_exn "jsoo_runtime" )
, dot
(EVar (ident Constant.global_object_))
(Utf8_string.of_string_exn "jsoo_runtime")
, EObj all )))
, N )
:: js
Expand Down Expand Up @@ -394,7 +392,7 @@ let pack ~wrap_with_fun ~standalone { Linker.runtime_code = js; always_required_
(* pack *)
let wrap_in_iife ~use_strict js =
let var ident e =
J.Statement (J.Variable_statement [ J.ident ident, Some (e, J.N) ]), J.N
J.Statement (J.variable_declaration [ J.ident ident, (e, J.N) ]), J.N
in
let expr e = J.Statement (J.Expression_statement e), J.N in
let freenames =
Expand Down Expand Up @@ -427,8 +425,14 @@ let pack ~wrap_with_fun ~standalone { Linker.runtime_code = js; always_required_
else js
in

let efun args body = J.EFun (None, args, body, J.U) in
let sfun name args body = J.Function_declaration (name, args, body, J.U), J.U in
let efun args body =
J.EFun (None, { async = false; generator = false }, args, body, J.U)
in
let sfun name args body =
( J.Function_declaration
(name, { async = false; generator = false }, args, body, J.U)
, J.U )
in
let mk f =
let js = export_shim js in
let js = old_global_object_shim js in
Expand All @@ -437,17 +441,14 @@ let pack ~wrap_with_fun ~standalone { Linker.runtime_code = js; always_required_
then expr (J.EStr (Utf8_string.of_string_exn "use strict")) :: js
else js
in
f [ J.ident Constant.global_object_ ] js
f [ J.param Constant.global_object_ ] js
in
match wrap_with_fun with
| `Anonymous -> expr (mk efun)
| `Named name ->
let name = Utf8_string.of_string_exn name in
mk (sfun (J.ident name))
| `Iife ->
expr
(J.ECall
(mk efun, [ J.EVar (J.ident Constant.global_object_), `Not_spread ], J.N))
| `Iife -> expr (J.call (mk efun) [ J.EVar (J.ident Constant.global_object_) ] J.N)
in
let always_required_js =
(* consider adding a comments in the generated file with original
Expand Down
18 changes: 0 additions & 18 deletions compiler/lib/dune
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,6 @@
--unused-token
T_POUND
--unused-token
T_PLING_PERIOD
--unused-token
T_PLING_PLING
--unused-token
T_OR_ASSIGN
--unused-token
T_AND_ASSIGN
--unused-token
T_NULLISH_ASSIGN
--unused-token
T_EXP
--unused-token
T_EXP_ASSIGN
--unused-token
T_ARROW
--unused-token
T_BIGINT
--unused-token
T_TEMPLATE_PART))

(menhir
Expand Down
Loading

0 comments on commit 77f06f9

Please sign in to comment.