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

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 28 additions & 24 deletions jscomp/lam_compile.ml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ module S = J_helper.Stmt

let method_cache_id = ref 1 (*TODO: move to js runtime for re-entrant *)

let add_jmps (ls : (Lam_compile_defs.jbl_label * Lam_compile_defs.value) list)
(m : Lam_compile_defs.value Lam_compile_defs.HandlerMap.t) : Lam_compile_defs.value Lam_compile_defs.HandlerMap.t =
List.fold_left (fun acc (l,s) -> Lam_compile_defs.HandlerMap.add l s acc) m ls

(* assume outer is [Lstaticcatch] *)
let rec flat_catches acc (x : Lambda.lambda)
Expand Down Expand Up @@ -696,10 +693,10 @@ and
end

| Lstaticraise(i, largs) -> (* TODO handlding *largs*)
(* [i] is the jump table, largs is the arguments passed to [Lstaticcatch]*)
(* [i] is the jump table, [largs] is the arguments passed to [Lstaticcatch]*)
begin
match Lam_compile_defs.HandlerMap.find i cxt.jmp_table with
| {exit_id; args } ->
| {exit_id; args ; order_id} ->
let args_code =
(Js_output.concat @@ List.map2 (
fun (x : Lambda.lambda) (arg : Ident.t) ->
Expand All @@ -712,7 +709,7 @@ and
) largs (args : Ident.t list))
in
args_code ++ (* Declared in [Lstaticraise ]*)
Js_output.make [S.assign exit_id (E.int i)]
Js_output.make [S.assign exit_id (E.int order_id)]
~value:(E.undefined ())
| exception Not_found ->
Js_output.make [S.unknown_lambda ~comment:"error" lam]
Expand All @@ -732,11 +729,8 @@ and

let exit_id = Ext_ident.gen_js ~name:"exit" () in
let exit_expr = E.var exit_id in
let code_jmps =
List.map (fun (i,_,bindings) ->
(i, ({exit_id; args = bindings} : Lam_compile_defs.value) )) code_table in
let bindings = Ext_list.flat_map (fun (_,_,bindings) -> bindings) code_table in
let handlers = List.map (fun (i,lam,_) -> (i,lam) ) code_table in

