Skip to content

Commit

Permalink
feature: add an option for -bs-no-any-assert
Browse files Browse the repository at this point in the history
  • Loading branch information
bobzhang committed Jul 2, 2016
1 parent 5191e1f commit 1ec4aff
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 8 deletions.
1 change: 0 additions & 1 deletion jscomp/.gitignore
@@ -1 +0,0 @@
*.js
3 changes: 3 additions & 0 deletions jscomp/cmd_tests/build.sh
@@ -0,0 +1,3 @@
#!/bin/sh
set -e
bsc -I ../runtime -I ../stdlib -bs-no-any-assert -bs-files *.ml *.mli
27 changes: 27 additions & 0 deletions jscomp/cmd_tests/test_assert_false.js
@@ -0,0 +1,27 @@
// GENERATED CODE BY BUCKLESCRIPT VERSION 0.6.2 , PLEASE EDIT WITH CARE
'use strict';

var Caml_builtin_exceptions = require("bs-platform/lib/js/caml_builtin_exceptions");

function f(x) {
if (x) {
var match = x[1];
if (match) {
if (match[1]) {
return 3;
}
else {
return 2;
}
}
else {
return /* impossible branch */0;
}
}
else {
throw Caml_builtin_exceptions.not_found;
}
}

exports.f = f;
/* No side effect */
7 changes: 7 additions & 0 deletions jscomp/cmd_tests/test_assert_false.ml
@@ -0,0 +1,7 @@


let f x = match x with
| [ _ ; _] -> 2
| [_] -> assert false
| [] -> raise Not_found
| _ -> assert (1 = 2); 3
1 change: 1 addition & 0 deletions jscomp/cmd_tests/test_assert_false.mli
@@ -0,0 +1 @@
val f : 'a list -> int
6 changes: 5 additions & 1 deletion jscomp/common/js_config.ml
Expand Up @@ -262,8 +262,12 @@ let is_same_file () =
!debug_file <> "" && !debug_file = !current_file

let tool_name = "BuckleScript"
let check_div_by_zero = ref true

let check_div_by_zero = ref true
let get_check_div_by_zero () = !check_div_by_zero

let no_any_assert = ref false

let set_no_any_assert () = no_any_assert := true
let get_no_any_assert () = !no_any_assert

9 changes: 9 additions & 0 deletions jscomp/common/js_config.mli
Expand Up @@ -106,3 +106,12 @@ val tool_name : string
val check_div_by_zero : bool ref

val get_check_div_by_zero : unit -> bool

(* It will imply [-noassert] be set too, note from the implmentation point of view,
in the lambda layer, it is impossible to tell whehther it is [assert (3 <> 2)] or
[if (3<>2) then assert false]
*)
val no_any_assert : bool ref

val set_no_any_assert : unit -> unit
val get_no_any_assert : unit -> bool
17 changes: 12 additions & 5 deletions jscomp/js_main.ml
Expand Up @@ -58,6 +58,10 @@ let add_include_path s =
else
Ext_pervasives.failwithf ~loc:__LOC__ "%s is not a directory" s

let set_noassert () =
Js_config.set_no_any_assert ();
Clflags.noassert := true


let buckle_script_flags =
("-bs-npm-output-path", Arg.String Js_config.set_npm_package_path,
Expand All @@ -79,6 +83,9 @@ let buckle_script_flags =
" More verbose output")
:: ("-bs-no-check-div-by-zero", Arg.Clear Js_config.check_div_by_zero,
" unsafe mode, don't check div by zero and mod by zero")
:: ("-bs-no-any-assert", Arg.Unit set_noassert,
" no code containing any assertion"
)
:: ("-bs-files", Arg.Rest collect_file,
" Provide batch of files, the compiler will sort it before compiling"
)
Expand All @@ -88,11 +95,13 @@ let buckle_script_flags =
:: Ocaml_options.mk__ anonymous
:: Ocaml_options.ocaml_options

let () =



let _ =
Clflags.unsafe_string := false;
Clflags.debug := true
Clflags.debug := true;

let main () =
try
Compenv.readenv ppf Before_args;
Arg.parse buckle_script_flags anonymous usage;
Expand All @@ -102,8 +111,6 @@ let main () =
Location.report_exception ppf x;
exit 2

let _ = main ()




16 changes: 15 additions & 1 deletion jscomp/lam.ml
Expand Up @@ -299,6 +299,8 @@ let false_ : t =
let unit : t =
Lconst (Const_pointer( 0, Pt_constructor "()"))

let assert_false_unit : t =
Lconst (Const_pointer( 0, Pt_constructor "impossible branch"))

(** [l || r ] *)
let sequor l r = if_ l true_ r
Expand Down Expand Up @@ -584,7 +586,19 @@ let lam_prim ~primitive:(p : Lambda.primitive) ~args : t =
| _ ->
prim ~primitive:(Pccall a) ~args
end
| Praise _ -> prim ~primitive:Praise ~args
| Praise _ ->
if Js_config.get_no_any_assert () then
begin match args with
| [Lprim {primitive = Pmakeblock (0, _, _) ;
args = [
Lprim {primitive = Pgetglobal ({name = "Assert_failure"} as id); args = []};
_
]
} ] when Ident.global id
-> assert_false_unit
| _ -> prim ~primitive:Praise ~args
end
else prim ~primitive:Praise ~args
| Psequand -> prim ~primitive:Psequand ~args
| Psequor -> prim ~primitive:Psequor ~args
| Pnot -> prim ~primitive:Pnot ~args
Expand Down

0 comments on commit 1ec4aff

Please sign in to comment.