Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions jscomp/bin/all_ounit_tests.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3529,6 +3529,7 @@ val js_array_ctor : string
val js_type_number : string
val js_type_string : string
val js_type_object : string
val js_type_boolean : string
val js_undefined : string
val js_prop_length : string

Expand Down Expand Up @@ -3663,6 +3664,7 @@ let js_array_ctor = "Array"
let js_type_number = "number"
let js_type_string = "string"
let js_type_object = "object"
let js_type_boolean = "boolean"
let js_undefined = "undefined"
let js_prop_length = "length"

Expand Down Expand Up @@ -6895,9 +6897,9 @@ val make_unused : unit -> Ident.t
val convert : string -> string


val undefined : Ident.t

val is_js_or_global : Ident.t -> bool
val nil : Ident.t



val compare : Ident.t -> Ident.t -> int
Expand Down Expand Up @@ -7219,9 +7221,6 @@ let reset () =
String_hashtbl.clear js_module_table


let undefined = create_js "undefined"
let nil = create_js "null"

(* Has to be total order, [x < y]
and [x > y] should be consistent
flags are not relevant here
Expand Down
2 changes: 1 addition & 1 deletion jscomp/core/bs_conditional_initial.ml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

let setup_env () =
#if BS_DEBUG then
Js_config.set_debug_file "pipe_syntax.ml";
Js_config.set_debug_file "gpr_2700_test.ml";
#end
Lexer.replace_directive_bool "BS" true;
Lexer.replace_directive_string "BS_VERSION" Bs_version.version
Expand Down
63 changes: 4 additions & 59 deletions jscomp/core/j.ml
Original file line number Diff line number Diff line change
Expand Up @@ -102,68 +102,17 @@ and expression_desc =
| Length of expression * length_object
| Char_of_int of expression
| Char_to_int of expression
| Is_null_undefined_to_boolean of expression
| Is_null_or_undefined of expression
(** where we use a trick [== null ] *)
| Array_of_size of expression
(* used in [#create_array] primitive, note having
uninitilized array is not as bad as in ocaml,
since GC does not rely on it
*)
| Array_copy of expression (* shallow copy, like [x.slice] *)
| Array_append of expression * expression (* For [caml_array_append]*)
(* | Tag_ml_obj of expression *)
| String_append of expression * expression

| Anything_to_number of expression
| String_append of expression * expression
| Bool of bool (* js true/false*)
(* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence
[typeof] is an operator
*)
| Typeof of expression
| Js_not of expression (* !v *)
| String_of_small_int_array of expression
(* String.fromCharCode.apply(null, args) *)
(* Convert JS boolean into OCaml boolean
like [+true], note this ast talks using js
terminnology unless explicity stated
*)
| Json_stringify of expression
(* TODO: in the future, it might make sense to group primitivie by type,
which makes optimizations easier
{[ JSON.stringify(value, replacer[, space]) ]}
*)
| Anything_to_string of expression
(* for debugging utitlites,
TODO: [Dump] is not necessary with this primitive
Note that the semantics is slightly different from [JSON.stringify]
{[
JSON.stringify("x")
]}
{[
""x""
]}
{[
JSON.stringify(undefined)
]}
{[
undefined
]}
{[ '' + undefined
]}
{[ 'undefined'
]}
*)
| Dump of Js_op.level * expression list
(* TODO:
add
{[ Assert of bool * expression ]}
*)
(* to support
val log1 : 'a -> unit
val log2 : 'a -> 'b -> unit
val log3 : 'a -> 'b -> 'c -> unit
*)

(* TODO: Add some primitives so that [js inliner] can do a better job *)
| Seq of expression * expression
| Cond of expression * expression * expression
Expand All @@ -178,11 +127,6 @@ and expression_desc =
if it's know at compile time, we can turn it into
f(args[0], args[1], ... )
*)
| Bind of expression * expression
(* {[ Bind (a,b) ]}
is literally
{[ a.bind(b) ]}
*)
| Call of expression * expression list * Js_call_info.t
(* Analysze over J expression is hard since,
some primitive call is translated
Expand Down Expand Up @@ -250,7 +194,8 @@ and expression_desc =
*)
| Number of number
| Object of property_map

| Undefined
| Null
and for_ident_expression = expression (* pure*)

and finish_ident_expression = expression (* pure *)
Expand Down
33 changes: 6 additions & 27 deletions jscomp/core/js_analyzer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,15 @@ let free_variables_of_expression used_idents defined_idents st =

let rec no_side_effect_expression_desc (x : J.expression_desc) =
match x with
| Undefined
| Null
| Bool _
| Var _
| Unicode _ -> true
| Fun _ -> true
| Number _ -> true (* Can be refined later *)
| Access (a,b) -> no_side_effect a && no_side_effect b
| Is_null_undefined_to_boolean b -> no_side_effect b
| Is_null_or_undefined b -> no_side_effect b
| Str (b,_) -> b
| Array (xs,_mutable_flag)
| Caml_block (xs, _mutable_flag, _, _)
Expand All @@ -100,10 +102,8 @@ let rec no_side_effect_expression_desc (x : J.expression_desc) =
the block is mutable does not mean this operation is non-pure
*)
List.for_all no_side_effect xs
| Bind(fn, obj) -> no_side_effect fn && no_side_effect obj
| Object kvs ->
List.for_all (fun (_property_name, y) -> no_side_effect y ) kvs
| Array_append (a,b)
| String_append (a,b)
| Seq (a,b) -> no_side_effect a && no_side_effect b
| Length (e, _)
Expand All @@ -115,15 +115,8 @@ let rec no_side_effect_expression_desc (x : J.expression_desc) =
| Bin (op, a, b) ->
op <> Eq && no_side_effect a && no_side_effect b
| Math _
| Array_of_size _
| Array_copy _
(* | Tag_ml_obj _ *)
| J.Anything_to_number _
| Js_not _
| String_of_small_int_array _
| Json_stringify _
| Anything_to_string _
| Dump _
| Cond _

| FlatCall _
Expand Down Expand Up @@ -184,6 +177,8 @@ let rec eq_expression
({expression_desc = x0} : J.expression)
({expression_desc = y0} : J.expression) =
begin match x0 with
| Null -> y0 = Null
| Undefined -> y0 = Undefined
| Number (Int i) ->
begin match y0 with
| Number (Int j) -> i = j
Expand Down Expand Up @@ -244,12 +239,6 @@ let rec eq_expression
p0 = p1 && b0 = b1 && eq_expression e0 e1
| _ -> false
end
| Dump (l0,es0) ->
begin match y0 with
| Dump(l1,es1) ->
l0 = l1 && eq_expression_list es0 es1
| _ -> false
end
| Seq (a0,b0) ->
begin match y0 with
| Seq(a1,b1) ->
Expand All @@ -264,23 +253,13 @@ let rec eq_expression
| Length _
| Char_of_int _
| Char_to_int _
| Is_null_undefined_to_boolean _
| Array_of_size _
| Is_null_or_undefined _
| Array_copy _
| Array_append _
| String_append _
| Anything_to_number _

| Typeof _
| Js_not _
| String_of_small_int_array _
| Json_stringify _
| Anything_to_string _


| Cond _
| FlatCall _
| Bind _
| String_access _

| New _
Expand Down
Loading