(* compile_list name l false (\*\) *)
(* if exit_code_id == code
handler -- ids are not useful, since
Expand All @@ -749,26 +743,36 @@ and
- another common scenario is that we have nested catch
(catch (catch (catch ..))
*)
(*
checkout example {!Digest.file}, you can not inline handler there,
we can spot such patten and use finally there?
{[
let file filename =
let ic = open_in_bin filename in
match channel ic (-1) with
| d -> close_in ic; d
| exception e -> close_in ic; raise e

]}
*)
(* TODO: handle NeedValue *)
let jmp_table = add_jmps code_jmps jmp_table in
let jmp_table, handlers = Lam_compile_defs.add_jmps (exit_id, code_table) jmp_table in

(* Declaration First, body and handler have the same value *)
(
(* There is a bug in google closure compiler:
(* There is a bug in google closure compiler:
https://github.com/google/closure-compiler/issues/1234#issuecomment-151976340
TODO: wait for a bug fix
*)
let declares =
S.define ~kind:Variable exit_id ~comment:"initialize"
(E.int (cxt.meta.unused_exit_code)) ::
List.map (fun x -> S.declare_variable ~kind:Variable x ) bindings in
let declares =
S.define ~kind:Variable exit_id
(E.int 0) ::
(* we should always make it zero here, since [zero] is reserved in our mapping*)
List.map (fun x -> S.declare_variable ~kind:Variable x ) bindings in

(match st with
begin match st with
(* could be optimized when cases are less than 3 *)
| NeedValue ->
let v = Ext_ident.gen_js (* ~name:"exit_value" *) () in

(* let _ret_value = *)
(* match lbody with {value= Some v; _ } -> v | _ -> assert false in *)
let v = Ext_ident.gen_js () in
let lbody = compile_lambda {cxt with
jmp_table = jmp_table;
st = Assign v
Expand Down Expand Up @@ -803,8 +807,8 @@ and
{cxt with jmp_table = jmp_table}
exit_expr
handlers
NonComplete)))

NonComplete)
end
| Lwhile(p,body) ->
(* Note that ``J.While(expression * statement )``
idealy if ocaml expression does not need fresh variables, we can generate
Expand Down
26 changes: 25 additions & 1 deletion jscomp/lam_compile_defs.ml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ module HandlerMap = Map.Make(struct
let compare x y= compare (x:t) y
end )

type value = { exit_id : Ident.t ; args : Ident.t list }
type value = {
exit_id : Ident.t ;
args : Ident.t list ;
order_id : int
}

(* delegate to the callee to generate expression
Invariant: [output] should return a trailing expression
Expand Down Expand Up @@ -68,3 +72,23 @@ type cxt = {
}

let empty_handler_map = HandlerMap.empty


let add_jmps (exit_id, code_table)
(m : value HandlerMap.t) =
(* always keep key id positive, specifically no [0] generated
*)
let map, _, handlers =
List.fold_left
(fun (acc,prev_order_id, handlers)
(l,lam, args) ->
let order_id = prev_order_id + 1 in
(HandlerMap.add l {exit_id ; args; order_id } acc,
order_id ,
(order_id, lam) :: handlers))
(m,
HandlerMap.cardinal m,
[]
)
code_table in
map, List.rev handlers
15 changes: 13 additions & 2 deletions jscomp/lam_compile_defs.mli
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,13 @@

type jbl_label = int

module HandlerMap : Map.S with type key = jbl_label

type value = { exit_id : Ident.t ; args : Ident.t list }

type value = {
exit_id : Ident.t ;
args : Ident.t list ;
order_id : int
}

type let_kind = Lambda.let_kind

Expand Down Expand Up @@ -61,6 +65,8 @@ type return_type =
Invariant: [output] should return a trailing expression
*)

module HandlerMap : Map.S with type key = jbl_label

type cxt = {
st : st ;
should_return : return_type;
Expand All @@ -69,3 +75,8 @@ type cxt = {
}

val empty_handler_map : value HandlerMap.t

val add_jmps :
Ident.t * (HandlerMap.key * 'a * Ident.t list) list ->
value HandlerMap.t -> value HandlerMap.t * (int * 'a) list

2 changes: 0 additions & 2 deletions jscomp/lam_pass_collect.ml
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,11 @@ let count_alias_globals
{alias_tbl = Hashtbl.create 31 ;
ident_tbl = Hashtbl.create 31;
exit_codes = Hash_set.create 31 ;
unused_exit_code = 0;
exports = export_idents;
required_modules = [] ;
filename;
env;
export_idents
} in
collect_helper meta lam ;
meta.unused_exit_code <- Lam_stats_util.find_unused_exit_code meta.exit_codes;
meta
2 changes: 1 addition & 1 deletion jscomp/lam_stats.ml
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ type meta = {
mutable export_idents : Ident.t list;
alias_tbl : alias_tbl;
exit_codes : int Hash_set.hashset;
mutable unused_exit_code : int ; (* clean up later*)

ident_tbl : ident_tbl;
(** we don't need count arities for all identifiers, for identifiers
for sure it's not a function, there is no need to count them
Expand Down
2 changes: 1 addition & 1 deletion jscomp/lam_stats.mli
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ type meta = {
mutable export_idents : Ident.t list;
alias_tbl : alias_tbl;
exit_codes : int Hash_set.hashset;
mutable unused_exit_code : int ; (* clean up later*)

ident_tbl : ident_tbl;
(** we don't need count arities for all identifiers, for identifiers
for sure it's not a function, there is no need to count them
Expand Down
20 changes: 10 additions & 10 deletions jscomp/stdlib/arg.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ function parse_argv_dynamic($staropt$star, argv, speclist, anonfun, errmsg) {
try {
var treat_action = (function(s){
return function (param) {
/* initialize */var exit = 0;
var exit = 0;
switch (param[0]) {
case 0 :
return param[1](/* () */0);
Expand Down Expand Up @@ -424,7 +424,7 @@ function parse_argv_dynamic($staropt$star, argv, speclist, anonfun, errmsg) {
return ++ current$1[1];
}
else {
exit = 44;
exit = 1;
}
break;
case 2 :
Expand All @@ -439,7 +439,7 @@ function parse_argv_dynamic($staropt$star, argv, speclist, anonfun, errmsg) {
return ++ current$1[1];
}
else {
exit = 44;
exit = 1;
}
break;
case 5 :
Expand All @@ -448,7 +448,7 @@ function parse_argv_dynamic($staropt$star, argv, speclist, anonfun, errmsg) {
return ++ current$1[1];
}
else {
exit = 44;
exit = 1;
}
break;
case 6 :
Expand Down Expand Up @@ -482,7 +482,7 @@ function parse_argv_dynamic($staropt$star, argv, speclist, anonfun, errmsg) {
return ++ current$1[1];
}
else {
exit = 44;
exit = 1;
}
break;
case 7 :
Expand Down Expand Up @@ -516,7 +516,7 @@ function parse_argv_dynamic($staropt$star, argv, speclist, anonfun, errmsg) {
return ++ current$1[1];
}
else {
exit = 44;
exit = 1;
}
break;
case 8 :
Expand Down Expand Up @@ -550,7 +550,7 @@ function parse_argv_dynamic($staropt$star, argv, speclist, anonfun, errmsg) {
return ++ current$1[1];
}
else {
exit = 44;
exit = 1;
}
break;
case 9 :
Expand Down Expand Up @@ -584,7 +584,7 @@ function parse_argv_dynamic($staropt$star, argv, speclist, anonfun, errmsg) {
return ++ current$1[1];
}
else {
exit = 44;
exit = 1;
}
break;
case 10 :
Expand All @@ -611,7 +611,7 @@ function parse_argv_dynamic($staropt$star, argv, speclist, anonfun, errmsg) {
}
}
else {
exit = 44;
exit = 1;
}
break;
case 12 :
Expand All @@ -623,7 +623,7 @@ function parse_argv_dynamic($staropt$star, argv, speclist, anonfun, errmsg) {
return /* () */0;

}
if (exit === 44) {
if (exit === 1) {
throw [
0,
Stop,
Expand Down
20 changes: 10 additions & 10 deletions jscomp/stdlib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,44 +199,44 @@ function advance_to_non_alpha(s, start) {
}
else {
var match = s.charCodeAt(i);
/* initialize */var exit = 0;
var exit = 0;
if (match >= 91) {
if (match >= 97) {
if (match >= 123) {
return i;
}
else {
exit = 14;
exit = 1;
}
}
else {
if (match !== 95) {
return i;
}
else {
exit = 14;
exit = 1;
}
}
}
else {
if (match >= 58) {
if (match >= 65) {
exit = 14;
exit = 1;
}
else {
return i;
}
}
else {
if (match >= 48) {
exit = 14;
exit = 1;
}
else {
return i;
}
}
}
if (exit === 14) {
if (exit === 1) {
_i = i + 1;
}

Expand All @@ -252,7 +252,7 @@ function find_ident(s, start, lim) {
}
else {
var c = s.charCodeAt(start);
/* initialize */var exit = 0;
var exit = 0;
if (c !== 40) {
if (c !== 123) {
var stop = advance_to_non_alpha(s, start + 1);
Expand All @@ -263,13 +263,13 @@ function find_ident(s, start, lim) {
];
}
else {
exit = 11;
exit = 1;
}
}
else {
exit = 11;
exit = 1;
}
if (exit === 11) {
if (exit === 1) {
var new_start = start + 1;
var stop$1 = advance_to_closing(c, closing(c), 0, s, new_start);
return [
Expand Down
Loading