Skip to content

Commit ae5a667

Browse files
authored
Merge pull request #4165 from BuckleScript/code_gen_platform_agnostic
make code gen platform agnostic (fix #4162)
2 parents 6490be4 + ebf2ba3 commit ae5a667

32 files changed

+569
-553
lines changed

jscomp/core/builtin_cmi_datasets.ml

Lines changed: 2 additions & 2 deletions
Large diffs are not rendered by default.

jscomp/core/builtin_cmj_datasets.ml

Lines changed: 4 additions & 4 deletions
Large diffs are not rendered by default.

jscomp/core/lam.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ val switch : t -> lambda_switch -> t
118118
val stringswitch : t -> (string * t) list -> t option -> t
119119

120120
(* val true_ : t *)
121-
(* val false_ : t *)
121+
val false_ : t
122122
val unit : t
123123

124124
(** convert [l || r] to [if l then true else r]*)

jscomp/core/lam_compat.ml

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -147,14 +147,11 @@ type bigarray_layout = Lambda.bigarray_layout =
147147
| Pbigarray_c_layout -> p1 = Pbigarray_c_layout
148148
| Pbigarray_fortran_layout -> p1 = Pbigarray_fortran_layout *)
149149

150-
type compile_time_constant = Lambda.compile_time_constant =
151-
| Big_endian
152-
| Word_size
153-
| Int_size
154-
| Max_wosize
150+
type compile_time_constant =
151+
| Big_endian
155152
| Ostype_unix
156-
| Ostype_win32
157-
| Ostype_cygwin
153+
| Ostype_win32
154+
| Ostype
158155
| Backend_type
159156

160157
(** relies on the fact that [compile_time_constant] is enum type *)

jscomp/core/lam_compat.mli

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,11 @@ type bigarray_layout = Lambda.bigarray_layout =
5252

5353

5454

55-
type compile_time_constant = Lambda.compile_time_constant =
55+
type compile_time_constant =
5656
| Big_endian
57-
| Word_size
58-
| Int_size
59-
| Max_wosize
6057
| Ostype_unix
6158
| Ostype_win32
62-
| Ostype_cygwin
59+
| Ostype
6360
| Backend_type
6461

6562

