Skip to content

Commit cb06e9b

Browse files
author
Hongbo Zhang
committed
fix bs_ppx # long term: more flexible in ocamlpack
1 parent 44b02f7 commit cb06e9b

File tree

4 files changed

+262
-246
lines changed

4 files changed

+262
-246
lines changed

jscomp/bin/bs_ppx.ml

Lines changed: 107 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(** Bundled by ocamlpack 08/12-16:52 *)
1+
(** Bundled by ocamlpack 08/15-11:16 *)
22
module String_map : sig
33
#1 "string_map.mli"
44
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
@@ -797,6 +797,12 @@ val bs_deriving : string
797797
val bs_deriving_dot : string
798798
val bs_type : string
799799

800+
(** nodejs *)
801+
802+
val node_modules : string
803+
val node_modules_length : int
804+
val package_json : string
805+
800806
end = struct
801807
#1 "literals.ml"
802808
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
@@ -870,6 +876,13 @@ let bs_deriving = "bs.deriving"
870876
let bs_deriving_dot = "bs.deriving."
871877
let bs_type = "bs.type"
872878

879+
880+
(** nodejs *)
881+
let node_modules = "node_modules"
882+
let node_modules_length = String.length "node_modules"
883+
let package_json = "package.json"
884+
885+
873886
end
874887
module Ast_attributes : sig
875888
#1 "ast_attributes.mli"
@@ -1842,7 +1855,7 @@ val to_js_type :
18421855

18431856

18441857
(** TODO: make it work for browser too *)
1845-
val to_js_undefined_type :
1858+
val to_undefined_type :
18461859
Location.t -> Parsetree.core_type -> Parsetree.core_type
18471860

18481861
val to_js_re_type : Location.t -> Parsetree.core_type
@@ -1928,7 +1941,7 @@ let to_js_type loc x =
19281941
let to_js_re_type loc =
19291942
Typ.constr ~loc { txt = re_id ; loc} []
19301943

1931-
let to_js_undefined_type loc x =
1944+
let to_undefined_type loc x =
19321945
Typ.constr ~loc
19331946
{txt = Ast_literal.Lid.js_undefined ; loc}
19341947
[x]
@@ -3237,65 +3250,6 @@ let rec dump r =
32373250
let dump v = dump (Obj.repr v)
32383251

32393252