jscomp/core/lam_compile_primitive.ml

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -587,15 +587,19 @@ let translate loc
587587
| Pctconst ct ->
588588
(match ct with
589589
| Big_endian -> E.bool Sys.big_endian
590-
| Word_size ->
591-
E.small_int Sys.word_size
592-
| Ostype_unix -> E.bool Sys.unix
593-
| Ostype_win32 -> E.bool Sys.win32
594-
| Ostype_cygwin -> E.bool Sys.cygwin
595-
| Int_size -> E.int 32l
596-
| Max_wosize ->
590+
| Ostype ->
591+
(E.runtime_call Js_runtime_modules.sys "os_type" args)
592+
| Ostype_unix ->
593+
E.string_equal
594+
(E.runtime_call Js_runtime_modules.sys "os_type" args)
595+
(E.str "Unix")
596+
| Ostype_win32 ->
597+
E.string_equal
598+
(E.runtime_call Js_runtime_modules.sys "os_type" args)
599+
(E.str "Win32")
600+
(* | Max_wosize ->
597601
(* max_array_length*)
598-
E.int 2147483647l (* 2 ^ 31 - 1 *)
602+
E.int 2147483647l (* 2 ^ 31 - 1 *) *)
599603
(* 4_294_967_295l not representable*)
600604
(* 2 ^ 32 - 1*)
601605
| Backend_type ->

jscomp/core/lam_convert.ml

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -378,9 +378,19 @@ let lam_prim ~primitive:( p : Lambda.primitive) ~args loc : Lam.t =
378378
| Pbigstring_set_64 x -> prim ~primitive:(Pbigstring_set_64 x) ~args loc
379379
| Pctconst x ->
380380
begin match x with
381-
| Word_size ->
382-
Lam.const (Const_int 32)
383-
| _ -> prim ~primitive:(Pctconst x) ~args loc
381+
| Word_size
382+
| Int_size -> Lam.const(Const_int 32)
383+
| Max_wosize -> Lam.const (Const_int 2147483647)
384+
| Big_endian
385+
-> prim ~primitive:(Pctconst Big_endian) ~args loc
386+
| Ostype_unix
387+
-> prim ~primitive:(Pctconst Ostype_unix) ~args loc
388+
| Ostype_win32
389+
-> prim ~primitive:(Pctconst Ostype_win32) ~args loc
390+
| Ostype_cygwin
391+
-> Lam.false_
392+
| Backend_type
393+
-> prim ~primitive:(Pctconst Backend_type) ~args loc
384394
end
385395

386396
| Pbbswap x -> prim ~primitive:(Pbbswap x) ~args loc
@@ -497,8 +507,8 @@ let convert (exports : Set_ident.t) (lam : Lambda.lambda) : Lam.t * Lam_module_i
497507
prim ~primitive:Pdebugger ~args:[] loc
498508
| _ when s = "#null" ->
499509
Lam.const (Const_js_null)
500-
| _ when s = "#os_type" ->
501-
Lam.const (Const_string Sys.os_type)
510+
| _ when s = "#os_type" ->
511+
prim ~primitive:(Pctconst Ostype) ~args:[unit] loc
502512
| _ when s = "#undefined" ->
503513
Lam.const (Const_js_undefined)
504514
| _ when s = "#init_mod" ->

jscomp/core/lam_dispatch_primitive.ml

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -610,19 +610,6 @@ let translate loc (prim_name : string)
610610
| "caml_set_oo_id" (* needed in {!camlinternalOO.set_id} *)
611611
->
612612
Js_of_lam_exception.caml_set_oo_id args
613-
| "caml_sys_const_big_endian" ->
614-
(** return false *)
615-
E.bool Sys.big_endian
616-
| "caml_sys_const_word_size" ->
617-
E.small_int Sys.word_size
618-
(** TODO: How it will affect program behavior *)
619-
| "caml_sys_const_ostype_cygwin" -> E.false_
620-
| "caml_sys_const_ostype_win32" -> E.false_
621-
| "caml_sys_const_ostype_unix" -> E.true_
622-
| "caml_sys_get_config" ->
623-
(** No cross compilation *)
624-
Js_of_lam_tuple.make [E.str Sys.os_type; E.small_int Sys.word_size;
625-
E.bool Sys.big_endian ]
626613
| "caml_sys_get_argv"
627614
(** TODO: refine
628615
Inlined here is helpful for DCE

jscomp/core/lam_print.ml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -232,13 +232,10 @@ let primitive ppf (prim : Lam_primitive.t) = match prim with
232232
| Parraysets -> fprintf ppf "array.set"
233233
| Pctconst c ->
234234
let const_name = match c with
235-
| Big_endian -> "big_endian"
236-
| Word_size -> "word_size"
235+
| Big_endian -> "big_endian"
237236
| Ostype_unix -> "ostype_unix"
238237
| Ostype_win32 -> "ostype_win32"
239-
| Ostype_cygwin -> "ostype_cygwin"
240-
| Int_size -> "int_size"
241-
| Max_wosize -> "max_wosize"
238+
| Ostype -> "ostype"
242239
| Backend_type -> "backend_type"
243240
in
244241
fprintf ppf "sys.constant_%s" const_name

jscomp/runtime/caml_sys.ml

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,19 @@ let caml_sys_getenv s =
3434
| None -> raise Not_found
3535
| Some x -> x
3636

37-
38-
37+
(* https://nodejs.org/dist/latest-v12.x/docs/api/os.html#os_os_platform
38+
The value is set at compile time. Possible values are 'aix', 'darwin','freebsd', 'linux', 'openbsd', 'sunos', and 'win32'.
39+
The return value is equivalent to process.platform.
40+
NodeJS does not support Cygwin very well
41+
*)
42+
let os_type : unit -> string = [%raw{|function(_){
43+
if(typeof process !== 'undefined' && process.platform === 'win32'){
44+
return "Win32"
45+
}
46+
else {
47+
return "Unix"
48+
}
49+
}|}]
3950
(* TODO: improve [js_pass_scope] to avoid remove unused n here *)
4051

4152
external now : unit -> float = "" [@@bs.val "Date.now"]

0 commit comments

Comments
 (0)