3240-
end
3241-
module Ext_sys : sig
3242-
#1 "ext_sys.mli"
3243-
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
3244-
*
3245-
* This program is free software: you can redistribute it and/or modify
3246-
* it under the terms of the GNU Lesser General Public License as published by
3247-
* the Free Software Foundation, either version 3 of the License, or
3248-
* (at your option) any later version.
3249-
*
3250-
* In addition to the permissions granted to you by the LGPL, you may combine
3251-
* or link a "work that uses the Library" with a publicly distributed version
3252-
* of this file to produce a combined library or application, then distribute
3253-
* that combined work under the terms of your choosing, with no requirement
3254-
* to comply with the obligations normally placed on you by section 4 of the
3255-
* LGPL version 3 (or the corresponding section of a later version of the LGPL
3256-
* should you choose to use a later version).
3257-
*
3258-
* This program is distributed in the hope that it will be useful,
3259-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
3260-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3261-
* GNU Lesser General Public License for more details.
3262-
*
3263-
* You should have received a copy of the GNU Lesser General Public License
3264-
* along with this program; if not, write to the Free Software
3265-
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
3266-
3267-
val is_directory_no_exn : string -> bool
3268-
3269-
end = struct
3270-
#1 "ext_sys.ml"
3271-
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
3272-
*
3273-
* This program is free software: you can redistribute it and/or modify
3274-
* it under the terms of the GNU Lesser General Public License as published by
3275-
* the Free Software Foundation, either version 3 of the License, or
3276-
* (at your option) any later version.
3277-
*
3278-
* In addition to the permissions granted to you by the LGPL, you may combine
3279-
* or link a "work that uses the Library" with a publicly distributed version
3280-
* of this file to produce a combined library or application, then distribute
3281-
* that combined work under the terms of your choosing, with no requirement
3282-
* to comply with the obligations normally placed on you by section 4 of the
3283-
* LGPL version 3 (or the corresponding section of a later version of the LGPL
3284-
* should you choose to use a later version).
3285-
*
3286-
* This program is distributed in the hope that it will be useful,
3287-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
3288-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3289-
* GNU Lesser General Public License for more details.
3290-
*
3291-
* You should have received a copy of the GNU Lesser General Public License
3292-
* along with this program; if not, write to the Free Software
3293-
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
3294-
3295-
3296-
let is_directory_no_exn f =
3297-
try Sys.is_directory f with _ -> false
3298-
32993253
end
33003254
module Ext_filename : sig
33013255
#1 "ext_filename.mli"
@@ -3356,7 +3310,7 @@ val node_relative_path : t -> [`File of string] -> string
33563310
val chop_extension : ?loc:string -> string -> string
33573311

33583312

3359-
val resolve_bs_package : cwd:string -> string -> string
3313+
33603314

33613315

33623316

@@ -3509,9 +3463,6 @@ let relative_path file_or_dir_1 file_or_dir_2 =
35093463

35103464

35113465

3512-
let node_modules = "node_modules"
3513-
let node_modules_length = String.length "node_modules"
3514-
let package_json = "package.json"
35153466

35163467

35173468

@@ -3526,7 +3477,7 @@ let package_json = "package.json"
35263477
*)
35273478
let node_relative_path (file1 : t)
35283479
(`File file2 as dep_file : [`File of string]) =
3529-
let v = Ext_string.find file2 ~sub:node_modules in
3480+
let v = Ext_string.find file2 ~sub:Literals.node_modules in
35303481
let len = String.length file2 in
35313482
if v >= 0 then
35323483
let rec skip i =
@@ -3547,7 +3498,7 @@ let node_relative_path (file1 : t)
35473498
*)
35483499
in
35493500
Ext_string.tail_from file2
3550-
(skip (v + node_modules_length))
3501+
(skip (v + Literals.node_modules_length))
35513502
else
35523503
relative_path
35533504
(absolute_path dep_file)
@@ -3557,43 +3508,11 @@ let node_relative_path (file1 : t)
35573508

35583509

35593510

3560-
(** [resolve cwd module_name],
3561-
[cwd] is current working directory, absolute path
3562-
Trying to find paths to load [module_name]
3563-
it is sepcialized for option [-bs-package-include] which requires
3564-
[npm_package_name/lib/ocaml]
3565-
*)
3566-
let resolve_bs_package ~cwd name =
3567-
let sub_path = name // "lib" // "ocaml" in
3568-
let rec aux origin cwd name =
3569-
let destdir = cwd // node_modules // sub_path in
3570-
if Ext_sys.is_directory_no_exn destdir then destdir
3571-
else
3572-
let cwd' = Filename.dirname cwd in
3573-
if String.length cwd' < String.length cwd then
3574-
aux origin cwd' name
3575-
else
3576-
try
3577-
let destdir =
3578-
Sys.getenv "npm_config_prefix"
3579-
// "lib" // node_modules // sub_path in
3580-
if Ext_sys.is_directory_no_exn destdir
3581-
then destdir
3582-
else
3583-
Ext_pervasives.failwithf
3584-
~loc:__LOC__ " %s not found in %s" name origin
3585-
3586-
with
3587-
Not_found ->
3588-
Ext_pervasives.failwithf
3589-
~loc:__LOC__ " %s not found in %s" name origin
3590-
in
3591-
aux cwd cwd name
35923511

35933512

35943513
let find_package_json_dir cwd =
35953514
let rec aux cwd =
3596-
if Sys.file_exists (cwd // package_json) then cwd
3515+
if Sys.file_exists (cwd // Literals.package_json) then cwd
35973516
else
35983517
let cwd' = Filename.dirname cwd in
35993518
if String.length cwd' < String.length cwd then
@@ -4804,7 +4723,7 @@ let handle_attributes
48044723
Ldot (Lident "*predef*", "option") },
48054724
[ty])}
48064725
->
4807-
(s, [], Ast_comb.to_js_undefined_type loc ty) :: acc
4726+
(s, [], Ast_comb.to_undefined_type loc ty) :: acc
48084727
| _ -> assert false
48094728
end
48104729
| (_, _), Ast_core_type.Empty -> acc
@@ -6068,23 +5987,35 @@ let rec unsafe_mapper : Ast_mapper.mapper =
60685987
({txt = "bs.node"; loc},
60695988
payload)
60705989
->
5990+
let strip s =
5991+
let len = String.length s in
5992+
if s.[len - 1] = '_' then
5993+
String.sub s 0 (len - 1)
5994+
else s in
60715995
begin match Ast_payload.as_ident payload with
6072-
| Some {txt = Lident ("__filename" | "__dirname" as name); loc}
6073-
->
6074-
6075-
Exp.constraint_ ~loc
6076-
(Ast_util.handle_raw loc
6077-
(Ast_payload.raw_string_payload loc
6078-
name ))
6079-
(Ast_literal.type_string ~loc ())
6080-
| Some {txt = Lident "__module"}
5996+
| Some {txt = Lident
5997+
("__filename"
5998+
| "__dirname"
5999+
| "module_"
6000+
| "require" as name); loc}
60816001
->
6082-
Exp.constraint_ ~loc
6083-
(Ast_util.handle_raw loc
6084-
(Ast_payload.raw_string_payload loc "module"))
6085-
(Typ.constr ~loc
6086-
{ txt = Ldot (Lident "Bs_node", "node_module") ;
6087-
loc} [] )
6002+
let exp =
6003+
Ast_util.handle_raw loc
6004+
(Ast_payload.raw_string_payload loc
6005+
(strip name) ) in
6006+
let typ =
6007+
Ast_comb.to_undefined_type loc @@
6008+
if name = "module_" then
6009+
Typ.constr ~loc
6010+
{ txt = Ldot (Lident "Bs_node", "node_module") ;
6011+
loc} []
6012+
else if name = "require" then
6013+
(Typ.constr ~loc
6014+
{ txt = Ldot (Lident "Bs_node", "node_require") ;
6015+
loc} [] )
6016+
else
6017+
Ast_literal.type_string ~loc () in
6018+
Exp.constraint_ ~loc exp typ
60886019
| Some _ | None -> Location.raise_errorf ~loc "Ilegal payload"
60896020
end
60906021

@@ -6496,3 +6427,62 @@ let () =
64966427
(* end: *)
64976428

64986429
end
6430+
module Ext_sys : sig
6431+
#1 "ext_sys.mli"
6432+
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
6433+
*
6434+
* This program is free software: you can redistribute it and/or modify
6435+
* it under the terms of the GNU Lesser General Public License as published by
6436+
* the Free Software Foundation, either version 3 of the License, or
6437+
* (at your option) any later version.
6438+
*
6439+
* In addition to the permissions granted to you by the LGPL, you may combine
6440+
* or link a "work that uses the Library" with a publicly distributed version
6441+
* of this file to produce a combined library or application, then distribute
6442+
* that combined work under the terms of your choosing, with no requirement
6443+
* to comply with the obligations normally placed on you by section 4 of the
6444+
* LGPL version 3 (or the corresponding section of a later version of the LGPL
6445+
* should you choose to use a later version).
6446+
*
6447+
* This program is distributed in the hope that it will be useful,
6448+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
6449+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6450+
* GNU Lesser General Public License for more details.
6451+
*
6452+
* You should have received a copy of the GNU Lesser General Public License
6453+
* along with this program; if not, write to the Free Software
6454+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
6455+
6456+
val is_directory_no_exn : string -> bool
6457+
6458+
end = struct
6459+
#1 "ext_sys.ml"
6460+
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
6461+
*
6462+
* This program is free software: you can redistribute it and/or modify
6463+
* it under the terms of the GNU Lesser General Public License as published by
6464+
* the Free Software Foundation, either version 3 of the License, or
6465+
* (at your option) any later version.
6466+
*
6467+
* In addition to the permissions granted to you by the LGPL, you may combine
6468+
* or link a "work that uses the Library" with a publicly distributed version
6469+
* of this file to produce a combined library or application, then distribute
6470+
* that combined work under the terms of your choosing, with no requirement
6471+
* to comply with the obligations normally placed on you by section 4 of the
6472+
* LGPL version 3 (or the corresponding section of a later version of the LGPL
6473+
* should you choose to use a later version).
6474+
*
6475+
* This program is distributed in the hope that it will be useful,
6476+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
6477+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6478+
* GNU Lesser General Public License for more details.
6479+
*
6480+
* You should have received a copy of the GNU Lesser General Public License
6481+
* along with this program; if not, write to the Free Software
6482+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
6483+
6484+
6485+
let is_directory_no_exn f =
6486+
try Sys.is_directory f with _ -> false
6487+
6488+
end

jscomp/bin/bs_ppx.mllib

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ include ../syntax/syntax.mllib
1010
../ext/ext_filename
1111
../ext/ext_sys
1212
../ext/ext_ref
13+
../ext/literals
14+
1315
../common/bs_loc
1416
../common/lam_methname
1517
../common/js_config
16-
../common/literals
18+
1719

1820
../bs_ppx_main

0 commit comments

Comments
 (0)